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 | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 43 | #include <linux/slab.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 44 | #include <linux/spinlock.h> |
| 45 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 46 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 47 | #include <linux/kmod.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 48 | #include <linux/delayacct.h> |
| 49 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 50 | #include <linux/hashtable.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 51 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 52 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 53 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 54 | #include <linux/flex_array.h> /* used in cgroup_attach_task */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 55 | #include <linux/kthread.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 56 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 57 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 58 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 59 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 60 | * pidlists linger the following amount before being destroyed. The goal |
| 61 | * is avoiding frequent destruction in the middle of consecutive read calls |
| 62 | * Expiring in the middle is a performance problem not a correctness one. |
| 63 | * 1 sec should be enough. |
| 64 | */ |
| 65 | #define CGROUP_PIDLIST_DESTROY_DELAY HZ |
| 66 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 67 | #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ |
| 68 | MAX_CFTYPE_NAME + 2) |
| 69 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 70 | /* |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 71 | * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file |
| 72 | * creation/removal and hierarchy changing operations including cgroup |
| 73 | * creation, removal, css association and controller rebinding. This outer |
| 74 | * lock is needed mainly to resolve the circular dependency between kernfs |
| 75 | * active ref and cgroup_mutex. cgroup_tree_mutex nests above both. |
| 76 | */ |
| 77 | static DEFINE_MUTEX(cgroup_tree_mutex); |
| 78 | |
| 79 | /* |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 80 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 81 | * hierarchy must be performed while holding it. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 82 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 83 | #ifdef CONFIG_PROVE_RCU |
| 84 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 85 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 86 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 87 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 88 | #endif |
| 89 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 90 | /* |
| 91 | * Protects cgroup_subsys->release_agent_path. Modifying it also requires |
| 92 | * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock. |
| 93 | */ |
| 94 | static DEFINE_SPINLOCK(release_agent_path_lock); |
| 95 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 96 | #define cgroup_assert_mutexes_or_rcu_locked() \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 97 | rcu_lockdep_assert(rcu_read_lock_held() || \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 98 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 99 | lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 100 | "cgroup_[tree_]mutex or RCU read lock required"); |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 101 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 102 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 103 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 104 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 105 | * destruction work items don't end up filling up max_active of system_wq |
| 106 | * which may lead to deadlock. |
| 107 | */ |
| 108 | static struct workqueue_struct *cgroup_destroy_wq; |
| 109 | |
| 110 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 111 | * pidlist destructions need to be flushed on cgroup destruction. Use a |
| 112 | * separate workqueue as flush domain. |
| 113 | */ |
| 114 | static struct workqueue_struct *cgroup_pidlist_destroy_wq; |
| 115 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 116 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 117 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 118 | static struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 119 | #include <linux/cgroup_subsys.h> |
| 120 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 121 | #undef SUBSYS |
| 122 | |
| 123 | /* array of cgroup subsystem names */ |
| 124 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 125 | static const char *cgroup_subsys_name[] = { |
| 126 | #include <linux/cgroup_subsys.h> |
| 127 | }; |
| 128 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 129 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 130 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 131 | * The dummy hierarchy, reserved for the subsystems that are otherwise |
| 132 | * unattached - it never has more than a single cgroup, and all tasks are |
| 133 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 134 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 135 | static struct cgroupfs_root cgroup_dummy_root; |
| 136 | |
| 137 | /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ |
| 138 | static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 139 | |
| 140 | /* The list of hierarchy roots */ |
| 141 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 142 | static LIST_HEAD(cgroup_roots); |
| 143 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 144 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 145 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 146 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 147 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 148 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 149 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 150 | /* |
| 151 | * Assign a monotonically increasing serial number to cgroups. It |
| 152 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 153 | * numbers. Also, as cgroups are always appended to the parent's |
| 154 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 155 | * the ascending serial number order on the list. Protected by |
| 156 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 157 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 158 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 159 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 160 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 161 | * check for fork/exit handlers to call. This avoids us having to do |
| 162 | * extra work in the fork/exit path if none of the subsystems need to |
| 163 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 164 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 165 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 166 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 167 | static struct cftype cgroup_base_files[]; |
| 168 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 169 | static void cgroup_put(struct cgroup *cgrp); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 170 | static int rebind_subsystems(struct cgroupfs_root *root, |
| 171 | unsigned long added_mask, unsigned removed_mask); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 172 | static void cgroup_destroy_css_killed(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 173 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 174 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 175 | bool is_add); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 176 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 177 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 178 | /** |
| 179 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 180 | * @cgrp: the cgroup of interest |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 181 | * @ss: the subsystem of interest (%NULL returns the dummy_css) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 182 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 183 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 184 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 185 | * the caller is responsible for pinning the returned css if it wants to |
| 186 | * keep accessing it outside the said locks. This function may return |
| 187 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 188 | */ |
| 189 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 190 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 191 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 192 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 193 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 194 | lockdep_is_held(&cgroup_tree_mutex) || |
| 195 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 196 | else |
| 197 | return &cgrp->dummy_css; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 198 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 199 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 200 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 201 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 202 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 203 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 206 | struct cgroup_subsys_state *seq_css(struct seq_file *seq) |
| 207 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 208 | struct kernfs_open_file *of = seq->private; |
| 209 | struct cgroup *cgrp = of->kn->parent->priv; |
| 210 | struct cftype *cft = seq_cft(seq); |
| 211 | |
| 212 | /* |
| 213 | * This is open and unprotected implementation of cgroup_css(). |
| 214 | * seq_css() is only called from a kernfs file operation which has |
| 215 | * an active reference on the file. Because all the subsystem |
| 216 | * files are drained before a css is disassociated with a cgroup, |
| 217 | * the matching css from the cgroup's subsys table is guaranteed to |
| 218 | * be and stay valid until the enclosing operation is complete. |
| 219 | */ |
| 220 | if (cft->ss) |
| 221 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 222 | else |
| 223 | return &cgrp->dummy_css; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 224 | } |
| 225 | EXPORT_SYMBOL_GPL(seq_css); |
| 226 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 227 | /** |
| 228 | * cgroup_is_descendant - test ancestry |
| 229 | * @cgrp: the cgroup to be tested |
| 230 | * @ancestor: possible ancestor of @cgrp |
| 231 | * |
| 232 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 233 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 234 | * and @ancestor are accessible. |
| 235 | */ |
| 236 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 237 | { |
| 238 | while (cgrp) { |
| 239 | if (cgrp == ancestor) |
| 240 | return true; |
| 241 | cgrp = cgrp->parent; |
| 242 | } |
| 243 | return false; |
| 244 | } |
| 245 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 246 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 247 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 248 | { |
| 249 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 250 | (1 << CGRP_RELEASABLE) | |
| 251 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 252 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 255 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 256 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 257 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 260 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 261 | * for_each_css - iterate all css's of a cgroup |
| 262 | * @css: the iteration cursor |
| 263 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 264 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 265 | * |
| 266 | * Should be called under cgroup_mutex. |
| 267 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 268 | #define for_each_css(css, ssid, cgrp) \ |
| 269 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 270 | if (!((css) = rcu_dereference_check( \ |
| 271 | (cgrp)->subsys[(ssid)], \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 272 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 273 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 274 | else |
| 275 | |
| 276 | /** |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 277 | * for_each_subsys - iterate all enabled cgroup subsystems |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 278 | * @ss: the iteration cursor |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 279 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 280 | */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 281 | #define for_each_subsys(ss, ssid) \ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 282 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \ |
| 283 | (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 284 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 285 | /* iterate across the active hierarchies */ |
| 286 | #define for_each_active_root(root) \ |
| 287 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 288 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 289 | /** |
| 290 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 291 | * @cgrp: the cgroup to be checked for liveness |
| 292 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 293 | * On success, returns true; the mutex should be later unlocked. On |
| 294 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 295 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 296 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 297 | { |
| 298 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 299 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 300 | mutex_unlock(&cgroup_mutex); |
| 301 | return false; |
| 302 | } |
| 303 | return true; |
| 304 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 305 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 306 | /* the list of cgroups eligible for automatic release. Protected by |
| 307 | * release_list_lock */ |
| 308 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 309 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 310 | static void cgroup_release_agent(struct work_struct *work); |
| 311 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 312 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 313 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 314 | /* |
| 315 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 316 | * belong to different cgroups on different hierarchies. In the other |
| 317 | * direction, a css_set is naturally associated with multiple cgroups. |
| 318 | * This M:N relationship is represented by the following link structure |
| 319 | * which exists for each association and allows traversing the associations |
| 320 | * from both sides. |
| 321 | */ |
| 322 | struct cgrp_cset_link { |
| 323 | /* the cgroup and css_set this link associates */ |
| 324 | struct cgroup *cgrp; |
| 325 | struct css_set *cset; |
| 326 | |
| 327 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 328 | struct list_head cset_link; |
| 329 | |
| 330 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 331 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | /* The default css_set - used by init and its children prior to any |
| 335 | * hierarchies being mounted. It contains a pointer to the root state |
| 336 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 337 | * reference-counted, to improve performance when child cgroups |
| 338 | * haven't been created. |
| 339 | */ |
| 340 | |
| 341 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 342 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 343 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 344 | /* |
| 345 | * css_set_lock protects the list of css_set objects, and the chain of |
| 346 | * tasks off each css_set. Nests outside task->alloc_lock due to |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 347 | * css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 348 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 349 | static DEFINE_RWLOCK(css_set_lock); |
| 350 | static int css_set_count; |
| 351 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 352 | /* |
| 353 | * hash table for cgroup groups. This improves the performance to find |
| 354 | * an existing css_set. This hash doesn't (currently) take into |
| 355 | * account cgroups in empty hierarchies. |
| 356 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 357 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 358 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 359 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 360 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 361 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 362 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 363 | struct cgroup_subsys *ss; |
| 364 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 365 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 366 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 367 | key += (unsigned long)css[i]; |
| 368 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 369 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 370 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 373 | /* |
| 374 | * 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] | 375 | * until after the first call to css_task_iter_start(). This reduces the |
| 376 | * fork()/exit() overhead for people who have cgroups compiled into their |
| 377 | * kernel but not actually in use. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 378 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 379 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 380 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 381 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 382 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 383 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 384 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 385 | /* |
| 386 | * Ensure that the refcount doesn't hit zero while any readers |
| 387 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 388 | * rwlock |
| 389 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 390 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 391 | return; |
| 392 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 393 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 394 | write_unlock(&css_set_lock); |
| 395 | return; |
| 396 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 397 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 398 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 399 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 400 | css_set_count--; |
| 401 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 402 | 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] | 403 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 404 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 405 | list_del(&link->cset_link); |
| 406 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 407 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 408 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 409 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 410 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 411 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 412 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 413 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 414 | |
| 415 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 416 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 417 | |
| 418 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 419 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /* |
| 423 | * refcounted get/put for css_set objects |
| 424 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 425 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 426 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 427 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 430 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 431 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 432 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 435 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 436 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 437 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 440 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 441 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 442 | * @cset: candidate css_set being tested |
| 443 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 444 | * @new_cgrp: cgroup that's being entered by the task |
| 445 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 446 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 447 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 448 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 449 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 450 | static bool compare_css_sets(struct css_set *cset, |
| 451 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 452 | struct cgroup *new_cgrp, |
| 453 | struct cgroup_subsys_state *template[]) |
| 454 | { |
| 455 | struct list_head *l1, *l2; |
| 456 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 457 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 458 | /* Not all subsystems matched */ |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Compare cgroup pointers in order to distinguish between |
| 464 | * different cgroups in heirarchies with no subsystems. We |
| 465 | * could get by with just this check alone (and skip the |
| 466 | * memcmp above) but on most setups the memcmp check will |
| 467 | * avoid the need for this more expensive check on almost all |
| 468 | * candidates. |
| 469 | */ |
| 470 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 471 | l1 = &cset->cgrp_links; |
| 472 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 473 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 474 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 475 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 476 | |
| 477 | l1 = l1->next; |
| 478 | l2 = l2->next; |
| 479 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 480 | if (l1 == &cset->cgrp_links) { |
| 481 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 482 | break; |
| 483 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 484 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 485 | } |
| 486 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 487 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 488 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 489 | cgrp1 = link1->cgrp; |
| 490 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 491 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 492 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 493 | |
| 494 | /* |
| 495 | * If this hierarchy is the hierarchy of the cgroup |
| 496 | * that's changing, then we need to check that this |
| 497 | * css_set points to the new cgroup; if it's any other |
| 498 | * hierarchy, then this css_set should point to the |
| 499 | * same cgroup as the old css_set. |
| 500 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 501 | if (cgrp1->root == new_cgrp->root) { |
| 502 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 503 | return false; |
| 504 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 505 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 506 | return false; |
| 507 | } |
| 508 | } |
| 509 | return true; |
| 510 | } |
| 511 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 512 | /** |
| 513 | * find_existing_css_set - init css array and find the matching css_set |
| 514 | * @old_cset: the css_set that we're using before the cgroup transition |
| 515 | * @cgrp: the cgroup that we're moving into |
| 516 | * @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] | 517 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 518 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 519 | struct cgroup *cgrp, |
| 520 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 521 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 522 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 523 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 524 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 525 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 526 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 527 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 528 | /* |
| 529 | * Build the set of subsystem state objects that we want to see in the |
| 530 | * new css_set. while subsystems can change globally, the entries here |
| 531 | * won't change, so no need for locking. |
| 532 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 533 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 534 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 535 | /* Subsystem is in this hierarchy. So we want |
| 536 | * the subsystem state from the new |
| 537 | * cgroup */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 538 | template[i] = cgroup_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 539 | } else { |
| 540 | /* Subsystem is not in this hierarchy, so we |
| 541 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 542 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 546 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 547 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 548 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 549 | continue; |
| 550 | |
| 551 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 552 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 553 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 554 | |
| 555 | /* No existing cgroup group matched */ |
| 556 | return NULL; |
| 557 | } |
| 558 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 559 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 560 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 561 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 562 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 563 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 564 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 565 | kfree(link); |
| 566 | } |
| 567 | } |
| 568 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 569 | /** |
| 570 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 571 | * @count: the number of links to allocate |
| 572 | * @tmp_links: list_head the allocated links are put on |
| 573 | * |
| 574 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 575 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 576 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 577 | 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] | 578 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 579 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 580 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 581 | |
| 582 | INIT_LIST_HEAD(tmp_links); |
| 583 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 584 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 585 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 586 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 587 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 588 | return -ENOMEM; |
| 589 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 590 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 591 | } |
| 592 | return 0; |
| 593 | } |
| 594 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 595 | /** |
| 596 | * 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] | 597 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 598 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 599 | * @cgrp: the destination cgroup |
| 600 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 601 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 602 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 603 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 604 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 605 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 606 | BUG_ON(list_empty(tmp_links)); |
| 607 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 608 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 609 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 610 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 611 | /* |
| 612 | * Always add links to the tail of the list so that the list |
| 613 | * is sorted by order of hierarchy creation |
| 614 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 615 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 618 | /** |
| 619 | * find_css_set - return a new css_set with one cgroup updated |
| 620 | * @old_cset: the baseline css_set |
| 621 | * @cgrp: the cgroup to be updated |
| 622 | * |
| 623 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 624 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 625 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 626 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 627 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 628 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 629 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 630 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 631 | struct list_head tmp_links; |
| 632 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 633 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 634 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 635 | lockdep_assert_held(&cgroup_mutex); |
| 636 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 637 | /* First see if we already have a cgroup group that matches |
| 638 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 639 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 640 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 641 | if (cset) |
| 642 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 643 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 644 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 645 | if (cset) |
| 646 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 647 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 648 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 649 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 650 | return NULL; |
| 651 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 652 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 653 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 654 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 655 | return NULL; |
| 656 | } |
| 657 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 658 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 659 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 660 | INIT_LIST_HEAD(&cset->tasks); |
| 661 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 662 | |
| 663 | /* Copy the set of subsystem state objects generated in |
| 664 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 665 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 666 | |
| 667 | write_lock(&css_set_lock); |
| 668 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 669 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 670 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 671 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 672 | if (c->root == cgrp->root) |
| 673 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 674 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 675 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 676 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 677 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 678 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 679 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 680 | |
| 681 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 682 | key = css_set_hash(cset->subsys); |
| 683 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 684 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 685 | write_unlock(&css_set_lock); |
| 686 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 687 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 690 | static struct cgroupfs_root *cgroup_root_from_kf(struct kernfs_root *kf_root) |
| 691 | { |
| 692 | struct cgroup *top_cgrp = kf_root->kn->priv; |
| 693 | |
| 694 | return top_cgrp->root; |
| 695 | } |
| 696 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 697 | static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end) |
| 698 | { |
| 699 | int id; |
| 700 | |
| 701 | lockdep_assert_held(&cgroup_mutex); |
| 702 | |
| 703 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 704 | GFP_KERNEL); |
| 705 | if (id < 0) |
| 706 | return id; |
| 707 | |
| 708 | root->hierarchy_id = id; |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 713 | { |
| 714 | lockdep_assert_held(&cgroup_mutex); |
| 715 | |
| 716 | if (root->hierarchy_id) { |
| 717 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
| 718 | root->hierarchy_id = 0; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | static void cgroup_free_root(struct cgroupfs_root *root) |
| 723 | { |
| 724 | if (root) { |
| 725 | /* hierarhcy ID shoulid already have been released */ |
| 726 | WARN_ON_ONCE(root->hierarchy_id); |
| 727 | |
| 728 | idr_destroy(&root->cgroup_idr); |
| 729 | kfree(root); |
| 730 | } |
| 731 | } |
| 732 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 733 | static void cgroup_get_root(struct cgroupfs_root *root) |
| 734 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 735 | /* |
| 736 | * The caller must ensure that @root is alive, which can be |
| 737 | * achieved by holding a ref on one of the member cgroups or |
| 738 | * following a registered reference to @root while holding |
| 739 | * cgroup_tree_mutex. |
| 740 | */ |
| 741 | WARN_ON_ONCE(atomic_read(&root->refcnt) <= 0); |
| 742 | atomic_inc(&root->refcnt); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static void cgroup_put_root(struct cgroupfs_root *root) |
| 746 | { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 747 | struct cgroup *cgrp = &root->top_cgroup; |
| 748 | struct cgrp_cset_link *link, *tmp_link; |
| 749 | int ret; |
| 750 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 751 | /* |
| 752 | * @root's refcnt reaching zero and its deregistration should be |
| 753 | * atomic w.r.t. cgroup_tree_mutex. This ensures that |
| 754 | * cgroup_get_root() is safe to invoke if @root is registered. |
| 755 | */ |
| 756 | mutex_lock(&cgroup_tree_mutex); |
| 757 | if (!atomic_dec_and_test(&root->refcnt)) { |
| 758 | mutex_unlock(&cgroup_tree_mutex); |
| 759 | return; |
| 760 | } |
| 761 | mutex_lock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 762 | |
| 763 | BUG_ON(root->number_of_cgroups != 1); |
| 764 | BUG_ON(!list_empty(&cgrp->children)); |
| 765 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 766 | /* Rebind all subsystems back to the default hierarchy */ |
| 767 | if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { |
| 768 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
| 769 | /* Shouldn't be able to fail ... */ |
| 770 | BUG_ON(ret); |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Release all the links from cset_links to this hierarchy's |
| 775 | * root cgroup |
| 776 | */ |
| 777 | write_lock(&css_set_lock); |
| 778 | |
| 779 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 780 | list_del(&link->cset_link); |
| 781 | list_del(&link->cgrp_link); |
| 782 | kfree(link); |
| 783 | } |
| 784 | write_unlock(&css_set_lock); |
| 785 | |
| 786 | if (!list_empty(&root->root_list)) { |
| 787 | list_del(&root->root_list); |
| 788 | cgroup_root_count--; |
| 789 | } |
| 790 | |
| 791 | cgroup_exit_root_id(root); |
| 792 | |
| 793 | mutex_unlock(&cgroup_mutex); |
| 794 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 795 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 796 | kernfs_destroy_root(root->kf_root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 797 | cgroup_free_root(root); |
| 798 | } |
| 799 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 800 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 801 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 802 | * called with cgroup_mutex held. |
| 803 | */ |
| 804 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 805 | struct cgroupfs_root *root) |
| 806 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 807 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 808 | struct cgroup *res = NULL; |
| 809 | |
| 810 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 811 | read_lock(&css_set_lock); |
| 812 | /* |
| 813 | * No need to lock the task - since we hold cgroup_mutex the |
| 814 | * task can't change groups, so the only thing that can happen |
| 815 | * is that it exits and its css is set back to init_css_set. |
| 816 | */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 817 | cset = task_css_set(task); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 818 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 819 | res = &root->top_cgroup; |
| 820 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 821 | struct cgrp_cset_link *link; |
| 822 | |
| 823 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 824 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 825 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 826 | if (c->root == root) { |
| 827 | res = c; |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | read_unlock(&css_set_lock); |
| 833 | BUG_ON(!res); |
| 834 | return res; |
| 835 | } |
| 836 | |
| 837 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 838 | * There is one global cgroup mutex. We also require taking |
| 839 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 840 | * See "The task_lock() exception", at the end of this comment. |
| 841 | * |
| 842 | * A task must hold cgroup_mutex to modify cgroups. |
| 843 | * |
| 844 | * Any task can increment and decrement the count field without lock. |
| 845 | * So in general, code holding cgroup_mutex can't rely on the count |
| 846 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 847 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 848 | * means that no tasks are currently attached, therefore there is no |
| 849 | * way a task attached to that cgroup can fork (the other way to |
| 850 | * increment the count). So code holding cgroup_mutex can safely |
| 851 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 852 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 853 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 854 | * needs that mutex. |
| 855 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 856 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 857 | * (usually) take cgroup_mutex. These are the two most performance |
| 858 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 859 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 860 | * 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] | 861 | * to the release agent with the name of the cgroup (path relative to |
| 862 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 863 | * |
| 864 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 865 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 866 | * tasks in the system use _some_ cgroup, and since there is always at |
| 867 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 868 | * always has either children cgroups and/or using tasks. So we don't |
| 869 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 870 | * |
| 871 | * The task_lock() exception |
| 872 | * |
| 873 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 874 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 875 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 876 | * several performance critical places that need to reference |
| 877 | * task->cgroup without the expense of grabbing a system global |
| 878 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 879 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 880 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 881 | * the task_struct routinely used for such matters. |
| 882 | * |
| 883 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 884 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 885 | */ |
| 886 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 887 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 888 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 889 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 890 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 891 | static struct cgroup_name *cgroup_alloc_name(const char *name_str) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 892 | { |
| 893 | struct cgroup_name *name; |
| 894 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 895 | name = kmalloc(sizeof(*name) + strlen(name_str) + 1, GFP_KERNEL); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 896 | if (!name) |
| 897 | return NULL; |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 898 | strcpy(name->name, name_str); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 899 | return name; |
| 900 | } |
| 901 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 902 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 903 | char *buf) |
| 904 | { |
| 905 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 906 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) |
| 907 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s", |
| 908 | cft->ss->name, cft->name); |
| 909 | else |
| 910 | strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
| 911 | return buf; |
| 912 | } |
| 913 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 914 | /** |
| 915 | * cgroup_file_mode - deduce file mode of a control file |
| 916 | * @cft: the control file in question |
| 917 | * |
| 918 | * returns cft->mode if ->mode is not 0 |
| 919 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 920 | * returns S_IRUGO if it has only a read handler |
| 921 | * returns S_IWUSR if it has only a write hander |
| 922 | */ |
| 923 | static umode_t cgroup_file_mode(const struct cftype *cft) |
| 924 | { |
| 925 | umode_t mode = 0; |
| 926 | |
| 927 | if (cft->mode) |
| 928 | return cft->mode; |
| 929 | |
| 930 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 931 | mode |= S_IRUGO; |
| 932 | |
| 933 | if (cft->write_u64 || cft->write_s64 || cft->write_string || |
| 934 | cft->trigger) |
| 935 | mode |= S_IWUSR; |
| 936 | |
| 937 | return mode; |
| 938 | } |
| 939 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 940 | static void cgroup_free_fn(struct work_struct *work) |
| 941 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 942 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 943 | |
| 944 | mutex_lock(&cgroup_mutex); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 945 | cgrp->root->number_of_cgroups--; |
| 946 | mutex_unlock(&cgroup_mutex); |
| 947 | |
| 948 | /* |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 949 | * We get a ref to the parent, and put the ref when this cgroup is |
| 950 | * being freed, so it's guaranteed that the parent won't be |
| 951 | * destroyed before its children. |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 952 | */ |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 953 | cgroup_put(cgrp->parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 954 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 955 | /* put the root reference that we took when we created the cgroup */ |
| 956 | cgroup_put_root(cgrp->root); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 957 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 958 | cgroup_pidlist_destroy_all(cgrp); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 959 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 960 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 961 | kfree(cgrp); |
| 962 | } |
| 963 | |
| 964 | static void cgroup_free_rcu(struct rcu_head *head) |
| 965 | { |
| 966 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 967 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 968 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 969 | queue_work(cgroup_destroy_wq, &cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 970 | } |
| 971 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 972 | static void cgroup_get(struct cgroup *cgrp) |
| 973 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 974 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
| 975 | WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0); |
| 976 | atomic_inc(&cgrp->refcnt); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 979 | static void cgroup_put(struct cgroup *cgrp) |
| 980 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 981 | if (!atomic_dec_and_test(&cgrp->refcnt)) |
| 982 | return; |
| 983 | if (WARN_ON_ONCE(!cgroup_is_dead(cgrp))) |
| 984 | return; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 985 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 986 | /* |
| 987 | * XXX: cgrp->id is only used to look up css's. As cgroup and |
| 988 | * css's lifetimes will be decoupled, it should be made |
| 989 | * per-subsystem and moved to css->id so that lookups are |
| 990 | * successful until the target css is released. |
| 991 | */ |
| 992 | mutex_lock(&cgroup_mutex); |
| 993 | idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 994 | mutex_unlock(&cgroup_mutex); |
| 995 | cgrp->id = -1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 996 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 997 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 1000 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1001 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1002 | char name[CGROUP_FILE_NAME_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1003 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1004 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1005 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1008 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 1009 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 1010 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1011 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 1012 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 1013 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1014 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1015 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1016 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1017 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1018 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 1019 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1020 | |
| 1021 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1022 | continue; |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 1023 | list_for_each_entry(cfts, &ss->cfts, node) |
| 1024 | cgroup_addrm_files(cgrp, cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1025 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1028 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1029 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1030 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1031 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1032 | struct cgroup_subsys *ss; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1033 | int i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1034 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1035 | lockdep_assert_held(&cgroup_tree_mutex); |
| 1036 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1037 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1038 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1039 | for_each_subsys(ss, i) |
| 1040 | if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root) |
| 1041 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1042 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1043 | ret = cgroup_populate_dir(cgrp, added_mask); |
| 1044 | if (ret) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1045 | return ret; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1046 | |
| 1047 | /* |
| 1048 | * Nothing can fail from this point on. Remove files for the |
| 1049 | * removed subsystems and rebind each subsystem. |
| 1050 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1051 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1052 | cgroup_clear_dir(cgrp, removed_mask); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1053 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1054 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1055 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1056 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1057 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1058 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1059 | /* We're binding this subsystem to this hierarchy */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1060 | BUG_ON(cgroup_css(cgrp, ss)); |
| 1061 | BUG_ON(!cgroup_css(cgroup_dummy_top, ss)); |
| 1062 | BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1063 | |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1064 | rcu_assign_pointer(cgrp->subsys[i], |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1065 | cgroup_css(cgroup_dummy_top, ss)); |
| 1066 | cgroup_css(cgrp, ss)->cgroup = cgrp; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1067 | |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1068 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1069 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1070 | ss->bind(cgroup_css(cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1071 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1072 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1073 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1074 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1075 | /* We're removing this subsystem */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1076 | BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss)); |
| 1077 | BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1078 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1079 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1080 | ss->bind(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1081 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1082 | cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1083 | RCU_INIT_POINTER(cgrp->subsys[i], NULL); |
| 1084 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1085 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1086 | root->subsys_mask &= ~bit; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1089 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1090 | /* |
| 1091 | * Mark @root has finished binding subsystems. @root->subsys_mask |
| 1092 | * now matches the bound subsystems. |
| 1093 | */ |
| 1094 | root->flags |= CGRP_ROOT_SUBSYS_BOUND; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1095 | kernfs_activate(cgrp->kn); |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1096 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1100 | static int cgroup_show_options(struct seq_file *seq, |
| 1101 | struct kernfs_root *kf_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1102 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1103 | struct cgroupfs_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1104 | struct cgroup_subsys *ss; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1105 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1106 | |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1107 | for_each_subsys(ss, ssid) |
| 1108 | if (root->subsys_mask & (1 << ssid)) |
| 1109 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1110 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1111 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1112 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1113 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1114 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1115 | seq_puts(seq, ",xattr"); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1116 | |
| 1117 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1118 | if (strlen(root->release_agent_path)) |
| 1119 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1120 | spin_unlock(&release_agent_path_lock); |
| 1121 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1122 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1123 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1124 | if (strlen(root->name)) |
| 1125 | seq_printf(seq, ",name=%s", root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1130 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1131 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1132 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1133 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1134 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1135 | /* User explicitly requested empty subsystem */ |
| 1136 | bool none; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1137 | }; |
| 1138 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1139 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1140 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1141 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1142 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1143 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1144 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1145 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1146 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1147 | char *token, *o = data; |
| 1148 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1149 | unsigned long mask = (unsigned long)-1; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1150 | struct cgroup_subsys *ss; |
| 1151 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1152 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1153 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1154 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1155 | #ifdef CONFIG_CPUSETS |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1156 | mask = ~(1UL << cpuset_cgrp_id); |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1157 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1158 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1159 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1160 | |
| 1161 | while ((token = strsep(&o, ",")) != NULL) { |
| 1162 | if (!*token) |
| 1163 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1164 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1165 | /* Explicitly have no subsystems */ |
| 1166 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1167 | continue; |
| 1168 | } |
| 1169 | if (!strcmp(token, "all")) { |
| 1170 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1171 | if (one_ss) |
| 1172 | return -EINVAL; |
| 1173 | all_ss = true; |
| 1174 | continue; |
| 1175 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1176 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1177 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1178 | continue; |
| 1179 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1180 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1181 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1182 | continue; |
| 1183 | } |
| 1184 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1185 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1186 | continue; |
| 1187 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1188 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1189 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1190 | continue; |
| 1191 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1192 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1193 | /* Specifying two release agents is forbidden */ |
| 1194 | if (opts->release_agent) |
| 1195 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1196 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1197 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1198 | if (!opts->release_agent) |
| 1199 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1200 | continue; |
| 1201 | } |
| 1202 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1203 | const char *name = token + 5; |
| 1204 | /* Can't specify an empty name */ |
| 1205 | if (!strlen(name)) |
| 1206 | return -EINVAL; |
| 1207 | /* Must match [\w.-]+ */ |
| 1208 | for (i = 0; i < strlen(name); i++) { |
| 1209 | char c = name[i]; |
| 1210 | if (isalnum(c)) |
| 1211 | continue; |
| 1212 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1213 | continue; |
| 1214 | return -EINVAL; |
| 1215 | } |
| 1216 | /* Specifying two names is forbidden */ |
| 1217 | if (opts->name) |
| 1218 | return -EINVAL; |
| 1219 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1220 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1221 | GFP_KERNEL); |
| 1222 | if (!opts->name) |
| 1223 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1224 | |
| 1225 | continue; |
| 1226 | } |
| 1227 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1228 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1229 | if (strcmp(token, ss->name)) |
| 1230 | continue; |
| 1231 | if (ss->disabled) |
| 1232 | continue; |
| 1233 | |
| 1234 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1235 | if (all_ss) |
| 1236 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1237 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1238 | one_ss = true; |
| 1239 | |
| 1240 | break; |
| 1241 | } |
| 1242 | if (i == CGROUP_SUBSYS_COUNT) |
| 1243 | return -ENOENT; |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1248 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1249 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1250 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1251 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1252 | for_each_subsys(ss, i) |
| 1253 | if (!ss->disabled) |
| 1254 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1255 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1256 | /* Consistency checks */ |
| 1257 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1258 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1259 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1260 | |
| 1261 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1262 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1263 | return -EINVAL; |
| 1264 | } |
| 1265 | |
| 1266 | if (opts->cpuset_clone_children) { |
| 1267 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1268 | return -EINVAL; |
| 1269 | } |
Tejun Heo | 86bf4b6 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 1270 | |
| 1271 | if (opts->flags & CGRP_ROOT_XATTR) |
| 1272 | pr_warning("cgroup: sane_behavior: xattr is always available, flag unnecessary\n"); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1275 | /* |
| 1276 | * Option noprefix was introduced just for backward compatibility |
| 1277 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1278 | * the cpuset subsystem. |
| 1279 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1280 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1281 | return -EINVAL; |
| 1282 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1283 | |
| 1284 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1285 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1286 | return -EINVAL; |
| 1287 | |
| 1288 | /* |
| 1289 | * We either have to specify by name or by subsystems. (So all |
| 1290 | * empty hierarchies must have a name). |
| 1291 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1292 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1293 | return -EINVAL; |
| 1294 | |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1298 | static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1299 | { |
| 1300 | int ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1301 | struct cgroupfs_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1302 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1303 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1304 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1305 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1306 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1307 | return -EINVAL; |
| 1308 | } |
| 1309 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1310 | mutex_lock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1311 | mutex_lock(&cgroup_mutex); |
| 1312 | |
| 1313 | /* See what subsystems are wanted */ |
| 1314 | ret = parse_cgroupfs_options(data, &opts); |
| 1315 | if (ret) |
| 1316 | goto out_unlock; |
| 1317 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1318 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1319 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1320 | task_tgid_nr(current), current->comm); |
| 1321 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1322 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1323 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1324 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1325 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1326 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1327 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1328 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1329 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1330 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1331 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1332 | goto out_unlock; |
| 1333 | } |
| 1334 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1335 | /* remounting is not allowed for populated hierarchies */ |
| 1336 | if (root->number_of_cgroups > 1) { |
| 1337 | ret = -EBUSY; |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1338 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1339 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1340 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1341 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1342 | if (ret) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1343 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1344 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1345 | if (opts.release_agent) { |
| 1346 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1347 | strcpy(root->release_agent_path, opts.release_agent); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1348 | spin_unlock(&release_agent_path_lock); |
| 1349 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1350 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1351 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1352 | kfree(opts.name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1353 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1354 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1355 | return ret; |
| 1356 | } |
| 1357 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1358 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1359 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1360 | atomic_set(&cgrp->refcnt, 1); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1361 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1362 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1363 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1364 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1365 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1366 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1367 | cgrp->dummy_css.cgroup = cgrp; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1368 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1369 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1370 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1371 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1372 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1373 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1374 | atomic_set(&root->refcnt, 1); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1375 | INIT_LIST_HEAD(&root->root_list); |
| 1376 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1377 | cgrp->root = root; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 1378 | RCU_INIT_POINTER(cgrp->name, &root_cgroup_name); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1379 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1380 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1383 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1384 | { |
| 1385 | struct cgroupfs_root *root; |
| 1386 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1387 | if (!opts->subsys_mask && !opts->none) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1388 | return ERR_PTR(-EINVAL); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1389 | |
| 1390 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1391 | if (!root) |
| 1392 | return ERR_PTR(-ENOMEM); |
| 1393 | |
| 1394 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1395 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1396 | /* |
| 1397 | * We need to set @root->subsys_mask now so that @root can be |
| 1398 | * matched by cgroup_test_super() before it finishes |
| 1399 | * initialization; otherwise, competing mounts with the same |
| 1400 | * options may try to bind the same subsystems instead of waiting |
| 1401 | * for the first one leading to unexpected mount errors. |
| 1402 | * SUBSYS_BOUND will be set once actual binding is complete. |
| 1403 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1404 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1405 | root->flags = opts->flags; |
| 1406 | if (opts->release_agent) |
| 1407 | strcpy(root->release_agent_path, opts->release_agent); |
| 1408 | if (opts->name) |
| 1409 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1410 | if (opts->cpuset_clone_children) |
| 1411 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1412 | return root; |
| 1413 | } |
| 1414 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1415 | static int cgroup_setup_root(struct cgroupfs_root *root) |
| 1416 | { |
| 1417 | LIST_HEAD(tmp_links); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1418 | struct cgroup *root_cgrp = &root->top_cgroup; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1419 | struct css_set *cset; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1420 | int i, ret; |
| 1421 | |
| 1422 | lockdep_assert_held(&cgroup_tree_mutex); |
| 1423 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1424 | |
| 1425 | ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL); |
| 1426 | if (ret < 0) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1427 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1428 | root_cgrp->id = ret; |
| 1429 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1430 | /* |
| 1431 | * We're accessing css_set_count without locking css_set_lock here, |
| 1432 | * but that's OK - it can only be increased by someone holding |
| 1433 | * cgroup_lock, and that's us. The worst that can happen is that we |
| 1434 | * have some link structures left over |
| 1435 | */ |
| 1436 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
| 1437 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1438 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1439 | |
| 1440 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1441 | ret = cgroup_init_root_id(root, 2, 0); |
| 1442 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1443 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1444 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1445 | root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops, |
| 1446 | KERNFS_ROOT_CREATE_DEACTIVATED, |
| 1447 | root_cgrp); |
| 1448 | if (IS_ERR(root->kf_root)) { |
| 1449 | ret = PTR_ERR(root->kf_root); |
| 1450 | goto exit_root_id; |
| 1451 | } |
| 1452 | root_cgrp->kn = root->kf_root->kn; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1453 | |
| 1454 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
| 1455 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1456 | goto destroy_root; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1457 | |
| 1458 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
| 1459 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1460 | goto destroy_root; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1461 | |
| 1462 | /* |
| 1463 | * There must be no failure case after here, since rebinding takes |
| 1464 | * care of subsystems' refcounts, which are explicitly dropped in |
| 1465 | * the failure exit path. |
| 1466 | */ |
| 1467 | list_add(&root->root_list, &cgroup_roots); |
| 1468 | cgroup_root_count++; |
| 1469 | |
| 1470 | /* |
| 1471 | * Link the top cgroup in this hierarchy into all the css_set |
| 1472 | * objects. |
| 1473 | */ |
| 1474 | write_lock(&css_set_lock); |
| 1475 | hash_for_each(css_set_table, i, cset, hlist) |
| 1476 | link_css_set(&tmp_links, cset, root_cgrp); |
| 1477 | write_unlock(&css_set_lock); |
| 1478 | |
| 1479 | BUG_ON(!list_empty(&root_cgrp->children)); |
| 1480 | BUG_ON(root->number_of_cgroups != 1); |
| 1481 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1482 | kernfs_activate(root_cgrp->kn); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1483 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1484 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1485 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1486 | destroy_root: |
| 1487 | kernfs_destroy_root(root->kf_root); |
| 1488 | root->kf_root = NULL; |
| 1489 | exit_root_id: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1490 | cgroup_exit_root_id(root); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1491 | out: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1492 | free_cgrp_cset_links(&tmp_links); |
| 1493 | return ret; |
| 1494 | } |
| 1495 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1496 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1497 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1498 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1499 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1500 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1501 | struct cgroup_sb_opts opts; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1502 | struct dentry *dentry; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1503 | int ret; |
| 1504 | |
| 1505 | mutex_lock(&cgroup_tree_mutex); |
| 1506 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1507 | |
| 1508 | /* First find the desired set of subsystems */ |
| 1509 | ret = parse_cgroupfs_options(data, &opts); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1510 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1511 | goto out_unlock; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1512 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1513 | /* look for a matching existing root */ |
| 1514 | for_each_active_root(root) { |
| 1515 | bool name_match = false; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1516 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1517 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1518 | * If we asked for a name then it must match. Also, if |
| 1519 | * name matches but sybsys_mask doesn't, we should fail. |
| 1520 | * Remember whether name matched. |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1521 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1522 | if (opts.name) { |
| 1523 | if (strcmp(opts.name, root->name)) |
| 1524 | continue; |
| 1525 | name_match = true; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * If we asked for subsystems (or explicitly for no |
| 1530 | * subsystems) then they must match. |
| 1531 | */ |
| 1532 | if ((opts.subsys_mask || opts.none) && |
| 1533 | (opts.subsys_mask != root->subsys_mask)) { |
| 1534 | if (!name_match) |
| 1535 | continue; |
| 1536 | ret = -EBUSY; |
| 1537 | goto out_unlock; |
| 1538 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1539 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1540 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1541 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1542 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1543 | ret = -EINVAL; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1544 | goto out_unlock; |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1545 | } else { |
| 1546 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1547 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1548 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1549 | |
| 1550 | cgroup_get_root(root); |
| 1551 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1554 | /* no such thing, create a new one */ |
| 1555 | root = cgroup_root_from_opts(&opts); |
| 1556 | if (IS_ERR(root)) { |
| 1557 | ret = PTR_ERR(root); |
| 1558 | goto out_unlock; |
| 1559 | } |
| 1560 | |
| 1561 | ret = cgroup_setup_root(root); |
| 1562 | if (ret) |
| 1563 | cgroup_free_root(root); |
| 1564 | |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1565 | out_unlock: |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1566 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1567 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1568 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1569 | kfree(opts.release_agent); |
| 1570 | kfree(opts.name); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1571 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1572 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1573 | return ERR_PTR(ret); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1574 | |
| 1575 | dentry = kernfs_mount(fs_type, flags, root->kf_root); |
| 1576 | if (IS_ERR(dentry)) |
| 1577 | cgroup_put_root(root); |
| 1578 | return dentry; |
| 1579 | } |
| 1580 | |
| 1581 | static void cgroup_kill_sb(struct super_block *sb) |
| 1582 | { |
| 1583 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
| 1584 | struct cgroupfs_root *root = cgroup_root_from_kf(kf_root); |
| 1585 | |
| 1586 | cgroup_put_root(root); |
| 1587 | kernfs_kill_sb(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1590 | static struct file_system_type cgroup_fs_type = { |
| 1591 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1592 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1593 | .kill_sb = cgroup_kill_sb, |
| 1594 | }; |
| 1595 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1596 | static struct kobject *cgroup_kobj; |
| 1597 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1598 | /** |
| 1599 | * cgroup_path - generate the path of a cgroup |
| 1600 | * @cgrp: the cgroup in question |
| 1601 | * @buf: the buffer to write the path into |
| 1602 | * @buflen: the length of the buffer |
| 1603 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1604 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1605 | * |
| 1606 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1607 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1608 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1609 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1610 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1611 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1612 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1613 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1614 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1615 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1616 | if (!cgrp->parent) { |
| 1617 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1618 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1619 | return 0; |
| 1620 | } |
| 1621 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1622 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1623 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1624 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1625 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1626 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1627 | const char *name = cgroup_name(cgrp); |
| 1628 | int len; |
| 1629 | |
| 1630 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1631 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1632 | goto out; |
| 1633 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1634 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1635 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1636 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1637 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1638 | |
| 1639 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1640 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1641 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1642 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1643 | out: |
| 1644 | rcu_read_unlock(); |
| 1645 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1646 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1647 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1648 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1649 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1650 | * 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] | 1651 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1652 | * @buf: the buffer to write the path into |
| 1653 | * @buflen: the length of the buffer |
| 1654 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1655 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1656 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1657 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1658 | * cgroup controller callbacks. |
| 1659 | * |
| 1660 | * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short. |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1661 | */ |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1662 | 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] | 1663 | { |
| 1664 | struct cgroupfs_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1665 | struct cgroup *cgrp; |
| 1666 | int hierarchy_id = 1, ret = 0; |
| 1667 | |
| 1668 | if (buflen < 2) |
| 1669 | return -ENAMETOOLONG; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1670 | |
| 1671 | mutex_lock(&cgroup_mutex); |
| 1672 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1673 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1674 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1675 | if (root) { |
| 1676 | cgrp = task_cgroup_from_root(task, root); |
| 1677 | ret = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1678 | } else { |
| 1679 | /* if no hierarchy exists, everyone is in "/" */ |
| 1680 | memcpy(buf, "/", 2); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1684 | return ret; |
| 1685 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1686 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1687 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1688 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1689 | * Control Group taskset |
| 1690 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1691 | struct task_and_cgroup { |
| 1692 | struct task_struct *task; |
| 1693 | struct cgroup *cgrp; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1694 | struct css_set *cset; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1695 | }; |
| 1696 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1697 | struct cgroup_taskset { |
| 1698 | struct task_and_cgroup single; |
| 1699 | struct flex_array *tc_array; |
| 1700 | int tc_array_len; |
| 1701 | int idx; |
| 1702 | struct cgroup *cur_cgrp; |
| 1703 | }; |
| 1704 | |
| 1705 | /** |
| 1706 | * cgroup_taskset_first - reset taskset and return the first task |
| 1707 | * @tset: taskset of interest |
| 1708 | * |
| 1709 | * @tset iteration is initialized and the first task is returned. |
| 1710 | */ |
| 1711 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1712 | { |
| 1713 | if (tset->tc_array) { |
| 1714 | tset->idx = 0; |
| 1715 | return cgroup_taskset_next(tset); |
| 1716 | } else { |
| 1717 | tset->cur_cgrp = tset->single.cgrp; |
| 1718 | return tset->single.task; |
| 1719 | } |
| 1720 | } |
| 1721 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1722 | |
| 1723 | /** |
| 1724 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1725 | * @tset: taskset of interest |
| 1726 | * |
| 1727 | * Return the next task in @tset. Iteration must have been initialized |
| 1728 | * with cgroup_taskset_first(). |
| 1729 | */ |
| 1730 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1731 | { |
| 1732 | struct task_and_cgroup *tc; |
| 1733 | |
| 1734 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1735 | return NULL; |
| 1736 | |
| 1737 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1738 | tset->cur_cgrp = tc->cgrp; |
| 1739 | return tc->task; |
| 1740 | } |
| 1741 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1742 | |
| 1743 | /** |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1744 | * cgroup_taskset_cur_css - return the matching css for the current task |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1745 | * @tset: taskset of interest |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1746 | * @subsys_id: the ID of the target subsystem |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1747 | * |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1748 | * Return the css for the current (last returned) task of @tset for |
| 1749 | * subsystem specified by @subsys_id. This function must be preceded by |
| 1750 | * either cgroup_taskset_first() or cgroup_taskset_next(). |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1751 | */ |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1752 | struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset, |
| 1753 | int subsys_id) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1754 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1755 | return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1756 | } |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1757 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1758 | |
| 1759 | /** |
| 1760 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1761 | * @tset: taskset of interest |
| 1762 | */ |
| 1763 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1764 | { |
| 1765 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1766 | } |
| 1767 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1768 | |
| 1769 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1770 | /* |
| 1771 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1772 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1773 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1774 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1775 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1776 | struct task_struct *tsk, |
| 1777 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1778 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1779 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1780 | |
| 1781 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1782 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1783 | * setting such that we can't race against cgroup_exit() changing the |
| 1784 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1785 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1786 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1787 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1788 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1789 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1790 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1791 | task_unlock(tsk); |
| 1792 | |
| 1793 | /* Update the css_set linked lists if we're using them */ |
| 1794 | write_lock(&css_set_lock); |
| 1795 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1796 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1797 | write_unlock(&css_set_lock); |
| 1798 | |
| 1799 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1800 | * We just gained a reference on old_cset by taking it from the |
| 1801 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1802 | * 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] | 1803 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1804 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1805 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1808 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1809 | * 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] | 1810 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1811 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1812 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1813 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1814 | * 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] | 1815 | * 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] | 1816 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1817 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1818 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1819 | { |
| 1820 | int retval, i, group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1821 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1822 | struct cgroup_subsys_state *css, *failed_css = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1823 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1824 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1825 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1826 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1827 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1828 | |
| 1829 | /* |
| 1830 | * step 0: in order to do expensive, possibly blocking operations for |
| 1831 | * every thread, we cannot iterate the thread group list, since it needs |
| 1832 | * 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] | 1833 | * group - group_rwsem prevents new threads from appearing, and if |
| 1834 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1835 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1836 | if (threadgroup) |
| 1837 | group_size = get_nr_threads(tsk); |
| 1838 | else |
| 1839 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1840 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1841 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1842 | if (!group) |
| 1843 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1844 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 1845 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1846 | if (retval) |
| 1847 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1848 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1849 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1850 | /* |
| 1851 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 1852 | * already PF_EXITING could be freed from underneath us unless we |
| 1853 | * take an rcu_read_lock. |
| 1854 | */ |
| 1855 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1856 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1857 | struct task_and_cgroup ent; |
| 1858 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1859 | /* @tsk either already exited or can't exit until the end */ |
| 1860 | if (tsk->flags & PF_EXITING) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1861 | goto next; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1862 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1863 | /* as per above, nr_threads may decrease, but not increase. */ |
| 1864 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1865 | ent.task = tsk; |
| 1866 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 1867 | /* nothing to do if this task is already in the cgroup */ |
| 1868 | if (ent.cgrp == cgrp) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1869 | goto next; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1870 | /* |
| 1871 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 1872 | * earlier, but it's good form to communicate our expectations. |
| 1873 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1874 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1875 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1876 | i++; |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1877 | next: |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1878 | if (!threadgroup) |
| 1879 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1880 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1881 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1882 | /* remember the number of threads in the array for later. */ |
| 1883 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1884 | tset.tc_array = group; |
| 1885 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1886 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1887 | /* methods shouldn't be called if no task is actually migrating */ |
| 1888 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 1889 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 1890 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1891 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1892 | /* |
| 1893 | * step 1: check that we can legitimately attach to the cgroup. |
| 1894 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1895 | for_each_css(css, i, cgrp) { |
| 1896 | if (css->ss->can_attach) { |
| 1897 | retval = css->ss->can_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1898 | if (retval) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1899 | failed_css = css; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1900 | goto out_cancel_attach; |
| 1901 | } |
| 1902 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | /* |
| 1906 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 1907 | * we use find_css_set, which allocates a new one if necessary. |
| 1908 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1909 | for (i = 0; i < group_size; i++) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1910 | struct css_set *old_cset; |
| 1911 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1912 | tc = flex_array_get(group, i); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1913 | old_cset = task_css_set(tc->task); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1914 | tc->cset = find_css_set(old_cset, cgrp); |
| 1915 | if (!tc->cset) { |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1916 | retval = -ENOMEM; |
| 1917 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 1922 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 1923 | * proceed to move all tasks to the new cgroup. There are no |
| 1924 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1925 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1926 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1927 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1928 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1929 | } |
| 1930 | /* nothing is sensitive to fork() after this point. */ |
| 1931 | |
| 1932 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 1933 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1934 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1935 | for_each_css(css, i, cgrp) |
| 1936 | if (css->ss->attach) |
| 1937 | css->ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1938 | |
| 1939 | /* |
| 1940 | * step 5: success! and cleanup |
| 1941 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1942 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1943 | out_put_css_set_refs: |
| 1944 | if (retval) { |
| 1945 | for (i = 0; i < group_size; i++) { |
| 1946 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1947 | if (!tc->cset) |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1948 | break; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1949 | put_css_set(tc->cset); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1950 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1951 | } |
| 1952 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1953 | if (retval) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1954 | for_each_css(css, i, cgrp) { |
| 1955 | if (css == failed_css) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1956 | break; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1957 | if (css->ss->cancel_attach) |
| 1958 | css->ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1959 | } |
| 1960 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1961 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1962 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1963 | return retval; |
| 1964 | } |
| 1965 | |
| 1966 | /* |
| 1967 | * 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] | 1968 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 1969 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1970 | */ |
| 1971 | 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] | 1972 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1973 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1974 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1975 | int ret; |
| 1976 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1977 | if (!cgroup_lock_live_group(cgrp)) |
| 1978 | return -ENODEV; |
| 1979 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 1980 | retry_find_task: |
| 1981 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1982 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 1983 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1984 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1985 | rcu_read_unlock(); |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 1986 | ret = -ESRCH; |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 1987 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1988 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1989 | /* |
| 1990 | * even if we're attaching all tasks in the thread group, we |
| 1991 | * only need to check permissions on one of them. |
| 1992 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1993 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 1994 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 1995 | !uid_eq(cred->euid, tcred->uid) && |
| 1996 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1997 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 1998 | ret = -EACCES; |
| 1999 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2000 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2001 | } else |
| 2002 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2003 | |
| 2004 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2005 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2006 | |
| 2007 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2008 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2009 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2010 | * with no rt_runtime allocated. Just say no. |
| 2011 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2012 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2013 | ret = -EINVAL; |
| 2014 | rcu_read_unlock(); |
| 2015 | goto out_unlock_cgroup; |
| 2016 | } |
| 2017 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2018 | get_task_struct(tsk); |
| 2019 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2020 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2021 | threadgroup_lock(tsk); |
| 2022 | if (threadgroup) { |
| 2023 | if (!thread_group_leader(tsk)) { |
| 2024 | /* |
| 2025 | * a race with de_thread from another thread's exec() |
| 2026 | * may strip us of our leadership, if this happens, |
| 2027 | * there is no choice but to throw this task away and |
| 2028 | * try again; this is |
| 2029 | * "double-double-toil-and-trouble-check locking". |
| 2030 | */ |
| 2031 | threadgroup_unlock(tsk); |
| 2032 | put_task_struct(tsk); |
| 2033 | goto retry_find_task; |
| 2034 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2038 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2039 | threadgroup_unlock(tsk); |
| 2040 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2041 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2042 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2043 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2044 | return ret; |
| 2045 | } |
| 2046 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2047 | /** |
| 2048 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2049 | * @from: attach to all cgroups of a given task |
| 2050 | * @tsk: the task to be attached |
| 2051 | */ |
| 2052 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2053 | { |
| 2054 | struct cgroupfs_root *root; |
| 2055 | int retval = 0; |
| 2056 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2057 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2058 | for_each_active_root(root) { |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2059 | struct cgroup *from_cgrp = task_cgroup_from_root(from, root); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2060 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2061 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2062 | if (retval) |
| 2063 | break; |
| 2064 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2065 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2066 | |
| 2067 | return retval; |
| 2068 | } |
| 2069 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2070 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2071 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2072 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2073 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2074 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2077 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2078 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2079 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2080 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2081 | } |
| 2082 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2083 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
| 2084 | struct cftype *cft, const char *buffer) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2085 | { |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2086 | struct cgroupfs_root *root = css->cgroup->root; |
| 2087 | |
| 2088 | BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2089 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2090 | return -ENODEV; |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2091 | spin_lock(&release_agent_path_lock); |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2092 | strlcpy(root->release_agent_path, buffer, |
| 2093 | sizeof(root->release_agent_path)); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2094 | spin_unlock(&release_agent_path_lock); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2095 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2096 | return 0; |
| 2097 | } |
| 2098 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2099 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2100 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2101 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2102 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2103 | if (!cgroup_lock_live_group(cgrp)) |
| 2104 | return -ENODEV; |
| 2105 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2106 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2107 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2108 | return 0; |
| 2109 | } |
| 2110 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2111 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2112 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2113 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2114 | |
| 2115 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2116 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2119 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 2120 | size_t nbytes, loff_t off) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2121 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2122 | struct cgroup *cgrp = of->kn->parent->priv; |
| 2123 | struct cftype *cft = of->kn->priv; |
| 2124 | struct cgroup_subsys_state *css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2125 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2126 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2127 | /* |
| 2128 | * kernfs guarantees that a file isn't deleted with operations in |
| 2129 | * flight, which means that the matching css is and stays alive and |
| 2130 | * doesn't need to be pinned. The RCU locking is not necessary |
| 2131 | * either. It's just for the convenience of using cgroup_css(). |
| 2132 | */ |
| 2133 | rcu_read_lock(); |
| 2134 | css = cgroup_css(cgrp, cft->ss); |
| 2135 | rcu_read_unlock(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2136 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2137 | if (cft->write_string) { |
| 2138 | ret = cft->write_string(css, cft, strstrip(buf)); |
| 2139 | } else if (cft->write_u64) { |
| 2140 | unsigned long long v; |
| 2141 | ret = kstrtoull(buf, 0, &v); |
| 2142 | if (!ret) |
| 2143 | ret = cft->write_u64(css, cft, v); |
| 2144 | } else if (cft->write_s64) { |
| 2145 | long long v; |
| 2146 | ret = kstrtoll(buf, 0, &v); |
| 2147 | if (!ret) |
| 2148 | ret = cft->write_s64(css, cft, v); |
| 2149 | } else if (cft->trigger) { |
| 2150 | ret = cft->trigger(css, (unsigned int)cft->private); |
| 2151 | } else { |
| 2152 | ret = -EINVAL; |
| 2153 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2154 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2155 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2158 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2159 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2160 | return seq_cft(seq)->seq_start(seq, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 2164 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2165 | return seq_cft(seq)->seq_next(seq, v, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 2169 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2170 | seq_cft(seq)->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2174 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2175 | struct cftype *cft = seq_cft(m); |
| 2176 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2177 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2178 | if (cft->seq_show) |
| 2179 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2180 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2181 | if (cft->read_u64) |
| 2182 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 2183 | else if (cft->read_s64) |
| 2184 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 2185 | else |
| 2186 | return -EINVAL; |
| 2187 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2190 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 2191 | .atomic_write_len = PAGE_SIZE, |
| 2192 | .write = cgroup_file_write, |
| 2193 | .seq_show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2194 | }; |
| 2195 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2196 | static struct kernfs_ops cgroup_kf_ops = { |
| 2197 | .atomic_write_len = PAGE_SIZE, |
| 2198 | .write = cgroup_file_write, |
| 2199 | .seq_start = cgroup_seqfile_start, |
| 2200 | .seq_next = cgroup_seqfile_next, |
| 2201 | .seq_stop = cgroup_seqfile_stop, |
| 2202 | .seq_show = cgroup_seqfile_show, |
| 2203 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2204 | |
| 2205 | /* |
| 2206 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2207 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2208 | static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent, |
| 2209 | const char *new_name_str) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2210 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2211 | struct cgroup *cgrp = kn->priv; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2212 | struct cgroup_name *name, *old_name; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2213 | int ret; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2214 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2215 | if (kernfs_type(kn) != KERNFS_DIR) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2216 | return -ENOTDIR; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2217 | if (kn->parent != new_parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2218 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2219 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2220 | /* |
| 2221 | * This isn't a proper migration and its usefulness is very |
| 2222 | * limited. Disallow if sane_behavior. |
| 2223 | */ |
| 2224 | if (cgroup_sane_behavior(cgrp)) |
| 2225 | return -EPERM; |
| 2226 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2227 | name = cgroup_alloc_name(new_name_str); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2228 | if (!name) |
| 2229 | return -ENOMEM; |
| 2230 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2231 | mutex_lock(&cgroup_tree_mutex); |
| 2232 | mutex_lock(&cgroup_mutex); |
| 2233 | |
| 2234 | ret = kernfs_rename(kn, new_parent, new_name_str); |
| 2235 | if (!ret) { |
| 2236 | old_name = rcu_dereference_protected(cgrp->name, true); |
| 2237 | rcu_assign_pointer(cgrp->name, name); |
| 2238 | } else { |
| 2239 | old_name = name; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2240 | } |
| 2241 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2242 | mutex_unlock(&cgroup_mutex); |
| 2243 | mutex_unlock(&cgroup_tree_mutex); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2244 | |
| 2245 | kfree_rcu(old_name, rcu_head); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2246 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2249 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2250 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2251 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2252 | struct kernfs_node *kn; |
| 2253 | struct lock_class_key *key = NULL; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2254 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2255 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 2256 | key = &cft->lockdep_key; |
| 2257 | #endif |
| 2258 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
| 2259 | cgroup_file_mode(cft), 0, cft->kf_ops, cft, |
| 2260 | NULL, false, key); |
| 2261 | if (IS_ERR(kn)) |
| 2262 | return PTR_ERR(kn); |
| 2263 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2264 | } |
| 2265 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2266 | /** |
| 2267 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2268 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2269 | * @cfts: array of cftypes to be added |
| 2270 | * @is_add: whether to add or remove |
| 2271 | * |
| 2272 | * 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] | 2273 | * For removals, this function never fails. If addition fails, this |
| 2274 | * function doesn't remove files already added. The caller is responsible |
| 2275 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2276 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2277 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2278 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2279 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2280 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2281 | int ret; |
| 2282 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2283 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2284 | |
| 2285 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2286 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2287 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2288 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2289 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2290 | continue; |
| 2291 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2292 | continue; |
| 2293 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2294 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2295 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2296 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2297 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2298 | cft->name, ret); |
| 2299 | return ret; |
| 2300 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2301 | } else { |
| 2302 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2303 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2304 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2305 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame^] | 2308 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2309 | { |
| 2310 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2311 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2312 | struct cgroup *root = &ss->root->top_cgroup; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2313 | struct cgroup_subsys_state *css; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2314 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2315 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame^] | 2316 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2317 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame^] | 2318 | /* don't bother if @ss isn't attached */ |
| 2319 | if (ss->root == &cgroup_dummy_root) |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2320 | return 0; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2321 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2322 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2323 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2324 | struct cgroup *cgrp = css->cgroup; |
| 2325 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2326 | if (cgroup_is_dead(cgrp)) |
| 2327 | continue; |
| 2328 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame^] | 2329 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2330 | if (ret) |
| 2331 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2332 | } |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame^] | 2333 | |
| 2334 | if (is_add && !ret) |
| 2335 | kernfs_activate(root->kn); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2336 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2337 | } |
| 2338 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2339 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 2340 | { |
| 2341 | struct cftype *cft; |
| 2342 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2343 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2344 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 2345 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 2346 | kfree(cft->kf_ops); |
| 2347 | cft->kf_ops = NULL; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2348 | cft->ss = NULL; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2349 | } |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2350 | } |
| 2351 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2352 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2353 | { |
| 2354 | struct cftype *cft; |
| 2355 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2356 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2357 | struct kernfs_ops *kf_ops; |
| 2358 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2359 | WARN_ON(cft->ss || cft->kf_ops); |
| 2360 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2361 | if (cft->seq_start) |
| 2362 | kf_ops = &cgroup_kf_ops; |
| 2363 | else |
| 2364 | kf_ops = &cgroup_kf_single_ops; |
| 2365 | |
| 2366 | /* |
| 2367 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 2368 | * make a copy of kf_ops to set its atomic_write_len. |
| 2369 | */ |
| 2370 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 2371 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 2372 | if (!kf_ops) { |
| 2373 | cgroup_exit_cftypes(cfts); |
| 2374 | return -ENOMEM; |
| 2375 | } |
| 2376 | kf_ops->atomic_write_len = cft->max_write_len; |
| 2377 | } |
| 2378 | |
| 2379 | cft->kf_ops = kf_ops; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2380 | cft->ss = ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | return 0; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2384 | } |
| 2385 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame^] | 2386 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 2387 | { |
| 2388 | lockdep_assert_held(&cgroup_tree_mutex); |
| 2389 | |
| 2390 | if (!cfts || !cfts[0].ss) |
| 2391 | return -ENOENT; |
| 2392 | |
| 2393 | list_del(&cfts->node); |
| 2394 | cgroup_apply_cftypes(cfts, false); |
| 2395 | cgroup_exit_cftypes(cfts); |
| 2396 | return 0; |
| 2397 | } |
| 2398 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2399 | /** |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2400 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2401 | * @cfts: zero-length name terminated array of cftypes |
| 2402 | * |
| 2403 | * Unregister @cfts. Files described by @cfts are removed from all |
| 2404 | * existing cgroups and all future cgroups won't have them either. This |
| 2405 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 2406 | * |
| 2407 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2408 | * registered. |
| 2409 | */ |
| 2410 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 2411 | { |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame^] | 2412 | int ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2413 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame^] | 2414 | mutex_lock(&cgroup_tree_mutex); |
| 2415 | ret = cgroup_rm_cftypes_locked(cfts); |
| 2416 | mutex_unlock(&cgroup_tree_mutex); |
| 2417 | return ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | /** |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2421 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2422 | * @ss: target cgroup subsystem |
| 2423 | * @cfts: zero-length name terminated array of cftypes |
| 2424 | * |
| 2425 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2426 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2427 | * have them too. This function can be called anytime whether @ss is |
| 2428 | * attached or not. |
| 2429 | * |
| 2430 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2431 | * function currently returns 0 as long as @cfts registration is successful |
| 2432 | * even if some file creation attempts on existing cgroups fail. |
| 2433 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2434 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2435 | { |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2436 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2437 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2438 | ret = cgroup_init_cftypes(ss, cfts); |
| 2439 | if (ret) |
| 2440 | return ret; |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2441 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame^] | 2442 | mutex_lock(&cgroup_tree_mutex); |
| 2443 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2444 | list_add_tail(&cfts->node, &ss->cfts); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame^] | 2445 | ret = cgroup_apply_cftypes(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2446 | if (ret) |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame^] | 2447 | cgroup_rm_cftypes_locked(cfts); |
| 2448 | |
| 2449 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2450 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2451 | } |
| 2452 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2453 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2454 | /** |
| 2455 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2456 | * @cgrp: the cgroup in question |
| 2457 | * |
| 2458 | * Return the number of tasks in the cgroup. |
| 2459 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2460 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2461 | { |
| 2462 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2463 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2464 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2465 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2466 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2467 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2468 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2469 | return count; |
| 2470 | } |
| 2471 | |
| 2472 | /* |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2473 | * To reduce the fork() overhead for systems that are not actually using |
| 2474 | * their cgroups capability, we don't maintain the lists running through |
| 2475 | * 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] | 2476 | * words after the first call to css_task_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2477 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2478 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2479 | { |
| 2480 | struct task_struct *p, *g; |
| 2481 | write_lock(&css_set_lock); |
| 2482 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2483 | /* |
| 2484 | * We need tasklist_lock because RCU is not safe against |
| 2485 | * while_each_thread(). Besides, a forking task that has passed |
| 2486 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2487 | * is not guaranteed to have its child immediately visible in the |
| 2488 | * tasklist if we walk through it with RCU. |
| 2489 | */ |
| 2490 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2491 | do_each_thread(g, p) { |
| 2492 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2493 | /* |
| 2494 | * We should check if the process is exiting, otherwise |
| 2495 | * it will race with cgroup_exit() in that the list |
| 2496 | * entry won't be deleted though the process has exited. |
| 2497 | */ |
| 2498 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2499 | list_add(&p->cg_list, &task_css_set(p)->tasks); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2500 | task_unlock(p); |
| 2501 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2502 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2503 | write_unlock(&css_set_lock); |
| 2504 | } |
| 2505 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2506 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2507 | * css_next_child - find the next child of a given css |
| 2508 | * @pos_css: the current position (%NULL to initiate traversal) |
| 2509 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2510 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2511 | * This function returns the next child of @parent_css and should be called |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2512 | * under either cgroup_mutex or RCU read lock. The only requirement is |
| 2513 | * that @parent_css and @pos_css are accessible. The next sibling is |
| 2514 | * guaranteed to be returned regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2515 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2516 | struct cgroup_subsys_state * |
| 2517 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 2518 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2519 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2520 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 2521 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2522 | struct cgroup *next; |
| 2523 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2524 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2525 | |
| 2526 | /* |
| 2527 | * @pos could already have been removed. Once a cgroup is removed, |
| 2528 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 2529 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 2530 | * before the cgroup is taken off the ->sibling list, if we see it |
| 2531 | * unasserted, it's guaranteed that the next sibling hasn't |
| 2532 | * finished its grace period even if it's already removed, and thus |
| 2533 | * safe to dereference from this RCU critical section. If |
| 2534 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 2535 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2536 | * |
| 2537 | * If @pos is dead, its next pointer can't be dereferenced; |
| 2538 | * however, as each cgroup is given a monotonically increasing |
| 2539 | * unique serial number and always appended to the sibling list, |
| 2540 | * the next one can be found by walking the parent's children until |
| 2541 | * we see a cgroup with higher serial number than @pos's. While |
| 2542 | * this path can be slower, it's taken only when either the current |
| 2543 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2544 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2545 | if (!pos) { |
| 2546 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 2547 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2548 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2549 | } else { |
| 2550 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 2551 | if (next->serial_nr > pos->serial_nr) |
| 2552 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2553 | } |
| 2554 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2555 | if (&next->sibling == &cgrp->children) |
| 2556 | return NULL; |
| 2557 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2558 | return cgroup_css(next, parent_css->ss); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2559 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2560 | EXPORT_SYMBOL_GPL(css_next_child); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2561 | |
| 2562 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2563 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2564 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2565 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2566 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2567 | * 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] | 2568 | * to visit for pre-order traversal of @root's descendants. @root is |
| 2569 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2570 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2571 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2572 | * doesn't require the whole traversal to be contained in a single critical |
| 2573 | * section. This function will return the correct next descendant as long |
| 2574 | * as both @pos and @root are accessible and @pos is a descendant of @root. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2575 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2576 | struct cgroup_subsys_state * |
| 2577 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 2578 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2579 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2580 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2581 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2582 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2583 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2584 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2585 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2586 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2587 | |
| 2588 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2589 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2590 | if (next) |
| 2591 | return next; |
| 2592 | |
| 2593 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2594 | while (pos != root) { |
| 2595 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2596 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2597 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2598 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2599 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2600 | |
| 2601 | return NULL; |
| 2602 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2603 | EXPORT_SYMBOL_GPL(css_next_descendant_pre); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2604 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2605 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2606 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 2607 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2608 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2609 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 2610 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2611 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2612 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2613 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2614 | * doesn't require the whole traversal to be contained in a single critical |
| 2615 | * section. This function will return the correct rightmost descendant as |
| 2616 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2617 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2618 | struct cgroup_subsys_state * |
| 2619 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2620 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2621 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2622 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2623 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2624 | |
| 2625 | do { |
| 2626 | last = pos; |
| 2627 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 2628 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2629 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2630 | pos = tmp; |
| 2631 | } while (pos); |
| 2632 | |
| 2633 | return last; |
| 2634 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2635 | EXPORT_SYMBOL_GPL(css_rightmost_descendant); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2636 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2637 | static struct cgroup_subsys_state * |
| 2638 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2639 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2640 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2641 | |
| 2642 | do { |
| 2643 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2644 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2645 | } while (pos); |
| 2646 | |
| 2647 | return last; |
| 2648 | } |
| 2649 | |
| 2650 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2651 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2652 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2653 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2654 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2655 | * 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] | 2656 | * to visit for post-order traversal of @root's descendants. @root is |
| 2657 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2658 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2659 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2660 | * doesn't require the whole traversal to be contained in a single critical |
| 2661 | * section. This function will return the correct next descendant as long |
| 2662 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 2663 | * @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2664 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2665 | struct cgroup_subsys_state * |
| 2666 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 2667 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2668 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2669 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2670 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2671 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2672 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 2673 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 2674 | if (!pos) |
| 2675 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2676 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2677 | /* if we visited @root, we're done */ |
| 2678 | if (pos == root) |
| 2679 | return NULL; |
| 2680 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2681 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2682 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2683 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2684 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2685 | |
| 2686 | /* no sibling left, visit parent */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2687 | return css_parent(pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2688 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2689 | EXPORT_SYMBOL_GPL(css_next_descendant_post); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2690 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2691 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2692 | * 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] | 2693 | * @it: the iterator to advance |
| 2694 | * |
| 2695 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2696 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2697 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2698 | { |
| 2699 | struct list_head *l = it->cset_link; |
| 2700 | struct cgrp_cset_link *link; |
| 2701 | struct css_set *cset; |
| 2702 | |
| 2703 | /* Advance to the next non-empty css_set */ |
| 2704 | do { |
| 2705 | l = l->next; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2706 | if (l == &it->origin_css->cgroup->cset_links) { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2707 | it->cset_link = NULL; |
| 2708 | return; |
| 2709 | } |
| 2710 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 2711 | cset = link->cset; |
| 2712 | } while (list_empty(&cset->tasks)); |
| 2713 | it->cset_link = l; |
| 2714 | it->task = cset->tasks.next; |
| 2715 | } |
| 2716 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2717 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2718 | * css_task_iter_start - initiate task iteration |
| 2719 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2720 | * @it: the task iterator to use |
| 2721 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2722 | * Initiate iteration through the tasks of @css. The caller can call |
| 2723 | * css_task_iter_next() to walk through the tasks until the function |
| 2724 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 2725 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2726 | * |
| 2727 | * Note that this function acquires a lock which is released when the |
| 2728 | * iteration finishes. The caller can't sleep while iteration is in |
| 2729 | * progress. |
| 2730 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2731 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 2732 | struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 2733 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2734 | { |
| 2735 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2736 | * The first time anyone tries to iterate across a css, we need to |
| 2737 | * enable the list linking each css_set to its tasks, and fix up |
| 2738 | * all existing tasks. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2739 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2740 | if (!use_task_css_set_links) |
| 2741 | cgroup_enable_task_cg_lists(); |
| 2742 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2743 | read_lock(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2744 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2745 | it->origin_css = css; |
| 2746 | it->cset_link = &css->cgroup->cset_links; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2747 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2748 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2749 | } |
| 2750 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2751 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2752 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2753 | * @it: the task iterator being iterated |
| 2754 | * |
| 2755 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2756 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 2757 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2758 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2759 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2760 | { |
| 2761 | struct task_struct *res; |
| 2762 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2763 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2764 | |
| 2765 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2766 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2767 | return NULL; |
| 2768 | res = list_entry(l, struct task_struct, cg_list); |
| 2769 | /* Advance iterator to find next entry */ |
| 2770 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2771 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 2772 | if (l == &link->cset->tasks) { |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2773 | /* |
| 2774 | * We reached the end of this task list - move on to the |
| 2775 | * next cgrp_cset_link. |
| 2776 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2777 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2778 | } else { |
| 2779 | it->task = l; |
| 2780 | } |
| 2781 | return res; |
| 2782 | } |
| 2783 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2784 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2785 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2786 | * @it: the task iterator to finish |
| 2787 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2788 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2789 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2790 | void css_task_iter_end(struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 2791 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2792 | { |
| 2793 | read_unlock(&css_set_lock); |
| 2794 | } |
| 2795 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2796 | static inline int started_after_time(struct task_struct *t1, |
| 2797 | struct timespec *time, |
| 2798 | struct task_struct *t2) |
| 2799 | { |
| 2800 | int start_diff = timespec_compare(&t1->start_time, time); |
| 2801 | if (start_diff > 0) { |
| 2802 | return 1; |
| 2803 | } else if (start_diff < 0) { |
| 2804 | return 0; |
| 2805 | } else { |
| 2806 | /* |
| 2807 | * Arbitrarily, if two processes started at the same |
| 2808 | * time, we'll say that the lower pointer value |
| 2809 | * started first. Note that t2 may have exited by now |
| 2810 | * so this may not be a valid pointer any longer, but |
| 2811 | * that's fine - it still serves to distinguish |
| 2812 | * between two tasks started (effectively) simultaneously. |
| 2813 | */ |
| 2814 | return t1 > t2; |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | /* |
| 2819 | * This function is a callback from heap_insert() and is used to order |
| 2820 | * the heap. |
| 2821 | * In this case we order the heap in descending task start time. |
| 2822 | */ |
| 2823 | static inline int started_after(void *p1, void *p2) |
| 2824 | { |
| 2825 | struct task_struct *t1 = p1; |
| 2826 | struct task_struct *t2 = p2; |
| 2827 | return started_after_time(t1, &t2->start_time, t2); |
| 2828 | } |
| 2829 | |
| 2830 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2831 | * css_scan_tasks - iterate though all the tasks in a css |
| 2832 | * @css: the css to iterate tasks of |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2833 | * @test: optional test callback |
| 2834 | * @process: process callback |
| 2835 | * @data: data passed to @test and @process |
| 2836 | * @heap: optional pre-allocated heap used for task iteration |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2837 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2838 | * Iterate through all the tasks in @css, calling @test for each, and if it |
| 2839 | * returns %true, call @process for it also. |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2840 | * |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2841 | * @test may be NULL, meaning always true (select all tasks), which |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2842 | * effectively duplicates css_task_iter_{start,next,end}() but does not |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2843 | * lock css_set_lock for the call to @process. |
| 2844 | * |
| 2845 | * 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] | 2846 | * of @css for the duration of this call. This function may or may not |
| 2847 | * call @process for tasks that exit or move to a different css during the |
| 2848 | * call, or are forked or move into the css during the call. |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2849 | * |
| 2850 | * Note that @test may be called with locks held, and may in some |
| 2851 | * situations be called multiple times for the same task, so it should be |
| 2852 | * cheap. |
| 2853 | * |
| 2854 | * If @heap is non-NULL, a heap has been pre-allocated and will be used for |
| 2855 | * heap operations (and its "gt" member will be overwritten), else a |
| 2856 | * temporary heap will be used (allocation of which may cause this function |
| 2857 | * to fail). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2858 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2859 | int css_scan_tasks(struct cgroup_subsys_state *css, |
| 2860 | bool (*test)(struct task_struct *, void *), |
| 2861 | void (*process)(struct task_struct *, void *), |
| 2862 | void *data, struct ptr_heap *heap) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2863 | { |
| 2864 | int retval, i; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2865 | struct css_task_iter it; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2866 | struct task_struct *p, *dropped; |
| 2867 | /* Never dereference latest_task, since it's not refcounted */ |
| 2868 | struct task_struct *latest_task = NULL; |
| 2869 | struct ptr_heap tmp_heap; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2870 | struct timespec latest_time = { 0, 0 }; |
| 2871 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2872 | if (heap) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2873 | /* The caller supplied our heap and pre-allocated its memory */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2874 | heap->gt = &started_after; |
| 2875 | } else { |
| 2876 | /* We need to allocate our own heap memory */ |
| 2877 | heap = &tmp_heap; |
| 2878 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 2879 | if (retval) |
| 2880 | /* cannot allocate the heap */ |
| 2881 | return retval; |
| 2882 | } |
| 2883 | |
| 2884 | again: |
| 2885 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2886 | * Scan tasks in the css, using the @test callback to determine |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2887 | * which are of interest, and invoking @process callback on the |
| 2888 | * ones which need an update. Since we don't want to hold any |
| 2889 | * locks during the task updates, gather tasks to be processed in a |
| 2890 | * heap structure. The heap is sorted by descending task start |
| 2891 | * time. If the statically-sized heap fills up, we overflow tasks |
| 2892 | * that started later, and in future iterations only consider tasks |
| 2893 | * that started after the latest task in the previous pass. This |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2894 | * guarantees forward progress and that we don't miss any tasks. |
| 2895 | */ |
| 2896 | heap->size = 0; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2897 | css_task_iter_start(css, &it); |
| 2898 | while ((p = css_task_iter_next(&it))) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2899 | /* |
| 2900 | * Only affect tasks that qualify per the caller's callback, |
| 2901 | * if he provided one |
| 2902 | */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2903 | if (test && !test(p, data)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2904 | continue; |
| 2905 | /* |
| 2906 | * Only process tasks that started after the last task |
| 2907 | * we processed |
| 2908 | */ |
| 2909 | if (!started_after_time(p, &latest_time, latest_task)) |
| 2910 | continue; |
| 2911 | dropped = heap_insert(heap, p); |
| 2912 | if (dropped == NULL) { |
| 2913 | /* |
| 2914 | * The new task was inserted; the heap wasn't |
| 2915 | * previously full |
| 2916 | */ |
| 2917 | get_task_struct(p); |
| 2918 | } else if (dropped != p) { |
| 2919 | /* |
| 2920 | * The new task was inserted, and pushed out a |
| 2921 | * different task |
| 2922 | */ |
| 2923 | get_task_struct(p); |
| 2924 | put_task_struct(dropped); |
| 2925 | } |
| 2926 | /* |
| 2927 | * Else the new task was newer than anything already in |
| 2928 | * the heap and wasn't inserted |
| 2929 | */ |
| 2930 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2931 | css_task_iter_end(&it); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2932 | |
| 2933 | if (heap->size) { |
| 2934 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 2935 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2936 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 2937 | latest_time = q->start_time; |
| 2938 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2939 | } |
| 2940 | /* Process the task per the caller's callback */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2941 | process(q, data); |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 2942 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2943 | } |
| 2944 | /* |
| 2945 | * If we had to process any tasks at all, scan again |
| 2946 | * in case some of them were in the middle of forking |
| 2947 | * children that didn't get processed. |
| 2948 | * Not the most efficient way to do it, but it avoids |
| 2949 | * having to take callback_mutex in the fork path |
| 2950 | */ |
| 2951 | goto again; |
| 2952 | } |
| 2953 | if (heap == &tmp_heap) |
| 2954 | heap_free(&tmp_heap); |
| 2955 | return 0; |
| 2956 | } |
| 2957 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2958 | static void cgroup_transfer_one_task(struct task_struct *task, void *data) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 2959 | { |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2960 | struct cgroup *new_cgroup = data; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 2961 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2962 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 2963 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2964 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | /** |
| 2968 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 2969 | * @to: cgroup to which the tasks will be moved |
| 2970 | * @from: cgroup in which the tasks currently reside |
| 2971 | */ |
| 2972 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 2973 | { |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2974 | return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task, |
| 2975 | to, NULL); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 2976 | } |
| 2977 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2978 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2979 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2980 | * |
| 2981 | * Reading this file can return large amounts of data if a cgroup has |
| 2982 | * *lots* of attached tasks. So it may need several calls to read(), |
| 2983 | * but we cannot guarantee that the information we produce is correct |
| 2984 | * unless we produce it entirely atomically. |
| 2985 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2986 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2987 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 2988 | /* which pidlist file are we talking about? */ |
| 2989 | enum cgroup_filetype { |
| 2990 | CGROUP_FILE_PROCS, |
| 2991 | CGROUP_FILE_TASKS, |
| 2992 | }; |
| 2993 | |
| 2994 | /* |
| 2995 | * A pidlist is a list of pids that virtually represents the contents of one |
| 2996 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 2997 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 2998 | * to the cgroup. |
| 2999 | */ |
| 3000 | struct cgroup_pidlist { |
| 3001 | /* |
| 3002 | * used to find which pidlist is wanted. doesn't change as long as |
| 3003 | * this particular list stays in the list. |
| 3004 | */ |
| 3005 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3006 | /* array of xids */ |
| 3007 | pid_t *list; |
| 3008 | /* how many elements the above list has */ |
| 3009 | int length; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3010 | /* each of these stored in a list by its cgroup */ |
| 3011 | struct list_head links; |
| 3012 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3013 | struct cgroup *owner; |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3014 | /* for delayed destruction */ |
| 3015 | struct delayed_work destroy_dwork; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3016 | }; |
| 3017 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3018 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3019 | * The following two functions "fix" the issue where there are more pids |
| 3020 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3021 | * TODO: replace with a kernel-wide solution to this problem |
| 3022 | */ |
| 3023 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3024 | static void *pidlist_allocate(int count) |
| 3025 | { |
| 3026 | if (PIDLIST_TOO_LARGE(count)) |
| 3027 | return vmalloc(count * sizeof(pid_t)); |
| 3028 | else |
| 3029 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3030 | } |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3031 | |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3032 | static void pidlist_free(void *p) |
| 3033 | { |
| 3034 | if (is_vmalloc_addr(p)) |
| 3035 | vfree(p); |
| 3036 | else |
| 3037 | kfree(p); |
| 3038 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3039 | |
| 3040 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3041 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 3042 | * should be left afterwards. |
| 3043 | */ |
| 3044 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 3045 | { |
| 3046 | struct cgroup_pidlist *l, *tmp_l; |
| 3047 | |
| 3048 | mutex_lock(&cgrp->pidlist_mutex); |
| 3049 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 3050 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 3051 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3052 | |
| 3053 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 3054 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 3055 | } |
| 3056 | |
| 3057 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 3058 | { |
| 3059 | struct delayed_work *dwork = to_delayed_work(work); |
| 3060 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 3061 | destroy_dwork); |
| 3062 | struct cgroup_pidlist *tofree = NULL; |
| 3063 | |
| 3064 | mutex_lock(&l->owner->pidlist_mutex); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3065 | |
| 3066 | /* |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3067 | * Destroy iff we didn't get queued again. The state won't change |
| 3068 | * as destroy_dwork can only be queued while locked. |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3069 | */ |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3070 | if (!delayed_work_pending(dwork)) { |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3071 | list_del(&l->links); |
| 3072 | pidlist_free(l->list); |
| 3073 | put_pid_ns(l->key.ns); |
| 3074 | tofree = l; |
| 3075 | } |
| 3076 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3077 | mutex_unlock(&l->owner->pidlist_mutex); |
| 3078 | kfree(tofree); |
| 3079 | } |
| 3080 | |
| 3081 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3082 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3083 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3084 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3085 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3086 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3087 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3088 | |
| 3089 | /* |
| 3090 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3091 | * edge cases first; no work needs to be done for either |
| 3092 | */ |
| 3093 | if (length == 0 || length == 1) |
| 3094 | return length; |
| 3095 | /* src and dest walk down the list; dest counts unique elements */ |
| 3096 | for (src = 1; src < length; src++) { |
| 3097 | /* find next unique element */ |
| 3098 | while (list[src] == list[src-1]) { |
| 3099 | src++; |
| 3100 | if (src == length) |
| 3101 | goto after; |
| 3102 | } |
| 3103 | /* dest always points to where the next unique element goes */ |
| 3104 | list[dest] = list[src]; |
| 3105 | dest++; |
| 3106 | } |
| 3107 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3108 | return dest; |
| 3109 | } |
| 3110 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3111 | /* |
| 3112 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 3113 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 3114 | * different per namespace, each namespace needs differently sorted list, |
| 3115 | * making it impossible to use, for example, single rbtree of member tasks |
| 3116 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 3117 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 3118 | * pidlists keyed by cgroup and namespace. |
| 3119 | * |
| 3120 | * All this extra complexity was caused by the original implementation |
| 3121 | * committing to an entirely unnecessary property. In the long term, we |
| 3122 | * want to do away with it. Explicitly scramble sort order if |
| 3123 | * sane_behavior so that no such expectation exists in the new interface. |
| 3124 | * |
| 3125 | * Scrambling is done by swapping every two consecutive bits, which is |
| 3126 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 3127 | */ |
| 3128 | static pid_t pid_fry(pid_t pid) |
| 3129 | { |
| 3130 | unsigned a = pid & 0x55555555; |
| 3131 | unsigned b = pid & 0xAAAAAAAA; |
| 3132 | |
| 3133 | return (a << 1) | (b >> 1); |
| 3134 | } |
| 3135 | |
| 3136 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 3137 | { |
| 3138 | if (cgroup_sane_behavior(cgrp)) |
| 3139 | return pid_fry(pid); |
| 3140 | else |
| 3141 | return pid; |
| 3142 | } |
| 3143 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3144 | static int cmppid(const void *a, const void *b) |
| 3145 | { |
| 3146 | return *(pid_t *)a - *(pid_t *)b; |
| 3147 | } |
| 3148 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3149 | static int fried_cmppid(const void *a, const void *b) |
| 3150 | { |
| 3151 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 3152 | } |
| 3153 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3154 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3155 | enum cgroup_filetype type) |
| 3156 | { |
| 3157 | struct cgroup_pidlist *l; |
| 3158 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3159 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3160 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3161 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3162 | |
| 3163 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 3164 | if (l->key.type == type && l->key.ns == ns) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3165 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3166 | return NULL; |
| 3167 | } |
| 3168 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3169 | /* |
| 3170 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3171 | * returns with the lock on that pidlist already held, and takes care |
| 3172 | * of the use count, or returns NULL with no locks held if we're out of |
| 3173 | * memory. |
| 3174 | */ |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3175 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 3176 | enum cgroup_filetype type) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3177 | { |
| 3178 | struct cgroup_pidlist *l; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3179 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3180 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3181 | |
| 3182 | l = cgroup_pidlist_find(cgrp, type); |
| 3183 | if (l) |
| 3184 | return l; |
| 3185 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3186 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3187 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3188 | if (!l) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3189 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3190 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3191 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3192 | l->key.type = type; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3193 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 3194 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3195 | l->owner = cgrp; |
| 3196 | list_add(&l->links, &cgrp->pidlists); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3197 | return l; |
| 3198 | } |
| 3199 | |
| 3200 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3201 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3202 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3203 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3204 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3205 | { |
| 3206 | pid_t *array; |
| 3207 | int length; |
| 3208 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3209 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3210 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3211 | struct cgroup_pidlist *l; |
| 3212 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3213 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3214 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3215 | /* |
| 3216 | * If cgroup gets more users after we read count, we won't have |
| 3217 | * enough space - tough. This race is indistinguishable to the |
| 3218 | * caller from the case that the additional cgroup users didn't |
| 3219 | * show up until sometime later on. |
| 3220 | */ |
| 3221 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3222 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3223 | if (!array) |
| 3224 | return -ENOMEM; |
| 3225 | /* now, populate the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3226 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3227 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3228 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3229 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3230 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3231 | if (type == CGROUP_FILE_PROCS) |
| 3232 | pid = task_tgid_vnr(tsk); |
| 3233 | else |
| 3234 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3235 | if (pid > 0) /* make sure to only use valid results */ |
| 3236 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3237 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3238 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3239 | length = n; |
| 3240 | /* now sort & (if procs) strip out duplicates */ |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3241 | if (cgroup_sane_behavior(cgrp)) |
| 3242 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 3243 | else |
| 3244 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3245 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3246 | length = pidlist_uniq(array, length); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3247 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3248 | l = cgroup_pidlist_find_create(cgrp, type); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3249 | if (!l) { |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3250 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3251 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3252 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3253 | } |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3254 | |
| 3255 | /* store array, freeing old if necessary */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3256 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3257 | l->list = array; |
| 3258 | l->length = length; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3259 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3260 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3261 | } |
| 3262 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3263 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3264 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3265 | * @stats: cgroupstats to fill information into |
| 3266 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3267 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3268 | * |
| 3269 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3270 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3271 | */ |
| 3272 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3273 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3274 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3275 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3276 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3277 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3278 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3279 | /* it should be kernfs_node belonging to cgroupfs and is a directory */ |
| 3280 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 3281 | kernfs_type(kn) != KERNFS_DIR) |
| 3282 | return -EINVAL; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3283 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3284 | /* |
| 3285 | * We aren't being called from kernfs and there's no guarantee on |
| 3286 | * @kn->priv's validity. For this and css_tryget_from_dir(), |
| 3287 | * @kn->priv is RCU safe. Let's do the RCU dancing. |
| 3288 | */ |
| 3289 | rcu_read_lock(); |
| 3290 | cgrp = rcu_dereference(kn->priv); |
| 3291 | if (!cgrp) { |
| 3292 | rcu_read_unlock(); |
| 3293 | return -ENOENT; |
| 3294 | } |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3295 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3296 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3297 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3298 | switch (tsk->state) { |
| 3299 | case TASK_RUNNING: |
| 3300 | stats->nr_running++; |
| 3301 | break; |
| 3302 | case TASK_INTERRUPTIBLE: |
| 3303 | stats->nr_sleeping++; |
| 3304 | break; |
| 3305 | case TASK_UNINTERRUPTIBLE: |
| 3306 | stats->nr_uninterruptible++; |
| 3307 | break; |
| 3308 | case TASK_STOPPED: |
| 3309 | stats->nr_stopped++; |
| 3310 | break; |
| 3311 | default: |
| 3312 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3313 | stats->nr_io_wait++; |
| 3314 | break; |
| 3315 | } |
| 3316 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3317 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3318 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3319 | rcu_read_unlock(); |
| 3320 | return 0; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3321 | } |
| 3322 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3323 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3324 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3325 | * 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] | 3326 | * 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] | 3327 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3328 | */ |
| 3329 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3330 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3331 | { |
| 3332 | /* |
| 3333 | * Initially we receive a position value that corresponds to |
| 3334 | * one more than the last pid shown (or 0 on the first call or |
| 3335 | * after a seek to the start). Use a binary-search to find the |
| 3336 | * next pid to display, if any |
| 3337 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3338 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3339 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3340 | struct cgroup_pidlist *l; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3341 | enum cgroup_filetype type = seq_cft(s)->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3342 | int index = 0, pid = *pos; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3343 | int *iter, ret; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3344 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3345 | mutex_lock(&cgrp->pidlist_mutex); |
| 3346 | |
| 3347 | /* |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3348 | * !NULL @of->priv indicates that this isn't the first start() |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3349 | * after open. If the matching pidlist is around, we can use that. |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3350 | * Look for it. Note that @of->priv can't be used directly. It |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3351 | * could already have been destroyed. |
| 3352 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3353 | if (of->priv) |
| 3354 | of->priv = cgroup_pidlist_find(cgrp, type); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3355 | |
| 3356 | /* |
| 3357 | * Either this is the first start() after open or the matching |
| 3358 | * pidlist has been destroyed inbetween. Create a new one. |
| 3359 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3360 | if (!of->priv) { |
| 3361 | ret = pidlist_array_load(cgrp, type, |
| 3362 | (struct cgroup_pidlist **)&of->priv); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3363 | if (ret) |
| 3364 | return ERR_PTR(ret); |
| 3365 | } |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3366 | l = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3367 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3368 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3369 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3370 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3371 | while (index < end) { |
| 3372 | int mid = (index + end) / 2; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3373 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3374 | index = mid; |
| 3375 | break; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3376 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3377 | index = mid + 1; |
| 3378 | else |
| 3379 | end = mid; |
| 3380 | } |
| 3381 | } |
| 3382 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3383 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3384 | return NULL; |
| 3385 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3386 | iter = l->list + index; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3387 | *pos = cgroup_pid_fry(cgrp, *iter); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3388 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3389 | } |
| 3390 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3391 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3392 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3393 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3394 | struct cgroup_pidlist *l = of->priv; |
Tejun Heo | 6223685 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3395 | |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3396 | if (l) |
| 3397 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3398 | CGROUP_PIDLIST_DESTROY_DELAY); |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3399 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3400 | } |
| 3401 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3402 | 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] | 3403 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3404 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3405 | struct cgroup_pidlist *l = of->priv; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3406 | pid_t *p = v; |
| 3407 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3408 | /* |
| 3409 | * Advance to the next pid in the array. If this goes off the |
| 3410 | * end, we're done |
| 3411 | */ |
| 3412 | p++; |
| 3413 | if (p >= end) { |
| 3414 | return NULL; |
| 3415 | } else { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3416 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3417 | return p; |
| 3418 | } |
| 3419 | } |
| 3420 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3421 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3422 | { |
| 3423 | return seq_printf(s, "%d\n", *(int *)v); |
| 3424 | } |
| 3425 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3426 | /* |
| 3427 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3428 | * independent of whether it's tasks or procs |
| 3429 | */ |
| 3430 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3431 | .start = cgroup_pidlist_start, |
| 3432 | .stop = cgroup_pidlist_stop, |
| 3433 | .next = cgroup_pidlist_next, |
| 3434 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3435 | }; |
| 3436 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3437 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3438 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3439 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3440 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3441 | } |
| 3442 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3443 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3444 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3445 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3446 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3447 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3448 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3449 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3450 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3451 | return 0; |
| 3452 | } |
| 3453 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3454 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 3455 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3456 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3457 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3458 | } |
| 3459 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3460 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 3461 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3462 | { |
| 3463 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3464 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3465 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3466 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3467 | return 0; |
| 3468 | } |
| 3469 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3470 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3471 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3472 | .name = "cgroup.procs", |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3473 | .seq_start = cgroup_pidlist_start, |
| 3474 | .seq_next = cgroup_pidlist_next, |
| 3475 | .seq_stop = cgroup_pidlist_stop, |
| 3476 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3477 | .private = CGROUP_FILE_PROCS, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3478 | .write_u64 = cgroup_procs_write, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3479 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3480 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3481 | { |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3482 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3483 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3484 | .read_u64 = cgroup_clone_children_read, |
| 3485 | .write_u64 = cgroup_clone_children_write, |
| 3486 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3487 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3488 | .name = "cgroup.sane_behavior", |
| 3489 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3490 | .seq_show = cgroup_sane_behavior_show, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3491 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3492 | |
| 3493 | /* |
| 3494 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 3495 | * don't exist if sane_behavior. If you're depending on these, be |
| 3496 | * prepared to be burned. |
| 3497 | */ |
| 3498 | { |
| 3499 | .name = "tasks", |
| 3500 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3501 | .seq_start = cgroup_pidlist_start, |
| 3502 | .seq_next = cgroup_pidlist_next, |
| 3503 | .seq_stop = cgroup_pidlist_stop, |
| 3504 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3505 | .private = CGROUP_FILE_TASKS, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3506 | .write_u64 = cgroup_tasks_write, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3507 | .mode = S_IRUGO | S_IWUSR, |
| 3508 | }, |
| 3509 | { |
| 3510 | .name = "notify_on_release", |
| 3511 | .flags = CFTYPE_INSANE, |
| 3512 | .read_u64 = cgroup_read_notify_on_release, |
| 3513 | .write_u64 = cgroup_write_notify_on_release, |
| 3514 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3515 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3516 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 3517 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3518 | .seq_show = cgroup_release_agent_show, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3519 | .write_string = cgroup_release_agent_write, |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3520 | .max_write_len = PATH_MAX - 1, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3521 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3522 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3523 | }; |
| 3524 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3525 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3526 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3527 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3528 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3529 | * |
| 3530 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3531 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3532 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3533 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3534 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3535 | int i, ret = 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3536 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3537 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3538 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3539 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3540 | |
| 3541 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3542 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3543 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3544 | list_for_each_entry(cfts, &ss->cfts, node) { |
| 3545 | ret = cgroup_addrm_files(cgrp, cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3546 | if (ret < 0) |
| 3547 | goto err; |
| 3548 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3549 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3550 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3551 | err: |
| 3552 | cgroup_clear_dir(cgrp, subsys_mask); |
| 3553 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3554 | } |
| 3555 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3556 | /* |
| 3557 | * css destruction is four-stage process. |
| 3558 | * |
| 3559 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 3560 | * Implemented in kill_css(). |
| 3561 | * |
| 3562 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
| 3563 | * and thus css_tryget() is guaranteed to fail, the css can be offlined |
| 3564 | * by invoking offline_css(). After offlining, the base ref is put. |
| 3565 | * Implemented in css_killed_work_fn(). |
| 3566 | * |
| 3567 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 3568 | * accessors are inside RCU read sections. css_release() schedules the |
| 3569 | * RCU callback. |
| 3570 | * |
| 3571 | * 4. After the grace period, the css can be freed. Implemented in |
| 3572 | * css_free_work_fn(). |
| 3573 | * |
| 3574 | * It is actually hairier because both step 2 and 4 require process context |
| 3575 | * and thus involve punting to css->destroy_work adding two additional |
| 3576 | * steps to the already complex sequence. |
| 3577 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3578 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3579 | { |
| 3580 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3581 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3582 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3583 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3584 | if (css->parent) |
| 3585 | css_put(css->parent); |
| 3586 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3587 | css->ss->css_free(css); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3588 | cgroup_put(cgrp); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 3592 | { |
| 3593 | struct cgroup_subsys_state *css = |
| 3594 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 3595 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3596 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 3597 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3598 | } |
| 3599 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3600 | static void css_release(struct percpu_ref *ref) |
| 3601 | { |
| 3602 | struct cgroup_subsys_state *css = |
| 3603 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 3604 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3605 | rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3606 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3607 | } |
| 3608 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3609 | static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss, |
| 3610 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3611 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3612 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 3613 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3614 | css->flags = 0; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3615 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3616 | if (cgrp->parent) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3617 | css->parent = cgroup_css(cgrp->parent, ss); |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3618 | else |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3619 | css->flags |= CSS_ROOT; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3620 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3621 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3622 | } |
| 3623 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 3624 | /* 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] | 3625 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3626 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3627 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3628 | int ret = 0; |
| 3629 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3630 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3631 | lockdep_assert_held(&cgroup_mutex); |
| 3632 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3633 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3634 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3635 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3636 | css->flags |= CSS_ONLINE; |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3637 | css->cgroup->nr_css++; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3638 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3639 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3640 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3641 | } |
| 3642 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 3643 | /* 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] | 3644 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3645 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3646 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3647 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3648 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3649 | lockdep_assert_held(&cgroup_mutex); |
| 3650 | |
| 3651 | if (!(css->flags & CSS_ONLINE)) |
| 3652 | return; |
| 3653 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 3654 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3655 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3656 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3657 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3658 | css->cgroup->nr_css--; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3659 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3660 | } |
| 3661 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3662 | /** |
| 3663 | * create_css - create a cgroup_subsys_state |
| 3664 | * @cgrp: the cgroup new css will be associated with |
| 3665 | * @ss: the subsys of new css |
| 3666 | * |
| 3667 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
| 3668 | * css is online and installed in @cgrp with all interface files created. |
| 3669 | * Returns 0 on success, -errno on failure. |
| 3670 | */ |
| 3671 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss) |
| 3672 | { |
| 3673 | struct cgroup *parent = cgrp->parent; |
| 3674 | struct cgroup_subsys_state *css; |
| 3675 | int err; |
| 3676 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3677 | lockdep_assert_held(&cgroup_mutex); |
| 3678 | |
| 3679 | css = ss->css_alloc(cgroup_css(parent, ss)); |
| 3680 | if (IS_ERR(css)) |
| 3681 | return PTR_ERR(css); |
| 3682 | |
| 3683 | err = percpu_ref_init(&css->refcnt, css_release); |
| 3684 | if (err) |
| 3685 | goto err_free; |
| 3686 | |
| 3687 | init_css(css, ss, cgrp); |
| 3688 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3689 | err = cgroup_populate_dir(cgrp, 1 << ss->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3690 | if (err) |
| 3691 | goto err_free; |
| 3692 | |
| 3693 | err = online_css(css); |
| 3694 | if (err) |
| 3695 | goto err_free; |
| 3696 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3697 | cgroup_get(cgrp); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3698 | css_get(css->parent); |
| 3699 | |
| 3700 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 3701 | parent->parent) { |
| 3702 | 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", |
| 3703 | current->comm, current->pid, ss->name); |
| 3704 | if (!strcmp(ss->name, "memory")) |
| 3705 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 3706 | ss->warned_broken_hierarchy = true; |
| 3707 | } |
| 3708 | |
| 3709 | return 0; |
| 3710 | |
| 3711 | err_free: |
| 3712 | percpu_ref_cancel_init(&css->refcnt); |
| 3713 | ss->css_free(css); |
| 3714 | return err; |
| 3715 | } |
| 3716 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3717 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3718 | * cgroup_create - create a cgroup |
| 3719 | * @parent: cgroup that will be parent of the new cgroup |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3720 | * @name_str: name of the new cgroup |
| 3721 | * @mode: mode to set on new cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3722 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3723 | static long cgroup_create(struct cgroup *parent, const char *name_str, |
| 3724 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3725 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3726 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 3727 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3728 | struct cgroupfs_root *root = parent->root; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 3729 | int ssid, err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3730 | struct cgroup_subsys *ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3731 | struct kernfs_node *kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3732 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 3733 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3734 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 3735 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3736 | return -ENOMEM; |
| 3737 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3738 | name = cgroup_alloc_name(name_str); |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 3739 | if (!name) { |
| 3740 | err = -ENOMEM; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 3741 | goto err_free_cgrp; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 3742 | } |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 3743 | rcu_assign_pointer(cgrp->name, name); |
| 3744 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3745 | mutex_lock(&cgroup_tree_mutex); |
| 3746 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 3747 | /* |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 3748 | * Only live parents can have children. Note that the liveliness |
| 3749 | * check isn't strictly necessary because cgroup_mkdir() and |
| 3750 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 3751 | * anyway so that locking is contained inside cgroup proper and we |
| 3752 | * don't get nasty surprises if we ever grow another caller. |
| 3753 | */ |
| 3754 | if (!cgroup_lock_live_group(parent)) { |
| 3755 | err = -ENODEV; |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3756 | goto err_unlock_tree; |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | /* |
| 3760 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 3761 | * a half-baked cgroup. |
| 3762 | */ |
| 3763 | cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL); |
| 3764 | if (cgrp->id < 0) { |
| 3765 | err = -ENOMEM; |
| 3766 | goto err_unlock; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 3767 | } |
| 3768 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3769 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3770 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3771 | cgrp->parent = parent; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3772 | cgrp->dummy_css.parent = &parent->dummy_css; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3773 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3774 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 3775 | if (notify_on_release(parent)) |
| 3776 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3777 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3778 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 3779 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3780 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3781 | /* create the directory */ |
| 3782 | kn = kernfs_create_dir(parent->kn, name->name, mode, cgrp); |
| 3783 | if (IS_ERR(kn)) { |
| 3784 | err = PTR_ERR(kn); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 3785 | goto err_free_id; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3786 | } |
| 3787 | cgrp->kn = kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3788 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 3789 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3790 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 3791 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 3792 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 3793 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 3794 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3795 | /* |
| 3796 | * Grab a reference on the root and parent so that they don't get |
| 3797 | * deleted while there are child cgroups. |
| 3798 | */ |
| 3799 | cgroup_get_root(root); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3800 | cgroup_get(parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 3801 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3802 | /* |
| 3803 | * @cgrp is now fully operational. If something fails after this |
| 3804 | * point, it'll be released via the normal destruction path. |
| 3805 | */ |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 3806 | idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 3807 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3808 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3809 | if (err) |
| 3810 | goto err_destroy; |
| 3811 | |
Tejun Heo | 9d403e9 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3812 | /* let's create and online css's */ |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 3813 | for_each_subsys(ss, ssid) { |
| 3814 | if (root->subsys_mask & (1 << ssid)) { |
| 3815 | err = create_css(cgrp, ss); |
| 3816 | if (err) |
| 3817 | goto err_destroy; |
| 3818 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3819 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3820 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3821 | kernfs_activate(kn); |
| 3822 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3823 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3824 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3825 | |
| 3826 | return 0; |
| 3827 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 3828 | err_free_id: |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 3829 | idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 3830 | err_unlock: |
| 3831 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3832 | err_unlock_tree: |
| 3833 | mutex_unlock(&cgroup_tree_mutex); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 3834 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3835 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3836 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3837 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3838 | |
| 3839 | err_destroy: |
| 3840 | cgroup_destroy_locked(cgrp); |
| 3841 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3842 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3843 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3844 | } |
| 3845 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3846 | static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, |
| 3847 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3848 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3849 | struct cgroup *parent = parent_kn->priv; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3850 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3851 | return cgroup_create(parent, name, mode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3852 | } |
| 3853 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3854 | /* |
| 3855 | * This is called when the refcnt of a css is confirmed to be killed. |
| 3856 | * css_tryget() is now guaranteed to fail. |
| 3857 | */ |
| 3858 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3859 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3860 | struct cgroup_subsys_state *css = |
| 3861 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 3862 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3863 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3864 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3865 | mutex_lock(&cgroup_mutex); |
| 3866 | |
| 3867 | /* |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3868 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 3869 | * initate destruction. |
| 3870 | */ |
| 3871 | offline_css(css); |
| 3872 | |
| 3873 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3874 | * If @cgrp is marked dead, it's waiting for refs of all css's to |
| 3875 | * be disabled before proceeding to the second phase of cgroup |
| 3876 | * destruction. If we are the last one, kick it off. |
| 3877 | */ |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3878 | if (!cgrp->nr_css && cgroup_is_dead(cgrp)) |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3879 | cgroup_destroy_css_killed(cgrp); |
| 3880 | |
| 3881 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3882 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3883 | |
| 3884 | /* |
| 3885 | * Put the css refs from kill_css(). Each css holds an extra |
| 3886 | * reference to the cgroup's dentry and cgroup removal proceeds |
| 3887 | * regardless of css refs. On the last put of each css, whenever |
| 3888 | * that may be, the extra dentry ref is put so that dentry |
| 3889 | * destruction happens only after all css's are released. |
| 3890 | */ |
| 3891 | css_put(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3892 | } |
| 3893 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3894 | /* css kill confirmation processing requires process context, bounce */ |
| 3895 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3896 | { |
| 3897 | struct cgroup_subsys_state *css = |
| 3898 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 3899 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3900 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 3901 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | /** |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3905 | * kill_css - destroy a css |
| 3906 | * @css: css to destroy |
| 3907 | * |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3908 | * This function initiates destruction of @css by removing cgroup interface |
| 3909 | * files and putting its base reference. ->css_offline() will be invoked |
| 3910 | * asynchronously once css_tryget() is guaranteed to fail and when the |
| 3911 | * reference count reaches zero, @css will be released. |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3912 | */ |
| 3913 | static void kill_css(struct cgroup_subsys_state *css) |
| 3914 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3915 | /* |
| 3916 | * This must happen before css is disassociated with its cgroup. |
| 3917 | * See seq_css() for details. |
| 3918 | */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3919 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3920 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3921 | /* |
| 3922 | * Killing would put the base ref, but we need to keep it alive |
| 3923 | * until after ->css_offline(). |
| 3924 | */ |
| 3925 | css_get(css); |
| 3926 | |
| 3927 | /* |
| 3928 | * cgroup core guarantees that, by the time ->css_offline() is |
| 3929 | * invoked, no new css reference will be given out via |
| 3930 | * css_tryget(). We can't simply call percpu_ref_kill() and |
| 3931 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 3932 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 3933 | * |
| 3934 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 3935 | * css is confirmed to be seen as killed on all CPUs. |
| 3936 | */ |
| 3937 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3938 | } |
| 3939 | |
| 3940 | /** |
| 3941 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 3942 | * @cgrp: cgroup to be destroyed |
| 3943 | * |
| 3944 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 3945 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 3946 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 3947 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 3948 | * the following two steps. |
| 3949 | * |
| 3950 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 3951 | * userland visible parts and start killing the percpu refcnts of |
| 3952 | * css's. Set up so that the next stage will be kicked off once all |
| 3953 | * the percpu refcnts are confirmed to be killed. |
| 3954 | * |
| 3955 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 3956 | * rest of destruction. Once all cgroup references are gone, the |
| 3957 | * cgroup is RCU-freed. |
| 3958 | * |
| 3959 | * This function implements s1. After this step, @cgrp is gone as far as |
| 3960 | * the userland is concerned and a new cgroup with the same name may be |
| 3961 | * created. As cgroup doesn't care about the names internally, this |
| 3962 | * doesn't cause any problem. |
| 3963 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3964 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 3965 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3966 | { |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 3967 | struct cgroup *child; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3968 | struct cgroup_subsys_state *css; |
| 3969 | struct kernfs_node *kn; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3970 | bool empty; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3971 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3972 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3973 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3974 | lockdep_assert_held(&cgroup_mutex); |
| 3975 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3976 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 3977 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 3978 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3979 | */ |
| 3980 | read_lock(&css_set_lock); |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 3981 | empty = list_empty(&cgrp->cset_links); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3982 | read_unlock(&css_set_lock); |
| 3983 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3984 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 3985 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 3986 | /* |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 3987 | * Make sure there's no live children. We can't test ->children |
| 3988 | * emptiness as dead children linger on it while being destroyed; |
| 3989 | * otherwise, "rmdir parent/child parent" may fail with -EBUSY. |
| 3990 | */ |
| 3991 | empty = true; |
| 3992 | rcu_read_lock(); |
| 3993 | list_for_each_entry_rcu(child, &cgrp->children, sibling) { |
| 3994 | empty = cgroup_is_dead(child); |
| 3995 | if (!empty) |
| 3996 | break; |
| 3997 | } |
| 3998 | rcu_read_unlock(); |
| 3999 | if (!empty) |
| 4000 | return -EBUSY; |
| 4001 | |
| 4002 | /* |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4003 | * Initiate massacre of all css's. cgroup_destroy_css_killed() |
| 4004 | * will be invoked to perform the rest of destruction once the |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4005 | * percpu refs of all css's are confirmed to be killed. This |
| 4006 | * involves removing the subsystem's files, drop cgroup_mutex. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4007 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4008 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4009 | for_each_css(css, ssid, cgrp) |
| 4010 | kill_css(css); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4011 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4012 | |
| 4013 | /* |
| 4014 | * Mark @cgrp dead. This prevents further task migration and child |
| 4015 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4016 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4017 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4018 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4019 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4020 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4021 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4022 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4023 | raw_spin_lock(&release_list_lock); |
| 4024 | if (!list_empty(&cgrp->release_list)) |
| 4025 | list_del_init(&cgrp->release_list); |
| 4026 | raw_spin_unlock(&release_list_lock); |
| 4027 | |
| 4028 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4029 | * If @cgrp has css's attached, the second stage of cgroup |
| 4030 | * destruction is kicked off from css_killed_work_fn() after the |
| 4031 | * refs of all attached css's are killed. If @cgrp doesn't have |
| 4032 | * any css, we kick it off here. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4033 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4034 | if (!cgrp->nr_css) |
| 4035 | cgroup_destroy_css_killed(cgrp); |
| 4036 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4037 | /* remove @cgrp directory along with the base files */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4038 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4039 | |
| 4040 | /* |
| 4041 | * There are two control paths which try to determine cgroup from |
| 4042 | * dentry without going through kernfs - cgroupstats_build() and |
| 4043 | * css_tryget_from_dir(). Those are supported by RCU protecting |
| 4044 | * clearing of cgrp->kn->priv backpointer, which should happen |
| 4045 | * after all files under it have been removed. |
| 4046 | */ |
| 4047 | kn = cgrp->kn; |
| 4048 | kernfs_get(kn); |
| 4049 | |
| 4050 | kernfs_remove(cgrp->kn); |
| 4051 | |
| 4052 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL); |
| 4053 | kernfs_put(kn); |
| 4054 | |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4055 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4056 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4057 | return 0; |
| 4058 | }; |
| 4059 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4060 | /** |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4061 | * cgroup_destroy_css_killed - the second step of cgroup destruction |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4062 | * @work: cgroup->destroy_free_work |
| 4063 | * |
| 4064 | * 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] | 4065 | * destroyed after all css's are offlined and performs the rest of |
| 4066 | * destruction. This is the second step of destruction described in the |
| 4067 | * comment above cgroup_destroy_locked(). |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4068 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4069 | static void cgroup_destroy_css_killed(struct cgroup *cgrp) |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4070 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4071 | struct cgroup *parent = cgrp->parent; |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4072 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4073 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4074 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4075 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4076 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4077 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4078 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4079 | cgroup_put(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4080 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4081 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4082 | check_for_release(parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4085 | static int cgroup_rmdir(struct kernfs_node *kn) |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4086 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4087 | struct cgroup *cgrp = kn->priv; |
| 4088 | int ret = 0; |
| 4089 | |
| 4090 | /* |
| 4091 | * This is self-destruction but @kn can't be removed while this |
| 4092 | * callback is in progress. Let's break active protection. Once |
| 4093 | * the protection is broken, @cgrp can be destroyed at any point. |
| 4094 | * Pin it so that it stays accessible. |
| 4095 | */ |
| 4096 | cgroup_get(cgrp); |
| 4097 | kernfs_break_active_protection(kn); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4098 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4099 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4100 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4101 | |
| 4102 | /* |
| 4103 | * @cgrp might already have been destroyed while we're trying to |
| 4104 | * grab the mutexes. |
| 4105 | */ |
| 4106 | if (!cgroup_is_dead(cgrp)) |
| 4107 | ret = cgroup_destroy_locked(cgrp); |
| 4108 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4109 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4110 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4111 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4112 | kernfs_unbreak_active_protection(kn); |
| 4113 | cgroup_put(cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4114 | return ret; |
| 4115 | } |
| 4116 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4117 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
| 4118 | .remount_fs = cgroup_remount, |
| 4119 | .show_options = cgroup_show_options, |
| 4120 | .mkdir = cgroup_mkdir, |
| 4121 | .rmdir = cgroup_rmdir, |
| 4122 | .rename = cgroup_rename, |
| 4123 | }; |
| 4124 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4125 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4126 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4127 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4128 | |
| 4129 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4130 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4131 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4132 | mutex_lock(&cgroup_mutex); |
| 4133 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4134 | INIT_LIST_HEAD(&ss->cfts); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4135 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4136 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4137 | ss->root = &cgroup_dummy_root; |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4138 | css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4139 | /* We don't handle early failures gracefully */ |
| 4140 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4141 | init_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4142 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4143 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4144 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4145 | * newly registered, all tasks and hence the |
| 4146 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4147 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4148 | |
| 4149 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4150 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4151 | /* At system boot, before all subsystems have been |
| 4152 | * registered, no tasks have been forked, so we don't |
| 4153 | * need to invoke fork callbacks here. */ |
| 4154 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4155 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4156 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4157 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4158 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4159 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4163 | * cgroup_init_early - cgroup initialization at system boot |
| 4164 | * |
| 4165 | * Initialize cgroups at system boot, and initialize any |
| 4166 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4167 | */ |
| 4168 | int __init cgroup_init_early(void) |
| 4169 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4170 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4171 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4172 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4173 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4174 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4175 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4176 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4177 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4178 | init_cgroup_root(&cgroup_dummy_root); |
| 4179 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4180 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4181 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4182 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4183 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4184 | 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] | 4185 | 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] | 4186 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4187 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4188 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4189 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n", |
| 4190 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4191 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4192 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 4193 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
| 4194 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4195 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4196 | ss->name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4197 | |
| 4198 | if (ss->early_init) |
| 4199 | cgroup_init_subsys(ss); |
| 4200 | } |
| 4201 | return 0; |
| 4202 | } |
| 4203 | |
| 4204 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4205 | * cgroup_init - cgroup initialization |
| 4206 | * |
| 4207 | * Register cgroup filesystem and /proc file, and initialize |
| 4208 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4209 | */ |
| 4210 | int __init cgroup_init(void) |
| 4211 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4212 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4213 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4214 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4215 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4216 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4217 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4218 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4219 | if (!ss->early_init) |
| 4220 | cgroup_init_subsys(ss); |
Tejun Heo | de00ffa | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4221 | |
| 4222 | /* |
| 4223 | * cftype registration needs kmalloc and can't be done |
| 4224 | * during early_init. Register base cftypes separately. |
| 4225 | */ |
| 4226 | if (ss->base_cftypes) |
| 4227 | WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4228 | } |
| 4229 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4230 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4231 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4232 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4233 | /* Add init_css_set to the hash table */ |
| 4234 | key = css_set_hash(init_css_set.subsys); |
| 4235 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4236 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4237 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4238 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4239 | err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top, |
| 4240 | 0, 1, GFP_KERNEL); |
| 4241 | BUG_ON(err < 0); |
| 4242 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4243 | mutex_unlock(&cgroup_mutex); |
| 4244 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4245 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4246 | if (!cgroup_kobj) |
| 4247 | return -ENOMEM; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4248 | |
| 4249 | err = register_filesystem(&cgroup_fs_type); |
| 4250 | if (err < 0) { |
| 4251 | kobject_put(cgroup_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4252 | return err; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4253 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4254 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4255 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4256 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4257 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4258 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4259 | static int __init cgroup_wq_init(void) |
| 4260 | { |
| 4261 | /* |
| 4262 | * There isn't much point in executing destruction path in |
| 4263 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Hugh Dickins | ab3f5fa | 2014-02-06 15:56:01 -0800 | [diff] [blame] | 4264 | * |
| 4265 | * XXX: Must be ordered to make sure parent is offlined after |
| 4266 | * children. The ordering requirement is for memcg where a |
| 4267 | * parent's offline may wait for a child's leading to deadlock. In |
| 4268 | * the long term, this should be fixed from memcg side. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4269 | * |
| 4270 | * We would prefer to do this in cgroup_init() above, but that |
| 4271 | * is called before init_workqueues(): so leave this until after. |
| 4272 | */ |
Hugh Dickins | ab3f5fa | 2014-02-06 15:56:01 -0800 | [diff] [blame] | 4273 | cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4274 | BUG_ON(!cgroup_destroy_wq); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 4275 | |
| 4276 | /* |
| 4277 | * Used to destroy pidlists and separate to serve as flush domain. |
| 4278 | * Cap @max_active to 1 too. |
| 4279 | */ |
| 4280 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 4281 | 0, 1); |
| 4282 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 4283 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4284 | return 0; |
| 4285 | } |
| 4286 | core_initcall(cgroup_wq_init); |
| 4287 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4288 | /* |
| 4289 | * proc_cgroup_show() |
| 4290 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4291 | * - Used for /proc/<pid>/cgroup. |
| 4292 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4293 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4294 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4295 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4296 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4297 | * cgroup to top_cgroup. |
| 4298 | */ |
| 4299 | |
| 4300 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4301 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4302 | { |
| 4303 | struct pid *pid; |
| 4304 | struct task_struct *tsk; |
| 4305 | char *buf; |
| 4306 | int retval; |
| 4307 | struct cgroupfs_root *root; |
| 4308 | |
| 4309 | retval = -ENOMEM; |
| 4310 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4311 | if (!buf) |
| 4312 | goto out; |
| 4313 | |
| 4314 | retval = -ESRCH; |
| 4315 | pid = m->private; |
| 4316 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4317 | if (!tsk) |
| 4318 | goto out_free; |
| 4319 | |
| 4320 | retval = 0; |
| 4321 | |
| 4322 | mutex_lock(&cgroup_mutex); |
| 4323 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4324 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4325 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4326 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4327 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4328 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4329 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4330 | for_each_subsys(ss, ssid) |
| 4331 | if (root->subsys_mask & (1 << ssid)) |
| 4332 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4333 | if (strlen(root->name)) |
| 4334 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4335 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4336 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4337 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4338 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4339 | if (retval < 0) |
| 4340 | goto out_unlock; |
| 4341 | seq_puts(m, buf); |
| 4342 | seq_putc(m, '\n'); |
| 4343 | } |
| 4344 | |
| 4345 | out_unlock: |
| 4346 | mutex_unlock(&cgroup_mutex); |
| 4347 | put_task_struct(tsk); |
| 4348 | out_free: |
| 4349 | kfree(buf); |
| 4350 | out: |
| 4351 | return retval; |
| 4352 | } |
| 4353 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4354 | /* Display information about each subsystem and each hierarchy */ |
| 4355 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4356 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4357 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4358 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4359 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4360 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4361 | /* |
| 4362 | * ideally we don't want subsystems moving around while we do this. |
| 4363 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4364 | * subsys/hierarchy state. |
| 4365 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4366 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4367 | |
| 4368 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4369 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4370 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4371 | ss->root->number_of_cgroups, !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4372 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4373 | mutex_unlock(&cgroup_mutex); |
| 4374 | return 0; |
| 4375 | } |
| 4376 | |
| 4377 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4378 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4379 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4380 | } |
| 4381 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4382 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4383 | .open = cgroupstats_open, |
| 4384 | .read = seq_read, |
| 4385 | .llseek = seq_lseek, |
| 4386 | .release = single_release, |
| 4387 | }; |
| 4388 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4389 | /** |
| 4390 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4391 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4392 | * |
| 4393 | * Description: A task inherits its parent's cgroup at fork(). |
| 4394 | * |
| 4395 | * A pointer to the shared css_set was automatically copied in |
| 4396 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4397 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 4398 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 4399 | * have already changed current->cgroups, allowing the previously |
| 4400 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4401 | * |
| 4402 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 4403 | * task, and the passed argument 'child' points to the child task. |
| 4404 | */ |
| 4405 | void cgroup_fork(struct task_struct *child) |
| 4406 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4407 | task_lock(current); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4408 | get_css_set(task_css_set(current)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4409 | child->cgroups = current->cgroups; |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4410 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4411 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4415 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4416 | * @child: the task in question |
| 4417 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4418 | * Adds the task to the list running through its css_set if necessary and |
| 4419 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4420 | * 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] | 4421 | * 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] | 4422 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4423 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4424 | void cgroup_post_fork(struct task_struct *child) |
| 4425 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4426 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4427 | int i; |
| 4428 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4429 | /* |
| 4430 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 4431 | * under the tasklist_lock and we read it here after we added the child |
| 4432 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 4433 | * yet in the tasklist when we walked through it from |
| 4434 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 4435 | * should be visible now due to the paired locking and barriers implied |
| 4436 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 4437 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 4438 | * lock on fork. |
| 4439 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4440 | if (use_task_css_set_links) { |
| 4441 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4442 | task_lock(child); |
| 4443 | if (list_empty(&child->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4444 | list_add(&child->cg_list, &task_css_set(child)->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4445 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4446 | write_unlock(&css_set_lock); |
| 4447 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4448 | |
| 4449 | /* |
| 4450 | * Call ss->fork(). This must happen after @child is linked on |
| 4451 | * css_set; otherwise, @child might change state between ->fork() |
| 4452 | * and addition to css_set. |
| 4453 | */ |
| 4454 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4455 | for_each_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4456 | if (ss->fork) |
| 4457 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4458 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4459 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4460 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4461 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4462 | * cgroup_exit - detach cgroup from exiting task |
| 4463 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4464 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4465 | * |
| 4466 | * Description: Detach cgroup from @tsk and release it. |
| 4467 | * |
| 4468 | * Note that cgroups marked notify_on_release force every task in |
| 4469 | * them to take the global cgroup_mutex mutex when exiting. |
| 4470 | * This could impact scaling on very large systems. Be reluctant to |
| 4471 | * use notify_on_release cgroups where very high task exit scaling |
| 4472 | * is required on large systems. |
| 4473 | * |
| 4474 | * the_top_cgroup_hack: |
| 4475 | * |
| 4476 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 4477 | * |
| 4478 | * We call cgroup_exit() while the task is still competent to |
| 4479 | * handle notify_on_release(), then leave the task attached to the |
| 4480 | * root cgroup in each hierarchy for the remainder of its exit. |
| 4481 | * |
| 4482 | * To do this properly, we would increment the reference count on |
| 4483 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 4484 | * code we would add a second cgroup function call, to drop that |
| 4485 | * reference. This would just create an unnecessary hot spot on |
| 4486 | * the top_cgroup reference count, to no avail. |
| 4487 | * |
| 4488 | * Normally, holding a reference to a cgroup without bumping its |
| 4489 | * count is unsafe. The cgroup could go away, or someone could |
| 4490 | * attach us to a different cgroup, decrementing the count on |
| 4491 | * the first cgroup that we never incremented. But in this case, |
| 4492 | * 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] | 4493 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 4494 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4495 | */ |
| 4496 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 4497 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4498 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4499 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4500 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4501 | |
| 4502 | /* |
| 4503 | * Unlink from the css_set task list if necessary. |
| 4504 | * Optimistically check cg_list before taking |
| 4505 | * css_set_lock |
| 4506 | */ |
| 4507 | if (!list_empty(&tsk->cg_list)) { |
| 4508 | write_lock(&css_set_lock); |
| 4509 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4510 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4511 | write_unlock(&css_set_lock); |
| 4512 | } |
| 4513 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4514 | /* Reassign the task to the init_css_set. */ |
| 4515 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4516 | cset = task_css_set(tsk); |
| 4517 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4518 | |
| 4519 | if (run_callbacks && need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4520 | /* see cgroup_post_fork() for details */ |
| 4521 | for_each_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4522 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4523 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 4524 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4525 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4526 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4527 | } |
| 4528 | } |
| 4529 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4530 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4531 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4532 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4533 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4534 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4535 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4536 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4537 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4538 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4539 | /* |
| 4540 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4541 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4542 | * it now |
| 4543 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4544 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4545 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4546 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4547 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4548 | list_empty(&cgrp->release_list)) { |
| 4549 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4550 | need_schedule_work = 1; |
| 4551 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4552 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4553 | if (need_schedule_work) |
| 4554 | schedule_work(&release_agent_work); |
| 4555 | } |
| 4556 | } |
| 4557 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4558 | /* |
| 4559 | * Notify userspace when a cgroup is released, by running the |
| 4560 | * configured release agent with the name of the cgroup (path |
| 4561 | * relative to the root of cgroup file system) as the argument. |
| 4562 | * |
| 4563 | * Most likely, this user command will try to rmdir this cgroup. |
| 4564 | * |
| 4565 | * This races with the possibility that some other task will be |
| 4566 | * attached to this cgroup before it is removed, or that some other |
| 4567 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 4568 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 4569 | * unused, and this cgroup will be reprieved from its death sentence, |
| 4570 | * to continue to serve a useful existence. Next time it's released, |
| 4571 | * we will get notified again, if it still has 'notify_on_release' set. |
| 4572 | * |
| 4573 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 4574 | * means only wait until the task is successfully execve()'d. The |
| 4575 | * separate release agent task is forked by call_usermodehelper(), |
| 4576 | * then control in this thread returns here, without waiting for the |
| 4577 | * release agent task. We don't bother to wait because the caller of |
| 4578 | * this routine has no use for the exit status of the release agent |
| 4579 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4580 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4581 | static void cgroup_release_agent(struct work_struct *work) |
| 4582 | { |
| 4583 | BUG_ON(work != &release_agent_work); |
| 4584 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4585 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4586 | while (!list_empty(&release_list)) { |
| 4587 | char *argv[3], *envp[3]; |
| 4588 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4589 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4590 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4591 | struct cgroup, |
| 4592 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4593 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4594 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4595 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4596 | if (!pathbuf) |
| 4597 | goto continue_free; |
| 4598 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 4599 | goto continue_free; |
| 4600 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 4601 | if (!agentbuf) |
| 4602 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4603 | |
| 4604 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4605 | argv[i++] = agentbuf; |
| 4606 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4607 | argv[i] = NULL; |
| 4608 | |
| 4609 | i = 0; |
| 4610 | /* minimal command environment */ |
| 4611 | envp[i++] = "HOME=/"; |
| 4612 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 4613 | envp[i] = NULL; |
| 4614 | |
| 4615 | /* Drop the lock while we invoke the usermode helper, |
| 4616 | * since the exec could involve hitting disk and hence |
| 4617 | * be a slow process */ |
| 4618 | mutex_unlock(&cgroup_mutex); |
| 4619 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4620 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4621 | continue_free: |
| 4622 | kfree(pathbuf); |
| 4623 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4624 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4625 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4626 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4627 | mutex_unlock(&cgroup_mutex); |
| 4628 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4629 | |
| 4630 | static int __init cgroup_disable(char *str) |
| 4631 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4632 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4633 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4634 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4635 | |
| 4636 | while ((token = strsep(&str, ",")) != NULL) { |
| 4637 | if (!*token) |
| 4638 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4639 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4640 | for_each_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4641 | if (!strcmp(token, ss->name)) { |
| 4642 | ss->disabled = 1; |
| 4643 | printk(KERN_INFO "Disabling %s control group" |
| 4644 | " subsystem\n", ss->name); |
| 4645 | break; |
| 4646 | } |
| 4647 | } |
| 4648 | } |
| 4649 | return 1; |
| 4650 | } |
| 4651 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4652 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4653 | /** |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4654 | * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4655 | * @dentry: directory dentry of interest |
| 4656 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4657 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4658 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 4659 | * to get the corresponding css and return it. If such css doesn't exist |
| 4660 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4661 | */ |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4662 | struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry, |
| 4663 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4664 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4665 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
| 4666 | struct cgroup_subsys_state *css = NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4667 | struct cgroup *cgrp; |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4668 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4669 | /* is @dentry a cgroup dir? */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4670 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 4671 | kernfs_type(kn) != KERNFS_DIR) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4672 | return ERR_PTR(-EBADF); |
| 4673 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4674 | rcu_read_lock(); |
| 4675 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4676 | /* |
| 4677 | * This path doesn't originate from kernfs and @kn could already |
| 4678 | * have been or be removed at any point. @kn->priv is RCU |
| 4679 | * protected for this access. See destroy_locked() for details. |
| 4680 | */ |
| 4681 | cgrp = rcu_dereference(kn->priv); |
| 4682 | if (cgrp) |
| 4683 | css = cgroup_css(cgrp, ss); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4684 | |
| 4685 | if (!css || !css_tryget(css)) |
| 4686 | css = ERR_PTR(-ENOENT); |
| 4687 | |
| 4688 | rcu_read_unlock(); |
| 4689 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4690 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4691 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 4692 | /** |
| 4693 | * css_from_id - lookup css by id |
| 4694 | * @id: the cgroup id |
| 4695 | * @ss: cgroup subsys to be looked into |
| 4696 | * |
| 4697 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 4698 | * Should be called under rcu_read_lock(). |
| 4699 | */ |
| 4700 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 4701 | { |
| 4702 | struct cgroup *cgrp; |
| 4703 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4704 | cgroup_assert_mutexes_or_rcu_locked(); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 4705 | |
| 4706 | cgrp = idr_find(&ss->root->cgroup_idr, id); |
| 4707 | if (cgrp) |
Tejun Heo | d162596 | 2013-08-27 14:27:23 -0400 | [diff] [blame] | 4708 | return cgroup_css(cgrp, ss); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 4709 | return NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4710 | } |
| 4711 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4712 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4713 | static struct cgroup_subsys_state * |
| 4714 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4715 | { |
| 4716 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 4717 | |
| 4718 | if (!css) |
| 4719 | return ERR_PTR(-ENOMEM); |
| 4720 | |
| 4721 | return css; |
| 4722 | } |
| 4723 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4724 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4725 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4726 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4727 | } |
| 4728 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4729 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 4730 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4731 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4732 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4733 | } |
| 4734 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4735 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 4736 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4737 | { |
| 4738 | return (u64)(unsigned long)current->cgroups; |
| 4739 | } |
| 4740 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4741 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 4742 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4743 | { |
| 4744 | u64 count; |
| 4745 | |
| 4746 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4747 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4748 | rcu_read_unlock(); |
| 4749 | return count; |
| 4750 | } |
| 4751 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4752 | static int current_css_set_cg_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4753 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4754 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4755 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4756 | |
| 4757 | read_lock(&css_set_lock); |
| 4758 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4759 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4760 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4761 | struct cgroup *c = link->cgrp; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4762 | const char *name = "?"; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4763 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4764 | if (c != cgroup_dummy_top) |
| 4765 | name = cgroup_name(c); |
| 4766 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4767 | seq_printf(seq, "Root %d group %s\n", |
| 4768 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4769 | } |
| 4770 | rcu_read_unlock(); |
| 4771 | read_unlock(&css_set_lock); |
| 4772 | return 0; |
| 4773 | } |
| 4774 | |
| 4775 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4776 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4777 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4778 | struct cgroup_subsys_state *css = seq_css(seq); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4779 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4780 | |
| 4781 | read_lock(&css_set_lock); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4782 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4783 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4784 | struct task_struct *task; |
| 4785 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4786 | seq_printf(seq, "css_set %p\n", cset); |
| 4787 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4788 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 4789 | seq_puts(seq, " ...\n"); |
| 4790 | break; |
| 4791 | } else { |
| 4792 | seq_printf(seq, " task %d\n", |
| 4793 | task_pid_vnr(task)); |
| 4794 | } |
| 4795 | } |
| 4796 | } |
| 4797 | read_unlock(&css_set_lock); |
| 4798 | return 0; |
| 4799 | } |
| 4800 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4801 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4802 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4803 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | static struct cftype debug_files[] = { |
| 4807 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4808 | .name = "taskcount", |
| 4809 | .read_u64 = debug_taskcount_read, |
| 4810 | }, |
| 4811 | |
| 4812 | { |
| 4813 | .name = "current_css_set", |
| 4814 | .read_u64 = current_css_set_read, |
| 4815 | }, |
| 4816 | |
| 4817 | { |
| 4818 | .name = "current_css_set_refcount", |
| 4819 | .read_u64 = current_css_set_refcount_read, |
| 4820 | }, |
| 4821 | |
| 4822 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4823 | .name = "current_css_set_cg_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4824 | .seq_show = current_css_set_cg_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4825 | }, |
| 4826 | |
| 4827 | { |
| 4828 | .name = "cgroup_css_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4829 | .seq_show = cgroup_css_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4830 | }, |
| 4831 | |
| 4832 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4833 | .name = "releasable", |
| 4834 | .read_u64 = releasable_read, |
| 4835 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4836 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4837 | { } /* terminate */ |
| 4838 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4839 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4840 | struct cgroup_subsys debug_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4841 | .css_alloc = debug_css_alloc, |
| 4842 | .css_free = debug_css_free, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4843 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4844 | }; |
| 4845 | #endif /* CONFIG_CGROUP_DEBUG */ |