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 | /** |
| 246 | * for_each_subsys - iterate all loaded cgroup subsystems |
| 247 | * @ss: the iteration cursor |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 248 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 249 | * |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 250 | * Iterates through all loaded subsystems. Should be called under |
| 251 | * cgroup_mutex or cgroup_root_mutex. |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 252 | */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 253 | #define for_each_subsys(ss, ssid) \ |
| 254 | for (({ cgroup_assert_mutex_or_root_locked(); (ssid) = 0; }); \ |
| 255 | (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 256 | if (!((ss) = cgroup_subsys[(ssid)])) { } \ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 257 | else |
| 258 | |
| 259 | /** |
| 260 | * for_each_builtin_subsys - iterate all built-in cgroup subsystems |
| 261 | * @ss: the iteration cursor |
| 262 | * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end |
| 263 | * |
| 264 | * Bulit-in subsystems are always present and iteration itself doesn't |
| 265 | * require any synchronization. |
| 266 | */ |
| 267 | #define for_each_builtin_subsys(ss, i) \ |
| 268 | for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \ |
| 269 | (((ss) = cgroup_subsys[i]) || true); (i)++) |
| 270 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 271 | /* iterate each subsystem attached to a hierarchy */ |
| 272 | #define for_each_root_subsys(root, ss) \ |
| 273 | list_for_each_entry((ss), &(root)->subsys_list, sibling) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 274 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 275 | /* iterate across the active hierarchies */ |
| 276 | #define for_each_active_root(root) \ |
| 277 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 278 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 279 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 280 | { |
| 281 | return dentry->d_fsdata; |
| 282 | } |
| 283 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 284 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 285 | { |
| 286 | return dentry->d_fsdata; |
| 287 | } |
| 288 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 289 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 290 | { |
| 291 | return __d_cfe(dentry)->type; |
| 292 | } |
| 293 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 294 | /** |
| 295 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 296 | * @cgrp: the cgroup to be checked for liveness |
| 297 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 298 | * On success, returns true; the mutex should be later unlocked. On |
| 299 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 300 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 301 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 302 | { |
| 303 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 304 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 305 | mutex_unlock(&cgroup_mutex); |
| 306 | return false; |
| 307 | } |
| 308 | return true; |
| 309 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 310 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 311 | /* the list of cgroups eligible for automatic release. Protected by |
| 312 | * release_list_lock */ |
| 313 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 314 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 315 | static void cgroup_release_agent(struct work_struct *work); |
| 316 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 317 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 318 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 319 | /* |
| 320 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 321 | * belong to different cgroups on different hierarchies. In the other |
| 322 | * direction, a css_set is naturally associated with multiple cgroups. |
| 323 | * This M:N relationship is represented by the following link structure |
| 324 | * which exists for each association and allows traversing the associations |
| 325 | * from both sides. |
| 326 | */ |
| 327 | struct cgrp_cset_link { |
| 328 | /* the cgroup and css_set this link associates */ |
| 329 | struct cgroup *cgrp; |
| 330 | struct css_set *cset; |
| 331 | |
| 332 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 333 | struct list_head cset_link; |
| 334 | |
| 335 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 336 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | /* The default css_set - used by init and its children prior to any |
| 340 | * hierarchies being mounted. It contains a pointer to the root state |
| 341 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 342 | * reference-counted, to improve performance when child cgroups |
| 343 | * haven't been created. |
| 344 | */ |
| 345 | |
| 346 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 347 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 348 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 349 | /* |
| 350 | * css_set_lock protects the list of css_set objects, and the chain of |
| 351 | * tasks off each css_set. Nests outside task->alloc_lock due to |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 352 | * css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 353 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 354 | static DEFINE_RWLOCK(css_set_lock); |
| 355 | static int css_set_count; |
| 356 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 357 | /* |
| 358 | * hash table for cgroup groups. This improves the performance to find |
| 359 | * an existing css_set. This hash doesn't (currently) take into |
| 360 | * account cgroups in empty hierarchies. |
| 361 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 362 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 363 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 364 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 365 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 366 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 367 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 368 | struct cgroup_subsys *ss; |
| 369 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 370 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 371 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 372 | key += (unsigned long)css[i]; |
| 373 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 374 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 375 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 378 | /* |
| 379 | * 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] | 380 | * until after the first call to css_task_iter_start(). This reduces the |
| 381 | * fork()/exit() overhead for people who have cgroups compiled into their |
| 382 | * kernel but not actually in use. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 383 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 384 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 385 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 386 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 387 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 388 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 389 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 390 | /* |
| 391 | * Ensure that the refcount doesn't hit zero while any readers |
| 392 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 393 | * rwlock |
| 394 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 395 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 396 | return; |
| 397 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 398 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 399 | write_unlock(&css_set_lock); |
| 400 | return; |
| 401 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 402 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 403 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 404 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 405 | css_set_count--; |
| 406 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 407 | 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] | 408 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 409 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 410 | list_del(&link->cset_link); |
| 411 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 412 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 413 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 414 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 415 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 416 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 417 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 418 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 419 | |
| 420 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 421 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 422 | |
| 423 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 424 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* |
| 428 | * refcounted get/put for css_set objects |
| 429 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 430 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 431 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 432 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 435 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 436 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 437 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 440 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 441 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 442 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 445 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 446 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 447 | * @cset: candidate css_set being tested |
| 448 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 449 | * @new_cgrp: cgroup that's being entered by the task |
| 450 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 451 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 452 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 453 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 454 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 455 | static bool compare_css_sets(struct css_set *cset, |
| 456 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 457 | struct cgroup *new_cgrp, |
| 458 | struct cgroup_subsys_state *template[]) |
| 459 | { |
| 460 | struct list_head *l1, *l2; |
| 461 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 462 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 463 | /* Not all subsystems matched */ |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Compare cgroup pointers in order to distinguish between |
| 469 | * different cgroups in heirarchies with no subsystems. We |
| 470 | * could get by with just this check alone (and skip the |
| 471 | * memcmp above) but on most setups the memcmp check will |
| 472 | * avoid the need for this more expensive check on almost all |
| 473 | * candidates. |
| 474 | */ |
| 475 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 476 | l1 = &cset->cgrp_links; |
| 477 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 478 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 479 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 480 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 481 | |
| 482 | l1 = l1->next; |
| 483 | l2 = l2->next; |
| 484 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 485 | if (l1 == &cset->cgrp_links) { |
| 486 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 487 | break; |
| 488 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 489 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 490 | } |
| 491 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 492 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 493 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 494 | cgrp1 = link1->cgrp; |
| 495 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 496 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 497 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 498 | |
| 499 | /* |
| 500 | * If this hierarchy is the hierarchy of the cgroup |
| 501 | * that's changing, then we need to check that this |
| 502 | * css_set points to the new cgroup; if it's any other |
| 503 | * hierarchy, then this css_set should point to the |
| 504 | * same cgroup as the old css_set. |
| 505 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 506 | if (cgrp1->root == new_cgrp->root) { |
| 507 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 508 | return false; |
| 509 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 510 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | } |
| 514 | return true; |
| 515 | } |
| 516 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 517 | /** |
| 518 | * find_existing_css_set - init css array and find the matching css_set |
| 519 | * @old_cset: the css_set that we're using before the cgroup transition |
| 520 | * @cgrp: the cgroup that we're moving into |
| 521 | * @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] | 522 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 523 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 524 | struct cgroup *cgrp, |
| 525 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 526 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 527 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 528 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 529 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 530 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 531 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 532 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 533 | /* |
| 534 | * Build the set of subsystem state objects that we want to see in the |
| 535 | * new css_set. while subsystems can change globally, the entries here |
| 536 | * won't change, so no need for locking. |
| 537 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 538 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 539 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 540 | /* Subsystem is in this hierarchy. So we want |
| 541 | * the subsystem state from the new |
| 542 | * cgroup */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 543 | template[i] = cgroup_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 544 | } else { |
| 545 | /* Subsystem is not in this hierarchy, so we |
| 546 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 547 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 551 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 552 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 553 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 554 | continue; |
| 555 | |
| 556 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 557 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 558 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 559 | |
| 560 | /* No existing cgroup group matched */ |
| 561 | return NULL; |
| 562 | } |
| 563 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 564 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 565 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 566 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 567 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 568 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 569 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 570 | kfree(link); |
| 571 | } |
| 572 | } |
| 573 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 574 | /** |
| 575 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 576 | * @count: the number of links to allocate |
| 577 | * @tmp_links: list_head the allocated links are put on |
| 578 | * |
| 579 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 580 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 581 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 582 | 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] | 583 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 584 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 585 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 586 | |
| 587 | INIT_LIST_HEAD(tmp_links); |
| 588 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 589 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 590 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 591 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 592 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 593 | return -ENOMEM; |
| 594 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 595 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 596 | } |
| 597 | return 0; |
| 598 | } |
| 599 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 600 | /** |
| 601 | * 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] | 602 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 603 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 604 | * @cgrp: the destination cgroup |
| 605 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 606 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 607 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 608 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 609 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 610 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 611 | BUG_ON(list_empty(tmp_links)); |
| 612 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 613 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 614 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 615 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 616 | /* |
| 617 | * Always add links to the tail of the list so that the list |
| 618 | * is sorted by order of hierarchy creation |
| 619 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 620 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 623 | /** |
| 624 | * find_css_set - return a new css_set with one cgroup updated |
| 625 | * @old_cset: the baseline css_set |
| 626 | * @cgrp: the cgroup to be updated |
| 627 | * |
| 628 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 629 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 630 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 631 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 632 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 633 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 634 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 635 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 636 | struct list_head tmp_links; |
| 637 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 638 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 639 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 640 | lockdep_assert_held(&cgroup_mutex); |
| 641 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 642 | /* First see if we already have a cgroup group that matches |
| 643 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 644 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 645 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 646 | if (cset) |
| 647 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 648 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 649 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 650 | if (cset) |
| 651 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 652 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 653 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 654 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 655 | return NULL; |
| 656 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 657 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 658 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 659 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 660 | return NULL; |
| 661 | } |
| 662 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 663 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 664 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 665 | INIT_LIST_HEAD(&cset->tasks); |
| 666 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 667 | |
| 668 | /* Copy the set of subsystem state objects generated in |
| 669 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 670 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 671 | |
| 672 | write_lock(&css_set_lock); |
| 673 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 674 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 675 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 676 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 677 | if (c->root == cgrp->root) |
| 678 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 679 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 680 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 681 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 682 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 683 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 684 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 685 | |
| 686 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 687 | key = css_set_hash(cset->subsys); |
| 688 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 689 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 690 | write_unlock(&css_set_lock); |
| 691 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 692 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 695 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 696 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 697 | * called with cgroup_mutex held. |
| 698 | */ |
| 699 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 700 | struct cgroupfs_root *root) |
| 701 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 702 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 703 | struct cgroup *res = NULL; |
| 704 | |
| 705 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 706 | read_lock(&css_set_lock); |
| 707 | /* |
| 708 | * No need to lock the task - since we hold cgroup_mutex the |
| 709 | * task can't change groups, so the only thing that can happen |
| 710 | * is that it exits and its css is set back to init_css_set. |
| 711 | */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 712 | cset = task_css_set(task); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 713 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 714 | res = &root->top_cgroup; |
| 715 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 716 | struct cgrp_cset_link *link; |
| 717 | |
| 718 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 719 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 720 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 721 | if (c->root == root) { |
| 722 | res = c; |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | read_unlock(&css_set_lock); |
| 728 | BUG_ON(!res); |
| 729 | return res; |
| 730 | } |
| 731 | |
| 732 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 733 | * There is one global cgroup mutex. We also require taking |
| 734 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 735 | * See "The task_lock() exception", at the end of this comment. |
| 736 | * |
| 737 | * A task must hold cgroup_mutex to modify cgroups. |
| 738 | * |
| 739 | * Any task can increment and decrement the count field without lock. |
| 740 | * So in general, code holding cgroup_mutex can't rely on the count |
| 741 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 742 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 743 | * means that no tasks are currently attached, therefore there is no |
| 744 | * way a task attached to that cgroup can fork (the other way to |
| 745 | * increment the count). So code holding cgroup_mutex can safely |
| 746 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 747 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 748 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 749 | * needs that mutex. |
| 750 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 751 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 752 | * (usually) take cgroup_mutex. These are the two most performance |
| 753 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 754 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 755 | * 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] | 756 | * to the release agent with the name of the cgroup (path relative to |
| 757 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 758 | * |
| 759 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 760 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 761 | * tasks in the system use _some_ cgroup, and since there is always at |
| 762 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 763 | * always has either children cgroups and/or using tasks. So we don't |
| 764 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 765 | * |
| 766 | * The task_lock() exception |
| 767 | * |
| 768 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 769 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 770 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 771 | * several performance critical places that need to reference |
| 772 | * task->cgroup without the expense of grabbing a system global |
| 773 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 774 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 775 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 776 | * the task_struct routinely used for such matters. |
| 777 | * |
| 778 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 779 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 780 | */ |
| 781 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 782 | /* |
| 783 | * A couple of forward declarations required, due to cyclic reference loop: |
| 784 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 785 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 786 | * -> cgroup_mkdir. |
| 787 | */ |
| 788 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 789 | 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] | 790 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 791 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 792 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 793 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 794 | |
| 795 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 796 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 797 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 798 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 799 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 800 | 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] | 801 | { |
| 802 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 803 | |
| 804 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 805 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 806 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 807 | inode->i_uid = current_fsuid(); |
| 808 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 809 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 810 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 811 | } |
| 812 | return inode; |
| 813 | } |
| 814 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 815 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 816 | { |
| 817 | struct cgroup_name *name; |
| 818 | |
| 819 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 820 | if (!name) |
| 821 | return NULL; |
| 822 | strcpy(name->name, dentry->d_name.name); |
| 823 | return name; |
| 824 | } |
| 825 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 826 | static void cgroup_free_fn(struct work_struct *work) |
| 827 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 828 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 829 | |
| 830 | mutex_lock(&cgroup_mutex); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 831 | cgrp->root->number_of_cgroups--; |
| 832 | mutex_unlock(&cgroup_mutex); |
| 833 | |
| 834 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 835 | * We get a ref to the parent's dentry, and put the ref when |
| 836 | * this cgroup is being freed, so it's guaranteed that the |
| 837 | * parent won't be destroyed before its children. |
| 838 | */ |
| 839 | dput(cgrp->parent->dentry); |
| 840 | |
| 841 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 842 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 843 | * created the cgroup. This will free cgrp->root, if we are |
| 844 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 845 | */ |
| 846 | deactivate_super(cgrp->root->sb); |
| 847 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 848 | cgroup_pidlist_destroy_all(cgrp); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 849 | |
| 850 | simple_xattrs_free(&cgrp->xattrs); |
| 851 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 852 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 853 | kfree(cgrp); |
| 854 | } |
| 855 | |
| 856 | static void cgroup_free_rcu(struct rcu_head *head) |
| 857 | { |
| 858 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 859 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 860 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 861 | queue_work(cgroup_destroy_wq, &cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 862 | } |
| 863 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 864 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 865 | { |
| 866 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 867 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 868 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 869 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 870 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 871 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 872 | } else { |
| 873 | struct cfent *cfe = __d_cfe(dentry); |
| 874 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 875 | |
| 876 | WARN_ONCE(!list_empty(&cfe->node) && |
| 877 | cgrp != &cgrp->root->top_cgroup, |
| 878 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 879 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 880 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 881 | } |
| 882 | iput(inode); |
| 883 | } |
| 884 | |
| 885 | static void remove_dir(struct dentry *d) |
| 886 | { |
| 887 | struct dentry *parent = dget(d->d_parent); |
| 888 | |
| 889 | d_delete(d); |
| 890 | simple_rmdir(parent->d_inode, d); |
| 891 | dput(parent); |
| 892 | } |
| 893 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 894 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 895 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 896 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 897 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 898 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 899 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 900 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 901 | /* |
| 902 | * If we're doing cleanup due to failure of cgroup_create(), |
| 903 | * the corresponding @cfe may not exist. |
| 904 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 905 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 906 | struct dentry *d = cfe->dentry; |
| 907 | |
| 908 | if (cft && cfe->type != cft) |
| 909 | continue; |
| 910 | |
| 911 | dget(d); |
| 912 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 913 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 914 | list_del_init(&cfe->node); |
| 915 | dput(d); |
| 916 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 917 | break; |
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 | } |
| 920 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 921 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 922 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 923 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 924 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 925 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 926 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 927 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 928 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 929 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 930 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 931 | for_each_subsys(ss, i) { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 932 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 933 | |
| 934 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 935 | continue; |
| 936 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 937 | cgroup_addrm_files(cgrp, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 938 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* |
| 942 | * NOTE : the dentry must have been dget()'ed |
| 943 | */ |
| 944 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 945 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 946 | struct dentry *parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 947 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 948 | parent = dentry->d_parent; |
| 949 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 950 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 951 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 952 | spin_unlock(&dentry->d_lock); |
| 953 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 954 | remove_dir(dentry); |
| 955 | } |
| 956 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 957 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 958 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 959 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 960 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 961 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 962 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 963 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 964 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 965 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 966 | struct cgroup_subsys *ss; |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 967 | unsigned long pinned = 0; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 968 | int i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 969 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 970 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 971 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 972 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 973 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 974 | for_each_subsys(ss, i) { |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 975 | if (!(added_mask & (1 << i))) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 976 | continue; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 977 | |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 978 | /* is the subsystem mounted elsewhere? */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 979 | if (ss->root != &cgroup_dummy_root) { |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 980 | ret = -EBUSY; |
| 981 | goto out_put; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 982 | } |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 983 | |
| 984 | /* pin the module */ |
| 985 | if (!try_module_get(ss->module)) { |
| 986 | ret = -ENOENT; |
| 987 | goto out_put; |
| 988 | } |
| 989 | pinned |= 1 << i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 992 | /* subsys could be missing if unloaded between parsing and here */ |
| 993 | if (added_mask != pinned) { |
| 994 | ret = -ENOENT; |
| 995 | goto out_put; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 996 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 997 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 998 | ret = cgroup_populate_dir(cgrp, added_mask); |
| 999 | if (ret) |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1000 | goto out_put; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1001 | |
| 1002 | /* |
| 1003 | * Nothing can fail from this point on. Remove files for the |
| 1004 | * removed subsystems and rebind each subsystem. |
| 1005 | */ |
| 1006 | cgroup_clear_dir(cgrp, removed_mask); |
| 1007 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1008 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1009 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1010 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1011 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1012 | /* We're binding this subsystem to this hierarchy */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1013 | BUG_ON(cgroup_css(cgrp, ss)); |
| 1014 | BUG_ON(!cgroup_css(cgroup_dummy_top, ss)); |
| 1015 | BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1016 | |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1017 | rcu_assign_pointer(cgrp->subsys[i], |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1018 | cgroup_css(cgroup_dummy_top, ss)); |
| 1019 | cgroup_css(cgrp, ss)->cgroup = cgrp; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1020 | |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1021 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1022 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1023 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1024 | ss->bind(cgroup_css(cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1025 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1026 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1027 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1028 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1029 | /* We're removing this subsystem */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1030 | BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss)); |
| 1031 | BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1032 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1033 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1034 | ss->bind(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1035 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1036 | cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1037 | RCU_INIT_POINTER(cgrp->subsys[i], NULL); |
| 1038 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1039 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
| 1040 | list_move(&ss->sibling, &cgroup_dummy_root.subsys_list); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1041 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1042 | /* subsystem is now free - drop reference on module */ |
| 1043 | module_put(ss->module); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1044 | root->subsys_mask &= ~bit; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1045 | } |
| 1046 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1047 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1048 | /* |
| 1049 | * Mark @root has finished binding subsystems. @root->subsys_mask |
| 1050 | * now matches the bound subsystems. |
| 1051 | */ |
| 1052 | root->flags |= CGRP_ROOT_SUBSYS_BOUND; |
| 1053 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1054 | return 0; |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1055 | |
| 1056 | out_put: |
| 1057 | for_each_subsys(ss, i) |
| 1058 | if (pinned & (1 << i)) |
| 1059 | module_put(ss->module); |
| 1060 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1063 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1064 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1065 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1066 | struct cgroup_subsys *ss; |
| 1067 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1068 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1069 | for_each_root_subsys(root, ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1070 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1071 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1072 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1073 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1074 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1075 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1076 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1077 | if (strlen(root->release_agent_path)) |
| 1078 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1079 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1080 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1081 | if (strlen(root->name)) |
| 1082 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1083 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1088 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1089 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1090 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1091 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1092 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1093 | /* User explicitly requested empty subsystem */ |
| 1094 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1095 | |
| 1096 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1097 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1098 | }; |
| 1099 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1100 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1101 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1102 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1103 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1104 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1105 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1106 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1107 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1108 | char *token, *o = data; |
| 1109 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1110 | unsigned long mask = (unsigned long)-1; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1111 | struct cgroup_subsys *ss; |
| 1112 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1113 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1114 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1115 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1116 | #ifdef CONFIG_CPUSETS |
| 1117 | mask = ~(1UL << cpuset_subsys_id); |
| 1118 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1119 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1120 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1121 | |
| 1122 | while ((token = strsep(&o, ",")) != NULL) { |
| 1123 | if (!*token) |
| 1124 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1125 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1126 | /* Explicitly have no subsystems */ |
| 1127 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1128 | continue; |
| 1129 | } |
| 1130 | if (!strcmp(token, "all")) { |
| 1131 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1132 | if (one_ss) |
| 1133 | return -EINVAL; |
| 1134 | all_ss = true; |
| 1135 | continue; |
| 1136 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1137 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1138 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1139 | continue; |
| 1140 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1141 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1142 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1143 | continue; |
| 1144 | } |
| 1145 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1146 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1147 | continue; |
| 1148 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1149 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1150 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1151 | continue; |
| 1152 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1153 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1154 | /* Specifying two release agents is forbidden */ |
| 1155 | if (opts->release_agent) |
| 1156 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1157 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1158 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1159 | if (!opts->release_agent) |
| 1160 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1161 | continue; |
| 1162 | } |
| 1163 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1164 | const char *name = token + 5; |
| 1165 | /* Can't specify an empty name */ |
| 1166 | if (!strlen(name)) |
| 1167 | return -EINVAL; |
| 1168 | /* Must match [\w.-]+ */ |
| 1169 | for (i = 0; i < strlen(name); i++) { |
| 1170 | char c = name[i]; |
| 1171 | if (isalnum(c)) |
| 1172 | continue; |
| 1173 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1174 | continue; |
| 1175 | return -EINVAL; |
| 1176 | } |
| 1177 | /* Specifying two names is forbidden */ |
| 1178 | if (opts->name) |
| 1179 | return -EINVAL; |
| 1180 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1181 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1182 | GFP_KERNEL); |
| 1183 | if (!opts->name) |
| 1184 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1185 | |
| 1186 | continue; |
| 1187 | } |
| 1188 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1189 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1190 | if (strcmp(token, ss->name)) |
| 1191 | continue; |
| 1192 | if (ss->disabled) |
| 1193 | continue; |
| 1194 | |
| 1195 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1196 | if (all_ss) |
| 1197 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1198 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1199 | one_ss = true; |
| 1200 | |
| 1201 | break; |
| 1202 | } |
| 1203 | if (i == CGROUP_SUBSYS_COUNT) |
| 1204 | return -ENOENT; |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1209 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1210 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1211 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1212 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1213 | for_each_subsys(ss, i) |
| 1214 | if (!ss->disabled) |
| 1215 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1216 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1217 | /* Consistency checks */ |
| 1218 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1219 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1220 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1221 | |
| 1222 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1223 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1224 | return -EINVAL; |
| 1225 | } |
| 1226 | |
| 1227 | if (opts->cpuset_clone_children) { |
| 1228 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1229 | return -EINVAL; |
| 1230 | } |
| 1231 | } |
| 1232 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1233 | /* |
| 1234 | * Option noprefix was introduced just for backward compatibility |
| 1235 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1236 | * the cpuset subsystem. |
| 1237 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1238 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1239 | return -EINVAL; |
| 1240 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1241 | |
| 1242 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1243 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1244 | return -EINVAL; |
| 1245 | |
| 1246 | /* |
| 1247 | * We either have to specify by name or by subsystems. (So all |
| 1248 | * empty hierarchies must have a name). |
| 1249 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1250 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1251 | return -EINVAL; |
| 1252 | |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1257 | { |
| 1258 | int ret = 0; |
| 1259 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1260 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1261 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1262 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1263 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1264 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1265 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1266 | return -EINVAL; |
| 1267 | } |
| 1268 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1269 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1270 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1271 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1272 | |
| 1273 | /* See what subsystems are wanted */ |
| 1274 | ret = parse_cgroupfs_options(data, &opts); |
| 1275 | if (ret) |
| 1276 | goto out_unlock; |
| 1277 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1278 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1279 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1280 | task_tgid_nr(current), current->comm); |
| 1281 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1282 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1283 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1284 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1285 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1286 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1287 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1288 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1289 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1290 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1291 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1292 | goto out_unlock; |
| 1293 | } |
| 1294 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1295 | /* remounting is not allowed for populated hierarchies */ |
| 1296 | if (root->number_of_cgroups > 1) { |
| 1297 | ret = -EBUSY; |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1298 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1299 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1300 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1301 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1302 | if (ret) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1303 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1304 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1305 | if (opts.release_agent) |
| 1306 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1307 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1308 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1309 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1310 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1311 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1312 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1313 | return ret; |
| 1314 | } |
| 1315 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1316 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1317 | .statfs = simple_statfs, |
| 1318 | .drop_inode = generic_delete_inode, |
| 1319 | .show_options = cgroup_show_options, |
| 1320 | .remount_fs = cgroup_remount, |
| 1321 | }; |
| 1322 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1323 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1324 | { |
| 1325 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1326 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1327 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1328 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1329 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1330 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1331 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1332 | cgrp->dummy_css.cgroup = cgrp; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1333 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1334 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1335 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1336 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1337 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1338 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1339 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1340 | INIT_LIST_HEAD(&root->subsys_list); |
| 1341 | INIT_LIST_HEAD(&root->root_list); |
| 1342 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1343 | cgrp->root = root; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 1344 | RCU_INIT_POINTER(cgrp->name, &root_cgroup_name); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1345 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1346 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1349 | 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] | 1350 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1351 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1352 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1353 | lockdep_assert_held(&cgroup_mutex); |
| 1354 | lockdep_assert_held(&cgroup_root_mutex); |
| 1355 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1356 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 1357 | GFP_KERNEL); |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1358 | if (id < 0) |
| 1359 | return id; |
| 1360 | |
| 1361 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1362 | return 0; |
| 1363 | } |
| 1364 | |
| 1365 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1366 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1367 | lockdep_assert_held(&cgroup_mutex); |
| 1368 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1369 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1370 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1371 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1372 | root->hierarchy_id = 0; |
| 1373 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1376 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1377 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1378 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1379 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1380 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1381 | /* If we asked for a name then it must match */ |
| 1382 | if (opts->name && strcmp(opts->name, root->name)) |
| 1383 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1384 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1385 | /* |
| 1386 | * If we asked for subsystems (or explicitly for no |
| 1387 | * subsystems) then they must match |
| 1388 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1389 | if ((opts->subsys_mask || opts->none) |
| 1390 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1391 | return 0; |
| 1392 | |
| 1393 | return 1; |
| 1394 | } |
| 1395 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1396 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1397 | { |
| 1398 | struct cgroupfs_root *root; |
| 1399 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1400 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1401 | return NULL; |
| 1402 | |
| 1403 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1404 | if (!root) |
| 1405 | return ERR_PTR(-ENOMEM); |
| 1406 | |
| 1407 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1408 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1409 | /* |
| 1410 | * We need to set @root->subsys_mask now so that @root can be |
| 1411 | * matched by cgroup_test_super() before it finishes |
| 1412 | * initialization; otherwise, competing mounts with the same |
| 1413 | * options may try to bind the same subsystems instead of waiting |
| 1414 | * for the first one leading to unexpected mount errors. |
| 1415 | * SUBSYS_BOUND will be set once actual binding is complete. |
| 1416 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1417 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1418 | root->flags = opts->flags; |
| 1419 | if (opts->release_agent) |
| 1420 | strcpy(root->release_agent_path, opts->release_agent); |
| 1421 | if (opts->name) |
| 1422 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1423 | if (opts->cpuset_clone_children) |
| 1424 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1425 | return root; |
| 1426 | } |
| 1427 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1428 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1429 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1430 | if (root) { |
| 1431 | /* hierarhcy ID shoulid already have been released */ |
| 1432 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1433 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1434 | idr_destroy(&root->cgroup_idr); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1435 | kfree(root); |
| 1436 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1439 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1440 | { |
| 1441 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1442 | struct cgroup_sb_opts *opts = data; |
| 1443 | |
| 1444 | /* If we don't have a new root, we can't set up a new sb */ |
| 1445 | if (!opts->new_root) |
| 1446 | return -EINVAL; |
| 1447 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1448 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1449 | |
| 1450 | ret = set_anon_super(sb, NULL); |
| 1451 | if (ret) |
| 1452 | return ret; |
| 1453 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1454 | sb->s_fs_info = opts->new_root; |
| 1455 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1456 | |
| 1457 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1458 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1459 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1460 | sb->s_op = &cgroup_ops; |
| 1461 | |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1466 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1467 | static const struct dentry_operations cgroup_dops = { |
| 1468 | .d_iput = cgroup_diput, |
Al Viro | b26d4cd | 2013-10-25 18:47:37 -0400 | [diff] [blame] | 1469 | .d_delete = always_delete_dentry, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1470 | }; |
| 1471 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1472 | struct inode *inode = |
| 1473 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1474 | |
| 1475 | if (!inode) |
| 1476 | return -ENOMEM; |
| 1477 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1478 | inode->i_fop = &simple_dir_operations; |
| 1479 | inode->i_op = &cgroup_dir_inode_operations; |
| 1480 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1481 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1482 | sb->s_root = d_make_root(inode); |
| 1483 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1484 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1485 | /* for everything else we want ->d_op set */ |
| 1486 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1487 | return 0; |
| 1488 | } |
| 1489 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1490 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1491 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1492 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1493 | { |
| 1494 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1495 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1496 | int ret = 0; |
| 1497 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1498 | struct cgroupfs_root *new_root; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1499 | struct list_head tmp_links; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1500 | struct inode *inode; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1501 | const struct cred *cred; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1502 | |
| 1503 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1504 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1505 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1506 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1507 | if (ret) |
| 1508 | goto out_err; |
| 1509 | |
| 1510 | /* |
| 1511 | * Allocate a new cgroup root. We may not need it if we're |
| 1512 | * reusing an existing hierarchy. |
| 1513 | */ |
| 1514 | new_root = cgroup_root_from_opts(&opts); |
| 1515 | if (IS_ERR(new_root)) { |
| 1516 | ret = PTR_ERR(new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1517 | goto out_err; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1518 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1519 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1520 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1521 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1522 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1523 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1524 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1525 | cgroup_free_root(opts.new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1526 | goto out_err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1529 | root = sb->s_fs_info; |
| 1530 | BUG_ON(!root); |
| 1531 | if (root == opts.new_root) { |
| 1532 | /* We used the new root structure, so this is a new hierarchy */ |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1533 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1534 | struct cgroupfs_root *existing_root; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1535 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1536 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1537 | |
| 1538 | BUG_ON(sb->s_root != NULL); |
| 1539 | |
| 1540 | ret = cgroup_get_rootdir(sb); |
| 1541 | if (ret) |
| 1542 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1543 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1544 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1545 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1546 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1547 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1548 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1549 | root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp, |
| 1550 | 0, 1, GFP_KERNEL); |
| 1551 | if (root_cgrp->id < 0) |
| 1552 | goto unlock_drop; |
| 1553 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1554 | /* Check for name clashes with existing mounts */ |
| 1555 | ret = -EBUSY; |
| 1556 | if (strlen(root->name)) |
| 1557 | for_each_active_root(existing_root) |
| 1558 | if (!strcmp(existing_root->name, root->name)) |
| 1559 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1560 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1561 | /* |
| 1562 | * We're accessing css_set_count without locking |
| 1563 | * css_set_lock here, but that's OK - it can only be |
| 1564 | * increased by someone holding cgroup_lock, and |
| 1565 | * that's us. The worst that can happen is that we |
| 1566 | * have some link structures left over |
| 1567 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1568 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1569 | if (ret) |
| 1570 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1571 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1572 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1573 | ret = cgroup_init_root_id(root, 2, 0); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1574 | if (ret) |
| 1575 | goto unlock_drop; |
| 1576 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1577 | sb->s_root->d_fsdata = root_cgrp; |
| 1578 | root_cgrp->dentry = sb->s_root; |
| 1579 | |
| 1580 | /* |
| 1581 | * We're inside get_sb() and will call lookup_one_len() to |
| 1582 | * create the root files, which doesn't work if SELinux is |
| 1583 | * in use. The following cred dancing somehow works around |
| 1584 | * it. See 2ce9738ba ("cgroupfs: use init_cred when |
| 1585 | * populating new cgroupfs mount") for more details. |
| 1586 | */ |
| 1587 | cred = override_creds(&init_cred); |
| 1588 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1589 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1590 | if (ret) |
| 1591 | goto rm_base_files; |
| 1592 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1593 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1594 | if (ret) |
| 1595 | goto rm_base_files; |
| 1596 | |
| 1597 | revert_creds(cred); |
| 1598 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1599 | /* |
| 1600 | * There must be no failure case after here, since rebinding |
| 1601 | * takes care of subsystems' refcounts, which are explicitly |
| 1602 | * dropped in the failure exit path. |
| 1603 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1604 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1605 | list_add(&root->root_list, &cgroup_roots); |
| 1606 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1607 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1608 | /* Link the top cgroup in this hierarchy into all |
| 1609 | * the css_set objects */ |
| 1610 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1611 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1612 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1613 | write_unlock(&css_set_lock); |
| 1614 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1615 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1616 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1617 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1618 | BUG_ON(root->number_of_cgroups != 1); |
| 1619 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1620 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1621 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1622 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1623 | } else { |
| 1624 | /* |
| 1625 | * We re-used an existing hierarchy - the new root (if |
| 1626 | * any) is not needed |
| 1627 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1628 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1629 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1630 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1631 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1632 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1633 | ret = -EINVAL; |
| 1634 | goto drop_new_super; |
| 1635 | } else { |
| 1636 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1637 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1638 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1641 | kfree(opts.release_agent); |
| 1642 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1643 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1644 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1645 | rm_base_files: |
| 1646 | free_cgrp_cset_links(&tmp_links); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1647 | cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1648 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1649 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1650 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1651 | mutex_unlock(&cgroup_root_mutex); |
| 1652 | mutex_unlock(&cgroup_mutex); |
| 1653 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1654 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1655 | deactivate_locked_super(sb); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1656 | out_err: |
| 1657 | kfree(opts.release_agent); |
| 1658 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1659 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1663 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1664 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1665 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1666 | int ret; |
| 1667 | |
| 1668 | BUG_ON(!root); |
| 1669 | |
| 1670 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1671 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1672 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1673 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1674 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1675 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1676 | |
| 1677 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1678 | if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { |
| 1679 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
| 1680 | /* Shouldn't be able to fail ... */ |
| 1681 | BUG_ON(ret); |
| 1682 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1683 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1684 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1685 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1686 | * root cgroup |
| 1687 | */ |
| 1688 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1689 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1690 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1691 | list_del(&link->cset_link); |
| 1692 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1693 | kfree(link); |
| 1694 | } |
| 1695 | write_unlock(&css_set_lock); |
| 1696 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1697 | if (!list_empty(&root->root_list)) { |
| 1698 | list_del(&root->root_list); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1699 | cgroup_root_count--; |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1700 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1701 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1702 | cgroup_exit_root_id(root); |
| 1703 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1704 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1705 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1706 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1707 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1708 | simple_xattrs_free(&cgrp->xattrs); |
| 1709 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1710 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1711 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | static struct file_system_type cgroup_fs_type = { |
| 1715 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1716 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1717 | .kill_sb = cgroup_kill_sb, |
| 1718 | }; |
| 1719 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1720 | static struct kobject *cgroup_kobj; |
| 1721 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1722 | /** |
| 1723 | * cgroup_path - generate the path of a cgroup |
| 1724 | * @cgrp: the cgroup in question |
| 1725 | * @buf: the buffer to write the path into |
| 1726 | * @buflen: the length of the buffer |
| 1727 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1728 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1729 | * |
| 1730 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1731 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1732 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1733 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1734 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1735 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1736 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1737 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1738 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1739 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1740 | if (!cgrp->parent) { |
| 1741 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1742 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1743 | return 0; |
| 1744 | } |
| 1745 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1746 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1747 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1748 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1749 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1750 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1751 | const char *name = cgroup_name(cgrp); |
| 1752 | int len; |
| 1753 | |
| 1754 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1755 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1756 | goto out; |
| 1757 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1758 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1759 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1760 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1761 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1762 | |
| 1763 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1764 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1765 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1766 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1767 | out: |
| 1768 | rcu_read_unlock(); |
| 1769 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1770 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1771 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1772 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1773 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1774 | * 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] | 1775 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1776 | * @buf: the buffer to write the path into |
| 1777 | * @buflen: the length of the buffer |
| 1778 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1779 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1780 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1781 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1782 | * cgroup controller callbacks. |
| 1783 | * |
| 1784 | * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short. |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1785 | */ |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1786 | 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] | 1787 | { |
| 1788 | struct cgroupfs_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1789 | struct cgroup *cgrp; |
| 1790 | int hierarchy_id = 1, ret = 0; |
| 1791 | |
| 1792 | if (buflen < 2) |
| 1793 | return -ENAMETOOLONG; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1794 | |
| 1795 | mutex_lock(&cgroup_mutex); |
| 1796 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1797 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1798 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1799 | if (root) { |
| 1800 | cgrp = task_cgroup_from_root(task, root); |
| 1801 | ret = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1802 | } else { |
| 1803 | /* if no hierarchy exists, everyone is in "/" */ |
| 1804 | memcpy(buf, "/", 2); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1808 | return ret; |
| 1809 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1810 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1811 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1812 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1813 | * Control Group taskset |
| 1814 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1815 | struct task_and_cgroup { |
| 1816 | struct task_struct *task; |
| 1817 | struct cgroup *cgrp; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1818 | struct css_set *cset; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1819 | }; |
| 1820 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1821 | struct cgroup_taskset { |
| 1822 | struct task_and_cgroup single; |
| 1823 | struct flex_array *tc_array; |
| 1824 | int tc_array_len; |
| 1825 | int idx; |
| 1826 | struct cgroup *cur_cgrp; |
| 1827 | }; |
| 1828 | |
| 1829 | /** |
| 1830 | * cgroup_taskset_first - reset taskset and return the first task |
| 1831 | * @tset: taskset of interest |
| 1832 | * |
| 1833 | * @tset iteration is initialized and the first task is returned. |
| 1834 | */ |
| 1835 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1836 | { |
| 1837 | if (tset->tc_array) { |
| 1838 | tset->idx = 0; |
| 1839 | return cgroup_taskset_next(tset); |
| 1840 | } else { |
| 1841 | tset->cur_cgrp = tset->single.cgrp; |
| 1842 | return tset->single.task; |
| 1843 | } |
| 1844 | } |
| 1845 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1846 | |
| 1847 | /** |
| 1848 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1849 | * @tset: taskset of interest |
| 1850 | * |
| 1851 | * Return the next task in @tset. Iteration must have been initialized |
| 1852 | * with cgroup_taskset_first(). |
| 1853 | */ |
| 1854 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1855 | { |
| 1856 | struct task_and_cgroup *tc; |
| 1857 | |
| 1858 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1859 | return NULL; |
| 1860 | |
| 1861 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1862 | tset->cur_cgrp = tc->cgrp; |
| 1863 | return tc->task; |
| 1864 | } |
| 1865 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1866 | |
| 1867 | /** |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1868 | * cgroup_taskset_cur_css - return the matching css for the current task |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1869 | * @tset: taskset of interest |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1870 | * @subsys_id: the ID of the target subsystem |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1871 | * |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1872 | * Return the css for the current (last returned) task of @tset for |
| 1873 | * subsystem specified by @subsys_id. This function must be preceded by |
| 1874 | * either cgroup_taskset_first() or cgroup_taskset_next(). |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1875 | */ |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1876 | struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset, |
| 1877 | int subsys_id) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1878 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1879 | return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1880 | } |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1881 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1882 | |
| 1883 | /** |
| 1884 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1885 | * @tset: taskset of interest |
| 1886 | */ |
| 1887 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1888 | { |
| 1889 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1890 | } |
| 1891 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1892 | |
| 1893 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1894 | /* |
| 1895 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1896 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1897 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1898 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1899 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1900 | struct task_struct *tsk, |
| 1901 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1902 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1903 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1904 | |
| 1905 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1906 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1907 | * setting such that we can't race against cgroup_exit() changing the |
| 1908 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1909 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1910 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1911 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1912 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1913 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1914 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1915 | task_unlock(tsk); |
| 1916 | |
| 1917 | /* Update the css_set linked lists if we're using them */ |
| 1918 | write_lock(&css_set_lock); |
| 1919 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1920 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1921 | write_unlock(&css_set_lock); |
| 1922 | |
| 1923 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1924 | * We just gained a reference on old_cset by taking it from the |
| 1925 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1926 | * 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] | 1927 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1928 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1929 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1930 | } |
| 1931 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1932 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1933 | * 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] | 1934 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1935 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1936 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1937 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1938 | * 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] | 1939 | * 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] | 1940 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1941 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1942 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1943 | { |
| 1944 | int retval, i, group_size; |
| 1945 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1946 | struct cgroupfs_root *root = cgrp->root; |
| 1947 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1948 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1949 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1950 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1951 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1952 | |
| 1953 | /* |
| 1954 | * step 0: in order to do expensive, possibly blocking operations for |
| 1955 | * every thread, we cannot iterate the thread group list, since it needs |
| 1956 | * 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] | 1957 | * group - group_rwsem prevents new threads from appearing, and if |
| 1958 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1959 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1960 | if (threadgroup) |
| 1961 | group_size = get_nr_threads(tsk); |
| 1962 | else |
| 1963 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1964 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1965 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1966 | if (!group) |
| 1967 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1968 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 1969 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1970 | if (retval) |
| 1971 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1972 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1973 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1974 | /* |
| 1975 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 1976 | * already PF_EXITING could be freed from underneath us unless we |
| 1977 | * take an rcu_read_lock. |
| 1978 | */ |
| 1979 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1980 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1981 | struct task_and_cgroup ent; |
| 1982 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1983 | /* @tsk either already exited or can't exit until the end */ |
| 1984 | if (tsk->flags & PF_EXITING) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1985 | goto next; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1986 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1987 | /* as per above, nr_threads may decrease, but not increase. */ |
| 1988 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1989 | ent.task = tsk; |
| 1990 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 1991 | /* nothing to do if this task is already in the cgroup */ |
| 1992 | if (ent.cgrp == cgrp) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1993 | goto next; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1994 | /* |
| 1995 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 1996 | * earlier, but it's good form to communicate our expectations. |
| 1997 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1998 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1999 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2000 | i++; |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2001 | next: |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2002 | if (!threadgroup) |
| 2003 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2004 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2005 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2006 | /* remember the number of threads in the array for later. */ |
| 2007 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2008 | tset.tc_array = group; |
| 2009 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2010 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2011 | /* methods shouldn't be called if no task is actually migrating */ |
| 2012 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2013 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2014 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2015 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2016 | /* |
| 2017 | * step 1: check that we can legitimately attach to the cgroup. |
| 2018 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2019 | for_each_root_subsys(root, ss) { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2020 | struct cgroup_subsys_state *css = cgroup_css(cgrp, ss); |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2021 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2022 | if (ss->can_attach) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2023 | retval = ss->can_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2024 | if (retval) { |
| 2025 | failed_ss = ss; |
| 2026 | goto out_cancel_attach; |
| 2027 | } |
| 2028 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | /* |
| 2032 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2033 | * we use find_css_set, which allocates a new one if necessary. |
| 2034 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2035 | for (i = 0; i < group_size; i++) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2036 | struct css_set *old_cset; |
| 2037 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2038 | tc = flex_array_get(group, i); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2039 | old_cset = task_css_set(tc->task); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2040 | tc->cset = find_css_set(old_cset, cgrp); |
| 2041 | if (!tc->cset) { |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2042 | retval = -ENOMEM; |
| 2043 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2048 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2049 | * proceed to move all tasks to the new cgroup. There are no |
| 2050 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2051 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2052 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2053 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2054 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2055 | } |
| 2056 | /* nothing is sensitive to fork() after this point. */ |
| 2057 | |
| 2058 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2059 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2060 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2061 | for_each_root_subsys(root, ss) { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2062 | struct cgroup_subsys_state *css = cgroup_css(cgrp, ss); |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2063 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2064 | if (ss->attach) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2065 | ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | /* |
| 2069 | * step 5: success! and cleanup |
| 2070 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2071 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2072 | out_put_css_set_refs: |
| 2073 | if (retval) { |
| 2074 | for (i = 0; i < group_size; i++) { |
| 2075 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2076 | if (!tc->cset) |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2077 | break; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2078 | put_css_set(tc->cset); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2079 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2080 | } |
| 2081 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2082 | if (retval) { |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2083 | for_each_root_subsys(root, ss) { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2084 | struct cgroup_subsys_state *css = cgroup_css(cgrp, ss); |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2085 | |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2086 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2087 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2088 | if (ss->cancel_attach) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2089 | ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2090 | } |
| 2091 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2092 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2093 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2094 | return retval; |
| 2095 | } |
| 2096 | |
| 2097 | /* |
| 2098 | * 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] | 2099 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2100 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2101 | */ |
| 2102 | 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] | 2103 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2104 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2105 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2106 | int ret; |
| 2107 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2108 | if (!cgroup_lock_live_group(cgrp)) |
| 2109 | return -ENODEV; |
| 2110 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2111 | retry_find_task: |
| 2112 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2113 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2114 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2115 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2116 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2117 | ret= -ESRCH; |
| 2118 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2119 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2120 | /* |
| 2121 | * even if we're attaching all tasks in the thread group, we |
| 2122 | * only need to check permissions on one of them. |
| 2123 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2124 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2125 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2126 | !uid_eq(cred->euid, tcred->uid) && |
| 2127 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2128 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2129 | ret = -EACCES; |
| 2130 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2131 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2132 | } else |
| 2133 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2134 | |
| 2135 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2136 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2137 | |
| 2138 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2139 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2140 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2141 | * with no rt_runtime allocated. Just say no. |
| 2142 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2143 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2144 | ret = -EINVAL; |
| 2145 | rcu_read_unlock(); |
| 2146 | goto out_unlock_cgroup; |
| 2147 | } |
| 2148 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2149 | get_task_struct(tsk); |
| 2150 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2151 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2152 | threadgroup_lock(tsk); |
| 2153 | if (threadgroup) { |
| 2154 | if (!thread_group_leader(tsk)) { |
| 2155 | /* |
| 2156 | * a race with de_thread from another thread's exec() |
| 2157 | * may strip us of our leadership, if this happens, |
| 2158 | * there is no choice but to throw this task away and |
| 2159 | * try again; this is |
| 2160 | * "double-double-toil-and-trouble-check locking". |
| 2161 | */ |
| 2162 | threadgroup_unlock(tsk); |
| 2163 | put_task_struct(tsk); |
| 2164 | goto retry_find_task; |
| 2165 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2169 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2170 | threadgroup_unlock(tsk); |
| 2171 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2172 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2173 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2174 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2175 | return ret; |
| 2176 | } |
| 2177 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2178 | /** |
| 2179 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2180 | * @from: attach to all cgroups of a given task |
| 2181 | * @tsk: the task to be attached |
| 2182 | */ |
| 2183 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2184 | { |
| 2185 | struct cgroupfs_root *root; |
| 2186 | int retval = 0; |
| 2187 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2188 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2189 | for_each_active_root(root) { |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2190 | struct cgroup *from_cgrp = task_cgroup_from_root(from, root); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2191 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2192 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2193 | if (retval) |
| 2194 | break; |
| 2195 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2196 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2197 | |
| 2198 | return retval; |
| 2199 | } |
| 2200 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2201 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2202 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2203 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2204 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2205 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2208 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2209 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2210 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2211 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2214 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
| 2215 | struct cftype *cft, const char *buffer) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2216 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2217 | BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2218 | if (strlen(buffer) >= PATH_MAX) |
| 2219 | return -EINVAL; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2220 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2221 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2222 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2223 | strcpy(css->cgroup->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2224 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2225 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2226 | return 0; |
| 2227 | } |
| 2228 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2229 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2230 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2231 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2232 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2233 | if (!cgroup_lock_live_group(cgrp)) |
| 2234 | return -ENODEV; |
| 2235 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2236 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2237 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2238 | return 0; |
| 2239 | } |
| 2240 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2241 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2242 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2243 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2244 | |
| 2245 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2246 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2249 | /* A buffer size big enough for numbers or short strings */ |
| 2250 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2251 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2252 | 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] | 2253 | size_t nbytes, loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2254 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2255 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2256 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2257 | struct cgroup_subsys_state *css = cfe->css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2258 | size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1; |
| 2259 | char *buf; |
| 2260 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2261 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2262 | if (nbytes >= max_bytes) |
| 2263 | return -E2BIG; |
| 2264 | |
| 2265 | buf = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2266 | if (!buf) |
| 2267 | return -ENOMEM; |
| 2268 | |
| 2269 | if (copy_from_user(buf, userbuf, nbytes)) { |
| 2270 | ret = -EFAULT; |
| 2271 | goto out_free; |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2272 | } |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2273 | |
| 2274 | buf[nbytes] = '\0'; |
| 2275 | |
| 2276 | if (cft->write_string) { |
| 2277 | ret = cft->write_string(css, cft, strstrip(buf)); |
| 2278 | } else if (cft->write_u64) { |
| 2279 | unsigned long long v; |
| 2280 | ret = kstrtoull(buf, 0, &v); |
| 2281 | if (!ret) |
| 2282 | ret = cft->write_u64(css, cft, v); |
| 2283 | } else if (cft->write_s64) { |
| 2284 | long long v; |
| 2285 | ret = kstrtoll(buf, 0, &v); |
| 2286 | if (!ret) |
| 2287 | ret = cft->write_s64(css, cft, v); |
| 2288 | } else if (cft->trigger) { |
| 2289 | ret = cft->trigger(css, (unsigned int)cft->private); |
| 2290 | } else { |
| 2291 | ret = -EINVAL; |
| 2292 | } |
| 2293 | out_free: |
| 2294 | kfree(buf); |
| 2295 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2298 | /* |
| 2299 | * seqfile ops/methods for returning structured data. Currently just |
| 2300 | * supports string->u64 maps, but can be extended in future. |
| 2301 | */ |
| 2302 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2303 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
| 2304 | { |
| 2305 | struct cftype *cft = seq_cft(seq); |
| 2306 | |
| 2307 | if (cft->seq_start) { |
| 2308 | return cft->seq_start(seq, ppos); |
| 2309 | } else { |
| 2310 | /* |
| 2311 | * The same behavior and code as single_open(). Returns |
| 2312 | * !NULL if pos is at the beginning; otherwise, NULL. |
| 2313 | */ |
| 2314 | return NULL + !*ppos; |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 2319 | { |
| 2320 | struct cftype *cft = seq_cft(seq); |
| 2321 | |
| 2322 | if (cft->seq_next) { |
| 2323 | return cft->seq_next(seq, v, ppos); |
| 2324 | } else { |
| 2325 | /* |
| 2326 | * The same behavior and code as single_open(), always |
| 2327 | * terminate after the initial read. |
| 2328 | */ |
| 2329 | ++*ppos; |
| 2330 | return NULL; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 2335 | { |
| 2336 | struct cftype *cft = seq_cft(seq); |
| 2337 | |
| 2338 | if (cft->seq_stop) |
| 2339 | cft->seq_stop(seq, v); |
| 2340 | } |
| 2341 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2342 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2343 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2344 | struct cftype *cft = seq_cft(m); |
| 2345 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2346 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2347 | if (cft->seq_show) |
| 2348 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2349 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2350 | if (cft->read_u64) |
| 2351 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 2352 | else if (cft->read_s64) |
| 2353 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 2354 | else |
| 2355 | return -EINVAL; |
| 2356 | return 0; |
| 2357 | } |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2358 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2359 | static struct seq_operations cgroup_seq_operations = { |
| 2360 | .start = cgroup_seqfile_start, |
| 2361 | .next = cgroup_seqfile_next, |
| 2362 | .stop = cgroup_seqfile_stop, |
| 2363 | .show = cgroup_seqfile_show, |
| 2364 | }; |
| 2365 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2366 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2367 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2368 | struct cfent *cfe = __d_cfe(file->f_dentry); |
| 2369 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2370 | struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent); |
| 2371 | struct cgroup_subsys_state *css; |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2372 | struct cgroup_open_file *of; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2373 | int err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2374 | |
| 2375 | err = generic_file_open(inode, file); |
| 2376 | if (err) |
| 2377 | return err; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2378 | |
| 2379 | /* |
| 2380 | * If the file belongs to a subsystem, pin the css. Will be |
| 2381 | * unpinned either on open failure or release. This ensures that |
| 2382 | * @css stays alive for all file operations. |
| 2383 | */ |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2384 | rcu_read_lock(); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2385 | css = cgroup_css(cgrp, cft->ss); |
| 2386 | if (cft->ss && !css_tryget(css)) |
| 2387 | css = NULL; |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2388 | rcu_read_unlock(); |
| 2389 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2390 | if (!css) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2391 | return -ENODEV; |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2392 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2393 | /* |
| 2394 | * @cfe->css is used by read/write/close to determine the |
| 2395 | * associated css. @file->private_data would be a better place but |
| 2396 | * that's already used by seqfile. Multiple accessors may use it |
| 2397 | * simultaneously which is okay as the association never changes. |
| 2398 | */ |
| 2399 | WARN_ON_ONCE(cfe->css && cfe->css != css); |
| 2400 | cfe->css = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2401 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2402 | of = __seq_open_private(file, &cgroup_seq_operations, |
| 2403 | sizeof(struct cgroup_open_file)); |
| 2404 | if (of) { |
| 2405 | of->cfe = cfe; |
| 2406 | return 0; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2407 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2408 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2409 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2410 | css_put(css); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2411 | return -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2415 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2416 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2417 | struct cgroup_subsys_state *css = cfe->css; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2418 | |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2419 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2420 | css_put(css); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2421 | return seq_release_private(inode, file); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | /* |
| 2425 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2426 | */ |
| 2427 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2428 | struct inode *new_dir, struct dentry *new_dentry) |
| 2429 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2430 | int ret; |
| 2431 | struct cgroup_name *name, *old_name; |
| 2432 | struct cgroup *cgrp; |
| 2433 | |
| 2434 | /* |
| 2435 | * It's convinient to use parent dir's i_mutex to protected |
| 2436 | * cgrp->name. |
| 2437 | */ |
| 2438 | lockdep_assert_held(&old_dir->i_mutex); |
| 2439 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2440 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2441 | return -ENOTDIR; |
| 2442 | if (new_dentry->d_inode) |
| 2443 | return -EEXIST; |
| 2444 | if (old_dir != new_dir) |
| 2445 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2446 | |
| 2447 | cgrp = __d_cgrp(old_dentry); |
| 2448 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2449 | /* |
| 2450 | * This isn't a proper migration and its usefulness is very |
| 2451 | * limited. Disallow if sane_behavior. |
| 2452 | */ |
| 2453 | if (cgroup_sane_behavior(cgrp)) |
| 2454 | return -EPERM; |
| 2455 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2456 | name = cgroup_alloc_name(new_dentry); |
| 2457 | if (!name) |
| 2458 | return -ENOMEM; |
| 2459 | |
| 2460 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2461 | if (ret) { |
| 2462 | kfree(name); |
| 2463 | return ret; |
| 2464 | } |
| 2465 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 2466 | old_name = rcu_dereference_protected(cgrp->name, true); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2467 | rcu_assign_pointer(cgrp->name, name); |
| 2468 | |
| 2469 | kfree_rcu(old_name, rcu_head); |
| 2470 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2473 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2474 | { |
| 2475 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2476 | return &__d_cgrp(dentry)->xattrs; |
| 2477 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2478 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | static inline int xattr_enabled(struct dentry *dentry) |
| 2482 | { |
| 2483 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2484 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | static bool is_valid_xattr(const char *name) |
| 2488 | { |
| 2489 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2490 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2491 | return true; |
| 2492 | return false; |
| 2493 | } |
| 2494 | |
| 2495 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2496 | const void *val, size_t size, int flags) |
| 2497 | { |
| 2498 | if (!xattr_enabled(dentry)) |
| 2499 | return -EOPNOTSUPP; |
| 2500 | if (!is_valid_xattr(name)) |
| 2501 | return -EINVAL; |
| 2502 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2503 | } |
| 2504 | |
| 2505 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2506 | { |
| 2507 | if (!xattr_enabled(dentry)) |
| 2508 | return -EOPNOTSUPP; |
| 2509 | if (!is_valid_xattr(name)) |
| 2510 | return -EINVAL; |
| 2511 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2512 | } |
| 2513 | |
| 2514 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2515 | void *buf, size_t size) |
| 2516 | { |
| 2517 | if (!xattr_enabled(dentry)) |
| 2518 | return -EOPNOTSUPP; |
| 2519 | if (!is_valid_xattr(name)) |
| 2520 | return -EINVAL; |
| 2521 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2522 | } |
| 2523 | |
| 2524 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2525 | { |
| 2526 | if (!xattr_enabled(dentry)) |
| 2527 | return -EOPNOTSUPP; |
| 2528 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2529 | } |
| 2530 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2531 | static const struct file_operations cgroup_file_operations = { |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2532 | .read = seq_read, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2533 | .write = cgroup_file_write, |
| 2534 | .llseek = generic_file_llseek, |
| 2535 | .open = cgroup_file_open, |
| 2536 | .release = cgroup_file_release, |
| 2537 | }; |
| 2538 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2539 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2540 | .setxattr = cgroup_setxattr, |
| 2541 | .getxattr = cgroup_getxattr, |
| 2542 | .listxattr = cgroup_listxattr, |
| 2543 | .removexattr = cgroup_removexattr, |
| 2544 | }; |
| 2545 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2546 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | 786e144 | 2013-07-14 17:50:23 +0400 | [diff] [blame] | 2547 | .lookup = simple_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2548 | .mkdir = cgroup_mkdir, |
| 2549 | .rmdir = cgroup_rmdir, |
| 2550 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2551 | .setxattr = cgroup_setxattr, |
| 2552 | .getxattr = cgroup_getxattr, |
| 2553 | .listxattr = cgroup_listxattr, |
| 2554 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2555 | }; |
| 2556 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2557 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2558 | struct super_block *sb) |
| 2559 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2560 | struct inode *inode; |
| 2561 | |
| 2562 | if (!dentry) |
| 2563 | return -ENOENT; |
| 2564 | if (dentry->d_inode) |
| 2565 | return -EEXIST; |
| 2566 | |
| 2567 | inode = cgroup_new_inode(mode, sb); |
| 2568 | if (!inode) |
| 2569 | return -ENOMEM; |
| 2570 | |
| 2571 | if (S_ISDIR(mode)) { |
| 2572 | inode->i_op = &cgroup_dir_inode_operations; |
| 2573 | inode->i_fop = &simple_dir_operations; |
| 2574 | |
| 2575 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2576 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2577 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2578 | |
Tejun Heo | b8a2df6a | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2579 | /* |
| 2580 | * Control reaches here with cgroup_mutex held. |
| 2581 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2582 | * want to populate it immediately without releasing |
| 2583 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2584 | * yet, trylock will always succeed without affecting |
| 2585 | * lockdep checks. |
| 2586 | */ |
| 2587 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2588 | } else if (S_ISREG(mode)) { |
| 2589 | inode->i_size = 0; |
| 2590 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2591 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2592 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2593 | d_instantiate(dentry, inode); |
| 2594 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2595 | return 0; |
| 2596 | } |
| 2597 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2598 | /** |
| 2599 | * cgroup_file_mode - deduce file mode of a control file |
| 2600 | * @cft: the control file in question |
| 2601 | * |
| 2602 | * returns cft->mode if ->mode is not 0 |
| 2603 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2604 | * returns S_IRUGO if it has only a read handler |
| 2605 | * returns S_IWUSR if it has only a write hander |
| 2606 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2607 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2608 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2609 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2610 | |
| 2611 | if (cft->mode) |
| 2612 | return cft->mode; |
| 2613 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2614 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2615 | mode |= S_IRUGO; |
| 2616 | |
Tejun Heo | 6e0755b | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2617 | if (cft->write_u64 || cft->write_s64 || cft->write_string || |
| 2618 | cft->trigger) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2619 | mode |= S_IWUSR; |
| 2620 | |
| 2621 | return mode; |
| 2622 | } |
| 2623 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2624 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2625 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2626 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2627 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2628 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2629 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2630 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2631 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2632 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2633 | |
Tejun Heo | 9fa4db3 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2634 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 2635 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2636 | strcpy(name, cft->ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2637 | strcat(name, "."); |
| 2638 | } |
| 2639 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2640 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2641 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2642 | |
| 2643 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2644 | if (!cfe) |
| 2645 | return -ENOMEM; |
| 2646 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2647 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2648 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2649 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2650 | goto out; |
| 2651 | } |
| 2652 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2653 | cfe->type = (void *)cft; |
| 2654 | cfe->dentry = dentry; |
| 2655 | dentry->d_fsdata = cfe; |
| 2656 | simple_xattrs_init(&cfe->xattrs); |
| 2657 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2658 | mode = cgroup_file_mode(cft); |
| 2659 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2660 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2661 | list_add_tail(&cfe->node, &parent->files); |
| 2662 | cfe = NULL; |
| 2663 | } |
| 2664 | dput(dentry); |
| 2665 | out: |
| 2666 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2667 | return error; |
| 2668 | } |
| 2669 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2670 | /** |
| 2671 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2672 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2673 | * @cfts: array of cftypes to be added |
| 2674 | * @is_add: whether to add or remove |
| 2675 | * |
| 2676 | * 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] | 2677 | * For removals, this function never fails. If addition fails, this |
| 2678 | * function doesn't remove files already added. The caller is responsible |
| 2679 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2680 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2681 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2682 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2683 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2684 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2685 | int ret; |
| 2686 | |
| 2687 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 2688 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2689 | |
| 2690 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2691 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2692 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2693 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2694 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2695 | continue; |
| 2696 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2697 | continue; |
| 2698 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2699 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2700 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2701 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2702 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2703 | cft->name, ret); |
| 2704 | return ret; |
| 2705 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2706 | } else { |
| 2707 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2708 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2709 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2710 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2711 | } |
| 2712 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2713 | static void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2714 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2715 | { |
| 2716 | /* |
| 2717 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2718 | * the existing cgroups under cgroup_mutex and create files. |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2719 | * Instead, we use css_for_each_descendant_pre() and drop RCU read |
| 2720 | * lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2721 | */ |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2722 | mutex_lock(&cgroup_mutex); |
| 2723 | } |
| 2724 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2725 | static int cgroup_cfts_commit(struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2726 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2727 | { |
| 2728 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2729 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2730 | struct cgroup *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2731 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2732 | struct dentry *prev = NULL; |
| 2733 | struct inode *inode; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2734 | struct cgroup_subsys_state *css; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2735 | u64 update_before; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2736 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2737 | |
| 2738 | /* %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] | 2739 | if (!cfts || ss->root == &cgroup_dummy_root || |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2740 | !atomic_inc_not_zero(&sb->s_active)) { |
| 2741 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2742 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2743 | } |
| 2744 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2745 | /* |
| 2746 | * All cgroups which are created after we drop cgroup_mutex will |
| 2747 | * 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] | 2748 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2749 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2750 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2751 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2752 | mutex_unlock(&cgroup_mutex); |
| 2753 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2754 | /* add/rm files for all cgroups created before */ |
| 2755 | rcu_read_lock(); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2756 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2757 | struct cgroup *cgrp = css->cgroup; |
| 2758 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2759 | if (cgroup_is_dead(cgrp)) |
| 2760 | continue; |
| 2761 | |
| 2762 | inode = cgrp->dentry->d_inode; |
| 2763 | dget(cgrp->dentry); |
| 2764 | rcu_read_unlock(); |
| 2765 | |
| 2766 | dput(prev); |
| 2767 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2768 | |
| 2769 | mutex_lock(&inode->i_mutex); |
| 2770 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2771 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2772 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2773 | mutex_unlock(&cgroup_mutex); |
| 2774 | mutex_unlock(&inode->i_mutex); |
| 2775 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2776 | rcu_read_lock(); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2777 | if (ret) |
| 2778 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2779 | } |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2780 | rcu_read_unlock(); |
| 2781 | dput(prev); |
| 2782 | deactivate_super(sb); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2783 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | /** |
| 2787 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2788 | * @ss: target cgroup subsystem |
| 2789 | * @cfts: zero-length name terminated array of cftypes |
| 2790 | * |
| 2791 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2792 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2793 | * have them too. This function can be called anytime whether @ss is |
| 2794 | * attached or not. |
| 2795 | * |
| 2796 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2797 | * function currently returns 0 as long as @cfts registration is successful |
| 2798 | * even if some file creation attempts on existing cgroups fail. |
| 2799 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2800 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2801 | { |
| 2802 | struct cftype_set *set; |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2803 | struct cftype *cft; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2804 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2805 | |
| 2806 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2807 | if (!set) |
| 2808 | return -ENOMEM; |
| 2809 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2810 | for (cft = cfts; cft->name[0] != '\0'; cft++) |
| 2811 | cft->ss = ss; |
| 2812 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2813 | cgroup_cfts_prepare(); |
| 2814 | set->cfts = cfts; |
| 2815 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2816 | ret = cgroup_cfts_commit(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2817 | if (ret) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2818 | cgroup_rm_cftypes(cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2819 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2820 | } |
| 2821 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2822 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2823 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2824 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2825 | * @cfts: zero-length name terminated array of cftypes |
| 2826 | * |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2827 | * Unregister @cfts. Files described by @cfts are removed from all |
| 2828 | * existing cgroups and all future cgroups won't have them either. This |
| 2829 | * function can be called anytime whether @cfts' subsys is attached or not. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2830 | * |
| 2831 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2832 | * registered. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2833 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2834 | int cgroup_rm_cftypes(struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2835 | { |
| 2836 | struct cftype_set *set; |
| 2837 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2838 | if (!cfts || !cfts[0].ss) |
| 2839 | return -ENOENT; |
| 2840 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2841 | cgroup_cfts_prepare(); |
| 2842 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2843 | list_for_each_entry(set, &cfts[0].ss->cftsets, node) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2844 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2845 | list_del(&set->node); |
| 2846 | kfree(set); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2847 | cgroup_cfts_commit(cfts, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2848 | return 0; |
| 2849 | } |
| 2850 | } |
| 2851 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2852 | cgroup_cfts_commit(NULL, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2853 | return -ENOENT; |
| 2854 | } |
| 2855 | |
| 2856 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2857 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2858 | * @cgrp: the cgroup in question |
| 2859 | * |
| 2860 | * Return the number of tasks in the cgroup. |
| 2861 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2862 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2863 | { |
| 2864 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2865 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2866 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2867 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2868 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2869 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2870 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2871 | return count; |
| 2872 | } |
| 2873 | |
| 2874 | /* |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2875 | * To reduce the fork() overhead for systems that are not actually using |
| 2876 | * their cgroups capability, we don't maintain the lists running through |
| 2877 | * 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] | 2878 | * words after the first call to css_task_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2879 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2880 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2881 | { |
| 2882 | struct task_struct *p, *g; |
| 2883 | write_lock(&css_set_lock); |
| 2884 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2885 | /* |
| 2886 | * We need tasklist_lock because RCU is not safe against |
| 2887 | * while_each_thread(). Besides, a forking task that has passed |
| 2888 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2889 | * is not guaranteed to have its child immediately visible in the |
| 2890 | * tasklist if we walk through it with RCU. |
| 2891 | */ |
| 2892 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2893 | do_each_thread(g, p) { |
| 2894 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2895 | /* |
| 2896 | * We should check if the process is exiting, otherwise |
| 2897 | * it will race with cgroup_exit() in that the list |
| 2898 | * entry won't be deleted though the process has exited. |
| 2899 | */ |
| 2900 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2901 | list_add(&p->cg_list, &task_css_set(p)->tasks); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2902 | task_unlock(p); |
| 2903 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2904 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2905 | write_unlock(&css_set_lock); |
| 2906 | } |
| 2907 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2908 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2909 | * css_next_child - find the next child of a given css |
| 2910 | * @pos_css: the current position (%NULL to initiate traversal) |
| 2911 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2912 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2913 | * 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] | 2914 | * under either cgroup_mutex or RCU read lock. The only requirement is |
| 2915 | * that @parent_css and @pos_css are accessible. The next sibling is |
| 2916 | * guaranteed to be returned regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2917 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2918 | struct cgroup_subsys_state * |
| 2919 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 2920 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2921 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2922 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 2923 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2924 | struct cgroup *next; |
| 2925 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2926 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2927 | |
| 2928 | /* |
| 2929 | * @pos could already have been removed. Once a cgroup is removed, |
| 2930 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 2931 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 2932 | * before the cgroup is taken off the ->sibling list, if we see it |
| 2933 | * unasserted, it's guaranteed that the next sibling hasn't |
| 2934 | * finished its grace period even if it's already removed, and thus |
| 2935 | * safe to dereference from this RCU critical section. If |
| 2936 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 2937 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2938 | * |
| 2939 | * If @pos is dead, its next pointer can't be dereferenced; |
| 2940 | * however, as each cgroup is given a monotonically increasing |
| 2941 | * unique serial number and always appended to the sibling list, |
| 2942 | * the next one can be found by walking the parent's children until |
| 2943 | * we see a cgroup with higher serial number than @pos's. While |
| 2944 | * this path can be slower, it's taken only when either the current |
| 2945 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2946 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2947 | if (!pos) { |
| 2948 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 2949 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2950 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2951 | } else { |
| 2952 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 2953 | if (next->serial_nr > pos->serial_nr) |
| 2954 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2955 | } |
| 2956 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2957 | if (&next->sibling == &cgrp->children) |
| 2958 | return NULL; |
| 2959 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2960 | return cgroup_css(next, parent_css->ss); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2961 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2962 | EXPORT_SYMBOL_GPL(css_next_child); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2963 | |
| 2964 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2965 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2966 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2967 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2968 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2969 | * 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] | 2970 | * to visit for pre-order traversal of @root's descendants. @root is |
| 2971 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2972 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2973 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2974 | * doesn't require the whole traversal to be contained in a single critical |
| 2975 | * section. This function will return the correct next descendant as long |
| 2976 | * 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] | 2977 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2978 | struct cgroup_subsys_state * |
| 2979 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 2980 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2981 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2982 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2983 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2984 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2985 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2986 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2987 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2988 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2989 | |
| 2990 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2991 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2992 | if (next) |
| 2993 | return next; |
| 2994 | |
| 2995 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2996 | while (pos != root) { |
| 2997 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2998 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2999 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3000 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3001 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3002 | |
| 3003 | return NULL; |
| 3004 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3005 | EXPORT_SYMBOL_GPL(css_next_descendant_pre); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3006 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3007 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3008 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 3009 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3010 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3011 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 3012 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3013 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3014 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3015 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3016 | * doesn't require the whole traversal to be contained in a single critical |
| 3017 | * section. This function will return the correct rightmost descendant as |
| 3018 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3019 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3020 | struct cgroup_subsys_state * |
| 3021 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3022 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3023 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3024 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3025 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3026 | |
| 3027 | do { |
| 3028 | last = pos; |
| 3029 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3030 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3031 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3032 | pos = tmp; |
| 3033 | } while (pos); |
| 3034 | |
| 3035 | return last; |
| 3036 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3037 | EXPORT_SYMBOL_GPL(css_rightmost_descendant); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3038 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3039 | static struct cgroup_subsys_state * |
| 3040 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3041 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3042 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3043 | |
| 3044 | do { |
| 3045 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3046 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3047 | } while (pos); |
| 3048 | |
| 3049 | return last; |
| 3050 | } |
| 3051 | |
| 3052 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3053 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3054 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3055 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3056 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3057 | * 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] | 3058 | * to visit for post-order traversal of @root's descendants. @root is |
| 3059 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3060 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3061 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3062 | * doesn't require the whole traversal to be contained in a single critical |
| 3063 | * section. This function will return the correct next descendant as long |
| 3064 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 3065 | * @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3066 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3067 | struct cgroup_subsys_state * |
| 3068 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 3069 | struct cgroup_subsys_state *root) |
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 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3072 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3073 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3074 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 3075 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 3076 | if (!pos) |
| 3077 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3078 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3079 | /* if we visited @root, we're done */ |
| 3080 | if (pos == root) |
| 3081 | return NULL; |
| 3082 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3083 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3084 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3085 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3086 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3087 | |
| 3088 | /* no sibling left, visit parent */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3089 | return css_parent(pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3090 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3091 | EXPORT_SYMBOL_GPL(css_next_descendant_post); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3092 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3093 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3094 | * 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] | 3095 | * @it: the iterator to advance |
| 3096 | * |
| 3097 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3098 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3099 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3100 | { |
| 3101 | struct list_head *l = it->cset_link; |
| 3102 | struct cgrp_cset_link *link; |
| 3103 | struct css_set *cset; |
| 3104 | |
| 3105 | /* Advance to the next non-empty css_set */ |
| 3106 | do { |
| 3107 | l = l->next; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3108 | if (l == &it->origin_css->cgroup->cset_links) { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3109 | it->cset_link = NULL; |
| 3110 | return; |
| 3111 | } |
| 3112 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3113 | cset = link->cset; |
| 3114 | } while (list_empty(&cset->tasks)); |
| 3115 | it->cset_link = l; |
| 3116 | it->task = cset->tasks.next; |
| 3117 | } |
| 3118 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3119 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3120 | * css_task_iter_start - initiate task iteration |
| 3121 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3122 | * @it: the task iterator to use |
| 3123 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3124 | * Initiate iteration through the tasks of @css. The caller can call |
| 3125 | * css_task_iter_next() to walk through the tasks until the function |
| 3126 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 3127 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3128 | * |
| 3129 | * Note that this function acquires a lock which is released when the |
| 3130 | * iteration finishes. The caller can't sleep while iteration is in |
| 3131 | * progress. |
| 3132 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3133 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 3134 | struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3135 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3136 | { |
| 3137 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3138 | * The first time anyone tries to iterate across a css, we need to |
| 3139 | * enable the list linking each css_set to its tasks, and fix up |
| 3140 | * all existing tasks. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3141 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3142 | if (!use_task_css_set_links) |
| 3143 | cgroup_enable_task_cg_lists(); |
| 3144 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3145 | read_lock(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3146 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3147 | it->origin_css = css; |
| 3148 | it->cset_link = &css->cgroup->cset_links; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3149 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3150 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3151 | } |
| 3152 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3153 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3154 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3155 | * @it: the task iterator being iterated |
| 3156 | * |
| 3157 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3158 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 3159 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3160 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3161 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3162 | { |
| 3163 | struct task_struct *res; |
| 3164 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3165 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3166 | |
| 3167 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3168 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3169 | return NULL; |
| 3170 | res = list_entry(l, struct task_struct, cg_list); |
| 3171 | /* Advance iterator to find next entry */ |
| 3172 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3173 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3174 | if (l == &link->cset->tasks) { |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3175 | /* |
| 3176 | * We reached the end of this task list - move on to the |
| 3177 | * next cgrp_cset_link. |
| 3178 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3179 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3180 | } else { |
| 3181 | it->task = l; |
| 3182 | } |
| 3183 | return res; |
| 3184 | } |
| 3185 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3186 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3187 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3188 | * @it: the task iterator to finish |
| 3189 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3190 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3191 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3192 | void css_task_iter_end(struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3193 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3194 | { |
| 3195 | read_unlock(&css_set_lock); |
| 3196 | } |
| 3197 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3198 | static inline int started_after_time(struct task_struct *t1, |
| 3199 | struct timespec *time, |
| 3200 | struct task_struct *t2) |
| 3201 | { |
| 3202 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3203 | if (start_diff > 0) { |
| 3204 | return 1; |
| 3205 | } else if (start_diff < 0) { |
| 3206 | return 0; |
| 3207 | } else { |
| 3208 | /* |
| 3209 | * Arbitrarily, if two processes started at the same |
| 3210 | * time, we'll say that the lower pointer value |
| 3211 | * started first. Note that t2 may have exited by now |
| 3212 | * so this may not be a valid pointer any longer, but |
| 3213 | * that's fine - it still serves to distinguish |
| 3214 | * between two tasks started (effectively) simultaneously. |
| 3215 | */ |
| 3216 | return t1 > t2; |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | /* |
| 3221 | * This function is a callback from heap_insert() and is used to order |
| 3222 | * the heap. |
| 3223 | * In this case we order the heap in descending task start time. |
| 3224 | */ |
| 3225 | static inline int started_after(void *p1, void *p2) |
| 3226 | { |
| 3227 | struct task_struct *t1 = p1; |
| 3228 | struct task_struct *t2 = p2; |
| 3229 | return started_after_time(t1, &t2->start_time, t2); |
| 3230 | } |
| 3231 | |
| 3232 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3233 | * css_scan_tasks - iterate though all the tasks in a css |
| 3234 | * @css: the css to iterate tasks of |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3235 | * @test: optional test callback |
| 3236 | * @process: process callback |
| 3237 | * @data: data passed to @test and @process |
| 3238 | * @heap: optional pre-allocated heap used for task iteration |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3239 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3240 | * Iterate through all the tasks in @css, calling @test for each, and if it |
| 3241 | * returns %true, call @process for it also. |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3242 | * |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3243 | * @test may be NULL, meaning always true (select all tasks), which |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3244 | * effectively duplicates css_task_iter_{start,next,end}() but does not |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3245 | * lock css_set_lock for the call to @process. |
| 3246 | * |
| 3247 | * 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] | 3248 | * of @css for the duration of this call. This function may or may not |
| 3249 | * call @process for tasks that exit or move to a different css during the |
| 3250 | * call, or are forked or move into the css during the call. |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3251 | * |
| 3252 | * Note that @test may be called with locks held, and may in some |
| 3253 | * situations be called multiple times for the same task, so it should be |
| 3254 | * cheap. |
| 3255 | * |
| 3256 | * If @heap is non-NULL, a heap has been pre-allocated and will be used for |
| 3257 | * heap operations (and its "gt" member will be overwritten), else a |
| 3258 | * temporary heap will be used (allocation of which may cause this function |
| 3259 | * to fail). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3260 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3261 | int css_scan_tasks(struct cgroup_subsys_state *css, |
| 3262 | bool (*test)(struct task_struct *, void *), |
| 3263 | void (*process)(struct task_struct *, void *), |
| 3264 | void *data, struct ptr_heap *heap) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3265 | { |
| 3266 | int retval, i; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3267 | struct css_task_iter it; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3268 | struct task_struct *p, *dropped; |
| 3269 | /* Never dereference latest_task, since it's not refcounted */ |
| 3270 | struct task_struct *latest_task = NULL; |
| 3271 | struct ptr_heap tmp_heap; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3272 | struct timespec latest_time = { 0, 0 }; |
| 3273 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3274 | if (heap) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3275 | /* The caller supplied our heap and pre-allocated its memory */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3276 | heap->gt = &started_after; |
| 3277 | } else { |
| 3278 | /* We need to allocate our own heap memory */ |
| 3279 | heap = &tmp_heap; |
| 3280 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3281 | if (retval) |
| 3282 | /* cannot allocate the heap */ |
| 3283 | return retval; |
| 3284 | } |
| 3285 | |
| 3286 | again: |
| 3287 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3288 | * Scan tasks in the css, using the @test callback to determine |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3289 | * which are of interest, and invoking @process callback on the |
| 3290 | * ones which need an update. Since we don't want to hold any |
| 3291 | * locks during the task updates, gather tasks to be processed in a |
| 3292 | * heap structure. The heap is sorted by descending task start |
| 3293 | * time. If the statically-sized heap fills up, we overflow tasks |
| 3294 | * that started later, and in future iterations only consider tasks |
| 3295 | * that started after the latest task in the previous pass. This |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3296 | * guarantees forward progress and that we don't miss any tasks. |
| 3297 | */ |
| 3298 | heap->size = 0; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3299 | css_task_iter_start(css, &it); |
| 3300 | while ((p = css_task_iter_next(&it))) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3301 | /* |
| 3302 | * Only affect tasks that qualify per the caller's callback, |
| 3303 | * if he provided one |
| 3304 | */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3305 | if (test && !test(p, data)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3306 | continue; |
| 3307 | /* |
| 3308 | * Only process tasks that started after the last task |
| 3309 | * we processed |
| 3310 | */ |
| 3311 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3312 | continue; |
| 3313 | dropped = heap_insert(heap, p); |
| 3314 | if (dropped == NULL) { |
| 3315 | /* |
| 3316 | * The new task was inserted; the heap wasn't |
| 3317 | * previously full |
| 3318 | */ |
| 3319 | get_task_struct(p); |
| 3320 | } else if (dropped != p) { |
| 3321 | /* |
| 3322 | * The new task was inserted, and pushed out a |
| 3323 | * different task |
| 3324 | */ |
| 3325 | get_task_struct(p); |
| 3326 | put_task_struct(dropped); |
| 3327 | } |
| 3328 | /* |
| 3329 | * Else the new task was newer than anything already in |
| 3330 | * the heap and wasn't inserted |
| 3331 | */ |
| 3332 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3333 | css_task_iter_end(&it); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3334 | |
| 3335 | if (heap->size) { |
| 3336 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3337 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3338 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3339 | latest_time = q->start_time; |
| 3340 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3341 | } |
| 3342 | /* Process the task per the caller's callback */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3343 | process(q, data); |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3344 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3345 | } |
| 3346 | /* |
| 3347 | * If we had to process any tasks at all, scan again |
| 3348 | * in case some of them were in the middle of forking |
| 3349 | * children that didn't get processed. |
| 3350 | * Not the most efficient way to do it, but it avoids |
| 3351 | * having to take callback_mutex in the fork path |
| 3352 | */ |
| 3353 | goto again; |
| 3354 | } |
| 3355 | if (heap == &tmp_heap) |
| 3356 | heap_free(&tmp_heap); |
| 3357 | return 0; |
| 3358 | } |
| 3359 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3360 | static void cgroup_transfer_one_task(struct task_struct *task, void *data) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3361 | { |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3362 | struct cgroup *new_cgroup = data; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3363 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3364 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3365 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3366 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | /** |
| 3370 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3371 | * @to: cgroup to which the tasks will be moved |
| 3372 | * @from: cgroup in which the tasks currently reside |
| 3373 | */ |
| 3374 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3375 | { |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3376 | return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task, |
| 3377 | to, NULL); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3378 | } |
| 3379 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3380 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3381 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3382 | * |
| 3383 | * Reading this file can return large amounts of data if a cgroup has |
| 3384 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3385 | * but we cannot guarantee that the information we produce is correct |
| 3386 | * unless we produce it entirely atomically. |
| 3387 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3388 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3389 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3390 | /* which pidlist file are we talking about? */ |
| 3391 | enum cgroup_filetype { |
| 3392 | CGROUP_FILE_PROCS, |
| 3393 | CGROUP_FILE_TASKS, |
| 3394 | }; |
| 3395 | |
| 3396 | /* |
| 3397 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3398 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3399 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3400 | * to the cgroup. |
| 3401 | */ |
| 3402 | struct cgroup_pidlist { |
| 3403 | /* |
| 3404 | * used to find which pidlist is wanted. doesn't change as long as |
| 3405 | * this particular list stays in the list. |
| 3406 | */ |
| 3407 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3408 | /* array of xids */ |
| 3409 | pid_t *list; |
| 3410 | /* how many elements the above list has */ |
| 3411 | int length; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3412 | /* each of these stored in a list by its cgroup */ |
| 3413 | struct list_head links; |
| 3414 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3415 | struct cgroup *owner; |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3416 | /* for delayed destruction */ |
| 3417 | struct delayed_work destroy_dwork; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3418 | }; |
| 3419 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3420 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3421 | * The following two functions "fix" the issue where there are more pids |
| 3422 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3423 | * TODO: replace with a kernel-wide solution to this problem |
| 3424 | */ |
| 3425 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3426 | static void *pidlist_allocate(int count) |
| 3427 | { |
| 3428 | if (PIDLIST_TOO_LARGE(count)) |
| 3429 | return vmalloc(count * sizeof(pid_t)); |
| 3430 | else |
| 3431 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3432 | } |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3433 | |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3434 | static void pidlist_free(void *p) |
| 3435 | { |
| 3436 | if (is_vmalloc_addr(p)) |
| 3437 | vfree(p); |
| 3438 | else |
| 3439 | kfree(p); |
| 3440 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3441 | |
| 3442 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3443 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 3444 | * should be left afterwards. |
| 3445 | */ |
| 3446 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 3447 | { |
| 3448 | struct cgroup_pidlist *l, *tmp_l; |
| 3449 | |
| 3450 | mutex_lock(&cgrp->pidlist_mutex); |
| 3451 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 3452 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 3453 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3454 | |
| 3455 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 3456 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 3457 | } |
| 3458 | |
| 3459 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 3460 | { |
| 3461 | struct delayed_work *dwork = to_delayed_work(work); |
| 3462 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 3463 | destroy_dwork); |
| 3464 | struct cgroup_pidlist *tofree = NULL; |
| 3465 | |
| 3466 | mutex_lock(&l->owner->pidlist_mutex); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3467 | |
| 3468 | /* |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3469 | * Destroy iff we didn't get queued again. The state won't change |
| 3470 | * as destroy_dwork can only be queued while locked. |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3471 | */ |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3472 | if (!delayed_work_pending(dwork)) { |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3473 | list_del(&l->links); |
| 3474 | pidlist_free(l->list); |
| 3475 | put_pid_ns(l->key.ns); |
| 3476 | tofree = l; |
| 3477 | } |
| 3478 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3479 | mutex_unlock(&l->owner->pidlist_mutex); |
| 3480 | kfree(tofree); |
| 3481 | } |
| 3482 | |
| 3483 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3484 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3485 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3486 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3487 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3488 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3489 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3490 | |
| 3491 | /* |
| 3492 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3493 | * edge cases first; no work needs to be done for either |
| 3494 | */ |
| 3495 | if (length == 0 || length == 1) |
| 3496 | return length; |
| 3497 | /* src and dest walk down the list; dest counts unique elements */ |
| 3498 | for (src = 1; src < length; src++) { |
| 3499 | /* find next unique element */ |
| 3500 | while (list[src] == list[src-1]) { |
| 3501 | src++; |
| 3502 | if (src == length) |
| 3503 | goto after; |
| 3504 | } |
| 3505 | /* dest always points to where the next unique element goes */ |
| 3506 | list[dest] = list[src]; |
| 3507 | dest++; |
| 3508 | } |
| 3509 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3510 | return dest; |
| 3511 | } |
| 3512 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3513 | /* |
| 3514 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 3515 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 3516 | * different per namespace, each namespace needs differently sorted list, |
| 3517 | * making it impossible to use, for example, single rbtree of member tasks |
| 3518 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 3519 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 3520 | * pidlists keyed by cgroup and namespace. |
| 3521 | * |
| 3522 | * All this extra complexity was caused by the original implementation |
| 3523 | * committing to an entirely unnecessary property. In the long term, we |
| 3524 | * want to do away with it. Explicitly scramble sort order if |
| 3525 | * sane_behavior so that no such expectation exists in the new interface. |
| 3526 | * |
| 3527 | * Scrambling is done by swapping every two consecutive bits, which is |
| 3528 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 3529 | */ |
| 3530 | static pid_t pid_fry(pid_t pid) |
| 3531 | { |
| 3532 | unsigned a = pid & 0x55555555; |
| 3533 | unsigned b = pid & 0xAAAAAAAA; |
| 3534 | |
| 3535 | return (a << 1) | (b >> 1); |
| 3536 | } |
| 3537 | |
| 3538 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 3539 | { |
| 3540 | if (cgroup_sane_behavior(cgrp)) |
| 3541 | return pid_fry(pid); |
| 3542 | else |
| 3543 | return pid; |
| 3544 | } |
| 3545 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3546 | static int cmppid(const void *a, const void *b) |
| 3547 | { |
| 3548 | return *(pid_t *)a - *(pid_t *)b; |
| 3549 | } |
| 3550 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3551 | static int fried_cmppid(const void *a, const void *b) |
| 3552 | { |
| 3553 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 3554 | } |
| 3555 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3556 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3557 | enum cgroup_filetype type) |
| 3558 | { |
| 3559 | struct cgroup_pidlist *l; |
| 3560 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3561 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3562 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3563 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3564 | |
| 3565 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 3566 | if (l->key.type == type && l->key.ns == ns) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3567 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3568 | return NULL; |
| 3569 | } |
| 3570 | |
| 3571 | /* |
| 3572 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3573 | * returns with the lock on that pidlist already held, and takes care |
| 3574 | * of the use count, or returns NULL with no locks held if we're out of |
| 3575 | * memory. |
| 3576 | */ |
| 3577 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 3578 | enum cgroup_filetype type) |
| 3579 | { |
| 3580 | struct cgroup_pidlist *l; |
| 3581 | |
| 3582 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3583 | |
| 3584 | l = cgroup_pidlist_find(cgrp, type); |
| 3585 | if (l) |
| 3586 | return l; |
| 3587 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3588 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3589 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3590 | if (!l) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3591 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3592 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3593 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3594 | l->key.type = type; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3595 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 3596 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3597 | l->owner = cgrp; |
| 3598 | list_add(&l->links, &cgrp->pidlists); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3599 | return l; |
| 3600 | } |
| 3601 | |
| 3602 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3603 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3604 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3605 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3606 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3607 | { |
| 3608 | pid_t *array; |
| 3609 | int length; |
| 3610 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3611 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3612 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3613 | struct cgroup_pidlist *l; |
| 3614 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3615 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3616 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3617 | /* |
| 3618 | * If cgroup gets more users after we read count, we won't have |
| 3619 | * enough space - tough. This race is indistinguishable to the |
| 3620 | * caller from the case that the additional cgroup users didn't |
| 3621 | * show up until sometime later on. |
| 3622 | */ |
| 3623 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3624 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3625 | if (!array) |
| 3626 | return -ENOMEM; |
| 3627 | /* now, populate the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3628 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3629 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3630 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3631 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3632 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3633 | if (type == CGROUP_FILE_PROCS) |
| 3634 | pid = task_tgid_vnr(tsk); |
| 3635 | else |
| 3636 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3637 | if (pid > 0) /* make sure to only use valid results */ |
| 3638 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3639 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3640 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3641 | length = n; |
| 3642 | /* now sort & (if procs) strip out duplicates */ |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3643 | if (cgroup_sane_behavior(cgrp)) |
| 3644 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 3645 | else |
| 3646 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3647 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3648 | length = pidlist_uniq(array, length); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3649 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3650 | l = cgroup_pidlist_find_create(cgrp, type); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3651 | if (!l) { |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3652 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3653 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3654 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3655 | } |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3656 | |
| 3657 | /* store array, freeing old if necessary */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3658 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3659 | l->list = array; |
| 3660 | l->length = length; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3661 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3662 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3663 | } |
| 3664 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3665 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3666 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3667 | * @stats: cgroupstats to fill information into |
| 3668 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3669 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3670 | * |
| 3671 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3672 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3673 | */ |
| 3674 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3675 | { |
| 3676 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3677 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3678 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3679 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3680 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3681 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3682 | * Validate dentry by checking the superblock operations, |
| 3683 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3684 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3685 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3686 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3687 | goto err; |
| 3688 | |
| 3689 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3690 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3691 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3692 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3693 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3694 | switch (tsk->state) { |
| 3695 | case TASK_RUNNING: |
| 3696 | stats->nr_running++; |
| 3697 | break; |
| 3698 | case TASK_INTERRUPTIBLE: |
| 3699 | stats->nr_sleeping++; |
| 3700 | break; |
| 3701 | case TASK_UNINTERRUPTIBLE: |
| 3702 | stats->nr_uninterruptible++; |
| 3703 | break; |
| 3704 | case TASK_STOPPED: |
| 3705 | stats->nr_stopped++; |
| 3706 | break; |
| 3707 | default: |
| 3708 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3709 | stats->nr_io_wait++; |
| 3710 | break; |
| 3711 | } |
| 3712 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3713 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3714 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3715 | err: |
| 3716 | return ret; |
| 3717 | } |
| 3718 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3719 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3720 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3721 | * 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] | 3722 | * 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] | 3723 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3724 | */ |
| 3725 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3726 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3727 | { |
| 3728 | /* |
| 3729 | * Initially we receive a position value that corresponds to |
| 3730 | * one more than the last pid shown (or 0 on the first call or |
| 3731 | * after a seek to the start). Use a binary-search to find the |
| 3732 | * next pid to display, if any |
| 3733 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3734 | struct cgroup_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3735 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3736 | struct cgroup_pidlist *l; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3737 | enum cgroup_filetype type = seq_cft(s)->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3738 | int index = 0, pid = *pos; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3739 | int *iter, ret; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3740 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3741 | mutex_lock(&cgrp->pidlist_mutex); |
| 3742 | |
| 3743 | /* |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3744 | * !NULL @of->priv indicates that this isn't the first start() |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3745 | * after open. If the matching pidlist is around, we can use that. |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3746 | * 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] | 3747 | * could already have been destroyed. |
| 3748 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3749 | if (of->priv) |
| 3750 | of->priv = cgroup_pidlist_find(cgrp, type); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3751 | |
| 3752 | /* |
| 3753 | * Either this is the first start() after open or the matching |
| 3754 | * pidlist has been destroyed inbetween. Create a new one. |
| 3755 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3756 | if (!of->priv) { |
| 3757 | ret = pidlist_array_load(cgrp, type, |
| 3758 | (struct cgroup_pidlist **)&of->priv); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3759 | if (ret) |
| 3760 | return ERR_PTR(ret); |
| 3761 | } |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3762 | l = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3763 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3764 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3765 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3766 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3767 | while (index < end) { |
| 3768 | int mid = (index + end) / 2; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3769 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3770 | index = mid; |
| 3771 | break; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3772 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3773 | index = mid + 1; |
| 3774 | else |
| 3775 | end = mid; |
| 3776 | } |
| 3777 | } |
| 3778 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3779 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3780 | return NULL; |
| 3781 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3782 | iter = l->list + index; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3783 | *pos = cgroup_pid_fry(cgrp, *iter); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3784 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3785 | } |
| 3786 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3787 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3788 | { |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3789 | struct cgroup_open_file *of = s->private; |
| 3790 | struct cgroup_pidlist *l = of->priv; |
Tejun Heo | 6223685 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3791 | |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3792 | if (l) |
| 3793 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3794 | CGROUP_PIDLIST_DESTROY_DELAY); |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3795 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3796 | } |
| 3797 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3798 | 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] | 3799 | { |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3800 | struct cgroup_open_file *of = s->private; |
| 3801 | struct cgroup_pidlist *l = of->priv; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3802 | pid_t *p = v; |
| 3803 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3804 | /* |
| 3805 | * Advance to the next pid in the array. If this goes off the |
| 3806 | * end, we're done |
| 3807 | */ |
| 3808 | p++; |
| 3809 | if (p >= end) { |
| 3810 | return NULL; |
| 3811 | } else { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3812 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3813 | return p; |
| 3814 | } |
| 3815 | } |
| 3816 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3817 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3818 | { |
| 3819 | return seq_printf(s, "%d\n", *(int *)v); |
| 3820 | } |
| 3821 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3822 | /* |
| 3823 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3824 | * independent of whether it's tasks or procs |
| 3825 | */ |
| 3826 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3827 | .start = cgroup_pidlist_start, |
| 3828 | .stop = cgroup_pidlist_stop, |
| 3829 | .next = cgroup_pidlist_next, |
| 3830 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3831 | }; |
| 3832 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3833 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3834 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3835 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3836 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3837 | } |
| 3838 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3839 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3840 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3841 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3842 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3843 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3844 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3845 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3846 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3847 | return 0; |
| 3848 | } |
| 3849 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3850 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3851 | * When dput() is called asynchronously, if umount has been done and |
| 3852 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3853 | * there's a small window that vfs will see the root dentry with non-zero |
| 3854 | * refcnt and trigger BUG(). |
| 3855 | * |
| 3856 | * That's why we hold a reference before dput() and drop it right after. |
| 3857 | */ |
| 3858 | static void cgroup_dput(struct cgroup *cgrp) |
| 3859 | { |
| 3860 | struct super_block *sb = cgrp->root->sb; |
| 3861 | |
| 3862 | atomic_inc(&sb->s_active); |
| 3863 | dput(cgrp->dentry); |
| 3864 | deactivate_super(sb); |
| 3865 | } |
| 3866 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3867 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 3868 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3869 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3870 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3871 | } |
| 3872 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3873 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 3874 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3875 | { |
| 3876 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3877 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3878 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3879 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3880 | return 0; |
| 3881 | } |
| 3882 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3883 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3884 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3885 | .name = "cgroup.procs", |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3886 | .seq_start = cgroup_pidlist_start, |
| 3887 | .seq_next = cgroup_pidlist_next, |
| 3888 | .seq_stop = cgroup_pidlist_stop, |
| 3889 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3890 | .private = CGROUP_FILE_PROCS, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3891 | .write_u64 = cgroup_procs_write, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3892 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3893 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3894 | { |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3895 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3896 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3897 | .read_u64 = cgroup_clone_children_read, |
| 3898 | .write_u64 = cgroup_clone_children_write, |
| 3899 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3900 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3901 | .name = "cgroup.sane_behavior", |
| 3902 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3903 | .seq_show = cgroup_sane_behavior_show, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3904 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3905 | |
| 3906 | /* |
| 3907 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 3908 | * don't exist if sane_behavior. If you're depending on these, be |
| 3909 | * prepared to be burned. |
| 3910 | */ |
| 3911 | { |
| 3912 | .name = "tasks", |
| 3913 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3914 | .seq_start = cgroup_pidlist_start, |
| 3915 | .seq_next = cgroup_pidlist_next, |
| 3916 | .seq_stop = cgroup_pidlist_stop, |
| 3917 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3918 | .private = CGROUP_FILE_TASKS, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3919 | .write_u64 = cgroup_tasks_write, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3920 | .mode = S_IRUGO | S_IWUSR, |
| 3921 | }, |
| 3922 | { |
| 3923 | .name = "notify_on_release", |
| 3924 | .flags = CFTYPE_INSANE, |
| 3925 | .read_u64 = cgroup_read_notify_on_release, |
| 3926 | .write_u64 = cgroup_write_notify_on_release, |
| 3927 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3928 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3929 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 3930 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3931 | .seq_show = cgroup_release_agent_show, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3932 | .write_string = cgroup_release_agent_write, |
| 3933 | .max_write_len = PATH_MAX, |
| 3934 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3935 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3936 | }; |
| 3937 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3938 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3939 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3940 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3941 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3942 | * |
| 3943 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3944 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3945 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3946 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3947 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3948 | int i, ret = 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3949 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3950 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3951 | for_each_subsys(ss, i) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3952 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3953 | |
| 3954 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3955 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3956 | |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3957 | list_for_each_entry(set, &ss->cftsets, node) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3958 | ret = cgroup_addrm_files(cgrp, set->cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3959 | if (ret < 0) |
| 3960 | goto err; |
| 3961 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3962 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3963 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3964 | err: |
| 3965 | cgroup_clear_dir(cgrp, subsys_mask); |
| 3966 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3967 | } |
| 3968 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3969 | /* |
| 3970 | * css destruction is four-stage process. |
| 3971 | * |
| 3972 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 3973 | * Implemented in kill_css(). |
| 3974 | * |
| 3975 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
| 3976 | * and thus css_tryget() is guaranteed to fail, the css can be offlined |
| 3977 | * by invoking offline_css(). After offlining, the base ref is put. |
| 3978 | * Implemented in css_killed_work_fn(). |
| 3979 | * |
| 3980 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 3981 | * accessors are inside RCU read sections. css_release() schedules the |
| 3982 | * RCU callback. |
| 3983 | * |
| 3984 | * 4. After the grace period, the css can be freed. Implemented in |
| 3985 | * css_free_work_fn(). |
| 3986 | * |
| 3987 | * It is actually hairier because both step 2 and 4 require process context |
| 3988 | * and thus involve punting to css->destroy_work adding two additional |
| 3989 | * steps to the already complex sequence. |
| 3990 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3991 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3992 | { |
| 3993 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3994 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3995 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3996 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3997 | if (css->parent) |
| 3998 | css_put(css->parent); |
| 3999 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4000 | css->ss->css_free(css); |
| 4001 | cgroup_dput(cgrp); |
| 4002 | } |
| 4003 | |
| 4004 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 4005 | { |
| 4006 | struct cgroup_subsys_state *css = |
| 4007 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 4008 | |
| 4009 | /* |
| 4010 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4011 | * css_put(). dput() requires process context which we don't have. |
| 4012 | */ |
| 4013 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4014 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4015 | } |
| 4016 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4017 | static void css_release(struct percpu_ref *ref) |
| 4018 | { |
| 4019 | struct cgroup_subsys_state *css = |
| 4020 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4021 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4022 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4023 | } |
| 4024 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4025 | static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss, |
| 4026 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4027 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4028 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4029 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4030 | css->flags = 0; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4031 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4032 | if (cgrp->parent) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4033 | css->parent = cgroup_css(cgrp->parent, ss); |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4034 | else |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4035 | css->flags |= CSS_ROOT; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4036 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4037 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4038 | } |
| 4039 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4040 | /* 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] | 4041 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4042 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4043 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4044 | int ret = 0; |
| 4045 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4046 | lockdep_assert_held(&cgroup_mutex); |
| 4047 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4048 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4049 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4050 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4051 | css->flags |= CSS_ONLINE; |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4052 | css->cgroup->nr_css++; |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4053 | rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css); |
| 4054 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4055 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4056 | } |
| 4057 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4058 | /* 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] | 4059 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4060 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4061 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4062 | |
| 4063 | lockdep_assert_held(&cgroup_mutex); |
| 4064 | |
| 4065 | if (!(css->flags & CSS_ONLINE)) |
| 4066 | return; |
| 4067 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4068 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4069 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4070 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4071 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4072 | css->cgroup->nr_css--; |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4073 | RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4074 | } |
| 4075 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4076 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4077 | * cgroup_create - create a cgroup |
| 4078 | * @parent: cgroup that will be parent of the new cgroup |
| 4079 | * @dentry: dentry of the new cgroup |
| 4080 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4081 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4082 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4083 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4084 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4085 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4086 | { |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4087 | struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { }; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4088 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4089 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4090 | struct cgroupfs_root *root = parent->root; |
| 4091 | int err = 0; |
| 4092 | struct cgroup_subsys *ss; |
| 4093 | struct super_block *sb = root->sb; |
| 4094 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4095 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4096 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4097 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4098 | return -ENOMEM; |
| 4099 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4100 | name = cgroup_alloc_name(dentry); |
| 4101 | if (!name) |
| 4102 | goto err_free_cgrp; |
| 4103 | rcu_assign_pointer(cgrp->name, name); |
| 4104 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4105 | /* |
| 4106 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4107 | * a half-baked cgroup. |
| 4108 | */ |
| 4109 | cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4110 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4111 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4112 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4113 | /* |
| 4114 | * Only live parents can have children. Note that the liveliness |
| 4115 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4116 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4117 | * anyway so that locking is contained inside cgroup proper and we |
| 4118 | * don't get nasty surprises if we ever grow another caller. |
| 4119 | */ |
| 4120 | if (!cgroup_lock_live_group(parent)) { |
| 4121 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4122 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4123 | } |
| 4124 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4125 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4126 | * get deleted on unmount if there are child cgroups. This |
| 4127 | * can be done outside cgroup_mutex, since the sb can't |
| 4128 | * disappear while someone has an open control file on the |
| 4129 | * fs */ |
| 4130 | atomic_inc(&sb->s_active); |
| 4131 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4132 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4133 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4134 | dentry->d_fsdata = cgrp; |
| 4135 | cgrp->dentry = dentry; |
| 4136 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4137 | cgrp->parent = parent; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4138 | cgrp->dummy_css.parent = &parent->dummy_css; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4139 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4140 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4141 | if (notify_on_release(parent)) |
| 4142 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4143 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4144 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4145 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4146 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4147 | /* |
| 4148 | * Create directory. cgroup_create_file() returns with the new |
| 4149 | * directory locked on success so that it can be populated without |
| 4150 | * dropping cgroup_mutex. |
| 4151 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4152 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4153 | if (err < 0) |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame^] | 4154 | goto err_unlock; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4155 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4156 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4157 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4158 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4159 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4160 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4161 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4162 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4163 | /* hold a ref to the parent's dentry */ |
| 4164 | dget(parent->dentry); |
| 4165 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame^] | 4166 | /* |
| 4167 | * @cgrp is now fully operational. If something fails after this |
| 4168 | * point, it'll be released via the normal destruction path. |
| 4169 | */ |
| 4170 | idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 4171 | |
| 4172 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
| 4173 | if (err) |
| 4174 | goto err_destroy; |
| 4175 | |
| 4176 | for_each_root_subsys(root, ss) { |
| 4177 | struct cgroup_subsys_state *css; |
| 4178 | |
| 4179 | css = ss->css_alloc(cgroup_css(parent, ss)); |
| 4180 | if (IS_ERR(css)) { |
| 4181 | err = PTR_ERR(css); |
| 4182 | goto err_destroy; |
| 4183 | } |
| 4184 | css_ar[ss->subsys_id] = css; |
| 4185 | |
| 4186 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4187 | if (err) |
| 4188 | goto err_destroy; |
| 4189 | |
| 4190 | init_css(css, ss, cgrp); |
| 4191 | } |
| 4192 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4193 | /* creation succeeded, notify subsystems */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4194 | for_each_root_subsys(root, ss) { |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4195 | struct cgroup_subsys_state *css = css_ar[ss->subsys_id]; |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4196 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame^] | 4197 | err = cgroup_populate_dir(cgrp, 1 << ss->subsys_id); |
| 4198 | if (err) |
| 4199 | goto err_destroy; |
| 4200 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4201 | err = online_css(css); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4202 | if (err) |
| 4203 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4204 | |
Tejun Heo | 266ccd5 | 2013-12-06 15:07:32 -0500 | [diff] [blame] | 4205 | /* each css holds a ref to the cgroup's dentry and parent css */ |
| 4206 | dget(dentry); |
| 4207 | css_get(css->parent); |
| 4208 | |
| 4209 | /* mark it consumed for error path */ |
| 4210 | css_ar[ss->subsys_id] = NULL; |
| 4211 | |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4212 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4213 | parent->parent) { |
| 4214 | 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", |
| 4215 | current->comm, current->pid, ss->name); |
| 4216 | if (!strcmp(ss->name, "memory")) |
| 4217 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4218 | ss->warned_broken_hierarchy = true; |
| 4219 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4220 | } |
| 4221 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4222 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4223 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4224 | |
| 4225 | return 0; |
| 4226 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame^] | 4227 | err_unlock: |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4228 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4229 | /* Release the reference count that we took on the superblock */ |
| 4230 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4231 | err_free_id: |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4232 | idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4233 | err_free_name: |
| 4234 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4235 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4236 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4237 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4238 | |
| 4239 | err_destroy: |
Tejun Heo | 266ccd5 | 2013-12-06 15:07:32 -0500 | [diff] [blame] | 4240 | for_each_root_subsys(root, ss) { |
| 4241 | struct cgroup_subsys_state *css = css_ar[ss->subsys_id]; |
| 4242 | |
| 4243 | if (css) { |
| 4244 | percpu_ref_cancel_init(&css->refcnt); |
| 4245 | ss->css_free(css); |
| 4246 | } |
| 4247 | } |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4248 | cgroup_destroy_locked(cgrp); |
| 4249 | mutex_unlock(&cgroup_mutex); |
| 4250 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4251 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4252 | } |
| 4253 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4254 | 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] | 4255 | { |
| 4256 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4257 | |
| 4258 | /* the vfs holds inode->i_mutex already */ |
| 4259 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4260 | } |
| 4261 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4262 | /* |
| 4263 | * This is called when the refcnt of a css is confirmed to be killed. |
| 4264 | * css_tryget() is now guaranteed to fail. |
| 4265 | */ |
| 4266 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4267 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4268 | struct cgroup_subsys_state *css = |
| 4269 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 4270 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4271 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4272 | mutex_lock(&cgroup_mutex); |
| 4273 | |
| 4274 | /* |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4275 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4276 | * initate destruction. |
| 4277 | */ |
| 4278 | offline_css(css); |
| 4279 | |
| 4280 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4281 | * If @cgrp is marked dead, it's waiting for refs of all css's to |
| 4282 | * be disabled before proceeding to the second phase of cgroup |
| 4283 | * destruction. If we are the last one, kick it off. |
| 4284 | */ |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4285 | if (!cgrp->nr_css && cgroup_is_dead(cgrp)) |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4286 | cgroup_destroy_css_killed(cgrp); |
| 4287 | |
| 4288 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4289 | |
| 4290 | /* |
| 4291 | * Put the css refs from kill_css(). Each css holds an extra |
| 4292 | * reference to the cgroup's dentry and cgroup removal proceeds |
| 4293 | * regardless of css refs. On the last put of each css, whenever |
| 4294 | * that may be, the extra dentry ref is put so that dentry |
| 4295 | * destruction happens only after all css's are released. |
| 4296 | */ |
| 4297 | css_put(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4298 | } |
| 4299 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4300 | /* css kill confirmation processing requires process context, bounce */ |
| 4301 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4302 | { |
| 4303 | struct cgroup_subsys_state *css = |
| 4304 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4305 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4306 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4307 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | /** |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4311 | * kill_css - destroy a css |
| 4312 | * @css: css to destroy |
| 4313 | * |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4314 | * This function initiates destruction of @css by removing cgroup interface |
| 4315 | * files and putting its base reference. ->css_offline() will be invoked |
| 4316 | * asynchronously once css_tryget() is guaranteed to fail and when the |
| 4317 | * reference count reaches zero, @css will be released. |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4318 | */ |
| 4319 | static void kill_css(struct cgroup_subsys_state *css) |
| 4320 | { |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4321 | cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id); |
| 4322 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4323 | /* |
| 4324 | * Killing would put the base ref, but we need to keep it alive |
| 4325 | * until after ->css_offline(). |
| 4326 | */ |
| 4327 | css_get(css); |
| 4328 | |
| 4329 | /* |
| 4330 | * cgroup core guarantees that, by the time ->css_offline() is |
| 4331 | * invoked, no new css reference will be given out via |
| 4332 | * css_tryget(). We can't simply call percpu_ref_kill() and |
| 4333 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 4334 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 4335 | * |
| 4336 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4337 | * css is confirmed to be seen as killed on all CPUs. |
| 4338 | */ |
| 4339 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | /** |
| 4343 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4344 | * @cgrp: cgroup to be destroyed |
| 4345 | * |
| 4346 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4347 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4348 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4349 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4350 | * the following two steps. |
| 4351 | * |
| 4352 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4353 | * userland visible parts and start killing the percpu refcnts of |
| 4354 | * css's. Set up so that the next stage will be kicked off once all |
| 4355 | * the percpu refcnts are confirmed to be killed. |
| 4356 | * |
| 4357 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4358 | * rest of destruction. Once all cgroup references are gone, the |
| 4359 | * cgroup is RCU-freed. |
| 4360 | * |
| 4361 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4362 | * the userland is concerned and a new cgroup with the same name may be |
| 4363 | * created. As cgroup doesn't care about the names internally, this |
| 4364 | * doesn't cause any problem. |
| 4365 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4366 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4367 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4368 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4369 | struct dentry *d = cgrp->dentry; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4370 | struct cgroup_subsys *ss; |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4371 | struct cgroup *child; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4372 | bool empty; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4373 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4374 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4375 | lockdep_assert_held(&cgroup_mutex); |
| 4376 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4377 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4378 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4379 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4380 | */ |
| 4381 | read_lock(&css_set_lock); |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4382 | empty = list_empty(&cgrp->cset_links); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4383 | read_unlock(&css_set_lock); |
| 4384 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4385 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4386 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4387 | /* |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4388 | * Make sure there's no live children. We can't test ->children |
| 4389 | * emptiness as dead children linger on it while being destroyed; |
| 4390 | * otherwise, "rmdir parent/child parent" may fail with -EBUSY. |
| 4391 | */ |
| 4392 | empty = true; |
| 4393 | rcu_read_lock(); |
| 4394 | list_for_each_entry_rcu(child, &cgrp->children, sibling) { |
| 4395 | empty = cgroup_is_dead(child); |
| 4396 | if (!empty) |
| 4397 | break; |
| 4398 | } |
| 4399 | rcu_read_unlock(); |
| 4400 | if (!empty) |
| 4401 | return -EBUSY; |
| 4402 | |
| 4403 | /* |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4404 | * Initiate massacre of all css's. cgroup_destroy_css_killed() |
| 4405 | * will be invoked to perform the rest of destruction once the |
| 4406 | * percpu refs of all css's are confirmed to be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4407 | */ |
Tejun Heo | 266ccd5 | 2013-12-06 15:07:32 -0500 | [diff] [blame] | 4408 | for_each_root_subsys(cgrp->root, ss) { |
| 4409 | struct cgroup_subsys_state *css = cgroup_css(cgrp, ss); |
| 4410 | |
| 4411 | if (css) |
| 4412 | kill_css(css); |
| 4413 | } |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4414 | |
| 4415 | /* |
| 4416 | * Mark @cgrp dead. This prevents further task migration and child |
| 4417 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4418 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4419 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4420 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4421 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4422 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4423 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4424 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4425 | raw_spin_lock(&release_list_lock); |
| 4426 | if (!list_empty(&cgrp->release_list)) |
| 4427 | list_del_init(&cgrp->release_list); |
| 4428 | raw_spin_unlock(&release_list_lock); |
| 4429 | |
| 4430 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4431 | * If @cgrp has css's attached, the second stage of cgroup |
| 4432 | * destruction is kicked off from css_killed_work_fn() after the |
| 4433 | * refs of all attached css's are killed. If @cgrp doesn't have |
| 4434 | * any css, we kick it off here. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4435 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4436 | if (!cgrp->nr_css) |
| 4437 | cgroup_destroy_css_killed(cgrp); |
| 4438 | |
| 4439 | /* |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4440 | * Clear the base files and remove @cgrp directory. The removal |
| 4441 | * puts the base ref but we aren't quite done with @cgrp yet, so |
| 4442 | * hold onto it. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4443 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4444 | cgroup_addrm_files(cgrp, cgroup_base_files, false); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4445 | dget(d); |
| 4446 | cgroup_d_remove_dir(d); |
| 4447 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4448 | return 0; |
| 4449 | }; |
| 4450 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4451 | /** |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4452 | * cgroup_destroy_css_killed - the second step of cgroup destruction |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4453 | * @work: cgroup->destroy_free_work |
| 4454 | * |
| 4455 | * 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] | 4456 | * destroyed after all css's are offlined and performs the rest of |
| 4457 | * destruction. This is the second step of destruction described in the |
| 4458 | * comment above cgroup_destroy_locked(). |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4459 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4460 | static void cgroup_destroy_css_killed(struct cgroup *cgrp) |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4461 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4462 | struct cgroup *parent = cgrp->parent; |
| 4463 | struct dentry *d = cgrp->dentry; |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4464 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4465 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4466 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4467 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4468 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4469 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4470 | /* |
| 4471 | * We should remove the cgroup object from idr before its grace |
| 4472 | * period starts, so we won't be looking up a cgroup while the |
| 4473 | * cgroup is being freed. |
| 4474 | */ |
| 4475 | idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 4476 | cgrp->id = -1; |
| 4477 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4478 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4479 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4480 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4481 | check_for_release(parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4482 | } |
| 4483 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4484 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4485 | { |
| 4486 | int ret; |
| 4487 | |
| 4488 | mutex_lock(&cgroup_mutex); |
| 4489 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4490 | mutex_unlock(&cgroup_mutex); |
| 4491 | |
| 4492 | return ret; |
| 4493 | } |
| 4494 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4495 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4496 | { |
| 4497 | INIT_LIST_HEAD(&ss->cftsets); |
| 4498 | |
| 4499 | /* |
| 4500 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4501 | * deregistration. |
| 4502 | */ |
| 4503 | if (ss->base_cftypes) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4504 | struct cftype *cft; |
| 4505 | |
| 4506 | for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++) |
| 4507 | cft->ss = ss; |
| 4508 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4509 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4510 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4511 | } |
| 4512 | } |
| 4513 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4514 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4515 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4516 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4517 | |
| 4518 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4519 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4520 | mutex_lock(&cgroup_mutex); |
| 4521 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4522 | /* init base cftset */ |
| 4523 | cgroup_init_cftsets(ss); |
| 4524 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4525 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4526 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4527 | ss->root = &cgroup_dummy_root; |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4528 | css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4529 | /* We don't handle early failures gracefully */ |
| 4530 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4531 | init_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4532 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4533 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4534 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4535 | * newly registered, all tasks and hence the |
| 4536 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4537 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4538 | |
| 4539 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4540 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4541 | /* At system boot, before all subsystems have been |
| 4542 | * registered, no tasks have been forked, so we don't |
| 4543 | * need to invoke fork callbacks here. */ |
| 4544 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4545 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4546 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4547 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4548 | mutex_unlock(&cgroup_mutex); |
| 4549 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4550 | /* this function shouldn't be used with modular subsystems, since they |
| 4551 | * need to register a subsys_id, among other things */ |
| 4552 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4553 | } |
| 4554 | |
| 4555 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4556 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4557 | * @ss: the subsystem to load |
| 4558 | * |
| 4559 | * 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] | 4560 | * 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] | 4561 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4562 | * simpler cgroup_init_subsys. |
| 4563 | */ |
| 4564 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4565 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4566 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4567 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4568 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4569 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4570 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4571 | |
| 4572 | /* check name and function validity */ |
| 4573 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4574 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4575 | return -EINVAL; |
| 4576 | |
| 4577 | /* |
| 4578 | * we don't support callbacks in modular subsystems. this check is |
| 4579 | * before the ss->module check for consistency; a subsystem that could |
| 4580 | * be a module should still have no callbacks even if the user isn't |
| 4581 | * compiling it as one. |
| 4582 | */ |
| 4583 | if (ss->fork || ss->exit) |
| 4584 | return -EINVAL; |
| 4585 | |
| 4586 | /* |
| 4587 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4588 | * since cgroup_init_subsys will have already taken care of it. |
| 4589 | */ |
| 4590 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4591 | /* a sanity check */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4592 | BUG_ON(cgroup_subsys[ss->subsys_id] != ss); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4593 | return 0; |
| 4594 | } |
| 4595 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4596 | /* init base cftset */ |
| 4597 | cgroup_init_cftsets(ss); |
| 4598 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4599 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4600 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4601 | cgroup_subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4602 | |
| 4603 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4604 | * no ss->css_alloc seems to need anything important in the ss |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4605 | * struct, so this can happen first (i.e. before the dummy root |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4606 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4607 | */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4608 | css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4609 | if (IS_ERR(css)) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4610 | /* failure case - need to deassign the cgroup_subsys[] slot. */ |
| 4611 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4612 | mutex_unlock(&cgroup_mutex); |
| 4613 | return PTR_ERR(css); |
| 4614 | } |
| 4615 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4616 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4617 | ss->root = &cgroup_dummy_root; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4618 | |
| 4619 | /* our new subsystem will be attached to the dummy hierarchy. */ |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4620 | init_css(css, ss, cgroup_dummy_top); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4621 | |
| 4622 | /* |
| 4623 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4624 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4625 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4626 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4627 | * table state, so each changed css_set will need its hash recomputed. |
| 4628 | * this is all done under the css_set_lock. |
| 4629 | */ |
| 4630 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4631 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4632 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4633 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4634 | continue; |
| 4635 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4636 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4637 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4638 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4639 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4640 | key = css_set_hash(cset->subsys); |
| 4641 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4642 | } |
| 4643 | write_unlock(&css_set_lock); |
| 4644 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4645 | ret = online_css(css); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4646 | if (ret) |
| 4647 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4648 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4649 | /* success! */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4650 | mutex_unlock(&cgroup_root_mutex); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4651 | mutex_unlock(&cgroup_mutex); |
| 4652 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4653 | |
| 4654 | err_unload: |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4655 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4656 | mutex_unlock(&cgroup_mutex); |
| 4657 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4658 | cgroup_unload_subsys(ss); |
| 4659 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4660 | } |
| 4661 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4662 | |
| 4663 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4664 | * cgroup_unload_subsys: unload a modular subsystem |
| 4665 | * @ss: the subsystem to unload |
| 4666 | * |
| 4667 | * This function should be called in a modular subsystem's exitcall. When this |
| 4668 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4669 | * the subsystem will not be attached to any hierarchy. |
| 4670 | */ |
| 4671 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4672 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4673 | struct cgrp_cset_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4674 | |
| 4675 | BUG_ON(ss->module == NULL); |
| 4676 | |
| 4677 | /* |
| 4678 | * 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] | 4679 | * try_module_get() in rebind_subsystems() should ensure that it |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4680 | * doesn't start being used while we're killing it off. |
| 4681 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4682 | BUG_ON(ss->root != &cgroup_dummy_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4683 | |
| 4684 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4685 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4686 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4687 | offline_css(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4688 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4689 | /* deassign the subsys_id */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4690 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4691 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4692 | /* remove subsystem from the dummy root's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4693 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4694 | |
| 4695 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4696 | * disentangle the css from all css_sets attached to the dummy |
| 4697 | * top. as in loading, we need to pay our respects to the hashtable |
| 4698 | * gods. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4699 | */ |
| 4700 | write_lock(&css_set_lock); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4701 | list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4702 | struct css_set *cset = link->cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4703 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4704 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4705 | hash_del(&cset->hlist); |
| 4706 | cset->subsys[ss->subsys_id] = NULL; |
| 4707 | key = css_set_hash(cset->subsys); |
| 4708 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4709 | } |
| 4710 | write_unlock(&css_set_lock); |
| 4711 | |
| 4712 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4713 | * remove subsystem's css from the cgroup_dummy_top and free it - |
| 4714 | * need to free before marking as null because ss->css_free needs |
Li Zefan | 2ff2a7d | 2013-09-23 16:57:03 +0800 | [diff] [blame] | 4715 | * the cgrp->subsys pointer to find their state. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4716 | */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4717 | ss->css_free(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4718 | RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4719 | |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4720 | mutex_unlock(&cgroup_root_mutex); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4721 | mutex_unlock(&cgroup_mutex); |
| 4722 | } |
| 4723 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4724 | |
| 4725 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4726 | * cgroup_init_early - cgroup initialization at system boot |
| 4727 | * |
| 4728 | * Initialize cgroups at system boot, and initialize any |
| 4729 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4730 | */ |
| 4731 | int __init cgroup_init_early(void) |
| 4732 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4733 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4734 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4735 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4736 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4737 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4738 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4739 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4740 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4741 | init_cgroup_root(&cgroup_dummy_root); |
| 4742 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4743 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4744 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4745 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4746 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4747 | 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] | 4748 | 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] | 4749 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4750 | /* at bootup time, we don't worry about modular subsystems */ |
| 4751 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4752 | BUG_ON(!ss->name); |
| 4753 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4754 | BUG_ON(!ss->css_alloc); |
| 4755 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4756 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4757 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4758 | ss->name, ss->subsys_id); |
| 4759 | BUG(); |
| 4760 | } |
| 4761 | |
| 4762 | if (ss->early_init) |
| 4763 | cgroup_init_subsys(ss); |
| 4764 | } |
| 4765 | return 0; |
| 4766 | } |
| 4767 | |
| 4768 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4769 | * cgroup_init - cgroup initialization |
| 4770 | * |
| 4771 | * Register cgroup filesystem and /proc file, and initialize |
| 4772 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4773 | */ |
| 4774 | int __init cgroup_init(void) |
| 4775 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4776 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4777 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4778 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4779 | |
| 4780 | err = bdi_init(&cgroup_backing_dev_info); |
| 4781 | if (err) |
| 4782 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4783 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4784 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4785 | if (!ss->early_init) |
| 4786 | cgroup_init_subsys(ss); |
| 4787 | } |
| 4788 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4789 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4790 | mutex_lock(&cgroup_mutex); |
| 4791 | mutex_lock(&cgroup_root_mutex); |
| 4792 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4793 | /* Add init_css_set to the hash table */ |
| 4794 | key = css_set_hash(init_css_set.subsys); |
| 4795 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4796 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4797 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4798 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4799 | err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top, |
| 4800 | 0, 1, GFP_KERNEL); |
| 4801 | BUG_ON(err < 0); |
| 4802 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4803 | mutex_unlock(&cgroup_root_mutex); |
| 4804 | mutex_unlock(&cgroup_mutex); |
| 4805 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4806 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4807 | if (!cgroup_kobj) { |
| 4808 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4809 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | err = register_filesystem(&cgroup_fs_type); |
| 4813 | if (err < 0) { |
| 4814 | kobject_put(cgroup_kobj); |
| 4815 | goto out; |
| 4816 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4817 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4818 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4819 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4820 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4821 | if (err) |
| 4822 | bdi_destroy(&cgroup_backing_dev_info); |
| 4823 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4824 | return err; |
| 4825 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4826 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4827 | static int __init cgroup_wq_init(void) |
| 4828 | { |
| 4829 | /* |
| 4830 | * There isn't much point in executing destruction path in |
| 4831 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
| 4832 | * Use 1 for @max_active. |
| 4833 | * |
| 4834 | * We would prefer to do this in cgroup_init() above, but that |
| 4835 | * is called before init_workqueues(): so leave this until after. |
| 4836 | */ |
| 4837 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
| 4838 | BUG_ON(!cgroup_destroy_wq); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 4839 | |
| 4840 | /* |
| 4841 | * Used to destroy pidlists and separate to serve as flush domain. |
| 4842 | * Cap @max_active to 1 too. |
| 4843 | */ |
| 4844 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 4845 | 0, 1); |
| 4846 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 4847 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4848 | return 0; |
| 4849 | } |
| 4850 | core_initcall(cgroup_wq_init); |
| 4851 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4852 | /* |
| 4853 | * proc_cgroup_show() |
| 4854 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4855 | * - Used for /proc/<pid>/cgroup. |
| 4856 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4857 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4858 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4859 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4860 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4861 | * cgroup to top_cgroup. |
| 4862 | */ |
| 4863 | |
| 4864 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4865 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4866 | { |
| 4867 | struct pid *pid; |
| 4868 | struct task_struct *tsk; |
| 4869 | char *buf; |
| 4870 | int retval; |
| 4871 | struct cgroupfs_root *root; |
| 4872 | |
| 4873 | retval = -ENOMEM; |
| 4874 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4875 | if (!buf) |
| 4876 | goto out; |
| 4877 | |
| 4878 | retval = -ESRCH; |
| 4879 | pid = m->private; |
| 4880 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4881 | if (!tsk) |
| 4882 | goto out_free; |
| 4883 | |
| 4884 | retval = 0; |
| 4885 | |
| 4886 | mutex_lock(&cgroup_mutex); |
| 4887 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4888 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4889 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4890 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4891 | int count = 0; |
| 4892 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4893 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4894 | for_each_root_subsys(root, ss) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4895 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4896 | if (strlen(root->name)) |
| 4897 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4898 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4899 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4900 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4901 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4902 | if (retval < 0) |
| 4903 | goto out_unlock; |
| 4904 | seq_puts(m, buf); |
| 4905 | seq_putc(m, '\n'); |
| 4906 | } |
| 4907 | |
| 4908 | out_unlock: |
| 4909 | mutex_unlock(&cgroup_mutex); |
| 4910 | put_task_struct(tsk); |
| 4911 | out_free: |
| 4912 | kfree(buf); |
| 4913 | out: |
| 4914 | return retval; |
| 4915 | } |
| 4916 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4917 | /* Display information about each subsystem and each hierarchy */ |
| 4918 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4919 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4920 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4921 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4922 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4923 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4924 | /* |
| 4925 | * ideally we don't want subsystems moving around while we do this. |
| 4926 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4927 | * subsys/hierarchy state. |
| 4928 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4929 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4930 | |
| 4931 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4932 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4933 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4934 | ss->root->number_of_cgroups, !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4935 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4936 | mutex_unlock(&cgroup_mutex); |
| 4937 | return 0; |
| 4938 | } |
| 4939 | |
| 4940 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4941 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4942 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4943 | } |
| 4944 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4945 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4946 | .open = cgroupstats_open, |
| 4947 | .read = seq_read, |
| 4948 | .llseek = seq_lseek, |
| 4949 | .release = single_release, |
| 4950 | }; |
| 4951 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4952 | /** |
| 4953 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4954 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4955 | * |
| 4956 | * Description: A task inherits its parent's cgroup at fork(). |
| 4957 | * |
| 4958 | * A pointer to the shared css_set was automatically copied in |
| 4959 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4960 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 4961 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 4962 | * have already changed current->cgroups, allowing the previously |
| 4963 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4964 | * |
| 4965 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 4966 | * task, and the passed argument 'child' points to the child task. |
| 4967 | */ |
| 4968 | void cgroup_fork(struct task_struct *child) |
| 4969 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4970 | task_lock(current); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4971 | get_css_set(task_css_set(current)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4972 | child->cgroups = current->cgroups; |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4973 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4974 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4975 | } |
| 4976 | |
| 4977 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4978 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4979 | * @child: the task in question |
| 4980 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4981 | * Adds the task to the list running through its css_set if necessary and |
| 4982 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4983 | * 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] | 4984 | * 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] | 4985 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4986 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4987 | void cgroup_post_fork(struct task_struct *child) |
| 4988 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4989 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4990 | int i; |
| 4991 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4992 | /* |
| 4993 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 4994 | * under the tasklist_lock and we read it here after we added the child |
| 4995 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 4996 | * yet in the tasklist when we walked through it from |
| 4997 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 4998 | * should be visible now due to the paired locking and barriers implied |
| 4999 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 5000 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 5001 | * lock on fork. |
| 5002 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5003 | if (use_task_css_set_links) { |
| 5004 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5005 | task_lock(child); |
| 5006 | if (list_empty(&child->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5007 | list_add(&child->cg_list, &task_css_set(child)->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5008 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5009 | write_unlock(&css_set_lock); |
| 5010 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5011 | |
| 5012 | /* |
| 5013 | * Call ss->fork(). This must happen after @child is linked on |
| 5014 | * css_set; otherwise, @child might change state between ->fork() |
| 5015 | * and addition to css_set. |
| 5016 | */ |
| 5017 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5018 | /* |
| 5019 | * fork/exit callbacks are supported only for builtin |
| 5020 | * subsystems, and the builtin section of the subsys |
| 5021 | * array is immutable, so we don't need to lock the |
| 5022 | * subsys array here. On the other hand, modular section |
| 5023 | * of the array can be freed at module unload, so we |
| 5024 | * can't touch that. |
| 5025 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5026 | for_each_builtin_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5027 | if (ss->fork) |
| 5028 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5029 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5030 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5031 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5032 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5033 | * cgroup_exit - detach cgroup from exiting task |
| 5034 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5035 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5036 | * |
| 5037 | * Description: Detach cgroup from @tsk and release it. |
| 5038 | * |
| 5039 | * Note that cgroups marked notify_on_release force every task in |
| 5040 | * them to take the global cgroup_mutex mutex when exiting. |
| 5041 | * This could impact scaling on very large systems. Be reluctant to |
| 5042 | * use notify_on_release cgroups where very high task exit scaling |
| 5043 | * is required on large systems. |
| 5044 | * |
| 5045 | * the_top_cgroup_hack: |
| 5046 | * |
| 5047 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 5048 | * |
| 5049 | * We call cgroup_exit() while the task is still competent to |
| 5050 | * handle notify_on_release(), then leave the task attached to the |
| 5051 | * root cgroup in each hierarchy for the remainder of its exit. |
| 5052 | * |
| 5053 | * To do this properly, we would increment the reference count on |
| 5054 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 5055 | * code we would add a second cgroup function call, to drop that |
| 5056 | * reference. This would just create an unnecessary hot spot on |
| 5057 | * the top_cgroup reference count, to no avail. |
| 5058 | * |
| 5059 | * Normally, holding a reference to a cgroup without bumping its |
| 5060 | * count is unsafe. The cgroup could go away, or someone could |
| 5061 | * attach us to a different cgroup, decrementing the count on |
| 5062 | * the first cgroup that we never incremented. But in this case, |
| 5063 | * 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] | 5064 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5065 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5066 | */ |
| 5067 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5068 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5069 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5070 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5071 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5072 | |
| 5073 | /* |
| 5074 | * Unlink from the css_set task list if necessary. |
| 5075 | * Optimistically check cg_list before taking |
| 5076 | * css_set_lock |
| 5077 | */ |
| 5078 | if (!list_empty(&tsk->cg_list)) { |
| 5079 | write_lock(&css_set_lock); |
| 5080 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5081 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5082 | write_unlock(&css_set_lock); |
| 5083 | } |
| 5084 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5085 | /* Reassign the task to the init_css_set. */ |
| 5086 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5087 | cset = task_css_set(tsk); |
| 5088 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5089 | |
| 5090 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5091 | /* |
| 5092 | * fork/exit callbacks are supported only for builtin |
| 5093 | * subsystems, see cgroup_post_fork() for details. |
| 5094 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5095 | for_each_builtin_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5096 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5097 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 5098 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5099 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5100 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5101 | } |
| 5102 | } |
| 5103 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5104 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5105 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5106 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5107 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5108 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5109 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5110 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5111 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5112 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5113 | /* |
| 5114 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5115 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5116 | * it now |
| 5117 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5118 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5119 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5120 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5121 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5122 | list_empty(&cgrp->release_list)) { |
| 5123 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5124 | need_schedule_work = 1; |
| 5125 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5126 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5127 | if (need_schedule_work) |
| 5128 | schedule_work(&release_agent_work); |
| 5129 | } |
| 5130 | } |
| 5131 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5132 | /* |
| 5133 | * Notify userspace when a cgroup is released, by running the |
| 5134 | * configured release agent with the name of the cgroup (path |
| 5135 | * relative to the root of cgroup file system) as the argument. |
| 5136 | * |
| 5137 | * Most likely, this user command will try to rmdir this cgroup. |
| 5138 | * |
| 5139 | * This races with the possibility that some other task will be |
| 5140 | * attached to this cgroup before it is removed, or that some other |
| 5141 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5142 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5143 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5144 | * to continue to serve a useful existence. Next time it's released, |
| 5145 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5146 | * |
| 5147 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5148 | * means only wait until the task is successfully execve()'d. The |
| 5149 | * separate release agent task is forked by call_usermodehelper(), |
| 5150 | * then control in this thread returns here, without waiting for the |
| 5151 | * release agent task. We don't bother to wait because the caller of |
| 5152 | * this routine has no use for the exit status of the release agent |
| 5153 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5154 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5155 | static void cgroup_release_agent(struct work_struct *work) |
| 5156 | { |
| 5157 | BUG_ON(work != &release_agent_work); |
| 5158 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5159 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5160 | while (!list_empty(&release_list)) { |
| 5161 | char *argv[3], *envp[3]; |
| 5162 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5163 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5164 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5165 | struct cgroup, |
| 5166 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5167 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5168 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5169 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5170 | if (!pathbuf) |
| 5171 | goto continue_free; |
| 5172 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5173 | goto continue_free; |
| 5174 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5175 | if (!agentbuf) |
| 5176 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5177 | |
| 5178 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5179 | argv[i++] = agentbuf; |
| 5180 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5181 | argv[i] = NULL; |
| 5182 | |
| 5183 | i = 0; |
| 5184 | /* minimal command environment */ |
| 5185 | envp[i++] = "HOME=/"; |
| 5186 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5187 | envp[i] = NULL; |
| 5188 | |
| 5189 | /* Drop the lock while we invoke the usermode helper, |
| 5190 | * since the exec could involve hitting disk and hence |
| 5191 | * be a slow process */ |
| 5192 | mutex_unlock(&cgroup_mutex); |
| 5193 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5194 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5195 | continue_free: |
| 5196 | kfree(pathbuf); |
| 5197 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5198 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5199 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5200 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5201 | mutex_unlock(&cgroup_mutex); |
| 5202 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5203 | |
| 5204 | static int __init cgroup_disable(char *str) |
| 5205 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5206 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5207 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5208 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5209 | |
| 5210 | while ((token = strsep(&str, ",")) != NULL) { |
| 5211 | if (!*token) |
| 5212 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5213 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5214 | /* |
| 5215 | * cgroup_disable, being at boot time, can't know about |
| 5216 | * module subsystems, so we don't worry about them. |
| 5217 | */ |
| 5218 | for_each_builtin_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5219 | if (!strcmp(token, ss->name)) { |
| 5220 | ss->disabled = 1; |
| 5221 | printk(KERN_INFO "Disabling %s control group" |
| 5222 | " subsystem\n", ss->name); |
| 5223 | break; |
| 5224 | } |
| 5225 | } |
| 5226 | } |
| 5227 | return 1; |
| 5228 | } |
| 5229 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5230 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5231 | /** |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5232 | * css_from_dir - get corresponding css from the dentry of a cgroup dir |
| 5233 | * @dentry: directory dentry of interest |
| 5234 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5235 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 5236 | * Must be called under cgroup_mutex or RCU read lock. The caller is |
| 5237 | * responsible for pinning the returned css if it needs to be accessed |
| 5238 | * outside the critical section. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5239 | */ |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5240 | struct cgroup_subsys_state *css_from_dir(struct dentry *dentry, |
| 5241 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5242 | { |
| 5243 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5244 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 5245 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5246 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5247 | /* is @dentry a cgroup dir? */ |
| 5248 | if (!dentry->d_inode || |
| 5249 | dentry->d_inode->i_op != &cgroup_dir_inode_operations) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5250 | return ERR_PTR(-EBADF); |
| 5251 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5252 | cgrp = __d_cgrp(dentry); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5253 | return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5254 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5255 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5256 | /** |
| 5257 | * css_from_id - lookup css by id |
| 5258 | * @id: the cgroup id |
| 5259 | * @ss: cgroup subsys to be looked into |
| 5260 | * |
| 5261 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5262 | * Should be called under rcu_read_lock(). |
| 5263 | */ |
| 5264 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5265 | { |
| 5266 | struct cgroup *cgrp; |
| 5267 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 5268 | cgroup_assert_mutex_or_rcu_locked(); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5269 | |
| 5270 | cgrp = idr_find(&ss->root->cgroup_idr, id); |
| 5271 | if (cgrp) |
Tejun Heo | d162596 | 2013-08-27 14:27:23 -0400 | [diff] [blame] | 5272 | return cgroup_css(cgrp, ss); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5273 | return NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5274 | } |
| 5275 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5276 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5277 | static struct cgroup_subsys_state * |
| 5278 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5279 | { |
| 5280 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5281 | |
| 5282 | if (!css) |
| 5283 | return ERR_PTR(-ENOMEM); |
| 5284 | |
| 5285 | return css; |
| 5286 | } |
| 5287 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5288 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5289 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5290 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5291 | } |
| 5292 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5293 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 5294 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5295 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5296 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5297 | } |
| 5298 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5299 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 5300 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5301 | { |
| 5302 | return (u64)(unsigned long)current->cgroups; |
| 5303 | } |
| 5304 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5305 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5306 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5307 | { |
| 5308 | u64 count; |
| 5309 | |
| 5310 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5311 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5312 | rcu_read_unlock(); |
| 5313 | return count; |
| 5314 | } |
| 5315 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5316 | 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] | 5317 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5318 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5319 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5320 | |
| 5321 | read_lock(&css_set_lock); |
| 5322 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5323 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5324 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5325 | struct cgroup *c = link->cgrp; |
| 5326 | const char *name; |
| 5327 | |
| 5328 | if (c->dentry) |
| 5329 | name = c->dentry->d_name.name; |
| 5330 | else |
| 5331 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5332 | seq_printf(seq, "Root %d group %s\n", |
| 5333 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5334 | } |
| 5335 | rcu_read_unlock(); |
| 5336 | read_unlock(&css_set_lock); |
| 5337 | return 0; |
| 5338 | } |
| 5339 | |
| 5340 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5341 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5342 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5343 | struct cgroup_subsys_state *css = seq_css(seq); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5344 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5345 | |
| 5346 | read_lock(&css_set_lock); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5347 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5348 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5349 | struct task_struct *task; |
| 5350 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5351 | seq_printf(seq, "css_set %p\n", cset); |
| 5352 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5353 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5354 | seq_puts(seq, " ...\n"); |
| 5355 | break; |
| 5356 | } else { |
| 5357 | seq_printf(seq, " task %d\n", |
| 5358 | task_pid_vnr(task)); |
| 5359 | } |
| 5360 | } |
| 5361 | } |
| 5362 | read_unlock(&css_set_lock); |
| 5363 | return 0; |
| 5364 | } |
| 5365 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5366 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5367 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5368 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | static struct cftype debug_files[] = { |
| 5372 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5373 | .name = "taskcount", |
| 5374 | .read_u64 = debug_taskcount_read, |
| 5375 | }, |
| 5376 | |
| 5377 | { |
| 5378 | .name = "current_css_set", |
| 5379 | .read_u64 = current_css_set_read, |
| 5380 | }, |
| 5381 | |
| 5382 | { |
| 5383 | .name = "current_css_set_refcount", |
| 5384 | .read_u64 = current_css_set_refcount_read, |
| 5385 | }, |
| 5386 | |
| 5387 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5388 | .name = "current_css_set_cg_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5389 | .seq_show = current_css_set_cg_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5390 | }, |
| 5391 | |
| 5392 | { |
| 5393 | .name = "cgroup_css_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5394 | .seq_show = cgroup_css_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5395 | }, |
| 5396 | |
| 5397 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5398 | .name = "releasable", |
| 5399 | .read_u64 = releasable_read, |
| 5400 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5401 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5402 | { } /* terminate */ |
| 5403 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5404 | |
| 5405 | struct cgroup_subsys debug_subsys = { |
| 5406 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5407 | .css_alloc = debug_css_alloc, |
| 5408 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5409 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5410 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5411 | }; |
| 5412 | #endif /* CONFIG_CGROUP_DEBUG */ |