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> |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 45 | #include <linux/rwsem.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 46 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 47 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 48 | #include <linux/kmod.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 49 | #include <linux/delayacct.h> |
| 50 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 51 | #include <linux/hashtable.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 52 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 53 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 54 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 55 | #include <linux/kthread.h> |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 56 | #include <linux/delay.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 57 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 58 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 59 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 60 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 61 | * pidlists linger the following amount before being destroyed. The goal |
| 62 | * is avoiding frequent destruction in the middle of consecutive read calls |
| 63 | * Expiring in the middle is a performance problem not a correctness one. |
| 64 | * 1 sec should be enough. |
| 65 | */ |
| 66 | #define CGROUP_PIDLIST_DESTROY_DELAY HZ |
| 67 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 68 | #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ |
| 69 | MAX_CFTYPE_NAME + 2) |
| 70 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 71 | /* |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 72 | * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file |
| 73 | * creation/removal and hierarchy changing operations including cgroup |
| 74 | * creation, removal, css association and controller rebinding. This outer |
| 75 | * lock is needed mainly to resolve the circular dependency between kernfs |
| 76 | * active ref and cgroup_mutex. cgroup_tree_mutex nests above both. |
| 77 | */ |
| 78 | static DEFINE_MUTEX(cgroup_tree_mutex); |
| 79 | |
| 80 | /* |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 81 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 82 | * hierarchy must be performed while holding it. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 83 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 84 | #ifdef CONFIG_PROVE_RCU |
| 85 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 86 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 87 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 88 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 89 | #endif |
| 90 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 91 | /* |
| 92 | * Protects cgroup_subsys->release_agent_path. Modifying it also requires |
| 93 | * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock. |
| 94 | */ |
| 95 | static DEFINE_SPINLOCK(release_agent_path_lock); |
| 96 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 97 | #define cgroup_assert_mutexes_or_rcu_locked() \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 98 | rcu_lockdep_assert(rcu_read_lock_held() || \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 99 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 100 | lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 101 | "cgroup_[tree_]mutex or RCU read lock required"); |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 102 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 103 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 104 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 105 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 106 | * destruction work items don't end up filling up max_active of system_wq |
| 107 | * which may lead to deadlock. |
| 108 | */ |
| 109 | static struct workqueue_struct *cgroup_destroy_wq; |
| 110 | |
| 111 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 112 | * pidlist destructions need to be flushed on cgroup destruction. Use a |
| 113 | * separate workqueue as flush domain. |
| 114 | */ |
| 115 | static struct workqueue_struct *cgroup_pidlist_destroy_wq; |
| 116 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 117 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 118 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 119 | static struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 120 | #include <linux/cgroup_subsys.h> |
| 121 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 122 | #undef SUBSYS |
| 123 | |
| 124 | /* array of cgroup subsystem names */ |
| 125 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 126 | static const char *cgroup_subsys_name[] = { |
| 127 | #include <linux/cgroup_subsys.h> |
| 128 | }; |
| 129 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 130 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 131 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 132 | * The dummy hierarchy, reserved for the subsystems that are otherwise |
| 133 | * unattached - it never has more than a single cgroup, and all tasks are |
| 134 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 135 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 136 | static struct cgroupfs_root cgroup_dummy_root; |
| 137 | |
| 138 | /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ |
| 139 | static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 140 | |
| 141 | /* The list of hierarchy roots */ |
| 142 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 143 | static LIST_HEAD(cgroup_roots); |
| 144 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 145 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 146 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 147 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 148 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 149 | /* |
| 150 | * Assign a monotonically increasing serial number to cgroups. It |
| 151 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 152 | * numbers. Also, as cgroups are always appended to the parent's |
| 153 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 154 | * the ascending serial number order on the list. Protected by |
| 155 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 156 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 157 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 158 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 159 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 160 | * check for fork/exit handlers to call. This avoids us having to do |
| 161 | * extra work in the fork/exit path if none of the subsystems need to |
| 162 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 163 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 164 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 165 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 166 | static struct cftype cgroup_base_files[]; |
| 167 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 168 | static void cgroup_put(struct cgroup *cgrp); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 169 | static int rebind_subsystems(struct cgroupfs_root *root, |
| 170 | unsigned long added_mask, unsigned removed_mask); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 171 | static void cgroup_destroy_css_killed(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 172 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 173 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 174 | bool is_add); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 175 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 176 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 177 | /** |
| 178 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 179 | * @cgrp: the cgroup of interest |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 180 | * @ss: the subsystem of interest (%NULL returns the dummy_css) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 181 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 182 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 183 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 184 | * the caller is responsible for pinning the returned css if it wants to |
| 185 | * keep accessing it outside the said locks. This function may return |
| 186 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 187 | */ |
| 188 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 189 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 190 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 191 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 192 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 193 | lockdep_is_held(&cgroup_tree_mutex) || |
| 194 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 195 | else |
| 196 | return &cgrp->dummy_css; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 197 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 198 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 199 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 200 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 201 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 202 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 205 | struct cgroup_subsys_state *seq_css(struct seq_file *seq) |
| 206 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 207 | struct kernfs_open_file *of = seq->private; |
| 208 | struct cgroup *cgrp = of->kn->parent->priv; |
| 209 | struct cftype *cft = seq_cft(seq); |
| 210 | |
| 211 | /* |
| 212 | * This is open and unprotected implementation of cgroup_css(). |
| 213 | * seq_css() is only called from a kernfs file operation which has |
| 214 | * an active reference on the file. Because all the subsystem |
| 215 | * files are drained before a css is disassociated with a cgroup, |
| 216 | * the matching css from the cgroup's subsys table is guaranteed to |
| 217 | * be and stay valid until the enclosing operation is complete. |
| 218 | */ |
| 219 | if (cft->ss) |
| 220 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 221 | else |
| 222 | return &cgrp->dummy_css; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 223 | } |
| 224 | EXPORT_SYMBOL_GPL(seq_css); |
| 225 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 226 | /** |
| 227 | * cgroup_is_descendant - test ancestry |
| 228 | * @cgrp: the cgroup to be tested |
| 229 | * @ancestor: possible ancestor of @cgrp |
| 230 | * |
| 231 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 232 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 233 | * and @ancestor are accessible. |
| 234 | */ |
| 235 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 236 | { |
| 237 | while (cgrp) { |
| 238 | if (cgrp == ancestor) |
| 239 | return true; |
| 240 | cgrp = cgrp->parent; |
| 241 | } |
| 242 | return false; |
| 243 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 244 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 245 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 246 | { |
| 247 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 248 | (1 << CGRP_RELEASABLE) | |
| 249 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 250 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 253 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 254 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 255 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 258 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 259 | * for_each_css - iterate all css's of a cgroup |
| 260 | * @css: the iteration cursor |
| 261 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 262 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 263 | * |
| 264 | * Should be called under cgroup_mutex. |
| 265 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 266 | #define for_each_css(css, ssid, cgrp) \ |
| 267 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 268 | if (!((css) = rcu_dereference_check( \ |
| 269 | (cgrp)->subsys[(ssid)], \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 270 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 271 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 272 | else |
| 273 | |
| 274 | /** |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 275 | * for_each_subsys - iterate all enabled cgroup subsystems |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 276 | * @ss: the iteration cursor |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 277 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 278 | */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 279 | #define for_each_subsys(ss, ssid) \ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 280 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \ |
| 281 | (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 282 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 283 | /* iterate across the active hierarchies */ |
| 284 | #define for_each_active_root(root) \ |
| 285 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 286 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 287 | /** |
| 288 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 289 | * @cgrp: the cgroup to be checked for liveness |
| 290 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 291 | * On success, returns true; the mutex should be later unlocked. On |
| 292 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 293 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 294 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 295 | { |
| 296 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 297 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 298 | mutex_unlock(&cgroup_mutex); |
| 299 | return false; |
| 300 | } |
| 301 | return true; |
| 302 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 303 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 304 | /* the list of cgroups eligible for automatic release. Protected by |
| 305 | * release_list_lock */ |
| 306 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 307 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 308 | static void cgroup_release_agent(struct work_struct *work); |
| 309 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 310 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 311 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 312 | /* |
| 313 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 314 | * belong to different cgroups on different hierarchies. In the other |
| 315 | * direction, a css_set is naturally associated with multiple cgroups. |
| 316 | * This M:N relationship is represented by the following link structure |
| 317 | * which exists for each association and allows traversing the associations |
| 318 | * from both sides. |
| 319 | */ |
| 320 | struct cgrp_cset_link { |
| 321 | /* the cgroup and css_set this link associates */ |
| 322 | struct cgroup *cgrp; |
| 323 | struct css_set *cset; |
| 324 | |
| 325 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 326 | struct list_head cset_link; |
| 327 | |
| 328 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 329 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | /* The default css_set - used by init and its children prior to any |
| 333 | * hierarchies being mounted. It contains a pointer to the root state |
| 334 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 335 | * reference-counted, to improve performance when child cgroups |
| 336 | * haven't been created. |
| 337 | */ |
| 338 | |
| 339 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 340 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 341 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 342 | /* |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 343 | * css_set_rwsem protects the list of css_set objects, and the chain of |
| 344 | * tasks off each css_set. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 345 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 346 | static DECLARE_RWSEM(css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 347 | static int css_set_count; |
| 348 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 349 | /* |
| 350 | * hash table for cgroup groups. This improves the performance to find |
| 351 | * an existing css_set. This hash doesn't (currently) take into |
| 352 | * account cgroups in empty hierarchies. |
| 353 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 354 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 355 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 356 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 357 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 358 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 359 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 360 | struct cgroup_subsys *ss; |
| 361 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 362 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 363 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 364 | key += (unsigned long)css[i]; |
| 365 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 366 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 367 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 370 | static void put_css_set_locked(struct css_set *cset, bool taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 371 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 372 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 373 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 374 | lockdep_assert_held(&css_set_rwsem); |
| 375 | |
| 376 | if (!atomic_dec_and_test(&cset->refcount)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 377 | return; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 378 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 379 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 380 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 381 | css_set_count--; |
| 382 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 383 | 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] | 384 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 385 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 386 | list_del(&link->cset_link); |
| 387 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 388 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 389 | /* @cgrp can't go away while we're holding css_set_rwsem */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 390 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 391 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 392 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 393 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 394 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 395 | |
| 396 | kfree(link); |
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 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 399 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 402 | static void put_css_set(struct css_set *cset, bool taskexit) |
| 403 | { |
| 404 | /* |
| 405 | * Ensure that the refcount doesn't hit zero while any readers |
| 406 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 407 | * rwlock |
| 408 | */ |
| 409 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
| 410 | return; |
| 411 | |
| 412 | down_write(&css_set_rwsem); |
| 413 | put_css_set_locked(cset, taskexit); |
| 414 | up_write(&css_set_rwsem); |
| 415 | } |
| 416 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 417 | /* |
| 418 | * refcounted get/put for css_set objects |
| 419 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 420 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 421 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 422 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 425 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 426 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 427 | * @cset: candidate css_set being tested |
| 428 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 429 | * @new_cgrp: cgroup that's being entered by the task |
| 430 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 431 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 432 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 433 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 434 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 435 | static bool compare_css_sets(struct css_set *cset, |
| 436 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 437 | struct cgroup *new_cgrp, |
| 438 | struct cgroup_subsys_state *template[]) |
| 439 | { |
| 440 | struct list_head *l1, *l2; |
| 441 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 442 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 443 | /* Not all subsystems matched */ |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Compare cgroup pointers in order to distinguish between |
| 449 | * different cgroups in heirarchies with no subsystems. We |
| 450 | * could get by with just this check alone (and skip the |
| 451 | * memcmp above) but on most setups the memcmp check will |
| 452 | * avoid the need for this more expensive check on almost all |
| 453 | * candidates. |
| 454 | */ |
| 455 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 456 | l1 = &cset->cgrp_links; |
| 457 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 458 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 459 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 460 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 461 | |
| 462 | l1 = l1->next; |
| 463 | l2 = l2->next; |
| 464 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 465 | if (l1 == &cset->cgrp_links) { |
| 466 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 467 | break; |
| 468 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 469 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 470 | } |
| 471 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 472 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 473 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 474 | cgrp1 = link1->cgrp; |
| 475 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 476 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 477 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * If this hierarchy is the hierarchy of the cgroup |
| 481 | * that's changing, then we need to check that this |
| 482 | * css_set points to the new cgroup; if it's any other |
| 483 | * hierarchy, then this css_set should point to the |
| 484 | * same cgroup as the old css_set. |
| 485 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 486 | if (cgrp1->root == new_cgrp->root) { |
| 487 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 488 | return false; |
| 489 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 490 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | } |
| 494 | return true; |
| 495 | } |
| 496 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 497 | /** |
| 498 | * find_existing_css_set - init css array and find the matching css_set |
| 499 | * @old_cset: the css_set that we're using before the cgroup transition |
| 500 | * @cgrp: the cgroup that we're moving into |
| 501 | * @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] | 502 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 503 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 504 | struct cgroup *cgrp, |
| 505 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 506 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 507 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 508 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 509 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 510 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 511 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 512 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 513 | /* |
| 514 | * Build the set of subsystem state objects that we want to see in the |
| 515 | * new css_set. while subsystems can change globally, the entries here |
| 516 | * won't change, so no need for locking. |
| 517 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 518 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 519 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 520 | /* Subsystem is in this hierarchy. So we want |
| 521 | * the subsystem state from the new |
| 522 | * cgroup */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 523 | template[i] = cgroup_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 524 | } else { |
| 525 | /* Subsystem is not in this hierarchy, so we |
| 526 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 527 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 531 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 532 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 533 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 534 | continue; |
| 535 | |
| 536 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 537 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 538 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 539 | |
| 540 | /* No existing cgroup group matched */ |
| 541 | return NULL; |
| 542 | } |
| 543 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 544 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 545 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 546 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 547 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 548 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 549 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 550 | kfree(link); |
| 551 | } |
| 552 | } |
| 553 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 554 | /** |
| 555 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 556 | * @count: the number of links to allocate |
| 557 | * @tmp_links: list_head the allocated links are put on |
| 558 | * |
| 559 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 560 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 561 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 562 | 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] | 563 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 564 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 565 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 566 | |
| 567 | INIT_LIST_HEAD(tmp_links); |
| 568 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 569 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 570 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 571 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 572 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 573 | return -ENOMEM; |
| 574 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 575 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 576 | } |
| 577 | return 0; |
| 578 | } |
| 579 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 580 | /** |
| 581 | * 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] | 582 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 583 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 584 | * @cgrp: the destination cgroup |
| 585 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 586 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 587 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 588 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 589 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 590 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 591 | BUG_ON(list_empty(tmp_links)); |
| 592 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 593 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 594 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 595 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 596 | /* |
| 597 | * Always add links to the tail of the list so that the list |
| 598 | * is sorted by order of hierarchy creation |
| 599 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 600 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 603 | /** |
| 604 | * find_css_set - return a new css_set with one cgroup updated |
| 605 | * @old_cset: the baseline css_set |
| 606 | * @cgrp: the cgroup to be updated |
| 607 | * |
| 608 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 609 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 610 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 611 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 612 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 613 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 614 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 615 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 616 | struct list_head tmp_links; |
| 617 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 618 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 619 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 620 | lockdep_assert_held(&cgroup_mutex); |
| 621 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 622 | /* First see if we already have a cgroup group that matches |
| 623 | * the desired set */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 624 | down_read(&css_set_rwsem); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 625 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 626 | if (cset) |
| 627 | get_css_set(cset); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 628 | up_read(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 629 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 630 | if (cset) |
| 631 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 632 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 633 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 634 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 635 | return NULL; |
| 636 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 637 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 638 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 639 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 640 | return NULL; |
| 641 | } |
| 642 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 643 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 644 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 645 | INIT_LIST_HEAD(&cset->tasks); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 646 | INIT_LIST_HEAD(&cset->mg_tasks); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 647 | INIT_LIST_HEAD(&cset->mg_preload_node); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 648 | INIT_LIST_HEAD(&cset->mg_node); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 649 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 650 | |
| 651 | /* Copy the set of subsystem state objects generated in |
| 652 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 653 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 654 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 655 | down_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 656 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 657 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 658 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 659 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 660 | if (c->root == cgrp->root) |
| 661 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 662 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 663 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 664 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 665 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 666 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 667 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 668 | |
| 669 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 670 | key = css_set_hash(cset->subsys); |
| 671 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 672 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 673 | up_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 674 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 675 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 678 | static struct cgroupfs_root *cgroup_root_from_kf(struct kernfs_root *kf_root) |
| 679 | { |
| 680 | struct cgroup *top_cgrp = kf_root->kn->priv; |
| 681 | |
| 682 | return top_cgrp->root; |
| 683 | } |
| 684 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 685 | static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end) |
| 686 | { |
| 687 | int id; |
| 688 | |
| 689 | lockdep_assert_held(&cgroup_mutex); |
| 690 | |
| 691 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 692 | GFP_KERNEL); |
| 693 | if (id < 0) |
| 694 | return id; |
| 695 | |
| 696 | root->hierarchy_id = id; |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 701 | { |
| 702 | lockdep_assert_held(&cgroup_mutex); |
| 703 | |
| 704 | if (root->hierarchy_id) { |
| 705 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
| 706 | root->hierarchy_id = 0; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | static void cgroup_free_root(struct cgroupfs_root *root) |
| 711 | { |
| 712 | if (root) { |
| 713 | /* hierarhcy ID shoulid already have been released */ |
| 714 | WARN_ON_ONCE(root->hierarchy_id); |
| 715 | |
| 716 | idr_destroy(&root->cgroup_idr); |
| 717 | kfree(root); |
| 718 | } |
| 719 | } |
| 720 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 721 | static void cgroup_destroy_root(struct cgroupfs_root *root) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 722 | { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 723 | struct cgroup *cgrp = &root->top_cgroup; |
| 724 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 725 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 726 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 727 | mutex_lock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 728 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 729 | BUG_ON(atomic_read(&root->nr_cgrps)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 730 | BUG_ON(!list_empty(&cgrp->children)); |
| 731 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 732 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 3558557 | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 733 | WARN_ON(rebind_subsystems(root, 0, root->subsys_mask)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 734 | |
| 735 | /* |
| 736 | * Release all the links from cset_links to this hierarchy's |
| 737 | * root cgroup |
| 738 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 739 | down_write(&css_set_rwsem); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 740 | |
| 741 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 742 | list_del(&link->cset_link); |
| 743 | list_del(&link->cgrp_link); |
| 744 | kfree(link); |
| 745 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 746 | up_write(&css_set_rwsem); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 747 | |
| 748 | if (!list_empty(&root->root_list)) { |
| 749 | list_del(&root->root_list); |
| 750 | cgroup_root_count--; |
| 751 | } |
| 752 | |
| 753 | cgroup_exit_root_id(root); |
| 754 | |
| 755 | mutex_unlock(&cgroup_mutex); |
| 756 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 757 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 758 | kernfs_destroy_root(root->kf_root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 759 | cgroup_free_root(root); |
| 760 | } |
| 761 | |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 762 | /* look up cgroup associated with given css_set on the specified hierarchy */ |
| 763 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 764 | struct cgroupfs_root *root) |
| 765 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 766 | struct cgroup *res = NULL; |
| 767 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 768 | lockdep_assert_held(&cgroup_mutex); |
| 769 | lockdep_assert_held(&css_set_rwsem); |
| 770 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 771 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 772 | res = &root->top_cgroup; |
| 773 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 774 | struct cgrp_cset_link *link; |
| 775 | |
| 776 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 777 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 778 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 779 | if (c->root == root) { |
| 780 | res = c; |
| 781 | break; |
| 782 | } |
| 783 | } |
| 784 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 785 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 786 | BUG_ON(!res); |
| 787 | return res; |
| 788 | } |
| 789 | |
| 790 | /* |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 791 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 792 | * called with cgroup_mutex and css_set_rwsem held. |
| 793 | */ |
| 794 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 795 | struct cgroupfs_root *root) |
| 796 | { |
| 797 | /* |
| 798 | * No need to lock the task - since we hold cgroup_mutex the |
| 799 | * task can't change groups, so the only thing that can happen |
| 800 | * is that it exits and its css is set back to init_css_set. |
| 801 | */ |
| 802 | return cset_cgroup_from_root(task_css_set(task), root); |
| 803 | } |
| 804 | |
| 805 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 806 | * There is one global cgroup mutex. We also require taking |
| 807 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 808 | * See "The task_lock() exception", at the end of this comment. |
| 809 | * |
| 810 | * A task must hold cgroup_mutex to modify cgroups. |
| 811 | * |
| 812 | * Any task can increment and decrement the count field without lock. |
| 813 | * So in general, code holding cgroup_mutex can't rely on the count |
| 814 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 815 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 816 | * means that no tasks are currently attached, therefore there is no |
| 817 | * way a task attached to that cgroup can fork (the other way to |
| 818 | * increment the count). So code holding cgroup_mutex can safely |
| 819 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 820 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 821 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 822 | * needs that mutex. |
| 823 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 824 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 825 | * (usually) take cgroup_mutex. These are the two most performance |
| 826 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 827 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 828 | * 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] | 829 | * to the release agent with the name of the cgroup (path relative to |
| 830 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 831 | * |
| 832 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 833 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 834 | * tasks in the system use _some_ cgroup, and since there is always at |
| 835 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 836 | * always has either children cgroups and/or using tasks. So we don't |
| 837 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 838 | * |
| 839 | * The task_lock() exception |
| 840 | * |
| 841 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 842 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 843 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 844 | * several performance critical places that need to reference |
| 845 | * task->cgroup without the expense of grabbing a system global |
| 846 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 847 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 848 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 849 | * the task_struct routinely used for such matters. |
| 850 | * |
| 851 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 852 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 853 | */ |
| 854 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 855 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 856 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 857 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 858 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 859 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 860 | char *buf) |
| 861 | { |
| 862 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 863 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) |
| 864 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s", |
| 865 | cft->ss->name, cft->name); |
| 866 | else |
| 867 | strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
| 868 | return buf; |
| 869 | } |
| 870 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 871 | /** |
| 872 | * cgroup_file_mode - deduce file mode of a control file |
| 873 | * @cft: the control file in question |
| 874 | * |
| 875 | * returns cft->mode if ->mode is not 0 |
| 876 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 877 | * returns S_IRUGO if it has only a read handler |
| 878 | * returns S_IWUSR if it has only a write hander |
| 879 | */ |
| 880 | static umode_t cgroup_file_mode(const struct cftype *cft) |
| 881 | { |
| 882 | umode_t mode = 0; |
| 883 | |
| 884 | if (cft->mode) |
| 885 | return cft->mode; |
| 886 | |
| 887 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 888 | mode |= S_IRUGO; |
| 889 | |
| 890 | if (cft->write_u64 || cft->write_s64 || cft->write_string || |
| 891 | cft->trigger) |
| 892 | mode |= S_IWUSR; |
| 893 | |
| 894 | return mode; |
| 895 | } |
| 896 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 897 | static void cgroup_free_fn(struct work_struct *work) |
| 898 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 899 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 900 | |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 901 | atomic_dec(&cgrp->root->nr_cgrps); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 902 | cgroup_pidlist_destroy_all(cgrp); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 903 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 904 | if (cgrp->parent) { |
| 905 | /* |
| 906 | * We get a ref to the parent, and put the ref when this |
| 907 | * cgroup is being freed, so it's guaranteed that the |
| 908 | * parent won't be destroyed before its children. |
| 909 | */ |
| 910 | cgroup_put(cgrp->parent); |
| 911 | kernfs_put(cgrp->kn); |
| 912 | kfree(cgrp); |
| 913 | } else { |
| 914 | /* |
| 915 | * This is top cgroup's refcnt reaching zero, which |
| 916 | * indicates that the root should be released. |
| 917 | */ |
| 918 | cgroup_destroy_root(cgrp->root); |
| 919 | } |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | static void cgroup_free_rcu(struct rcu_head *head) |
| 923 | { |
| 924 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 925 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 926 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 927 | queue_work(cgroup_destroy_wq, &cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 928 | } |
| 929 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 930 | static void cgroup_get(struct cgroup *cgrp) |
| 931 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 932 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
| 933 | WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0); |
| 934 | atomic_inc(&cgrp->refcnt); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 937 | static void cgroup_put(struct cgroup *cgrp) |
| 938 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 939 | if (!atomic_dec_and_test(&cgrp->refcnt)) |
| 940 | return; |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 941 | if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp))) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 942 | return; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 943 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 944 | /* |
| 945 | * XXX: cgrp->id is only used to look up css's. As cgroup and |
| 946 | * css's lifetimes will be decoupled, it should be made |
| 947 | * per-subsystem and moved to css->id so that lookups are |
| 948 | * successful until the target css is released. |
| 949 | */ |
| 950 | mutex_lock(&cgroup_mutex); |
| 951 | idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 952 | mutex_unlock(&cgroup_mutex); |
| 953 | cgrp->id = -1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 954 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 955 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 958 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 959 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 960 | char name[CGROUP_FILE_NAME_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 961 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 962 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 963 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 966 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 967 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 968 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 969 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 970 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 971 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 972 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 973 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 974 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 975 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 976 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 977 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 978 | |
| 979 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 980 | continue; |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 981 | list_for_each_entry(cfts, &ss->cfts, node) |
| 982 | cgroup_addrm_files(cgrp, cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 983 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 986 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 987 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 988 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 989 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 990 | struct cgroup_subsys *ss; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 991 | int i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 992 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 993 | lockdep_assert_held(&cgroup_tree_mutex); |
| 994 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 995 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 996 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 997 | for_each_subsys(ss, i) |
| 998 | if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root) |
| 999 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1000 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1001 | ret = cgroup_populate_dir(cgrp, added_mask); |
| 1002 | if (ret) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1003 | return ret; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1004 | |
| 1005 | /* |
| 1006 | * Nothing can fail from this point on. Remove files for the |
| 1007 | * removed subsystems and rebind each subsystem. |
| 1008 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1009 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1010 | cgroup_clear_dir(cgrp, removed_mask); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1011 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1012 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1013 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1014 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1015 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1016 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1017 | /* We're binding this subsystem to this hierarchy */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1018 | BUG_ON(cgroup_css(cgrp, ss)); |
| 1019 | BUG_ON(!cgroup_css(cgroup_dummy_top, ss)); |
| 1020 | BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1021 | |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1022 | rcu_assign_pointer(cgrp->subsys[i], |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1023 | cgroup_css(cgroup_dummy_top, ss)); |
| 1024 | cgroup_css(cgrp, ss)->cgroup = cgrp; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1025 | |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1026 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1027 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1028 | ss->bind(cgroup_css(cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1029 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1030 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1031 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1032 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1033 | /* We're removing this subsystem */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1034 | BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss)); |
| 1035 | BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1036 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1037 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1038 | ss->bind(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1039 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1040 | cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1041 | RCU_INIT_POINTER(cgrp->subsys[i], NULL); |
| 1042 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1043 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1044 | root->subsys_mask &= ~bit; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1045 | } |
| 1046 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1047 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1048 | kernfs_activate(cgrp->kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1049 | return 0; |
| 1050 | } |
| 1051 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1052 | static int cgroup_show_options(struct seq_file *seq, |
| 1053 | struct kernfs_root *kf_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1054 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1055 | struct cgroupfs_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1056 | struct cgroup_subsys *ss; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1057 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1058 | |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1059 | for_each_subsys(ss, ssid) |
| 1060 | if (root->subsys_mask & (1 << ssid)) |
| 1061 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1062 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1063 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1064 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1065 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1066 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1067 | seq_puts(seq, ",xattr"); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1068 | |
| 1069 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1070 | if (strlen(root->release_agent_path)) |
| 1071 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1072 | spin_unlock(&release_agent_path_lock); |
| 1073 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1074 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1075 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1076 | if (strlen(root->name)) |
| 1077 | seq_printf(seq, ",name=%s", root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1082 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1083 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1084 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1085 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1086 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1087 | /* User explicitly requested empty subsystem */ |
| 1088 | bool none; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1089 | }; |
| 1090 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1091 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1092 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1093 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1094 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1095 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1096 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1097 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1098 | { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1099 | char *token, *o = data; |
| 1100 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1101 | unsigned long mask = (unsigned long)-1; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1102 | struct cgroup_subsys *ss; |
| 1103 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1104 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1105 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1106 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1107 | #ifdef CONFIG_CPUSETS |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1108 | mask = ~(1UL << cpuset_cgrp_id); |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1109 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1110 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1111 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1112 | |
| 1113 | while ((token = strsep(&o, ",")) != NULL) { |
| 1114 | if (!*token) |
| 1115 | return -EINVAL; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1116 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1117 | /* Explicitly have no subsystems */ |
| 1118 | opts->none = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1119 | continue; |
| 1120 | } |
| 1121 | if (!strcmp(token, "all")) { |
| 1122 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1123 | if (one_ss) |
| 1124 | return -EINVAL; |
| 1125 | all_ss = true; |
| 1126 | continue; |
| 1127 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1128 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1129 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1130 | continue; |
| 1131 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1132 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1133 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1134 | continue; |
| 1135 | } |
| 1136 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1137 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1138 | continue; |
| 1139 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1140 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1141 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1142 | continue; |
| 1143 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1144 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1145 | /* Specifying two release agents is forbidden */ |
| 1146 | if (opts->release_agent) |
| 1147 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1148 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1149 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1150 | if (!opts->release_agent) |
| 1151 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1152 | continue; |
| 1153 | } |
| 1154 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1155 | const char *name = token + 5; |
| 1156 | /* Can't specify an empty name */ |
| 1157 | if (!strlen(name)) |
| 1158 | return -EINVAL; |
| 1159 | /* Must match [\w.-]+ */ |
| 1160 | for (i = 0; i < strlen(name); i++) { |
| 1161 | char c = name[i]; |
| 1162 | if (isalnum(c)) |
| 1163 | continue; |
| 1164 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1165 | continue; |
| 1166 | return -EINVAL; |
| 1167 | } |
| 1168 | /* Specifying two names is forbidden */ |
| 1169 | if (opts->name) |
| 1170 | return -EINVAL; |
| 1171 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1172 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1173 | GFP_KERNEL); |
| 1174 | if (!opts->name) |
| 1175 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1176 | |
| 1177 | continue; |
| 1178 | } |
| 1179 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1180 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1181 | if (strcmp(token, ss->name)) |
| 1182 | continue; |
| 1183 | if (ss->disabled) |
| 1184 | continue; |
| 1185 | |
| 1186 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1187 | if (all_ss) |
| 1188 | return -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1189 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1190 | one_ss = true; |
| 1191 | |
| 1192 | break; |
| 1193 | } |
| 1194 | if (i == CGROUP_SUBSYS_COUNT) |
| 1195 | return -ENOENT; |
| 1196 | } |
| 1197 | |
| 1198 | /* |
| 1199 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1200 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1201 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1202 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1203 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1204 | for_each_subsys(ss, i) |
| 1205 | if (!ss->disabled) |
| 1206 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1207 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1208 | /* Consistency checks */ |
| 1209 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1210 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1211 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1212 | |
Tejun Heo | d3ba07c | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1213 | if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) || |
| 1214 | opts->cpuset_clone_children || opts->release_agent || |
| 1215 | opts->name) { |
| 1216 | pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n"); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1217 | return -EINVAL; |
| 1218 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1221 | /* |
| 1222 | * Option noprefix was introduced just for backward compatibility |
| 1223 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1224 | * the cpuset subsystem. |
| 1225 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1226 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1227 | return -EINVAL; |
| 1228 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1229 | |
| 1230 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1231 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1232 | return -EINVAL; |
| 1233 | |
| 1234 | /* |
| 1235 | * We either have to specify by name or by subsystems. (So all |
| 1236 | * empty hierarchies must have a name). |
| 1237 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1238 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1239 | return -EINVAL; |
| 1240 | |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1244 | 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] | 1245 | { |
| 1246 | int ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1247 | struct cgroupfs_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1248 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1249 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1250 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1251 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1252 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1253 | return -EINVAL; |
| 1254 | } |
| 1255 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1256 | mutex_lock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1257 | mutex_lock(&cgroup_mutex); |
| 1258 | |
| 1259 | /* See what subsystems are wanted */ |
| 1260 | ret = parse_cgroupfs_options(data, &opts); |
| 1261 | if (ret) |
| 1262 | goto out_unlock; |
| 1263 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1264 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1265 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1266 | task_tgid_nr(current), current->comm); |
| 1267 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1268 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1269 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1270 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1271 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1272 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1273 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1274 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1275 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1276 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1277 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1278 | goto out_unlock; |
| 1279 | } |
| 1280 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1281 | /* remounting is not allowed for populated hierarchies */ |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1282 | if (!list_empty(&root->top_cgroup.children)) { |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1283 | ret = -EBUSY; |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1284 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1285 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1286 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1287 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1288 | if (ret) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1289 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1290 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1291 | if (opts.release_agent) { |
| 1292 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1293 | strcpy(root->release_agent_path, opts.release_agent); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1294 | spin_unlock(&release_agent_path_lock); |
| 1295 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1296 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1297 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1298 | kfree(opts.name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1299 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1300 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1301 | return ret; |
| 1302 | } |
| 1303 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1304 | /* |
| 1305 | * To reduce the fork() overhead for systems that are not actually using |
| 1306 | * their cgroups capability, we don't maintain the lists running through |
| 1307 | * each css_set to its tasks until we see the list actually used - in other |
| 1308 | * words after the first mount. |
| 1309 | */ |
| 1310 | static bool use_task_css_set_links __read_mostly; |
| 1311 | |
| 1312 | static void cgroup_enable_task_cg_lists(void) |
| 1313 | { |
| 1314 | struct task_struct *p, *g; |
| 1315 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1316 | down_write(&css_set_rwsem); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1317 | |
| 1318 | if (use_task_css_set_links) |
| 1319 | goto out_unlock; |
| 1320 | |
| 1321 | use_task_css_set_links = true; |
| 1322 | |
| 1323 | /* |
| 1324 | * We need tasklist_lock because RCU is not safe against |
| 1325 | * while_each_thread(). Besides, a forking task that has passed |
| 1326 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 1327 | * is not guaranteed to have its child immediately visible in the |
| 1328 | * tasklist if we walk through it with RCU. |
| 1329 | */ |
| 1330 | read_lock(&tasklist_lock); |
| 1331 | do_each_thread(g, p) { |
| 1332 | task_lock(p); |
| 1333 | |
| 1334 | WARN_ON_ONCE(!list_empty(&p->cg_list) || |
| 1335 | task_css_set(p) != &init_css_set); |
| 1336 | |
| 1337 | /* |
| 1338 | * We should check if the process is exiting, otherwise |
| 1339 | * it will race with cgroup_exit() in that the list |
| 1340 | * entry won't be deleted though the process has exited. |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1341 | * Do it while holding siglock so that we don't end up |
| 1342 | * racing against cgroup_exit(). |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1343 | */ |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1344 | spin_lock_irq(&p->sighand->siglock); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 1345 | if (!(p->flags & PF_EXITING)) { |
| 1346 | struct css_set *cset = task_css_set(p); |
| 1347 | |
| 1348 | list_add(&p->cg_list, &cset->tasks); |
| 1349 | get_css_set(cset); |
| 1350 | } |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1351 | spin_unlock_irq(&p->sighand->siglock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1352 | |
| 1353 | task_unlock(p); |
| 1354 | } while_each_thread(g, p); |
| 1355 | read_unlock(&tasklist_lock); |
| 1356 | out_unlock: |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1357 | up_write(&css_set_rwsem); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1358 | } |
| 1359 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1360 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1361 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1362 | atomic_set(&cgrp->refcnt, 1); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1363 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1364 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1365 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1366 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1367 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1368 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1369 | cgrp->dummy_css.cgroup = cgrp; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1370 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1371 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1372 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1373 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1374 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1375 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1376 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1377 | atomic_set(&root->nr_cgrps, 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1378 | cgrp->root = root; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1379 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8 | 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 | a1a71b45 | 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 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1396 | root->flags = opts->flags; |
| 1397 | if (opts->release_agent) |
| 1398 | strcpy(root->release_agent_path, opts->release_agent); |
| 1399 | if (opts->name) |
| 1400 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1401 | if (opts->cpuset_clone_children) |
| 1402 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1403 | return root; |
| 1404 | } |
| 1405 | |
Tejun Heo | 3558557 | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1406 | static int cgroup_setup_root(struct cgroupfs_root *root, unsigned long ss_mask) |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1407 | { |
| 1408 | LIST_HEAD(tmp_links); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1409 | struct cgroup *root_cgrp = &root->top_cgroup; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1410 | struct css_set *cset; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1411 | int i, ret; |
| 1412 | |
| 1413 | lockdep_assert_held(&cgroup_tree_mutex); |
| 1414 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1415 | |
| 1416 | ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL); |
| 1417 | if (ret < 0) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1418 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1419 | root_cgrp->id = ret; |
| 1420 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1421 | /* |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1422 | * We're accessing css_set_count without locking css_set_rwsem here, |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1423 | * but that's OK - it can only be increased by someone holding |
| 1424 | * cgroup_lock, and that's us. The worst that can happen is that we |
| 1425 | * have some link structures left over |
| 1426 | */ |
| 1427 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
| 1428 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1429 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1430 | |
| 1431 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1432 | ret = cgroup_init_root_id(root, 2, 0); |
| 1433 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1434 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1435 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1436 | root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops, |
| 1437 | KERNFS_ROOT_CREATE_DEACTIVATED, |
| 1438 | root_cgrp); |
| 1439 | if (IS_ERR(root->kf_root)) { |
| 1440 | ret = PTR_ERR(root->kf_root); |
| 1441 | goto exit_root_id; |
| 1442 | } |
| 1443 | root_cgrp->kn = root->kf_root->kn; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1444 | |
| 1445 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
| 1446 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1447 | goto destroy_root; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1448 | |
Tejun Heo | 3558557 | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1449 | ret = rebind_subsystems(root, ss_mask, 0); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1450 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1451 | goto destroy_root; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1452 | |
| 1453 | /* |
| 1454 | * There must be no failure case after here, since rebinding takes |
| 1455 | * care of subsystems' refcounts, which are explicitly dropped in |
| 1456 | * the failure exit path. |
| 1457 | */ |
| 1458 | list_add(&root->root_list, &cgroup_roots); |
| 1459 | cgroup_root_count++; |
| 1460 | |
| 1461 | /* |
| 1462 | * Link the top cgroup in this hierarchy into all the css_set |
| 1463 | * objects. |
| 1464 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1465 | down_write(&css_set_rwsem); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1466 | hash_for_each(css_set_table, i, cset, hlist) |
| 1467 | link_css_set(&tmp_links, cset, root_cgrp); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1468 | up_write(&css_set_rwsem); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1469 | |
| 1470 | BUG_ON(!list_empty(&root_cgrp->children)); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1471 | BUG_ON(atomic_read(&root->nr_cgrps) != 1); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1472 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1473 | kernfs_activate(root_cgrp->kn); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1474 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1475 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1476 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1477 | destroy_root: |
| 1478 | kernfs_destroy_root(root->kf_root); |
| 1479 | root->kf_root = NULL; |
| 1480 | exit_root_id: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1481 | cgroup_exit_root_id(root); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1482 | out: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1483 | free_cgrp_cset_links(&tmp_links); |
| 1484 | return ret; |
| 1485 | } |
| 1486 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1487 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1488 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1489 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1490 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1491 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1492 | struct cgroup_sb_opts opts; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1493 | struct dentry *dentry; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1494 | int ret; |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1495 | |
| 1496 | /* |
| 1497 | * The first time anyone tries to mount a cgroup, enable the list |
| 1498 | * linking each css_set to its tasks and fix up all existing tasks. |
| 1499 | */ |
| 1500 | if (!use_task_css_set_links) |
| 1501 | cgroup_enable_task_cg_lists(); |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1502 | retry: |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1503 | mutex_lock(&cgroup_tree_mutex); |
| 1504 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1505 | |
| 1506 | /* First find the desired set of subsystems */ |
| 1507 | ret = parse_cgroupfs_options(data, &opts); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1508 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1509 | goto out_unlock; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1510 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1511 | /* look for a matching existing root */ |
| 1512 | for_each_active_root(root) { |
| 1513 | bool name_match = false; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1514 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1515 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1516 | * If we asked for a name then it must match. Also, if |
| 1517 | * name matches but sybsys_mask doesn't, we should fail. |
| 1518 | * Remember whether name matched. |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1519 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1520 | if (opts.name) { |
| 1521 | if (strcmp(opts.name, root->name)) |
| 1522 | continue; |
| 1523 | name_match = true; |
| 1524 | } |
| 1525 | |
| 1526 | /* |
| 1527 | * If we asked for subsystems (or explicitly for no |
| 1528 | * subsystems) then they must match. |
| 1529 | */ |
| 1530 | if ((opts.subsys_mask || opts.none) && |
| 1531 | (opts.subsys_mask != root->subsys_mask)) { |
| 1532 | if (!name_match) |
| 1533 | continue; |
| 1534 | ret = -EBUSY; |
| 1535 | goto out_unlock; |
| 1536 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1537 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1538 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1539 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1540 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1541 | ret = -EINVAL; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1542 | goto out_unlock; |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1543 | } else { |
| 1544 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1545 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1546 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1547 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1548 | /* |
| 1549 | * A root's lifetime is governed by its top cgroup. Zero |
| 1550 | * ref indicate that the root is being destroyed. Wait for |
| 1551 | * destruction to complete so that the subsystems are free. |
| 1552 | * We can use wait_queue for the wait but this path is |
| 1553 | * super cold. Let's just sleep for a bit and retry. |
| 1554 | */ |
| 1555 | if (!atomic_inc_not_zero(&root->top_cgroup.refcnt)) { |
| 1556 | mutex_unlock(&cgroup_mutex); |
| 1557 | mutex_unlock(&cgroup_tree_mutex); |
Li Zefan | 6534fd6 | 2014-02-14 16:55:04 +0800 | [diff] [blame] | 1558 | kfree(opts.release_agent); |
| 1559 | kfree(opts.name); |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1560 | msleep(10); |
| 1561 | goto retry; |
| 1562 | } |
| 1563 | |
| 1564 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1565 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1568 | /* no such thing, create a new one */ |
| 1569 | root = cgroup_root_from_opts(&opts); |
| 1570 | if (IS_ERR(root)) { |
| 1571 | ret = PTR_ERR(root); |
| 1572 | goto out_unlock; |
| 1573 | } |
| 1574 | |
Tejun Heo | 3558557 | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1575 | ret = cgroup_setup_root(root, opts.subsys_mask); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1576 | if (ret) |
| 1577 | cgroup_free_root(root); |
| 1578 | |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1579 | out_unlock: |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1580 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1581 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1582 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1583 | kfree(opts.release_agent); |
| 1584 | kfree(opts.name); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1585 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1586 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1587 | return ERR_PTR(ret); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1588 | |
| 1589 | dentry = kernfs_mount(fs_type, flags, root->kf_root); |
| 1590 | if (IS_ERR(dentry)) |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1591 | cgroup_put(&root->top_cgroup); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1592 | return dentry; |
| 1593 | } |
| 1594 | |
| 1595 | static void cgroup_kill_sb(struct super_block *sb) |
| 1596 | { |
| 1597 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
| 1598 | struct cgroupfs_root *root = cgroup_root_from_kf(kf_root); |
| 1599 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1600 | cgroup_put(&root->top_cgroup); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1601 | kernfs_kill_sb(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1604 | static struct file_system_type cgroup_fs_type = { |
| 1605 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1606 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1607 | .kill_sb = cgroup_kill_sb, |
| 1608 | }; |
| 1609 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1610 | static struct kobject *cgroup_kobj; |
| 1611 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1612 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1613 | * 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] | 1614 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1615 | * @buf: the buffer to write the path into |
| 1616 | * @buflen: the length of the buffer |
| 1617 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1618 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1619 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1620 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1621 | * cgroup controller callbacks. |
| 1622 | * |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1623 | * Return value is the same as kernfs_path(). |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1624 | */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1625 | char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1626 | { |
| 1627 | struct cgroupfs_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1628 | struct cgroup *cgrp; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1629 | int hierarchy_id = 1; |
| 1630 | char *path = NULL; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1631 | |
| 1632 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1633 | down_read(&css_set_rwsem); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1634 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1635 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1636 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1637 | if (root) { |
| 1638 | cgrp = task_cgroup_from_root(task, root); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1639 | path = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1640 | } else { |
| 1641 | /* if no hierarchy exists, everyone is in "/" */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1642 | if (strlcpy(buf, "/", buflen) < buflen) |
| 1643 | path = buf; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1646 | up_read(&css_set_rwsem); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1647 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1648 | return path; |
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 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1651 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1652 | /* used to track tasks and other necessary states during migration */ |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1653 | struct cgroup_taskset { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1654 | /* the src and dst cset list running through cset->mg_node */ |
| 1655 | struct list_head src_csets; |
| 1656 | struct list_head dst_csets; |
| 1657 | |
| 1658 | /* |
| 1659 | * Fields for cgroup_taskset_*() iteration. |
| 1660 | * |
| 1661 | * Before migration is committed, the target migration tasks are on |
| 1662 | * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of |
| 1663 | * the csets on ->dst_csets. ->csets point to either ->src_csets |
| 1664 | * or ->dst_csets depending on whether migration is committed. |
| 1665 | * |
| 1666 | * ->cur_csets and ->cur_task point to the current task position |
| 1667 | * during iteration. |
| 1668 | */ |
| 1669 | struct list_head *csets; |
| 1670 | struct css_set *cur_cset; |
| 1671 | struct task_struct *cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1672 | }; |
| 1673 | |
| 1674 | /** |
| 1675 | * cgroup_taskset_first - reset taskset and return the first task |
| 1676 | * @tset: taskset of interest |
| 1677 | * |
| 1678 | * @tset iteration is initialized and the first task is returned. |
| 1679 | */ |
| 1680 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1681 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1682 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); |
| 1683 | tset->cur_task = NULL; |
| 1684 | |
| 1685 | return cgroup_taskset_next(tset); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1686 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1687 | |
| 1688 | /** |
| 1689 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1690 | * @tset: taskset of interest |
| 1691 | * |
| 1692 | * Return the next task in @tset. Iteration must have been initialized |
| 1693 | * with cgroup_taskset_first(). |
| 1694 | */ |
| 1695 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1696 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1697 | struct css_set *cset = tset->cur_cset; |
| 1698 | struct task_struct *task = tset->cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1699 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1700 | while (&cset->mg_node != tset->csets) { |
| 1701 | if (!task) |
| 1702 | task = list_first_entry(&cset->mg_tasks, |
| 1703 | struct task_struct, cg_list); |
| 1704 | else |
| 1705 | task = list_next_entry(task, cg_list); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1706 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1707 | if (&task->cg_list != &cset->mg_tasks) { |
| 1708 | tset->cur_cset = cset; |
| 1709 | tset->cur_task = task; |
| 1710 | return task; |
| 1711 | } |
| 1712 | |
| 1713 | cset = list_next_entry(cset, mg_node); |
| 1714 | task = NULL; |
| 1715 | } |
| 1716 | |
| 1717 | return NULL; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1718 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1719 | |
| 1720 | /** |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1721 | * cgroup_task_migrate - move a task from one cgroup to another. |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1722 | * @old_cgrp; the cgroup @tsk is being migrated from |
| 1723 | * @tsk: the task being migrated |
| 1724 | * @new_cset: the new css_set @tsk is being attached to |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1725 | * |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1726 | * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1727 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1728 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1729 | struct task_struct *tsk, |
| 1730 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1731 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1732 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1733 | |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1734 | lockdep_assert_held(&cgroup_mutex); |
| 1735 | lockdep_assert_held(&css_set_rwsem); |
| 1736 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1737 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1738 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1739 | * setting such that we can't race against cgroup_exit() changing the |
| 1740 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1741 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1742 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1743 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1744 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1745 | get_css_set(new_cset); |
| 1746 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1747 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1748 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1749 | task_unlock(tsk); |
| 1750 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1751 | list_move(&tsk->cg_list, &new_cset->mg_tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1752 | |
| 1753 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1754 | * We just gained a reference on old_cset by taking it from the |
| 1755 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1756 | * 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] | 1757 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1758 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1759 | put_css_set_locked(old_cset, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1762 | /** |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1763 | * cgroup_migrate_finish - cleanup after attach |
| 1764 | * @preloaded_csets: list of preloaded css_sets |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1765 | * |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1766 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See |
| 1767 | * those functions for details. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1768 | */ |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1769 | static void cgroup_migrate_finish(struct list_head *preloaded_csets) |
| 1770 | { |
| 1771 | struct css_set *cset, *tmp_cset; |
| 1772 | |
| 1773 | lockdep_assert_held(&cgroup_mutex); |
| 1774 | |
| 1775 | down_write(&css_set_rwsem); |
| 1776 | list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) { |
| 1777 | cset->mg_src_cgrp = NULL; |
| 1778 | cset->mg_dst_cset = NULL; |
| 1779 | list_del_init(&cset->mg_preload_node); |
| 1780 | put_css_set_locked(cset, false); |
| 1781 | } |
| 1782 | up_write(&css_set_rwsem); |
| 1783 | } |
| 1784 | |
| 1785 | /** |
| 1786 | * cgroup_migrate_add_src - add a migration source css_set |
| 1787 | * @src_cset: the source css_set to add |
| 1788 | * @dst_cgrp: the destination cgroup |
| 1789 | * @preloaded_csets: list of preloaded css_sets |
| 1790 | * |
| 1791 | * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin |
| 1792 | * @src_cset and add it to @preloaded_csets, which should later be cleaned |
| 1793 | * up by cgroup_migrate_finish(). |
| 1794 | * |
| 1795 | * This function may be called without holding threadgroup_lock even if the |
| 1796 | * target is a process. Threads may be created and destroyed but as long |
| 1797 | * as cgroup_mutex is not dropped, no new css_set can be put into play and |
| 1798 | * the preloaded css_sets are guaranteed to cover all migrations. |
| 1799 | */ |
| 1800 | static void cgroup_migrate_add_src(struct css_set *src_cset, |
| 1801 | struct cgroup *dst_cgrp, |
| 1802 | struct list_head *preloaded_csets) |
| 1803 | { |
| 1804 | struct cgroup *src_cgrp; |
| 1805 | |
| 1806 | lockdep_assert_held(&cgroup_mutex); |
| 1807 | lockdep_assert_held(&css_set_rwsem); |
| 1808 | |
| 1809 | src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); |
| 1810 | |
| 1811 | /* nothing to do if this cset already belongs to the cgroup */ |
| 1812 | if (src_cgrp == dst_cgrp) |
| 1813 | return; |
| 1814 | |
| 1815 | if (!list_empty(&src_cset->mg_preload_node)) |
| 1816 | return; |
| 1817 | |
| 1818 | WARN_ON(src_cset->mg_src_cgrp); |
| 1819 | WARN_ON(!list_empty(&src_cset->mg_tasks)); |
| 1820 | WARN_ON(!list_empty(&src_cset->mg_node)); |
| 1821 | |
| 1822 | src_cset->mg_src_cgrp = src_cgrp; |
| 1823 | get_css_set(src_cset); |
| 1824 | list_add(&src_cset->mg_preload_node, preloaded_csets); |
| 1825 | } |
| 1826 | |
| 1827 | /** |
| 1828 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration |
| 1829 | * @dst_cgrp: the destination cgroup |
| 1830 | * @preloaded_csets: list of preloaded source css_sets |
| 1831 | * |
| 1832 | * Tasks are about to be moved to @dst_cgrp and all the source css_sets |
| 1833 | * have been preloaded to @preloaded_csets. This function looks up and |
| 1834 | * pins all destination css_sets, links each to its source, and put them on |
| 1835 | * @preloaded_csets. |
| 1836 | * |
| 1837 | * This function must be called after cgroup_migrate_add_src() has been |
| 1838 | * called on each migration source css_set. After migration is performed |
| 1839 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on |
| 1840 | * @preloaded_csets. |
| 1841 | */ |
| 1842 | static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp, |
| 1843 | struct list_head *preloaded_csets) |
| 1844 | { |
| 1845 | LIST_HEAD(csets); |
| 1846 | struct css_set *src_cset; |
| 1847 | |
| 1848 | lockdep_assert_held(&cgroup_mutex); |
| 1849 | |
| 1850 | /* look up the dst cset for each src cset and link it to src */ |
| 1851 | list_for_each_entry(src_cset, preloaded_csets, mg_preload_node) { |
| 1852 | struct css_set *dst_cset; |
| 1853 | |
| 1854 | dst_cset = find_css_set(src_cset, dst_cgrp); |
| 1855 | if (!dst_cset) |
| 1856 | goto err; |
| 1857 | |
| 1858 | WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset); |
| 1859 | src_cset->mg_dst_cset = dst_cset; |
| 1860 | |
| 1861 | if (list_empty(&dst_cset->mg_preload_node)) |
| 1862 | list_add(&dst_cset->mg_preload_node, &csets); |
| 1863 | else |
| 1864 | put_css_set(dst_cset, false); |
| 1865 | } |
| 1866 | |
| 1867 | list_splice(&csets, preloaded_csets); |
| 1868 | return 0; |
| 1869 | err: |
| 1870 | cgroup_migrate_finish(&csets); |
| 1871 | return -ENOMEM; |
| 1872 | } |
| 1873 | |
| 1874 | /** |
| 1875 | * cgroup_migrate - migrate a process or task to a cgroup |
| 1876 | * @cgrp: the destination cgroup |
| 1877 | * @leader: the leader of the process or the task to migrate |
| 1878 | * @threadgroup: whether @leader points to the whole process or a single task |
| 1879 | * |
| 1880 | * Migrate a process or task denoted by @leader to @cgrp. If migrating a |
| 1881 | * process, the caller must be holding threadgroup_lock of @leader. The |
| 1882 | * caller is also responsible for invoking cgroup_migrate_add_src() and |
| 1883 | * cgroup_migrate_prepare_dst() on the targets before invoking this |
| 1884 | * function and following up with cgroup_migrate_finish(). |
| 1885 | * |
| 1886 | * As long as a controller's ->can_attach() doesn't fail, this function is |
| 1887 | * guaranteed to succeed. This means that, excluding ->can_attach() |
| 1888 | * failure, when migrating multiple targets, the success or failure can be |
| 1889 | * decided for all targets by invoking group_migrate_prepare_dst() before |
| 1890 | * actually starting migrating. |
| 1891 | */ |
| 1892 | static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader, |
| 1893 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1894 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1895 | struct cgroup_taskset tset = { |
| 1896 | .src_csets = LIST_HEAD_INIT(tset.src_csets), |
| 1897 | .dst_csets = LIST_HEAD_INIT(tset.dst_csets), |
| 1898 | .csets = &tset.src_csets, |
| 1899 | }; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1900 | struct cgroup_subsys_state *css, *failed_css = NULL; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1901 | struct css_set *cset, *tmp_cset; |
| 1902 | struct task_struct *task, *tmp_task; |
| 1903 | int i, ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1904 | |
| 1905 | /* |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1906 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 1907 | * already PF_EXITING could be freed from underneath us unless we |
| 1908 | * take an rcu_read_lock. |
| 1909 | */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1910 | down_write(&css_set_rwsem); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1911 | rcu_read_lock(); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 1912 | task = leader; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1913 | do { |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 1914 | /* @task either already exited or can't exit until the end */ |
| 1915 | if (task->flags & PF_EXITING) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1916 | goto next; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1917 | |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 1918 | /* leave @task alone if post_fork() hasn't linked it yet */ |
| 1919 | if (list_empty(&task->cg_list)) |
| 1920 | goto next; |
| 1921 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1922 | cset = task_css_set(task); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1923 | if (!cset->mg_src_cgrp) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1924 | goto next; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1925 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1926 | list_move(&task->cg_list, &cset->mg_tasks); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1927 | list_move(&cset->mg_node, &tset.src_csets); |
| 1928 | list_move(&cset->mg_dst_cset->mg_node, &tset.dst_csets); |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1929 | next: |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1930 | if (!threadgroup) |
| 1931 | break; |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 1932 | } while_each_thread(leader, task); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1933 | rcu_read_unlock(); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1934 | up_write(&css_set_rwsem); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1935 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1936 | /* methods shouldn't be called if no task is actually migrating */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1937 | if (list_empty(&tset.src_csets)) |
| 1938 | return 0; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1939 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1940 | /* check that we can legitimately attach to the cgroup */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1941 | for_each_css(css, i, cgrp) { |
| 1942 | if (css->ss->can_attach) { |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 1943 | ret = css->ss->can_attach(css, &tset); |
| 1944 | if (ret) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1945 | failed_css = css; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1946 | goto out_cancel_attach; |
| 1947 | } |
| 1948 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | /* |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1952 | * Now that we're guaranteed success, proceed to move all tasks to |
| 1953 | * the new cgroup. There are no failure cases after here, so this |
| 1954 | * is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1955 | */ |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1956 | down_write(&css_set_rwsem); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1957 | list_for_each_entry(cset, &tset.src_csets, mg_node) { |
| 1958 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) |
| 1959 | cgroup_task_migrate(cset->mg_src_cgrp, task, |
| 1960 | cset->mg_dst_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1961 | } |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1962 | up_write(&css_set_rwsem); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1963 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1964 | /* |
| 1965 | * Migration is committed, all target tasks are now on dst_csets. |
| 1966 | * Nothing is sensitive to fork() after this point. Notify |
| 1967 | * controllers that migration is complete. |
| 1968 | */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1969 | tset.csets = &tset.dst_csets; |
| 1970 | |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1971 | for_each_css(css, i, cgrp) |
| 1972 | if (css->ss->attach) |
| 1973 | css->ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1974 | |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 1975 | ret = 0; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1976 | goto out_release_tset; |
| 1977 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1978 | out_cancel_attach: |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1979 | for_each_css(css, i, cgrp) { |
| 1980 | if (css == failed_css) |
| 1981 | break; |
| 1982 | if (css->ss->cancel_attach) |
| 1983 | css->ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1984 | } |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1985 | out_release_tset: |
| 1986 | down_write(&css_set_rwsem); |
| 1987 | list_splice_init(&tset.dst_csets, &tset.src_csets); |
| 1988 | list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) { |
| 1989 | list_splice_init(&cset->mg_tasks, &cset->tasks); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1990 | list_del_init(&cset->mg_node); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1991 | } |
| 1992 | up_write(&css_set_rwsem); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 1993 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1996 | /** |
| 1997 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
| 1998 | * @dst_cgrp: the cgroup to attach to |
| 1999 | * @leader: the task or the leader of the threadgroup to be attached |
| 2000 | * @threadgroup: attach the whole threadgroup? |
| 2001 | * |
| 2002 | * Call holding cgroup_mutex and the group_rwsem of the leader. Will take |
| 2003 | * task_lock of @tsk or each thread in the threadgroup individually in turn. |
| 2004 | */ |
| 2005 | static int cgroup_attach_task(struct cgroup *dst_cgrp, |
| 2006 | struct task_struct *leader, bool threadgroup) |
| 2007 | { |
| 2008 | LIST_HEAD(preloaded_csets); |
| 2009 | struct task_struct *task; |
| 2010 | int ret; |
| 2011 | |
| 2012 | /* look up all src csets */ |
| 2013 | down_read(&css_set_rwsem); |
| 2014 | rcu_read_lock(); |
| 2015 | task = leader; |
| 2016 | do { |
| 2017 | cgroup_migrate_add_src(task_css_set(task), dst_cgrp, |
| 2018 | &preloaded_csets); |
| 2019 | if (!threadgroup) |
| 2020 | break; |
| 2021 | } while_each_thread(leader, task); |
| 2022 | rcu_read_unlock(); |
| 2023 | up_read(&css_set_rwsem); |
| 2024 | |
| 2025 | /* prepare dst csets and commit */ |
| 2026 | ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets); |
| 2027 | if (!ret) |
| 2028 | ret = cgroup_migrate(dst_cgrp, leader, threadgroup); |
| 2029 | |
| 2030 | cgroup_migrate_finish(&preloaded_csets); |
| 2031 | return ret; |
| 2032 | } |
| 2033 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2034 | /* |
| 2035 | * 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] | 2036 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2037 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2038 | */ |
| 2039 | 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] | 2040 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2041 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2042 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2043 | int ret; |
| 2044 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2045 | if (!cgroup_lock_live_group(cgrp)) |
| 2046 | return -ENODEV; |
| 2047 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2048 | retry_find_task: |
| 2049 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2050 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2051 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2052 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2053 | rcu_read_unlock(); |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2054 | ret = -ESRCH; |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2055 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2056 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2057 | /* |
| 2058 | * even if we're attaching all tasks in the thread group, we |
| 2059 | * only need to check permissions on one of them. |
| 2060 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2061 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2062 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2063 | !uid_eq(cred->euid, tcred->uid) && |
| 2064 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2065 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2066 | ret = -EACCES; |
| 2067 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2068 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2069 | } else |
| 2070 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2071 | |
| 2072 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2073 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2074 | |
| 2075 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2076 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2077 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2078 | * with no rt_runtime allocated. Just say no. |
| 2079 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2080 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2081 | ret = -EINVAL; |
| 2082 | rcu_read_unlock(); |
| 2083 | goto out_unlock_cgroup; |
| 2084 | } |
| 2085 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2086 | get_task_struct(tsk); |
| 2087 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2088 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2089 | threadgroup_lock(tsk); |
| 2090 | if (threadgroup) { |
| 2091 | if (!thread_group_leader(tsk)) { |
| 2092 | /* |
| 2093 | * a race with de_thread from another thread's exec() |
| 2094 | * may strip us of our leadership, if this happens, |
| 2095 | * there is no choice but to throw this task away and |
| 2096 | * try again; this is |
| 2097 | * "double-double-toil-and-trouble-check locking". |
| 2098 | */ |
| 2099 | threadgroup_unlock(tsk); |
| 2100 | put_task_struct(tsk); |
| 2101 | goto retry_find_task; |
| 2102 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2106 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2107 | threadgroup_unlock(tsk); |
| 2108 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2109 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2110 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2111 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2112 | return ret; |
| 2113 | } |
| 2114 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2115 | /** |
| 2116 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2117 | * @from: attach to all cgroups of a given task |
| 2118 | * @tsk: the task to be attached |
| 2119 | */ |
| 2120 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2121 | { |
| 2122 | struct cgroupfs_root *root; |
| 2123 | int retval = 0; |
| 2124 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2125 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2126 | for_each_active_root(root) { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2127 | struct cgroup *from_cgrp; |
| 2128 | |
| 2129 | down_read(&css_set_rwsem); |
| 2130 | from_cgrp = task_cgroup_from_root(from, root); |
| 2131 | up_read(&css_set_rwsem); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2132 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2133 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2134 | if (retval) |
| 2135 | break; |
| 2136 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2137 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2138 | |
| 2139 | return retval; |
| 2140 | } |
| 2141 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2142 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2143 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2144 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2145 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2146 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2149 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2150 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2151 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2152 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2155 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
| 2156 | struct cftype *cft, const char *buffer) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2157 | { |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2158 | struct cgroupfs_root *root = css->cgroup->root; |
| 2159 | |
| 2160 | BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2161 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2162 | return -ENODEV; |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2163 | spin_lock(&release_agent_path_lock); |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2164 | strlcpy(root->release_agent_path, buffer, |
| 2165 | sizeof(root->release_agent_path)); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2166 | spin_unlock(&release_agent_path_lock); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2167 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2168 | return 0; |
| 2169 | } |
| 2170 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2171 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2172 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2173 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2174 | |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2175 | if (!cgroup_lock_live_group(cgrp)) |
| 2176 | return -ENODEV; |
| 2177 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2178 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2179 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2180 | return 0; |
| 2181 | } |
| 2182 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2183 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2184 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2185 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2186 | |
| 2187 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2188 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2191 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 2192 | size_t nbytes, loff_t off) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2193 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2194 | struct cgroup *cgrp = of->kn->parent->priv; |
| 2195 | struct cftype *cft = of->kn->priv; |
| 2196 | struct cgroup_subsys_state *css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2197 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2198 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2199 | /* |
| 2200 | * kernfs guarantees that a file isn't deleted with operations in |
| 2201 | * flight, which means that the matching css is and stays alive and |
| 2202 | * doesn't need to be pinned. The RCU locking is not necessary |
| 2203 | * either. It's just for the convenience of using cgroup_css(). |
| 2204 | */ |
| 2205 | rcu_read_lock(); |
| 2206 | css = cgroup_css(cgrp, cft->ss); |
| 2207 | rcu_read_unlock(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2208 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2209 | if (cft->write_string) { |
| 2210 | ret = cft->write_string(css, cft, strstrip(buf)); |
| 2211 | } else if (cft->write_u64) { |
| 2212 | unsigned long long v; |
| 2213 | ret = kstrtoull(buf, 0, &v); |
| 2214 | if (!ret) |
| 2215 | ret = cft->write_u64(css, cft, v); |
| 2216 | } else if (cft->write_s64) { |
| 2217 | long long v; |
| 2218 | ret = kstrtoll(buf, 0, &v); |
| 2219 | if (!ret) |
| 2220 | ret = cft->write_s64(css, cft, v); |
| 2221 | } else if (cft->trigger) { |
| 2222 | ret = cft->trigger(css, (unsigned int)cft->private); |
| 2223 | } else { |
| 2224 | ret = -EINVAL; |
| 2225 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2226 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2227 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2230 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2231 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2232 | return seq_cft(seq)->seq_start(seq, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 2236 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2237 | return seq_cft(seq)->seq_next(seq, v, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 2241 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2242 | seq_cft(seq)->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2246 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2247 | struct cftype *cft = seq_cft(m); |
| 2248 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2249 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2250 | if (cft->seq_show) |
| 2251 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2252 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2253 | if (cft->read_u64) |
| 2254 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 2255 | else if (cft->read_s64) |
| 2256 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 2257 | else |
| 2258 | return -EINVAL; |
| 2259 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2260 | } |
| 2261 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2262 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 2263 | .atomic_write_len = PAGE_SIZE, |
| 2264 | .write = cgroup_file_write, |
| 2265 | .seq_show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2266 | }; |
| 2267 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2268 | static struct kernfs_ops cgroup_kf_ops = { |
| 2269 | .atomic_write_len = PAGE_SIZE, |
| 2270 | .write = cgroup_file_write, |
| 2271 | .seq_start = cgroup_seqfile_start, |
| 2272 | .seq_next = cgroup_seqfile_next, |
| 2273 | .seq_stop = cgroup_seqfile_stop, |
| 2274 | .seq_show = cgroup_seqfile_show, |
| 2275 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2276 | |
| 2277 | /* |
| 2278 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2279 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2280 | static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent, |
| 2281 | const char *new_name_str) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2282 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2283 | struct cgroup *cgrp = kn->priv; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2284 | int ret; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2285 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2286 | if (kernfs_type(kn) != KERNFS_DIR) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2287 | return -ENOTDIR; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2288 | if (kn->parent != new_parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2289 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2290 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2291 | /* |
| 2292 | * This isn't a proper migration and its usefulness is very |
| 2293 | * limited. Disallow if sane_behavior. |
| 2294 | */ |
| 2295 | if (cgroup_sane_behavior(cgrp)) |
| 2296 | return -EPERM; |
| 2297 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2298 | mutex_lock(&cgroup_tree_mutex); |
| 2299 | mutex_lock(&cgroup_mutex); |
| 2300 | |
| 2301 | ret = kernfs_rename(kn, new_parent, new_name_str); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2302 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2303 | mutex_unlock(&cgroup_mutex); |
| 2304 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2305 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2308 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2309 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2310 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2311 | struct kernfs_node *kn; |
| 2312 | struct lock_class_key *key = NULL; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2313 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2314 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 2315 | key = &cft->lockdep_key; |
| 2316 | #endif |
| 2317 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
| 2318 | cgroup_file_mode(cft), 0, cft->kf_ops, cft, |
| 2319 | NULL, false, key); |
Fengguang Wu | 430af8ad | 2014-02-13 16:42:43 -0500 | [diff] [blame] | 2320 | return PTR_ERR_OR_ZERO(kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2323 | /** |
| 2324 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2325 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2326 | * @cfts: array of cftypes to be added |
| 2327 | * @is_add: whether to add or remove |
| 2328 | * |
| 2329 | * 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] | 2330 | * For removals, this function never fails. If addition fails, this |
| 2331 | * function doesn't remove files already added. The caller is responsible |
| 2332 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2333 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2334 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2335 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2336 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2337 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2338 | int ret; |
| 2339 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2340 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2341 | |
| 2342 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2343 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2344 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2345 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2346 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2347 | continue; |
| 2348 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2349 | continue; |
| 2350 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2351 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2352 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2353 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2354 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2355 | cft->name, ret); |
| 2356 | return ret; |
| 2357 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2358 | } else { |
| 2359 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2360 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2361 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2362 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2363 | } |
| 2364 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2365 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2366 | { |
| 2367 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2368 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2369 | struct cgroup *root = &ss->root->top_cgroup; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2370 | struct cgroup_subsys_state *css; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2371 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2372 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2373 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2374 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2375 | /* don't bother if @ss isn't attached */ |
| 2376 | if (ss->root == &cgroup_dummy_root) |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2377 | return 0; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2378 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2379 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2380 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2381 | struct cgroup *cgrp = css->cgroup; |
| 2382 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2383 | if (cgroup_is_dead(cgrp)) |
| 2384 | continue; |
| 2385 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2386 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2387 | if (ret) |
| 2388 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2389 | } |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2390 | |
| 2391 | if (is_add && !ret) |
| 2392 | kernfs_activate(root->kn); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2393 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2394 | } |
| 2395 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2396 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 2397 | { |
| 2398 | struct cftype *cft; |
| 2399 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2400 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2401 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 2402 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 2403 | kfree(cft->kf_ops); |
| 2404 | cft->kf_ops = NULL; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2405 | cft->ss = NULL; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2406 | } |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2407 | } |
| 2408 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2409 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2410 | { |
| 2411 | struct cftype *cft; |
| 2412 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2413 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2414 | struct kernfs_ops *kf_ops; |
| 2415 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2416 | WARN_ON(cft->ss || cft->kf_ops); |
| 2417 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2418 | if (cft->seq_start) |
| 2419 | kf_ops = &cgroup_kf_ops; |
| 2420 | else |
| 2421 | kf_ops = &cgroup_kf_single_ops; |
| 2422 | |
| 2423 | /* |
| 2424 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 2425 | * make a copy of kf_ops to set its atomic_write_len. |
| 2426 | */ |
| 2427 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 2428 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 2429 | if (!kf_ops) { |
| 2430 | cgroup_exit_cftypes(cfts); |
| 2431 | return -ENOMEM; |
| 2432 | } |
| 2433 | kf_ops->atomic_write_len = cft->max_write_len; |
| 2434 | } |
| 2435 | |
| 2436 | cft->kf_ops = kf_ops; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2437 | cft->ss = ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | return 0; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2441 | } |
| 2442 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2443 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 2444 | { |
| 2445 | lockdep_assert_held(&cgroup_tree_mutex); |
| 2446 | |
| 2447 | if (!cfts || !cfts[0].ss) |
| 2448 | return -ENOENT; |
| 2449 | |
| 2450 | list_del(&cfts->node); |
| 2451 | cgroup_apply_cftypes(cfts, false); |
| 2452 | cgroup_exit_cftypes(cfts); |
| 2453 | return 0; |
| 2454 | } |
| 2455 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2456 | /** |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2457 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2458 | * @cfts: zero-length name terminated array of cftypes |
| 2459 | * |
| 2460 | * Unregister @cfts. Files described by @cfts are removed from all |
| 2461 | * existing cgroups and all future cgroups won't have them either. This |
| 2462 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 2463 | * |
| 2464 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2465 | * registered. |
| 2466 | */ |
| 2467 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 2468 | { |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2469 | int ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2470 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2471 | mutex_lock(&cgroup_tree_mutex); |
| 2472 | ret = cgroup_rm_cftypes_locked(cfts); |
| 2473 | mutex_unlock(&cgroup_tree_mutex); |
| 2474 | return ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | /** |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2478 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2479 | * @ss: target cgroup subsystem |
| 2480 | * @cfts: zero-length name terminated array of cftypes |
| 2481 | * |
| 2482 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2483 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2484 | * have them too. This function can be called anytime whether @ss is |
| 2485 | * attached or not. |
| 2486 | * |
| 2487 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2488 | * function currently returns 0 as long as @cfts registration is successful |
| 2489 | * even if some file creation attempts on existing cgroups fail. |
| 2490 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2491 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2492 | { |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2493 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2494 | |
Li Zefan | dc5736e | 2014-02-17 10:41:50 +0800 | [diff] [blame] | 2495 | if (!cfts || cfts[0].name[0] == '\0') |
| 2496 | return 0; |
| 2497 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2498 | ret = cgroup_init_cftypes(ss, cfts); |
| 2499 | if (ret) |
| 2500 | return ret; |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2501 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2502 | mutex_lock(&cgroup_tree_mutex); |
| 2503 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2504 | list_add_tail(&cfts->node, &ss->cfts); |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2505 | ret = cgroup_apply_cftypes(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2506 | if (ret) |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2507 | cgroup_rm_cftypes_locked(cfts); |
| 2508 | |
| 2509 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2510 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2511 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2512 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2513 | /** |
| 2514 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2515 | * @cgrp: the cgroup in question |
| 2516 | * |
| 2517 | * Return the number of tasks in the cgroup. |
| 2518 | */ |
Tejun Heo | 07bc356 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 2519 | static int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2520 | { |
| 2521 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2522 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2523 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2524 | down_read(&css_set_rwsem); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2525 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2526 | count += atomic_read(&link->cset->refcount); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2527 | up_read(&css_set_rwsem); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2528 | return count; |
| 2529 | } |
| 2530 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2531 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2532 | * css_next_child - find the next child of a given css |
| 2533 | * @pos_css: the current position (%NULL to initiate traversal) |
| 2534 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2535 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2536 | * 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] | 2537 | * under either cgroup_mutex or RCU read lock. The only requirement is |
| 2538 | * that @parent_css and @pos_css are accessible. The next sibling is |
| 2539 | * guaranteed to be returned regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2540 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2541 | struct cgroup_subsys_state * |
| 2542 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 2543 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2544 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2545 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 2546 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2547 | struct cgroup *next; |
| 2548 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2549 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2550 | |
| 2551 | /* |
| 2552 | * @pos could already have been removed. Once a cgroup is removed, |
| 2553 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 2554 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 2555 | * before the cgroup is taken off the ->sibling list, if we see it |
| 2556 | * unasserted, it's guaranteed that the next sibling hasn't |
| 2557 | * finished its grace period even if it's already removed, and thus |
| 2558 | * safe to dereference from this RCU critical section. If |
| 2559 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 2560 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2561 | * |
| 2562 | * If @pos is dead, its next pointer can't be dereferenced; |
| 2563 | * however, as each cgroup is given a monotonically increasing |
| 2564 | * unique serial number and always appended to the sibling list, |
| 2565 | * the next one can be found by walking the parent's children until |
| 2566 | * we see a cgroup with higher serial number than @pos's. While |
| 2567 | * this path can be slower, it's taken only when either the current |
| 2568 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2569 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2570 | if (!pos) { |
| 2571 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 2572 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2573 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2574 | } else { |
| 2575 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 2576 | if (next->serial_nr > pos->serial_nr) |
| 2577 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2578 | } |
| 2579 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2580 | if (&next->sibling == &cgrp->children) |
| 2581 | return NULL; |
| 2582 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2583 | return cgroup_css(next, parent_css->ss); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2584 | } |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2585 | |
| 2586 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2587 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2588 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2589 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2590 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2591 | * 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] | 2592 | * to visit for pre-order traversal of @root's descendants. @root is |
| 2593 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2594 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2595 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2596 | * doesn't require the whole traversal to be contained in a single critical |
| 2597 | * section. This function will return the correct next descendant as long |
| 2598 | * 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] | 2599 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2600 | struct cgroup_subsys_state * |
| 2601 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 2602 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2603 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2604 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2605 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2606 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2607 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2608 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2609 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2610 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2611 | |
| 2612 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2613 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2614 | if (next) |
| 2615 | return next; |
| 2616 | |
| 2617 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2618 | while (pos != root) { |
| 2619 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2620 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2621 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2622 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2623 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2624 | |
| 2625 | return NULL; |
| 2626 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2627 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2628 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2629 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 2630 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2631 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2632 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 2633 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2634 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2635 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2636 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2637 | * doesn't require the whole traversal to be contained in a single critical |
| 2638 | * section. This function will return the correct rightmost descendant as |
| 2639 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2640 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2641 | struct cgroup_subsys_state * |
| 2642 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2643 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2644 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2645 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2646 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2647 | |
| 2648 | do { |
| 2649 | last = pos; |
| 2650 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 2651 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2652 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2653 | pos = tmp; |
| 2654 | } while (pos); |
| 2655 | |
| 2656 | return last; |
| 2657 | } |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2658 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2659 | static struct cgroup_subsys_state * |
| 2660 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2661 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2662 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2663 | |
| 2664 | do { |
| 2665 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2666 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2667 | } while (pos); |
| 2668 | |
| 2669 | return last; |
| 2670 | } |
| 2671 | |
| 2672 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2673 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2674 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2675 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2676 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2677 | * 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] | 2678 | * to visit for post-order traversal of @root's descendants. @root is |
| 2679 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2680 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2681 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2682 | * doesn't require the whole traversal to be contained in a single critical |
| 2683 | * section. This function will return the correct next descendant as long |
| 2684 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 2685 | * @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2686 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2687 | struct cgroup_subsys_state * |
| 2688 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 2689 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2690 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2691 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2692 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2693 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2694 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 2695 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 2696 | if (!pos) |
| 2697 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2698 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2699 | /* if we visited @root, we're done */ |
| 2700 | if (pos == root) |
| 2701 | return NULL; |
| 2702 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2703 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2704 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2705 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2706 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2707 | |
| 2708 | /* no sibling left, visit parent */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2709 | return css_parent(pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2710 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2711 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2712 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2713 | * 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] | 2714 | * @it: the iterator to advance |
| 2715 | * |
| 2716 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2717 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2718 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2719 | { |
| 2720 | struct list_head *l = it->cset_link; |
| 2721 | struct cgrp_cset_link *link; |
| 2722 | struct css_set *cset; |
| 2723 | |
| 2724 | /* Advance to the next non-empty css_set */ |
| 2725 | do { |
| 2726 | l = l->next; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2727 | if (l == &it->origin_css->cgroup->cset_links) { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2728 | it->cset_link = NULL; |
| 2729 | return; |
| 2730 | } |
| 2731 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 2732 | cset = link->cset; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2733 | } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks)); |
| 2734 | |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2735 | it->cset_link = l; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2736 | |
| 2737 | if (!list_empty(&cset->tasks)) |
| 2738 | it->task = cset->tasks.next; |
| 2739 | else |
| 2740 | it->task = cset->mg_tasks.next; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2741 | } |
| 2742 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2743 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2744 | * css_task_iter_start - initiate task iteration |
| 2745 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2746 | * @it: the task iterator to use |
| 2747 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2748 | * Initiate iteration through the tasks of @css. The caller can call |
| 2749 | * css_task_iter_next() to walk through the tasks until the function |
| 2750 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 2751 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2752 | * |
| 2753 | * Note that this function acquires a lock which is released when the |
| 2754 | * iteration finishes. The caller can't sleep while iteration is in |
| 2755 | * progress. |
| 2756 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2757 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 2758 | struct css_task_iter *it) |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2759 | __acquires(css_set_rwsem) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2760 | { |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 2761 | /* no one should try to iterate before mounting cgroups */ |
| 2762 | WARN_ON_ONCE(!use_task_css_set_links); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2763 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2764 | down_read(&css_set_rwsem); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2765 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2766 | it->origin_css = css; |
| 2767 | it->cset_link = &css->cgroup->cset_links; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2768 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2769 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2772 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2773 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2774 | * @it: the task iterator being iterated |
| 2775 | * |
| 2776 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2777 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 2778 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2779 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2780 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2781 | { |
| 2782 | struct task_struct *res; |
| 2783 | struct list_head *l = it->task; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2784 | struct cgrp_cset_link *link = list_entry(it->cset_link, |
| 2785 | struct cgrp_cset_link, cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2786 | |
| 2787 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2788 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2789 | return NULL; |
| 2790 | res = list_entry(l, struct task_struct, cg_list); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2791 | |
| 2792 | /* |
| 2793 | * Advance iterator to find next entry. cset->tasks is consumed |
| 2794 | * first and then ->mg_tasks. After ->mg_tasks, we move onto the |
| 2795 | * next cset. |
| 2796 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2797 | l = l->next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2798 | |
| 2799 | if (l == &link->cset->tasks) |
| 2800 | l = link->cset->mg_tasks.next; |
| 2801 | |
| 2802 | if (l == &link->cset->mg_tasks) |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2803 | css_advance_task_iter(it); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2804 | else |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2805 | it->task = l; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2806 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2807 | return res; |
| 2808 | } |
| 2809 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2810 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2811 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2812 | * @it: the task iterator to finish |
| 2813 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2814 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2815 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2816 | void css_task_iter_end(struct css_task_iter *it) |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2817 | __releases(css_set_rwsem) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2818 | { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2819 | up_read(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2820 | } |
| 2821 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 2822 | /** |
| 2823 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 2824 | * @to: cgroup to which the tasks will be moved |
| 2825 | * @from: cgroup in which the tasks currently reside |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 2826 | * |
| 2827 | * Locking rules between cgroup_post_fork() and the migration path |
| 2828 | * guarantee that, if a task is forking while being migrated, the new child |
| 2829 | * is guaranteed to be either visible in the source cgroup after the |
| 2830 | * parent's migration is complete or put into the target cgroup. No task |
| 2831 | * can slip out of migration through forking. |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 2832 | */ |
| 2833 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 2834 | { |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 2835 | struct css_task_iter it; |
| 2836 | struct task_struct *task; |
| 2837 | int ret = 0; |
| 2838 | |
| 2839 | do { |
| 2840 | css_task_iter_start(&from->dummy_css, &it); |
| 2841 | task = css_task_iter_next(&it); |
| 2842 | if (task) |
| 2843 | get_task_struct(task); |
| 2844 | css_task_iter_end(&it); |
| 2845 | |
| 2846 | if (task) { |
| 2847 | mutex_lock(&cgroup_mutex); |
| 2848 | ret = cgroup_attach_task(to, task, false); |
| 2849 | mutex_unlock(&cgroup_mutex); |
| 2850 | put_task_struct(task); |
| 2851 | } |
| 2852 | } while (task && !ret); |
| 2853 | |
| 2854 | return ret; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 2855 | } |
| 2856 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2857 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2858 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2859 | * |
| 2860 | * Reading this file can return large amounts of data if a cgroup has |
| 2861 | * *lots* of attached tasks. So it may need several calls to read(), |
| 2862 | * but we cannot guarantee that the information we produce is correct |
| 2863 | * unless we produce it entirely atomically. |
| 2864 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2865 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2866 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 2867 | /* which pidlist file are we talking about? */ |
| 2868 | enum cgroup_filetype { |
| 2869 | CGROUP_FILE_PROCS, |
| 2870 | CGROUP_FILE_TASKS, |
| 2871 | }; |
| 2872 | |
| 2873 | /* |
| 2874 | * A pidlist is a list of pids that virtually represents the contents of one |
| 2875 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 2876 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 2877 | * to the cgroup. |
| 2878 | */ |
| 2879 | struct cgroup_pidlist { |
| 2880 | /* |
| 2881 | * used to find which pidlist is wanted. doesn't change as long as |
| 2882 | * this particular list stays in the list. |
| 2883 | */ |
| 2884 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 2885 | /* array of xids */ |
| 2886 | pid_t *list; |
| 2887 | /* how many elements the above list has */ |
| 2888 | int length; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 2889 | /* each of these stored in a list by its cgroup */ |
| 2890 | struct list_head links; |
| 2891 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 2892 | struct cgroup *owner; |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 2893 | /* for delayed destruction */ |
| 2894 | struct delayed_work destroy_dwork; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 2895 | }; |
| 2896 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2897 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 2898 | * The following two functions "fix" the issue where there are more pids |
| 2899 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 2900 | * TODO: replace with a kernel-wide solution to this problem |
| 2901 | */ |
| 2902 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 2903 | static void *pidlist_allocate(int count) |
| 2904 | { |
| 2905 | if (PIDLIST_TOO_LARGE(count)) |
| 2906 | return vmalloc(count * sizeof(pid_t)); |
| 2907 | else |
| 2908 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 2909 | } |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 2910 | |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 2911 | static void pidlist_free(void *p) |
| 2912 | { |
| 2913 | if (is_vmalloc_addr(p)) |
| 2914 | vfree(p); |
| 2915 | else |
| 2916 | kfree(p); |
| 2917 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 2918 | |
| 2919 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 2920 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 2921 | * should be left afterwards. |
| 2922 | */ |
| 2923 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 2924 | { |
| 2925 | struct cgroup_pidlist *l, *tmp_l; |
| 2926 | |
| 2927 | mutex_lock(&cgrp->pidlist_mutex); |
| 2928 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 2929 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 2930 | mutex_unlock(&cgrp->pidlist_mutex); |
| 2931 | |
| 2932 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 2933 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 2934 | } |
| 2935 | |
| 2936 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 2937 | { |
| 2938 | struct delayed_work *dwork = to_delayed_work(work); |
| 2939 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 2940 | destroy_dwork); |
| 2941 | struct cgroup_pidlist *tofree = NULL; |
| 2942 | |
| 2943 | mutex_lock(&l->owner->pidlist_mutex); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 2944 | |
| 2945 | /* |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 2946 | * Destroy iff we didn't get queued again. The state won't change |
| 2947 | * as destroy_dwork can only be queued while locked. |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 2948 | */ |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 2949 | if (!delayed_work_pending(dwork)) { |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 2950 | list_del(&l->links); |
| 2951 | pidlist_free(l->list); |
| 2952 | put_pid_ns(l->key.ns); |
| 2953 | tofree = l; |
| 2954 | } |
| 2955 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 2956 | mutex_unlock(&l->owner->pidlist_mutex); |
| 2957 | kfree(tofree); |
| 2958 | } |
| 2959 | |
| 2960 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2961 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2962 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2963 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2964 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2965 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2966 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2967 | |
| 2968 | /* |
| 2969 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 2970 | * edge cases first; no work needs to be done for either |
| 2971 | */ |
| 2972 | if (length == 0 || length == 1) |
| 2973 | return length; |
| 2974 | /* src and dest walk down the list; dest counts unique elements */ |
| 2975 | for (src = 1; src < length; src++) { |
| 2976 | /* find next unique element */ |
| 2977 | while (list[src] == list[src-1]) { |
| 2978 | src++; |
| 2979 | if (src == length) |
| 2980 | goto after; |
| 2981 | } |
| 2982 | /* dest always points to where the next unique element goes */ |
| 2983 | list[dest] = list[src]; |
| 2984 | dest++; |
| 2985 | } |
| 2986 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2987 | return dest; |
| 2988 | } |
| 2989 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 2990 | /* |
| 2991 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 2992 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 2993 | * different per namespace, each namespace needs differently sorted list, |
| 2994 | * making it impossible to use, for example, single rbtree of member tasks |
| 2995 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 2996 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 2997 | * pidlists keyed by cgroup and namespace. |
| 2998 | * |
| 2999 | * All this extra complexity was caused by the original implementation |
| 3000 | * committing to an entirely unnecessary property. In the long term, we |
| 3001 | * want to do away with it. Explicitly scramble sort order if |
| 3002 | * sane_behavior so that no such expectation exists in the new interface. |
| 3003 | * |
| 3004 | * Scrambling is done by swapping every two consecutive bits, which is |
| 3005 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 3006 | */ |
| 3007 | static pid_t pid_fry(pid_t pid) |
| 3008 | { |
| 3009 | unsigned a = pid & 0x55555555; |
| 3010 | unsigned b = pid & 0xAAAAAAAA; |
| 3011 | |
| 3012 | return (a << 1) | (b >> 1); |
| 3013 | } |
| 3014 | |
| 3015 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 3016 | { |
| 3017 | if (cgroup_sane_behavior(cgrp)) |
| 3018 | return pid_fry(pid); |
| 3019 | else |
| 3020 | return pid; |
| 3021 | } |
| 3022 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3023 | static int cmppid(const void *a, const void *b) |
| 3024 | { |
| 3025 | return *(pid_t *)a - *(pid_t *)b; |
| 3026 | } |
| 3027 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3028 | static int fried_cmppid(const void *a, const void *b) |
| 3029 | { |
| 3030 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 3031 | } |
| 3032 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3033 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3034 | enum cgroup_filetype type) |
| 3035 | { |
| 3036 | struct cgroup_pidlist *l; |
| 3037 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3038 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3039 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3040 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3041 | |
| 3042 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 3043 | if (l->key.type == type && l->key.ns == ns) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3044 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3045 | return NULL; |
| 3046 | } |
| 3047 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3048 | /* |
| 3049 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3050 | * returns with the lock on that pidlist already held, and takes care |
| 3051 | * of the use count, or returns NULL with no locks held if we're out of |
| 3052 | * memory. |
| 3053 | */ |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3054 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 3055 | enum cgroup_filetype type) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3056 | { |
| 3057 | struct cgroup_pidlist *l; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3058 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3059 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3060 | |
| 3061 | l = cgroup_pidlist_find(cgrp, type); |
| 3062 | if (l) |
| 3063 | return l; |
| 3064 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3065 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3066 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3067 | if (!l) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3068 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3069 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3070 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3071 | l->key.type = type; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3072 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 3073 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3074 | l->owner = cgrp; |
| 3075 | list_add(&l->links, &cgrp->pidlists); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3076 | return l; |
| 3077 | } |
| 3078 | |
| 3079 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3080 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3081 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3082 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3083 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3084 | { |
| 3085 | pid_t *array; |
| 3086 | int length; |
| 3087 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3088 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3089 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3090 | struct cgroup_pidlist *l; |
| 3091 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3092 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3093 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3094 | /* |
| 3095 | * If cgroup gets more users after we read count, we won't have |
| 3096 | * enough space - tough. This race is indistinguishable to the |
| 3097 | * caller from the case that the additional cgroup users didn't |
| 3098 | * show up until sometime later on. |
| 3099 | */ |
| 3100 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3101 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3102 | if (!array) |
| 3103 | return -ENOMEM; |
| 3104 | /* now, populate the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3105 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3106 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3107 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3108 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3109 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3110 | if (type == CGROUP_FILE_PROCS) |
| 3111 | pid = task_tgid_vnr(tsk); |
| 3112 | else |
| 3113 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3114 | if (pid > 0) /* make sure to only use valid results */ |
| 3115 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3116 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3117 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3118 | length = n; |
| 3119 | /* now sort & (if procs) strip out duplicates */ |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3120 | if (cgroup_sane_behavior(cgrp)) |
| 3121 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 3122 | else |
| 3123 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3124 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3125 | length = pidlist_uniq(array, length); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3126 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3127 | l = cgroup_pidlist_find_create(cgrp, type); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3128 | if (!l) { |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3129 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3130 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3131 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3132 | } |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3133 | |
| 3134 | /* store array, freeing old if necessary */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3135 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3136 | l->list = array; |
| 3137 | l->length = length; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3138 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3139 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3140 | } |
| 3141 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3142 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3143 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3144 | * @stats: cgroupstats to fill information into |
| 3145 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3146 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3147 | * |
| 3148 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3149 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3150 | */ |
| 3151 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3152 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3153 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3154 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3155 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3156 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3157 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3158 | /* it should be kernfs_node belonging to cgroupfs and is a directory */ |
| 3159 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 3160 | kernfs_type(kn) != KERNFS_DIR) |
| 3161 | return -EINVAL; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3162 | |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3163 | mutex_lock(&cgroup_mutex); |
| 3164 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3165 | /* |
| 3166 | * We aren't being called from kernfs and there's no guarantee on |
| 3167 | * @kn->priv's validity. For this and css_tryget_from_dir(), |
| 3168 | * @kn->priv is RCU safe. Let's do the RCU dancing. |
| 3169 | */ |
| 3170 | rcu_read_lock(); |
| 3171 | cgrp = rcu_dereference(kn->priv); |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3172 | if (!cgrp || cgroup_is_dead(cgrp)) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3173 | rcu_read_unlock(); |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3174 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3175 | return -ENOENT; |
| 3176 | } |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3177 | rcu_read_unlock(); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3178 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3179 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3180 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3181 | switch (tsk->state) { |
| 3182 | case TASK_RUNNING: |
| 3183 | stats->nr_running++; |
| 3184 | break; |
| 3185 | case TASK_INTERRUPTIBLE: |
| 3186 | stats->nr_sleeping++; |
| 3187 | break; |
| 3188 | case TASK_UNINTERRUPTIBLE: |
| 3189 | stats->nr_uninterruptible++; |
| 3190 | break; |
| 3191 | case TASK_STOPPED: |
| 3192 | stats->nr_stopped++; |
| 3193 | break; |
| 3194 | default: |
| 3195 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3196 | stats->nr_io_wait++; |
| 3197 | break; |
| 3198 | } |
| 3199 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3200 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3201 | |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3202 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3203 | return 0; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3204 | } |
| 3205 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3206 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3207 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3208 | * 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] | 3209 | * 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] | 3210 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3211 | */ |
| 3212 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3213 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3214 | { |
| 3215 | /* |
| 3216 | * Initially we receive a position value that corresponds to |
| 3217 | * one more than the last pid shown (or 0 on the first call or |
| 3218 | * after a seek to the start). Use a binary-search to find the |
| 3219 | * next pid to display, if any |
| 3220 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3221 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3222 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3223 | struct cgroup_pidlist *l; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3224 | enum cgroup_filetype type = seq_cft(s)->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3225 | int index = 0, pid = *pos; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3226 | int *iter, ret; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3227 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3228 | mutex_lock(&cgrp->pidlist_mutex); |
| 3229 | |
| 3230 | /* |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3231 | * !NULL @of->priv indicates that this isn't the first start() |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3232 | * after open. If the matching pidlist is around, we can use that. |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3233 | * 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] | 3234 | * could already have been destroyed. |
| 3235 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3236 | if (of->priv) |
| 3237 | of->priv = cgroup_pidlist_find(cgrp, type); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3238 | |
| 3239 | /* |
| 3240 | * Either this is the first start() after open or the matching |
| 3241 | * pidlist has been destroyed inbetween. Create a new one. |
| 3242 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3243 | if (!of->priv) { |
| 3244 | ret = pidlist_array_load(cgrp, type, |
| 3245 | (struct cgroup_pidlist **)&of->priv); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3246 | if (ret) |
| 3247 | return ERR_PTR(ret); |
| 3248 | } |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3249 | l = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3250 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3251 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3252 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3253 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3254 | while (index < end) { |
| 3255 | int mid = (index + end) / 2; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3256 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3257 | index = mid; |
| 3258 | break; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3259 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3260 | index = mid + 1; |
| 3261 | else |
| 3262 | end = mid; |
| 3263 | } |
| 3264 | } |
| 3265 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3266 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3267 | return NULL; |
| 3268 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3269 | iter = l->list + index; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3270 | *pos = cgroup_pid_fry(cgrp, *iter); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3271 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3272 | } |
| 3273 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3274 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3275 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3276 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3277 | struct cgroup_pidlist *l = of->priv; |
Tejun Heo | 6223685 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3278 | |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3279 | if (l) |
| 3280 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3281 | CGROUP_PIDLIST_DESTROY_DELAY); |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3282 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3283 | } |
| 3284 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3285 | 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] | 3286 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3287 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3288 | struct cgroup_pidlist *l = of->priv; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3289 | pid_t *p = v; |
| 3290 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3291 | /* |
| 3292 | * Advance to the next pid in the array. If this goes off the |
| 3293 | * end, we're done |
| 3294 | */ |
| 3295 | p++; |
| 3296 | if (p >= end) { |
| 3297 | return NULL; |
| 3298 | } else { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3299 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3300 | return p; |
| 3301 | } |
| 3302 | } |
| 3303 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3304 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3305 | { |
| 3306 | return seq_printf(s, "%d\n", *(int *)v); |
| 3307 | } |
| 3308 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3309 | /* |
| 3310 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3311 | * independent of whether it's tasks or procs |
| 3312 | */ |
| 3313 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3314 | .start = cgroup_pidlist_start, |
| 3315 | .stop = cgroup_pidlist_stop, |
| 3316 | .next = cgroup_pidlist_next, |
| 3317 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3318 | }; |
| 3319 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3320 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3321 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3322 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3323 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3324 | } |
| 3325 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3326 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3327 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3328 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3329 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3330 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3331 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3332 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3333 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3334 | return 0; |
| 3335 | } |
| 3336 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3337 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 3338 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3339 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3340 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3341 | } |
| 3342 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3343 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 3344 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3345 | { |
| 3346 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3347 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3348 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3349 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3350 | return 0; |
| 3351 | } |
| 3352 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3353 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3354 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3355 | .name = "cgroup.procs", |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3356 | .seq_start = cgroup_pidlist_start, |
| 3357 | .seq_next = cgroup_pidlist_next, |
| 3358 | .seq_stop = cgroup_pidlist_stop, |
| 3359 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3360 | .private = CGROUP_FILE_PROCS, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3361 | .write_u64 = cgroup_procs_write, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3362 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3363 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3364 | { |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3365 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3366 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3367 | .read_u64 = cgroup_clone_children_read, |
| 3368 | .write_u64 = cgroup_clone_children_write, |
| 3369 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3370 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3371 | .name = "cgroup.sane_behavior", |
| 3372 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3373 | .seq_show = cgroup_sane_behavior_show, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3374 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3375 | |
| 3376 | /* |
| 3377 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 3378 | * don't exist if sane_behavior. If you're depending on these, be |
| 3379 | * prepared to be burned. |
| 3380 | */ |
| 3381 | { |
| 3382 | .name = "tasks", |
| 3383 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3384 | .seq_start = cgroup_pidlist_start, |
| 3385 | .seq_next = cgroup_pidlist_next, |
| 3386 | .seq_stop = cgroup_pidlist_stop, |
| 3387 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3388 | .private = CGROUP_FILE_TASKS, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3389 | .write_u64 = cgroup_tasks_write, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3390 | .mode = S_IRUGO | S_IWUSR, |
| 3391 | }, |
| 3392 | { |
| 3393 | .name = "notify_on_release", |
| 3394 | .flags = CFTYPE_INSANE, |
| 3395 | .read_u64 = cgroup_read_notify_on_release, |
| 3396 | .write_u64 = cgroup_write_notify_on_release, |
| 3397 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3398 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3399 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 3400 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3401 | .seq_show = cgroup_release_agent_show, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3402 | .write_string = cgroup_release_agent_write, |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3403 | .max_write_len = PATH_MAX - 1, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3404 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3405 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3406 | }; |
| 3407 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3408 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3409 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3410 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3411 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3412 | * |
| 3413 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3414 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3415 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3416 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3417 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3418 | int i, ret = 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3419 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3420 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3421 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3422 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3423 | |
| 3424 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3425 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3426 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3427 | list_for_each_entry(cfts, &ss->cfts, node) { |
| 3428 | ret = cgroup_addrm_files(cgrp, cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3429 | if (ret < 0) |
| 3430 | goto err; |
| 3431 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3432 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3433 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3434 | err: |
| 3435 | cgroup_clear_dir(cgrp, subsys_mask); |
| 3436 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3437 | } |
| 3438 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3439 | /* |
| 3440 | * css destruction is four-stage process. |
| 3441 | * |
| 3442 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 3443 | * Implemented in kill_css(). |
| 3444 | * |
| 3445 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
| 3446 | * and thus css_tryget() is guaranteed to fail, the css can be offlined |
| 3447 | * by invoking offline_css(). After offlining, the base ref is put. |
| 3448 | * Implemented in css_killed_work_fn(). |
| 3449 | * |
| 3450 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 3451 | * accessors are inside RCU read sections. css_release() schedules the |
| 3452 | * RCU callback. |
| 3453 | * |
| 3454 | * 4. After the grace period, the css can be freed. Implemented in |
| 3455 | * css_free_work_fn(). |
| 3456 | * |
| 3457 | * It is actually hairier because both step 2 and 4 require process context |
| 3458 | * and thus involve punting to css->destroy_work adding two additional |
| 3459 | * steps to the already complex sequence. |
| 3460 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3461 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3462 | { |
| 3463 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3464 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3465 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3466 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3467 | if (css->parent) |
| 3468 | css_put(css->parent); |
| 3469 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3470 | css->ss->css_free(css); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3471 | cgroup_put(cgrp); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 3475 | { |
| 3476 | struct cgroup_subsys_state *css = |
| 3477 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 3478 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3479 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 3480 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3481 | } |
| 3482 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3483 | static void css_release(struct percpu_ref *ref) |
| 3484 | { |
| 3485 | struct cgroup_subsys_state *css = |
| 3486 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 3487 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3488 | rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3489 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3490 | } |
| 3491 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3492 | static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss, |
| 3493 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3494 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3495 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 3496 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3497 | css->flags = 0; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3498 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3499 | if (cgrp->parent) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3500 | css->parent = cgroup_css(cgrp->parent, ss); |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3501 | else |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3502 | css->flags |= CSS_ROOT; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3503 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3504 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3505 | } |
| 3506 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 3507 | /* 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] | 3508 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3509 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3510 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3511 | int ret = 0; |
| 3512 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3513 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3514 | lockdep_assert_held(&cgroup_mutex); |
| 3515 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3516 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3517 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3518 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3519 | css->flags |= CSS_ONLINE; |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3520 | css->cgroup->nr_css++; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3521 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3522 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3523 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3524 | } |
| 3525 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 3526 | /* 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] | 3527 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3528 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3529 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3530 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3531 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3532 | lockdep_assert_held(&cgroup_mutex); |
| 3533 | |
| 3534 | if (!(css->flags & CSS_ONLINE)) |
| 3535 | return; |
| 3536 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 3537 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3538 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3539 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3540 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3541 | css->cgroup->nr_css--; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3542 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3543 | } |
| 3544 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3545 | /** |
| 3546 | * create_css - create a cgroup_subsys_state |
| 3547 | * @cgrp: the cgroup new css will be associated with |
| 3548 | * @ss: the subsys of new css |
| 3549 | * |
| 3550 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
| 3551 | * css is online and installed in @cgrp with all interface files created. |
| 3552 | * Returns 0 on success, -errno on failure. |
| 3553 | */ |
| 3554 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss) |
| 3555 | { |
| 3556 | struct cgroup *parent = cgrp->parent; |
| 3557 | struct cgroup_subsys_state *css; |
| 3558 | int err; |
| 3559 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3560 | lockdep_assert_held(&cgroup_mutex); |
| 3561 | |
| 3562 | css = ss->css_alloc(cgroup_css(parent, ss)); |
| 3563 | if (IS_ERR(css)) |
| 3564 | return PTR_ERR(css); |
| 3565 | |
| 3566 | err = percpu_ref_init(&css->refcnt, css_release); |
| 3567 | if (err) |
| 3568 | goto err_free; |
| 3569 | |
| 3570 | init_css(css, ss, cgrp); |
| 3571 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3572 | err = cgroup_populate_dir(cgrp, 1 << ss->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3573 | if (err) |
| 3574 | goto err_free; |
| 3575 | |
| 3576 | err = online_css(css); |
| 3577 | if (err) |
| 3578 | goto err_free; |
| 3579 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3580 | cgroup_get(cgrp); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3581 | css_get(css->parent); |
| 3582 | |
| 3583 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 3584 | parent->parent) { |
| 3585 | 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", |
| 3586 | current->comm, current->pid, ss->name); |
| 3587 | if (!strcmp(ss->name, "memory")) |
| 3588 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 3589 | ss->warned_broken_hierarchy = true; |
| 3590 | } |
| 3591 | |
| 3592 | return 0; |
| 3593 | |
| 3594 | err_free: |
| 3595 | percpu_ref_cancel_init(&css->refcnt); |
| 3596 | ss->css_free(css); |
| 3597 | return err; |
| 3598 | } |
| 3599 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3600 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3601 | * cgroup_create - create a cgroup |
| 3602 | * @parent: cgroup that will be parent of the new cgroup |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 3603 | * @name: name of the new cgroup |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3604 | * @mode: mode to set on new cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3605 | */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 3606 | static long cgroup_create(struct cgroup *parent, const char *name, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3607 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3608 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3609 | struct cgroup *cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3610 | struct cgroupfs_root *root = parent->root; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 3611 | int ssid, err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3612 | struct cgroup_subsys *ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3613 | struct kernfs_node *kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3614 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 3615 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3616 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 3617 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3618 | return -ENOMEM; |
| 3619 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3620 | mutex_lock(&cgroup_tree_mutex); |
| 3621 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 3622 | /* |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 3623 | * Only live parents can have children. Note that the liveliness |
| 3624 | * check isn't strictly necessary because cgroup_mkdir() and |
| 3625 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 3626 | * anyway so that locking is contained inside cgroup proper and we |
| 3627 | * don't get nasty surprises if we ever grow another caller. |
| 3628 | */ |
| 3629 | if (!cgroup_lock_live_group(parent)) { |
| 3630 | err = -ENODEV; |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3631 | goto err_unlock_tree; |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | /* |
| 3635 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 3636 | * a half-baked cgroup. |
| 3637 | */ |
| 3638 | cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL); |
| 3639 | if (cgrp->id < 0) { |
| 3640 | err = -ENOMEM; |
| 3641 | goto err_unlock; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 3642 | } |
| 3643 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3644 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3645 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3646 | cgrp->parent = parent; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3647 | cgrp->dummy_css.parent = &parent->dummy_css; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3648 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3649 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 3650 | if (notify_on_release(parent)) |
| 3651 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3652 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3653 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 3654 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3655 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3656 | /* create the directory */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 3657 | kn = kernfs_create_dir(parent->kn, name, mode, cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3658 | if (IS_ERR(kn)) { |
| 3659 | err = PTR_ERR(kn); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 3660 | goto err_free_id; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3661 | } |
| 3662 | cgrp->kn = kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3663 | |
Tejun Heo | 6f30558 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 3664 | /* |
| 3665 | * This extra ref will be put in cgroup_free_fn() and guarantees |
| 3666 | * that @cgrp->kn is always accessible. |
| 3667 | */ |
| 3668 | kernfs_get(kn); |
| 3669 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 3670 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3671 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 3672 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 3673 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 3674 | atomic_inc(&root->nr_cgrps); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3675 | cgroup_get(parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 3676 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3677 | /* |
| 3678 | * @cgrp is now fully operational. If something fails after this |
| 3679 | * point, it'll be released via the normal destruction path. |
| 3680 | */ |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 3681 | idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 3682 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3683 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3684 | if (err) |
| 3685 | goto err_destroy; |
| 3686 | |
Tejun Heo | 9d403e9 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3687 | /* let's create and online css's */ |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 3688 | for_each_subsys(ss, ssid) { |
| 3689 | if (root->subsys_mask & (1 << ssid)) { |
| 3690 | err = create_css(cgrp, ss); |
| 3691 | if (err) |
| 3692 | goto err_destroy; |
| 3693 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3694 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3695 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3696 | kernfs_activate(kn); |
| 3697 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3698 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3699 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3700 | |
| 3701 | return 0; |
| 3702 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 3703 | err_free_id: |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 3704 | idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 3705 | err_unlock: |
| 3706 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3707 | err_unlock_tree: |
| 3708 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3709 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3710 | return err; |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3711 | |
| 3712 | err_destroy: |
| 3713 | cgroup_destroy_locked(cgrp); |
| 3714 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3715 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3716 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3717 | } |
| 3718 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3719 | static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, |
| 3720 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3721 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3722 | struct cgroup *parent = parent_kn->priv; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3723 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3724 | return cgroup_create(parent, name, mode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3725 | } |
| 3726 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3727 | /* |
| 3728 | * This is called when the refcnt of a css is confirmed to be killed. |
| 3729 | * css_tryget() is now guaranteed to fail. |
| 3730 | */ |
| 3731 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3732 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3733 | struct cgroup_subsys_state *css = |
| 3734 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 3735 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3736 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3737 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3738 | mutex_lock(&cgroup_mutex); |
| 3739 | |
| 3740 | /* |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3741 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 3742 | * initate destruction. |
| 3743 | */ |
| 3744 | offline_css(css); |
| 3745 | |
| 3746 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3747 | * If @cgrp is marked dead, it's waiting for refs of all css's to |
| 3748 | * be disabled before proceeding to the second phase of cgroup |
| 3749 | * destruction. If we are the last one, kick it off. |
| 3750 | */ |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3751 | if (!cgrp->nr_css && cgroup_is_dead(cgrp)) |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3752 | cgroup_destroy_css_killed(cgrp); |
| 3753 | |
| 3754 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3755 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3756 | |
| 3757 | /* |
| 3758 | * Put the css refs from kill_css(). Each css holds an extra |
| 3759 | * reference to the cgroup's dentry and cgroup removal proceeds |
| 3760 | * regardless of css refs. On the last put of each css, whenever |
| 3761 | * that may be, the extra dentry ref is put so that dentry |
| 3762 | * destruction happens only after all css's are released. |
| 3763 | */ |
| 3764 | css_put(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3765 | } |
| 3766 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3767 | /* css kill confirmation processing requires process context, bounce */ |
| 3768 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3769 | { |
| 3770 | struct cgroup_subsys_state *css = |
| 3771 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 3772 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3773 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 3774 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | /** |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3778 | * kill_css - destroy a css |
| 3779 | * @css: css to destroy |
| 3780 | * |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3781 | * This function initiates destruction of @css by removing cgroup interface |
| 3782 | * files and putting its base reference. ->css_offline() will be invoked |
| 3783 | * asynchronously once css_tryget() is guaranteed to fail and when the |
| 3784 | * reference count reaches zero, @css will be released. |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3785 | */ |
| 3786 | static void kill_css(struct cgroup_subsys_state *css) |
| 3787 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3788 | /* |
| 3789 | * This must happen before css is disassociated with its cgroup. |
| 3790 | * See seq_css() for details. |
| 3791 | */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3792 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3793 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3794 | /* |
| 3795 | * Killing would put the base ref, but we need to keep it alive |
| 3796 | * until after ->css_offline(). |
| 3797 | */ |
| 3798 | css_get(css); |
| 3799 | |
| 3800 | /* |
| 3801 | * cgroup core guarantees that, by the time ->css_offline() is |
| 3802 | * invoked, no new css reference will be given out via |
| 3803 | * css_tryget(). We can't simply call percpu_ref_kill() and |
| 3804 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 3805 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 3806 | * |
| 3807 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 3808 | * css is confirmed to be seen as killed on all CPUs. |
| 3809 | */ |
| 3810 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | /** |
| 3814 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 3815 | * @cgrp: cgroup to be destroyed |
| 3816 | * |
| 3817 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 3818 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 3819 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 3820 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 3821 | * the following two steps. |
| 3822 | * |
| 3823 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 3824 | * userland visible parts and start killing the percpu refcnts of |
| 3825 | * css's. Set up so that the next stage will be kicked off once all |
| 3826 | * the percpu refcnts are confirmed to be killed. |
| 3827 | * |
| 3828 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 3829 | * rest of destruction. Once all cgroup references are gone, the |
| 3830 | * cgroup is RCU-freed. |
| 3831 | * |
| 3832 | * This function implements s1. After this step, @cgrp is gone as far as |
| 3833 | * the userland is concerned and a new cgroup with the same name may be |
| 3834 | * created. As cgroup doesn't care about the names internally, this |
| 3835 | * doesn't cause any problem. |
| 3836 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3837 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 3838 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3839 | { |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 3840 | struct cgroup *child; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3841 | struct cgroup_subsys_state *css; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3842 | bool empty; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3843 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3844 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3845 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3846 | lockdep_assert_held(&cgroup_mutex); |
| 3847 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3848 | /* |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3849 | * css_set_rwsem synchronizes access to ->cset_links and prevents |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3850 | * @cgrp from being removed while put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3851 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3852 | down_read(&css_set_rwsem); |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 3853 | empty = list_empty(&cgrp->cset_links); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3854 | up_read(&css_set_rwsem); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3855 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3856 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 3857 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 3858 | /* |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 3859 | * Make sure there's no live children. We can't test ->children |
| 3860 | * emptiness as dead children linger on it while being destroyed; |
| 3861 | * otherwise, "rmdir parent/child parent" may fail with -EBUSY. |
| 3862 | */ |
| 3863 | empty = true; |
| 3864 | rcu_read_lock(); |
| 3865 | list_for_each_entry_rcu(child, &cgrp->children, sibling) { |
| 3866 | empty = cgroup_is_dead(child); |
| 3867 | if (!empty) |
| 3868 | break; |
| 3869 | } |
| 3870 | rcu_read_unlock(); |
| 3871 | if (!empty) |
| 3872 | return -EBUSY; |
| 3873 | |
| 3874 | /* |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3875 | * Initiate massacre of all css's. cgroup_destroy_css_killed() |
| 3876 | * will be invoked to perform the rest of destruction once the |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3877 | * percpu refs of all css's are confirmed to be killed. This |
| 3878 | * involves removing the subsystem's files, drop cgroup_mutex. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 3879 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3880 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3881 | for_each_css(css, ssid, cgrp) |
| 3882 | kill_css(css); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3883 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 3884 | |
| 3885 | /* |
| 3886 | * Mark @cgrp dead. This prevents further task migration and child |
| 3887 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3888 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 3889 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3890 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 3891 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 3892 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 3893 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 3894 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 3895 | raw_spin_lock(&release_list_lock); |
| 3896 | if (!list_empty(&cgrp->release_list)) |
| 3897 | list_del_init(&cgrp->release_list); |
| 3898 | raw_spin_unlock(&release_list_lock); |
| 3899 | |
| 3900 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3901 | * If @cgrp has css's attached, the second stage of cgroup |
| 3902 | * destruction is kicked off from css_killed_work_fn() after the |
| 3903 | * refs of all attached css's are killed. If @cgrp doesn't have |
| 3904 | * any css, we kick it off here. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 3905 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3906 | if (!cgrp->nr_css) |
| 3907 | cgroup_destroy_css_killed(cgrp); |
| 3908 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3909 | /* remove @cgrp directory along with the base files */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3910 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3911 | |
| 3912 | /* |
| 3913 | * There are two control paths which try to determine cgroup from |
| 3914 | * dentry without going through kernfs - cgroupstats_build() and |
| 3915 | * css_tryget_from_dir(). Those are supported by RCU protecting |
| 3916 | * clearing of cgrp->kn->priv backpointer, which should happen |
| 3917 | * after all files under it have been removed. |
| 3918 | */ |
Tejun Heo | 6f30558 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 3919 | kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3920 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3921 | |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3922 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 3923 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3924 | return 0; |
| 3925 | }; |
| 3926 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3927 | /** |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3928 | * cgroup_destroy_css_killed - the second step of cgroup destruction |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3929 | * @work: cgroup->destroy_free_work |
| 3930 | * |
| 3931 | * 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] | 3932 | * destroyed after all css's are offlined and performs the rest of |
| 3933 | * destruction. This is the second step of destruction described in the |
| 3934 | * comment above cgroup_destroy_locked(). |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3935 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3936 | static void cgroup_destroy_css_killed(struct cgroup *cgrp) |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3937 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3938 | struct cgroup *parent = cgrp->parent; |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3939 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3940 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3941 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3942 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 3943 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3944 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 3945 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3946 | cgroup_put(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3947 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3948 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3949 | check_for_release(parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3950 | } |
| 3951 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3952 | static int cgroup_rmdir(struct kernfs_node *kn) |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3953 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3954 | struct cgroup *cgrp = kn->priv; |
| 3955 | int ret = 0; |
| 3956 | |
| 3957 | /* |
| 3958 | * This is self-destruction but @kn can't be removed while this |
| 3959 | * callback is in progress. Let's break active protection. Once |
| 3960 | * the protection is broken, @cgrp can be destroyed at any point. |
| 3961 | * Pin it so that it stays accessible. |
| 3962 | */ |
| 3963 | cgroup_get(cgrp); |
| 3964 | kernfs_break_active_protection(kn); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3965 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3966 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3967 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3968 | |
| 3969 | /* |
| 3970 | * @cgrp might already have been destroyed while we're trying to |
| 3971 | * grab the mutexes. |
| 3972 | */ |
| 3973 | if (!cgroup_is_dead(cgrp)) |
| 3974 | ret = cgroup_destroy_locked(cgrp); |
| 3975 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3976 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3977 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3978 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3979 | kernfs_unbreak_active_protection(kn); |
| 3980 | cgroup_put(cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3981 | return ret; |
| 3982 | } |
| 3983 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3984 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
| 3985 | .remount_fs = cgroup_remount, |
| 3986 | .show_options = cgroup_show_options, |
| 3987 | .mkdir = cgroup_mkdir, |
| 3988 | .rmdir = cgroup_rmdir, |
| 3989 | .rename = cgroup_rename, |
| 3990 | }; |
| 3991 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 3992 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3993 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3994 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 3995 | |
| 3996 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3997 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3998 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 3999 | mutex_lock(&cgroup_mutex); |
| 4000 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4001 | INIT_LIST_HEAD(&ss->cfts); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4002 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4003 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4004 | ss->root = &cgroup_dummy_root; |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4005 | css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4006 | /* We don't handle early failures gracefully */ |
| 4007 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4008 | init_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4009 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4010 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4011 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4012 | * newly registered, all tasks and hence the |
| 4013 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4014 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4015 | |
| 4016 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4017 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4018 | /* At system boot, before all subsystems have been |
| 4019 | * registered, no tasks have been forked, so we don't |
| 4020 | * need to invoke fork callbacks here. */ |
| 4021 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4022 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4023 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4024 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4025 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4026 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4030 | * cgroup_init_early - cgroup initialization at system boot |
| 4031 | * |
| 4032 | * Initialize cgroups at system boot, and initialize any |
| 4033 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4034 | */ |
| 4035 | int __init cgroup_init_early(void) |
| 4036 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4037 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4038 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4039 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4040 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4041 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4042 | INIT_LIST_HEAD(&init_css_set.tasks); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4043 | INIT_LIST_HEAD(&init_css_set.mg_tasks); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4044 | INIT_LIST_HEAD(&init_css_set.mg_preload_node); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4045 | INIT_LIST_HEAD(&init_css_set.mg_node); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4046 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4047 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4048 | init_cgroup_root(&cgroup_dummy_root); |
| 4049 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4050 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4051 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4052 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4053 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4054 | 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] | 4055 | 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] | 4056 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4057 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4058 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4059 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n", |
| 4060 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4061 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4062 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 4063 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
| 4064 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4065 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4066 | ss->name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4067 | |
| 4068 | if (ss->early_init) |
| 4069 | cgroup_init_subsys(ss); |
| 4070 | } |
| 4071 | return 0; |
| 4072 | } |
| 4073 | |
| 4074 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4075 | * cgroup_init - cgroup initialization |
| 4076 | * |
| 4077 | * Register cgroup filesystem and /proc file, and initialize |
| 4078 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4079 | */ |
| 4080 | int __init cgroup_init(void) |
| 4081 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4082 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4083 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4084 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4085 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4086 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4087 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4088 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4089 | if (!ss->early_init) |
| 4090 | cgroup_init_subsys(ss); |
Tejun Heo | de00ffa | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4091 | |
| 4092 | /* |
| 4093 | * cftype registration needs kmalloc and can't be done |
| 4094 | * during early_init. Register base cftypes separately. |
| 4095 | */ |
| 4096 | if (ss->base_cftypes) |
| 4097 | WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4098 | } |
| 4099 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4100 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4101 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4102 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4103 | /* Add init_css_set to the hash table */ |
| 4104 | key = css_set_hash(init_css_set.subsys); |
| 4105 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4106 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4107 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4108 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4109 | err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top, |
| 4110 | 0, 1, GFP_KERNEL); |
| 4111 | BUG_ON(err < 0); |
| 4112 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4113 | mutex_unlock(&cgroup_mutex); |
| 4114 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4115 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4116 | if (!cgroup_kobj) |
| 4117 | return -ENOMEM; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4118 | |
| 4119 | err = register_filesystem(&cgroup_fs_type); |
| 4120 | if (err < 0) { |
| 4121 | kobject_put(cgroup_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4122 | return err; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4123 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4124 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4125 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4126 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4127 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4128 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4129 | static int __init cgroup_wq_init(void) |
| 4130 | { |
| 4131 | /* |
| 4132 | * There isn't much point in executing destruction path in |
| 4133 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 4134 | * Use 1 for @max_active. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4135 | * |
| 4136 | * We would prefer to do this in cgroup_init() above, but that |
| 4137 | * is called before init_workqueues(): so leave this until after. |
| 4138 | */ |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 4139 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4140 | BUG_ON(!cgroup_destroy_wq); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 4141 | |
| 4142 | /* |
| 4143 | * Used to destroy pidlists and separate to serve as flush domain. |
| 4144 | * Cap @max_active to 1 too. |
| 4145 | */ |
| 4146 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 4147 | 0, 1); |
| 4148 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 4149 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4150 | return 0; |
| 4151 | } |
| 4152 | core_initcall(cgroup_wq_init); |
| 4153 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4154 | /* |
| 4155 | * proc_cgroup_show() |
| 4156 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4157 | * - Used for /proc/<pid>/cgroup. |
| 4158 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4159 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4160 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4161 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4162 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4163 | * cgroup to top_cgroup. |
| 4164 | */ |
| 4165 | |
| 4166 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4167 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4168 | { |
| 4169 | struct pid *pid; |
| 4170 | struct task_struct *tsk; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4171 | char *buf, *path; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4172 | int retval; |
| 4173 | struct cgroupfs_root *root; |
| 4174 | |
| 4175 | retval = -ENOMEM; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4176 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4177 | if (!buf) |
| 4178 | goto out; |
| 4179 | |
| 4180 | retval = -ESRCH; |
| 4181 | pid = m->private; |
| 4182 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4183 | if (!tsk) |
| 4184 | goto out_free; |
| 4185 | |
| 4186 | retval = 0; |
| 4187 | |
| 4188 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4189 | down_read(&css_set_rwsem); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4190 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4191 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4192 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4193 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4194 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4195 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4196 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4197 | for_each_subsys(ss, ssid) |
| 4198 | if (root->subsys_mask & (1 << ssid)) |
| 4199 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4200 | if (strlen(root->name)) |
| 4201 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4202 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4203 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4204 | cgrp = task_cgroup_from_root(tsk, root); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4205 | path = cgroup_path(cgrp, buf, PATH_MAX); |
| 4206 | if (!path) { |
| 4207 | retval = -ENAMETOOLONG; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4208 | goto out_unlock; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4209 | } |
| 4210 | seq_puts(m, path); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4211 | seq_putc(m, '\n'); |
| 4212 | } |
| 4213 | |
| 4214 | out_unlock: |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4215 | up_read(&css_set_rwsem); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4216 | mutex_unlock(&cgroup_mutex); |
| 4217 | put_task_struct(tsk); |
| 4218 | out_free: |
| 4219 | kfree(buf); |
| 4220 | out: |
| 4221 | return retval; |
| 4222 | } |
| 4223 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4224 | /* Display information about each subsystem and each hierarchy */ |
| 4225 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4226 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4227 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4228 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4229 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4230 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4231 | /* |
| 4232 | * ideally we don't want subsystems moving around while we do this. |
| 4233 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4234 | * subsys/hierarchy state. |
| 4235 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4236 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4237 | |
| 4238 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4239 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4240 | ss->name, ss->root->hierarchy_id, |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4241 | atomic_read(&ss->root->nr_cgrps), !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4242 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4243 | mutex_unlock(&cgroup_mutex); |
| 4244 | return 0; |
| 4245 | } |
| 4246 | |
| 4247 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4248 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4249 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4250 | } |
| 4251 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4252 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4253 | .open = cgroupstats_open, |
| 4254 | .read = seq_read, |
| 4255 | .llseek = seq_lseek, |
| 4256 | .release = single_release, |
| 4257 | }; |
| 4258 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4259 | /** |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 4260 | * cgroup_fork - initialize cgroup related fields during copy_process() |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4261 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4262 | * |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 4263 | * A task is associated with the init_css_set until cgroup_post_fork() |
| 4264 | * attaches it to the parent's css_set. Empty cg_list indicates that |
| 4265 | * @child isn't holding reference to its css_set. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4266 | */ |
| 4267 | void cgroup_fork(struct task_struct *child) |
| 4268 | { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 4269 | RCU_INIT_POINTER(child->cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4270 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4271 | } |
| 4272 | |
| 4273 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4274 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4275 | * @child: the task in question |
| 4276 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4277 | * Adds the task to the list running through its css_set if necessary and |
| 4278 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4279 | * 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] | 4280 | * 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] | 4281 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4282 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4283 | void cgroup_post_fork(struct task_struct *child) |
| 4284 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4285 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4286 | int i; |
| 4287 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4288 | /* |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 4289 | * This may race against cgroup_enable_task_cg_links(). As that |
| 4290 | * function sets use_task_css_set_links before grabbing |
| 4291 | * tasklist_lock and we just went through tasklist_lock to add |
| 4292 | * @child, it's guaranteed that either we see the set |
| 4293 | * use_task_css_set_links or cgroup_enable_task_cg_lists() sees |
| 4294 | * @child during its iteration. |
| 4295 | * |
| 4296 | * If we won the race, @child is associated with %current's |
| 4297 | * css_set. Grabbing css_set_rwsem guarantees both that the |
| 4298 | * association is stable, and, on completion of the parent's |
| 4299 | * migration, @child is visible in the source of migration or |
| 4300 | * already in the destination cgroup. This guarantee is necessary |
| 4301 | * when implementing operations which need to migrate all tasks of |
| 4302 | * a cgroup to another. |
| 4303 | * |
| 4304 | * Note that if we lose to cgroup_enable_task_cg_links(), @child |
| 4305 | * will remain in init_css_set. This is safe because all tasks are |
| 4306 | * in the init_css_set before cg_links is enabled and there's no |
| 4307 | * operation which transfers all tasks out of init_css_set. |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4308 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4309 | if (use_task_css_set_links) { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 4310 | struct css_set *cset; |
| 4311 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4312 | down_write(&css_set_rwsem); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 4313 | cset = task_css_set_check(current, |
| 4314 | lockdep_is_held(&css_set_rwsem)); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4315 | task_lock(child); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 4316 | if (list_empty(&child->cg_list)) { |
| 4317 | rcu_assign_pointer(child->cgroups, cset); |
| 4318 | list_add(&child->cg_list, &cset->tasks); |
| 4319 | get_css_set(cset); |
| 4320 | } |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4321 | task_unlock(child); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4322 | up_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4323 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4324 | |
| 4325 | /* |
| 4326 | * Call ss->fork(). This must happen after @child is linked on |
| 4327 | * css_set; otherwise, @child might change state between ->fork() |
| 4328 | * and addition to css_set. |
| 4329 | */ |
| 4330 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4331 | for_each_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4332 | if (ss->fork) |
| 4333 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4334 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4335 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4336 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4337 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4338 | * cgroup_exit - detach cgroup from exiting task |
| 4339 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4340 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4341 | * |
| 4342 | * Description: Detach cgroup from @tsk and release it. |
| 4343 | * |
| 4344 | * Note that cgroups marked notify_on_release force every task in |
| 4345 | * them to take the global cgroup_mutex mutex when exiting. |
| 4346 | * This could impact scaling on very large systems. Be reluctant to |
| 4347 | * use notify_on_release cgroups where very high task exit scaling |
| 4348 | * is required on large systems. |
| 4349 | * |
| 4350 | * the_top_cgroup_hack: |
| 4351 | * |
| 4352 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 4353 | * |
| 4354 | * We call cgroup_exit() while the task is still competent to |
| 4355 | * handle notify_on_release(), then leave the task attached to the |
| 4356 | * root cgroup in each hierarchy for the remainder of its exit. |
| 4357 | * |
| 4358 | * To do this properly, we would increment the reference count on |
| 4359 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 4360 | * code we would add a second cgroup function call, to drop that |
| 4361 | * reference. This would just create an unnecessary hot spot on |
| 4362 | * the top_cgroup reference count, to no avail. |
| 4363 | * |
| 4364 | * Normally, holding a reference to a cgroup without bumping its |
| 4365 | * count is unsafe. The cgroup could go away, or someone could |
| 4366 | * attach us to a different cgroup, decrementing the count on |
| 4367 | * the first cgroup that we never incremented. But in this case, |
| 4368 | * 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] | 4369 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 4370 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4371 | */ |
| 4372 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 4373 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4374 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4375 | struct css_set *cset; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 4376 | bool put_cset = false; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4377 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4378 | |
| 4379 | /* |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4380 | * Unlink from the css_set task list if necessary. Optimistically |
| 4381 | * check cg_list before taking css_set_rwsem. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4382 | */ |
| 4383 | if (!list_empty(&tsk->cg_list)) { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4384 | down_write(&css_set_rwsem); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 4385 | if (!list_empty(&tsk->cg_list)) { |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4386 | list_del_init(&tsk->cg_list); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 4387 | put_cset = true; |
| 4388 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4389 | up_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4390 | } |
| 4391 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4392 | /* Reassign the task to the init_css_set. */ |
| 4393 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4394 | cset = task_css_set(tsk); |
| 4395 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4396 | |
| 4397 | if (run_callbacks && need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4398 | /* see cgroup_post_fork() for details */ |
| 4399 | for_each_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4400 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4401 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 4402 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4403 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4404 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4405 | } |
| 4406 | } |
| 4407 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4408 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4409 | |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame^] | 4410 | if (put_cset) |
| 4411 | put_css_set(cset, true); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4412 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4413 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4414 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4415 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4416 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4417 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4418 | /* |
| 4419 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4420 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4421 | * it now |
| 4422 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4423 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4424 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4425 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4426 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4427 | list_empty(&cgrp->release_list)) { |
| 4428 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4429 | need_schedule_work = 1; |
| 4430 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4431 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4432 | if (need_schedule_work) |
| 4433 | schedule_work(&release_agent_work); |
| 4434 | } |
| 4435 | } |
| 4436 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4437 | /* |
| 4438 | * Notify userspace when a cgroup is released, by running the |
| 4439 | * configured release agent with the name of the cgroup (path |
| 4440 | * relative to the root of cgroup file system) as the argument. |
| 4441 | * |
| 4442 | * Most likely, this user command will try to rmdir this cgroup. |
| 4443 | * |
| 4444 | * This races with the possibility that some other task will be |
| 4445 | * attached to this cgroup before it is removed, or that some other |
| 4446 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 4447 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 4448 | * unused, and this cgroup will be reprieved from its death sentence, |
| 4449 | * to continue to serve a useful existence. Next time it's released, |
| 4450 | * we will get notified again, if it still has 'notify_on_release' set. |
| 4451 | * |
| 4452 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 4453 | * means only wait until the task is successfully execve()'d. The |
| 4454 | * separate release agent task is forked by call_usermodehelper(), |
| 4455 | * then control in this thread returns here, without waiting for the |
| 4456 | * release agent task. We don't bother to wait because the caller of |
| 4457 | * this routine has no use for the exit status of the release agent |
| 4458 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4459 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4460 | static void cgroup_release_agent(struct work_struct *work) |
| 4461 | { |
| 4462 | BUG_ON(work != &release_agent_work); |
| 4463 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4464 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4465 | while (!list_empty(&release_list)) { |
| 4466 | char *argv[3], *envp[3]; |
| 4467 | int i; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4468 | char *pathbuf = NULL, *agentbuf = NULL, *path; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4469 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4470 | struct cgroup, |
| 4471 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4472 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4473 | raw_spin_unlock(&release_list_lock); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4474 | pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4475 | if (!pathbuf) |
| 4476 | goto continue_free; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4477 | path = cgroup_path(cgrp, pathbuf, PATH_MAX); |
| 4478 | if (!path) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4479 | goto continue_free; |
| 4480 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 4481 | if (!agentbuf) |
| 4482 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4483 | |
| 4484 | i = 0; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4485 | argv[i++] = agentbuf; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4486 | argv[i++] = path; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4487 | argv[i] = NULL; |
| 4488 | |
| 4489 | i = 0; |
| 4490 | /* minimal command environment */ |
| 4491 | envp[i++] = "HOME=/"; |
| 4492 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 4493 | envp[i] = NULL; |
| 4494 | |
| 4495 | /* Drop the lock while we invoke the usermode helper, |
| 4496 | * since the exec could involve hitting disk and hence |
| 4497 | * be a slow process */ |
| 4498 | mutex_unlock(&cgroup_mutex); |
| 4499 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4500 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4501 | continue_free: |
| 4502 | kfree(pathbuf); |
| 4503 | kfree(agentbuf); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4504 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4505 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4506 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4507 | mutex_unlock(&cgroup_mutex); |
| 4508 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4509 | |
| 4510 | static int __init cgroup_disable(char *str) |
| 4511 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4512 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4513 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4514 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4515 | |
| 4516 | while ((token = strsep(&str, ",")) != NULL) { |
| 4517 | if (!*token) |
| 4518 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4519 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4520 | for_each_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4521 | if (!strcmp(token, ss->name)) { |
| 4522 | ss->disabled = 1; |
| 4523 | printk(KERN_INFO "Disabling %s control group" |
| 4524 | " subsystem\n", ss->name); |
| 4525 | break; |
| 4526 | } |
| 4527 | } |
| 4528 | } |
| 4529 | return 1; |
| 4530 | } |
| 4531 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4532 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4533 | /** |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4534 | * 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] | 4535 | * @dentry: directory dentry of interest |
| 4536 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4537 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4538 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 4539 | * to get the corresponding css and return it. If such css doesn't exist |
| 4540 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4541 | */ |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4542 | struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry, |
| 4543 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4544 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4545 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
| 4546 | struct cgroup_subsys_state *css = NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4547 | struct cgroup *cgrp; |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4548 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4549 | /* is @dentry a cgroup dir? */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4550 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 4551 | kernfs_type(kn) != KERNFS_DIR) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4552 | return ERR_PTR(-EBADF); |
| 4553 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4554 | rcu_read_lock(); |
| 4555 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4556 | /* |
| 4557 | * This path doesn't originate from kernfs and @kn could already |
| 4558 | * have been or be removed at any point. @kn->priv is RCU |
| 4559 | * protected for this access. See destroy_locked() for details. |
| 4560 | */ |
| 4561 | cgrp = rcu_dereference(kn->priv); |
| 4562 | if (cgrp) |
| 4563 | css = cgroup_css(cgrp, ss); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4564 | |
| 4565 | if (!css || !css_tryget(css)) |
| 4566 | css = ERR_PTR(-ENOENT); |
| 4567 | |
| 4568 | rcu_read_unlock(); |
| 4569 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4570 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4571 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 4572 | /** |
| 4573 | * css_from_id - lookup css by id |
| 4574 | * @id: the cgroup id |
| 4575 | * @ss: cgroup subsys to be looked into |
| 4576 | * |
| 4577 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 4578 | * Should be called under rcu_read_lock(). |
| 4579 | */ |
| 4580 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 4581 | { |
| 4582 | struct cgroup *cgrp; |
| 4583 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4584 | cgroup_assert_mutexes_or_rcu_locked(); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 4585 | |
| 4586 | cgrp = idr_find(&ss->root->cgroup_idr, id); |
| 4587 | if (cgrp) |
Tejun Heo | d162596 | 2013-08-27 14:27:23 -0400 | [diff] [blame] | 4588 | return cgroup_css(cgrp, ss); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 4589 | return NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4590 | } |
| 4591 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4592 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4593 | static struct cgroup_subsys_state * |
| 4594 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4595 | { |
| 4596 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 4597 | |
| 4598 | if (!css) |
| 4599 | return ERR_PTR(-ENOMEM); |
| 4600 | |
| 4601 | return css; |
| 4602 | } |
| 4603 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4604 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4605 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4606 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4607 | } |
| 4608 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4609 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 4610 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4611 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4612 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4613 | } |
| 4614 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4615 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 4616 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4617 | { |
| 4618 | return (u64)(unsigned long)current->cgroups; |
| 4619 | } |
| 4620 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4621 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 4622 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4623 | { |
| 4624 | u64 count; |
| 4625 | |
| 4626 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4627 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4628 | rcu_read_unlock(); |
| 4629 | return count; |
| 4630 | } |
| 4631 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4632 | 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] | 4633 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4634 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4635 | struct css_set *cset; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4636 | char *name_buf; |
| 4637 | |
| 4638 | name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL); |
| 4639 | if (!name_buf) |
| 4640 | return -ENOMEM; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4641 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4642 | down_read(&css_set_rwsem); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4643 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4644 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4645 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4646 | struct cgroup *c = link->cgrp; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4647 | const char *name = "?"; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4648 | |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4649 | if (c != cgroup_dummy_top) { |
| 4650 | cgroup_name(c, name_buf, NAME_MAX + 1); |
| 4651 | name = name_buf; |
| 4652 | } |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4653 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4654 | seq_printf(seq, "Root %d group %s\n", |
| 4655 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4656 | } |
| 4657 | rcu_read_unlock(); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4658 | up_read(&css_set_rwsem); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4659 | kfree(name_buf); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4660 | return 0; |
| 4661 | } |
| 4662 | |
| 4663 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4664 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4665 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4666 | struct cgroup_subsys_state *css = seq_css(seq); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4667 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4668 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4669 | down_read(&css_set_rwsem); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4670 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4671 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4672 | struct task_struct *task; |
| 4673 | int count = 0; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4674 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4675 | seq_printf(seq, "css_set %p\n", cset); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4676 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4677 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4678 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 4679 | goto overflow; |
| 4680 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4681 | } |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4682 | |
| 4683 | list_for_each_entry(task, &cset->mg_tasks, cg_list) { |
| 4684 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 4685 | goto overflow; |
| 4686 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
| 4687 | } |
| 4688 | continue; |
| 4689 | overflow: |
| 4690 | seq_puts(seq, " ...\n"); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4691 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4692 | up_read(&css_set_rwsem); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4693 | return 0; |
| 4694 | } |
| 4695 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4696 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4697 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4698 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | static struct cftype debug_files[] = { |
| 4702 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4703 | .name = "taskcount", |
| 4704 | .read_u64 = debug_taskcount_read, |
| 4705 | }, |
| 4706 | |
| 4707 | { |
| 4708 | .name = "current_css_set", |
| 4709 | .read_u64 = current_css_set_read, |
| 4710 | }, |
| 4711 | |
| 4712 | { |
| 4713 | .name = "current_css_set_refcount", |
| 4714 | .read_u64 = current_css_set_refcount_read, |
| 4715 | }, |
| 4716 | |
| 4717 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4718 | .name = "current_css_set_cg_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4719 | .seq_show = current_css_set_cg_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4720 | }, |
| 4721 | |
| 4722 | { |
| 4723 | .name = "cgroup_css_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4724 | .seq_show = cgroup_css_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4725 | }, |
| 4726 | |
| 4727 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4728 | .name = "releasable", |
| 4729 | .read_u64 = releasable_read, |
| 4730 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4731 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4732 | { } /* terminate */ |
| 4733 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4734 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4735 | struct cgroup_subsys debug_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4736 | .css_alloc = debug_css_alloc, |
| 4737 | .css_free = debug_css_free, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4738 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4739 | }; |
| 4740 | #endif /* CONFIG_CGROUP_DEBUG */ |