Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2 | * Generic process-grouping system. |
| 3 | * |
| 4 | * Based originally on the cpuset system, extracted by Paul Menage |
| 5 | * Copyright (C) 2006 Google, Inc |
| 6 | * |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 7 | * Notifications support |
| 8 | * Copyright (C) 2009 Nokia Corporation |
| 9 | * Author: Kirill A. Shutemov |
| 10 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 11 | * Copyright notices from the original cpuset code: |
| 12 | * -------------------------------------------------- |
| 13 | * Copyright (C) 2003 BULL SA. |
| 14 | * Copyright (C) 2004-2006 Silicon Graphics, Inc. |
| 15 | * |
| 16 | * Portions derived from Patrick Mochel's sysfs code. |
| 17 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
| 18 | * |
| 19 | * 2003-10-10 Written by Simon Derr. |
| 20 | * 2003-10-22 Updates by Stephen Hemminger. |
| 21 | * 2004 May-July Rework by Paul Jackson. |
| 22 | * --------------------------------------------------- |
| 23 | * |
| 24 | * This file is subject to the terms and conditions of the GNU General Public |
| 25 | * License. See the file COPYING in the main directory of the Linux |
| 26 | * distribution for more details. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/cgroup.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 30 | #include <linux/cred.h> |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 31 | #include <linux/ctype.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 33 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 34 | #include <linux/kernel.h> |
| 35 | #include <linux/list.h> |
| 36 | #include <linux/mm.h> |
| 37 | #include <linux/mutex.h> |
| 38 | #include <linux/mount.h> |
| 39 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 40 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 41 | #include <linux/rcupdate.h> |
| 42 | #include <linux/sched.h> |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 43 | #include <linux/backing-dev.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 44 | #include <linux/seq_file.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include <linux/magic.h> |
| 47 | #include <linux/spinlock.h> |
| 48 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 49 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 50 | #include <linux/kmod.h> |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 51 | #include <linux/module.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 52 | #include <linux/delayacct.h> |
| 53 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 54 | #include <linux/hashtable.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 55 | #include <linux/namei.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 56 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 57 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 58 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 59 | #include <linux/eventfd.h> |
| 60 | #include <linux/poll.h> |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 61 | #include <linux/flex_array.h> /* used in cgroup_attach_task */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 62 | #include <linux/kthread.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 63 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 64 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 65 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 66 | /* |
| 67 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 68 | * hierarchy must be performed while holding it. |
| 69 | * |
| 70 | * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify |
| 71 | * cgroupfs_root of any cgroup hierarchy - subsys list, flags, |
| 72 | * release_agent_path and so on. Modifying requires both cgroup_mutex and |
| 73 | * cgroup_root_mutex. Readers can acquire either of the two. This is to |
| 74 | * break the following locking order cycle. |
| 75 | * |
| 76 | * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem |
| 77 | * B. namespace_sem -> cgroup_mutex |
| 78 | * |
| 79 | * B happens only through cgroup_show_options() and using cgroup_root_mutex |
| 80 | * breaks it. |
| 81 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_PROVE_RCU |
| 83 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 84 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 85 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 86 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 87 | #endif |
| 88 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 89 | static DEFINE_MUTEX(cgroup_root_mutex); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 90 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 91 | /* |
| 92 | * Generate an array of cgroup subsystem pointers. At boot time, this is |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 93 | * populated with the built in subsystems, and modular subsystems are |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 94 | * registered after that. The mutable section of this array is protected by |
| 95 | * cgroup_mutex. |
| 96 | */ |
Daniel Wagner | 80f4c87 | 2012-09-12 16:12:06 +0200 | [diff] [blame] | 97 | #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, |
Daniel Wagner | 5fc0b02 | 2012-09-12 16:12:05 +0200 | [diff] [blame] | 98 | #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 99 | static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 100 | #include <linux/cgroup_subsys.h> |
| 101 | }; |
| 102 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 103 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 104 | * The dummy hierarchy, reserved for the subsystems that are otherwise |
| 105 | * unattached - it never has more than a single cgroup, and all tasks are |
| 106 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 107 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 108 | static struct cgroupfs_root cgroup_dummy_root; |
| 109 | |
| 110 | /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ |
| 111 | static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 112 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 113 | /* |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 114 | * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. |
| 115 | */ |
| 116 | struct cfent { |
| 117 | struct list_head node; |
| 118 | struct dentry *dentry; |
| 119 | struct cftype *type; |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 120 | struct cgroup_subsys_state *css; |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 121 | |
| 122 | /* file xattrs */ |
| 123 | struct simple_xattrs xattrs; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | /* |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 127 | * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when |
| 128 | * cgroup_subsys->use_id != 0. |
| 129 | */ |
| 130 | #define CSS_ID_MAX (65535) |
| 131 | struct css_id { |
| 132 | /* |
| 133 | * The css to which this ID points. This pointer is set to valid value |
| 134 | * after cgroup is populated. If cgroup is removed, this will be NULL. |
| 135 | * This pointer is expected to be RCU-safe because destroy() |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 136 | * is called after synchronize_rcu(). But for safe use, css_tryget() |
| 137 | * should be used for avoiding race. |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 138 | */ |
Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 139 | struct cgroup_subsys_state __rcu *css; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 140 | /* |
| 141 | * ID of this css. |
| 142 | */ |
| 143 | unsigned short id; |
| 144 | /* |
| 145 | * Depth in hierarchy which this ID belongs to. |
| 146 | */ |
| 147 | unsigned short depth; |
| 148 | /* |
| 149 | * ID is freed by RCU. (and lookup routine is RCU safe.) |
| 150 | */ |
| 151 | struct rcu_head rcu_head; |
| 152 | /* |
| 153 | * Hierarchy of CSS ID belongs to. |
| 154 | */ |
| 155 | unsigned short stack[0]; /* Array of Length (depth+1) */ |
| 156 | }; |
| 157 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 158 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 159 | * cgroup_event represents events which userspace want to receive. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 160 | */ |
| 161 | struct cgroup_event { |
| 162 | /* |
Tejun Heo | 81eeaf0 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 163 | * css which the event belongs to. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 164 | */ |
Tejun Heo | 81eeaf0 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 165 | struct cgroup_subsys_state *css; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 166 | /* |
| 167 | * Control file which the event associated. |
| 168 | */ |
| 169 | struct cftype *cft; |
| 170 | /* |
| 171 | * eventfd to signal userspace about the event. |
| 172 | */ |
| 173 | struct eventfd_ctx *eventfd; |
| 174 | /* |
| 175 | * Each of these stored in a list by the cgroup. |
| 176 | */ |
| 177 | struct list_head list; |
| 178 | /* |
| 179 | * All fields below needed to unregister event when |
| 180 | * userspace closes eventfd. |
| 181 | */ |
| 182 | poll_table pt; |
| 183 | wait_queue_head_t *wqh; |
| 184 | wait_queue_t wait; |
| 185 | struct work_struct remove; |
| 186 | }; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 187 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 188 | /* The list of hierarchy roots */ |
| 189 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 190 | static LIST_HEAD(cgroup_roots); |
| 191 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 192 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 193 | /* |
| 194 | * Hierarchy ID allocation and mapping. It follows the same exclusion |
| 195 | * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for |
| 196 | * writes, either for reads. |
| 197 | */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 198 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 199 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 200 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 201 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 202 | /* |
| 203 | * Assign a monotonically increasing serial number to cgroups. It |
| 204 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 205 | * numbers. Also, as cgroups are always appended to the parent's |
| 206 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 207 | * the ascending serial number order on the list. Protected by |
| 208 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 209 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 210 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 211 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 212 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 213 | * check for fork/exit handlers to call. This avoids us having to do |
| 214 | * extra work in the fork/exit path if none of the subsystems need to |
| 215 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 216 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 217 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 218 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 219 | static struct cftype cgroup_base_files[]; |
| 220 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 221 | static void cgroup_destroy_css_killed(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 222 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 223 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 224 | bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 225 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 226 | /** |
| 227 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 228 | * @cgrp: the cgroup of interest |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 229 | * @ss: the subsystem of interest (%NULL returns the dummy_css) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 230 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 231 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 232 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 233 | * the caller is responsible for pinning the returned css if it wants to |
| 234 | * keep accessing it outside the said locks. This function may return |
| 235 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 236 | */ |
| 237 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 238 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 239 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 240 | if (ss) |
| 241 | return rcu_dereference_check(cgrp->subsys[ss->subsys_id], |
| 242 | lockdep_is_held(&cgroup_mutex)); |
| 243 | else |
| 244 | return &cgrp->dummy_css; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 247 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 248 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 249 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 250 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 253 | /** |
| 254 | * cgroup_is_descendant - test ancestry |
| 255 | * @cgrp: the cgroup to be tested |
| 256 | * @ancestor: possible ancestor of @cgrp |
| 257 | * |
| 258 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 259 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 260 | * and @ancestor are accessible. |
| 261 | */ |
| 262 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 263 | { |
| 264 | while (cgrp) { |
| 265 | if (cgrp == ancestor) |
| 266 | return true; |
| 267 | cgrp = cgrp->parent; |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 272 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 273 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 274 | { |
| 275 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 276 | (1 << CGRP_RELEASABLE) | |
| 277 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 278 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 281 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 282 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 283 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 286 | /** |
| 287 | * for_each_subsys - iterate all loaded cgroup subsystems |
| 288 | * @ss: the iteration cursor |
| 289 | * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
| 290 | * |
| 291 | * Should be called under cgroup_mutex. |
| 292 | */ |
| 293 | #define for_each_subsys(ss, i) \ |
| 294 | for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \ |
| 295 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 296 | !((ss) = cgroup_subsys[i]); })) { } \ |
| 297 | else |
| 298 | |
| 299 | /** |
| 300 | * for_each_builtin_subsys - iterate all built-in cgroup subsystems |
| 301 | * @ss: the iteration cursor |
| 302 | * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end |
| 303 | * |
| 304 | * Bulit-in subsystems are always present and iteration itself doesn't |
| 305 | * require any synchronization. |
| 306 | */ |
| 307 | #define for_each_builtin_subsys(ss, i) \ |
| 308 | for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \ |
| 309 | (((ss) = cgroup_subsys[i]) || true); (i)++) |
| 310 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 311 | /* iterate each subsystem attached to a hierarchy */ |
| 312 | #define for_each_root_subsys(root, ss) \ |
| 313 | list_for_each_entry((ss), &(root)->subsys_list, sibling) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 314 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 315 | /* iterate across the active hierarchies */ |
| 316 | #define for_each_active_root(root) \ |
| 317 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 318 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 319 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 320 | { |
| 321 | return dentry->d_fsdata; |
| 322 | } |
| 323 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 324 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 325 | { |
| 326 | return dentry->d_fsdata; |
| 327 | } |
| 328 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 329 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 330 | { |
| 331 | return __d_cfe(dentry)->type; |
| 332 | } |
| 333 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 334 | /** |
| 335 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 336 | * @cgrp: the cgroup to be checked for liveness |
| 337 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 338 | * On success, returns true; the mutex should be later unlocked. On |
| 339 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 340 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 341 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 342 | { |
| 343 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 344 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 345 | mutex_unlock(&cgroup_mutex); |
| 346 | return false; |
| 347 | } |
| 348 | return true; |
| 349 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 350 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 351 | /* the list of cgroups eligible for automatic release. Protected by |
| 352 | * release_list_lock */ |
| 353 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 354 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 355 | static void cgroup_release_agent(struct work_struct *work); |
| 356 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 357 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 358 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 359 | /* |
| 360 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 361 | * belong to different cgroups on different hierarchies. In the other |
| 362 | * direction, a css_set is naturally associated with multiple cgroups. |
| 363 | * This M:N relationship is represented by the following link structure |
| 364 | * which exists for each association and allows traversing the associations |
| 365 | * from both sides. |
| 366 | */ |
| 367 | struct cgrp_cset_link { |
| 368 | /* the cgroup and css_set this link associates */ |
| 369 | struct cgroup *cgrp; |
| 370 | struct css_set *cset; |
| 371 | |
| 372 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 373 | struct list_head cset_link; |
| 374 | |
| 375 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 376 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | /* The default css_set - used by init and its children prior to any |
| 380 | * hierarchies being mounted. It contains a pointer to the root state |
| 381 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 382 | * reference-counted, to improve performance when child cgroups |
| 383 | * haven't been created. |
| 384 | */ |
| 385 | |
| 386 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 387 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 388 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 389 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 390 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 391 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 392 | /* |
| 393 | * css_set_lock protects the list of css_set objects, and the chain of |
| 394 | * tasks off each css_set. Nests outside task->alloc_lock due to |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 395 | * css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 396 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 397 | static DEFINE_RWLOCK(css_set_lock); |
| 398 | static int css_set_count; |
| 399 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 400 | /* |
| 401 | * hash table for cgroup groups. This improves the performance to find |
| 402 | * an existing css_set. This hash doesn't (currently) take into |
| 403 | * account cgroups in empty hierarchies. |
| 404 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 405 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 406 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 407 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 408 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 409 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 410 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 411 | struct cgroup_subsys *ss; |
| 412 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 413 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 414 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 415 | key += (unsigned long)css[i]; |
| 416 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 417 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 418 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 421 | /* |
| 422 | * We don't maintain the lists running through each css_set to its task |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 423 | * until after the first call to css_task_iter_start(). This reduces the |
| 424 | * fork()/exit() overhead for people who have cgroups compiled into their |
| 425 | * kernel but not actually in use. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 426 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 427 | static int use_task_css_set_links __read_mostly; |
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 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 430 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 431 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 432 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 433 | /* |
| 434 | * Ensure that the refcount doesn't hit zero while any readers |
| 435 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 436 | * rwlock |
| 437 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 438 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 439 | return; |
| 440 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 441 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 442 | write_unlock(&css_set_lock); |
| 443 | return; |
| 444 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 445 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 446 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 447 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 448 | css_set_count--; |
| 449 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 450 | 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] | 451 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 452 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 453 | list_del(&link->cset_link); |
| 454 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 455 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 456 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 457 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 458 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 459 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 460 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 461 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 462 | |
| 463 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 464 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 465 | |
| 466 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 467 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* |
| 471 | * refcounted get/put for css_set objects |
| 472 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 473 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 474 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 475 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 478 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 479 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 480 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 483 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 484 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 485 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 488 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 489 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 490 | * @cset: candidate css_set being tested |
| 491 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 492 | * @new_cgrp: cgroup that's being entered by the task |
| 493 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 494 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 495 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 496 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 497 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 498 | static bool compare_css_sets(struct css_set *cset, |
| 499 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 500 | struct cgroup *new_cgrp, |
| 501 | struct cgroup_subsys_state *template[]) |
| 502 | { |
| 503 | struct list_head *l1, *l2; |
| 504 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 505 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 506 | /* Not all subsystems matched */ |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Compare cgroup pointers in order to distinguish between |
| 512 | * different cgroups in heirarchies with no subsystems. We |
| 513 | * could get by with just this check alone (and skip the |
| 514 | * memcmp above) but on most setups the memcmp check will |
| 515 | * avoid the need for this more expensive check on almost all |
| 516 | * candidates. |
| 517 | */ |
| 518 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 519 | l1 = &cset->cgrp_links; |
| 520 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 521 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 522 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 523 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 524 | |
| 525 | l1 = l1->next; |
| 526 | l2 = l2->next; |
| 527 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 528 | if (l1 == &cset->cgrp_links) { |
| 529 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 530 | break; |
| 531 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 532 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 533 | } |
| 534 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 535 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 536 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 537 | cgrp1 = link1->cgrp; |
| 538 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 539 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 540 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 541 | |
| 542 | /* |
| 543 | * If this hierarchy is the hierarchy of the cgroup |
| 544 | * that's changing, then we need to check that this |
| 545 | * css_set points to the new cgroup; if it's any other |
| 546 | * hierarchy, then this css_set should point to the |
| 547 | * same cgroup as the old css_set. |
| 548 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 549 | if (cgrp1->root == new_cgrp->root) { |
| 550 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 551 | return false; |
| 552 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 553 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 554 | return false; |
| 555 | } |
| 556 | } |
| 557 | return true; |
| 558 | } |
| 559 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 560 | /** |
| 561 | * find_existing_css_set - init css array and find the matching css_set |
| 562 | * @old_cset: the css_set that we're using before the cgroup transition |
| 563 | * @cgrp: the cgroup that we're moving into |
| 564 | * @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] | 565 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 566 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 567 | struct cgroup *cgrp, |
| 568 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 569 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 570 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 571 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 572 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 573 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 574 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 575 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 576 | /* |
| 577 | * Build the set of subsystem state objects that we want to see in the |
| 578 | * new css_set. while subsystems can change globally, the entries here |
| 579 | * won't change, so no need for locking. |
| 580 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 581 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 582 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 583 | /* Subsystem is in this hierarchy. So we want |
| 584 | * the subsystem state from the new |
| 585 | * cgroup */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 586 | template[i] = cgroup_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 587 | } else { |
| 588 | /* Subsystem is not in this hierarchy, so we |
| 589 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 590 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 594 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 595 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 596 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 597 | continue; |
| 598 | |
| 599 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 600 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 601 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 602 | |
| 603 | /* No existing cgroup group matched */ |
| 604 | return NULL; |
| 605 | } |
| 606 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 607 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 608 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 609 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 610 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 611 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 612 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 613 | kfree(link); |
| 614 | } |
| 615 | } |
| 616 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 617 | /** |
| 618 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 619 | * @count: the number of links to allocate |
| 620 | * @tmp_links: list_head the allocated links are put on |
| 621 | * |
| 622 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 623 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 624 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 625 | 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] | 626 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 627 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 628 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 629 | |
| 630 | INIT_LIST_HEAD(tmp_links); |
| 631 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 632 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 633 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 634 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 635 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 636 | return -ENOMEM; |
| 637 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 638 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 639 | } |
| 640 | return 0; |
| 641 | } |
| 642 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 643 | /** |
| 644 | * 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] | 645 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 646 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 647 | * @cgrp: the destination cgroup |
| 648 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 649 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 650 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 651 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 652 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 653 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 654 | BUG_ON(list_empty(tmp_links)); |
| 655 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 656 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 657 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 658 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 659 | /* |
| 660 | * Always add links to the tail of the list so that the list |
| 661 | * is sorted by order of hierarchy creation |
| 662 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 663 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 666 | /** |
| 667 | * find_css_set - return a new css_set with one cgroup updated |
| 668 | * @old_cset: the baseline css_set |
| 669 | * @cgrp: the cgroup to be updated |
| 670 | * |
| 671 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 672 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 673 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 674 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 675 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 676 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 677 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 678 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 679 | struct list_head tmp_links; |
| 680 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 681 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 682 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 683 | lockdep_assert_held(&cgroup_mutex); |
| 684 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 685 | /* First see if we already have a cgroup group that matches |
| 686 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 687 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 688 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 689 | if (cset) |
| 690 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 691 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 692 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 693 | if (cset) |
| 694 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 695 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 696 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 697 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 698 | return NULL; |
| 699 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 700 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 701 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 702 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 703 | return NULL; |
| 704 | } |
| 705 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 706 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 707 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 708 | INIT_LIST_HEAD(&cset->tasks); |
| 709 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 710 | |
| 711 | /* Copy the set of subsystem state objects generated in |
| 712 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 713 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 714 | |
| 715 | write_lock(&css_set_lock); |
| 716 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 717 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 718 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 719 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 720 | if (c->root == cgrp->root) |
| 721 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 722 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 723 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 724 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 725 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 726 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 727 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 728 | |
| 729 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 730 | key = css_set_hash(cset->subsys); |
| 731 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 732 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 733 | write_unlock(&css_set_lock); |
| 734 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 735 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 738 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 739 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 740 | * called with cgroup_mutex held. |
| 741 | */ |
| 742 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 743 | struct cgroupfs_root *root) |
| 744 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 745 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 746 | struct cgroup *res = NULL; |
| 747 | |
| 748 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 749 | read_lock(&css_set_lock); |
| 750 | /* |
| 751 | * No need to lock the task - since we hold cgroup_mutex the |
| 752 | * task can't change groups, so the only thing that can happen |
| 753 | * is that it exits and its css is set back to init_css_set. |
| 754 | */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 755 | cset = task_css_set(task); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 756 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 757 | res = &root->top_cgroup; |
| 758 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 759 | struct cgrp_cset_link *link; |
| 760 | |
| 761 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 762 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 763 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 764 | if (c->root == root) { |
| 765 | res = c; |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | read_unlock(&css_set_lock); |
| 771 | BUG_ON(!res); |
| 772 | return res; |
| 773 | } |
| 774 | |
| 775 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 776 | * There is one global cgroup mutex. We also require taking |
| 777 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 778 | * See "The task_lock() exception", at the end of this comment. |
| 779 | * |
| 780 | * A task must hold cgroup_mutex to modify cgroups. |
| 781 | * |
| 782 | * Any task can increment and decrement the count field without lock. |
| 783 | * So in general, code holding cgroup_mutex can't rely on the count |
| 784 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 785 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 786 | * means that no tasks are currently attached, therefore there is no |
| 787 | * way a task attached to that cgroup can fork (the other way to |
| 788 | * increment the count). So code holding cgroup_mutex can safely |
| 789 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 790 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 791 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 792 | * needs that mutex. |
| 793 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 794 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 795 | * (usually) take cgroup_mutex. These are the two most performance |
| 796 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 797 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 798 | * 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] | 799 | * to the release agent with the name of the cgroup (path relative to |
| 800 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 801 | * |
| 802 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 803 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 804 | * tasks in the system use _some_ cgroup, and since there is always at |
| 805 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 806 | * always has either children cgroups and/or using tasks. So we don't |
| 807 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 808 | * |
| 809 | * The task_lock() exception |
| 810 | * |
| 811 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 812 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 813 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 814 | * several performance critical places that need to reference |
| 815 | * task->cgroup without the expense of grabbing a system global |
| 816 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 817 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 818 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 819 | * the task_struct routinely used for such matters. |
| 820 | * |
| 821 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 822 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 823 | */ |
| 824 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 825 | /* |
| 826 | * A couple of forward declarations required, due to cyclic reference loop: |
| 827 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 828 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 829 | * -> cgroup_mkdir. |
| 830 | */ |
| 831 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 832 | 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] | 833 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 834 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 835 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 836 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 837 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 838 | |
| 839 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 840 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 841 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 842 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 843 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 844 | static int alloc_css_id(struct cgroup_subsys_state *child_css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 845 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 846 | 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] | 847 | { |
| 848 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 849 | |
| 850 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 851 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 852 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 853 | inode->i_uid = current_fsuid(); |
| 854 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 855 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 856 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 857 | } |
| 858 | return inode; |
| 859 | } |
| 860 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 861 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 862 | { |
| 863 | struct cgroup_name *name; |
| 864 | |
| 865 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 866 | if (!name) |
| 867 | return NULL; |
| 868 | strcpy(name->name, dentry->d_name.name); |
| 869 | return name; |
| 870 | } |
| 871 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 872 | static void cgroup_free_fn(struct work_struct *work) |
| 873 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 874 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 875 | |
| 876 | mutex_lock(&cgroup_mutex); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 877 | cgrp->root->number_of_cgroups--; |
| 878 | mutex_unlock(&cgroup_mutex); |
| 879 | |
| 880 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 881 | * We get a ref to the parent's dentry, and put the ref when |
| 882 | * this cgroup is being freed, so it's guaranteed that the |
| 883 | * parent won't be destroyed before its children. |
| 884 | */ |
| 885 | dput(cgrp->parent->dentry); |
| 886 | |
| 887 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 888 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 889 | * created the cgroup. This will free cgrp->root, if we are |
| 890 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 891 | */ |
| 892 | deactivate_super(cgrp->root->sb); |
| 893 | |
| 894 | /* |
| 895 | * if we're getting rid of the cgroup, refcount should ensure |
| 896 | * that there are no pidlists left. |
| 897 | */ |
| 898 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 899 | |
| 900 | simple_xattrs_free(&cgrp->xattrs); |
| 901 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 902 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 903 | kfree(cgrp); |
| 904 | } |
| 905 | |
| 906 | static void cgroup_free_rcu(struct rcu_head *head) |
| 907 | { |
| 908 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 909 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 910 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
| 911 | schedule_work(&cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 912 | } |
| 913 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 914 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 915 | { |
| 916 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 917 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 918 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 919 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 920 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 921 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 922 | } else { |
| 923 | struct cfent *cfe = __d_cfe(dentry); |
| 924 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 925 | |
| 926 | WARN_ONCE(!list_empty(&cfe->node) && |
| 927 | cgrp != &cgrp->root->top_cgroup, |
| 928 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 929 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 930 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 931 | } |
| 932 | iput(inode); |
| 933 | } |
| 934 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 935 | static int cgroup_delete(const struct dentry *d) |
| 936 | { |
| 937 | return 1; |
| 938 | } |
| 939 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 940 | static void remove_dir(struct dentry *d) |
| 941 | { |
| 942 | struct dentry *parent = dget(d->d_parent); |
| 943 | |
| 944 | d_delete(d); |
| 945 | simple_rmdir(parent->d_inode, d); |
| 946 | dput(parent); |
| 947 | } |
| 948 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 949 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 950 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 951 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 952 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 953 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 954 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 955 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 956 | /* |
| 957 | * If we're doing cleanup due to failure of cgroup_create(), |
| 958 | * the corresponding @cfe may not exist. |
| 959 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 960 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 961 | struct dentry *d = cfe->dentry; |
| 962 | |
| 963 | if (cft && cfe->type != cft) |
| 964 | continue; |
| 965 | |
| 966 | dget(d); |
| 967 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 968 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 969 | list_del_init(&cfe->node); |
| 970 | dput(d); |
| 971 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 972 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 973 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 976 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 977 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 978 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 979 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 980 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 981 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 982 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 983 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 984 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 985 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 986 | for_each_subsys(ss, i) { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 987 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 988 | |
| 989 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 990 | continue; |
| 991 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 992 | cgroup_addrm_files(cgrp, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 993 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* |
| 997 | * NOTE : the dentry must have been dget()'ed |
| 998 | */ |
| 999 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 1000 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1001 | struct dentry *parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1002 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1003 | parent = dentry->d_parent; |
| 1004 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 1005 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1006 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1007 | spin_unlock(&dentry->d_lock); |
| 1008 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1009 | remove_dir(dentry); |
| 1010 | } |
| 1011 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 1012 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1013 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 1014 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 1015 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1016 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1017 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1018 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1019 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1020 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1021 | struct cgroup_subsys *ss; |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1022 | unsigned long pinned = 0; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1023 | int i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1024 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1025 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1026 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1027 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1028 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1029 | for_each_subsys(ss, i) { |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1030 | if (!(added_mask & (1 << i))) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1031 | continue; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1032 | |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1033 | /* is the subsystem mounted elsewhere? */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1034 | if (ss->root != &cgroup_dummy_root) { |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1035 | ret = -EBUSY; |
| 1036 | goto out_put; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1037 | } |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1038 | |
| 1039 | /* pin the module */ |
| 1040 | if (!try_module_get(ss->module)) { |
| 1041 | ret = -ENOENT; |
| 1042 | goto out_put; |
| 1043 | } |
| 1044 | pinned |= 1 << i; |
| 1045 | } |
| 1046 | |
| 1047 | /* subsys could be missing if unloaded between parsing and here */ |
| 1048 | if (added_mask != pinned) { |
| 1049 | ret = -ENOENT; |
| 1050 | goto out_put; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1053 | ret = cgroup_populate_dir(cgrp, added_mask); |
| 1054 | if (ret) |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1055 | goto out_put; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1056 | |
| 1057 | /* |
| 1058 | * Nothing can fail from this point on. Remove files for the |
| 1059 | * removed subsystems and rebind each subsystem. |
| 1060 | */ |
| 1061 | cgroup_clear_dir(cgrp, removed_mask); |
| 1062 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1063 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1064 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1065 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1066 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1067 | /* We're binding this subsystem to this hierarchy */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1068 | BUG_ON(cgroup_css(cgrp, ss)); |
| 1069 | BUG_ON(!cgroup_css(cgroup_dummy_top, ss)); |
| 1070 | BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1071 | |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1072 | rcu_assign_pointer(cgrp->subsys[i], |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1073 | cgroup_css(cgroup_dummy_top, ss)); |
| 1074 | cgroup_css(cgrp, ss)->cgroup = cgrp; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1075 | |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1076 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1077 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1078 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1079 | ss->bind(cgroup_css(cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1080 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1081 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1082 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1083 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1084 | /* We're removing this subsystem */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1085 | BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss)); |
| 1086 | BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1087 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1088 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1089 | ss->bind(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1090 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1091 | cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1092 | RCU_INIT_POINTER(cgrp->subsys[i], NULL); |
| 1093 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1094 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
| 1095 | list_move(&ss->sibling, &cgroup_dummy_root.subsys_list); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1096 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1097 | /* subsystem is now free - drop reference on module */ |
| 1098 | module_put(ss->module); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1099 | root->subsys_mask &= ~bit; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1100 | } |
| 1101 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1102 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1103 | /* |
| 1104 | * Mark @root has finished binding subsystems. @root->subsys_mask |
| 1105 | * now matches the bound subsystems. |
| 1106 | */ |
| 1107 | root->flags |= CGRP_ROOT_SUBSYS_BOUND; |
| 1108 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1109 | return 0; |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1110 | |
| 1111 | out_put: |
| 1112 | for_each_subsys(ss, i) |
| 1113 | if (pinned & (1 << i)) |
| 1114 | module_put(ss->module); |
| 1115 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1118 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1119 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1120 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1121 | struct cgroup_subsys *ss; |
| 1122 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1123 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1124 | for_each_root_subsys(root, ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1125 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1126 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1127 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1128 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1129 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1130 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1131 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1132 | if (strlen(root->release_agent_path)) |
| 1133 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1134 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1135 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1136 | if (strlen(root->name)) |
| 1137 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1138 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1143 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1144 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1145 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1146 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1147 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1148 | /* User explicitly requested empty subsystem */ |
| 1149 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1150 | |
| 1151 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1152 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1153 | }; |
| 1154 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1155 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1156 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1157 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1158 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1159 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1160 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1161 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1162 | { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1163 | char *token, *o = data; |
| 1164 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1165 | unsigned long mask = (unsigned long)-1; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1166 | struct cgroup_subsys *ss; |
| 1167 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1168 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1169 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1170 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1171 | #ifdef CONFIG_CPUSETS |
| 1172 | mask = ~(1UL << cpuset_subsys_id); |
| 1173 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1174 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1175 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1176 | |
| 1177 | while ((token = strsep(&o, ",")) != NULL) { |
| 1178 | if (!*token) |
| 1179 | return -EINVAL; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1180 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1181 | /* Explicitly have no subsystems */ |
| 1182 | opts->none = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1183 | continue; |
| 1184 | } |
| 1185 | if (!strcmp(token, "all")) { |
| 1186 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1187 | if (one_ss) |
| 1188 | return -EINVAL; |
| 1189 | all_ss = true; |
| 1190 | continue; |
| 1191 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1192 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1193 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1194 | continue; |
| 1195 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1196 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1197 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1198 | continue; |
| 1199 | } |
| 1200 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1201 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1202 | continue; |
| 1203 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1204 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1205 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1206 | continue; |
| 1207 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1208 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1209 | /* Specifying two release agents is forbidden */ |
| 1210 | if (opts->release_agent) |
| 1211 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1212 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1213 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1214 | if (!opts->release_agent) |
| 1215 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1216 | continue; |
| 1217 | } |
| 1218 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1219 | const char *name = token + 5; |
| 1220 | /* Can't specify an empty name */ |
| 1221 | if (!strlen(name)) |
| 1222 | return -EINVAL; |
| 1223 | /* Must match [\w.-]+ */ |
| 1224 | for (i = 0; i < strlen(name); i++) { |
| 1225 | char c = name[i]; |
| 1226 | if (isalnum(c)) |
| 1227 | continue; |
| 1228 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1229 | continue; |
| 1230 | return -EINVAL; |
| 1231 | } |
| 1232 | /* Specifying two names is forbidden */ |
| 1233 | if (opts->name) |
| 1234 | return -EINVAL; |
| 1235 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1236 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1237 | GFP_KERNEL); |
| 1238 | if (!opts->name) |
| 1239 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1240 | |
| 1241 | continue; |
| 1242 | } |
| 1243 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1244 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1245 | if (strcmp(token, ss->name)) |
| 1246 | continue; |
| 1247 | if (ss->disabled) |
| 1248 | continue; |
| 1249 | |
| 1250 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1251 | if (all_ss) |
| 1252 | return -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1253 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1254 | one_ss = true; |
| 1255 | |
| 1256 | break; |
| 1257 | } |
| 1258 | if (i == CGROUP_SUBSYS_COUNT) |
| 1259 | return -ENOENT; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1264 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1265 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1266 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1267 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1268 | for_each_subsys(ss, i) |
| 1269 | if (!ss->disabled) |
| 1270 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1271 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1272 | /* Consistency checks */ |
| 1273 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1274 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1275 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1276 | |
| 1277 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1278 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1279 | return -EINVAL; |
| 1280 | } |
| 1281 | |
| 1282 | if (opts->cpuset_clone_children) { |
| 1283 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1284 | return -EINVAL; |
| 1285 | } |
| 1286 | } |
| 1287 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1288 | /* |
| 1289 | * Option noprefix was introduced just for backward compatibility |
| 1290 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1291 | * the cpuset subsystem. |
| 1292 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1293 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1294 | return -EINVAL; |
| 1295 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1296 | |
| 1297 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1298 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1299 | return -EINVAL; |
| 1300 | |
| 1301 | /* |
| 1302 | * We either have to specify by name or by subsystems. (So all |
| 1303 | * empty hierarchies must have a name). |
| 1304 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1305 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1306 | return -EINVAL; |
| 1307 | |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1312 | { |
| 1313 | int ret = 0; |
| 1314 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1315 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1316 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1317 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1318 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1319 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1320 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1321 | return -EINVAL; |
| 1322 | } |
| 1323 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1324 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1325 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1326 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1327 | |
| 1328 | /* See what subsystems are wanted */ |
| 1329 | ret = parse_cgroupfs_options(data, &opts); |
| 1330 | if (ret) |
| 1331 | goto out_unlock; |
| 1332 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1333 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1334 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1335 | task_tgid_nr(current), current->comm); |
| 1336 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1337 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1338 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1339 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1340 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1341 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1342 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1343 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1344 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1345 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1346 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1347 | goto out_unlock; |
| 1348 | } |
| 1349 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1350 | /* remounting is not allowed for populated hierarchies */ |
| 1351 | if (root->number_of_cgroups > 1) { |
| 1352 | ret = -EBUSY; |
| 1353 | goto out_unlock; |
| 1354 | } |
| 1355 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1356 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1357 | if (ret) |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1358 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1359 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1360 | if (opts.release_agent) |
| 1361 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1362 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1363 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1364 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1365 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1366 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1367 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1368 | return ret; |
| 1369 | } |
| 1370 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1371 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1372 | .statfs = simple_statfs, |
| 1373 | .drop_inode = generic_delete_inode, |
| 1374 | .show_options = cgroup_show_options, |
| 1375 | .remount_fs = cgroup_remount, |
| 1376 | }; |
| 1377 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1378 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1379 | { |
| 1380 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1381 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1382 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1383 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1384 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1385 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1386 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1387 | cgrp->dummy_css.cgroup = cgrp; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1388 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1389 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1390 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1391 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1392 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1393 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1394 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1395 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1396 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1397 | INIT_LIST_HEAD(&root->subsys_list); |
| 1398 | INIT_LIST_HEAD(&root->root_list); |
| 1399 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1400 | cgrp->root = root; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 1401 | RCU_INIT_POINTER(cgrp->name, &root_cgroup_name); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1402 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1403 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1406 | static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1407 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1408 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1409 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1410 | lockdep_assert_held(&cgroup_mutex); |
| 1411 | lockdep_assert_held(&cgroup_root_mutex); |
| 1412 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1413 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 1414 | GFP_KERNEL); |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1415 | if (id < 0) |
| 1416 | return id; |
| 1417 | |
| 1418 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1419 | return 0; |
| 1420 | } |
| 1421 | |
| 1422 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1423 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1424 | lockdep_assert_held(&cgroup_mutex); |
| 1425 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1426 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1427 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1428 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1429 | root->hierarchy_id = 0; |
| 1430 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1433 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1434 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1435 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1436 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1437 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1438 | /* If we asked for a name then it must match */ |
| 1439 | if (opts->name && strcmp(opts->name, root->name)) |
| 1440 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1441 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1442 | /* |
| 1443 | * If we asked for subsystems (or explicitly for no |
| 1444 | * subsystems) then they must match |
| 1445 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1446 | if ((opts->subsys_mask || opts->none) |
| 1447 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1448 | return 0; |
| 1449 | |
| 1450 | return 1; |
| 1451 | } |
| 1452 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1453 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1454 | { |
| 1455 | struct cgroupfs_root *root; |
| 1456 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1457 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1458 | return NULL; |
| 1459 | |
| 1460 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1461 | if (!root) |
| 1462 | return ERR_PTR(-ENOMEM); |
| 1463 | |
| 1464 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1465 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1466 | /* |
| 1467 | * We need to set @root->subsys_mask now so that @root can be |
| 1468 | * matched by cgroup_test_super() before it finishes |
| 1469 | * initialization; otherwise, competing mounts with the same |
| 1470 | * options may try to bind the same subsystems instead of waiting |
| 1471 | * for the first one leading to unexpected mount errors. |
| 1472 | * SUBSYS_BOUND will be set once actual binding is complete. |
| 1473 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1474 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1475 | root->flags = opts->flags; |
| 1476 | if (opts->release_agent) |
| 1477 | strcpy(root->release_agent_path, opts->release_agent); |
| 1478 | if (opts->name) |
| 1479 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1480 | if (opts->cpuset_clone_children) |
| 1481 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1482 | return root; |
| 1483 | } |
| 1484 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1485 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1486 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1487 | if (root) { |
| 1488 | /* hierarhcy ID shoulid already have been released */ |
| 1489 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1490 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1491 | idr_destroy(&root->cgroup_idr); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1492 | kfree(root); |
| 1493 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1496 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1497 | { |
| 1498 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1499 | struct cgroup_sb_opts *opts = data; |
| 1500 | |
| 1501 | /* If we don't have a new root, we can't set up a new sb */ |
| 1502 | if (!opts->new_root) |
| 1503 | return -EINVAL; |
| 1504 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1505 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1506 | |
| 1507 | ret = set_anon_super(sb, NULL); |
| 1508 | if (ret) |
| 1509 | return ret; |
| 1510 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1511 | sb->s_fs_info = opts->new_root; |
| 1512 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1513 | |
| 1514 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1515 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1516 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1517 | sb->s_op = &cgroup_ops; |
| 1518 | |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
| 1522 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1523 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1524 | static const struct dentry_operations cgroup_dops = { |
| 1525 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1526 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1527 | }; |
| 1528 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1529 | struct inode *inode = |
| 1530 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1531 | |
| 1532 | if (!inode) |
| 1533 | return -ENOMEM; |
| 1534 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1535 | inode->i_fop = &simple_dir_operations; |
| 1536 | inode->i_op = &cgroup_dir_inode_operations; |
| 1537 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1538 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1539 | sb->s_root = d_make_root(inode); |
| 1540 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1541 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1542 | /* for everything else we want ->d_op set */ |
| 1543 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1544 | return 0; |
| 1545 | } |
| 1546 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1547 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1548 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1549 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1550 | { |
| 1551 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1552 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1553 | int ret = 0; |
| 1554 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1555 | struct cgroupfs_root *new_root; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1556 | struct list_head tmp_links; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1557 | struct inode *inode; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1558 | const struct cred *cred; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1559 | |
| 1560 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1561 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1562 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1563 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1564 | if (ret) |
| 1565 | goto out_err; |
| 1566 | |
| 1567 | /* |
| 1568 | * Allocate a new cgroup root. We may not need it if we're |
| 1569 | * reusing an existing hierarchy. |
| 1570 | */ |
| 1571 | new_root = cgroup_root_from_opts(&opts); |
| 1572 | if (IS_ERR(new_root)) { |
| 1573 | ret = PTR_ERR(new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1574 | goto out_err; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1575 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1576 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1577 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1578 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1579 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1580 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1581 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1582 | cgroup_free_root(opts.new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1583 | goto out_err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1586 | root = sb->s_fs_info; |
| 1587 | BUG_ON(!root); |
| 1588 | if (root == opts.new_root) { |
| 1589 | /* We used the new root structure, so this is a new hierarchy */ |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1590 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1591 | struct cgroupfs_root *existing_root; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1592 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1593 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1594 | |
| 1595 | BUG_ON(sb->s_root != NULL); |
| 1596 | |
| 1597 | ret = cgroup_get_rootdir(sb); |
| 1598 | if (ret) |
| 1599 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1600 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1601 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1602 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1603 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1604 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1605 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1606 | root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp, |
| 1607 | 0, 1, GFP_KERNEL); |
| 1608 | if (root_cgrp->id < 0) |
| 1609 | goto unlock_drop; |
| 1610 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1611 | /* Check for name clashes with existing mounts */ |
| 1612 | ret = -EBUSY; |
| 1613 | if (strlen(root->name)) |
| 1614 | for_each_active_root(existing_root) |
| 1615 | if (!strcmp(existing_root->name, root->name)) |
| 1616 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1617 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1618 | /* |
| 1619 | * We're accessing css_set_count without locking |
| 1620 | * css_set_lock here, but that's OK - it can only be |
| 1621 | * increased by someone holding cgroup_lock, and |
| 1622 | * that's us. The worst that can happen is that we |
| 1623 | * have some link structures left over |
| 1624 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1625 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1626 | if (ret) |
| 1627 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1628 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1629 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1630 | ret = cgroup_init_root_id(root, 2, 0); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1631 | if (ret) |
| 1632 | goto unlock_drop; |
| 1633 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1634 | sb->s_root->d_fsdata = root_cgrp; |
| 1635 | root_cgrp->dentry = sb->s_root; |
| 1636 | |
| 1637 | /* |
| 1638 | * We're inside get_sb() and will call lookup_one_len() to |
| 1639 | * create the root files, which doesn't work if SELinux is |
| 1640 | * in use. The following cred dancing somehow works around |
| 1641 | * it. See 2ce9738ba ("cgroupfs: use init_cred when |
| 1642 | * populating new cgroupfs mount") for more details. |
| 1643 | */ |
| 1644 | cred = override_creds(&init_cred); |
| 1645 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1646 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1647 | if (ret) |
| 1648 | goto rm_base_files; |
| 1649 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1650 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1651 | if (ret) |
| 1652 | goto rm_base_files; |
| 1653 | |
| 1654 | revert_creds(cred); |
| 1655 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1656 | /* |
| 1657 | * There must be no failure case after here, since rebinding |
| 1658 | * takes care of subsystems' refcounts, which are explicitly |
| 1659 | * dropped in the failure exit path. |
| 1660 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1661 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1662 | list_add(&root->root_list, &cgroup_roots); |
| 1663 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1664 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1665 | /* Link the top cgroup in this hierarchy into all |
| 1666 | * the css_set objects */ |
| 1667 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1668 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1669 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1670 | write_unlock(&css_set_lock); |
| 1671 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1672 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1673 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1674 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1675 | BUG_ON(root->number_of_cgroups != 1); |
| 1676 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1677 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1678 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1679 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1680 | } else { |
| 1681 | /* |
| 1682 | * We re-used an existing hierarchy - the new root (if |
| 1683 | * any) is not needed |
| 1684 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1685 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1686 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1687 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1688 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1689 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1690 | ret = -EINVAL; |
| 1691 | goto drop_new_super; |
| 1692 | } else { |
| 1693 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1694 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1695 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1698 | kfree(opts.release_agent); |
| 1699 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1700 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1701 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1702 | rm_base_files: |
| 1703 | free_cgrp_cset_links(&tmp_links); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1704 | cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1705 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1706 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1707 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1708 | mutex_unlock(&cgroup_root_mutex); |
| 1709 | mutex_unlock(&cgroup_mutex); |
| 1710 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1711 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1712 | deactivate_locked_super(sb); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1713 | out_err: |
| 1714 | kfree(opts.release_agent); |
| 1715 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1716 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1720 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1721 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1722 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1723 | int ret; |
| 1724 | |
| 1725 | BUG_ON(!root); |
| 1726 | |
| 1727 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1728 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1729 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1730 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1731 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1732 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1733 | |
| 1734 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1735 | if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { |
| 1736 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
| 1737 | /* Shouldn't be able to fail ... */ |
| 1738 | BUG_ON(ret); |
| 1739 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1740 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1741 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1742 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1743 | * root cgroup |
| 1744 | */ |
| 1745 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1746 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1747 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1748 | list_del(&link->cset_link); |
| 1749 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1750 | kfree(link); |
| 1751 | } |
| 1752 | write_unlock(&css_set_lock); |
| 1753 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1754 | if (!list_empty(&root->root_list)) { |
| 1755 | list_del(&root->root_list); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1756 | cgroup_root_count--; |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1757 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1758 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1759 | cgroup_exit_root_id(root); |
| 1760 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1761 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1762 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1763 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1764 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1765 | simple_xattrs_free(&cgrp->xattrs); |
| 1766 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1767 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1768 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | static struct file_system_type cgroup_fs_type = { |
| 1772 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1773 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1774 | .kill_sb = cgroup_kill_sb, |
| 1775 | }; |
| 1776 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1777 | static struct kobject *cgroup_kobj; |
| 1778 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1779 | /** |
| 1780 | * cgroup_path - generate the path of a cgroup |
| 1781 | * @cgrp: the cgroup in question |
| 1782 | * @buf: the buffer to write the path into |
| 1783 | * @buflen: the length of the buffer |
| 1784 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1785 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1786 | * |
| 1787 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1788 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1789 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1790 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1791 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1792 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1793 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1794 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1795 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1796 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1797 | if (!cgrp->parent) { |
| 1798 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1799 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1800 | return 0; |
| 1801 | } |
| 1802 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1803 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1804 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1805 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1806 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1807 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1808 | const char *name = cgroup_name(cgrp); |
| 1809 | int len; |
| 1810 | |
| 1811 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1812 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1813 | goto out; |
| 1814 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1815 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1816 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1817 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1818 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1819 | |
| 1820 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1821 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1822 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1823 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1824 | out: |
| 1825 | rcu_read_unlock(); |
| 1826 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1827 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1828 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1829 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1830 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1831 | * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1832 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1833 | * @buf: the buffer to write the path into |
| 1834 | * @buflen: the length of the buffer |
| 1835 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1836 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1837 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1838 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1839 | * cgroup controller callbacks. |
| 1840 | * |
| 1841 | * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short. |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1842 | */ |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1843 | int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1844 | { |
| 1845 | struct cgroupfs_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1846 | struct cgroup *cgrp; |
| 1847 | int hierarchy_id = 1, ret = 0; |
| 1848 | |
| 1849 | if (buflen < 2) |
| 1850 | return -ENAMETOOLONG; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1851 | |
| 1852 | mutex_lock(&cgroup_mutex); |
| 1853 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1854 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1855 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1856 | if (root) { |
| 1857 | cgrp = task_cgroup_from_root(task, root); |
| 1858 | ret = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1859 | } else { |
| 1860 | /* if no hierarchy exists, everyone is in "/" */ |
| 1861 | memcpy(buf, "/", 2); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1865 | return ret; |
| 1866 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1867 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1868 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1869 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1870 | * Control Group taskset |
| 1871 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1872 | struct task_and_cgroup { |
| 1873 | struct task_struct *task; |
| 1874 | struct cgroup *cgrp; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1875 | struct css_set *cset; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1876 | }; |
| 1877 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1878 | struct cgroup_taskset { |
| 1879 | struct task_and_cgroup single; |
| 1880 | struct flex_array *tc_array; |
| 1881 | int tc_array_len; |
| 1882 | int idx; |
| 1883 | struct cgroup *cur_cgrp; |
| 1884 | }; |
| 1885 | |
| 1886 | /** |
| 1887 | * cgroup_taskset_first - reset taskset and return the first task |
| 1888 | * @tset: taskset of interest |
| 1889 | * |
| 1890 | * @tset iteration is initialized and the first task is returned. |
| 1891 | */ |
| 1892 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1893 | { |
| 1894 | if (tset->tc_array) { |
| 1895 | tset->idx = 0; |
| 1896 | return cgroup_taskset_next(tset); |
| 1897 | } else { |
| 1898 | tset->cur_cgrp = tset->single.cgrp; |
| 1899 | return tset->single.task; |
| 1900 | } |
| 1901 | } |
| 1902 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1903 | |
| 1904 | /** |
| 1905 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1906 | * @tset: taskset of interest |
| 1907 | * |
| 1908 | * Return the next task in @tset. Iteration must have been initialized |
| 1909 | * with cgroup_taskset_first(). |
| 1910 | */ |
| 1911 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1912 | { |
| 1913 | struct task_and_cgroup *tc; |
| 1914 | |
| 1915 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1916 | return NULL; |
| 1917 | |
| 1918 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1919 | tset->cur_cgrp = tc->cgrp; |
| 1920 | return tc->task; |
| 1921 | } |
| 1922 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1923 | |
| 1924 | /** |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1925 | * cgroup_taskset_cur_css - return the matching css for the current task |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1926 | * @tset: taskset of interest |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1927 | * @subsys_id: the ID of the target subsystem |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1928 | * |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1929 | * Return the css for the current (last returned) task of @tset for |
| 1930 | * subsystem specified by @subsys_id. This function must be preceded by |
| 1931 | * either cgroup_taskset_first() or cgroup_taskset_next(). |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1932 | */ |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1933 | struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset, |
| 1934 | int subsys_id) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1935 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1936 | return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1937 | } |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1938 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1939 | |
| 1940 | /** |
| 1941 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1942 | * @tset: taskset of interest |
| 1943 | */ |
| 1944 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1945 | { |
| 1946 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1947 | } |
| 1948 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1949 | |
| 1950 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1951 | /* |
| 1952 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1953 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1954 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1955 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1956 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1957 | struct task_struct *tsk, |
| 1958 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1959 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1960 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1961 | |
| 1962 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1963 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1964 | * setting such that we can't race against cgroup_exit() changing the |
| 1965 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1966 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1967 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1968 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1969 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1970 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1971 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1972 | task_unlock(tsk); |
| 1973 | |
| 1974 | /* Update the css_set linked lists if we're using them */ |
| 1975 | write_lock(&css_set_lock); |
| 1976 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1977 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1978 | write_unlock(&css_set_lock); |
| 1979 | |
| 1980 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1981 | * We just gained a reference on old_cset by taking it from the |
| 1982 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1983 | * 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] | 1984 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1985 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1986 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1989 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1990 | * 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] | 1991 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1992 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1993 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1994 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1995 | * 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] | 1996 | * 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] | 1997 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1998 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1999 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2000 | { |
| 2001 | int retval, i, group_size; |
| 2002 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2003 | struct cgroupfs_root *root = cgrp->root; |
| 2004 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2005 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2006 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2007 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2008 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2009 | |
| 2010 | /* |
| 2011 | * step 0: in order to do expensive, possibly blocking operations for |
| 2012 | * every thread, we cannot iterate the thread group list, since it needs |
| 2013 | * 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] | 2014 | * group - group_rwsem prevents new threads from appearing, and if |
| 2015 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2016 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2017 | if (threadgroup) |
| 2018 | group_size = get_nr_threads(tsk); |
| 2019 | else |
| 2020 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2021 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2022 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2023 | if (!group) |
| 2024 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2025 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2026 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2027 | if (retval) |
| 2028 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2029 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2030 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2031 | /* |
| 2032 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2033 | * already PF_EXITING could be freed from underneath us unless we |
| 2034 | * take an rcu_read_lock. |
| 2035 | */ |
| 2036 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2037 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2038 | struct task_and_cgroup ent; |
| 2039 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2040 | /* @tsk either already exited or can't exit until the end */ |
| 2041 | if (tsk->flags & PF_EXITING) |
| 2042 | continue; |
| 2043 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2044 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2045 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2046 | ent.task = tsk; |
| 2047 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2048 | /* nothing to do if this task is already in the cgroup */ |
| 2049 | if (ent.cgrp == cgrp) |
| 2050 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2051 | /* |
| 2052 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2053 | * earlier, but it's good form to communicate our expectations. |
| 2054 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2055 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2056 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2057 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2058 | |
| 2059 | if (!threadgroup) |
| 2060 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2061 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2062 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2063 | /* remember the number of threads in the array for later. */ |
| 2064 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2065 | tset.tc_array = group; |
| 2066 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2067 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2068 | /* methods shouldn't be called if no task is actually migrating */ |
| 2069 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2070 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2071 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2072 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2073 | /* |
| 2074 | * step 1: check that we can legitimately attach to the cgroup. |
| 2075 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2076 | for_each_root_subsys(root, ss) { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2077 | struct cgroup_subsys_state *css = cgroup_css(cgrp, ss); |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2078 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2079 | if (ss->can_attach) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2080 | retval = ss->can_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2081 | if (retval) { |
| 2082 | failed_ss = ss; |
| 2083 | goto out_cancel_attach; |
| 2084 | } |
| 2085 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | /* |
| 2089 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2090 | * we use find_css_set, which allocates a new one if necessary. |
| 2091 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2092 | for (i = 0; i < group_size; i++) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2093 | struct css_set *old_cset; |
| 2094 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2095 | tc = flex_array_get(group, i); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2096 | old_cset = task_css_set(tc->task); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2097 | tc->cset = find_css_set(old_cset, cgrp); |
| 2098 | if (!tc->cset) { |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2099 | retval = -ENOMEM; |
| 2100 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2105 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2106 | * proceed to move all tasks to the new cgroup. There are no |
| 2107 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2108 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2109 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2110 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2111 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2112 | } |
| 2113 | /* nothing is sensitive to fork() after this point. */ |
| 2114 | |
| 2115 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2116 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2117 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2118 | for_each_root_subsys(root, ss) { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2119 | struct cgroup_subsys_state *css = cgroup_css(cgrp, ss); |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2120 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2121 | if (ss->attach) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2122 | ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | /* |
| 2126 | * step 5: success! and cleanup |
| 2127 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2128 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2129 | out_put_css_set_refs: |
| 2130 | if (retval) { |
| 2131 | for (i = 0; i < group_size; i++) { |
| 2132 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2133 | if (!tc->cset) |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2134 | break; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2135 | put_css_set(tc->cset); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2136 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2137 | } |
| 2138 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2139 | if (retval) { |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2140 | for_each_root_subsys(root, ss) { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2141 | struct cgroup_subsys_state *css = cgroup_css(cgrp, ss); |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2142 | |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2143 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2144 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2145 | if (ss->cancel_attach) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2146 | ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2147 | } |
| 2148 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2149 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2150 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2151 | return retval; |
| 2152 | } |
| 2153 | |
| 2154 | /* |
| 2155 | * 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] | 2156 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2157 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2158 | */ |
| 2159 | 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] | 2160 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2161 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2162 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2163 | int ret; |
| 2164 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2165 | if (!cgroup_lock_live_group(cgrp)) |
| 2166 | return -ENODEV; |
| 2167 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2168 | retry_find_task: |
| 2169 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2170 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2171 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2172 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2173 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2174 | ret= -ESRCH; |
| 2175 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2176 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2177 | /* |
| 2178 | * even if we're attaching all tasks in the thread group, we |
| 2179 | * only need to check permissions on one of them. |
| 2180 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2181 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2182 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2183 | !uid_eq(cred->euid, tcred->uid) && |
| 2184 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2185 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2186 | ret = -EACCES; |
| 2187 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2188 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2189 | } else |
| 2190 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2191 | |
| 2192 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2193 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2194 | |
| 2195 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2196 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2197 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2198 | * with no rt_runtime allocated. Just say no. |
| 2199 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2200 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2201 | ret = -EINVAL; |
| 2202 | rcu_read_unlock(); |
| 2203 | goto out_unlock_cgroup; |
| 2204 | } |
| 2205 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2206 | get_task_struct(tsk); |
| 2207 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2208 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2209 | threadgroup_lock(tsk); |
| 2210 | if (threadgroup) { |
| 2211 | if (!thread_group_leader(tsk)) { |
| 2212 | /* |
| 2213 | * a race with de_thread from another thread's exec() |
| 2214 | * may strip us of our leadership, if this happens, |
| 2215 | * there is no choice but to throw this task away and |
| 2216 | * try again; this is |
| 2217 | * "double-double-toil-and-trouble-check locking". |
| 2218 | */ |
| 2219 | threadgroup_unlock(tsk); |
| 2220 | put_task_struct(tsk); |
| 2221 | goto retry_find_task; |
| 2222 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2226 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2227 | threadgroup_unlock(tsk); |
| 2228 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2229 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2230 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2231 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2232 | return ret; |
| 2233 | } |
| 2234 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2235 | /** |
| 2236 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2237 | * @from: attach to all cgroups of a given task |
| 2238 | * @tsk: the task to be attached |
| 2239 | */ |
| 2240 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2241 | { |
| 2242 | struct cgroupfs_root *root; |
| 2243 | int retval = 0; |
| 2244 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2245 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2246 | for_each_active_root(root) { |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2247 | struct cgroup *from_cgrp = task_cgroup_from_root(from, root); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2248 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2249 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2250 | if (retval) |
| 2251 | break; |
| 2252 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2253 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2254 | |
| 2255 | return retval; |
| 2256 | } |
| 2257 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2258 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2259 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2260 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2261 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2262 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2265 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2266 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2267 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2268 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2269 | } |
| 2270 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2271 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
| 2272 | struct cftype *cft, const char *buffer) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2273 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2274 | BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2275 | if (strlen(buffer) >= PATH_MAX) |
| 2276 | return -EINVAL; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2277 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2278 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2279 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2280 | strcpy(css->cgroup->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2281 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2282 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2283 | return 0; |
| 2284 | } |
| 2285 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2286 | static int cgroup_release_agent_show(struct cgroup_subsys_state *css, |
| 2287 | struct cftype *cft, struct seq_file *seq) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2288 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2289 | struct cgroup *cgrp = css->cgroup; |
| 2290 | |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2291 | if (!cgroup_lock_live_group(cgrp)) |
| 2292 | return -ENODEV; |
| 2293 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2294 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2295 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2296 | return 0; |
| 2297 | } |
| 2298 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2299 | static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css, |
| 2300 | struct cftype *cft, struct seq_file *seq) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2301 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2302 | seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2303 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2304 | } |
| 2305 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2306 | /* A buffer size big enough for numbers or short strings */ |
| 2307 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2308 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2309 | static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css, |
| 2310 | struct cftype *cft, struct file *file, |
| 2311 | const char __user *userbuf, size_t nbytes, |
| 2312 | loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2313 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2314 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2315 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2316 | char *end; |
| 2317 | |
| 2318 | if (!nbytes) |
| 2319 | return -EINVAL; |
| 2320 | if (nbytes >= sizeof(buffer)) |
| 2321 | return -E2BIG; |
| 2322 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2323 | return -EFAULT; |
| 2324 | |
| 2325 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2326 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2327 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2328 | if (*end) |
| 2329 | return -EINVAL; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2330 | retval = cft->write_u64(css, cft, val); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2331 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2332 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2333 | if (*end) |
| 2334 | return -EINVAL; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2335 | retval = cft->write_s64(css, cft, val); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2336 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2337 | if (!retval) |
| 2338 | retval = nbytes; |
| 2339 | return retval; |
| 2340 | } |
| 2341 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2342 | static ssize_t cgroup_write_string(struct cgroup_subsys_state *css, |
| 2343 | struct cftype *cft, struct file *file, |
| 2344 | const char __user *userbuf, size_t nbytes, |
| 2345 | loff_t *unused_ppos) |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2346 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2347 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2348 | int retval = 0; |
| 2349 | size_t max_bytes = cft->max_write_len; |
| 2350 | char *buffer = local_buffer; |
| 2351 | |
| 2352 | if (!max_bytes) |
| 2353 | max_bytes = sizeof(local_buffer) - 1; |
| 2354 | if (nbytes >= max_bytes) |
| 2355 | return -E2BIG; |
| 2356 | /* Allocate a dynamic buffer if we need one */ |
| 2357 | if (nbytes >= sizeof(local_buffer)) { |
| 2358 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2359 | if (buffer == NULL) |
| 2360 | return -ENOMEM; |
| 2361 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2362 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2363 | retval = -EFAULT; |
| 2364 | goto out; |
| 2365 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2366 | |
| 2367 | buffer[nbytes] = 0; /* nul-terminate */ |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2368 | retval = cft->write_string(css, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2369 | if (!retval) |
| 2370 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2371 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2372 | if (buffer != local_buffer) |
| 2373 | kfree(buffer); |
| 2374 | return retval; |
| 2375 | } |
| 2376 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2377 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2378 | size_t nbytes, loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2379 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2380 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2381 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2382 | struct cgroup_subsys_state *css = cfe->css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2383 | |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2384 | if (cft->write) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2385 | return cft->write(css, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2386 | if (cft->write_u64 || cft->write_s64) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2387 | return cgroup_write_X64(css, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2388 | if (cft->write_string) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2389 | return cgroup_write_string(css, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2390 | if (cft->trigger) { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2391 | int ret = cft->trigger(css, (unsigned int)cft->private); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2392 | return ret ? ret : nbytes; |
| 2393 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2394 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2395 | } |
| 2396 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2397 | static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css, |
| 2398 | struct cftype *cft, struct file *file, |
| 2399 | char __user *buf, size_t nbytes, loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2400 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2401 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2402 | u64 val = cft->read_u64(css, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2403 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2404 | |
| 2405 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2406 | } |
| 2407 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2408 | static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css, |
| 2409 | struct cftype *cft, struct file *file, |
| 2410 | char __user *buf, size_t nbytes, loff_t *ppos) |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2411 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2412 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2413 | s64 val = cft->read_s64(css, cft); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2414 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2415 | |
| 2416 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2417 | } |
| 2418 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2419 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2420 | size_t nbytes, loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2421 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2422 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2423 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2424 | struct cgroup_subsys_state *css = cfe->css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2425 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2426 | if (cft->read) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2427 | return cft->read(css, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2428 | if (cft->read_u64) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2429 | return cgroup_read_u64(css, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2430 | if (cft->read_s64) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2431 | return cgroup_read_s64(css, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2432 | return -EINVAL; |
| 2433 | } |
| 2434 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2435 | /* |
| 2436 | * seqfile ops/methods for returning structured data. Currently just |
| 2437 | * supports string->u64 maps, but can be extended in future. |
| 2438 | */ |
| 2439 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2440 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2441 | { |
| 2442 | struct seq_file *sf = cb->state; |
| 2443 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2444 | } |
| 2445 | |
| 2446 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2447 | { |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2448 | struct cfent *cfe = m->private; |
| 2449 | struct cftype *cft = cfe->type; |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2450 | struct cgroup_subsys_state *css = cfe->css; |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2451 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2452 | if (cft->read_map) { |
| 2453 | struct cgroup_map_cb cb = { |
| 2454 | .fill = cgroup_map_add, |
| 2455 | .state = m, |
| 2456 | }; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2457 | return cft->read_map(css, cft, &cb); |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2458 | } |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2459 | return cft->read_seq_string(css, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2460 | } |
| 2461 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2462 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2463 | .read = seq_read, |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2464 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2465 | .llseek = seq_lseek, |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2466 | .release = single_release, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2467 | }; |
| 2468 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2469 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2470 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2471 | struct cfent *cfe = __d_cfe(file->f_dentry); |
| 2472 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2473 | struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent); |
| 2474 | struct cgroup_subsys_state *css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2475 | int err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2476 | |
| 2477 | err = generic_file_open(inode, file); |
| 2478 | if (err) |
| 2479 | return err; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2480 | |
| 2481 | /* |
| 2482 | * If the file belongs to a subsystem, pin the css. Will be |
| 2483 | * unpinned either on open failure or release. This ensures that |
| 2484 | * @css stays alive for all file operations. |
| 2485 | */ |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2486 | rcu_read_lock(); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2487 | css = cgroup_css(cgrp, cft->ss); |
| 2488 | if (cft->ss && !css_tryget(css)) |
| 2489 | css = NULL; |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2490 | rcu_read_unlock(); |
| 2491 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2492 | if (!css) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2493 | return -ENODEV; |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2494 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2495 | /* |
| 2496 | * @cfe->css is used by read/write/close to determine the |
| 2497 | * associated css. @file->private_data would be a better place but |
| 2498 | * that's already used by seqfile. Multiple accessors may use it |
| 2499 | * simultaneously which is okay as the association never changes. |
| 2500 | */ |
| 2501 | WARN_ON_ONCE(cfe->css && cfe->css != css); |
| 2502 | cfe->css = css; |
| 2503 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2504 | if (cft->read_map || cft->read_seq_string) { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2505 | file->f_op = &cgroup_seqfile_operations; |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2506 | err = single_open(file, cgroup_seqfile_show, cfe); |
| 2507 | } else if (cft->open) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2508 | err = cft->open(inode, file); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2509 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2510 | |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2511 | if (css->ss && err) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2512 | css_put(css); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2513 | return err; |
| 2514 | } |
| 2515 | |
| 2516 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2517 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2518 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2519 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2520 | struct cgroup_subsys_state *css = cfe->css; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2521 | int ret = 0; |
| 2522 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2523 | if (cft->release) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2524 | ret = cft->release(inode, file); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2525 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2526 | css_put(css); |
| 2527 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | /* |
| 2531 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2532 | */ |
| 2533 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2534 | struct inode *new_dir, struct dentry *new_dentry) |
| 2535 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2536 | int ret; |
| 2537 | struct cgroup_name *name, *old_name; |
| 2538 | struct cgroup *cgrp; |
| 2539 | |
| 2540 | /* |
| 2541 | * It's convinient to use parent dir's i_mutex to protected |
| 2542 | * cgrp->name. |
| 2543 | */ |
| 2544 | lockdep_assert_held(&old_dir->i_mutex); |
| 2545 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2546 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2547 | return -ENOTDIR; |
| 2548 | if (new_dentry->d_inode) |
| 2549 | return -EEXIST; |
| 2550 | if (old_dir != new_dir) |
| 2551 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2552 | |
| 2553 | cgrp = __d_cgrp(old_dentry); |
| 2554 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2555 | /* |
| 2556 | * This isn't a proper migration and its usefulness is very |
| 2557 | * limited. Disallow if sane_behavior. |
| 2558 | */ |
| 2559 | if (cgroup_sane_behavior(cgrp)) |
| 2560 | return -EPERM; |
| 2561 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2562 | name = cgroup_alloc_name(new_dentry); |
| 2563 | if (!name) |
| 2564 | return -ENOMEM; |
| 2565 | |
| 2566 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2567 | if (ret) { |
| 2568 | kfree(name); |
| 2569 | return ret; |
| 2570 | } |
| 2571 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 2572 | old_name = rcu_dereference_protected(cgrp->name, true); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2573 | rcu_assign_pointer(cgrp->name, name); |
| 2574 | |
| 2575 | kfree_rcu(old_name, rcu_head); |
| 2576 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2577 | } |
| 2578 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2579 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2580 | { |
| 2581 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2582 | return &__d_cgrp(dentry)->xattrs; |
| 2583 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2584 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | static inline int xattr_enabled(struct dentry *dentry) |
| 2588 | { |
| 2589 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2590 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | static bool is_valid_xattr(const char *name) |
| 2594 | { |
| 2595 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2596 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2597 | return true; |
| 2598 | return false; |
| 2599 | } |
| 2600 | |
| 2601 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2602 | const void *val, size_t size, int flags) |
| 2603 | { |
| 2604 | if (!xattr_enabled(dentry)) |
| 2605 | return -EOPNOTSUPP; |
| 2606 | if (!is_valid_xattr(name)) |
| 2607 | return -EINVAL; |
| 2608 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2609 | } |
| 2610 | |
| 2611 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2612 | { |
| 2613 | if (!xattr_enabled(dentry)) |
| 2614 | return -EOPNOTSUPP; |
| 2615 | if (!is_valid_xattr(name)) |
| 2616 | return -EINVAL; |
| 2617 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2618 | } |
| 2619 | |
| 2620 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2621 | void *buf, size_t size) |
| 2622 | { |
| 2623 | if (!xattr_enabled(dentry)) |
| 2624 | return -EOPNOTSUPP; |
| 2625 | if (!is_valid_xattr(name)) |
| 2626 | return -EINVAL; |
| 2627 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2628 | } |
| 2629 | |
| 2630 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2631 | { |
| 2632 | if (!xattr_enabled(dentry)) |
| 2633 | return -EOPNOTSUPP; |
| 2634 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2635 | } |
| 2636 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2637 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2638 | .read = cgroup_file_read, |
| 2639 | .write = cgroup_file_write, |
| 2640 | .llseek = generic_file_llseek, |
| 2641 | .open = cgroup_file_open, |
| 2642 | .release = cgroup_file_release, |
| 2643 | }; |
| 2644 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2645 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2646 | .setxattr = cgroup_setxattr, |
| 2647 | .getxattr = cgroup_getxattr, |
| 2648 | .listxattr = cgroup_listxattr, |
| 2649 | .removexattr = cgroup_removexattr, |
| 2650 | }; |
| 2651 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2652 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2653 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2654 | .mkdir = cgroup_mkdir, |
| 2655 | .rmdir = cgroup_rmdir, |
| 2656 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2657 | .setxattr = cgroup_setxattr, |
| 2658 | .getxattr = cgroup_getxattr, |
| 2659 | .listxattr = cgroup_listxattr, |
| 2660 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2661 | }; |
| 2662 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2663 | 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] | 2664 | { |
| 2665 | if (dentry->d_name.len > NAME_MAX) |
| 2666 | return ERR_PTR(-ENAMETOOLONG); |
| 2667 | d_add(dentry, NULL); |
| 2668 | return NULL; |
| 2669 | } |
| 2670 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2671 | /* |
| 2672 | * Check if a file is a control file |
| 2673 | */ |
| 2674 | static inline struct cftype *__file_cft(struct file *file) |
| 2675 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2676 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2677 | return ERR_PTR(-EINVAL); |
| 2678 | return __d_cft(file->f_dentry); |
| 2679 | } |
| 2680 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2681 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2682 | struct super_block *sb) |
| 2683 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2684 | struct inode *inode; |
| 2685 | |
| 2686 | if (!dentry) |
| 2687 | return -ENOENT; |
| 2688 | if (dentry->d_inode) |
| 2689 | return -EEXIST; |
| 2690 | |
| 2691 | inode = cgroup_new_inode(mode, sb); |
| 2692 | if (!inode) |
| 2693 | return -ENOMEM; |
| 2694 | |
| 2695 | if (S_ISDIR(mode)) { |
| 2696 | inode->i_op = &cgroup_dir_inode_operations; |
| 2697 | inode->i_fop = &simple_dir_operations; |
| 2698 | |
| 2699 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2700 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2701 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2702 | |
Tejun Heo | b8a2df6 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2703 | /* |
| 2704 | * Control reaches here with cgroup_mutex held. |
| 2705 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2706 | * want to populate it immediately without releasing |
| 2707 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2708 | * yet, trylock will always succeed without affecting |
| 2709 | * lockdep checks. |
| 2710 | */ |
| 2711 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2712 | } else if (S_ISREG(mode)) { |
| 2713 | inode->i_size = 0; |
| 2714 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2715 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2716 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2717 | d_instantiate(dentry, inode); |
| 2718 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2719 | return 0; |
| 2720 | } |
| 2721 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2722 | /** |
| 2723 | * cgroup_file_mode - deduce file mode of a control file |
| 2724 | * @cft: the control file in question |
| 2725 | * |
| 2726 | * returns cft->mode if ->mode is not 0 |
| 2727 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2728 | * returns S_IRUGO if it has only a read handler |
| 2729 | * returns S_IWUSR if it has only a write hander |
| 2730 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2731 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2732 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2733 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2734 | |
| 2735 | if (cft->mode) |
| 2736 | return cft->mode; |
| 2737 | |
| 2738 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2739 | cft->read_map || cft->read_seq_string) |
| 2740 | mode |= S_IRUGO; |
| 2741 | |
| 2742 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2743 | cft->write_string || cft->trigger) |
| 2744 | mode |= S_IWUSR; |
| 2745 | |
| 2746 | return mode; |
| 2747 | } |
| 2748 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2749 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2750 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2751 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2752 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2753 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2754 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2755 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2756 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2757 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2758 | |
Tejun Heo | 9fa4db3 | 2013-08-26 18:40:56 -0400 | [diff] [blame^] | 2759 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 2760 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2761 | strcpy(name, cft->ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2762 | strcat(name, "."); |
| 2763 | } |
| 2764 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2765 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2766 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2767 | |
| 2768 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2769 | if (!cfe) |
| 2770 | return -ENOMEM; |
| 2771 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2772 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2773 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2774 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2775 | goto out; |
| 2776 | } |
| 2777 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2778 | cfe->type = (void *)cft; |
| 2779 | cfe->dentry = dentry; |
| 2780 | dentry->d_fsdata = cfe; |
| 2781 | simple_xattrs_init(&cfe->xattrs); |
| 2782 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2783 | mode = cgroup_file_mode(cft); |
| 2784 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2785 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2786 | list_add_tail(&cfe->node, &parent->files); |
| 2787 | cfe = NULL; |
| 2788 | } |
| 2789 | dput(dentry); |
| 2790 | out: |
| 2791 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2792 | return error; |
| 2793 | } |
| 2794 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2795 | /** |
| 2796 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2797 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2798 | * @cfts: array of cftypes to be added |
| 2799 | * @is_add: whether to add or remove |
| 2800 | * |
| 2801 | * Depending on @is_add, add or remove files defined by @cfts on @cgrp. |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2802 | * For removals, this function never fails. If addition fails, this |
| 2803 | * function doesn't remove files already added. The caller is responsible |
| 2804 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2805 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2806 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2807 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2808 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2809 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2810 | int ret; |
| 2811 | |
| 2812 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 2813 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2814 | |
| 2815 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2816 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2817 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2818 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2819 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2820 | continue; |
| 2821 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2822 | continue; |
| 2823 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2824 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2825 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2826 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2827 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2828 | cft->name, ret); |
| 2829 | return ret; |
| 2830 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2831 | } else { |
| 2832 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2833 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2834 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2835 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2836 | } |
| 2837 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2838 | static void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2839 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2840 | { |
| 2841 | /* |
| 2842 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2843 | * the existing cgroups under cgroup_mutex and create files. |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2844 | * Instead, we use css_for_each_descendant_pre() and drop RCU read |
| 2845 | * lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2846 | */ |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2847 | mutex_lock(&cgroup_mutex); |
| 2848 | } |
| 2849 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2850 | static int cgroup_cfts_commit(struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2851 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2852 | { |
| 2853 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2854 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2855 | struct cgroup *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2856 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2857 | struct dentry *prev = NULL; |
| 2858 | struct inode *inode; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2859 | struct cgroup_subsys_state *css; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2860 | u64 update_before; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2861 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2862 | |
| 2863 | /* %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] | 2864 | if (!cfts || ss->root == &cgroup_dummy_root || |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2865 | !atomic_inc_not_zero(&sb->s_active)) { |
| 2866 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2867 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2868 | } |
| 2869 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2870 | /* |
| 2871 | * All cgroups which are created after we drop cgroup_mutex will |
| 2872 | * 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] | 2873 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2874 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2875 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2876 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2877 | mutex_unlock(&cgroup_mutex); |
| 2878 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2879 | /* add/rm files for all cgroups created before */ |
| 2880 | rcu_read_lock(); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2881 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2882 | struct cgroup *cgrp = css->cgroup; |
| 2883 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2884 | if (cgroup_is_dead(cgrp)) |
| 2885 | continue; |
| 2886 | |
| 2887 | inode = cgrp->dentry->d_inode; |
| 2888 | dget(cgrp->dentry); |
| 2889 | rcu_read_unlock(); |
| 2890 | |
| 2891 | dput(prev); |
| 2892 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2893 | |
| 2894 | mutex_lock(&inode->i_mutex); |
| 2895 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2896 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2897 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2898 | mutex_unlock(&cgroup_mutex); |
| 2899 | mutex_unlock(&inode->i_mutex); |
| 2900 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2901 | rcu_read_lock(); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2902 | if (ret) |
| 2903 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2904 | } |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2905 | rcu_read_unlock(); |
| 2906 | dput(prev); |
| 2907 | deactivate_super(sb); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2908 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | /** |
| 2912 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2913 | * @ss: target cgroup subsystem |
| 2914 | * @cfts: zero-length name terminated array of cftypes |
| 2915 | * |
| 2916 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2917 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2918 | * have them too. This function can be called anytime whether @ss is |
| 2919 | * attached or not. |
| 2920 | * |
| 2921 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2922 | * function currently returns 0 as long as @cfts registration is successful |
| 2923 | * even if some file creation attempts on existing cgroups fail. |
| 2924 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2925 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2926 | { |
| 2927 | struct cftype_set *set; |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2928 | struct cftype *cft; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2929 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2930 | |
| 2931 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2932 | if (!set) |
| 2933 | return -ENOMEM; |
| 2934 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2935 | for (cft = cfts; cft->name[0] != '\0'; cft++) |
| 2936 | cft->ss = ss; |
| 2937 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2938 | cgroup_cfts_prepare(); |
| 2939 | set->cfts = cfts; |
| 2940 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2941 | ret = cgroup_cfts_commit(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2942 | if (ret) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2943 | cgroup_rm_cftypes(cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2944 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2945 | } |
| 2946 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2947 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2948 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2949 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2950 | * @cfts: zero-length name terminated array of cftypes |
| 2951 | * |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2952 | * Unregister @cfts. Files described by @cfts are removed from all |
| 2953 | * existing cgroups and all future cgroups won't have them either. This |
| 2954 | * function can be called anytime whether @cfts' subsys is attached or not. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2955 | * |
| 2956 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2957 | * registered. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2958 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2959 | int cgroup_rm_cftypes(struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2960 | { |
| 2961 | struct cftype_set *set; |
| 2962 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2963 | if (!cfts || !cfts[0].ss) |
| 2964 | return -ENOENT; |
| 2965 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2966 | cgroup_cfts_prepare(); |
| 2967 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2968 | list_for_each_entry(set, &cfts[0].ss->cftsets, node) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2969 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2970 | list_del(&set->node); |
| 2971 | kfree(set); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2972 | cgroup_cfts_commit(cfts, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2973 | return 0; |
| 2974 | } |
| 2975 | } |
| 2976 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2977 | cgroup_cfts_commit(NULL, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2978 | return -ENOENT; |
| 2979 | } |
| 2980 | |
| 2981 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2982 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2983 | * @cgrp: the cgroup in question |
| 2984 | * |
| 2985 | * Return the number of tasks in the cgroup. |
| 2986 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2987 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2988 | { |
| 2989 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2990 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2991 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2992 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2993 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2994 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2995 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2996 | return count; |
| 2997 | } |
| 2998 | |
| 2999 | /* |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3000 | * To reduce the fork() overhead for systems that are not actually using |
| 3001 | * their cgroups capability, we don't maintain the lists running through |
| 3002 | * each css_set to its tasks until we see the list actually used - in other |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3003 | * words after the first call to css_task_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3004 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 3005 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3006 | { |
| 3007 | struct task_struct *p, *g; |
| 3008 | write_lock(&css_set_lock); |
| 3009 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 3010 | /* |
| 3011 | * We need tasklist_lock because RCU is not safe against |
| 3012 | * while_each_thread(). Besides, a forking task that has passed |
| 3013 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 3014 | * is not guaranteed to have its child immediately visible in the |
| 3015 | * tasklist if we walk through it with RCU. |
| 3016 | */ |
| 3017 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3018 | do_each_thread(g, p) { |
| 3019 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 3020 | /* |
| 3021 | * We should check if the process is exiting, otherwise |
| 3022 | * it will race with cgroup_exit() in that the list |
| 3023 | * entry won't be deleted though the process has exited. |
| 3024 | */ |
| 3025 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 3026 | list_add(&p->cg_list, &task_css_set(p)->tasks); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3027 | task_unlock(p); |
| 3028 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 3029 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3030 | write_unlock(&css_set_lock); |
| 3031 | } |
| 3032 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3033 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3034 | * css_next_child - find the next child of a given css |
| 3035 | * @pos_css: the current position (%NULL to initiate traversal) |
| 3036 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3037 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3038 | * This function returns the next child of @parent_css and should be called |
| 3039 | * under RCU read lock. The only requirement is that @parent_css and |
| 3040 | * @pos_css are accessible. The next sibling is guaranteed to be returned |
| 3041 | * regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3042 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3043 | struct cgroup_subsys_state * |
| 3044 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 3045 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3046 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3047 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 3048 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3049 | struct cgroup *next; |
| 3050 | |
| 3051 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3052 | |
| 3053 | /* |
| 3054 | * @pos could already have been removed. Once a cgroup is removed, |
| 3055 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3056 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 3057 | * before the cgroup is taken off the ->sibling list, if we see it |
| 3058 | * unasserted, it's guaranteed that the next sibling hasn't |
| 3059 | * finished its grace period even if it's already removed, and thus |
| 3060 | * safe to dereference from this RCU critical section. If |
| 3061 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 3062 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3063 | * |
| 3064 | * If @pos is dead, its next pointer can't be dereferenced; |
| 3065 | * however, as each cgroup is given a monotonically increasing |
| 3066 | * unique serial number and always appended to the sibling list, |
| 3067 | * the next one can be found by walking the parent's children until |
| 3068 | * we see a cgroup with higher serial number than @pos's. While |
| 3069 | * this path can be slower, it's taken only when either the current |
| 3070 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3071 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3072 | if (!pos) { |
| 3073 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 3074 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3075 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3076 | } else { |
| 3077 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 3078 | if (next->serial_nr > pos->serial_nr) |
| 3079 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3080 | } |
| 3081 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3082 | if (&next->sibling == &cgrp->children) |
| 3083 | return NULL; |
| 3084 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3085 | return cgroup_css(next, parent_css->ss); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3086 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3087 | EXPORT_SYMBOL_GPL(css_next_child); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3088 | |
| 3089 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3090 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3091 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3092 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3093 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3094 | * To be used by css_for_each_descendant_pre(). Find the next descendant |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3095 | * to visit for pre-order traversal of @root's descendants. @root is |
| 3096 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3097 | * |
| 3098 | * While this function requires RCU read locking, it doesn't require the |
| 3099 | * whole traversal to be contained in a single RCU critical section. This |
| 3100 | * function will return the correct next descendant as long as both @pos |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3101 | * and @root are accessible and @pos is a descendant of @root. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3102 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3103 | struct cgroup_subsys_state * |
| 3104 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 3105 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3106 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3107 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3108 | |
| 3109 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3110 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3111 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3112 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3113 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3114 | |
| 3115 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3116 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3117 | if (next) |
| 3118 | return next; |
| 3119 | |
| 3120 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3121 | while (pos != root) { |
| 3122 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3123 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3124 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3125 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3126 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3127 | |
| 3128 | return NULL; |
| 3129 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3130 | EXPORT_SYMBOL_GPL(css_next_descendant_pre); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3131 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3132 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3133 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 3134 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3135 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3136 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 3137 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3138 | * subtree of @pos. |
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 rightmost descendant as long as @pos is |
| 3143 | * accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3144 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3145 | struct cgroup_subsys_state * |
| 3146 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3147 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3148 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3149 | |
| 3150 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3151 | |
| 3152 | do { |
| 3153 | last = pos; |
| 3154 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3155 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3156 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3157 | pos = tmp; |
| 3158 | } while (pos); |
| 3159 | |
| 3160 | return last; |
| 3161 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3162 | EXPORT_SYMBOL_GPL(css_rightmost_descendant); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3163 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3164 | static struct cgroup_subsys_state * |
| 3165 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3166 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3167 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3168 | |
| 3169 | do { |
| 3170 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3171 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3172 | } while (pos); |
| 3173 | |
| 3174 | return last; |
| 3175 | } |
| 3176 | |
| 3177 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3178 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3179 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3180 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3181 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3182 | * To be used by css_for_each_descendant_post(). Find the next descendant |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3183 | * to visit for post-order traversal of @root's descendants. @root is |
| 3184 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3185 | * |
| 3186 | * While this function requires RCU read locking, it doesn't require the |
| 3187 | * whole traversal to be contained in a single RCU critical section. This |
| 3188 | * function will return the correct next descendant as long as both @pos |
| 3189 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3190 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3191 | struct cgroup_subsys_state * |
| 3192 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 3193 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3194 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3195 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3196 | |
| 3197 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3198 | |
| 3199 | /* if first iteration, visit the leftmost descendant */ |
| 3200 | if (!pos) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3201 | next = css_leftmost_descendant(root); |
| 3202 | return next != root ? next : NULL; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3203 | } |
| 3204 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3205 | /* if we visited @root, we're done */ |
| 3206 | if (pos == root) |
| 3207 | return NULL; |
| 3208 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3209 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3210 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3211 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3212 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3213 | |
| 3214 | /* no sibling left, visit parent */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3215 | return css_parent(pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3216 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3217 | EXPORT_SYMBOL_GPL(css_next_descendant_post); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3218 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3219 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3220 | * css_advance_task_iter - advance a task itererator to the next css_set |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3221 | * @it: the iterator to advance |
| 3222 | * |
| 3223 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3224 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3225 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3226 | { |
| 3227 | struct list_head *l = it->cset_link; |
| 3228 | struct cgrp_cset_link *link; |
| 3229 | struct css_set *cset; |
| 3230 | |
| 3231 | /* Advance to the next non-empty css_set */ |
| 3232 | do { |
| 3233 | l = l->next; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3234 | if (l == &it->origin_css->cgroup->cset_links) { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3235 | it->cset_link = NULL; |
| 3236 | return; |
| 3237 | } |
| 3238 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3239 | cset = link->cset; |
| 3240 | } while (list_empty(&cset->tasks)); |
| 3241 | it->cset_link = l; |
| 3242 | it->task = cset->tasks.next; |
| 3243 | } |
| 3244 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3245 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3246 | * css_task_iter_start - initiate task iteration |
| 3247 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3248 | * @it: the task iterator to use |
| 3249 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3250 | * Initiate iteration through the tasks of @css. The caller can call |
| 3251 | * css_task_iter_next() to walk through the tasks until the function |
| 3252 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 3253 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3254 | * |
| 3255 | * Note that this function acquires a lock which is released when the |
| 3256 | * iteration finishes. The caller can't sleep while iteration is in |
| 3257 | * progress. |
| 3258 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3259 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 3260 | struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3261 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3262 | { |
| 3263 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3264 | * The first time anyone tries to iterate across a css, we need to |
| 3265 | * enable the list linking each css_set to its tasks, and fix up |
| 3266 | * all existing tasks. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3267 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3268 | if (!use_task_css_set_links) |
| 3269 | cgroup_enable_task_cg_lists(); |
| 3270 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3271 | read_lock(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3272 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3273 | it->origin_css = css; |
| 3274 | it->cset_link = &css->cgroup->cset_links; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3275 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3276 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3279 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3280 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3281 | * @it: the task iterator being iterated |
| 3282 | * |
| 3283 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3284 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 3285 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3286 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3287 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3288 | { |
| 3289 | struct task_struct *res; |
| 3290 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3291 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3292 | |
| 3293 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3294 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3295 | return NULL; |
| 3296 | res = list_entry(l, struct task_struct, cg_list); |
| 3297 | /* Advance iterator to find next entry */ |
| 3298 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3299 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3300 | if (l == &link->cset->tasks) { |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3301 | /* |
| 3302 | * We reached the end of this task list - move on to the |
| 3303 | * next cgrp_cset_link. |
| 3304 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3305 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3306 | } else { |
| 3307 | it->task = l; |
| 3308 | } |
| 3309 | return res; |
| 3310 | } |
| 3311 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3312 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3313 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3314 | * @it: the task iterator to finish |
| 3315 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3316 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3317 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3318 | void css_task_iter_end(struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3319 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3320 | { |
| 3321 | read_unlock(&css_set_lock); |
| 3322 | } |
| 3323 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3324 | static inline int started_after_time(struct task_struct *t1, |
| 3325 | struct timespec *time, |
| 3326 | struct task_struct *t2) |
| 3327 | { |
| 3328 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3329 | if (start_diff > 0) { |
| 3330 | return 1; |
| 3331 | } else if (start_diff < 0) { |
| 3332 | return 0; |
| 3333 | } else { |
| 3334 | /* |
| 3335 | * Arbitrarily, if two processes started at the same |
| 3336 | * time, we'll say that the lower pointer value |
| 3337 | * started first. Note that t2 may have exited by now |
| 3338 | * so this may not be a valid pointer any longer, but |
| 3339 | * that's fine - it still serves to distinguish |
| 3340 | * between two tasks started (effectively) simultaneously. |
| 3341 | */ |
| 3342 | return t1 > t2; |
| 3343 | } |
| 3344 | } |
| 3345 | |
| 3346 | /* |
| 3347 | * This function is a callback from heap_insert() and is used to order |
| 3348 | * the heap. |
| 3349 | * In this case we order the heap in descending task start time. |
| 3350 | */ |
| 3351 | static inline int started_after(void *p1, void *p2) |
| 3352 | { |
| 3353 | struct task_struct *t1 = p1; |
| 3354 | struct task_struct *t2 = p2; |
| 3355 | return started_after_time(t1, &t2->start_time, t2); |
| 3356 | } |
| 3357 | |
| 3358 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3359 | * css_scan_tasks - iterate though all the tasks in a css |
| 3360 | * @css: the css to iterate tasks of |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3361 | * @test: optional test callback |
| 3362 | * @process: process callback |
| 3363 | * @data: data passed to @test and @process |
| 3364 | * @heap: optional pre-allocated heap used for task iteration |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3365 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3366 | * Iterate through all the tasks in @css, calling @test for each, and if it |
| 3367 | * returns %true, call @process for it also. |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3368 | * |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3369 | * @test may be NULL, meaning always true (select all tasks), which |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3370 | * effectively duplicates css_task_iter_{start,next,end}() but does not |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3371 | * lock css_set_lock for the call to @process. |
| 3372 | * |
| 3373 | * It is guaranteed that @process will act on every task that is a member |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3374 | * of @css for the duration of this call. This function may or may not |
| 3375 | * call @process for tasks that exit or move to a different css during the |
| 3376 | * call, or are forked or move into the css during the call. |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3377 | * |
| 3378 | * Note that @test may be called with locks held, and may in some |
| 3379 | * situations be called multiple times for the same task, so it should be |
| 3380 | * cheap. |
| 3381 | * |
| 3382 | * If @heap is non-NULL, a heap has been pre-allocated and will be used for |
| 3383 | * heap operations (and its "gt" member will be overwritten), else a |
| 3384 | * temporary heap will be used (allocation of which may cause this function |
| 3385 | * to fail). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3386 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3387 | int css_scan_tasks(struct cgroup_subsys_state *css, |
| 3388 | bool (*test)(struct task_struct *, void *), |
| 3389 | void (*process)(struct task_struct *, void *), |
| 3390 | void *data, struct ptr_heap *heap) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3391 | { |
| 3392 | int retval, i; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3393 | struct css_task_iter it; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3394 | struct task_struct *p, *dropped; |
| 3395 | /* Never dereference latest_task, since it's not refcounted */ |
| 3396 | struct task_struct *latest_task = NULL; |
| 3397 | struct ptr_heap tmp_heap; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3398 | struct timespec latest_time = { 0, 0 }; |
| 3399 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3400 | if (heap) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3401 | /* The caller supplied our heap and pre-allocated its memory */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3402 | heap->gt = &started_after; |
| 3403 | } else { |
| 3404 | /* We need to allocate our own heap memory */ |
| 3405 | heap = &tmp_heap; |
| 3406 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3407 | if (retval) |
| 3408 | /* cannot allocate the heap */ |
| 3409 | return retval; |
| 3410 | } |
| 3411 | |
| 3412 | again: |
| 3413 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3414 | * Scan tasks in the css, using the @test callback to determine |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3415 | * which are of interest, and invoking @process callback on the |
| 3416 | * ones which need an update. Since we don't want to hold any |
| 3417 | * locks during the task updates, gather tasks to be processed in a |
| 3418 | * heap structure. The heap is sorted by descending task start |
| 3419 | * time. If the statically-sized heap fills up, we overflow tasks |
| 3420 | * that started later, and in future iterations only consider tasks |
| 3421 | * that started after the latest task in the previous pass. This |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3422 | * guarantees forward progress and that we don't miss any tasks. |
| 3423 | */ |
| 3424 | heap->size = 0; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3425 | css_task_iter_start(css, &it); |
| 3426 | while ((p = css_task_iter_next(&it))) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3427 | /* |
| 3428 | * Only affect tasks that qualify per the caller's callback, |
| 3429 | * if he provided one |
| 3430 | */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3431 | if (test && !test(p, data)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3432 | continue; |
| 3433 | /* |
| 3434 | * Only process tasks that started after the last task |
| 3435 | * we processed |
| 3436 | */ |
| 3437 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3438 | continue; |
| 3439 | dropped = heap_insert(heap, p); |
| 3440 | if (dropped == NULL) { |
| 3441 | /* |
| 3442 | * The new task was inserted; the heap wasn't |
| 3443 | * previously full |
| 3444 | */ |
| 3445 | get_task_struct(p); |
| 3446 | } else if (dropped != p) { |
| 3447 | /* |
| 3448 | * The new task was inserted, and pushed out a |
| 3449 | * different task |
| 3450 | */ |
| 3451 | get_task_struct(p); |
| 3452 | put_task_struct(dropped); |
| 3453 | } |
| 3454 | /* |
| 3455 | * Else the new task was newer than anything already in |
| 3456 | * the heap and wasn't inserted |
| 3457 | */ |
| 3458 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3459 | css_task_iter_end(&it); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3460 | |
| 3461 | if (heap->size) { |
| 3462 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3463 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3464 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3465 | latest_time = q->start_time; |
| 3466 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3467 | } |
| 3468 | /* Process the task per the caller's callback */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3469 | process(q, data); |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3470 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3471 | } |
| 3472 | /* |
| 3473 | * If we had to process any tasks at all, scan again |
| 3474 | * in case some of them were in the middle of forking |
| 3475 | * children that didn't get processed. |
| 3476 | * Not the most efficient way to do it, but it avoids |
| 3477 | * having to take callback_mutex in the fork path |
| 3478 | */ |
| 3479 | goto again; |
| 3480 | } |
| 3481 | if (heap == &tmp_heap) |
| 3482 | heap_free(&tmp_heap); |
| 3483 | return 0; |
| 3484 | } |
| 3485 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3486 | static void cgroup_transfer_one_task(struct task_struct *task, void *data) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3487 | { |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3488 | struct cgroup *new_cgroup = data; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3489 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3490 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3491 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3492 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | /** |
| 3496 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3497 | * @to: cgroup to which the tasks will be moved |
| 3498 | * @from: cgroup in which the tasks currently reside |
| 3499 | */ |
| 3500 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3501 | { |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3502 | return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task, |
| 3503 | to, NULL); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3504 | } |
| 3505 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3506 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3507 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3508 | * |
| 3509 | * Reading this file can return large amounts of data if a cgroup has |
| 3510 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3511 | * but we cannot guarantee that the information we produce is correct |
| 3512 | * unless we produce it entirely atomically. |
| 3513 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3514 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3515 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3516 | /* which pidlist file are we talking about? */ |
| 3517 | enum cgroup_filetype { |
| 3518 | CGROUP_FILE_PROCS, |
| 3519 | CGROUP_FILE_TASKS, |
| 3520 | }; |
| 3521 | |
| 3522 | /* |
| 3523 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3524 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3525 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3526 | * to the cgroup. |
| 3527 | */ |
| 3528 | struct cgroup_pidlist { |
| 3529 | /* |
| 3530 | * used to find which pidlist is wanted. doesn't change as long as |
| 3531 | * this particular list stays in the list. |
| 3532 | */ |
| 3533 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3534 | /* array of xids */ |
| 3535 | pid_t *list; |
| 3536 | /* how many elements the above list has */ |
| 3537 | int length; |
| 3538 | /* how many files are using the current array */ |
| 3539 | int use_count; |
| 3540 | /* each of these stored in a list by its cgroup */ |
| 3541 | struct list_head links; |
| 3542 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3543 | struct cgroup *owner; |
| 3544 | /* protects the other fields */ |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3545 | struct rw_semaphore rwsem; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3546 | }; |
| 3547 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3548 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3549 | * The following two functions "fix" the issue where there are more pids |
| 3550 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3551 | * TODO: replace with a kernel-wide solution to this problem |
| 3552 | */ |
| 3553 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3554 | static void *pidlist_allocate(int count) |
| 3555 | { |
| 3556 | if (PIDLIST_TOO_LARGE(count)) |
| 3557 | return vmalloc(count * sizeof(pid_t)); |
| 3558 | else |
| 3559 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3560 | } |
| 3561 | static void pidlist_free(void *p) |
| 3562 | { |
| 3563 | if (is_vmalloc_addr(p)) |
| 3564 | vfree(p); |
| 3565 | else |
| 3566 | kfree(p); |
| 3567 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3568 | |
| 3569 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3570 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3571 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3572 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3573 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3574 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3575 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3576 | |
| 3577 | /* |
| 3578 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3579 | * edge cases first; no work needs to be done for either |
| 3580 | */ |
| 3581 | if (length == 0 || length == 1) |
| 3582 | return length; |
| 3583 | /* src and dest walk down the list; dest counts unique elements */ |
| 3584 | for (src = 1; src < length; src++) { |
| 3585 | /* find next unique element */ |
| 3586 | while (list[src] == list[src-1]) { |
| 3587 | src++; |
| 3588 | if (src == length) |
| 3589 | goto after; |
| 3590 | } |
| 3591 | /* dest always points to where the next unique element goes */ |
| 3592 | list[dest] = list[src]; |
| 3593 | dest++; |
| 3594 | } |
| 3595 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3596 | return dest; |
| 3597 | } |
| 3598 | |
| 3599 | static int cmppid(const void *a, const void *b) |
| 3600 | { |
| 3601 | return *(pid_t *)a - *(pid_t *)b; |
| 3602 | } |
| 3603 | |
| 3604 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3605 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3606 | * returns with the lock on that pidlist already held, and takes care |
| 3607 | * of the use count, or returns NULL with no locks held if we're out of |
| 3608 | * memory. |
| 3609 | */ |
| 3610 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3611 | enum cgroup_filetype type) |
| 3612 | { |
| 3613 | struct cgroup_pidlist *l; |
| 3614 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3615 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3616 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3617 | /* |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3618 | * We can't drop the pidlist_mutex before taking the l->rwsem in case |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3619 | * the last ref-holder is trying to remove l from the list at the same |
| 3620 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3621 | * list we find out from under us - compare release_pid_array(). |
| 3622 | */ |
| 3623 | mutex_lock(&cgrp->pidlist_mutex); |
| 3624 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3625 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3626 | /* make sure l doesn't vanish out from under us */ |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3627 | down_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3628 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3629 | return l; |
| 3630 | } |
| 3631 | } |
| 3632 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3633 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3634 | if (!l) { |
| 3635 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3636 | return l; |
| 3637 | } |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3638 | init_rwsem(&l->rwsem); |
| 3639 | down_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3640 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3641 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3642 | l->owner = cgrp; |
| 3643 | list_add(&l->links, &cgrp->pidlists); |
| 3644 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3645 | return l; |
| 3646 | } |
| 3647 | |
| 3648 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3649 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3650 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3651 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3652 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3653 | { |
| 3654 | pid_t *array; |
| 3655 | int length; |
| 3656 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3657 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3658 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3659 | struct cgroup_pidlist *l; |
| 3660 | |
| 3661 | /* |
| 3662 | * If cgroup gets more users after we read count, we won't have |
| 3663 | * enough space - tough. This race is indistinguishable to the |
| 3664 | * caller from the case that the additional cgroup users didn't |
| 3665 | * show up until sometime later on. |
| 3666 | */ |
| 3667 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3668 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3669 | if (!array) |
| 3670 | return -ENOMEM; |
| 3671 | /* now, populate the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3672 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3673 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3674 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3675 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3676 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3677 | if (type == CGROUP_FILE_PROCS) |
| 3678 | pid = task_tgid_vnr(tsk); |
| 3679 | else |
| 3680 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3681 | if (pid > 0) /* make sure to only use valid results */ |
| 3682 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3683 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3684 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3685 | length = n; |
| 3686 | /* now sort & (if procs) strip out duplicates */ |
| 3687 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3688 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3689 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3690 | l = cgroup_pidlist_find(cgrp, type); |
| 3691 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3692 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3693 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3694 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3695 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3696 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3697 | l->list = array; |
| 3698 | l->length = length; |
| 3699 | l->use_count++; |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3700 | up_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3701 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3702 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3703 | } |
| 3704 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3705 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3706 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3707 | * @stats: cgroupstats to fill information into |
| 3708 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3709 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3710 | * |
| 3711 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3712 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3713 | */ |
| 3714 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3715 | { |
| 3716 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3717 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3718 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3719 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3720 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3721 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3722 | * Validate dentry by checking the superblock operations, |
| 3723 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3724 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3725 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3726 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3727 | goto err; |
| 3728 | |
| 3729 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3730 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3731 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3732 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3733 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3734 | switch (tsk->state) { |
| 3735 | case TASK_RUNNING: |
| 3736 | stats->nr_running++; |
| 3737 | break; |
| 3738 | case TASK_INTERRUPTIBLE: |
| 3739 | stats->nr_sleeping++; |
| 3740 | break; |
| 3741 | case TASK_UNINTERRUPTIBLE: |
| 3742 | stats->nr_uninterruptible++; |
| 3743 | break; |
| 3744 | case TASK_STOPPED: |
| 3745 | stats->nr_stopped++; |
| 3746 | break; |
| 3747 | default: |
| 3748 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3749 | stats->nr_io_wait++; |
| 3750 | break; |
| 3751 | } |
| 3752 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3753 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3754 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3755 | err: |
| 3756 | return ret; |
| 3757 | } |
| 3758 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3759 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3760 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3761 | * 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] | 3762 | * 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] | 3763 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3764 | */ |
| 3765 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3766 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3767 | { |
| 3768 | /* |
| 3769 | * Initially we receive a position value that corresponds to |
| 3770 | * one more than the last pid shown (or 0 on the first call or |
| 3771 | * after a seek to the start). Use a binary-search to find the |
| 3772 | * next pid to display, if any |
| 3773 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3774 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3775 | int index = 0, pid = *pos; |
| 3776 | int *iter; |
| 3777 | |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3778 | down_read(&l->rwsem); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3779 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3780 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3781 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3782 | while (index < end) { |
| 3783 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3784 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3785 | index = mid; |
| 3786 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3787 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3788 | index = mid + 1; |
| 3789 | else |
| 3790 | end = mid; |
| 3791 | } |
| 3792 | } |
| 3793 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3794 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3795 | return NULL; |
| 3796 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3797 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3798 | *pos = *iter; |
| 3799 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3800 | } |
| 3801 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3802 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3803 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3804 | struct cgroup_pidlist *l = s->private; |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3805 | up_read(&l->rwsem); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3806 | } |
| 3807 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3808 | 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] | 3809 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3810 | struct cgroup_pidlist *l = s->private; |
| 3811 | pid_t *p = v; |
| 3812 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3813 | /* |
| 3814 | * Advance to the next pid in the array. If this goes off the |
| 3815 | * end, we're done |
| 3816 | */ |
| 3817 | p++; |
| 3818 | if (p >= end) { |
| 3819 | return NULL; |
| 3820 | } else { |
| 3821 | *pos = *p; |
| 3822 | return p; |
| 3823 | } |
| 3824 | } |
| 3825 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3826 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3827 | { |
| 3828 | return seq_printf(s, "%d\n", *(int *)v); |
| 3829 | } |
| 3830 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3831 | /* |
| 3832 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3833 | * independent of whether it's tasks or procs |
| 3834 | */ |
| 3835 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3836 | .start = cgroup_pidlist_start, |
| 3837 | .stop = cgroup_pidlist_stop, |
| 3838 | .next = cgroup_pidlist_next, |
| 3839 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3840 | }; |
| 3841 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3842 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3843 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3844 | /* |
| 3845 | * the case where we're the last user of this particular pidlist will |
| 3846 | * have us remove it from the cgroup's list, which entails taking the |
| 3847 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3848 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3849 | */ |
| 3850 | mutex_lock(&l->owner->pidlist_mutex); |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3851 | down_write(&l->rwsem); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3852 | BUG_ON(!l->use_count); |
| 3853 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3854 | /* we're the last user if refcount is 0; remove and free */ |
| 3855 | list_del(&l->links); |
| 3856 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3857 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3858 | put_pid_ns(l->key.ns); |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3859 | up_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3860 | kfree(l); |
| 3861 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3862 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3863 | mutex_unlock(&l->owner->pidlist_mutex); |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3864 | up_write(&l->rwsem); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3865 | } |
| 3866 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3867 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3868 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3869 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3870 | if (!(file->f_mode & FMODE_READ)) |
| 3871 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3872 | /* |
| 3873 | * the seq_file will only be initialized if the file was opened for |
| 3874 | * reading; hence we check if it's not null only in that case. |
| 3875 | */ |
| 3876 | l = ((struct seq_file *)file->private_data)->private; |
| 3877 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3878 | return seq_release(inode, file); |
| 3879 | } |
| 3880 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3881 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3882 | .read = seq_read, |
| 3883 | .llseek = seq_lseek, |
| 3884 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3885 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3886 | }; |
| 3887 | |
| 3888 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3889 | * The following functions handle opens on a file that displays a pidlist |
| 3890 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3891 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3892 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3893 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3894 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3895 | { |
| 3896 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3897 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3898 | int retval; |
| 3899 | |
| 3900 | /* Nothing to do for write-only files */ |
| 3901 | if (!(file->f_mode & FMODE_READ)) |
| 3902 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3903 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3904 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3905 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3906 | if (retval) |
| 3907 | return retval; |
| 3908 | /* configure file information */ |
| 3909 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3910 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3911 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3912 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3913 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3914 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3915 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3916 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3917 | return 0; |
| 3918 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3919 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3920 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3921 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3922 | } |
| 3923 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3924 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3925 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3926 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3927 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3928 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3929 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3930 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3931 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3932 | } |
| 3933 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3934 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3935 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3936 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3937 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3938 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3939 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3940 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3941 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3942 | return 0; |
| 3943 | } |
| 3944 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3945 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3946 | * When dput() is called asynchronously, if umount has been done and |
| 3947 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3948 | * there's a small window that vfs will see the root dentry with non-zero |
| 3949 | * refcnt and trigger BUG(). |
| 3950 | * |
| 3951 | * That's why we hold a reference before dput() and drop it right after. |
| 3952 | */ |
| 3953 | static void cgroup_dput(struct cgroup *cgrp) |
| 3954 | { |
| 3955 | struct super_block *sb = cgrp->root->sb; |
| 3956 | |
| 3957 | atomic_inc(&sb->s_active); |
| 3958 | dput(cgrp->dentry); |
| 3959 | deactivate_super(sb); |
| 3960 | } |
| 3961 | |
| 3962 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3963 | * Unregister event and free resources. |
| 3964 | * |
| 3965 | * Gets called from workqueue. |
| 3966 | */ |
| 3967 | static void cgroup_event_remove(struct work_struct *work) |
| 3968 | { |
| 3969 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3970 | remove); |
Tejun Heo | 81eeaf0 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3971 | struct cgroup_subsys_state *css = event->css; |
| 3972 | struct cgroup *cgrp = css->cgroup; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3973 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3974 | remove_wait_queue(event->wqh, &event->wait); |
| 3975 | |
Tejun Heo | 81eeaf0 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3976 | event->cft->unregister_event(css, event->cft, event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3977 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3978 | /* Notify userspace the event is going away. */ |
| 3979 | eventfd_signal(event->eventfd, 1); |
| 3980 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3981 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3982 | kfree(event); |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3983 | cgroup_dput(cgrp); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | /* |
| 3987 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3988 | * |
| 3989 | * Called with wqh->lock held and interrupts disabled. |
| 3990 | */ |
| 3991 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3992 | int sync, void *key) |
| 3993 | { |
| 3994 | struct cgroup_event *event = container_of(wait, |
| 3995 | struct cgroup_event, wait); |
Tejun Heo | 81eeaf0 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3996 | struct cgroup *cgrp = event->css->cgroup; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3997 | unsigned long flags = (unsigned long)key; |
| 3998 | |
| 3999 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4000 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4001 | * If the event has been detached at cgroup removal, we |
| 4002 | * can simply return knowing the other side will cleanup |
| 4003 | * for us. |
| 4004 | * |
| 4005 | * We can't race against event freeing since the other |
| 4006 | * side will require wqh->lock via remove_wait_queue(), |
| 4007 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4008 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4009 | spin_lock(&cgrp->event_list_lock); |
| 4010 | if (!list_empty(&event->list)) { |
| 4011 | list_del_init(&event->list); |
| 4012 | /* |
| 4013 | * We are in atomic context, but cgroup_event_remove() |
| 4014 | * may sleep, so we have to call it in workqueue. |
| 4015 | */ |
| 4016 | schedule_work(&event->remove); |
| 4017 | } |
| 4018 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | return 0; |
| 4022 | } |
| 4023 | |
| 4024 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 4025 | wait_queue_head_t *wqh, poll_table *pt) |
| 4026 | { |
| 4027 | struct cgroup_event *event = container_of(pt, |
| 4028 | struct cgroup_event, pt); |
| 4029 | |
| 4030 | event->wqh = wqh; |
| 4031 | add_wait_queue(wqh, &event->wait); |
| 4032 | } |
| 4033 | |
| 4034 | /* |
| 4035 | * Parse input and register new cgroup event handler. |
| 4036 | * |
| 4037 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 4038 | * Interpretation of args is defined by control file implementation. |
| 4039 | */ |
Tejun Heo | 6e6eab0 | 2013-08-15 11:43:15 -0400 | [diff] [blame] | 4040 | static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css, |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4041 | struct cftype *cft, const char *buffer) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4042 | { |
Tejun Heo | 6e6eab0 | 2013-08-15 11:43:15 -0400 | [diff] [blame] | 4043 | struct cgroup *cgrp = dummy_css->cgroup; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4044 | struct cgroup_event *event; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 4045 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4046 | unsigned int efd, cfd; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4047 | struct file *efile; |
| 4048 | struct file *cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4049 | char *endp; |
| 4050 | int ret; |
| 4051 | |
| 4052 | efd = simple_strtoul(buffer, &endp, 10); |
| 4053 | if (*endp != ' ') |
| 4054 | return -EINVAL; |
| 4055 | buffer = endp + 1; |
| 4056 | |
| 4057 | cfd = simple_strtoul(buffer, &endp, 10); |
| 4058 | if ((*endp != ' ') && (*endp != '\0')) |
| 4059 | return -EINVAL; |
| 4060 | buffer = endp + 1; |
| 4061 | |
| 4062 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 4063 | if (!event) |
| 4064 | return -ENOMEM; |
Tejun Heo | 6e6eab0 | 2013-08-15 11:43:15 -0400 | [diff] [blame] | 4065 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4066 | INIT_LIST_HEAD(&event->list); |
| 4067 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 4068 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 4069 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 4070 | |
| 4071 | efile = eventfd_fget(efd); |
| 4072 | if (IS_ERR(efile)) { |
| 4073 | ret = PTR_ERR(efile); |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4074 | goto out_kfree; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4075 | } |
| 4076 | |
| 4077 | event->eventfd = eventfd_ctx_fileget(efile); |
| 4078 | if (IS_ERR(event->eventfd)) { |
| 4079 | ret = PTR_ERR(event->eventfd); |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4080 | goto out_put_efile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | cfile = fget(cfd); |
| 4084 | if (!cfile) { |
| 4085 | ret = -EBADF; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4086 | goto out_put_eventfd; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 4090 | /* 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] | 4091 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4092 | if (ret < 0) |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4093 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4094 | |
| 4095 | event->cft = __file_cft(cfile); |
| 4096 | if (IS_ERR(event->cft)) { |
| 4097 | ret = PTR_ERR(event->cft); |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4098 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4099 | } |
| 4100 | |
Tejun Heo | 6e6eab0 | 2013-08-15 11:43:15 -0400 | [diff] [blame] | 4101 | if (!event->cft->ss) { |
| 4102 | ret = -EBADF; |
| 4103 | goto out_put_cfile; |
| 4104 | } |
| 4105 | |
| 4106 | /* determine the css of @cfile and associate @event with it */ |
| 4107 | rcu_read_lock(); |
| 4108 | |
| 4109 | ret = -EINVAL; |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4110 | event->css = cgroup_css(cgrp, event->cft->ss); |
Tejun Heo | 6e6eab0 | 2013-08-15 11:43:15 -0400 | [diff] [blame] | 4111 | if (event->css) |
| 4112 | ret = 0; |
| 4113 | |
| 4114 | rcu_read_unlock(); |
| 4115 | if (ret) |
| 4116 | goto out_put_cfile; |
| 4117 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 4118 | /* |
| 4119 | * The file to be monitored must be in the same cgroup as |
| 4120 | * cgroup.event_control is. |
| 4121 | */ |
| 4122 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 4123 | if (cgrp_cfile != cgrp) { |
| 4124 | ret = -EINVAL; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4125 | goto out_put_cfile; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 4126 | } |
| 4127 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4128 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 4129 | ret = -EINVAL; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4130 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4131 | } |
| 4132 | |
Tejun Heo | 6e6eab0 | 2013-08-15 11:43:15 -0400 | [diff] [blame] | 4133 | ret = event->cft->register_event(event->css, event->cft, |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4134 | event->eventfd, buffer); |
| 4135 | if (ret) |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4136 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4137 | |
Li Zefan | 7ef70e4 | 2013-04-26 11:58:03 -0700 | [diff] [blame] | 4138 | efile->f_op->poll(efile, &event->pt); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4139 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4140 | /* |
| 4141 | * Events should be removed after rmdir of cgroup directory, but before |
| 4142 | * destroying subsystem state objects. Let's take reference to cgroup |
| 4143 | * directory dentry to do that. |
| 4144 | */ |
| 4145 | dget(cgrp->dentry); |
| 4146 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4147 | spin_lock(&cgrp->event_list_lock); |
| 4148 | list_add(&event->list, &cgrp->event_list); |
| 4149 | spin_unlock(&cgrp->event_list_lock); |
| 4150 | |
| 4151 | fput(cfile); |
| 4152 | fput(efile); |
| 4153 | |
| 4154 | return 0; |
| 4155 | |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4156 | out_put_cfile: |
| 4157 | fput(cfile); |
| 4158 | out_put_eventfd: |
| 4159 | eventfd_ctx_put(event->eventfd); |
| 4160 | out_put_efile: |
| 4161 | fput(efile); |
| 4162 | out_kfree: |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4163 | kfree(event); |
| 4164 | |
| 4165 | return ret; |
| 4166 | } |
| 4167 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4168 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 4169 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4170 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4171 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4172 | } |
| 4173 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4174 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 4175 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4176 | { |
| 4177 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4178 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4179 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4180 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4181 | return 0; |
| 4182 | } |
| 4183 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4184 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4185 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4186 | .name = "cgroup.procs", |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4187 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4188 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4189 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4190 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4191 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4192 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4193 | .name = "cgroup.event_control", |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4194 | .write_string = cgroup_write_event_control, |
| 4195 | .mode = S_IWUGO, |
| 4196 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4197 | { |
| 4198 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4199 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4200 | .read_u64 = cgroup_clone_children_read, |
| 4201 | .write_u64 = cgroup_clone_children_write, |
| 4202 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4203 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4204 | .name = "cgroup.sane_behavior", |
| 4205 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4206 | .read_seq_string = cgroup_sane_behavior_show, |
| 4207 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4208 | |
| 4209 | /* |
| 4210 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 4211 | * don't exist if sane_behavior. If you're depending on these, be |
| 4212 | * prepared to be burned. |
| 4213 | */ |
| 4214 | { |
| 4215 | .name = "tasks", |
| 4216 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
| 4217 | .open = cgroup_tasks_open, |
| 4218 | .write_u64 = cgroup_tasks_write, |
| 4219 | .release = cgroup_pidlist_release, |
| 4220 | .mode = S_IRUGO | S_IWUSR, |
| 4221 | }, |
| 4222 | { |
| 4223 | .name = "notify_on_release", |
| 4224 | .flags = CFTYPE_INSANE, |
| 4225 | .read_u64 = cgroup_read_notify_on_release, |
| 4226 | .write_u64 = cgroup_write_notify_on_release, |
| 4227 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4228 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4229 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4230 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4231 | .read_seq_string = cgroup_release_agent_show, |
| 4232 | .write_string = cgroup_release_agent_write, |
| 4233 | .max_write_len = PATH_MAX, |
| 4234 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4235 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4236 | }; |
| 4237 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4238 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4239 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4240 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4241 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4242 | * |
| 4243 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4244 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4245 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4246 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4247 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4248 | int i, ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4249 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4250 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4251 | for_each_subsys(ss, i) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4252 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4253 | |
| 4254 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4255 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4256 | |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4257 | list_for_each_entry(set, &ss->cftsets, node) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4258 | ret = cgroup_addrm_files(cgrp, set->cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4259 | if (ret < 0) |
| 4260 | goto err; |
| 4261 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4262 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4263 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4264 | /* This cgroup is ready now */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4265 | for_each_root_subsys(cgrp->root, ss) { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4266 | struct cgroup_subsys_state *css = cgroup_css(cgrp, ss); |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4267 | struct css_id *id = rcu_dereference_protected(css->id, true); |
| 4268 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4269 | /* |
| 4270 | * Update id->css pointer and make this css visible from |
| 4271 | * CSS ID functions. This pointer will be dereferened |
| 4272 | * from RCU-read-side without locks. |
| 4273 | */ |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4274 | if (id) |
| 4275 | rcu_assign_pointer(id->css, css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4276 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4277 | |
| 4278 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4279 | err: |
| 4280 | cgroup_clear_dir(cgrp, subsys_mask); |
| 4281 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4282 | } |
| 4283 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4284 | /* |
| 4285 | * css destruction is four-stage process. |
| 4286 | * |
| 4287 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 4288 | * Implemented in kill_css(). |
| 4289 | * |
| 4290 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
| 4291 | * and thus css_tryget() is guaranteed to fail, the css can be offlined |
| 4292 | * by invoking offline_css(). After offlining, the base ref is put. |
| 4293 | * Implemented in css_killed_work_fn(). |
| 4294 | * |
| 4295 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 4296 | * accessors are inside RCU read sections. css_release() schedules the |
| 4297 | * RCU callback. |
| 4298 | * |
| 4299 | * 4. After the grace period, the css can be freed. Implemented in |
| 4300 | * css_free_work_fn(). |
| 4301 | * |
| 4302 | * It is actually hairier because both step 2 and 4 require process context |
| 4303 | * and thus involve punting to css->destroy_work adding two additional |
| 4304 | * steps to the already complex sequence. |
| 4305 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4306 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4307 | { |
| 4308 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4309 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4310 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4311 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4312 | if (css->parent) |
| 4313 | css_put(css->parent); |
| 4314 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4315 | css->ss->css_free(css); |
| 4316 | cgroup_dput(cgrp); |
| 4317 | } |
| 4318 | |
| 4319 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 4320 | { |
| 4321 | struct cgroup_subsys_state *css = |
| 4322 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 4323 | |
| 4324 | /* |
| 4325 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4326 | * css_put(). dput() requires process context which we don't have. |
| 4327 | */ |
| 4328 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
| 4329 | schedule_work(&css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4330 | } |
| 4331 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4332 | static void css_release(struct percpu_ref *ref) |
| 4333 | { |
| 4334 | struct cgroup_subsys_state *css = |
| 4335 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4336 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4337 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4338 | } |
| 4339 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4340 | static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss, |
| 4341 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4342 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4343 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4344 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4345 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4346 | css->id = NULL; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4347 | |
| 4348 | if (cgrp->parent) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4349 | css->parent = cgroup_css(cgrp->parent, ss); |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4350 | else |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4351 | css->flags |= CSS_ROOT; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4352 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4353 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4354 | } |
| 4355 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4356 | /* invoke ->css_online() on a new CSS and mark it online if successful */ |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4357 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4358 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4359 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4360 | int ret = 0; |
| 4361 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4362 | lockdep_assert_held(&cgroup_mutex); |
| 4363 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4364 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4365 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4366 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4367 | css->flags |= CSS_ONLINE; |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4368 | css->cgroup->nr_css++; |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4369 | rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css); |
| 4370 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4371 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4372 | } |
| 4373 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4374 | /* if the CSS is online, invoke ->css_offline() on it and mark it offline */ |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4375 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4376 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4377 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4378 | |
| 4379 | lockdep_assert_held(&cgroup_mutex); |
| 4380 | |
| 4381 | if (!(css->flags & CSS_ONLINE)) |
| 4382 | return; |
| 4383 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4384 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4385 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4386 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4387 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4388 | css->cgroup->nr_css--; |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4389 | RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4390 | } |
| 4391 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4392 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4393 | * cgroup_create - create a cgroup |
| 4394 | * @parent: cgroup that will be parent of the new cgroup |
| 4395 | * @dentry: dentry of the new cgroup |
| 4396 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4397 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4398 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4399 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4400 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4401 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4402 | { |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4403 | struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { }; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4404 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4405 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4406 | struct cgroupfs_root *root = parent->root; |
| 4407 | int err = 0; |
| 4408 | struct cgroup_subsys *ss; |
| 4409 | struct super_block *sb = root->sb; |
| 4410 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4411 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4412 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4413 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4414 | return -ENOMEM; |
| 4415 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4416 | name = cgroup_alloc_name(dentry); |
| 4417 | if (!name) |
| 4418 | goto err_free_cgrp; |
| 4419 | rcu_assign_pointer(cgrp->name, name); |
| 4420 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4421 | /* |
| 4422 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4423 | * a half-baked cgroup. |
| 4424 | */ |
| 4425 | cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4426 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4427 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4428 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4429 | /* |
| 4430 | * Only live parents can have children. Note that the liveliness |
| 4431 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4432 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4433 | * anyway so that locking is contained inside cgroup proper and we |
| 4434 | * don't get nasty surprises if we ever grow another caller. |
| 4435 | */ |
| 4436 | if (!cgroup_lock_live_group(parent)) { |
| 4437 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4438 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4439 | } |
| 4440 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4441 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4442 | * get deleted on unmount if there are child cgroups. This |
| 4443 | * can be done outside cgroup_mutex, since the sb can't |
| 4444 | * disappear while someone has an open control file on the |
| 4445 | * fs */ |
| 4446 | atomic_inc(&sb->s_active); |
| 4447 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4448 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4449 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4450 | dentry->d_fsdata = cgrp; |
| 4451 | cgrp->dentry = dentry; |
| 4452 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4453 | cgrp->parent = parent; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4454 | cgrp->dummy_css.parent = &parent->dummy_css; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4455 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4456 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4457 | if (notify_on_release(parent)) |
| 4458 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4459 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4460 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4461 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4462 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4463 | for_each_root_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4464 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4465 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4466 | css = ss->css_alloc(cgroup_css(parent, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4467 | if (IS_ERR(css)) { |
| 4468 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4469 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4470 | } |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4471 | css_ar[ss->subsys_id] = css; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4472 | |
| 4473 | err = percpu_ref_init(&css->refcnt, css_release); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4474 | if (err) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4475 | goto err_free_all; |
| 4476 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4477 | init_css(css, ss, cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4478 | |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4479 | if (ss->use_id) { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4480 | err = alloc_css_id(css); |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4481 | if (err) |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4482 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4483 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4484 | } |
| 4485 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4486 | /* |
| 4487 | * Create directory. cgroup_create_file() returns with the new |
| 4488 | * directory locked on success so that it can be populated without |
| 4489 | * dropping cgroup_mutex. |
| 4490 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4491 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4492 | if (err < 0) |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4493 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4494 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4495 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4496 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4497 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4498 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4499 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4500 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4501 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4502 | /* each css holds a ref to the cgroup's dentry and the parent css */ |
| 4503 | for_each_root_subsys(root, ss) { |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4504 | struct cgroup_subsys_state *css = css_ar[ss->subsys_id]; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4505 | |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4506 | dget(dentry); |
Li Zhong | 930913a | 2013-08-16 17:57:14 +0800 | [diff] [blame] | 4507 | css_get(css->parent); |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4508 | } |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4509 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4510 | /* hold a ref to the parent's dentry */ |
| 4511 | dget(parent->dentry); |
| 4512 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4513 | /* creation succeeded, notify subsystems */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4514 | for_each_root_subsys(root, ss) { |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4515 | struct cgroup_subsys_state *css = css_ar[ss->subsys_id]; |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4516 | |
| 4517 | err = online_css(css); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4518 | if (err) |
| 4519 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4520 | |
| 4521 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4522 | parent->parent) { |
| 4523 | 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", |
| 4524 | current->comm, current->pid, ss->name); |
| 4525 | if (!strcmp(ss->name, "memory")) |
| 4526 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4527 | ss->warned_broken_hierarchy = true; |
| 4528 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4529 | } |
| 4530 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4531 | idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 4532 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4533 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4534 | if (err) |
| 4535 | goto err_destroy; |
| 4536 | |
| 4537 | err = cgroup_populate_dir(cgrp, root->subsys_mask); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4538 | if (err) |
| 4539 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4540 | |
| 4541 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4542 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4543 | |
| 4544 | return 0; |
| 4545 | |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4546 | err_free_all: |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4547 | for_each_root_subsys(root, ss) { |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4548 | struct cgroup_subsys_state *css = css_ar[ss->subsys_id]; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4549 | |
| 4550 | if (css) { |
| 4551 | percpu_ref_cancel_init(&css->refcnt); |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4552 | ss->css_free(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4553 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4554 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4555 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4556 | /* Release the reference count that we took on the superblock */ |
| 4557 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4558 | err_free_id: |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4559 | idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4560 | err_free_name: |
| 4561 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4562 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4563 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4564 | return err; |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4565 | |
| 4566 | err_destroy: |
| 4567 | cgroup_destroy_locked(cgrp); |
| 4568 | mutex_unlock(&cgroup_mutex); |
| 4569 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4570 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4571 | } |
| 4572 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4573 | 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] | 4574 | { |
| 4575 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4576 | |
| 4577 | /* the vfs holds inode->i_mutex already */ |
| 4578 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4579 | } |
| 4580 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4581 | /* |
| 4582 | * This is called when the refcnt of a css is confirmed to be killed. |
| 4583 | * css_tryget() is now guaranteed to fail. |
| 4584 | */ |
| 4585 | static void css_killed_work_fn(struct work_struct *work) |
| 4586 | { |
| 4587 | struct cgroup_subsys_state *css = |
| 4588 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 4589 | struct cgroup *cgrp = css->cgroup; |
| 4590 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4591 | mutex_lock(&cgroup_mutex); |
| 4592 | |
| 4593 | /* |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4594 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4595 | * initate destruction. |
| 4596 | */ |
| 4597 | offline_css(css); |
| 4598 | |
| 4599 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4600 | * If @cgrp is marked dead, it's waiting for refs of all css's to |
| 4601 | * be disabled before proceeding to the second phase of cgroup |
| 4602 | * destruction. If we are the last one, kick it off. |
| 4603 | */ |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4604 | if (!cgrp->nr_css && cgroup_is_dead(cgrp)) |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4605 | cgroup_destroy_css_killed(cgrp); |
| 4606 | |
| 4607 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4608 | |
| 4609 | /* |
| 4610 | * Put the css refs from kill_css(). Each css holds an extra |
| 4611 | * reference to the cgroup's dentry and cgroup removal proceeds |
| 4612 | * regardless of css refs. On the last put of each css, whenever |
| 4613 | * that may be, the extra dentry ref is put so that dentry |
| 4614 | * destruction happens only after all css's are released. |
| 4615 | */ |
| 4616 | css_put(css); |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | /* css kill confirmation processing requires process context, bounce */ |
| 4620 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4621 | { |
| 4622 | struct cgroup_subsys_state *css = |
| 4623 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4624 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4625 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
| 4626 | schedule_work(&css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | /** |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4630 | * kill_css - destroy a css |
| 4631 | * @css: css to destroy |
| 4632 | * |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4633 | * This function initiates destruction of @css by removing cgroup interface |
| 4634 | * files and putting its base reference. ->css_offline() will be invoked |
| 4635 | * asynchronously once css_tryget() is guaranteed to fail and when the |
| 4636 | * reference count reaches zero, @css will be released. |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4637 | */ |
| 4638 | static void kill_css(struct cgroup_subsys_state *css) |
| 4639 | { |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4640 | cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id); |
| 4641 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4642 | /* |
| 4643 | * Killing would put the base ref, but we need to keep it alive |
| 4644 | * until after ->css_offline(). |
| 4645 | */ |
| 4646 | css_get(css); |
| 4647 | |
| 4648 | /* |
| 4649 | * cgroup core guarantees that, by the time ->css_offline() is |
| 4650 | * invoked, no new css reference will be given out via |
| 4651 | * css_tryget(). We can't simply call percpu_ref_kill() and |
| 4652 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 4653 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 4654 | * |
| 4655 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4656 | * css is confirmed to be seen as killed on all CPUs. |
| 4657 | */ |
| 4658 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
| 4659 | } |
| 4660 | |
| 4661 | /** |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4662 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4663 | * @cgrp: cgroup to be destroyed |
| 4664 | * |
| 4665 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4666 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4667 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4668 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4669 | * the following two steps. |
| 4670 | * |
| 4671 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4672 | * userland visible parts and start killing the percpu refcnts of |
| 4673 | * css's. Set up so that the next stage will be kicked off once all |
| 4674 | * the percpu refcnts are confirmed to be killed. |
| 4675 | * |
| 4676 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4677 | * rest of destruction. Once all cgroup references are gone, the |
| 4678 | * cgroup is RCU-freed. |
| 4679 | * |
| 4680 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4681 | * the userland is concerned and a new cgroup with the same name may be |
| 4682 | * created. As cgroup doesn't care about the names internally, this |
| 4683 | * doesn't cause any problem. |
| 4684 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4685 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4686 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4687 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4688 | struct dentry *d = cgrp->dentry; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4689 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4690 | struct cgroup_subsys *ss; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4691 | bool empty; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4692 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4693 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4694 | lockdep_assert_held(&cgroup_mutex); |
| 4695 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4696 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4697 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4698 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4699 | */ |
| 4700 | read_lock(&css_set_lock); |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4701 | empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4702 | read_unlock(&css_set_lock); |
| 4703 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4704 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4705 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4706 | /* |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4707 | * Initiate massacre of all css's. cgroup_destroy_css_killed() |
| 4708 | * will be invoked to perform the rest of destruction once the |
| 4709 | * percpu refs of all css's are confirmed to be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4710 | */ |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4711 | for_each_root_subsys(cgrp->root, ss) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4712 | kill_css(cgroup_css(cgrp, ss)); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4713 | |
| 4714 | /* |
| 4715 | * Mark @cgrp dead. This prevents further task migration and child |
| 4716 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4717 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4718 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4719 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4720 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4721 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4722 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4723 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4724 | raw_spin_lock(&release_list_lock); |
| 4725 | if (!list_empty(&cgrp->release_list)) |
| 4726 | list_del_init(&cgrp->release_list); |
| 4727 | raw_spin_unlock(&release_list_lock); |
| 4728 | |
| 4729 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4730 | * If @cgrp has css's attached, the second stage of cgroup |
| 4731 | * destruction is kicked off from css_killed_work_fn() after the |
| 4732 | * refs of all attached css's are killed. If @cgrp doesn't have |
| 4733 | * any css, we kick it off here. |
| 4734 | */ |
| 4735 | if (!cgrp->nr_css) |
| 4736 | cgroup_destroy_css_killed(cgrp); |
| 4737 | |
| 4738 | /* |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4739 | * Clear the base files and remove @cgrp directory. The removal |
| 4740 | * puts the base ref but we aren't quite done with @cgrp yet, so |
| 4741 | * hold onto it. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4742 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4743 | cgroup_addrm_files(cgrp, cgroup_base_files, false); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4744 | dget(d); |
| 4745 | cgroup_d_remove_dir(d); |
| 4746 | |
| 4747 | /* |
| 4748 | * Unregister events and notify userspace. |
| 4749 | * Notify userspace about cgroup removing only after rmdir of cgroup |
| 4750 | * directory to avoid race between userspace and kernelspace. |
| 4751 | */ |
| 4752 | spin_lock(&cgrp->event_list_lock); |
| 4753 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
| 4754 | list_del_init(&event->list); |
| 4755 | schedule_work(&event->remove); |
| 4756 | } |
| 4757 | spin_unlock(&cgrp->event_list_lock); |
| 4758 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4759 | return 0; |
| 4760 | }; |
| 4761 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4762 | /** |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4763 | * cgroup_destroy_css_killed - the second step of cgroup destruction |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4764 | * @work: cgroup->destroy_free_work |
| 4765 | * |
| 4766 | * This function is invoked from a work item for a cgroup which is being |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4767 | * destroyed after all css's are offlined and performs the rest of |
| 4768 | * destruction. This is the second step of destruction described in the |
| 4769 | * comment above cgroup_destroy_locked(). |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4770 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4771 | static void cgroup_destroy_css_killed(struct cgroup *cgrp) |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4772 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4773 | struct cgroup *parent = cgrp->parent; |
| 4774 | struct dentry *d = cgrp->dentry; |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4775 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4776 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4777 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4778 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4779 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4780 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4781 | /* |
| 4782 | * We should remove the cgroup object from idr before its grace |
| 4783 | * period starts, so we won't be looking up a cgroup while the |
| 4784 | * cgroup is being freed. |
| 4785 | */ |
| 4786 | idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 4787 | cgrp->id = -1; |
| 4788 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4789 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4790 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4791 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4792 | check_for_release(parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4793 | } |
| 4794 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4795 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4796 | { |
| 4797 | int ret; |
| 4798 | |
| 4799 | mutex_lock(&cgroup_mutex); |
| 4800 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4801 | mutex_unlock(&cgroup_mutex); |
| 4802 | |
| 4803 | return ret; |
| 4804 | } |
| 4805 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4806 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4807 | { |
| 4808 | INIT_LIST_HEAD(&ss->cftsets); |
| 4809 | |
| 4810 | /* |
| 4811 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4812 | * deregistration. |
| 4813 | */ |
| 4814 | if (ss->base_cftypes) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4815 | struct cftype *cft; |
| 4816 | |
| 4817 | for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++) |
| 4818 | cft->ss = ss; |
| 4819 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4820 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4821 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4822 | } |
| 4823 | } |
| 4824 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4825 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4826 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4827 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4828 | |
| 4829 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4830 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4831 | mutex_lock(&cgroup_mutex); |
| 4832 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4833 | /* init base cftset */ |
| 4834 | cgroup_init_cftsets(ss); |
| 4835 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4836 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4837 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4838 | ss->root = &cgroup_dummy_root; |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4839 | css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4840 | /* We don't handle early failures gracefully */ |
| 4841 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4842 | init_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4843 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4844 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4845 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4846 | * newly registered, all tasks and hence the |
| 4847 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4848 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4849 | |
| 4850 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4851 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4852 | /* At system boot, before all subsystems have been |
| 4853 | * registered, no tasks have been forked, so we don't |
| 4854 | * need to invoke fork callbacks here. */ |
| 4855 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4856 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4857 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4858 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4859 | mutex_unlock(&cgroup_mutex); |
| 4860 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4861 | /* this function shouldn't be used with modular subsystems, since they |
| 4862 | * need to register a subsys_id, among other things */ |
| 4863 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4867 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4868 | * @ss: the subsystem to load |
| 4869 | * |
| 4870 | * 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] | 4871 | * 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] | 4872 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4873 | * simpler cgroup_init_subsys. |
| 4874 | */ |
| 4875 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4876 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4877 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4878 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4879 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4880 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4881 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4882 | |
| 4883 | /* check name and function validity */ |
| 4884 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4885 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4886 | return -EINVAL; |
| 4887 | |
| 4888 | /* |
| 4889 | * we don't support callbacks in modular subsystems. this check is |
| 4890 | * before the ss->module check for consistency; a subsystem that could |
| 4891 | * be a module should still have no callbacks even if the user isn't |
| 4892 | * compiling it as one. |
| 4893 | */ |
| 4894 | if (ss->fork || ss->exit) |
| 4895 | return -EINVAL; |
| 4896 | |
| 4897 | /* |
| 4898 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4899 | * since cgroup_init_subsys will have already taken care of it. |
| 4900 | */ |
| 4901 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4902 | /* a sanity check */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4903 | BUG_ON(cgroup_subsys[ss->subsys_id] != ss); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4904 | return 0; |
| 4905 | } |
| 4906 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4907 | /* init base cftset */ |
| 4908 | cgroup_init_cftsets(ss); |
| 4909 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4910 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4911 | cgroup_subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4912 | |
| 4913 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4914 | * no ss->css_alloc seems to need anything important in the ss |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4915 | * struct, so this can happen first (i.e. before the dummy root |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4916 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4917 | */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4918 | css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4919 | if (IS_ERR(css)) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4920 | /* failure case - need to deassign the cgroup_subsys[] slot. */ |
| 4921 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4922 | mutex_unlock(&cgroup_mutex); |
| 4923 | return PTR_ERR(css); |
| 4924 | } |
| 4925 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4926 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4927 | ss->root = &cgroup_dummy_root; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4928 | |
| 4929 | /* our new subsystem will be attached to the dummy hierarchy. */ |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4930 | init_css(css, ss, cgroup_dummy_top); |
| 4931 | /* init_idr must be after init_css() because it sets css->id. */ |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4932 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4933 | ret = cgroup_init_idr(ss, css); |
| 4934 | if (ret) |
| 4935 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | /* |
| 4939 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4940 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4941 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4942 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4943 | * table state, so each changed css_set will need its hash recomputed. |
| 4944 | * this is all done under the css_set_lock. |
| 4945 | */ |
| 4946 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4947 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4948 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4949 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4950 | continue; |
| 4951 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4952 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4953 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4954 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4955 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4956 | key = css_set_hash(cset->subsys); |
| 4957 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4958 | } |
| 4959 | write_unlock(&css_set_lock); |
| 4960 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4961 | ret = online_css(css); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4962 | if (ret) |
| 4963 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4964 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4965 | /* success! */ |
| 4966 | mutex_unlock(&cgroup_mutex); |
| 4967 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4968 | |
| 4969 | err_unload: |
| 4970 | mutex_unlock(&cgroup_mutex); |
| 4971 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4972 | cgroup_unload_subsys(ss); |
| 4973 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4974 | } |
| 4975 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4976 | |
| 4977 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4978 | * cgroup_unload_subsys: unload a modular subsystem |
| 4979 | * @ss: the subsystem to unload |
| 4980 | * |
| 4981 | * This function should be called in a modular subsystem's exitcall. When this |
| 4982 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4983 | * the subsystem will not be attached to any hierarchy. |
| 4984 | */ |
| 4985 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4986 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4987 | struct cgrp_cset_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4988 | |
| 4989 | BUG_ON(ss->module == NULL); |
| 4990 | |
| 4991 | /* |
| 4992 | * we shouldn't be called if the subsystem is in use, and the use of |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 4993 | * try_module_get() in rebind_subsystems() should ensure that it |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4994 | * doesn't start being used while we're killing it off. |
| 4995 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4996 | BUG_ON(ss->root != &cgroup_dummy_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4997 | |
| 4998 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4999 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5000 | offline_css(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5001 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 5002 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5003 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5004 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5005 | /* deassign the subsys_id */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 5006 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5007 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 5008 | /* remove subsystem from the dummy root's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5009 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5010 | |
| 5011 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 5012 | * disentangle the css from all css_sets attached to the dummy |
| 5013 | * top. as in loading, we need to pay our respects to the hashtable |
| 5014 | * gods. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5015 | */ |
| 5016 | write_lock(&css_set_lock); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 5017 | list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5018 | struct css_set *cset = link->cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 5019 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5020 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5021 | hash_del(&cset->hlist); |
| 5022 | cset->subsys[ss->subsys_id] = NULL; |
| 5023 | key = css_set_hash(cset->subsys); |
| 5024 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5025 | } |
| 5026 | write_unlock(&css_set_lock); |
| 5027 | |
| 5028 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 5029 | * remove subsystem's css from the cgroup_dummy_top and free it - |
| 5030 | * need to free before marking as null because ss->css_free needs |
| 5031 | * the cgrp->subsys pointer to find their state. note that this |
| 5032 | * also takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5033 | */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5034 | ss->css_free(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 5035 | RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5036 | |
| 5037 | mutex_unlock(&cgroup_mutex); |
| 5038 | } |
| 5039 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 5040 | |
| 5041 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5042 | * cgroup_init_early - cgroup initialization at system boot |
| 5043 | * |
| 5044 | * Initialize cgroups at system boot, and initialize any |
| 5045 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5046 | */ |
| 5047 | int __init cgroup_init_early(void) |
| 5048 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5049 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5050 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5051 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 5052 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5053 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5054 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 5055 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5056 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 5057 | init_cgroup_root(&cgroup_dummy_root); |
| 5058 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5059 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5060 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5061 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 5062 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 5063 | 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] | 5064 | 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] | 5065 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5066 | /* at bootup time, we don't worry about modular subsystems */ |
| 5067 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5068 | BUG_ON(!ss->name); |
| 5069 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5070 | BUG_ON(!ss->css_alloc); |
| 5071 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5072 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 5073 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5074 | ss->name, ss->subsys_id); |
| 5075 | BUG(); |
| 5076 | } |
| 5077 | |
| 5078 | if (ss->early_init) |
| 5079 | cgroup_init_subsys(ss); |
| 5080 | } |
| 5081 | return 0; |
| 5082 | } |
| 5083 | |
| 5084 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5085 | * cgroup_init - cgroup initialization |
| 5086 | * |
| 5087 | * Register cgroup filesystem and /proc file, and initialize |
| 5088 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5089 | */ |
| 5090 | int __init cgroup_init(void) |
| 5091 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5092 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 5093 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5094 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5095 | |
| 5096 | err = bdi_init(&cgroup_backing_dev_info); |
| 5097 | if (err) |
| 5098 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5099 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5100 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5101 | if (!ss->early_init) |
| 5102 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5103 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5104 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5105 | } |
| 5106 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 5107 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5108 | mutex_lock(&cgroup_mutex); |
| 5109 | mutex_lock(&cgroup_root_mutex); |
| 5110 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5111 | /* Add init_css_set to the hash table */ |
| 5112 | key = css_set_hash(init_css_set.subsys); |
| 5113 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 5114 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5115 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5116 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 5117 | err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top, |
| 5118 | 0, 1, GFP_KERNEL); |
| 5119 | BUG_ON(err < 0); |
| 5120 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5121 | mutex_unlock(&cgroup_root_mutex); |
| 5122 | mutex_unlock(&cgroup_mutex); |
| 5123 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5124 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 5125 | if (!cgroup_kobj) { |
| 5126 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5127 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | err = register_filesystem(&cgroup_fs_type); |
| 5131 | if (err < 0) { |
| 5132 | kobject_put(cgroup_kobj); |
| 5133 | goto out; |
| 5134 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5135 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 5136 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5137 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5138 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5139 | if (err) |
| 5140 | bdi_destroy(&cgroup_backing_dev_info); |
| 5141 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5142 | return err; |
| 5143 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5144 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5145 | /* |
| 5146 | * proc_cgroup_show() |
| 5147 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 5148 | * - Used for /proc/<pid>/cgroup. |
| 5149 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 5150 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 5151 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5152 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 5153 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 5154 | * cgroup to top_cgroup. |
| 5155 | */ |
| 5156 | |
| 5157 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 5158 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5159 | { |
| 5160 | struct pid *pid; |
| 5161 | struct task_struct *tsk; |
| 5162 | char *buf; |
| 5163 | int retval; |
| 5164 | struct cgroupfs_root *root; |
| 5165 | |
| 5166 | retval = -ENOMEM; |
| 5167 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 5168 | if (!buf) |
| 5169 | goto out; |
| 5170 | |
| 5171 | retval = -ESRCH; |
| 5172 | pid = m->private; |
| 5173 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 5174 | if (!tsk) |
| 5175 | goto out_free; |
| 5176 | |
| 5177 | retval = 0; |
| 5178 | |
| 5179 | mutex_lock(&cgroup_mutex); |
| 5180 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 5181 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5182 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5183 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5184 | int count = 0; |
| 5185 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5186 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 5187 | for_each_root_subsys(root, ss) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5188 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 5189 | if (strlen(root->name)) |
| 5190 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 5191 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5192 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5193 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5194 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5195 | if (retval < 0) |
| 5196 | goto out_unlock; |
| 5197 | seq_puts(m, buf); |
| 5198 | seq_putc(m, '\n'); |
| 5199 | } |
| 5200 | |
| 5201 | out_unlock: |
| 5202 | mutex_unlock(&cgroup_mutex); |
| 5203 | put_task_struct(tsk); |
| 5204 | out_free: |
| 5205 | kfree(buf); |
| 5206 | out: |
| 5207 | return retval; |
| 5208 | } |
| 5209 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5210 | /* Display information about each subsystem and each hierarchy */ |
| 5211 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 5212 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5213 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5214 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5215 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5216 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 5217 | /* |
| 5218 | * ideally we don't want subsystems moving around while we do this. |
| 5219 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 5220 | * subsys/hierarchy state. |
| 5221 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5222 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5223 | |
| 5224 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5225 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 5226 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5227 | ss->root->number_of_cgroups, !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5228 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5229 | mutex_unlock(&cgroup_mutex); |
| 5230 | return 0; |
| 5231 | } |
| 5232 | |
| 5233 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 5234 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 5235 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5236 | } |
| 5237 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 5238 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5239 | .open = cgroupstats_open, |
| 5240 | .read = seq_read, |
| 5241 | .llseek = seq_lseek, |
| 5242 | .release = single_release, |
| 5243 | }; |
| 5244 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5245 | /** |
| 5246 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5247 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5248 | * |
| 5249 | * Description: A task inherits its parent's cgroup at fork(). |
| 5250 | * |
| 5251 | * A pointer to the shared css_set was automatically copied in |
| 5252 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5253 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 5254 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 5255 | * have already changed current->cgroups, allowing the previously |
| 5256 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5257 | * |
| 5258 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 5259 | * task, and the passed argument 'child' points to the child task. |
| 5260 | */ |
| 5261 | void cgroup_fork(struct task_struct *child) |
| 5262 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5263 | task_lock(current); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5264 | get_css_set(task_css_set(current)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5265 | child->cgroups = current->cgroups; |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5266 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5267 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5271 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5272 | * @child: the task in question |
| 5273 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5274 | * Adds the task to the list running through its css_set if necessary and |
| 5275 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5276 | * visible on the task list in case we race with the first call to |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 5277 | * cgroup_task_iter_start() - to guarantee that the new task ends up on its |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5278 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5279 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5280 | void cgroup_post_fork(struct task_struct *child) |
| 5281 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5282 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5283 | int i; |
| 5284 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5285 | /* |
| 5286 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 5287 | * under the tasklist_lock and we read it here after we added the child |
| 5288 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 5289 | * yet in the tasklist when we walked through it from |
| 5290 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 5291 | * should be visible now due to the paired locking and barriers implied |
| 5292 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 5293 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 5294 | * lock on fork. |
| 5295 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5296 | if (use_task_css_set_links) { |
| 5297 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5298 | task_lock(child); |
| 5299 | if (list_empty(&child->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5300 | list_add(&child->cg_list, &task_css_set(child)->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5301 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5302 | write_unlock(&css_set_lock); |
| 5303 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5304 | |
| 5305 | /* |
| 5306 | * Call ss->fork(). This must happen after @child is linked on |
| 5307 | * css_set; otherwise, @child might change state between ->fork() |
| 5308 | * and addition to css_set. |
| 5309 | */ |
| 5310 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5311 | /* |
| 5312 | * fork/exit callbacks are supported only for builtin |
| 5313 | * subsystems, and the builtin section of the subsys |
| 5314 | * array is immutable, so we don't need to lock the |
| 5315 | * subsys array here. On the other hand, modular section |
| 5316 | * of the array can be freed at module unload, so we |
| 5317 | * can't touch that. |
| 5318 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5319 | for_each_builtin_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5320 | if (ss->fork) |
| 5321 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5322 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5323 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5324 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5325 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5326 | * cgroup_exit - detach cgroup from exiting task |
| 5327 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5328 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5329 | * |
| 5330 | * Description: Detach cgroup from @tsk and release it. |
| 5331 | * |
| 5332 | * Note that cgroups marked notify_on_release force every task in |
| 5333 | * them to take the global cgroup_mutex mutex when exiting. |
| 5334 | * This could impact scaling on very large systems. Be reluctant to |
| 5335 | * use notify_on_release cgroups where very high task exit scaling |
| 5336 | * is required on large systems. |
| 5337 | * |
| 5338 | * the_top_cgroup_hack: |
| 5339 | * |
| 5340 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 5341 | * |
| 5342 | * We call cgroup_exit() while the task is still competent to |
| 5343 | * handle notify_on_release(), then leave the task attached to the |
| 5344 | * root cgroup in each hierarchy for the remainder of its exit. |
| 5345 | * |
| 5346 | * To do this properly, we would increment the reference count on |
| 5347 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 5348 | * code we would add a second cgroup function call, to drop that |
| 5349 | * reference. This would just create an unnecessary hot spot on |
| 5350 | * the top_cgroup reference count, to no avail. |
| 5351 | * |
| 5352 | * Normally, holding a reference to a cgroup without bumping its |
| 5353 | * count is unsafe. The cgroup could go away, or someone could |
| 5354 | * attach us to a different cgroup, decrementing the count on |
| 5355 | * the first cgroup that we never incremented. But in this case, |
| 5356 | * 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] | 5357 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5358 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5359 | */ |
| 5360 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5361 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5362 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5363 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5364 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5365 | |
| 5366 | /* |
| 5367 | * Unlink from the css_set task list if necessary. |
| 5368 | * Optimistically check cg_list before taking |
| 5369 | * css_set_lock |
| 5370 | */ |
| 5371 | if (!list_empty(&tsk->cg_list)) { |
| 5372 | write_lock(&css_set_lock); |
| 5373 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5374 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5375 | write_unlock(&css_set_lock); |
| 5376 | } |
| 5377 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5378 | /* Reassign the task to the init_css_set. */ |
| 5379 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5380 | cset = task_css_set(tsk); |
| 5381 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5382 | |
| 5383 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5384 | /* |
| 5385 | * fork/exit callbacks are supported only for builtin |
| 5386 | * subsystems, see cgroup_post_fork() for details. |
| 5387 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5388 | for_each_builtin_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5389 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5390 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 5391 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5392 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5393 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5394 | } |
| 5395 | } |
| 5396 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5397 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5398 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5399 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5400 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5401 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5402 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5403 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5404 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5405 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5406 | /* |
| 5407 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5408 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5409 | * it now |
| 5410 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5411 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5412 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5413 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5414 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5415 | list_empty(&cgrp->release_list)) { |
| 5416 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5417 | need_schedule_work = 1; |
| 5418 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5419 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5420 | if (need_schedule_work) |
| 5421 | schedule_work(&release_agent_work); |
| 5422 | } |
| 5423 | } |
| 5424 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5425 | /* |
| 5426 | * Notify userspace when a cgroup is released, by running the |
| 5427 | * configured release agent with the name of the cgroup (path |
| 5428 | * relative to the root of cgroup file system) as the argument. |
| 5429 | * |
| 5430 | * Most likely, this user command will try to rmdir this cgroup. |
| 5431 | * |
| 5432 | * This races with the possibility that some other task will be |
| 5433 | * attached to this cgroup before it is removed, or that some other |
| 5434 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5435 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5436 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5437 | * to continue to serve a useful existence. Next time it's released, |
| 5438 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5439 | * |
| 5440 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5441 | * means only wait until the task is successfully execve()'d. The |
| 5442 | * separate release agent task is forked by call_usermodehelper(), |
| 5443 | * then control in this thread returns here, without waiting for the |
| 5444 | * release agent task. We don't bother to wait because the caller of |
| 5445 | * this routine has no use for the exit status of the release agent |
| 5446 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5447 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5448 | static void cgroup_release_agent(struct work_struct *work) |
| 5449 | { |
| 5450 | BUG_ON(work != &release_agent_work); |
| 5451 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5452 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5453 | while (!list_empty(&release_list)) { |
| 5454 | char *argv[3], *envp[3]; |
| 5455 | int i; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5456 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5457 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5458 | struct cgroup, |
| 5459 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5460 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5461 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5462 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5463 | if (!pathbuf) |
| 5464 | goto continue_free; |
| 5465 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5466 | goto continue_free; |
| 5467 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5468 | if (!agentbuf) |
| 5469 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5470 | |
| 5471 | i = 0; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5472 | argv[i++] = agentbuf; |
| 5473 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5474 | argv[i] = NULL; |
| 5475 | |
| 5476 | i = 0; |
| 5477 | /* minimal command environment */ |
| 5478 | envp[i++] = "HOME=/"; |
| 5479 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5480 | envp[i] = NULL; |
| 5481 | |
| 5482 | /* Drop the lock while we invoke the usermode helper, |
| 5483 | * since the exec could involve hitting disk and hence |
| 5484 | * be a slow process */ |
| 5485 | mutex_unlock(&cgroup_mutex); |
| 5486 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5487 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5488 | continue_free: |
| 5489 | kfree(pathbuf); |
| 5490 | kfree(agentbuf); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5491 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5492 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5493 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5494 | mutex_unlock(&cgroup_mutex); |
| 5495 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5496 | |
| 5497 | static int __init cgroup_disable(char *str) |
| 5498 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5499 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5500 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5501 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5502 | |
| 5503 | while ((token = strsep(&str, ",")) != NULL) { |
| 5504 | if (!*token) |
| 5505 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5506 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5507 | /* |
| 5508 | * cgroup_disable, being at boot time, can't know about |
| 5509 | * module subsystems, so we don't worry about them. |
| 5510 | */ |
| 5511 | for_each_builtin_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5512 | if (!strcmp(token, ss->name)) { |
| 5513 | ss->disabled = 1; |
| 5514 | printk(KERN_INFO "Disabling %s control group" |
| 5515 | " subsystem\n", ss->name); |
| 5516 | break; |
| 5517 | } |
| 5518 | } |
| 5519 | } |
| 5520 | return 1; |
| 5521 | } |
| 5522 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5523 | |
| 5524 | /* |
| 5525 | * Functons for CSS ID. |
| 5526 | */ |
| 5527 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5528 | /* 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] | 5529 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5530 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5531 | struct css_id *cssid; |
| 5532 | |
| 5533 | /* |
| 5534 | * This css_id() can return correct value when somone has refcnt |
| 5535 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5536 | * it's unchanged until freed. |
| 5537 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5538 | cssid = rcu_dereference_raw(css->id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5539 | |
| 5540 | if (cssid) |
| 5541 | return cssid->id; |
| 5542 | return 0; |
| 5543 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5544 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5545 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5546 | /** |
| 5547 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5548 | * @child: the css to be tested. |
| 5549 | * @root: the css supporsed to be an ancestor of the child. |
| 5550 | * |
| 5551 | * 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] | 5552 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5553 | * But, considering usual usage, the csses should be valid objects after test. |
| 5554 | * Assuming that the caller will do some action to the child if this returns |
| 5555 | * returns true, the caller must take "child";s reference count. |
| 5556 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5557 | */ |
| 5558 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5559 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5560 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5561 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5562 | struct css_id *child_id; |
| 5563 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5564 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5565 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5566 | if (!child_id) |
| 5567 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5568 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5569 | if (!root_id) |
| 5570 | return false; |
| 5571 | if (child_id->depth < root_id->depth) |
| 5572 | return false; |
| 5573 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5574 | return false; |
| 5575 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5576 | } |
| 5577 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5578 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5579 | { |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5580 | struct css_id *id = rcu_dereference_protected(css->id, true); |
| 5581 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5582 | /* When this is called before css_id initialization, id can be NULL */ |
| 5583 | if (!id) |
| 5584 | return; |
| 5585 | |
| 5586 | BUG_ON(!ss->use_id); |
| 5587 | |
| 5588 | rcu_assign_pointer(id->css, NULL); |
| 5589 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5590 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5591 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5592 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5593 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5594 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5595 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5596 | |
| 5597 | /* |
| 5598 | * This is called by init or create(). Then, calls to this function are |
| 5599 | * always serialized (By cgroup_mutex() at create()). |
| 5600 | */ |
| 5601 | |
| 5602 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5603 | { |
| 5604 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5605 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5606 | |
| 5607 | BUG_ON(!ss->use_id); |
| 5608 | |
| 5609 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5610 | newid = kzalloc(size, GFP_KERNEL); |
| 5611 | if (!newid) |
| 5612 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5613 | |
| 5614 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5615 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5616 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5617 | 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] | 5618 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5619 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5620 | |
| 5621 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5622 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5623 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5624 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5625 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5626 | newid->depth = depth; |
| 5627 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5628 | err_out: |
| 5629 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5630 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5631 | |
| 5632 | } |
| 5633 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5634 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5635 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5636 | { |
| 5637 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5638 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5639 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5640 | idr_init(&ss->idr); |
| 5641 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5642 | newid = get_new_cssid(ss, 0); |
| 5643 | if (IS_ERR(newid)) |
| 5644 | return PTR_ERR(newid); |
| 5645 | |
| 5646 | newid->stack[0] = newid->id; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5647 | RCU_INIT_POINTER(newid->css, rootcss); |
| 5648 | RCU_INIT_POINTER(rootcss->id, newid); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5649 | return 0; |
| 5650 | } |
| 5651 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 5652 | static int alloc_css_id(struct cgroup_subsys_state *child_css) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5653 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 5654 | struct cgroup_subsys_state *parent_css = css_parent(child_css); |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5655 | struct css_id *child_id, *parent_id; |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 5656 | int i, depth; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5657 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5658 | parent_id = rcu_dereference_protected(parent_css->id, true); |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5659 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5660 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 5661 | child_id = get_new_cssid(child_css->ss, depth); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5662 | if (IS_ERR(child_id)) |
| 5663 | return PTR_ERR(child_id); |
| 5664 | |
| 5665 | for (i = 0; i < depth; i++) |
| 5666 | child_id->stack[i] = parent_id->stack[i]; |
| 5667 | child_id->stack[depth] = child_id->id; |
| 5668 | /* |
| 5669 | * child_id->css pointer will be set after this cgroup is available |
| 5670 | * see cgroup_populate_dir() |
| 5671 | */ |
| 5672 | rcu_assign_pointer(child_css->id, child_id); |
| 5673 | |
| 5674 | return 0; |
| 5675 | } |
| 5676 | |
| 5677 | /** |
| 5678 | * css_lookup - lookup css by id |
| 5679 | * @ss: cgroup subsys to be looked into. |
| 5680 | * @id: the id |
| 5681 | * |
| 5682 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5683 | * NULL if not. Should be called under rcu_read_lock() |
| 5684 | */ |
| 5685 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5686 | { |
| 5687 | struct css_id *cssid = NULL; |
| 5688 | |
| 5689 | BUG_ON(!ss->use_id); |
| 5690 | cssid = idr_find(&ss->idr, id); |
| 5691 | |
| 5692 | if (unlikely(!cssid)) |
| 5693 | return NULL; |
| 5694 | |
| 5695 | return rcu_dereference(cssid->css); |
| 5696 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5697 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5698 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5699 | /** |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5700 | * css_from_dir - get corresponding css from the dentry of a cgroup dir |
| 5701 | * @dentry: directory dentry of interest |
| 5702 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5703 | * |
| 5704 | * Must be called under RCU read lock. The caller is responsible for |
| 5705 | * pinning the returned css if it needs to be accessed outside the RCU |
| 5706 | * critical section. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5707 | */ |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5708 | struct cgroup_subsys_state *css_from_dir(struct dentry *dentry, |
| 5709 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5710 | { |
| 5711 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5712 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5713 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 5714 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5715 | /* is @dentry a cgroup dir? */ |
| 5716 | if (!dentry->d_inode || |
| 5717 | dentry->d_inode->i_op != &cgroup_dir_inode_operations) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5718 | return ERR_PTR(-EBADF); |
| 5719 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5720 | cgrp = __d_cgrp(dentry); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5721 | return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5722 | } |
| 5723 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5724 | /** |
| 5725 | * css_from_id - lookup css by id |
| 5726 | * @id: the cgroup id |
| 5727 | * @ss: cgroup subsys to be looked into |
| 5728 | * |
| 5729 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5730 | * Should be called under rcu_read_lock(). |
| 5731 | */ |
| 5732 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5733 | { |
| 5734 | struct cgroup *cgrp; |
| 5735 | |
| 5736 | rcu_lockdep_assert(rcu_read_lock_held() || |
| 5737 | lockdep_is_held(&cgroup_mutex), |
| 5738 | "css_from_id() needs proper protection"); |
| 5739 | |
| 5740 | cgrp = idr_find(&ss->root->cgroup_idr, id); |
| 5741 | if (cgrp) |
| 5742 | return cgroup_css(cgrp, ss->subsys_id); |
| 5743 | return NULL; |
| 5744 | } |
| 5745 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5746 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5747 | static struct cgroup_subsys_state * |
| 5748 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5749 | { |
| 5750 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5751 | |
| 5752 | if (!css) |
| 5753 | return ERR_PTR(-ENOMEM); |
| 5754 | |
| 5755 | return css; |
| 5756 | } |
| 5757 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5758 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5759 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5760 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5761 | } |
| 5762 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5763 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 5764 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5765 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5766 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5767 | } |
| 5768 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5769 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 5770 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5771 | { |
| 5772 | return (u64)(unsigned long)current->cgroups; |
| 5773 | } |
| 5774 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5775 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5776 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5777 | { |
| 5778 | u64 count; |
| 5779 | |
| 5780 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5781 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5782 | rcu_read_unlock(); |
| 5783 | return count; |
| 5784 | } |
| 5785 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5786 | static int current_css_set_cg_links_read(struct cgroup_subsys_state *css, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5787 | struct cftype *cft, |
| 5788 | struct seq_file *seq) |
| 5789 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5790 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5791 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5792 | |
| 5793 | read_lock(&css_set_lock); |
| 5794 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5795 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5796 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5797 | struct cgroup *c = link->cgrp; |
| 5798 | const char *name; |
| 5799 | |
| 5800 | if (c->dentry) |
| 5801 | name = c->dentry->d_name.name; |
| 5802 | else |
| 5803 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5804 | seq_printf(seq, "Root %d group %s\n", |
| 5805 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5806 | } |
| 5807 | rcu_read_unlock(); |
| 5808 | read_unlock(&css_set_lock); |
| 5809 | return 0; |
| 5810 | } |
| 5811 | |
| 5812 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5813 | static int cgroup_css_links_read(struct cgroup_subsys_state *css, |
| 5814 | struct cftype *cft, struct seq_file *seq) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5815 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5816 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5817 | |
| 5818 | read_lock(&css_set_lock); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5819 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5820 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5821 | struct task_struct *task; |
| 5822 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5823 | seq_printf(seq, "css_set %p\n", cset); |
| 5824 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5825 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5826 | seq_puts(seq, " ...\n"); |
| 5827 | break; |
| 5828 | } else { |
| 5829 | seq_printf(seq, " task %d\n", |
| 5830 | task_pid_vnr(task)); |
| 5831 | } |
| 5832 | } |
| 5833 | } |
| 5834 | read_unlock(&css_set_lock); |
| 5835 | return 0; |
| 5836 | } |
| 5837 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5838 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5839 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5840 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5841 | } |
| 5842 | |
| 5843 | static struct cftype debug_files[] = { |
| 5844 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5845 | .name = "taskcount", |
| 5846 | .read_u64 = debug_taskcount_read, |
| 5847 | }, |
| 5848 | |
| 5849 | { |
| 5850 | .name = "current_css_set", |
| 5851 | .read_u64 = current_css_set_read, |
| 5852 | }, |
| 5853 | |
| 5854 | { |
| 5855 | .name = "current_css_set_refcount", |
| 5856 | .read_u64 = current_css_set_refcount_read, |
| 5857 | }, |
| 5858 | |
| 5859 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5860 | .name = "current_css_set_cg_links", |
| 5861 | .read_seq_string = current_css_set_cg_links_read, |
| 5862 | }, |
| 5863 | |
| 5864 | { |
| 5865 | .name = "cgroup_css_links", |
| 5866 | .read_seq_string = cgroup_css_links_read, |
| 5867 | }, |
| 5868 | |
| 5869 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5870 | .name = "releasable", |
| 5871 | .read_u64 = releasable_read, |
| 5872 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5873 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5874 | { } /* terminate */ |
| 5875 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5876 | |
| 5877 | struct cgroup_subsys debug_subsys = { |
| 5878 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5879 | .css_alloc = debug_css_alloc, |
| 5880 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5881 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5882 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5883 | }; |
| 5884 | #endif /* CONFIG_CGROUP_DEBUG */ |