Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2 | * Generic process-grouping system. |
| 3 | * |
| 4 | * Based originally on the cpuset system, extracted by Paul Menage |
| 5 | * Copyright (C) 2006 Google, Inc |
| 6 | * |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 7 | * Notifications support |
| 8 | * Copyright (C) 2009 Nokia Corporation |
| 9 | * Author: Kirill A. Shutemov |
| 10 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 11 | * Copyright notices from the original cpuset code: |
| 12 | * -------------------------------------------------- |
| 13 | * Copyright (C) 2003 BULL SA. |
| 14 | * Copyright (C) 2004-2006 Silicon Graphics, Inc. |
| 15 | * |
| 16 | * Portions derived from Patrick Mochel's sysfs code. |
| 17 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
| 18 | * |
| 19 | * 2003-10-10 Written by Simon Derr. |
| 20 | * 2003-10-22 Updates by Stephen Hemminger. |
| 21 | * 2004 May-July Rework by Paul Jackson. |
| 22 | * --------------------------------------------------- |
| 23 | * |
| 24 | * This file is subject to the terms and conditions of the GNU General Public |
| 25 | * License. See the file COPYING in the main directory of the Linux |
| 26 | * distribution for more details. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/cgroup.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 30 | #include <linux/cred.h> |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 31 | #include <linux/ctype.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 33 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 34 | #include <linux/kernel.h> |
| 35 | #include <linux/list.h> |
| 36 | #include <linux/mm.h> |
| 37 | #include <linux/mutex.h> |
| 38 | #include <linux/mount.h> |
| 39 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 40 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 41 | #include <linux/rcupdate.h> |
| 42 | #include <linux/sched.h> |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 43 | #include <linux/backing-dev.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 44 | #include <linux/slab.h> |
| 45 | #include <linux/magic.h> |
| 46 | #include <linux/spinlock.h> |
| 47 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 48 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 49 | #include <linux/kmod.h> |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 50 | #include <linux/module.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 51 | #include <linux/delayacct.h> |
| 52 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 53 | #include <linux/hashtable.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 54 | #include <linux/namei.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 55 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 56 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 57 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 58 | #include <linux/flex_array.h> /* used in cgroup_attach_task */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 59 | #include <linux/kthread.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 60 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 61 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 62 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 63 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 64 | * pidlists linger the following amount before being destroyed. The goal |
| 65 | * is avoiding frequent destruction in the middle of consecutive read calls |
| 66 | * Expiring in the middle is a performance problem not a correctness one. |
| 67 | * 1 sec should be enough. |
| 68 | */ |
| 69 | #define CGROUP_PIDLIST_DESTROY_DELAY HZ |
| 70 | |
| 71 | /* |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 72 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 73 | * hierarchy must be performed while holding it. |
| 74 | * |
| 75 | * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify |
| 76 | * cgroupfs_root of any cgroup hierarchy - subsys list, flags, |
| 77 | * release_agent_path and so on. Modifying requires both cgroup_mutex and |
| 78 | * cgroup_root_mutex. Readers can acquire either of the two. This is to |
| 79 | * break the following locking order cycle. |
| 80 | * |
| 81 | * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem |
| 82 | * B. namespace_sem -> cgroup_mutex |
| 83 | * |
| 84 | * B happens only through cgroup_show_options() and using cgroup_root_mutex |
| 85 | * breaks it. |
| 86 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 87 | #ifdef CONFIG_PROVE_RCU |
| 88 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 89 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 90 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 91 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 92 | #endif |
| 93 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 94 | static DEFINE_MUTEX(cgroup_root_mutex); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 95 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 96 | #define cgroup_assert_mutex_or_rcu_locked() \ |
| 97 | rcu_lockdep_assert(rcu_read_lock_held() || \ |
| 98 | lockdep_is_held(&cgroup_mutex), \ |
| 99 | "cgroup_mutex or RCU read lock required"); |
| 100 | |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 101 | #ifdef CONFIG_LOCKDEP |
| 102 | #define cgroup_assert_mutex_or_root_locked() \ |
| 103 | WARN_ON_ONCE(debug_locks && (!lockdep_is_held(&cgroup_mutex) && \ |
| 104 | !lockdep_is_held(&cgroup_root_mutex))) |
| 105 | #else |
| 106 | #define cgroup_assert_mutex_or_root_locked() do { } while (0) |
| 107 | #endif |
| 108 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 109 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 110 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 111 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 112 | * destruction work items don't end up filling up max_active of system_wq |
| 113 | * which may lead to deadlock. |
| 114 | */ |
| 115 | static struct workqueue_struct *cgroup_destroy_wq; |
| 116 | |
| 117 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 118 | * pidlist destructions need to be flushed on cgroup destruction. Use a |
| 119 | * separate workqueue as flush domain. |
| 120 | */ |
| 121 | static struct workqueue_struct *cgroup_pidlist_destroy_wq; |
| 122 | |
| 123 | /* |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 124 | * Generate an array of cgroup subsystem pointers. At boot time, this is |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 125 | * populated with the built in subsystems, and modular subsystems are |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 126 | * registered after that. The mutable section of this array is protected by |
| 127 | * cgroup_mutex. |
| 128 | */ |
Daniel Wagner | 80f4c87 | 2012-09-12 16:12:06 +0200 | [diff] [blame] | 129 | #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, |
Daniel Wagner | 5fc0b02 | 2012-09-12 16:12:05 +0200 | [diff] [blame] | 130 | #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 131 | static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 132 | #include <linux/cgroup_subsys.h> |
| 133 | }; |
| 134 | |
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 | * The dummy hierarchy, reserved for the subsystems that are otherwise |
| 137 | * unattached - it never has more than a single cgroup, and all tasks are |
| 138 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 139 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 140 | static struct cgroupfs_root cgroup_dummy_root; |
| 141 | |
| 142 | /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ |
| 143 | static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 144 | |
| 145 | /* The list of hierarchy roots */ |
| 146 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 147 | static LIST_HEAD(cgroup_roots); |
| 148 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 149 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 150 | /* |
| 151 | * Hierarchy ID allocation and mapping. It follows the same exclusion |
| 152 | * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for |
| 153 | * writes, either for reads. |
| 154 | */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 155 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 156 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 157 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 158 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 159 | /* |
| 160 | * Assign a monotonically increasing serial number to cgroups. It |
| 161 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 162 | * numbers. Also, as cgroups are always appended to the parent's |
| 163 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 164 | * the ascending serial number order on the list. Protected by |
| 165 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 166 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 167 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 168 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 169 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 170 | * check for fork/exit handlers to call. This avoids us having to do |
| 171 | * extra work in the fork/exit path if none of the subsystems need to |
| 172 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 173 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 174 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 175 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 176 | static struct cftype cgroup_base_files[]; |
| 177 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 178 | static void cgroup_destroy_css_killed(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 179 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 180 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 181 | bool is_add); |
Tejun Heo | e605b36 | 2013-11-27 18:16:21 -0500 | [diff] [blame] | 182 | static int cgroup_file_release(struct inode *inode, struct file *file); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 183 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 184 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 185 | /** |
| 186 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 187 | * @cgrp: the cgroup of interest |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 188 | * @ss: the subsystem of interest (%NULL returns the dummy_css) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 189 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 190 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 191 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 192 | * the caller is responsible for pinning the returned css if it wants to |
| 193 | * keep accessing it outside the said locks. This function may return |
| 194 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 195 | */ |
| 196 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 197 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 198 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 199 | if (ss) |
| 200 | return rcu_dereference_check(cgrp->subsys[ss->subsys_id], |
| 201 | lockdep_is_held(&cgroup_mutex)); |
| 202 | else |
| 203 | return &cgrp->dummy_css; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 204 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 205 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 206 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 207 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 208 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 209 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 212 | /** |
| 213 | * cgroup_is_descendant - test ancestry |
| 214 | * @cgrp: the cgroup to be tested |
| 215 | * @ancestor: possible ancestor of @cgrp |
| 216 | * |
| 217 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 218 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 219 | * and @ancestor are accessible. |
| 220 | */ |
| 221 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 222 | { |
| 223 | while (cgrp) { |
| 224 | if (cgrp == ancestor) |
| 225 | return true; |
| 226 | cgrp = cgrp->parent; |
| 227 | } |
| 228 | return false; |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 231 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 232 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 233 | { |
| 234 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 235 | (1 << CGRP_RELEASABLE) | |
| 236 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 237 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 240 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 241 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 242 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 245 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 246 | * for_each_css - iterate all css's of a cgroup |
| 247 | * @css: the iteration cursor |
| 248 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 249 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 250 | * |
| 251 | * Should be called under cgroup_mutex. |
| 252 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 253 | #define for_each_css(css, ssid, cgrp) \ |
| 254 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 255 | if (!((css) = rcu_dereference_check( \ |
| 256 | (cgrp)->subsys[(ssid)], \ |
| 257 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 258 | else |
| 259 | |
| 260 | /** |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 261 | * for_each_subsys - iterate all loaded cgroup subsystems |
| 262 | * @ss: the iteration cursor |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 263 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 264 | * |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 265 | * Iterates through all loaded subsystems. Should be called under |
| 266 | * cgroup_mutex or cgroup_root_mutex. |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 267 | */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 268 | #define for_each_subsys(ss, ssid) \ |
| 269 | for (({ cgroup_assert_mutex_or_root_locked(); (ssid) = 0; }); \ |
| 270 | (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 271 | if (!((ss) = cgroup_subsys[(ssid)])) { } \ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 272 | else |
| 273 | |
| 274 | /** |
| 275 | * for_each_builtin_subsys - iterate all built-in cgroup subsystems |
| 276 | * @ss: the iteration cursor |
| 277 | * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end |
| 278 | * |
| 279 | * Bulit-in subsystems are always present and iteration itself doesn't |
| 280 | * require any synchronization. |
| 281 | */ |
| 282 | #define for_each_builtin_subsys(ss, i) \ |
| 283 | for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \ |
| 284 | (((ss) = cgroup_subsys[i]) || true); (i)++) |
| 285 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 286 | /* iterate across the active hierarchies */ |
| 287 | #define for_each_active_root(root) \ |
| 288 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 289 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 290 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 291 | { |
| 292 | return dentry->d_fsdata; |
| 293 | } |
| 294 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 295 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 296 | { |
| 297 | return dentry->d_fsdata; |
| 298 | } |
| 299 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 300 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 301 | { |
| 302 | return __d_cfe(dentry)->type; |
| 303 | } |
| 304 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 305 | /** |
| 306 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 307 | * @cgrp: the cgroup to be checked for liveness |
| 308 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 309 | * On success, returns true; the mutex should be later unlocked. On |
| 310 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 311 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 312 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 313 | { |
| 314 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 315 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 316 | mutex_unlock(&cgroup_mutex); |
| 317 | return false; |
| 318 | } |
| 319 | return true; |
| 320 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 321 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 322 | /* the list of cgroups eligible for automatic release. Protected by |
| 323 | * release_list_lock */ |
| 324 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 325 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 326 | static void cgroup_release_agent(struct work_struct *work); |
| 327 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 328 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 329 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 330 | /* |
| 331 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 332 | * belong to different cgroups on different hierarchies. In the other |
| 333 | * direction, a css_set is naturally associated with multiple cgroups. |
| 334 | * This M:N relationship is represented by the following link structure |
| 335 | * which exists for each association and allows traversing the associations |
| 336 | * from both sides. |
| 337 | */ |
| 338 | struct cgrp_cset_link { |
| 339 | /* the cgroup and css_set this link associates */ |
| 340 | struct cgroup *cgrp; |
| 341 | struct css_set *cset; |
| 342 | |
| 343 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 344 | struct list_head cset_link; |
| 345 | |
| 346 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 347 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | /* The default css_set - used by init and its children prior to any |
| 351 | * hierarchies being mounted. It contains a pointer to the root state |
| 352 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 353 | * reference-counted, to improve performance when child cgroups |
| 354 | * haven't been created. |
| 355 | */ |
| 356 | |
| 357 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 358 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 359 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 360 | /* |
| 361 | * css_set_lock protects the list of css_set objects, and the chain of |
| 362 | * tasks off each css_set. Nests outside task->alloc_lock due to |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 363 | * css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 364 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 365 | static DEFINE_RWLOCK(css_set_lock); |
| 366 | static int css_set_count; |
| 367 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 368 | /* |
| 369 | * hash table for cgroup groups. This improves the performance to find |
| 370 | * an existing css_set. This hash doesn't (currently) take into |
| 371 | * account cgroups in empty hierarchies. |
| 372 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 373 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 374 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 375 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 376 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 377 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 378 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 379 | struct cgroup_subsys *ss; |
| 380 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 381 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 382 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 383 | key += (unsigned long)css[i]; |
| 384 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 385 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 386 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 389 | /* |
| 390 | * We don't maintain the lists running through each css_set to its task |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 391 | * until after the first call to css_task_iter_start(). This reduces the |
| 392 | * fork()/exit() overhead for people who have cgroups compiled into their |
| 393 | * kernel but not actually in use. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 394 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 395 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 396 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 397 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 398 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 399 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 400 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 401 | /* |
| 402 | * Ensure that the refcount doesn't hit zero while any readers |
| 403 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 404 | * rwlock |
| 405 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 406 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 407 | return; |
| 408 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 409 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 410 | write_unlock(&css_set_lock); |
| 411 | return; |
| 412 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 413 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 414 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 415 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 416 | css_set_count--; |
| 417 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 418 | 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] | 419 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 420 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 421 | list_del(&link->cset_link); |
| 422 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 423 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 424 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 425 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 426 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 427 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 428 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 429 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 430 | |
| 431 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 432 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 433 | |
| 434 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 435 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* |
| 439 | * refcounted get/put for css_set objects |
| 440 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 441 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 442 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 443 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 446 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 447 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 448 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 451 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 452 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 453 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 456 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 457 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 458 | * @cset: candidate css_set being tested |
| 459 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 460 | * @new_cgrp: cgroup that's being entered by the task |
| 461 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 462 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 463 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 464 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 465 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 466 | static bool compare_css_sets(struct css_set *cset, |
| 467 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 468 | struct cgroup *new_cgrp, |
| 469 | struct cgroup_subsys_state *template[]) |
| 470 | { |
| 471 | struct list_head *l1, *l2; |
| 472 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 473 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 474 | /* Not all subsystems matched */ |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Compare cgroup pointers in order to distinguish between |
| 480 | * different cgroups in heirarchies with no subsystems. We |
| 481 | * could get by with just this check alone (and skip the |
| 482 | * memcmp above) but on most setups the memcmp check will |
| 483 | * avoid the need for this more expensive check on almost all |
| 484 | * candidates. |
| 485 | */ |
| 486 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 487 | l1 = &cset->cgrp_links; |
| 488 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 489 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 490 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 491 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 492 | |
| 493 | l1 = l1->next; |
| 494 | l2 = l2->next; |
| 495 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 496 | if (l1 == &cset->cgrp_links) { |
| 497 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 498 | break; |
| 499 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 500 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 501 | } |
| 502 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 503 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 504 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 505 | cgrp1 = link1->cgrp; |
| 506 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 507 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 508 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 509 | |
| 510 | /* |
| 511 | * If this hierarchy is the hierarchy of the cgroup |
| 512 | * that's changing, then we need to check that this |
| 513 | * css_set points to the new cgroup; if it's any other |
| 514 | * hierarchy, then this css_set should point to the |
| 515 | * same cgroup as the old css_set. |
| 516 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 517 | if (cgrp1->root == new_cgrp->root) { |
| 518 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 519 | return false; |
| 520 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 521 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 522 | return false; |
| 523 | } |
| 524 | } |
| 525 | return true; |
| 526 | } |
| 527 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 528 | /** |
| 529 | * find_existing_css_set - init css array and find the matching css_set |
| 530 | * @old_cset: the css_set that we're using before the cgroup transition |
| 531 | * @cgrp: the cgroup that we're moving into |
| 532 | * @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] | 533 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 534 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 535 | struct cgroup *cgrp, |
| 536 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 537 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 538 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 539 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 540 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 541 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 542 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 543 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 544 | /* |
| 545 | * Build the set of subsystem state objects that we want to see in the |
| 546 | * new css_set. while subsystems can change globally, the entries here |
| 547 | * won't change, so no need for locking. |
| 548 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 549 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 550 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 551 | /* Subsystem is in this hierarchy. So we want |
| 552 | * the subsystem state from the new |
| 553 | * cgroup */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 554 | template[i] = cgroup_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 555 | } else { |
| 556 | /* Subsystem is not in this hierarchy, so we |
| 557 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 558 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 562 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 563 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 564 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 565 | continue; |
| 566 | |
| 567 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 568 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 569 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 570 | |
| 571 | /* No existing cgroup group matched */ |
| 572 | return NULL; |
| 573 | } |
| 574 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 575 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 576 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 577 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 578 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 579 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 580 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 581 | kfree(link); |
| 582 | } |
| 583 | } |
| 584 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 585 | /** |
| 586 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 587 | * @count: the number of links to allocate |
| 588 | * @tmp_links: list_head the allocated links are put on |
| 589 | * |
| 590 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 591 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 592 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 593 | 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] | 594 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 595 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 596 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 597 | |
| 598 | INIT_LIST_HEAD(tmp_links); |
| 599 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 600 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 601 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 602 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 603 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 604 | return -ENOMEM; |
| 605 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 606 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 607 | } |
| 608 | return 0; |
| 609 | } |
| 610 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 611 | /** |
| 612 | * 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] | 613 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 614 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 615 | * @cgrp: the destination cgroup |
| 616 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 617 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 618 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 619 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 620 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 621 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 622 | BUG_ON(list_empty(tmp_links)); |
| 623 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 624 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 625 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 626 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 627 | /* |
| 628 | * Always add links to the tail of the list so that the list |
| 629 | * is sorted by order of hierarchy creation |
| 630 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 631 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 632 | } |
| 633 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 634 | /** |
| 635 | * find_css_set - return a new css_set with one cgroup updated |
| 636 | * @old_cset: the baseline css_set |
| 637 | * @cgrp: the cgroup to be updated |
| 638 | * |
| 639 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 640 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 641 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 642 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 643 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 644 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 645 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 646 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 647 | struct list_head tmp_links; |
| 648 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 649 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 650 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 651 | lockdep_assert_held(&cgroup_mutex); |
| 652 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 653 | /* First see if we already have a cgroup group that matches |
| 654 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 655 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 656 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 657 | if (cset) |
| 658 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 659 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 660 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 661 | if (cset) |
| 662 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 663 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 664 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 665 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 666 | return NULL; |
| 667 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 668 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 669 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 670 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 671 | return NULL; |
| 672 | } |
| 673 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 674 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 675 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 676 | INIT_LIST_HEAD(&cset->tasks); |
| 677 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 678 | |
| 679 | /* Copy the set of subsystem state objects generated in |
| 680 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 681 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 682 | |
| 683 | write_lock(&css_set_lock); |
| 684 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 685 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 686 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 687 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 688 | if (c->root == cgrp->root) |
| 689 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 690 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 691 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 692 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 693 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 694 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 695 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 696 | |
| 697 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 698 | key = css_set_hash(cset->subsys); |
| 699 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 700 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 701 | write_unlock(&css_set_lock); |
| 702 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 703 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 706 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 707 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 708 | * called with cgroup_mutex held. |
| 709 | */ |
| 710 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 711 | struct cgroupfs_root *root) |
| 712 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 713 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 714 | struct cgroup *res = NULL; |
| 715 | |
| 716 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 717 | read_lock(&css_set_lock); |
| 718 | /* |
| 719 | * No need to lock the task - since we hold cgroup_mutex the |
| 720 | * task can't change groups, so the only thing that can happen |
| 721 | * is that it exits and its css is set back to init_css_set. |
| 722 | */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 723 | cset = task_css_set(task); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 724 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 725 | res = &root->top_cgroup; |
| 726 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 727 | struct cgrp_cset_link *link; |
| 728 | |
| 729 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 730 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 731 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 732 | if (c->root == root) { |
| 733 | res = c; |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | read_unlock(&css_set_lock); |
| 739 | BUG_ON(!res); |
| 740 | return res; |
| 741 | } |
| 742 | |
| 743 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 744 | * There is one global cgroup mutex. We also require taking |
| 745 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 746 | * See "The task_lock() exception", at the end of this comment. |
| 747 | * |
| 748 | * A task must hold cgroup_mutex to modify cgroups. |
| 749 | * |
| 750 | * Any task can increment and decrement the count field without lock. |
| 751 | * So in general, code holding cgroup_mutex can't rely on the count |
| 752 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 753 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 754 | * means that no tasks are currently attached, therefore there is no |
| 755 | * way a task attached to that cgroup can fork (the other way to |
| 756 | * increment the count). So code holding cgroup_mutex can safely |
| 757 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 758 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 759 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 760 | * needs that mutex. |
| 761 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 762 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 763 | * (usually) take cgroup_mutex. These are the two most performance |
| 764 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 765 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 766 | * 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] | 767 | * to the release agent with the name of the cgroup (path relative to |
| 768 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 769 | * |
| 770 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 771 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 772 | * tasks in the system use _some_ cgroup, and since there is always at |
| 773 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 774 | * always has either children cgroups and/or using tasks. So we don't |
| 775 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 776 | * |
| 777 | * The task_lock() exception |
| 778 | * |
| 779 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 780 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 781 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 782 | * several performance critical places that need to reference |
| 783 | * task->cgroup without the expense of grabbing a system global |
| 784 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 785 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 786 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 787 | * the task_struct routinely used for such matters. |
| 788 | * |
| 789 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 790 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 791 | */ |
| 792 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 793 | /* |
| 794 | * A couple of forward declarations required, due to cyclic reference loop: |
| 795 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 796 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 797 | * -> cgroup_mkdir. |
| 798 | */ |
| 799 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 800 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 801 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 802 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 803 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 804 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 805 | |
| 806 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 807 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 808 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 809 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 810 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 811 | static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 812 | { |
| 813 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 814 | |
| 815 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 816 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 817 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 818 | inode->i_uid = current_fsuid(); |
| 819 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 820 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 821 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 822 | } |
| 823 | return inode; |
| 824 | } |
| 825 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 826 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 827 | { |
| 828 | struct cgroup_name *name; |
| 829 | |
| 830 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 831 | if (!name) |
| 832 | return NULL; |
| 833 | strcpy(name->name, dentry->d_name.name); |
| 834 | return name; |
| 835 | } |
| 836 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 837 | static void cgroup_free_fn(struct work_struct *work) |
| 838 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 839 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 840 | |
| 841 | mutex_lock(&cgroup_mutex); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 842 | cgrp->root->number_of_cgroups--; |
| 843 | mutex_unlock(&cgroup_mutex); |
| 844 | |
| 845 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 846 | * We get a ref to the parent's dentry, and put the ref when |
| 847 | * this cgroup is being freed, so it's guaranteed that the |
| 848 | * parent won't be destroyed before its children. |
| 849 | */ |
| 850 | dput(cgrp->parent->dentry); |
| 851 | |
| 852 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 853 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 854 | * created the cgroup. This will free cgrp->root, if we are |
| 855 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 856 | */ |
| 857 | deactivate_super(cgrp->root->sb); |
| 858 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 859 | cgroup_pidlist_destroy_all(cgrp); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 860 | |
| 861 | simple_xattrs_free(&cgrp->xattrs); |
| 862 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 863 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 864 | kfree(cgrp); |
| 865 | } |
| 866 | |
| 867 | static void cgroup_free_rcu(struct rcu_head *head) |
| 868 | { |
| 869 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 870 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 871 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 872 | queue_work(cgroup_destroy_wq, &cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 873 | } |
| 874 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 875 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 876 | { |
| 877 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 878 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 879 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 880 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 881 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | c1a7150 | 2013-12-17 11:13:39 +0800 | [diff] [blame] | 882 | |
| 883 | /* |
| 884 | * XXX: cgrp->id is only used to look up css's. As cgroup |
| 885 | * and css's lifetimes will be decoupled, it should be made |
| 886 | * per-subsystem and moved to css->id so that lookups are |
| 887 | * successful until the target css is released. |
| 888 | */ |
| 889 | idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 890 | cgrp->id = -1; |
| 891 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 892 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 893 | } else { |
| 894 | struct cfent *cfe = __d_cfe(dentry); |
| 895 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 896 | |
| 897 | WARN_ONCE(!list_empty(&cfe->node) && |
| 898 | cgrp != &cgrp->root->top_cgroup, |
| 899 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 900 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 901 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 902 | } |
| 903 | iput(inode); |
| 904 | } |
| 905 | |
| 906 | static void remove_dir(struct dentry *d) |
| 907 | { |
| 908 | struct dentry *parent = dget(d->d_parent); |
| 909 | |
| 910 | d_delete(d); |
| 911 | simple_rmdir(parent->d_inode, d); |
| 912 | dput(parent); |
| 913 | } |
| 914 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 915 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 916 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 917 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 918 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 919 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 920 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 921 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 922 | /* |
| 923 | * If we're doing cleanup due to failure of cgroup_create(), |
| 924 | * the corresponding @cfe may not exist. |
| 925 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 926 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 927 | struct dentry *d = cfe->dentry; |
| 928 | |
| 929 | if (cft && cfe->type != cft) |
| 930 | continue; |
| 931 | |
| 932 | dget(d); |
| 933 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 934 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 935 | list_del_init(&cfe->node); |
| 936 | dput(d); |
| 937 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 938 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 939 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 942 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 943 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 944 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 945 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 946 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 947 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 948 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 949 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 950 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 951 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 952 | for_each_subsys(ss, i) { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 953 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 954 | |
| 955 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 956 | continue; |
| 957 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 958 | cgroup_addrm_files(cgrp, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 959 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* |
| 963 | * NOTE : the dentry must have been dget()'ed |
| 964 | */ |
| 965 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 966 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 967 | struct dentry *parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 968 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 969 | parent = dentry->d_parent; |
| 970 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 971 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 972 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 973 | spin_unlock(&dentry->d_lock); |
| 974 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 975 | remove_dir(dentry); |
| 976 | } |
| 977 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 978 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 979 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 980 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 981 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 982 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 983 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 984 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 985 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 986 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 987 | struct cgroup_subsys *ss; |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 988 | unsigned long pinned = 0; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 989 | int i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 990 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 991 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 992 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 993 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 994 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 995 | for_each_subsys(ss, i) { |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 996 | if (!(added_mask & (1 << i))) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 997 | continue; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 998 | |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 999 | /* is the subsystem mounted elsewhere? */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1000 | if (ss->root != &cgroup_dummy_root) { |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1001 | ret = -EBUSY; |
| 1002 | goto out_put; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1003 | } |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1004 | |
| 1005 | /* pin the module */ |
| 1006 | if (!try_module_get(ss->module)) { |
| 1007 | ret = -ENOENT; |
| 1008 | goto out_put; |
| 1009 | } |
| 1010 | pinned |= 1 << i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1013 | /* subsys could be missing if unloaded between parsing and here */ |
| 1014 | if (added_mask != pinned) { |
| 1015 | ret = -ENOENT; |
| 1016 | goto out_put; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1017 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1018 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1019 | ret = cgroup_populate_dir(cgrp, added_mask); |
| 1020 | if (ret) |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1021 | goto out_put; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1022 | |
| 1023 | /* |
| 1024 | * Nothing can fail from this point on. Remove files for the |
| 1025 | * removed subsystems and rebind each subsystem. |
| 1026 | */ |
| 1027 | cgroup_clear_dir(cgrp, removed_mask); |
| 1028 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1029 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1030 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1031 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1032 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1033 | /* We're binding this subsystem to this hierarchy */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1034 | BUG_ON(cgroup_css(cgrp, ss)); |
| 1035 | BUG_ON(!cgroup_css(cgroup_dummy_top, ss)); |
| 1036 | BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1037 | |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1038 | rcu_assign_pointer(cgrp->subsys[i], |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1039 | cgroup_css(cgroup_dummy_top, ss)); |
| 1040 | cgroup_css(cgrp, ss)->cgroup = cgrp; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1041 | |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1042 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1043 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1044 | ss->bind(cgroup_css(cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1045 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1046 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1047 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1048 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1049 | /* We're removing this subsystem */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1050 | BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss)); |
| 1051 | BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1052 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1053 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1054 | ss->bind(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1055 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1056 | cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1057 | RCU_INIT_POINTER(cgrp->subsys[i], NULL); |
| 1058 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1059 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1060 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1061 | /* subsystem is now free - drop reference on module */ |
| 1062 | module_put(ss->module); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1063 | root->subsys_mask &= ~bit; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1064 | } |
| 1065 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1066 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1067 | /* |
| 1068 | * Mark @root has finished binding subsystems. @root->subsys_mask |
| 1069 | * now matches the bound subsystems. |
| 1070 | */ |
| 1071 | root->flags |= CGRP_ROOT_SUBSYS_BOUND; |
| 1072 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1073 | return 0; |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1074 | |
| 1075 | out_put: |
| 1076 | for_each_subsys(ss, i) |
| 1077 | if (pinned & (1 << i)) |
| 1078 | module_put(ss->module); |
| 1079 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1082 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1083 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1084 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1085 | struct cgroup_subsys *ss; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1086 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1087 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1088 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1089 | for_each_subsys(ss, ssid) |
| 1090 | if (root->subsys_mask & (1 << ssid)) |
| 1091 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1092 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1093 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1094 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1095 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1096 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1097 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1098 | if (strlen(root->release_agent_path)) |
| 1099 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1100 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1101 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1102 | if (strlen(root->name)) |
| 1103 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1104 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1109 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1110 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1111 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1112 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1113 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1114 | /* User explicitly requested empty subsystem */ |
| 1115 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1116 | |
| 1117 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1118 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1119 | }; |
| 1120 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1121 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1122 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1123 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1124 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1125 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1126 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1127 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1128 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1129 | char *token, *o = data; |
| 1130 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1131 | unsigned long mask = (unsigned long)-1; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1132 | struct cgroup_subsys *ss; |
| 1133 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1134 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1135 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1136 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1137 | #ifdef CONFIG_CPUSETS |
| 1138 | mask = ~(1UL << cpuset_subsys_id); |
| 1139 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1140 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1141 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1142 | |
| 1143 | while ((token = strsep(&o, ",")) != NULL) { |
| 1144 | if (!*token) |
| 1145 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1146 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1147 | /* Explicitly have no subsystems */ |
| 1148 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1149 | continue; |
| 1150 | } |
| 1151 | if (!strcmp(token, "all")) { |
| 1152 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1153 | if (one_ss) |
| 1154 | return -EINVAL; |
| 1155 | all_ss = true; |
| 1156 | continue; |
| 1157 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1158 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1159 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1160 | continue; |
| 1161 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1162 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1163 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1164 | continue; |
| 1165 | } |
| 1166 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1167 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1168 | continue; |
| 1169 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1170 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1171 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1172 | continue; |
| 1173 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1174 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1175 | /* Specifying two release agents is forbidden */ |
| 1176 | if (opts->release_agent) |
| 1177 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1178 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1179 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1180 | if (!opts->release_agent) |
| 1181 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1182 | continue; |
| 1183 | } |
| 1184 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1185 | const char *name = token + 5; |
| 1186 | /* Can't specify an empty name */ |
| 1187 | if (!strlen(name)) |
| 1188 | return -EINVAL; |
| 1189 | /* Must match [\w.-]+ */ |
| 1190 | for (i = 0; i < strlen(name); i++) { |
| 1191 | char c = name[i]; |
| 1192 | if (isalnum(c)) |
| 1193 | continue; |
| 1194 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1195 | continue; |
| 1196 | return -EINVAL; |
| 1197 | } |
| 1198 | /* Specifying two names is forbidden */ |
| 1199 | if (opts->name) |
| 1200 | return -EINVAL; |
| 1201 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1202 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1203 | GFP_KERNEL); |
| 1204 | if (!opts->name) |
| 1205 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1206 | |
| 1207 | continue; |
| 1208 | } |
| 1209 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1210 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1211 | if (strcmp(token, ss->name)) |
| 1212 | continue; |
| 1213 | if (ss->disabled) |
| 1214 | continue; |
| 1215 | |
| 1216 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1217 | if (all_ss) |
| 1218 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1219 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1220 | one_ss = true; |
| 1221 | |
| 1222 | break; |
| 1223 | } |
| 1224 | if (i == CGROUP_SUBSYS_COUNT) |
| 1225 | return -ENOENT; |
| 1226 | } |
| 1227 | |
| 1228 | /* |
| 1229 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1230 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1231 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1232 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1233 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1234 | for_each_subsys(ss, i) |
| 1235 | if (!ss->disabled) |
| 1236 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1237 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1238 | /* Consistency checks */ |
| 1239 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1240 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1241 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1242 | |
| 1243 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1244 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1245 | return -EINVAL; |
| 1246 | } |
| 1247 | |
| 1248 | if (opts->cpuset_clone_children) { |
| 1249 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1250 | return -EINVAL; |
| 1251 | } |
| 1252 | } |
| 1253 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1254 | /* |
| 1255 | * Option noprefix was introduced just for backward compatibility |
| 1256 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1257 | * the cpuset subsystem. |
| 1258 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1259 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1260 | return -EINVAL; |
| 1261 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1262 | |
| 1263 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1264 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1265 | return -EINVAL; |
| 1266 | |
| 1267 | /* |
| 1268 | * We either have to specify by name or by subsystems. (So all |
| 1269 | * empty hierarchies must have a name). |
| 1270 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1271 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1272 | return -EINVAL; |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1278 | { |
| 1279 | int ret = 0; |
| 1280 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1281 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1282 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1283 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1284 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1285 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1286 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1287 | return -EINVAL; |
| 1288 | } |
| 1289 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1290 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1291 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1292 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1293 | |
| 1294 | /* See what subsystems are wanted */ |
| 1295 | ret = parse_cgroupfs_options(data, &opts); |
| 1296 | if (ret) |
| 1297 | goto out_unlock; |
| 1298 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1299 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1300 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1301 | task_tgid_nr(current), current->comm); |
| 1302 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1303 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1304 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1305 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1306 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1307 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1308 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1309 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1310 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1311 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1312 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1313 | goto out_unlock; |
| 1314 | } |
| 1315 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1316 | /* remounting is not allowed for populated hierarchies */ |
| 1317 | if (root->number_of_cgroups > 1) { |
| 1318 | ret = -EBUSY; |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1319 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1320 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1321 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1322 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1323 | if (ret) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1324 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1325 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1326 | if (opts.release_agent) |
| 1327 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1328 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1329 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1330 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1331 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1332 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1333 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1334 | return ret; |
| 1335 | } |
| 1336 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1337 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1338 | .statfs = simple_statfs, |
| 1339 | .drop_inode = generic_delete_inode, |
| 1340 | .show_options = cgroup_show_options, |
| 1341 | .remount_fs = cgroup_remount, |
| 1342 | }; |
| 1343 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1344 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1345 | { |
| 1346 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1347 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1348 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1349 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1350 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1351 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1352 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1353 | cgrp->dummy_css.cgroup = cgrp; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1354 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1355 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1356 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1357 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1358 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1359 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1360 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1361 | INIT_LIST_HEAD(&root->root_list); |
| 1362 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1363 | cgrp->root = root; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 1364 | RCU_INIT_POINTER(cgrp->name, &root_cgroup_name); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1365 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1366 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1369 | static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1370 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1371 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1372 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1373 | lockdep_assert_held(&cgroup_mutex); |
| 1374 | lockdep_assert_held(&cgroup_root_mutex); |
| 1375 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1376 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 1377 | GFP_KERNEL); |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1378 | if (id < 0) |
| 1379 | return id; |
| 1380 | |
| 1381 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1382 | return 0; |
| 1383 | } |
| 1384 | |
| 1385 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1386 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1387 | lockdep_assert_held(&cgroup_mutex); |
| 1388 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1389 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1390 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1391 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1392 | root->hierarchy_id = 0; |
| 1393 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1396 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1397 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1398 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1399 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1400 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1401 | /* If we asked for a name then it must match */ |
| 1402 | if (opts->name && strcmp(opts->name, root->name)) |
| 1403 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1404 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1405 | /* |
| 1406 | * If we asked for subsystems (or explicitly for no |
| 1407 | * subsystems) then they must match |
| 1408 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1409 | if ((opts->subsys_mask || opts->none) |
| 1410 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1411 | return 0; |
| 1412 | |
| 1413 | return 1; |
| 1414 | } |
| 1415 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1416 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1417 | { |
| 1418 | struct cgroupfs_root *root; |
| 1419 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1420 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1421 | return NULL; |
| 1422 | |
| 1423 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1424 | if (!root) |
| 1425 | return ERR_PTR(-ENOMEM); |
| 1426 | |
| 1427 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1428 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1429 | /* |
| 1430 | * We need to set @root->subsys_mask now so that @root can be |
| 1431 | * matched by cgroup_test_super() before it finishes |
| 1432 | * initialization; otherwise, competing mounts with the same |
| 1433 | * options may try to bind the same subsystems instead of waiting |
| 1434 | * for the first one leading to unexpected mount errors. |
| 1435 | * SUBSYS_BOUND will be set once actual binding is complete. |
| 1436 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1437 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1438 | root->flags = opts->flags; |
| 1439 | if (opts->release_agent) |
| 1440 | strcpy(root->release_agent_path, opts->release_agent); |
| 1441 | if (opts->name) |
| 1442 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1443 | if (opts->cpuset_clone_children) |
| 1444 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1445 | return root; |
| 1446 | } |
| 1447 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1448 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1449 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1450 | if (root) { |
| 1451 | /* hierarhcy ID shoulid already have been released */ |
| 1452 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1453 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1454 | idr_destroy(&root->cgroup_idr); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1455 | kfree(root); |
| 1456 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1459 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1460 | { |
| 1461 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1462 | struct cgroup_sb_opts *opts = data; |
| 1463 | |
| 1464 | /* If we don't have a new root, we can't set up a new sb */ |
| 1465 | if (!opts->new_root) |
| 1466 | return -EINVAL; |
| 1467 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1468 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1469 | |
| 1470 | ret = set_anon_super(sb, NULL); |
| 1471 | if (ret) |
| 1472 | return ret; |
| 1473 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1474 | sb->s_fs_info = opts->new_root; |
| 1475 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1476 | |
| 1477 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1478 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1479 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1480 | sb->s_op = &cgroup_ops; |
| 1481 | |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1486 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1487 | static const struct dentry_operations cgroup_dops = { |
| 1488 | .d_iput = cgroup_diput, |
Al Viro | b26d4cd | 2013-10-25 18:47:37 -0400 | [diff] [blame] | 1489 | .d_delete = always_delete_dentry, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1490 | }; |
| 1491 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1492 | struct inode *inode = |
| 1493 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1494 | |
| 1495 | if (!inode) |
| 1496 | return -ENOMEM; |
| 1497 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1498 | inode->i_fop = &simple_dir_operations; |
| 1499 | inode->i_op = &cgroup_dir_inode_operations; |
| 1500 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1501 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1502 | sb->s_root = d_make_root(inode); |
| 1503 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1504 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1505 | /* for everything else we want ->d_op set */ |
| 1506 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1507 | return 0; |
| 1508 | } |
| 1509 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1510 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1511 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1512 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1513 | { |
| 1514 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1515 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1516 | int ret = 0; |
| 1517 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1518 | struct cgroupfs_root *new_root; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1519 | struct list_head tmp_links; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1520 | struct inode *inode; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1521 | const struct cred *cred; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1522 | |
| 1523 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1524 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1525 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1526 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1527 | if (ret) |
| 1528 | goto out_err; |
| 1529 | |
| 1530 | /* |
| 1531 | * Allocate a new cgroup root. We may not need it if we're |
| 1532 | * reusing an existing hierarchy. |
| 1533 | */ |
| 1534 | new_root = cgroup_root_from_opts(&opts); |
| 1535 | if (IS_ERR(new_root)) { |
| 1536 | ret = PTR_ERR(new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1537 | goto out_err; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1538 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1539 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1540 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1541 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1542 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1543 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1544 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1545 | cgroup_free_root(opts.new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1546 | goto out_err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1547 | } |
| 1548 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1549 | root = sb->s_fs_info; |
| 1550 | BUG_ON(!root); |
| 1551 | if (root == opts.new_root) { |
| 1552 | /* We used the new root structure, so this is a new hierarchy */ |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1553 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1554 | struct cgroupfs_root *existing_root; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1555 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1556 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1557 | |
| 1558 | BUG_ON(sb->s_root != NULL); |
| 1559 | |
| 1560 | ret = cgroup_get_rootdir(sb); |
| 1561 | if (ret) |
| 1562 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1563 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1564 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1565 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1566 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1567 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1568 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1569 | root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp, |
| 1570 | 0, 1, GFP_KERNEL); |
| 1571 | if (root_cgrp->id < 0) |
| 1572 | goto unlock_drop; |
| 1573 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1574 | /* Check for name clashes with existing mounts */ |
| 1575 | ret = -EBUSY; |
| 1576 | if (strlen(root->name)) |
| 1577 | for_each_active_root(existing_root) |
| 1578 | if (!strcmp(existing_root->name, root->name)) |
| 1579 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1580 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1581 | /* |
| 1582 | * We're accessing css_set_count without locking |
| 1583 | * css_set_lock here, but that's OK - it can only be |
| 1584 | * increased by someone holding cgroup_lock, and |
| 1585 | * that's us. The worst that can happen is that we |
| 1586 | * have some link structures left over |
| 1587 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1588 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1589 | if (ret) |
| 1590 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1591 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1592 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1593 | ret = cgroup_init_root_id(root, 2, 0); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1594 | if (ret) |
| 1595 | goto unlock_drop; |
| 1596 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1597 | sb->s_root->d_fsdata = root_cgrp; |
| 1598 | root_cgrp->dentry = sb->s_root; |
| 1599 | |
| 1600 | /* |
| 1601 | * We're inside get_sb() and will call lookup_one_len() to |
| 1602 | * create the root files, which doesn't work if SELinux is |
| 1603 | * in use. The following cred dancing somehow works around |
| 1604 | * it. See 2ce9738ba ("cgroupfs: use init_cred when |
| 1605 | * populating new cgroupfs mount") for more details. |
| 1606 | */ |
| 1607 | cred = override_creds(&init_cred); |
| 1608 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1609 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1610 | if (ret) |
| 1611 | goto rm_base_files; |
| 1612 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1613 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1614 | if (ret) |
| 1615 | goto rm_base_files; |
| 1616 | |
| 1617 | revert_creds(cred); |
| 1618 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1619 | /* |
| 1620 | * There must be no failure case after here, since rebinding |
| 1621 | * takes care of subsystems' refcounts, which are explicitly |
| 1622 | * dropped in the failure exit path. |
| 1623 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1624 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1625 | list_add(&root->root_list, &cgroup_roots); |
| 1626 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1627 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1628 | /* Link the top cgroup in this hierarchy into all |
| 1629 | * the css_set objects */ |
| 1630 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1631 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1632 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1633 | write_unlock(&css_set_lock); |
| 1634 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1635 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1636 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1637 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1638 | BUG_ON(root->number_of_cgroups != 1); |
| 1639 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1640 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1641 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1642 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1643 | } else { |
| 1644 | /* |
| 1645 | * We re-used an existing hierarchy - the new root (if |
| 1646 | * any) is not needed |
| 1647 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1648 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1649 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1650 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1651 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1652 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1653 | ret = -EINVAL; |
| 1654 | goto drop_new_super; |
| 1655 | } else { |
| 1656 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1657 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1658 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1661 | kfree(opts.release_agent); |
| 1662 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1663 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1664 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1665 | rm_base_files: |
| 1666 | free_cgrp_cset_links(&tmp_links); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1667 | cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1668 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1669 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1670 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1671 | mutex_unlock(&cgroup_root_mutex); |
| 1672 | mutex_unlock(&cgroup_mutex); |
| 1673 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1674 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1675 | deactivate_locked_super(sb); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1676 | out_err: |
| 1677 | kfree(opts.release_agent); |
| 1678 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1679 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 1682 | static void cgroup_kill_sb(struct super_block *sb) |
| 1683 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1684 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1685 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1686 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1687 | int ret; |
| 1688 | |
| 1689 | BUG_ON(!root); |
| 1690 | |
| 1691 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1692 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1693 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1694 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1695 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1696 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1697 | |
| 1698 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1699 | if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { |
| 1700 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
| 1701 | /* Shouldn't be able to fail ... */ |
| 1702 | BUG_ON(ret); |
| 1703 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1704 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1705 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1706 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1707 | * root cgroup |
| 1708 | */ |
| 1709 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1710 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1711 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1712 | list_del(&link->cset_link); |
| 1713 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1714 | kfree(link); |
| 1715 | } |
| 1716 | write_unlock(&css_set_lock); |
| 1717 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1718 | if (!list_empty(&root->root_list)) { |
| 1719 | list_del(&root->root_list); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1720 | cgroup_root_count--; |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1721 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1722 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1723 | cgroup_exit_root_id(root); |
| 1724 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1725 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1726 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1727 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1728 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1729 | simple_xattrs_free(&cgrp->xattrs); |
| 1730 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1731 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1732 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | static struct file_system_type cgroup_fs_type = { |
| 1736 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1737 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1738 | .kill_sb = cgroup_kill_sb, |
| 1739 | }; |
| 1740 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1741 | static struct kobject *cgroup_kobj; |
| 1742 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1743 | /** |
| 1744 | * cgroup_path - generate the path of a cgroup |
| 1745 | * @cgrp: the cgroup in question |
| 1746 | * @buf: the buffer to write the path into |
| 1747 | * @buflen: the length of the buffer |
| 1748 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1749 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1750 | * |
| 1751 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1752 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1753 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1754 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1755 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1756 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1757 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1758 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1759 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1760 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1761 | if (!cgrp->parent) { |
| 1762 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1763 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1764 | return 0; |
| 1765 | } |
| 1766 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1767 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1768 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1769 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1770 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1771 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1772 | const char *name = cgroup_name(cgrp); |
| 1773 | int len; |
| 1774 | |
| 1775 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1776 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1777 | goto out; |
| 1778 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1779 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1780 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1781 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1782 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1783 | |
| 1784 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1785 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1786 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1787 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1788 | out: |
| 1789 | rcu_read_unlock(); |
| 1790 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1791 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1792 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1793 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1794 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1795 | * 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] | 1796 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1797 | * @buf: the buffer to write the path into |
| 1798 | * @buflen: the length of the buffer |
| 1799 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1800 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1801 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1802 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1803 | * cgroup controller callbacks. |
| 1804 | * |
| 1805 | * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short. |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1806 | */ |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1807 | int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1808 | { |
| 1809 | struct cgroupfs_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1810 | struct cgroup *cgrp; |
| 1811 | int hierarchy_id = 1, ret = 0; |
| 1812 | |
| 1813 | if (buflen < 2) |
| 1814 | return -ENAMETOOLONG; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1815 | |
| 1816 | mutex_lock(&cgroup_mutex); |
| 1817 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1818 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1819 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1820 | if (root) { |
| 1821 | cgrp = task_cgroup_from_root(task, root); |
| 1822 | ret = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1823 | } else { |
| 1824 | /* if no hierarchy exists, everyone is in "/" */ |
| 1825 | memcpy(buf, "/", 2); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1829 | return ret; |
| 1830 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1831 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1832 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1833 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1834 | * Control Group taskset |
| 1835 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1836 | struct task_and_cgroup { |
| 1837 | struct task_struct *task; |
| 1838 | struct cgroup *cgrp; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1839 | struct css_set *cset; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1840 | }; |
| 1841 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1842 | struct cgroup_taskset { |
| 1843 | struct task_and_cgroup single; |
| 1844 | struct flex_array *tc_array; |
| 1845 | int tc_array_len; |
| 1846 | int idx; |
| 1847 | struct cgroup *cur_cgrp; |
| 1848 | }; |
| 1849 | |
| 1850 | /** |
| 1851 | * cgroup_taskset_first - reset taskset and return the first task |
| 1852 | * @tset: taskset of interest |
| 1853 | * |
| 1854 | * @tset iteration is initialized and the first task is returned. |
| 1855 | */ |
| 1856 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1857 | { |
| 1858 | if (tset->tc_array) { |
| 1859 | tset->idx = 0; |
| 1860 | return cgroup_taskset_next(tset); |
| 1861 | } else { |
| 1862 | tset->cur_cgrp = tset->single.cgrp; |
| 1863 | return tset->single.task; |
| 1864 | } |
| 1865 | } |
| 1866 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1867 | |
| 1868 | /** |
| 1869 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1870 | * @tset: taskset of interest |
| 1871 | * |
| 1872 | * Return the next task in @tset. Iteration must have been initialized |
| 1873 | * with cgroup_taskset_first(). |
| 1874 | */ |
| 1875 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1876 | { |
| 1877 | struct task_and_cgroup *tc; |
| 1878 | |
| 1879 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1880 | return NULL; |
| 1881 | |
| 1882 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1883 | tset->cur_cgrp = tc->cgrp; |
| 1884 | return tc->task; |
| 1885 | } |
| 1886 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1887 | |
| 1888 | /** |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1889 | * cgroup_taskset_cur_css - return the matching css for the current task |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1890 | * @tset: taskset of interest |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1891 | * @subsys_id: the ID of the target subsystem |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1892 | * |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1893 | * Return the css for the current (last returned) task of @tset for |
| 1894 | * subsystem specified by @subsys_id. This function must be preceded by |
| 1895 | * either cgroup_taskset_first() or cgroup_taskset_next(). |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1896 | */ |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1897 | struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset, |
| 1898 | int subsys_id) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1899 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1900 | return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1901 | } |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1902 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1903 | |
| 1904 | /** |
| 1905 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1906 | * @tset: taskset of interest |
| 1907 | */ |
| 1908 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1909 | { |
| 1910 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1911 | } |
| 1912 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1913 | |
| 1914 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1915 | /* |
| 1916 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1917 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1918 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1919 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1920 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1921 | struct task_struct *tsk, |
| 1922 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1923 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1924 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1925 | |
| 1926 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1927 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1928 | * setting such that we can't race against cgroup_exit() changing the |
| 1929 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1930 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1931 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1932 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1933 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1934 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1935 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1936 | task_unlock(tsk); |
| 1937 | |
| 1938 | /* Update the css_set linked lists if we're using them */ |
| 1939 | write_lock(&css_set_lock); |
| 1940 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1941 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1942 | write_unlock(&css_set_lock); |
| 1943 | |
| 1944 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1945 | * We just gained a reference on old_cset by taking it from the |
| 1946 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1947 | * 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] | 1948 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1949 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1950 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1951 | } |
| 1952 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1953 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1954 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1955 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1956 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1957 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1958 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1959 | * Call holding cgroup_mutex and the group_rwsem of the leader. Will take |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1960 | * task_lock of @tsk or each thread in the threadgroup individually in turn. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1961 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1962 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1963 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1964 | { |
| 1965 | int retval, i, group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1966 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1967 | struct cgroup_subsys_state *css, *failed_css = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1968 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1969 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1970 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1971 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1972 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1973 | |
| 1974 | /* |
| 1975 | * step 0: in order to do expensive, possibly blocking operations for |
| 1976 | * every thread, we cannot iterate the thread group list, since it needs |
| 1977 | * rcu or tasklist locked. instead, build an array of all threads in the |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1978 | * group - group_rwsem prevents new threads from appearing, and if |
| 1979 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1980 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1981 | if (threadgroup) |
| 1982 | group_size = get_nr_threads(tsk); |
| 1983 | else |
| 1984 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1985 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1986 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1987 | if (!group) |
| 1988 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1989 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 1990 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1991 | if (retval) |
| 1992 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1993 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1994 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1995 | /* |
| 1996 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 1997 | * already PF_EXITING could be freed from underneath us unless we |
| 1998 | * take an rcu_read_lock. |
| 1999 | */ |
| 2000 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2001 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2002 | struct task_and_cgroup ent; |
| 2003 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2004 | /* @tsk either already exited or can't exit until the end */ |
| 2005 | if (tsk->flags & PF_EXITING) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2006 | goto next; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2007 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2008 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2009 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2010 | ent.task = tsk; |
| 2011 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2012 | /* nothing to do if this task is already in the cgroup */ |
| 2013 | if (ent.cgrp == cgrp) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2014 | goto next; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2015 | /* |
| 2016 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2017 | * earlier, but it's good form to communicate our expectations. |
| 2018 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2019 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2020 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2021 | i++; |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2022 | next: |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2023 | if (!threadgroup) |
| 2024 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2025 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2026 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2027 | /* remember the number of threads in the array for later. */ |
| 2028 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2029 | tset.tc_array = group; |
| 2030 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2031 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2032 | /* methods shouldn't be called if no task is actually migrating */ |
| 2033 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2034 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2035 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2036 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2037 | /* |
| 2038 | * step 1: check that we can legitimately attach to the cgroup. |
| 2039 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2040 | for_each_css(css, i, cgrp) { |
| 2041 | if (css->ss->can_attach) { |
| 2042 | retval = css->ss->can_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2043 | if (retval) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2044 | failed_css = css; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2045 | goto out_cancel_attach; |
| 2046 | } |
| 2047 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | /* |
| 2051 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2052 | * we use find_css_set, which allocates a new one if necessary. |
| 2053 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2054 | for (i = 0; i < group_size; i++) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2055 | struct css_set *old_cset; |
| 2056 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2057 | tc = flex_array_get(group, i); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2058 | old_cset = task_css_set(tc->task); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2059 | tc->cset = find_css_set(old_cset, cgrp); |
| 2060 | if (!tc->cset) { |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2061 | retval = -ENOMEM; |
| 2062 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2067 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2068 | * proceed to move all tasks to the new cgroup. There are no |
| 2069 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2070 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2071 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2072 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2073 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2074 | } |
| 2075 | /* nothing is sensitive to fork() after this point. */ |
| 2076 | |
| 2077 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2078 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2079 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2080 | for_each_css(css, i, cgrp) |
| 2081 | if (css->ss->attach) |
| 2082 | css->ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2083 | |
| 2084 | /* |
| 2085 | * step 5: success! and cleanup |
| 2086 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2087 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2088 | out_put_css_set_refs: |
| 2089 | if (retval) { |
| 2090 | for (i = 0; i < group_size; i++) { |
| 2091 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2092 | if (!tc->cset) |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2093 | break; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2094 | put_css_set(tc->cset); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2095 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2096 | } |
| 2097 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2098 | if (retval) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2099 | for_each_css(css, i, cgrp) { |
| 2100 | if (css == failed_css) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2101 | break; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2102 | if (css->ss->cancel_attach) |
| 2103 | css->ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2104 | } |
| 2105 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2106 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2107 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2108 | return retval; |
| 2109 | } |
| 2110 | |
| 2111 | /* |
| 2112 | * 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] | 2113 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2114 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2115 | */ |
| 2116 | 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] | 2117 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2118 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2119 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2120 | int ret; |
| 2121 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2122 | if (!cgroup_lock_live_group(cgrp)) |
| 2123 | return -ENODEV; |
| 2124 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2125 | retry_find_task: |
| 2126 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2127 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2128 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2129 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2130 | rcu_read_unlock(); |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2131 | ret = -ESRCH; |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2132 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2133 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2134 | /* |
| 2135 | * even if we're attaching all tasks in the thread group, we |
| 2136 | * only need to check permissions on one of them. |
| 2137 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2138 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2139 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2140 | !uid_eq(cred->euid, tcred->uid) && |
| 2141 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2142 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2143 | ret = -EACCES; |
| 2144 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2145 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2146 | } else |
| 2147 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2148 | |
| 2149 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2150 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2151 | |
| 2152 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2153 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2154 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2155 | * with no rt_runtime allocated. Just say no. |
| 2156 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2157 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2158 | ret = -EINVAL; |
| 2159 | rcu_read_unlock(); |
| 2160 | goto out_unlock_cgroup; |
| 2161 | } |
| 2162 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2163 | get_task_struct(tsk); |
| 2164 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2165 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2166 | threadgroup_lock(tsk); |
| 2167 | if (threadgroup) { |
| 2168 | if (!thread_group_leader(tsk)) { |
| 2169 | /* |
| 2170 | * a race with de_thread from another thread's exec() |
| 2171 | * may strip us of our leadership, if this happens, |
| 2172 | * there is no choice but to throw this task away and |
| 2173 | * try again; this is |
| 2174 | * "double-double-toil-and-trouble-check locking". |
| 2175 | */ |
| 2176 | threadgroup_unlock(tsk); |
| 2177 | put_task_struct(tsk); |
| 2178 | goto retry_find_task; |
| 2179 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2183 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2184 | threadgroup_unlock(tsk); |
| 2185 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2186 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2187 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2188 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2189 | return ret; |
| 2190 | } |
| 2191 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2192 | /** |
| 2193 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2194 | * @from: attach to all cgroups of a given task |
| 2195 | * @tsk: the task to be attached |
| 2196 | */ |
| 2197 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2198 | { |
| 2199 | struct cgroupfs_root *root; |
| 2200 | int retval = 0; |
| 2201 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2202 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2203 | for_each_active_root(root) { |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2204 | struct cgroup *from_cgrp = task_cgroup_from_root(from, root); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2205 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2206 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2207 | if (retval) |
| 2208 | break; |
| 2209 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2210 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2211 | |
| 2212 | return retval; |
| 2213 | } |
| 2214 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2215 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2216 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2217 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2218 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2219 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2222 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2223 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2224 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2225 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2228 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
| 2229 | struct cftype *cft, const char *buffer) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2230 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2231 | BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2232 | if (strlen(buffer) >= PATH_MAX) |
| 2233 | return -EINVAL; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2234 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2235 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2236 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2237 | strcpy(css->cgroup->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2238 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2239 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2240 | return 0; |
| 2241 | } |
| 2242 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2243 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2244 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2245 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2246 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2247 | if (!cgroup_lock_live_group(cgrp)) |
| 2248 | return -ENODEV; |
| 2249 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2250 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2251 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2252 | return 0; |
| 2253 | } |
| 2254 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2255 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2256 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2257 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2258 | |
| 2259 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2260 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2261 | } |
| 2262 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2263 | /* A buffer size big enough for numbers or short strings */ |
| 2264 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2265 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2266 | static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf, |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2267 | size_t nbytes, loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2268 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2269 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2270 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2271 | struct cgroup_subsys_state *css = cfe->css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2272 | size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1; |
| 2273 | char *buf; |
| 2274 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2275 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2276 | if (nbytes >= max_bytes) |
| 2277 | return -E2BIG; |
| 2278 | |
| 2279 | buf = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2280 | if (!buf) |
| 2281 | return -ENOMEM; |
| 2282 | |
| 2283 | if (copy_from_user(buf, userbuf, nbytes)) { |
| 2284 | ret = -EFAULT; |
| 2285 | goto out_free; |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2286 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2287 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2288 | buf[nbytes] = '\0'; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2289 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2290 | if (cft->write_string) { |
| 2291 | ret = cft->write_string(css, cft, strstrip(buf)); |
| 2292 | } else if (cft->write_u64) { |
| 2293 | unsigned long long v; |
| 2294 | ret = kstrtoull(buf, 0, &v); |
| 2295 | if (!ret) |
| 2296 | ret = cft->write_u64(css, cft, v); |
| 2297 | } else if (cft->write_s64) { |
| 2298 | long long v; |
| 2299 | ret = kstrtoll(buf, 0, &v); |
| 2300 | if (!ret) |
| 2301 | ret = cft->write_s64(css, cft, v); |
| 2302 | } else if (cft->trigger) { |
| 2303 | ret = cft->trigger(css, (unsigned int)cft->private); |
| 2304 | } else { |
| 2305 | ret = -EINVAL; |
| 2306 | } |
| 2307 | out_free: |
| 2308 | kfree(buf); |
| 2309 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2310 | } |
| 2311 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2312 | /* |
| 2313 | * seqfile ops/methods for returning structured data. Currently just |
| 2314 | * supports string->u64 maps, but can be extended in future. |
| 2315 | */ |
| 2316 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2317 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2318 | { |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2319 | struct cftype *cft = seq_cft(seq); |
| 2320 | |
| 2321 | if (cft->seq_start) { |
| 2322 | return cft->seq_start(seq, ppos); |
| 2323 | } else { |
| 2324 | /* |
| 2325 | * The same behavior and code as single_open(). Returns |
| 2326 | * !NULL if pos is at the beginning; otherwise, NULL. |
| 2327 | */ |
| 2328 | return NULL + !*ppos; |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 2333 | { |
| 2334 | struct cftype *cft = seq_cft(seq); |
| 2335 | |
| 2336 | if (cft->seq_next) { |
| 2337 | return cft->seq_next(seq, v, ppos); |
| 2338 | } else { |
| 2339 | /* |
| 2340 | * The same behavior and code as single_open(), always |
| 2341 | * terminate after the initial read. |
| 2342 | */ |
| 2343 | ++*ppos; |
| 2344 | return NULL; |
| 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 2349 | { |
| 2350 | struct cftype *cft = seq_cft(seq); |
| 2351 | |
| 2352 | if (cft->seq_stop) |
| 2353 | cft->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2354 | } |
| 2355 | |
| 2356 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2357 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2358 | struct cftype *cft = seq_cft(m); |
| 2359 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2360 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2361 | if (cft->seq_show) |
| 2362 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2363 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2364 | if (cft->read_u64) |
| 2365 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 2366 | else if (cft->read_s64) |
| 2367 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 2368 | else |
| 2369 | return -EINVAL; |
| 2370 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2371 | } |
| 2372 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2373 | static struct seq_operations cgroup_seq_operations = { |
| 2374 | .start = cgroup_seqfile_start, |
| 2375 | .next = cgroup_seqfile_next, |
| 2376 | .stop = cgroup_seqfile_stop, |
| 2377 | .show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2378 | }; |
| 2379 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2380 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2381 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2382 | struct cfent *cfe = __d_cfe(file->f_dentry); |
| 2383 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2384 | struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent); |
| 2385 | struct cgroup_subsys_state *css; |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2386 | struct cgroup_open_file *of; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2387 | int err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2388 | |
| 2389 | err = generic_file_open(inode, file); |
| 2390 | if (err) |
| 2391 | return err; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2392 | |
| 2393 | /* |
| 2394 | * If the file belongs to a subsystem, pin the css. Will be |
| 2395 | * unpinned either on open failure or release. This ensures that |
| 2396 | * @css stays alive for all file operations. |
| 2397 | */ |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2398 | rcu_read_lock(); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2399 | css = cgroup_css(cgrp, cft->ss); |
| 2400 | if (cft->ss && !css_tryget(css)) |
| 2401 | css = NULL; |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2402 | rcu_read_unlock(); |
| 2403 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2404 | if (!css) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2405 | return -ENODEV; |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2406 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2407 | /* |
| 2408 | * @cfe->css is used by read/write/close to determine the |
| 2409 | * associated css. @file->private_data would be a better place but |
| 2410 | * that's already used by seqfile. Multiple accessors may use it |
| 2411 | * simultaneously which is okay as the association never changes. |
| 2412 | */ |
| 2413 | WARN_ON_ONCE(cfe->css && cfe->css != css); |
| 2414 | cfe->css = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2415 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2416 | of = __seq_open_private(file, &cgroup_seq_operations, |
| 2417 | sizeof(struct cgroup_open_file)); |
| 2418 | if (of) { |
| 2419 | of->cfe = cfe; |
| 2420 | return 0; |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2421 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2422 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2423 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2424 | css_put(css); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2425 | return -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2429 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2430 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2431 | struct cgroup_subsys_state *css = cfe->css; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2432 | |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2433 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2434 | css_put(css); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2435 | return seq_release_private(inode, file); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | /* |
| 2439 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2440 | */ |
| 2441 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2442 | struct inode *new_dir, struct dentry *new_dentry) |
| 2443 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2444 | int ret; |
| 2445 | struct cgroup_name *name, *old_name; |
| 2446 | struct cgroup *cgrp; |
| 2447 | |
| 2448 | /* |
| 2449 | * It's convinient to use parent dir's i_mutex to protected |
| 2450 | * cgrp->name. |
| 2451 | */ |
| 2452 | lockdep_assert_held(&old_dir->i_mutex); |
| 2453 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2454 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2455 | return -ENOTDIR; |
| 2456 | if (new_dentry->d_inode) |
| 2457 | return -EEXIST; |
| 2458 | if (old_dir != new_dir) |
| 2459 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2460 | |
| 2461 | cgrp = __d_cgrp(old_dentry); |
| 2462 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2463 | /* |
| 2464 | * This isn't a proper migration and its usefulness is very |
| 2465 | * limited. Disallow if sane_behavior. |
| 2466 | */ |
| 2467 | if (cgroup_sane_behavior(cgrp)) |
| 2468 | return -EPERM; |
| 2469 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2470 | name = cgroup_alloc_name(new_dentry); |
| 2471 | if (!name) |
| 2472 | return -ENOMEM; |
| 2473 | |
| 2474 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2475 | if (ret) { |
| 2476 | kfree(name); |
| 2477 | return ret; |
| 2478 | } |
| 2479 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 2480 | old_name = rcu_dereference_protected(cgrp->name, true); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2481 | rcu_assign_pointer(cgrp->name, name); |
| 2482 | |
| 2483 | kfree_rcu(old_name, rcu_head); |
| 2484 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2485 | } |
| 2486 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2487 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2488 | { |
| 2489 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2490 | return &__d_cgrp(dentry)->xattrs; |
| 2491 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2492 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | static inline int xattr_enabled(struct dentry *dentry) |
| 2496 | { |
| 2497 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2498 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | static bool is_valid_xattr(const char *name) |
| 2502 | { |
| 2503 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2504 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2505 | return true; |
| 2506 | return false; |
| 2507 | } |
| 2508 | |
| 2509 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2510 | const void *val, size_t size, int flags) |
| 2511 | { |
| 2512 | if (!xattr_enabled(dentry)) |
| 2513 | return -EOPNOTSUPP; |
| 2514 | if (!is_valid_xattr(name)) |
| 2515 | return -EINVAL; |
| 2516 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2517 | } |
| 2518 | |
| 2519 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2520 | { |
| 2521 | if (!xattr_enabled(dentry)) |
| 2522 | return -EOPNOTSUPP; |
| 2523 | if (!is_valid_xattr(name)) |
| 2524 | return -EINVAL; |
| 2525 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2526 | } |
| 2527 | |
| 2528 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2529 | void *buf, size_t size) |
| 2530 | { |
| 2531 | if (!xattr_enabled(dentry)) |
| 2532 | return -EOPNOTSUPP; |
| 2533 | if (!is_valid_xattr(name)) |
| 2534 | return -EINVAL; |
| 2535 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2536 | } |
| 2537 | |
| 2538 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2539 | { |
| 2540 | if (!xattr_enabled(dentry)) |
| 2541 | return -EOPNOTSUPP; |
| 2542 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2543 | } |
| 2544 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2545 | static const struct file_operations cgroup_file_operations = { |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2546 | .read = seq_read, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2547 | .write = cgroup_file_write, |
| 2548 | .llseek = generic_file_llseek, |
| 2549 | .open = cgroup_file_open, |
| 2550 | .release = cgroup_file_release, |
| 2551 | }; |
| 2552 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2553 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2554 | .setxattr = cgroup_setxattr, |
| 2555 | .getxattr = cgroup_getxattr, |
| 2556 | .listxattr = cgroup_listxattr, |
| 2557 | .removexattr = cgroup_removexattr, |
| 2558 | }; |
| 2559 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2560 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | 786e144 | 2013-07-14 17:50:23 +0400 | [diff] [blame] | 2561 | .lookup = simple_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2562 | .mkdir = cgroup_mkdir, |
| 2563 | .rmdir = cgroup_rmdir, |
| 2564 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2565 | .setxattr = cgroup_setxattr, |
| 2566 | .getxattr = cgroup_getxattr, |
| 2567 | .listxattr = cgroup_listxattr, |
| 2568 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2569 | }; |
| 2570 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2571 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2572 | struct super_block *sb) |
| 2573 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2574 | struct inode *inode; |
| 2575 | |
| 2576 | if (!dentry) |
| 2577 | return -ENOENT; |
| 2578 | if (dentry->d_inode) |
| 2579 | return -EEXIST; |
| 2580 | |
| 2581 | inode = cgroup_new_inode(mode, sb); |
| 2582 | if (!inode) |
| 2583 | return -ENOMEM; |
| 2584 | |
| 2585 | if (S_ISDIR(mode)) { |
| 2586 | inode->i_op = &cgroup_dir_inode_operations; |
| 2587 | inode->i_fop = &simple_dir_operations; |
| 2588 | |
| 2589 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2590 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2591 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2592 | |
Tejun Heo | b8a2df6a | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2593 | /* |
| 2594 | * Control reaches here with cgroup_mutex held. |
| 2595 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2596 | * want to populate it immediately without releasing |
| 2597 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2598 | * yet, trylock will always succeed without affecting |
| 2599 | * lockdep checks. |
| 2600 | */ |
| 2601 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2602 | } else if (S_ISREG(mode)) { |
| 2603 | inode->i_size = 0; |
| 2604 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2605 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2606 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2607 | d_instantiate(dentry, inode); |
| 2608 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2609 | return 0; |
| 2610 | } |
| 2611 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2612 | /** |
| 2613 | * cgroup_file_mode - deduce file mode of a control file |
| 2614 | * @cft: the control file in question |
| 2615 | * |
| 2616 | * returns cft->mode if ->mode is not 0 |
| 2617 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2618 | * returns S_IRUGO if it has only a read handler |
| 2619 | * returns S_IWUSR if it has only a write hander |
| 2620 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2621 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2622 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2623 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2624 | |
| 2625 | if (cft->mode) |
| 2626 | return cft->mode; |
| 2627 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2628 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2629 | mode |= S_IRUGO; |
| 2630 | |
Tejun Heo | 6e0755b | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2631 | if (cft->write_u64 || cft->write_s64 || cft->write_string || |
| 2632 | cft->trigger) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2633 | mode |= S_IWUSR; |
| 2634 | |
| 2635 | return mode; |
| 2636 | } |
| 2637 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2638 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2639 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2640 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2641 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2642 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2643 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2644 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2645 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2646 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2647 | |
Tejun Heo | 9fa4db3 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2648 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 2649 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2650 | strcpy(name, cft->ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2651 | strcat(name, "."); |
| 2652 | } |
| 2653 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2654 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2655 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2656 | |
| 2657 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2658 | if (!cfe) |
| 2659 | return -ENOMEM; |
| 2660 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2661 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2662 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2663 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2664 | goto out; |
| 2665 | } |
| 2666 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2667 | cfe->type = (void *)cft; |
| 2668 | cfe->dentry = dentry; |
| 2669 | dentry->d_fsdata = cfe; |
| 2670 | simple_xattrs_init(&cfe->xattrs); |
| 2671 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2672 | mode = cgroup_file_mode(cft); |
| 2673 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2674 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2675 | list_add_tail(&cfe->node, &parent->files); |
| 2676 | cfe = NULL; |
| 2677 | } |
| 2678 | dput(dentry); |
| 2679 | out: |
| 2680 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2681 | return error; |
| 2682 | } |
| 2683 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2684 | /** |
| 2685 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2686 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2687 | * @cfts: array of cftypes to be added |
| 2688 | * @is_add: whether to add or remove |
| 2689 | * |
| 2690 | * 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] | 2691 | * For removals, this function never fails. If addition fails, this |
| 2692 | * function doesn't remove files already added. The caller is responsible |
| 2693 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2694 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2695 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2696 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2697 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2698 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2699 | int ret; |
| 2700 | |
| 2701 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 2702 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2703 | |
| 2704 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2705 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2706 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2707 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2708 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2709 | continue; |
| 2710 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2711 | continue; |
| 2712 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2713 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2714 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2715 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2716 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2717 | cft->name, ret); |
| 2718 | return ret; |
| 2719 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2720 | } else { |
| 2721 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2722 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2723 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2724 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2725 | } |
| 2726 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2727 | static void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2728 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2729 | { |
| 2730 | /* |
| 2731 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2732 | * the existing cgroups under cgroup_mutex and create files. |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2733 | * Instead, we use css_for_each_descendant_pre() and drop RCU read |
| 2734 | * lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2735 | */ |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2736 | mutex_lock(&cgroup_mutex); |
| 2737 | } |
| 2738 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2739 | static int cgroup_cfts_commit(struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2740 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2741 | { |
| 2742 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2743 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2744 | struct cgroup *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2745 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2746 | struct dentry *prev = NULL; |
| 2747 | struct inode *inode; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2748 | struct cgroup_subsys_state *css; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2749 | u64 update_before; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2750 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2751 | |
| 2752 | /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 2753 | if (!cfts || ss->root == &cgroup_dummy_root || |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2754 | !atomic_inc_not_zero(&sb->s_active)) { |
| 2755 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2756 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2759 | /* |
| 2760 | * All cgroups which are created after we drop cgroup_mutex will |
| 2761 | * have the updated set of files, so we only need to update the |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2762 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2763 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2764 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2765 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2766 | mutex_unlock(&cgroup_mutex); |
| 2767 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2768 | /* add/rm files for all cgroups created before */ |
| 2769 | rcu_read_lock(); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2770 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2771 | struct cgroup *cgrp = css->cgroup; |
| 2772 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2773 | if (cgroup_is_dead(cgrp)) |
| 2774 | continue; |
| 2775 | |
| 2776 | inode = cgrp->dentry->d_inode; |
| 2777 | dget(cgrp->dentry); |
| 2778 | rcu_read_unlock(); |
| 2779 | |
| 2780 | dput(prev); |
| 2781 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2782 | |
| 2783 | mutex_lock(&inode->i_mutex); |
| 2784 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2785 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2786 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2787 | mutex_unlock(&cgroup_mutex); |
| 2788 | mutex_unlock(&inode->i_mutex); |
| 2789 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2790 | rcu_read_lock(); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2791 | if (ret) |
| 2792 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2793 | } |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2794 | rcu_read_unlock(); |
| 2795 | dput(prev); |
| 2796 | deactivate_super(sb); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2797 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | /** |
| 2801 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2802 | * @ss: target cgroup subsystem |
| 2803 | * @cfts: zero-length name terminated array of cftypes |
| 2804 | * |
| 2805 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2806 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2807 | * have them too. This function can be called anytime whether @ss is |
| 2808 | * attached or not. |
| 2809 | * |
| 2810 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2811 | * function currently returns 0 as long as @cfts registration is successful |
| 2812 | * even if some file creation attempts on existing cgroups fail. |
| 2813 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2814 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2815 | { |
| 2816 | struct cftype_set *set; |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2817 | struct cftype *cft; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2818 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2819 | |
| 2820 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2821 | if (!set) |
| 2822 | return -ENOMEM; |
| 2823 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2824 | for (cft = cfts; cft->name[0] != '\0'; cft++) |
| 2825 | cft->ss = ss; |
| 2826 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2827 | cgroup_cfts_prepare(); |
| 2828 | set->cfts = cfts; |
| 2829 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2830 | ret = cgroup_cfts_commit(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2831 | if (ret) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2832 | cgroup_rm_cftypes(cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2833 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2834 | } |
| 2835 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2836 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2837 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2838 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2839 | * @cfts: zero-length name terminated array of cftypes |
| 2840 | * |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2841 | * Unregister @cfts. Files described by @cfts are removed from all |
| 2842 | * existing cgroups and all future cgroups won't have them either. This |
| 2843 | * function can be called anytime whether @cfts' subsys is attached or not. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2844 | * |
| 2845 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2846 | * registered. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2847 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2848 | int cgroup_rm_cftypes(struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2849 | { |
| 2850 | struct cftype_set *set; |
| 2851 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2852 | if (!cfts || !cfts[0].ss) |
| 2853 | return -ENOENT; |
| 2854 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2855 | cgroup_cfts_prepare(); |
| 2856 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2857 | list_for_each_entry(set, &cfts[0].ss->cftsets, node) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2858 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2859 | list_del(&set->node); |
| 2860 | kfree(set); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2861 | cgroup_cfts_commit(cfts, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2862 | return 0; |
| 2863 | } |
| 2864 | } |
| 2865 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2866 | cgroup_cfts_commit(NULL, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2867 | return -ENOENT; |
| 2868 | } |
| 2869 | |
| 2870 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2871 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2872 | * @cgrp: the cgroup in question |
| 2873 | * |
| 2874 | * Return the number of tasks in the cgroup. |
| 2875 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2876 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2877 | { |
| 2878 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2879 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2880 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2881 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2882 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2883 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2884 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2885 | return count; |
| 2886 | } |
| 2887 | |
| 2888 | /* |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2889 | * To reduce the fork() overhead for systems that are not actually using |
| 2890 | * their cgroups capability, we don't maintain the lists running through |
| 2891 | * each css_set to its tasks until we see the list actually used - in other |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2892 | * words after the first call to css_task_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2893 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2894 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2895 | { |
| 2896 | struct task_struct *p, *g; |
| 2897 | write_lock(&css_set_lock); |
| 2898 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2899 | /* |
| 2900 | * We need tasklist_lock because RCU is not safe against |
| 2901 | * while_each_thread(). Besides, a forking task that has passed |
| 2902 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2903 | * is not guaranteed to have its child immediately visible in the |
| 2904 | * tasklist if we walk through it with RCU. |
| 2905 | */ |
| 2906 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2907 | do_each_thread(g, p) { |
| 2908 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2909 | /* |
| 2910 | * We should check if the process is exiting, otherwise |
| 2911 | * it will race with cgroup_exit() in that the list |
| 2912 | * entry won't be deleted though the process has exited. |
| 2913 | */ |
| 2914 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2915 | list_add(&p->cg_list, &task_css_set(p)->tasks); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2916 | task_unlock(p); |
| 2917 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2918 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2919 | write_unlock(&css_set_lock); |
| 2920 | } |
| 2921 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2922 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2923 | * css_next_child - find the next child of a given css |
| 2924 | * @pos_css: the current position (%NULL to initiate traversal) |
| 2925 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2926 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2927 | * 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] | 2928 | * under either cgroup_mutex or RCU read lock. The only requirement is |
| 2929 | * that @parent_css and @pos_css are accessible. The next sibling is |
| 2930 | * guaranteed to be returned regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2931 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2932 | struct cgroup_subsys_state * |
| 2933 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 2934 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2935 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2936 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 2937 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2938 | struct cgroup *next; |
| 2939 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2940 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2941 | |
| 2942 | /* |
| 2943 | * @pos could already have been removed. Once a cgroup is removed, |
| 2944 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 2945 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 2946 | * before the cgroup is taken off the ->sibling list, if we see it |
| 2947 | * unasserted, it's guaranteed that the next sibling hasn't |
| 2948 | * finished its grace period even if it's already removed, and thus |
| 2949 | * safe to dereference from this RCU critical section. If |
| 2950 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 2951 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2952 | * |
| 2953 | * If @pos is dead, its next pointer can't be dereferenced; |
| 2954 | * however, as each cgroup is given a monotonically increasing |
| 2955 | * unique serial number and always appended to the sibling list, |
| 2956 | * the next one can be found by walking the parent's children until |
| 2957 | * we see a cgroup with higher serial number than @pos's. While |
| 2958 | * this path can be slower, it's taken only when either the current |
| 2959 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2960 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2961 | if (!pos) { |
| 2962 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 2963 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2964 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2965 | } else { |
| 2966 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 2967 | if (next->serial_nr > pos->serial_nr) |
| 2968 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2969 | } |
| 2970 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2971 | if (&next->sibling == &cgrp->children) |
| 2972 | return NULL; |
| 2973 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2974 | return cgroup_css(next, parent_css->ss); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2975 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2976 | EXPORT_SYMBOL_GPL(css_next_child); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2977 | |
| 2978 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2979 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2980 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2981 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2982 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2983 | * 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] | 2984 | * to visit for pre-order traversal of @root's descendants. @root is |
| 2985 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2986 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2987 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2988 | * doesn't require the whole traversal to be contained in a single critical |
| 2989 | * section. This function will return the correct next descendant as long |
| 2990 | * 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] | 2991 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2992 | struct cgroup_subsys_state * |
| 2993 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 2994 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2995 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2996 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2997 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2998 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2999 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3000 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3001 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3002 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3003 | |
| 3004 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3005 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3006 | if (next) |
| 3007 | return next; |
| 3008 | |
| 3009 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3010 | while (pos != root) { |
| 3011 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3012 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3013 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3014 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3015 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3016 | |
| 3017 | return NULL; |
| 3018 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3019 | EXPORT_SYMBOL_GPL(css_next_descendant_pre); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3020 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3021 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3022 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 3023 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3024 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3025 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 3026 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3027 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3028 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3029 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3030 | * doesn't require the whole traversal to be contained in a single critical |
| 3031 | * section. This function will return the correct rightmost descendant as |
| 3032 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3033 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3034 | struct cgroup_subsys_state * |
| 3035 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3036 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3037 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3038 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3039 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3040 | |
| 3041 | do { |
| 3042 | last = pos; |
| 3043 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3044 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3045 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3046 | pos = tmp; |
| 3047 | } while (pos); |
| 3048 | |
| 3049 | return last; |
| 3050 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3051 | EXPORT_SYMBOL_GPL(css_rightmost_descendant); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3052 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3053 | static struct cgroup_subsys_state * |
| 3054 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3055 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3056 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3057 | |
| 3058 | do { |
| 3059 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3060 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3061 | } while (pos); |
| 3062 | |
| 3063 | return last; |
| 3064 | } |
| 3065 | |
| 3066 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3067 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3068 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3069 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3070 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3071 | * 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] | 3072 | * to visit for post-order traversal of @root's descendants. @root is |
| 3073 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3074 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3075 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3076 | * doesn't require the whole traversal to be contained in a single critical |
| 3077 | * section. This function will return the correct next descendant as long |
| 3078 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 3079 | * @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3080 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3081 | struct cgroup_subsys_state * |
| 3082 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 3083 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3084 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3085 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3086 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3087 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3088 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 3089 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 3090 | if (!pos) |
| 3091 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3092 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3093 | /* if we visited @root, we're done */ |
| 3094 | if (pos == root) |
| 3095 | return NULL; |
| 3096 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3097 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3098 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3099 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3100 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3101 | |
| 3102 | /* no sibling left, visit parent */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3103 | return css_parent(pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3104 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3105 | EXPORT_SYMBOL_GPL(css_next_descendant_post); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3106 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3107 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3108 | * 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] | 3109 | * @it: the iterator to advance |
| 3110 | * |
| 3111 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3112 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3113 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3114 | { |
| 3115 | struct list_head *l = it->cset_link; |
| 3116 | struct cgrp_cset_link *link; |
| 3117 | struct css_set *cset; |
| 3118 | |
| 3119 | /* Advance to the next non-empty css_set */ |
| 3120 | do { |
| 3121 | l = l->next; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3122 | if (l == &it->origin_css->cgroup->cset_links) { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3123 | it->cset_link = NULL; |
| 3124 | return; |
| 3125 | } |
| 3126 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3127 | cset = link->cset; |
| 3128 | } while (list_empty(&cset->tasks)); |
| 3129 | it->cset_link = l; |
| 3130 | it->task = cset->tasks.next; |
| 3131 | } |
| 3132 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3133 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3134 | * css_task_iter_start - initiate task iteration |
| 3135 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3136 | * @it: the task iterator to use |
| 3137 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3138 | * Initiate iteration through the tasks of @css. The caller can call |
| 3139 | * css_task_iter_next() to walk through the tasks until the function |
| 3140 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 3141 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3142 | * |
| 3143 | * Note that this function acquires a lock which is released when the |
| 3144 | * iteration finishes. The caller can't sleep while iteration is in |
| 3145 | * progress. |
| 3146 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3147 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 3148 | struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3149 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3150 | { |
| 3151 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3152 | * The first time anyone tries to iterate across a css, we need to |
| 3153 | * enable the list linking each css_set to its tasks, and fix up |
| 3154 | * all existing tasks. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3155 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3156 | if (!use_task_css_set_links) |
| 3157 | cgroup_enable_task_cg_lists(); |
| 3158 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3159 | read_lock(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3160 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3161 | it->origin_css = css; |
| 3162 | it->cset_link = &css->cgroup->cset_links; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3163 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3164 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3165 | } |
| 3166 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3167 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3168 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3169 | * @it: the task iterator being iterated |
| 3170 | * |
| 3171 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3172 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 3173 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3174 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3175 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3176 | { |
| 3177 | struct task_struct *res; |
| 3178 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3179 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3180 | |
| 3181 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3182 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3183 | return NULL; |
| 3184 | res = list_entry(l, struct task_struct, cg_list); |
| 3185 | /* Advance iterator to find next entry */ |
| 3186 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3187 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3188 | if (l == &link->cset->tasks) { |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3189 | /* |
| 3190 | * We reached the end of this task list - move on to the |
| 3191 | * next cgrp_cset_link. |
| 3192 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3193 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3194 | } else { |
| 3195 | it->task = l; |
| 3196 | } |
| 3197 | return res; |
| 3198 | } |
| 3199 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3200 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3201 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3202 | * @it: the task iterator to finish |
| 3203 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3204 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3205 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3206 | void css_task_iter_end(struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3207 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3208 | { |
| 3209 | read_unlock(&css_set_lock); |
| 3210 | } |
| 3211 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3212 | static inline int started_after_time(struct task_struct *t1, |
| 3213 | struct timespec *time, |
| 3214 | struct task_struct *t2) |
| 3215 | { |
| 3216 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3217 | if (start_diff > 0) { |
| 3218 | return 1; |
| 3219 | } else if (start_diff < 0) { |
| 3220 | return 0; |
| 3221 | } else { |
| 3222 | /* |
| 3223 | * Arbitrarily, if two processes started at the same |
| 3224 | * time, we'll say that the lower pointer value |
| 3225 | * started first. Note that t2 may have exited by now |
| 3226 | * so this may not be a valid pointer any longer, but |
| 3227 | * that's fine - it still serves to distinguish |
| 3228 | * between two tasks started (effectively) simultaneously. |
| 3229 | */ |
| 3230 | return t1 > t2; |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | /* |
| 3235 | * This function is a callback from heap_insert() and is used to order |
| 3236 | * the heap. |
| 3237 | * In this case we order the heap in descending task start time. |
| 3238 | */ |
| 3239 | static inline int started_after(void *p1, void *p2) |
| 3240 | { |
| 3241 | struct task_struct *t1 = p1; |
| 3242 | struct task_struct *t2 = p2; |
| 3243 | return started_after_time(t1, &t2->start_time, t2); |
| 3244 | } |
| 3245 | |
| 3246 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3247 | * css_scan_tasks - iterate though all the tasks in a css |
| 3248 | * @css: the css to iterate tasks of |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3249 | * @test: optional test callback |
| 3250 | * @process: process callback |
| 3251 | * @data: data passed to @test and @process |
| 3252 | * @heap: optional pre-allocated heap used for task iteration |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3253 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3254 | * Iterate through all the tasks in @css, calling @test for each, and if it |
| 3255 | * returns %true, call @process for it also. |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3256 | * |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3257 | * @test may be NULL, meaning always true (select all tasks), which |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3258 | * effectively duplicates css_task_iter_{start,next,end}() but does not |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3259 | * lock css_set_lock for the call to @process. |
| 3260 | * |
| 3261 | * It is guaranteed that @process will act on every task that is a member |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3262 | * of @css for the duration of this call. This function may or may not |
| 3263 | * call @process for tasks that exit or move to a different css during the |
| 3264 | * call, or are forked or move into the css during the call. |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3265 | * |
| 3266 | * Note that @test may be called with locks held, and may in some |
| 3267 | * situations be called multiple times for the same task, so it should be |
| 3268 | * cheap. |
| 3269 | * |
| 3270 | * If @heap is non-NULL, a heap has been pre-allocated and will be used for |
| 3271 | * heap operations (and its "gt" member will be overwritten), else a |
| 3272 | * temporary heap will be used (allocation of which may cause this function |
| 3273 | * to fail). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3274 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3275 | int css_scan_tasks(struct cgroup_subsys_state *css, |
| 3276 | bool (*test)(struct task_struct *, void *), |
| 3277 | void (*process)(struct task_struct *, void *), |
| 3278 | void *data, struct ptr_heap *heap) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3279 | { |
| 3280 | int retval, i; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3281 | struct css_task_iter it; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3282 | struct task_struct *p, *dropped; |
| 3283 | /* Never dereference latest_task, since it's not refcounted */ |
| 3284 | struct task_struct *latest_task = NULL; |
| 3285 | struct ptr_heap tmp_heap; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3286 | struct timespec latest_time = { 0, 0 }; |
| 3287 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3288 | if (heap) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3289 | /* The caller supplied our heap and pre-allocated its memory */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3290 | heap->gt = &started_after; |
| 3291 | } else { |
| 3292 | /* We need to allocate our own heap memory */ |
| 3293 | heap = &tmp_heap; |
| 3294 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3295 | if (retval) |
| 3296 | /* cannot allocate the heap */ |
| 3297 | return retval; |
| 3298 | } |
| 3299 | |
| 3300 | again: |
| 3301 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3302 | * Scan tasks in the css, using the @test callback to determine |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3303 | * which are of interest, and invoking @process callback on the |
| 3304 | * ones which need an update. Since we don't want to hold any |
| 3305 | * locks during the task updates, gather tasks to be processed in a |
| 3306 | * heap structure. The heap is sorted by descending task start |
| 3307 | * time. If the statically-sized heap fills up, we overflow tasks |
| 3308 | * that started later, and in future iterations only consider tasks |
| 3309 | * that started after the latest task in the previous pass. This |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3310 | * guarantees forward progress and that we don't miss any tasks. |
| 3311 | */ |
| 3312 | heap->size = 0; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3313 | css_task_iter_start(css, &it); |
| 3314 | while ((p = css_task_iter_next(&it))) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3315 | /* |
| 3316 | * Only affect tasks that qualify per the caller's callback, |
| 3317 | * if he provided one |
| 3318 | */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3319 | if (test && !test(p, data)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3320 | continue; |
| 3321 | /* |
| 3322 | * Only process tasks that started after the last task |
| 3323 | * we processed |
| 3324 | */ |
| 3325 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3326 | continue; |
| 3327 | dropped = heap_insert(heap, p); |
| 3328 | if (dropped == NULL) { |
| 3329 | /* |
| 3330 | * The new task was inserted; the heap wasn't |
| 3331 | * previously full |
| 3332 | */ |
| 3333 | get_task_struct(p); |
| 3334 | } else if (dropped != p) { |
| 3335 | /* |
| 3336 | * The new task was inserted, and pushed out a |
| 3337 | * different task |
| 3338 | */ |
| 3339 | get_task_struct(p); |
| 3340 | put_task_struct(dropped); |
| 3341 | } |
| 3342 | /* |
| 3343 | * Else the new task was newer than anything already in |
| 3344 | * the heap and wasn't inserted |
| 3345 | */ |
| 3346 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3347 | css_task_iter_end(&it); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3348 | |
| 3349 | if (heap->size) { |
| 3350 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3351 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3352 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3353 | latest_time = q->start_time; |
| 3354 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3355 | } |
| 3356 | /* Process the task per the caller's callback */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3357 | process(q, data); |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3358 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3359 | } |
| 3360 | /* |
| 3361 | * If we had to process any tasks at all, scan again |
| 3362 | * in case some of them were in the middle of forking |
| 3363 | * children that didn't get processed. |
| 3364 | * Not the most efficient way to do it, but it avoids |
| 3365 | * having to take callback_mutex in the fork path |
| 3366 | */ |
| 3367 | goto again; |
| 3368 | } |
| 3369 | if (heap == &tmp_heap) |
| 3370 | heap_free(&tmp_heap); |
| 3371 | return 0; |
| 3372 | } |
| 3373 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3374 | static void cgroup_transfer_one_task(struct task_struct *task, void *data) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3375 | { |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3376 | struct cgroup *new_cgroup = data; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3377 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3378 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3379 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3380 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | /** |
| 3384 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3385 | * @to: cgroup to which the tasks will be moved |
| 3386 | * @from: cgroup in which the tasks currently reside |
| 3387 | */ |
| 3388 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3389 | { |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3390 | return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task, |
| 3391 | to, NULL); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3392 | } |
| 3393 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3394 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3395 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3396 | * |
| 3397 | * Reading this file can return large amounts of data if a cgroup has |
| 3398 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3399 | * but we cannot guarantee that the information we produce is correct |
| 3400 | * unless we produce it entirely atomically. |
| 3401 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3402 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3403 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3404 | /* which pidlist file are we talking about? */ |
| 3405 | enum cgroup_filetype { |
| 3406 | CGROUP_FILE_PROCS, |
| 3407 | CGROUP_FILE_TASKS, |
| 3408 | }; |
| 3409 | |
| 3410 | /* |
| 3411 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3412 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3413 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3414 | * to the cgroup. |
| 3415 | */ |
| 3416 | struct cgroup_pidlist { |
| 3417 | /* |
| 3418 | * used to find which pidlist is wanted. doesn't change as long as |
| 3419 | * this particular list stays in the list. |
| 3420 | */ |
| 3421 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3422 | /* array of xids */ |
| 3423 | pid_t *list; |
| 3424 | /* how many elements the above list has */ |
| 3425 | int length; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3426 | /* each of these stored in a list by its cgroup */ |
| 3427 | struct list_head links; |
| 3428 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3429 | struct cgroup *owner; |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3430 | /* for delayed destruction */ |
| 3431 | struct delayed_work destroy_dwork; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3432 | }; |
| 3433 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3434 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3435 | * The following two functions "fix" the issue where there are more pids |
| 3436 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3437 | * TODO: replace with a kernel-wide solution to this problem |
| 3438 | */ |
| 3439 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3440 | static void *pidlist_allocate(int count) |
| 3441 | { |
| 3442 | if (PIDLIST_TOO_LARGE(count)) |
| 3443 | return vmalloc(count * sizeof(pid_t)); |
| 3444 | else |
| 3445 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3446 | } |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3447 | |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3448 | static void pidlist_free(void *p) |
| 3449 | { |
| 3450 | if (is_vmalloc_addr(p)) |
| 3451 | vfree(p); |
| 3452 | else |
| 3453 | kfree(p); |
| 3454 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3455 | |
| 3456 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3457 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 3458 | * should be left afterwards. |
| 3459 | */ |
| 3460 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 3461 | { |
| 3462 | struct cgroup_pidlist *l, *tmp_l; |
| 3463 | |
| 3464 | mutex_lock(&cgrp->pidlist_mutex); |
| 3465 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 3466 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 3467 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3468 | |
| 3469 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 3470 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 3471 | } |
| 3472 | |
| 3473 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 3474 | { |
| 3475 | struct delayed_work *dwork = to_delayed_work(work); |
| 3476 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 3477 | destroy_dwork); |
| 3478 | struct cgroup_pidlist *tofree = NULL; |
| 3479 | |
| 3480 | mutex_lock(&l->owner->pidlist_mutex); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3481 | |
| 3482 | /* |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3483 | * Destroy iff we didn't get queued again. The state won't change |
| 3484 | * as destroy_dwork can only be queued while locked. |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3485 | */ |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3486 | if (!delayed_work_pending(dwork)) { |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3487 | list_del(&l->links); |
| 3488 | pidlist_free(l->list); |
| 3489 | put_pid_ns(l->key.ns); |
| 3490 | tofree = l; |
| 3491 | } |
| 3492 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3493 | mutex_unlock(&l->owner->pidlist_mutex); |
| 3494 | kfree(tofree); |
| 3495 | } |
| 3496 | |
| 3497 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3498 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3499 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3500 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3501 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3502 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3503 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3504 | |
| 3505 | /* |
| 3506 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3507 | * edge cases first; no work needs to be done for either |
| 3508 | */ |
| 3509 | if (length == 0 || length == 1) |
| 3510 | return length; |
| 3511 | /* src and dest walk down the list; dest counts unique elements */ |
| 3512 | for (src = 1; src < length; src++) { |
| 3513 | /* find next unique element */ |
| 3514 | while (list[src] == list[src-1]) { |
| 3515 | src++; |
| 3516 | if (src == length) |
| 3517 | goto after; |
| 3518 | } |
| 3519 | /* dest always points to where the next unique element goes */ |
| 3520 | list[dest] = list[src]; |
| 3521 | dest++; |
| 3522 | } |
| 3523 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3524 | return dest; |
| 3525 | } |
| 3526 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3527 | /* |
| 3528 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 3529 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 3530 | * different per namespace, each namespace needs differently sorted list, |
| 3531 | * making it impossible to use, for example, single rbtree of member tasks |
| 3532 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 3533 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 3534 | * pidlists keyed by cgroup and namespace. |
| 3535 | * |
| 3536 | * All this extra complexity was caused by the original implementation |
| 3537 | * committing to an entirely unnecessary property. In the long term, we |
| 3538 | * want to do away with it. Explicitly scramble sort order if |
| 3539 | * sane_behavior so that no such expectation exists in the new interface. |
| 3540 | * |
| 3541 | * Scrambling is done by swapping every two consecutive bits, which is |
| 3542 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 3543 | */ |
| 3544 | static pid_t pid_fry(pid_t pid) |
| 3545 | { |
| 3546 | unsigned a = pid & 0x55555555; |
| 3547 | unsigned b = pid & 0xAAAAAAAA; |
| 3548 | |
| 3549 | return (a << 1) | (b >> 1); |
| 3550 | } |
| 3551 | |
| 3552 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 3553 | { |
| 3554 | if (cgroup_sane_behavior(cgrp)) |
| 3555 | return pid_fry(pid); |
| 3556 | else |
| 3557 | return pid; |
| 3558 | } |
| 3559 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3560 | static int cmppid(const void *a, const void *b) |
| 3561 | { |
| 3562 | return *(pid_t *)a - *(pid_t *)b; |
| 3563 | } |
| 3564 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3565 | static int fried_cmppid(const void *a, const void *b) |
| 3566 | { |
| 3567 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 3568 | } |
| 3569 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3570 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3571 | enum cgroup_filetype type) |
| 3572 | { |
| 3573 | struct cgroup_pidlist *l; |
| 3574 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3575 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3576 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3577 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3578 | |
| 3579 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 3580 | if (l->key.type == type && l->key.ns == ns) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3581 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3582 | return NULL; |
| 3583 | } |
| 3584 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3585 | /* |
| 3586 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3587 | * returns with the lock on that pidlist already held, and takes care |
| 3588 | * of the use count, or returns NULL with no locks held if we're out of |
| 3589 | * memory. |
| 3590 | */ |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3591 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 3592 | enum cgroup_filetype type) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3593 | { |
| 3594 | struct cgroup_pidlist *l; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3595 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3596 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3597 | |
| 3598 | l = cgroup_pidlist_find(cgrp, type); |
| 3599 | if (l) |
| 3600 | return l; |
| 3601 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3602 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3603 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3604 | if (!l) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3605 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3606 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3607 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3608 | l->key.type = type; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3609 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 3610 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3611 | l->owner = cgrp; |
| 3612 | list_add(&l->links, &cgrp->pidlists); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3613 | return l; |
| 3614 | } |
| 3615 | |
| 3616 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3617 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3618 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3619 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3620 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3621 | { |
| 3622 | pid_t *array; |
| 3623 | int length; |
| 3624 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3625 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3626 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3627 | struct cgroup_pidlist *l; |
| 3628 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3629 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3630 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3631 | /* |
| 3632 | * If cgroup gets more users after we read count, we won't have |
| 3633 | * enough space - tough. This race is indistinguishable to the |
| 3634 | * caller from the case that the additional cgroup users didn't |
| 3635 | * show up until sometime later on. |
| 3636 | */ |
| 3637 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3638 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3639 | if (!array) |
| 3640 | return -ENOMEM; |
| 3641 | /* now, populate the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3642 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3643 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3644 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3645 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3646 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3647 | if (type == CGROUP_FILE_PROCS) |
| 3648 | pid = task_tgid_vnr(tsk); |
| 3649 | else |
| 3650 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3651 | if (pid > 0) /* make sure to only use valid results */ |
| 3652 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3653 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3654 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3655 | length = n; |
| 3656 | /* now sort & (if procs) strip out duplicates */ |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3657 | if (cgroup_sane_behavior(cgrp)) |
| 3658 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 3659 | else |
| 3660 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3661 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3662 | length = pidlist_uniq(array, length); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3663 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3664 | l = cgroup_pidlist_find_create(cgrp, type); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3665 | if (!l) { |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3666 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3667 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3668 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3669 | } |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3670 | |
| 3671 | /* store array, freeing old if necessary */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3672 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3673 | l->list = array; |
| 3674 | l->length = length; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3675 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3676 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3677 | } |
| 3678 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3679 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3680 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3681 | * @stats: cgroupstats to fill information into |
| 3682 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3683 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3684 | * |
| 3685 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3686 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3687 | */ |
| 3688 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3689 | { |
| 3690 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3691 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3692 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3693 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3694 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3695 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3696 | * Validate dentry by checking the superblock operations, |
| 3697 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3698 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3699 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3700 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3701 | goto err; |
| 3702 | |
| 3703 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3704 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3705 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3706 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3707 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3708 | switch (tsk->state) { |
| 3709 | case TASK_RUNNING: |
| 3710 | stats->nr_running++; |
| 3711 | break; |
| 3712 | case TASK_INTERRUPTIBLE: |
| 3713 | stats->nr_sleeping++; |
| 3714 | break; |
| 3715 | case TASK_UNINTERRUPTIBLE: |
| 3716 | stats->nr_uninterruptible++; |
| 3717 | break; |
| 3718 | case TASK_STOPPED: |
| 3719 | stats->nr_stopped++; |
| 3720 | break; |
| 3721 | default: |
| 3722 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3723 | stats->nr_io_wait++; |
| 3724 | break; |
| 3725 | } |
| 3726 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3727 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3728 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3729 | err: |
| 3730 | return ret; |
| 3731 | } |
| 3732 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3733 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3734 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3735 | * 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] | 3736 | * 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] | 3737 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3738 | */ |
| 3739 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3740 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3741 | { |
| 3742 | /* |
| 3743 | * Initially we receive a position value that corresponds to |
| 3744 | * one more than the last pid shown (or 0 on the first call or |
| 3745 | * after a seek to the start). Use a binary-search to find the |
| 3746 | * next pid to display, if any |
| 3747 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3748 | struct cgroup_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3749 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3750 | struct cgroup_pidlist *l; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3751 | enum cgroup_filetype type = seq_cft(s)->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3752 | int index = 0, pid = *pos; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3753 | int *iter, ret; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3754 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3755 | mutex_lock(&cgrp->pidlist_mutex); |
| 3756 | |
| 3757 | /* |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3758 | * !NULL @of->priv indicates that this isn't the first start() |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3759 | * after open. If the matching pidlist is around, we can use that. |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3760 | * 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] | 3761 | * could already have been destroyed. |
| 3762 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3763 | if (of->priv) |
| 3764 | of->priv = cgroup_pidlist_find(cgrp, type); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3765 | |
| 3766 | /* |
| 3767 | * Either this is the first start() after open or the matching |
| 3768 | * pidlist has been destroyed inbetween. Create a new one. |
| 3769 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3770 | if (!of->priv) { |
| 3771 | ret = pidlist_array_load(cgrp, type, |
| 3772 | (struct cgroup_pidlist **)&of->priv); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3773 | if (ret) |
| 3774 | return ERR_PTR(ret); |
| 3775 | } |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3776 | l = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3777 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3778 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3779 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3780 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3781 | while (index < end) { |
| 3782 | int mid = (index + end) / 2; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3783 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3784 | index = mid; |
| 3785 | break; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3786 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3787 | index = mid + 1; |
| 3788 | else |
| 3789 | end = mid; |
| 3790 | } |
| 3791 | } |
| 3792 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3793 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3794 | return NULL; |
| 3795 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3796 | iter = l->list + index; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3797 | *pos = cgroup_pid_fry(cgrp, *iter); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3798 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3799 | } |
| 3800 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3801 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3802 | { |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3803 | struct cgroup_open_file *of = s->private; |
| 3804 | struct cgroup_pidlist *l = of->priv; |
Tejun Heo | 6223685 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3805 | |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3806 | if (l) |
| 3807 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3808 | CGROUP_PIDLIST_DESTROY_DELAY); |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3809 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3810 | } |
| 3811 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3812 | 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] | 3813 | { |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3814 | struct cgroup_open_file *of = s->private; |
| 3815 | struct cgroup_pidlist *l = of->priv; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3816 | pid_t *p = v; |
| 3817 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3818 | /* |
| 3819 | * Advance to the next pid in the array. If this goes off the |
| 3820 | * end, we're done |
| 3821 | */ |
| 3822 | p++; |
| 3823 | if (p >= end) { |
| 3824 | return NULL; |
| 3825 | } else { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3826 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3827 | return p; |
| 3828 | } |
| 3829 | } |
| 3830 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3831 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3832 | { |
| 3833 | return seq_printf(s, "%d\n", *(int *)v); |
| 3834 | } |
| 3835 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3836 | /* |
| 3837 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3838 | * independent of whether it's tasks or procs |
| 3839 | */ |
| 3840 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3841 | .start = cgroup_pidlist_start, |
| 3842 | .stop = cgroup_pidlist_stop, |
| 3843 | .next = cgroup_pidlist_next, |
| 3844 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3845 | }; |
| 3846 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3847 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3848 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3849 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3850 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3851 | } |
| 3852 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3853 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3854 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3855 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3856 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3857 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3858 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3859 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3860 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3861 | return 0; |
| 3862 | } |
| 3863 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3864 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3865 | * When dput() is called asynchronously, if umount has been done and |
| 3866 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3867 | * there's a small window that vfs will see the root dentry with non-zero |
| 3868 | * refcnt and trigger BUG(). |
| 3869 | * |
| 3870 | * That's why we hold a reference before dput() and drop it right after. |
| 3871 | */ |
| 3872 | static void cgroup_dput(struct cgroup *cgrp) |
| 3873 | { |
| 3874 | struct super_block *sb = cgrp->root->sb; |
| 3875 | |
| 3876 | atomic_inc(&sb->s_active); |
| 3877 | dput(cgrp->dentry); |
| 3878 | deactivate_super(sb); |
| 3879 | } |
| 3880 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3881 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 3882 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3883 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3884 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3885 | } |
| 3886 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3887 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 3888 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3889 | { |
| 3890 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3891 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3892 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3893 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3894 | return 0; |
| 3895 | } |
| 3896 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3897 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3898 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3899 | .name = "cgroup.procs", |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3900 | .seq_start = cgroup_pidlist_start, |
| 3901 | .seq_next = cgroup_pidlist_next, |
| 3902 | .seq_stop = cgroup_pidlist_stop, |
| 3903 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3904 | .private = CGROUP_FILE_PROCS, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3905 | .write_u64 = cgroup_procs_write, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3906 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3907 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3908 | { |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3909 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3910 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3911 | .read_u64 = cgroup_clone_children_read, |
| 3912 | .write_u64 = cgroup_clone_children_write, |
| 3913 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3914 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3915 | .name = "cgroup.sane_behavior", |
| 3916 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3917 | .seq_show = cgroup_sane_behavior_show, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3918 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3919 | |
| 3920 | /* |
| 3921 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 3922 | * don't exist if sane_behavior. If you're depending on these, be |
| 3923 | * prepared to be burned. |
| 3924 | */ |
| 3925 | { |
| 3926 | .name = "tasks", |
| 3927 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3928 | .seq_start = cgroup_pidlist_start, |
| 3929 | .seq_next = cgroup_pidlist_next, |
| 3930 | .seq_stop = cgroup_pidlist_stop, |
| 3931 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3932 | .private = CGROUP_FILE_TASKS, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3933 | .write_u64 = cgroup_tasks_write, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3934 | .mode = S_IRUGO | S_IWUSR, |
| 3935 | }, |
| 3936 | { |
| 3937 | .name = "notify_on_release", |
| 3938 | .flags = CFTYPE_INSANE, |
| 3939 | .read_u64 = cgroup_read_notify_on_release, |
| 3940 | .write_u64 = cgroup_write_notify_on_release, |
| 3941 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3942 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3943 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 3944 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3945 | .seq_show = cgroup_release_agent_show, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3946 | .write_string = cgroup_release_agent_write, |
| 3947 | .max_write_len = PATH_MAX, |
| 3948 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3949 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3950 | }; |
| 3951 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3952 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3953 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3954 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3955 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3956 | * |
| 3957 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3958 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3959 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3960 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3961 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3962 | int i, ret = 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3963 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3964 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3965 | for_each_subsys(ss, i) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3966 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3967 | |
| 3968 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3969 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3970 | |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3971 | list_for_each_entry(set, &ss->cftsets, node) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3972 | ret = cgroup_addrm_files(cgrp, set->cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3973 | if (ret < 0) |
| 3974 | goto err; |
| 3975 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3976 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3977 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3978 | err: |
| 3979 | cgroup_clear_dir(cgrp, subsys_mask); |
| 3980 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3981 | } |
| 3982 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3983 | /* |
| 3984 | * css destruction is four-stage process. |
| 3985 | * |
| 3986 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 3987 | * Implemented in kill_css(). |
| 3988 | * |
| 3989 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
| 3990 | * and thus css_tryget() is guaranteed to fail, the css can be offlined |
| 3991 | * by invoking offline_css(). After offlining, the base ref is put. |
| 3992 | * Implemented in css_killed_work_fn(). |
| 3993 | * |
| 3994 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 3995 | * accessors are inside RCU read sections. css_release() schedules the |
| 3996 | * RCU callback. |
| 3997 | * |
| 3998 | * 4. After the grace period, the css can be freed. Implemented in |
| 3999 | * css_free_work_fn(). |
| 4000 | * |
| 4001 | * It is actually hairier because both step 2 and 4 require process context |
| 4002 | * and thus involve punting to css->destroy_work adding two additional |
| 4003 | * steps to the already complex sequence. |
| 4004 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4005 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4006 | { |
| 4007 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4008 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4009 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4010 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4011 | if (css->parent) |
| 4012 | css_put(css->parent); |
| 4013 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4014 | css->ss->css_free(css); |
| 4015 | cgroup_dput(cgrp); |
| 4016 | } |
| 4017 | |
| 4018 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 4019 | { |
| 4020 | struct cgroup_subsys_state *css = |
| 4021 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 4022 | |
| 4023 | /* |
| 4024 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4025 | * css_put(). dput() requires process context which we don't have. |
| 4026 | */ |
| 4027 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4028 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4029 | } |
| 4030 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4031 | static void css_release(struct percpu_ref *ref) |
| 4032 | { |
| 4033 | struct cgroup_subsys_state *css = |
| 4034 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4035 | |
Li Zefan | c1a7150 | 2013-12-17 11:13:39 +0800 | [diff] [blame] | 4036 | rcu_assign_pointer(css->cgroup->subsys[css->ss->subsys_id], NULL); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4037 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4038 | } |
| 4039 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4040 | static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss, |
| 4041 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4042 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4043 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4044 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4045 | css->flags = 0; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4046 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4047 | if (cgrp->parent) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4048 | css->parent = cgroup_css(cgrp->parent, ss); |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4049 | else |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4050 | css->flags |= CSS_ROOT; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4051 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4052 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4053 | } |
| 4054 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4055 | /* 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] | 4056 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4057 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4058 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4059 | int ret = 0; |
| 4060 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4061 | lockdep_assert_held(&cgroup_mutex); |
| 4062 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4063 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4064 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4065 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4066 | css->flags |= CSS_ONLINE; |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4067 | css->cgroup->nr_css++; |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4068 | rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css); |
| 4069 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4070 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4071 | } |
| 4072 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4073 | /* 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] | 4074 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4075 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4076 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4077 | |
| 4078 | lockdep_assert_held(&cgroup_mutex); |
| 4079 | |
| 4080 | if (!(css->flags & CSS_ONLINE)) |
| 4081 | return; |
| 4082 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4083 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4084 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4085 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4086 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4087 | css->cgroup->nr_css--; |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4088 | RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4089 | } |
| 4090 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4091 | /** |
| 4092 | * create_css - create a cgroup_subsys_state |
| 4093 | * @cgrp: the cgroup new css will be associated with |
| 4094 | * @ss: the subsys of new css |
| 4095 | * |
| 4096 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
| 4097 | * css is online and installed in @cgrp with all interface files created. |
| 4098 | * Returns 0 on success, -errno on failure. |
| 4099 | */ |
| 4100 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss) |
| 4101 | { |
| 4102 | struct cgroup *parent = cgrp->parent; |
| 4103 | struct cgroup_subsys_state *css; |
| 4104 | int err; |
| 4105 | |
| 4106 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 4107 | lockdep_assert_held(&cgroup_mutex); |
| 4108 | |
| 4109 | css = ss->css_alloc(cgroup_css(parent, ss)); |
| 4110 | if (IS_ERR(css)) |
| 4111 | return PTR_ERR(css); |
| 4112 | |
| 4113 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4114 | if (err) |
| 4115 | goto err_free; |
| 4116 | |
| 4117 | init_css(css, ss, cgrp); |
| 4118 | |
| 4119 | err = cgroup_populate_dir(cgrp, 1 << ss->subsys_id); |
| 4120 | if (err) |
| 4121 | goto err_free; |
| 4122 | |
| 4123 | err = online_css(css); |
| 4124 | if (err) |
| 4125 | goto err_free; |
| 4126 | |
| 4127 | dget(cgrp->dentry); |
| 4128 | css_get(css->parent); |
| 4129 | |
| 4130 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4131 | parent->parent) { |
| 4132 | 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", |
| 4133 | current->comm, current->pid, ss->name); |
| 4134 | if (!strcmp(ss->name, "memory")) |
| 4135 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4136 | ss->warned_broken_hierarchy = true; |
| 4137 | } |
| 4138 | |
| 4139 | return 0; |
| 4140 | |
| 4141 | err_free: |
| 4142 | percpu_ref_cancel_init(&css->refcnt); |
| 4143 | ss->css_free(css); |
| 4144 | return err; |
| 4145 | } |
| 4146 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4147 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4148 | * cgroup_create - create a cgroup |
| 4149 | * @parent: cgroup that will be parent of the new cgroup |
| 4150 | * @dentry: dentry of the new cgroup |
| 4151 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4152 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4153 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4154 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4155 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4156 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4157 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4158 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4159 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4160 | struct cgroupfs_root *root = parent->root; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4161 | int ssid, err = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4162 | struct cgroup_subsys *ss; |
| 4163 | struct super_block *sb = root->sb; |
| 4164 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4165 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4166 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4167 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4168 | return -ENOMEM; |
| 4169 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4170 | name = cgroup_alloc_name(dentry); |
| 4171 | if (!name) |
| 4172 | goto err_free_cgrp; |
| 4173 | rcu_assign_pointer(cgrp->name, name); |
| 4174 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4175 | /* |
| 4176 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4177 | * a half-baked cgroup. |
| 4178 | */ |
| 4179 | cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4180 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4181 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4182 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4183 | /* |
| 4184 | * Only live parents can have children. Note that the liveliness |
| 4185 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4186 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4187 | * anyway so that locking is contained inside cgroup proper and we |
| 4188 | * don't get nasty surprises if we ever grow another caller. |
| 4189 | */ |
| 4190 | if (!cgroup_lock_live_group(parent)) { |
| 4191 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4192 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4193 | } |
| 4194 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4195 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4196 | * get deleted on unmount if there are child cgroups. This |
| 4197 | * can be done outside cgroup_mutex, since the sb can't |
| 4198 | * disappear while someone has an open control file on the |
| 4199 | * fs */ |
| 4200 | atomic_inc(&sb->s_active); |
| 4201 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4202 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4203 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4204 | dentry->d_fsdata = cgrp; |
| 4205 | cgrp->dentry = dentry; |
| 4206 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4207 | cgrp->parent = parent; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4208 | cgrp->dummy_css.parent = &parent->dummy_css; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4209 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4210 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4211 | if (notify_on_release(parent)) |
| 4212 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4213 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4214 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4215 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4216 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4217 | /* |
| 4218 | * Create directory. cgroup_create_file() returns with the new |
| 4219 | * directory locked on success so that it can be populated without |
| 4220 | * dropping cgroup_mutex. |
| 4221 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4222 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4223 | if (err < 0) |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4224 | goto err_unlock; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4225 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4226 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4227 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4228 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4229 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4230 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4231 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4232 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4233 | /* hold a ref to the parent's dentry */ |
| 4234 | dget(parent->dentry); |
| 4235 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4236 | /* |
| 4237 | * @cgrp is now fully operational. If something fails after this |
| 4238 | * point, it'll be released via the normal destruction path. |
| 4239 | */ |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4240 | idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 4241 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4242 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4243 | if (err) |
| 4244 | goto err_destroy; |
| 4245 | |
Tejun Heo | 9d403e9 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4246 | /* let's create and online css's */ |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4247 | for_each_subsys(ss, ssid) { |
| 4248 | if (root->subsys_mask & (1 << ssid)) { |
| 4249 | err = create_css(cgrp, ss); |
| 4250 | if (err) |
| 4251 | goto err_destroy; |
| 4252 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4253 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4254 | |
| 4255 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4256 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4257 | |
| 4258 | return 0; |
| 4259 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4260 | err_unlock: |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4261 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4262 | /* Release the reference count that we took on the superblock */ |
| 4263 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4264 | err_free_id: |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4265 | idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4266 | err_free_name: |
| 4267 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4268 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4269 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4270 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4271 | |
| 4272 | err_destroy: |
| 4273 | cgroup_destroy_locked(cgrp); |
| 4274 | mutex_unlock(&cgroup_mutex); |
| 4275 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4276 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4277 | } |
| 4278 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4279 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4280 | { |
| 4281 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4282 | |
| 4283 | /* the vfs holds inode->i_mutex already */ |
| 4284 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4285 | } |
| 4286 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4287 | /* |
| 4288 | * This is called when the refcnt of a css is confirmed to be killed. |
| 4289 | * css_tryget() is now guaranteed to fail. |
| 4290 | */ |
| 4291 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4292 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4293 | struct cgroup_subsys_state *css = |
| 4294 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 4295 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4296 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4297 | mutex_lock(&cgroup_mutex); |
| 4298 | |
| 4299 | /* |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4300 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4301 | * initate destruction. |
| 4302 | */ |
| 4303 | offline_css(css); |
| 4304 | |
| 4305 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4306 | * If @cgrp is marked dead, it's waiting for refs of all css's to |
| 4307 | * be disabled before proceeding to the second phase of cgroup |
| 4308 | * destruction. If we are the last one, kick it off. |
| 4309 | */ |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4310 | if (!cgrp->nr_css && cgroup_is_dead(cgrp)) |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4311 | cgroup_destroy_css_killed(cgrp); |
| 4312 | |
| 4313 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4314 | |
| 4315 | /* |
| 4316 | * Put the css refs from kill_css(). Each css holds an extra |
| 4317 | * reference to the cgroup's dentry and cgroup removal proceeds |
| 4318 | * regardless of css refs. On the last put of each css, whenever |
| 4319 | * that may be, the extra dentry ref is put so that dentry |
| 4320 | * destruction happens only after all css's are released. |
| 4321 | */ |
| 4322 | css_put(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4323 | } |
| 4324 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4325 | /* css kill confirmation processing requires process context, bounce */ |
| 4326 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4327 | { |
| 4328 | struct cgroup_subsys_state *css = |
| 4329 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4330 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4331 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4332 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | /** |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4336 | * kill_css - destroy a css |
| 4337 | * @css: css to destroy |
| 4338 | * |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4339 | * This function initiates destruction of @css by removing cgroup interface |
| 4340 | * files and putting its base reference. ->css_offline() will be invoked |
| 4341 | * asynchronously once css_tryget() is guaranteed to fail and when the |
| 4342 | * reference count reaches zero, @css will be released. |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4343 | */ |
| 4344 | static void kill_css(struct cgroup_subsys_state *css) |
| 4345 | { |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4346 | cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id); |
| 4347 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4348 | /* |
| 4349 | * Killing would put the base ref, but we need to keep it alive |
| 4350 | * until after ->css_offline(). |
| 4351 | */ |
| 4352 | css_get(css); |
| 4353 | |
| 4354 | /* |
| 4355 | * cgroup core guarantees that, by the time ->css_offline() is |
| 4356 | * invoked, no new css reference will be given out via |
| 4357 | * css_tryget(). We can't simply call percpu_ref_kill() and |
| 4358 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 4359 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 4360 | * |
| 4361 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4362 | * css is confirmed to be seen as killed on all CPUs. |
| 4363 | */ |
| 4364 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | /** |
| 4368 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4369 | * @cgrp: cgroup to be destroyed |
| 4370 | * |
| 4371 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4372 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4373 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4374 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4375 | * the following two steps. |
| 4376 | * |
| 4377 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4378 | * userland visible parts and start killing the percpu refcnts of |
| 4379 | * css's. Set up so that the next stage will be kicked off once all |
| 4380 | * the percpu refcnts are confirmed to be killed. |
| 4381 | * |
| 4382 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4383 | * rest of destruction. Once all cgroup references are gone, the |
| 4384 | * cgroup is RCU-freed. |
| 4385 | * |
| 4386 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4387 | * the userland is concerned and a new cgroup with the same name may be |
| 4388 | * created. As cgroup doesn't care about the names internally, this |
| 4389 | * doesn't cause any problem. |
| 4390 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4391 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4392 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4393 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4394 | struct dentry *d = cgrp->dentry; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4395 | struct cgroup_subsys_state *css; |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4396 | struct cgroup *child; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4397 | bool empty; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4398 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4399 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4400 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4401 | lockdep_assert_held(&cgroup_mutex); |
| 4402 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4403 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4404 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4405 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4406 | */ |
| 4407 | read_lock(&css_set_lock); |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4408 | empty = list_empty(&cgrp->cset_links); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4409 | read_unlock(&css_set_lock); |
| 4410 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4411 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4412 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4413 | /* |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4414 | * Make sure there's no live children. We can't test ->children |
| 4415 | * emptiness as dead children linger on it while being destroyed; |
| 4416 | * otherwise, "rmdir parent/child parent" may fail with -EBUSY. |
| 4417 | */ |
| 4418 | empty = true; |
| 4419 | rcu_read_lock(); |
| 4420 | list_for_each_entry_rcu(child, &cgrp->children, sibling) { |
| 4421 | empty = cgroup_is_dead(child); |
| 4422 | if (!empty) |
| 4423 | break; |
| 4424 | } |
| 4425 | rcu_read_unlock(); |
| 4426 | if (!empty) |
| 4427 | return -EBUSY; |
| 4428 | |
| 4429 | /* |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4430 | * Initiate massacre of all css's. cgroup_destroy_css_killed() |
| 4431 | * will be invoked to perform the rest of destruction once the |
| 4432 | * percpu refs of all css's are confirmed to be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4433 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4434 | for_each_css(css, ssid, cgrp) |
| 4435 | kill_css(css); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4436 | |
| 4437 | /* |
| 4438 | * Mark @cgrp dead. This prevents further task migration and child |
| 4439 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4440 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4441 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4442 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4443 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4444 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4445 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4446 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4447 | raw_spin_lock(&release_list_lock); |
| 4448 | if (!list_empty(&cgrp->release_list)) |
| 4449 | list_del_init(&cgrp->release_list); |
| 4450 | raw_spin_unlock(&release_list_lock); |
| 4451 | |
| 4452 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4453 | * If @cgrp has css's attached, the second stage of cgroup |
| 4454 | * destruction is kicked off from css_killed_work_fn() after the |
| 4455 | * refs of all attached css's are killed. If @cgrp doesn't have |
| 4456 | * any css, we kick it off here. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4457 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4458 | if (!cgrp->nr_css) |
| 4459 | cgroup_destroy_css_killed(cgrp); |
| 4460 | |
| 4461 | /* |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4462 | * Clear the base files and remove @cgrp directory. The removal |
| 4463 | * puts the base ref but we aren't quite done with @cgrp yet, so |
| 4464 | * hold onto it. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4465 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4466 | cgroup_addrm_files(cgrp, cgroup_base_files, false); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4467 | dget(d); |
| 4468 | cgroup_d_remove_dir(d); |
| 4469 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4470 | return 0; |
| 4471 | }; |
| 4472 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4473 | /** |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4474 | * cgroup_destroy_css_killed - the second step of cgroup destruction |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4475 | * @work: cgroup->destroy_free_work |
| 4476 | * |
| 4477 | * 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] | 4478 | * destroyed after all css's are offlined and performs the rest of |
| 4479 | * destruction. This is the second step of destruction described in the |
| 4480 | * comment above cgroup_destroy_locked(). |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4481 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4482 | static void cgroup_destroy_css_killed(struct cgroup *cgrp) |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4483 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4484 | struct cgroup *parent = cgrp->parent; |
| 4485 | struct dentry *d = cgrp->dentry; |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4486 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4487 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4488 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4489 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4490 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4491 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4492 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4493 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4494 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4495 | check_for_release(parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4496 | } |
| 4497 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4498 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4499 | { |
| 4500 | int ret; |
| 4501 | |
| 4502 | mutex_lock(&cgroup_mutex); |
| 4503 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4504 | mutex_unlock(&cgroup_mutex); |
| 4505 | |
| 4506 | return ret; |
| 4507 | } |
| 4508 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4509 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4510 | { |
| 4511 | INIT_LIST_HEAD(&ss->cftsets); |
| 4512 | |
| 4513 | /* |
| 4514 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4515 | * deregistration. |
| 4516 | */ |
| 4517 | if (ss->base_cftypes) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4518 | struct cftype *cft; |
| 4519 | |
| 4520 | for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++) |
| 4521 | cft->ss = ss; |
| 4522 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4523 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4524 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4525 | } |
| 4526 | } |
| 4527 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4528 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4529 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4530 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4531 | |
| 4532 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4533 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4534 | mutex_lock(&cgroup_mutex); |
| 4535 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4536 | /* init base cftset */ |
| 4537 | cgroup_init_cftsets(ss); |
| 4538 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4539 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4540 | ss->root = &cgroup_dummy_root; |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4541 | css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4542 | /* We don't handle early failures gracefully */ |
| 4543 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4544 | init_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4545 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4546 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4547 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4548 | * newly registered, all tasks and hence the |
| 4549 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4550 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4551 | |
| 4552 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4553 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4554 | /* At system boot, before all subsystems have been |
| 4555 | * registered, no tasks have been forked, so we don't |
| 4556 | * need to invoke fork callbacks here. */ |
| 4557 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4558 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4559 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4560 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4561 | mutex_unlock(&cgroup_mutex); |
| 4562 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4563 | /* this function shouldn't be used with modular subsystems, since they |
| 4564 | * need to register a subsys_id, among other things */ |
| 4565 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4566 | } |
| 4567 | |
| 4568 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4569 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4570 | * @ss: the subsystem to load |
| 4571 | * |
| 4572 | * This function should be called in a modular subsystem's initcall. If the |
Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 4573 | * subsystem is built as a module, it will be assigned a new subsys_id and set |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4574 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4575 | * simpler cgroup_init_subsys. |
| 4576 | */ |
| 4577 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4578 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4579 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4580 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4581 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4582 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4583 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4584 | |
| 4585 | /* check name and function validity */ |
| 4586 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4587 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4588 | return -EINVAL; |
| 4589 | |
| 4590 | /* |
| 4591 | * we don't support callbacks in modular subsystems. this check is |
| 4592 | * before the ss->module check for consistency; a subsystem that could |
| 4593 | * be a module should still have no callbacks even if the user isn't |
| 4594 | * compiling it as one. |
| 4595 | */ |
| 4596 | if (ss->fork || ss->exit) |
| 4597 | return -EINVAL; |
| 4598 | |
| 4599 | /* |
| 4600 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4601 | * since cgroup_init_subsys will have already taken care of it. |
| 4602 | */ |
| 4603 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4604 | /* a sanity check */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4605 | BUG_ON(cgroup_subsys[ss->subsys_id] != ss); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4606 | return 0; |
| 4607 | } |
| 4608 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4609 | /* init base cftset */ |
| 4610 | cgroup_init_cftsets(ss); |
| 4611 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4612 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4613 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4614 | cgroup_subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4615 | |
| 4616 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4617 | * no ss->css_alloc seems to need anything important in the ss |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4618 | * struct, so this can happen first (i.e. before the dummy root |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4619 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4620 | */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4621 | css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4622 | if (IS_ERR(css)) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4623 | /* failure case - need to deassign the cgroup_subsys[] slot. */ |
| 4624 | cgroup_subsys[ss->subsys_id] = NULL; |
Wei Yongjun | 0be8669 | 2013-12-09 20:38:29 +0800 | [diff] [blame] | 4625 | mutex_unlock(&cgroup_root_mutex); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4626 | mutex_unlock(&cgroup_mutex); |
| 4627 | return PTR_ERR(css); |
| 4628 | } |
| 4629 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4630 | ss->root = &cgroup_dummy_root; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4631 | |
| 4632 | /* our new subsystem will be attached to the dummy hierarchy. */ |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4633 | init_css(css, ss, cgroup_dummy_top); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4634 | |
| 4635 | /* |
| 4636 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4637 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4638 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4639 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4640 | * table state, so each changed css_set will need its hash recomputed. |
| 4641 | * this is all done under the css_set_lock. |
| 4642 | */ |
| 4643 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4644 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4645 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4646 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4647 | continue; |
| 4648 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4649 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4650 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4651 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4652 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4653 | key = css_set_hash(cset->subsys); |
| 4654 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4655 | } |
| 4656 | write_unlock(&css_set_lock); |
| 4657 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4658 | ret = online_css(css); |
Vladimir Davydov | 10bf2f7 | 2013-12-12 23:17:08 +0400 | [diff] [blame] | 4659 | if (ret) { |
| 4660 | ss->css_free(css); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4661 | goto err_unload; |
Vladimir Davydov | 10bf2f7 | 2013-12-12 23:17:08 +0400 | [diff] [blame] | 4662 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4663 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4664 | /* success! */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4665 | mutex_unlock(&cgroup_root_mutex); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4666 | mutex_unlock(&cgroup_mutex); |
| 4667 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4668 | |
| 4669 | err_unload: |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4670 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4671 | mutex_unlock(&cgroup_mutex); |
| 4672 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4673 | cgroup_unload_subsys(ss); |
| 4674 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4675 | } |
| 4676 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4677 | |
| 4678 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4679 | * cgroup_unload_subsys: unload a modular subsystem |
| 4680 | * @ss: the subsystem to unload |
| 4681 | * |
| 4682 | * This function should be called in a modular subsystem's exitcall. When this |
| 4683 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4684 | * the subsystem will not be attached to any hierarchy. |
| 4685 | */ |
| 4686 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4687 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4688 | struct cgrp_cset_link *link; |
Vladimir Davydov | 10bf2f7 | 2013-12-12 23:17:08 +0400 | [diff] [blame] | 4689 | struct cgroup_subsys_state *css; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4690 | |
| 4691 | BUG_ON(ss->module == NULL); |
| 4692 | |
| 4693 | /* |
| 4694 | * we shouldn't be called if the subsystem is in use, and the use of |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 4695 | * try_module_get() in rebind_subsystems() should ensure that it |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4696 | * doesn't start being used while we're killing it off. |
| 4697 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4698 | BUG_ON(ss->root != &cgroup_dummy_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4699 | |
| 4700 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4701 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4702 | |
Vladimir Davydov | 10bf2f7 | 2013-12-12 23:17:08 +0400 | [diff] [blame] | 4703 | css = cgroup_css(cgroup_dummy_top, ss); |
| 4704 | if (css) |
| 4705 | offline_css(css); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4706 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4707 | /* deassign the subsys_id */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4708 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4709 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4710 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4711 | * disentangle the css from all css_sets attached to the dummy |
| 4712 | * top. as in loading, we need to pay our respects to the hashtable |
| 4713 | * gods. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4714 | */ |
| 4715 | write_lock(&css_set_lock); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4716 | list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4717 | struct css_set *cset = link->cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4718 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4719 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4720 | hash_del(&cset->hlist); |
| 4721 | cset->subsys[ss->subsys_id] = NULL; |
| 4722 | key = css_set_hash(cset->subsys); |
| 4723 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4724 | } |
| 4725 | write_unlock(&css_set_lock); |
| 4726 | |
| 4727 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4728 | * remove subsystem's css from the cgroup_dummy_top and free it - |
| 4729 | * need to free before marking as null because ss->css_free needs |
Li Zefan | 2ff2a7d | 2013-09-23 16:57:03 +0800 | [diff] [blame] | 4730 | * the cgrp->subsys pointer to find their state. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4731 | */ |
Vladimir Davydov | 10bf2f7 | 2013-12-12 23:17:08 +0400 | [diff] [blame] | 4732 | if (css) |
| 4733 | ss->css_free(css); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4734 | RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4735 | |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4736 | mutex_unlock(&cgroup_root_mutex); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4737 | mutex_unlock(&cgroup_mutex); |
| 4738 | } |
| 4739 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4740 | |
| 4741 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4742 | * cgroup_init_early - cgroup initialization at system boot |
| 4743 | * |
| 4744 | * Initialize cgroups at system boot, and initialize any |
| 4745 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4746 | */ |
| 4747 | int __init cgroup_init_early(void) |
| 4748 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4749 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4750 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4751 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4752 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4753 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4754 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4755 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4756 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4757 | init_cgroup_root(&cgroup_dummy_root); |
| 4758 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4759 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4760 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4761 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4762 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4763 | 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] | 4764 | 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] | 4765 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4766 | /* at bootup time, we don't worry about modular subsystems */ |
| 4767 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4768 | BUG_ON(!ss->name); |
| 4769 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4770 | BUG_ON(!ss->css_alloc); |
| 4771 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4772 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4773 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4774 | ss->name, ss->subsys_id); |
| 4775 | BUG(); |
| 4776 | } |
| 4777 | |
| 4778 | if (ss->early_init) |
| 4779 | cgroup_init_subsys(ss); |
| 4780 | } |
| 4781 | return 0; |
| 4782 | } |
| 4783 | |
| 4784 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4785 | * cgroup_init - cgroup initialization |
| 4786 | * |
| 4787 | * Register cgroup filesystem and /proc file, and initialize |
| 4788 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4789 | */ |
| 4790 | int __init cgroup_init(void) |
| 4791 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4792 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4793 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4794 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4795 | |
| 4796 | err = bdi_init(&cgroup_backing_dev_info); |
| 4797 | if (err) |
| 4798 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4799 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4800 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4801 | if (!ss->early_init) |
| 4802 | cgroup_init_subsys(ss); |
| 4803 | } |
| 4804 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4805 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4806 | mutex_lock(&cgroup_mutex); |
| 4807 | mutex_lock(&cgroup_root_mutex); |
| 4808 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4809 | /* Add init_css_set to the hash table */ |
| 4810 | key = css_set_hash(init_css_set.subsys); |
| 4811 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4812 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4813 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4814 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4815 | err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top, |
| 4816 | 0, 1, GFP_KERNEL); |
| 4817 | BUG_ON(err < 0); |
| 4818 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4819 | mutex_unlock(&cgroup_root_mutex); |
| 4820 | mutex_unlock(&cgroup_mutex); |
| 4821 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4822 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4823 | if (!cgroup_kobj) { |
| 4824 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4825 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4826 | } |
| 4827 | |
| 4828 | err = register_filesystem(&cgroup_fs_type); |
| 4829 | if (err < 0) { |
| 4830 | kobject_put(cgroup_kobj); |
| 4831 | goto out; |
| 4832 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4833 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4834 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4835 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4836 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4837 | if (err) |
| 4838 | bdi_destroy(&cgroup_backing_dev_info); |
| 4839 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4840 | return err; |
| 4841 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4842 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4843 | static int __init cgroup_wq_init(void) |
| 4844 | { |
| 4845 | /* |
| 4846 | * There isn't much point in executing destruction path in |
| 4847 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
| 4848 | * Use 1 for @max_active. |
| 4849 | * |
| 4850 | * We would prefer to do this in cgroup_init() above, but that |
| 4851 | * is called before init_workqueues(): so leave this until after. |
| 4852 | */ |
| 4853 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
| 4854 | BUG_ON(!cgroup_destroy_wq); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 4855 | |
| 4856 | /* |
| 4857 | * Used to destroy pidlists and separate to serve as flush domain. |
| 4858 | * Cap @max_active to 1 too. |
| 4859 | */ |
| 4860 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 4861 | 0, 1); |
| 4862 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 4863 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4864 | return 0; |
| 4865 | } |
| 4866 | core_initcall(cgroup_wq_init); |
| 4867 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4868 | /* |
| 4869 | * proc_cgroup_show() |
| 4870 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4871 | * - Used for /proc/<pid>/cgroup. |
| 4872 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4873 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4874 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4875 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4876 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4877 | * cgroup to top_cgroup. |
| 4878 | */ |
| 4879 | |
| 4880 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4881 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4882 | { |
| 4883 | struct pid *pid; |
| 4884 | struct task_struct *tsk; |
| 4885 | char *buf; |
| 4886 | int retval; |
| 4887 | struct cgroupfs_root *root; |
| 4888 | |
| 4889 | retval = -ENOMEM; |
| 4890 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4891 | if (!buf) |
| 4892 | goto out; |
| 4893 | |
| 4894 | retval = -ESRCH; |
| 4895 | pid = m->private; |
| 4896 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4897 | if (!tsk) |
| 4898 | goto out_free; |
| 4899 | |
| 4900 | retval = 0; |
| 4901 | |
| 4902 | mutex_lock(&cgroup_mutex); |
| 4903 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4904 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4905 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4906 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4907 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4908 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4909 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4910 | for_each_subsys(ss, ssid) |
| 4911 | if (root->subsys_mask & (1 << ssid)) |
| 4912 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4913 | if (strlen(root->name)) |
| 4914 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4915 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4916 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4917 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4918 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4919 | if (retval < 0) |
| 4920 | goto out_unlock; |
| 4921 | seq_puts(m, buf); |
| 4922 | seq_putc(m, '\n'); |
| 4923 | } |
| 4924 | |
| 4925 | out_unlock: |
| 4926 | mutex_unlock(&cgroup_mutex); |
| 4927 | put_task_struct(tsk); |
| 4928 | out_free: |
| 4929 | kfree(buf); |
| 4930 | out: |
| 4931 | return retval; |
| 4932 | } |
| 4933 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4934 | /* Display information about each subsystem and each hierarchy */ |
| 4935 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4936 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4937 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4938 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4939 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4940 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4941 | /* |
| 4942 | * ideally we don't want subsystems moving around while we do this. |
| 4943 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4944 | * subsys/hierarchy state. |
| 4945 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4946 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4947 | |
| 4948 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4949 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4950 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4951 | ss->root->number_of_cgroups, !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4952 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4953 | mutex_unlock(&cgroup_mutex); |
| 4954 | return 0; |
| 4955 | } |
| 4956 | |
| 4957 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4958 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4959 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4960 | } |
| 4961 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4962 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4963 | .open = cgroupstats_open, |
| 4964 | .read = seq_read, |
| 4965 | .llseek = seq_lseek, |
| 4966 | .release = single_release, |
| 4967 | }; |
| 4968 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4969 | /** |
| 4970 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4971 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4972 | * |
| 4973 | * Description: A task inherits its parent's cgroup at fork(). |
| 4974 | * |
| 4975 | * A pointer to the shared css_set was automatically copied in |
| 4976 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4977 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 4978 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 4979 | * have already changed current->cgroups, allowing the previously |
| 4980 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4981 | * |
| 4982 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 4983 | * task, and the passed argument 'child' points to the child task. |
| 4984 | */ |
| 4985 | void cgroup_fork(struct task_struct *child) |
| 4986 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4987 | task_lock(current); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4988 | get_css_set(task_css_set(current)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4989 | child->cgroups = current->cgroups; |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4990 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4991 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4992 | } |
| 4993 | |
| 4994 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4995 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4996 | * @child: the task in question |
| 4997 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4998 | * Adds the task to the list running through its css_set if necessary and |
| 4999 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5000 | * 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] | 5001 | * 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] | 5002 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5003 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5004 | void cgroup_post_fork(struct task_struct *child) |
| 5005 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5006 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5007 | int i; |
| 5008 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5009 | /* |
| 5010 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 5011 | * under the tasklist_lock and we read it here after we added the child |
| 5012 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 5013 | * yet in the tasklist when we walked through it from |
| 5014 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 5015 | * should be visible now due to the paired locking and barriers implied |
| 5016 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 5017 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 5018 | * lock on fork. |
| 5019 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5020 | if (use_task_css_set_links) { |
| 5021 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5022 | task_lock(child); |
| 5023 | if (list_empty(&child->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5024 | list_add(&child->cg_list, &task_css_set(child)->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5025 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5026 | write_unlock(&css_set_lock); |
| 5027 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5028 | |
| 5029 | /* |
| 5030 | * Call ss->fork(). This must happen after @child is linked on |
| 5031 | * css_set; otherwise, @child might change state between ->fork() |
| 5032 | * and addition to css_set. |
| 5033 | */ |
| 5034 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5035 | /* |
| 5036 | * fork/exit callbacks are supported only for builtin |
| 5037 | * subsystems, and the builtin section of the subsys |
| 5038 | * array is immutable, so we don't need to lock the |
| 5039 | * subsys array here. On the other hand, modular section |
| 5040 | * of the array can be freed at module unload, so we |
| 5041 | * can't touch that. |
| 5042 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5043 | for_each_builtin_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5044 | if (ss->fork) |
| 5045 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5046 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5047 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5048 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5049 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5050 | * cgroup_exit - detach cgroup from exiting task |
| 5051 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5052 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5053 | * |
| 5054 | * Description: Detach cgroup from @tsk and release it. |
| 5055 | * |
| 5056 | * Note that cgroups marked notify_on_release force every task in |
| 5057 | * them to take the global cgroup_mutex mutex when exiting. |
| 5058 | * This could impact scaling on very large systems. Be reluctant to |
| 5059 | * use notify_on_release cgroups where very high task exit scaling |
| 5060 | * is required on large systems. |
| 5061 | * |
| 5062 | * the_top_cgroup_hack: |
| 5063 | * |
| 5064 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 5065 | * |
| 5066 | * We call cgroup_exit() while the task is still competent to |
| 5067 | * handle notify_on_release(), then leave the task attached to the |
| 5068 | * root cgroup in each hierarchy for the remainder of its exit. |
| 5069 | * |
| 5070 | * To do this properly, we would increment the reference count on |
| 5071 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 5072 | * code we would add a second cgroup function call, to drop that |
| 5073 | * reference. This would just create an unnecessary hot spot on |
| 5074 | * the top_cgroup reference count, to no avail. |
| 5075 | * |
| 5076 | * Normally, holding a reference to a cgroup without bumping its |
| 5077 | * count is unsafe. The cgroup could go away, or someone could |
| 5078 | * attach us to a different cgroup, decrementing the count on |
| 5079 | * the first cgroup that we never incremented. But in this case, |
| 5080 | * 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] | 5081 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5082 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5083 | */ |
| 5084 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5085 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5086 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5087 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5088 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5089 | |
| 5090 | /* |
| 5091 | * Unlink from the css_set task list if necessary. |
| 5092 | * Optimistically check cg_list before taking |
| 5093 | * css_set_lock |
| 5094 | */ |
| 5095 | if (!list_empty(&tsk->cg_list)) { |
| 5096 | write_lock(&css_set_lock); |
| 5097 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5098 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5099 | write_unlock(&css_set_lock); |
| 5100 | } |
| 5101 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5102 | /* Reassign the task to the init_css_set. */ |
| 5103 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5104 | cset = task_css_set(tsk); |
| 5105 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5106 | |
| 5107 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5108 | /* |
| 5109 | * fork/exit callbacks are supported only for builtin |
| 5110 | * subsystems, see cgroup_post_fork() for details. |
| 5111 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5112 | for_each_builtin_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5113 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5114 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 5115 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5116 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5117 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5118 | } |
| 5119 | } |
| 5120 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5121 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5122 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5123 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5124 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5125 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5126 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5127 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5128 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5129 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5130 | /* |
| 5131 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5132 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5133 | * it now |
| 5134 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5135 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5136 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5137 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5138 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5139 | list_empty(&cgrp->release_list)) { |
| 5140 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5141 | need_schedule_work = 1; |
| 5142 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5143 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5144 | if (need_schedule_work) |
| 5145 | schedule_work(&release_agent_work); |
| 5146 | } |
| 5147 | } |
| 5148 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5149 | /* |
| 5150 | * Notify userspace when a cgroup is released, by running the |
| 5151 | * configured release agent with the name of the cgroup (path |
| 5152 | * relative to the root of cgroup file system) as the argument. |
| 5153 | * |
| 5154 | * Most likely, this user command will try to rmdir this cgroup. |
| 5155 | * |
| 5156 | * This races with the possibility that some other task will be |
| 5157 | * attached to this cgroup before it is removed, or that some other |
| 5158 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5159 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5160 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5161 | * to continue to serve a useful existence. Next time it's released, |
| 5162 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5163 | * |
| 5164 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5165 | * means only wait until the task is successfully execve()'d. The |
| 5166 | * separate release agent task is forked by call_usermodehelper(), |
| 5167 | * then control in this thread returns here, without waiting for the |
| 5168 | * release agent task. We don't bother to wait because the caller of |
| 5169 | * this routine has no use for the exit status of the release agent |
| 5170 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5171 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5172 | static void cgroup_release_agent(struct work_struct *work) |
| 5173 | { |
| 5174 | BUG_ON(work != &release_agent_work); |
| 5175 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5176 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5177 | while (!list_empty(&release_list)) { |
| 5178 | char *argv[3], *envp[3]; |
| 5179 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5180 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5181 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5182 | struct cgroup, |
| 5183 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5184 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5185 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5186 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5187 | if (!pathbuf) |
| 5188 | goto continue_free; |
| 5189 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5190 | goto continue_free; |
| 5191 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5192 | if (!agentbuf) |
| 5193 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5194 | |
| 5195 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5196 | argv[i++] = agentbuf; |
| 5197 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5198 | argv[i] = NULL; |
| 5199 | |
| 5200 | i = 0; |
| 5201 | /* minimal command environment */ |
| 5202 | envp[i++] = "HOME=/"; |
| 5203 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5204 | envp[i] = NULL; |
| 5205 | |
| 5206 | /* Drop the lock while we invoke the usermode helper, |
| 5207 | * since the exec could involve hitting disk and hence |
| 5208 | * be a slow process */ |
| 5209 | mutex_unlock(&cgroup_mutex); |
| 5210 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5211 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5212 | continue_free: |
| 5213 | kfree(pathbuf); |
| 5214 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5215 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5216 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5217 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5218 | mutex_unlock(&cgroup_mutex); |
| 5219 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5220 | |
| 5221 | static int __init cgroup_disable(char *str) |
| 5222 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5223 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5224 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5225 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5226 | |
| 5227 | while ((token = strsep(&str, ",")) != NULL) { |
| 5228 | if (!*token) |
| 5229 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5230 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5231 | /* |
| 5232 | * cgroup_disable, being at boot time, can't know about |
| 5233 | * module subsystems, so we don't worry about them. |
| 5234 | */ |
| 5235 | for_each_builtin_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5236 | if (!strcmp(token, ss->name)) { |
| 5237 | ss->disabled = 1; |
| 5238 | printk(KERN_INFO "Disabling %s control group" |
| 5239 | " subsystem\n", ss->name); |
| 5240 | break; |
| 5241 | } |
| 5242 | } |
| 5243 | } |
| 5244 | return 1; |
| 5245 | } |
| 5246 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5247 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5248 | /** |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5249 | * css_from_dir - get corresponding css from the dentry of a cgroup dir |
| 5250 | * @dentry: directory dentry of interest |
| 5251 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5252 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 5253 | * Must be called under cgroup_mutex or RCU read lock. The caller is |
| 5254 | * responsible for pinning the returned css if it needs to be accessed |
| 5255 | * outside the critical section. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5256 | */ |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5257 | struct cgroup_subsys_state *css_from_dir(struct dentry *dentry, |
| 5258 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5259 | { |
| 5260 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5261 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 5262 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5263 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5264 | /* is @dentry a cgroup dir? */ |
| 5265 | if (!dentry->d_inode || |
| 5266 | dentry->d_inode->i_op != &cgroup_dir_inode_operations) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5267 | return ERR_PTR(-EBADF); |
| 5268 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5269 | cgrp = __d_cgrp(dentry); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5270 | return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5271 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5272 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5273 | /** |
| 5274 | * css_from_id - lookup css by id |
| 5275 | * @id: the cgroup id |
| 5276 | * @ss: cgroup subsys to be looked into |
| 5277 | * |
| 5278 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5279 | * Should be called under rcu_read_lock(). |
| 5280 | */ |
| 5281 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5282 | { |
| 5283 | struct cgroup *cgrp; |
| 5284 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 5285 | cgroup_assert_mutex_or_rcu_locked(); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5286 | |
| 5287 | cgrp = idr_find(&ss->root->cgroup_idr, id); |
| 5288 | if (cgrp) |
Tejun Heo | d162596 | 2013-08-27 14:27:23 -0400 | [diff] [blame] | 5289 | return cgroup_css(cgrp, ss); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5290 | return NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5291 | } |
| 5292 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5293 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5294 | static struct cgroup_subsys_state * |
| 5295 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5296 | { |
| 5297 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5298 | |
| 5299 | if (!css) |
| 5300 | return ERR_PTR(-ENOMEM); |
| 5301 | |
| 5302 | return css; |
| 5303 | } |
| 5304 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5305 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5306 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5307 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5308 | } |
| 5309 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5310 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 5311 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5312 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5313 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5314 | } |
| 5315 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5316 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 5317 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5318 | { |
| 5319 | return (u64)(unsigned long)current->cgroups; |
| 5320 | } |
| 5321 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5322 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5323 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5324 | { |
| 5325 | u64 count; |
| 5326 | |
| 5327 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5328 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5329 | rcu_read_unlock(); |
| 5330 | return count; |
| 5331 | } |
| 5332 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5333 | 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] | 5334 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5335 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5336 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5337 | |
| 5338 | read_lock(&css_set_lock); |
| 5339 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5340 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5341 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5342 | struct cgroup *c = link->cgrp; |
| 5343 | const char *name; |
| 5344 | |
| 5345 | if (c->dentry) |
| 5346 | name = c->dentry->d_name.name; |
| 5347 | else |
| 5348 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5349 | seq_printf(seq, "Root %d group %s\n", |
| 5350 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5351 | } |
| 5352 | rcu_read_unlock(); |
| 5353 | read_unlock(&css_set_lock); |
| 5354 | return 0; |
| 5355 | } |
| 5356 | |
| 5357 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5358 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5359 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5360 | struct cgroup_subsys_state *css = seq_css(seq); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5361 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5362 | |
| 5363 | read_lock(&css_set_lock); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5364 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5365 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5366 | struct task_struct *task; |
| 5367 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5368 | seq_printf(seq, "css_set %p\n", cset); |
| 5369 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5370 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5371 | seq_puts(seq, " ...\n"); |
| 5372 | break; |
| 5373 | } else { |
| 5374 | seq_printf(seq, " task %d\n", |
| 5375 | task_pid_vnr(task)); |
| 5376 | } |
| 5377 | } |
| 5378 | } |
| 5379 | read_unlock(&css_set_lock); |
| 5380 | return 0; |
| 5381 | } |
| 5382 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5383 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5384 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5385 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | static struct cftype debug_files[] = { |
| 5389 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5390 | .name = "taskcount", |
| 5391 | .read_u64 = debug_taskcount_read, |
| 5392 | }, |
| 5393 | |
| 5394 | { |
| 5395 | .name = "current_css_set", |
| 5396 | .read_u64 = current_css_set_read, |
| 5397 | }, |
| 5398 | |
| 5399 | { |
| 5400 | .name = "current_css_set_refcount", |
| 5401 | .read_u64 = current_css_set_refcount_read, |
| 5402 | }, |
| 5403 | |
| 5404 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5405 | .name = "current_css_set_cg_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5406 | .seq_show = current_css_set_cg_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5407 | }, |
| 5408 | |
| 5409 | { |
| 5410 | .name = "cgroup_css_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5411 | .seq_show = cgroup_css_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5412 | }, |
| 5413 | |
| 5414 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5415 | .name = "releasable", |
| 5416 | .read_u64 = releasable_read, |
| 5417 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5418 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5419 | { } /* terminate */ |
| 5420 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5421 | |
| 5422 | struct cgroup_subsys debug_subsys = { |
| 5423 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5424 | .css_alloc = debug_css_alloc, |
| 5425 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5426 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5427 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5428 | }; |
| 5429 | #endif /* CONFIG_CGROUP_DEBUG */ |