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> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 50 | #include <linux/delayacct.h> |
| 51 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 52 | #include <linux/hashtable.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 53 | #include <linux/namei.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 54 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 55 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 56 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 57 | #include <linux/flex_array.h> /* used in cgroup_attach_task */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 58 | #include <linux/kthread.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 59 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 60 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 61 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 62 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 63 | * pidlists linger the following amount before being destroyed. The goal |
| 64 | * is avoiding frequent destruction in the middle of consecutive read calls |
| 65 | * Expiring in the middle is a performance problem not a correctness one. |
| 66 | * 1 sec should be enough. |
| 67 | */ |
| 68 | #define CGROUP_PIDLIST_DESTROY_DELAY HZ |
| 69 | |
| 70 | /* |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 71 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 72 | * hierarchy must be performed while holding it. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 73 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 74 | #ifdef CONFIG_PROVE_RCU |
| 75 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 76 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 77 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 78 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 79 | #endif |
| 80 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 81 | /* |
| 82 | * Protects cgroup_subsys->release_agent_path. Modifying it also requires |
| 83 | * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock. |
| 84 | */ |
| 85 | static DEFINE_SPINLOCK(release_agent_path_lock); |
| 86 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 87 | #define cgroup_assert_mutex_or_rcu_locked() \ |
| 88 | rcu_lockdep_assert(rcu_read_lock_held() || \ |
| 89 | lockdep_is_held(&cgroup_mutex), \ |
| 90 | "cgroup_mutex or RCU read lock required"); |
| 91 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 92 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 93 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 94 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 95 | * destruction work items don't end up filling up max_active of system_wq |
| 96 | * which may lead to deadlock. |
| 97 | */ |
| 98 | static struct workqueue_struct *cgroup_destroy_wq; |
| 99 | |
| 100 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 101 | * pidlist destructions need to be flushed on cgroup destruction. Use a |
| 102 | * separate workqueue as flush domain. |
| 103 | */ |
| 104 | static struct workqueue_struct *cgroup_pidlist_destroy_wq; |
| 105 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 106 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 107 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 108 | static struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 109 | #include <linux/cgroup_subsys.h> |
| 110 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 111 | #undef SUBSYS |
| 112 | |
| 113 | /* array of cgroup subsystem names */ |
| 114 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 115 | static const char *cgroup_subsys_name[] = { |
| 116 | #include <linux/cgroup_subsys.h> |
| 117 | }; |
| 118 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 119 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 120 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 121 | * The dummy hierarchy, reserved for the subsystems that are otherwise |
| 122 | * unattached - it never has more than a single cgroup, and all tasks are |
| 123 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 124 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 125 | static struct cgroupfs_root cgroup_dummy_root; |
| 126 | |
| 127 | /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ |
| 128 | static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 129 | |
| 130 | /* The list of hierarchy roots */ |
| 131 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 132 | static LIST_HEAD(cgroup_roots); |
| 133 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 134 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 135 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 136 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 137 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 138 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 139 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 140 | /* |
| 141 | * Assign a monotonically increasing serial number to cgroups. It |
| 142 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 143 | * numbers. Also, as cgroups are always appended to the parent's |
| 144 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 145 | * the ascending serial number order on the list. Protected by |
| 146 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 147 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 148 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 149 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 150 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 151 | * check for fork/exit handlers to call. This avoids us having to do |
| 152 | * extra work in the fork/exit path if none of the subsystems need to |
| 153 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 154 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 155 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 156 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 157 | static struct cftype cgroup_base_files[]; |
| 158 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 159 | static void cgroup_destroy_css_killed(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 160 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 161 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 162 | bool is_add); |
Tejun Heo | e605b36 | 2013-11-27 18:16:21 -0500 | [diff] [blame] | 163 | static int cgroup_file_release(struct inode *inode, struct file *file); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 164 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 165 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 166 | /** |
| 167 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 168 | * @cgrp: the cgroup of interest |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 169 | * @ss: the subsystem of interest (%NULL returns the dummy_css) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 170 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 171 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 172 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 173 | * the caller is responsible for pinning the returned css if it wants to |
| 174 | * keep accessing it outside the said locks. This function may return |
| 175 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 176 | */ |
| 177 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 178 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 179 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 180 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 181 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 182 | lockdep_is_held(&cgroup_mutex)); |
| 183 | else |
| 184 | return &cgrp->dummy_css; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 185 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 186 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 187 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 188 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 189 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 190 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 193 | /** |
| 194 | * cgroup_is_descendant - test ancestry |
| 195 | * @cgrp: the cgroup to be tested |
| 196 | * @ancestor: possible ancestor of @cgrp |
| 197 | * |
| 198 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 199 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 200 | * and @ancestor are accessible. |
| 201 | */ |
| 202 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 203 | { |
| 204 | while (cgrp) { |
| 205 | if (cgrp == ancestor) |
| 206 | return true; |
| 207 | cgrp = cgrp->parent; |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 212 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 213 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 214 | { |
| 215 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 216 | (1 << CGRP_RELEASABLE) | |
| 217 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 218 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 221 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 222 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 223 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 226 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 227 | * for_each_css - iterate all css's of a cgroup |
| 228 | * @css: the iteration cursor |
| 229 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 230 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 231 | * |
| 232 | * Should be called under cgroup_mutex. |
| 233 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 234 | #define for_each_css(css, ssid, cgrp) \ |
| 235 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 236 | if (!((css) = rcu_dereference_check( \ |
| 237 | (cgrp)->subsys[(ssid)], \ |
| 238 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 239 | else |
| 240 | |
| 241 | /** |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 242 | * for_each_subsys - iterate all enabled cgroup subsystems |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 243 | * @ss: the iteration cursor |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 244 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 245 | */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 246 | #define for_each_subsys(ss, ssid) \ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 247 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \ |
| 248 | (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 249 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 250 | /* iterate across the active hierarchies */ |
| 251 | #define for_each_active_root(root) \ |
| 252 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 253 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 254 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 255 | { |
| 256 | return dentry->d_fsdata; |
| 257 | } |
| 258 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 259 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 260 | { |
| 261 | return dentry->d_fsdata; |
| 262 | } |
| 263 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 264 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 265 | { |
| 266 | return __d_cfe(dentry)->type; |
| 267 | } |
| 268 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 269 | /** |
| 270 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 271 | * @cgrp: the cgroup to be checked for liveness |
| 272 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 273 | * On success, returns true; the mutex should be later unlocked. On |
| 274 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 275 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 276 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 277 | { |
| 278 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 279 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 280 | mutex_unlock(&cgroup_mutex); |
| 281 | return false; |
| 282 | } |
| 283 | return true; |
| 284 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 285 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 286 | /* the list of cgroups eligible for automatic release. Protected by |
| 287 | * release_list_lock */ |
| 288 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 289 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 290 | static void cgroup_release_agent(struct work_struct *work); |
| 291 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 292 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 293 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 294 | /* |
| 295 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 296 | * belong to different cgroups on different hierarchies. In the other |
| 297 | * direction, a css_set is naturally associated with multiple cgroups. |
| 298 | * This M:N relationship is represented by the following link structure |
| 299 | * which exists for each association and allows traversing the associations |
| 300 | * from both sides. |
| 301 | */ |
| 302 | struct cgrp_cset_link { |
| 303 | /* the cgroup and css_set this link associates */ |
| 304 | struct cgroup *cgrp; |
| 305 | struct css_set *cset; |
| 306 | |
| 307 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 308 | struct list_head cset_link; |
| 309 | |
| 310 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 311 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | /* The default css_set - used by init and its children prior to any |
| 315 | * hierarchies being mounted. It contains a pointer to the root state |
| 316 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 317 | * reference-counted, to improve performance when child cgroups |
| 318 | * haven't been created. |
| 319 | */ |
| 320 | |
| 321 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 322 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 323 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 324 | /* |
| 325 | * css_set_lock protects the list of css_set objects, and the chain of |
| 326 | * tasks off each css_set. Nests outside task->alloc_lock due to |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 327 | * css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 328 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 329 | static DEFINE_RWLOCK(css_set_lock); |
| 330 | static int css_set_count; |
| 331 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 332 | /* |
| 333 | * hash table for cgroup groups. This improves the performance to find |
| 334 | * an existing css_set. This hash doesn't (currently) take into |
| 335 | * account cgroups in empty hierarchies. |
| 336 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 337 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 338 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 339 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 340 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 341 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 342 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 343 | struct cgroup_subsys *ss; |
| 344 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 345 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 346 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 347 | key += (unsigned long)css[i]; |
| 348 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 349 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 350 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 353 | /* |
| 354 | * 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] | 355 | * until after the first call to css_task_iter_start(). This reduces the |
| 356 | * fork()/exit() overhead for people who have cgroups compiled into their |
| 357 | * kernel but not actually in use. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 358 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 359 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 360 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 361 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 362 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 363 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 364 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 365 | /* |
| 366 | * Ensure that the refcount doesn't hit zero while any readers |
| 367 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 368 | * rwlock |
| 369 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 370 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 371 | return; |
| 372 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 373 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 374 | write_unlock(&css_set_lock); |
| 375 | return; |
| 376 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 377 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 378 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 379 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 380 | css_set_count--; |
| 381 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 382 | 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] | 383 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 384 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 385 | list_del(&link->cset_link); |
| 386 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 387 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 388 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 389 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 390 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 391 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 392 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 393 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 394 | |
| 395 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 396 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 397 | |
| 398 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 399 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* |
| 403 | * refcounted get/put for css_set objects |
| 404 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 405 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 406 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 407 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 410 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 411 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 412 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 415 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 416 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 417 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 420 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 421 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 422 | * @cset: candidate css_set being tested |
| 423 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 424 | * @new_cgrp: cgroup that's being entered by the task |
| 425 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 426 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 427 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 428 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 429 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 430 | static bool compare_css_sets(struct css_set *cset, |
| 431 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 432 | struct cgroup *new_cgrp, |
| 433 | struct cgroup_subsys_state *template[]) |
| 434 | { |
| 435 | struct list_head *l1, *l2; |
| 436 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 437 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 438 | /* Not all subsystems matched */ |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Compare cgroup pointers in order to distinguish between |
| 444 | * different cgroups in heirarchies with no subsystems. We |
| 445 | * could get by with just this check alone (and skip the |
| 446 | * memcmp above) but on most setups the memcmp check will |
| 447 | * avoid the need for this more expensive check on almost all |
| 448 | * candidates. |
| 449 | */ |
| 450 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 451 | l1 = &cset->cgrp_links; |
| 452 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 453 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 454 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 455 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 456 | |
| 457 | l1 = l1->next; |
| 458 | l2 = l2->next; |
| 459 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 460 | if (l1 == &cset->cgrp_links) { |
| 461 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 462 | break; |
| 463 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 464 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 465 | } |
| 466 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 467 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 468 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 469 | cgrp1 = link1->cgrp; |
| 470 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 471 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 472 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 473 | |
| 474 | /* |
| 475 | * If this hierarchy is the hierarchy of the cgroup |
| 476 | * that's changing, then we need to check that this |
| 477 | * css_set points to the new cgroup; if it's any other |
| 478 | * hierarchy, then this css_set should point to the |
| 479 | * same cgroup as the old css_set. |
| 480 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 481 | if (cgrp1->root == new_cgrp->root) { |
| 482 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 483 | return false; |
| 484 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 485 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 486 | return false; |
| 487 | } |
| 488 | } |
| 489 | return true; |
| 490 | } |
| 491 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 492 | /** |
| 493 | * find_existing_css_set - init css array and find the matching css_set |
| 494 | * @old_cset: the css_set that we're using before the cgroup transition |
| 495 | * @cgrp: the cgroup that we're moving into |
| 496 | * @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] | 497 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 498 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 499 | struct cgroup *cgrp, |
| 500 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 501 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 502 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 503 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 504 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 505 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 506 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 507 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 508 | /* |
| 509 | * Build the set of subsystem state objects that we want to see in the |
| 510 | * new css_set. while subsystems can change globally, the entries here |
| 511 | * won't change, so no need for locking. |
| 512 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 513 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 514 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 515 | /* Subsystem is in this hierarchy. So we want |
| 516 | * the subsystem state from the new |
| 517 | * cgroup */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 518 | template[i] = cgroup_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 519 | } else { |
| 520 | /* Subsystem is not in this hierarchy, so we |
| 521 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 522 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 526 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 527 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 528 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 529 | continue; |
| 530 | |
| 531 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 532 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 533 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 534 | |
| 535 | /* No existing cgroup group matched */ |
| 536 | return NULL; |
| 537 | } |
| 538 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 539 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 540 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 541 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 542 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 543 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 544 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 545 | kfree(link); |
| 546 | } |
| 547 | } |
| 548 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 549 | /** |
| 550 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 551 | * @count: the number of links to allocate |
| 552 | * @tmp_links: list_head the allocated links are put on |
| 553 | * |
| 554 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 555 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 556 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 557 | 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] | 558 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 559 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 560 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 561 | |
| 562 | INIT_LIST_HEAD(tmp_links); |
| 563 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 564 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 565 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 566 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 567 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 568 | return -ENOMEM; |
| 569 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 570 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 571 | } |
| 572 | return 0; |
| 573 | } |
| 574 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 575 | /** |
| 576 | * 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] | 577 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 578 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 579 | * @cgrp: the destination cgroup |
| 580 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 581 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 582 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 583 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 584 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 585 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 586 | BUG_ON(list_empty(tmp_links)); |
| 587 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 588 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 589 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 590 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 591 | /* |
| 592 | * Always add links to the tail of the list so that the list |
| 593 | * is sorted by order of hierarchy creation |
| 594 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 595 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 598 | /** |
| 599 | * find_css_set - return a new css_set with one cgroup updated |
| 600 | * @old_cset: the baseline css_set |
| 601 | * @cgrp: the cgroup to be updated |
| 602 | * |
| 603 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 604 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 605 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 606 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 607 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 608 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 609 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 610 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 611 | struct list_head tmp_links; |
| 612 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 613 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 614 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 615 | lockdep_assert_held(&cgroup_mutex); |
| 616 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 617 | /* First see if we already have a cgroup group that matches |
| 618 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 619 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 620 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 621 | if (cset) |
| 622 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 623 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 624 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 625 | if (cset) |
| 626 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 627 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 628 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 629 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 630 | return NULL; |
| 631 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 632 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 633 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 634 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 635 | return NULL; |
| 636 | } |
| 637 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 638 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 639 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 640 | INIT_LIST_HEAD(&cset->tasks); |
| 641 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 642 | |
| 643 | /* Copy the set of subsystem state objects generated in |
| 644 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 645 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 646 | |
| 647 | write_lock(&css_set_lock); |
| 648 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 649 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 650 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 651 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 652 | if (c->root == cgrp->root) |
| 653 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 654 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 655 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 656 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 657 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 658 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 659 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 660 | |
| 661 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 662 | key = css_set_hash(cset->subsys); |
| 663 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 664 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 665 | write_unlock(&css_set_lock); |
| 666 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 667 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 670 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 671 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 672 | * called with cgroup_mutex held. |
| 673 | */ |
| 674 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 675 | struct cgroupfs_root *root) |
| 676 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 677 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 678 | struct cgroup *res = NULL; |
| 679 | |
| 680 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 681 | read_lock(&css_set_lock); |
| 682 | /* |
| 683 | * No need to lock the task - since we hold cgroup_mutex the |
| 684 | * task can't change groups, so the only thing that can happen |
| 685 | * is that it exits and its css is set back to init_css_set. |
| 686 | */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 687 | cset = task_css_set(task); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 688 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 689 | res = &root->top_cgroup; |
| 690 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 691 | struct cgrp_cset_link *link; |
| 692 | |
| 693 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 694 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 695 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 696 | if (c->root == root) { |
| 697 | res = c; |
| 698 | break; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | read_unlock(&css_set_lock); |
| 703 | BUG_ON(!res); |
| 704 | return res; |
| 705 | } |
| 706 | |
| 707 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 708 | * There is one global cgroup mutex. We also require taking |
| 709 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 710 | * See "The task_lock() exception", at the end of this comment. |
| 711 | * |
| 712 | * A task must hold cgroup_mutex to modify cgroups. |
| 713 | * |
| 714 | * Any task can increment and decrement the count field without lock. |
| 715 | * So in general, code holding cgroup_mutex can't rely on the count |
| 716 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 717 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 718 | * means that no tasks are currently attached, therefore there is no |
| 719 | * way a task attached to that cgroup can fork (the other way to |
| 720 | * increment the count). So code holding cgroup_mutex can safely |
| 721 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 722 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 723 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 724 | * needs that mutex. |
| 725 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 726 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 727 | * (usually) take cgroup_mutex. These are the two most performance |
| 728 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 729 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 730 | * 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] | 731 | * to the release agent with the name of the cgroup (path relative to |
| 732 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 733 | * |
| 734 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 735 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 736 | * tasks in the system use _some_ cgroup, and since there is always at |
| 737 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 738 | * always has either children cgroups and/or using tasks. So we don't |
| 739 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 740 | * |
| 741 | * The task_lock() exception |
| 742 | * |
| 743 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 744 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 745 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 746 | * several performance critical places that need to reference |
| 747 | * task->cgroup without the expense of grabbing a system global |
| 748 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 749 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 750 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 751 | * the task_struct routinely used for such matters. |
| 752 | * |
| 753 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 754 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 755 | */ |
| 756 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 757 | /* |
| 758 | * A couple of forward declarations required, due to cyclic reference loop: |
| 759 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 760 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 761 | * -> cgroup_mkdir. |
| 762 | */ |
| 763 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 764 | 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] | 765 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 766 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 767 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 768 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 769 | |
| 770 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 771 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 772 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 773 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 774 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 775 | 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] | 776 | { |
| 777 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 778 | |
| 779 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 780 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 781 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 782 | inode->i_uid = current_fsuid(); |
| 783 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 784 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 785 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 786 | } |
| 787 | return inode; |
| 788 | } |
| 789 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 790 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 791 | { |
| 792 | struct cgroup_name *name; |
| 793 | |
| 794 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 795 | if (!name) |
| 796 | return NULL; |
| 797 | strcpy(name->name, dentry->d_name.name); |
| 798 | return name; |
| 799 | } |
| 800 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 801 | static void cgroup_free_fn(struct work_struct *work) |
| 802 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 803 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 804 | |
| 805 | mutex_lock(&cgroup_mutex); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 806 | cgrp->root->number_of_cgroups--; |
| 807 | mutex_unlock(&cgroup_mutex); |
| 808 | |
| 809 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 810 | * We get a ref to the parent's dentry, and put the ref when |
| 811 | * this cgroup is being freed, so it's guaranteed that the |
| 812 | * parent won't be destroyed before its children. |
| 813 | */ |
| 814 | dput(cgrp->parent->dentry); |
| 815 | |
| 816 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 817 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 818 | * created the cgroup. This will free cgrp->root, if we are |
| 819 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 820 | */ |
| 821 | deactivate_super(cgrp->root->sb); |
| 822 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 823 | cgroup_pidlist_destroy_all(cgrp); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 824 | |
| 825 | simple_xattrs_free(&cgrp->xattrs); |
| 826 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 827 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 828 | kfree(cgrp); |
| 829 | } |
| 830 | |
| 831 | static void cgroup_free_rcu(struct rcu_head *head) |
| 832 | { |
| 833 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 834 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 835 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 836 | queue_work(cgroup_destroy_wq, &cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 837 | } |
| 838 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 839 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 840 | { |
| 841 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 842 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 843 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 844 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 845 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | c1a7150 | 2013-12-17 11:13:39 +0800 | [diff] [blame] | 846 | |
| 847 | /* |
| 848 | * XXX: cgrp->id is only used to look up css's. As cgroup |
| 849 | * and css's lifetimes will be decoupled, it should be made |
| 850 | * per-subsystem and moved to css->id so that lookups are |
| 851 | * successful until the target css is released. |
| 852 | */ |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 853 | mutex_lock(&cgroup_mutex); |
Li Zefan | c1a7150 | 2013-12-17 11:13:39 +0800 | [diff] [blame] | 854 | idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 855 | mutex_unlock(&cgroup_mutex); |
Li Zefan | c1a7150 | 2013-12-17 11:13:39 +0800 | [diff] [blame] | 856 | cgrp->id = -1; |
| 857 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 858 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 859 | } else { |
| 860 | struct cfent *cfe = __d_cfe(dentry); |
| 861 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 862 | |
| 863 | WARN_ONCE(!list_empty(&cfe->node) && |
| 864 | cgrp != &cgrp->root->top_cgroup, |
| 865 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 866 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 867 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 868 | } |
| 869 | iput(inode); |
| 870 | } |
| 871 | |
| 872 | static void remove_dir(struct dentry *d) |
| 873 | { |
| 874 | struct dentry *parent = dget(d->d_parent); |
| 875 | |
| 876 | d_delete(d); |
| 877 | simple_rmdir(parent->d_inode, d); |
| 878 | dput(parent); |
| 879 | } |
| 880 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 881 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 882 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 883 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 884 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 885 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 886 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 887 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 888 | /* |
| 889 | * If we're doing cleanup due to failure of cgroup_create(), |
| 890 | * the corresponding @cfe may not exist. |
| 891 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 892 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 893 | struct dentry *d = cfe->dentry; |
| 894 | |
| 895 | if (cft && cfe->type != cft) |
| 896 | continue; |
| 897 | |
| 898 | dget(d); |
| 899 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 900 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 901 | list_del_init(&cfe->node); |
| 902 | dput(d); |
| 903 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 904 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 905 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 908 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 909 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 910 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 911 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 912 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 913 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 914 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 915 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 916 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 917 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 918 | for_each_subsys(ss, i) { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 919 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 920 | |
| 921 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 922 | continue; |
| 923 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 924 | cgroup_addrm_files(cgrp, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 925 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | /* |
| 929 | * NOTE : the dentry must have been dget()'ed |
| 930 | */ |
| 931 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 932 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 933 | struct dentry *parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 934 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 935 | parent = dentry->d_parent; |
| 936 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 937 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 938 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 939 | spin_unlock(&dentry->d_lock); |
| 940 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 941 | remove_dir(dentry); |
| 942 | } |
| 943 | |
| 944 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 945 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 946 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 947 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 948 | struct cgroup_subsys *ss; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 949 | int i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 950 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 951 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 952 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 953 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 954 | for_each_subsys(ss, i) |
| 955 | if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root) |
| 956 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 957 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 958 | ret = cgroup_populate_dir(cgrp, added_mask); |
| 959 | if (ret) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 960 | return ret; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 961 | |
| 962 | /* |
| 963 | * Nothing can fail from this point on. Remove files for the |
| 964 | * removed subsystems and rebind each subsystem. |
| 965 | */ |
| 966 | cgroup_clear_dir(cgrp, removed_mask); |
| 967 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 968 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 969 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 970 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 971 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 972 | /* We're binding this subsystem to this hierarchy */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 973 | BUG_ON(cgroup_css(cgrp, ss)); |
| 974 | BUG_ON(!cgroup_css(cgroup_dummy_top, ss)); |
| 975 | BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 976 | |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 977 | rcu_assign_pointer(cgrp->subsys[i], |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 978 | cgroup_css(cgroup_dummy_top, ss)); |
| 979 | cgroup_css(cgrp, ss)->cgroup = cgrp; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 980 | |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 981 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 982 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 983 | ss->bind(cgroup_css(cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 984 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 985 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 986 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 987 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 988 | /* We're removing this subsystem */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 989 | BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss)); |
| 990 | BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 991 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 992 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 993 | ss->bind(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 994 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 995 | cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 996 | RCU_INIT_POINTER(cgrp->subsys[i], NULL); |
| 997 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 998 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 999 | root->subsys_mask &= ~bit; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1000 | } |
| 1001 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1002 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1003 | /* |
| 1004 | * Mark @root has finished binding subsystems. @root->subsys_mask |
| 1005 | * now matches the bound subsystems. |
| 1006 | */ |
| 1007 | root->flags |= CGRP_ROOT_SUBSYS_BOUND; |
| 1008 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
| 1011 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1012 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1013 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1014 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1015 | struct cgroup_subsys *ss; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1016 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1017 | |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1018 | for_each_subsys(ss, ssid) |
| 1019 | if (root->subsys_mask & (1 << ssid)) |
| 1020 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1021 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1022 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1023 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1024 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1025 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1026 | seq_puts(seq, ",xattr"); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1027 | |
| 1028 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1029 | if (strlen(root->release_agent_path)) |
| 1030 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1031 | spin_unlock(&release_agent_path_lock); |
| 1032 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1033 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1034 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1035 | if (strlen(root->name)) |
| 1036 | seq_printf(seq, ",name=%s", root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1037 | return 0; |
| 1038 | } |
| 1039 | |
| 1040 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1041 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1042 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1043 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1044 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1045 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1046 | /* User explicitly requested empty subsystem */ |
| 1047 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1048 | |
| 1049 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1050 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1051 | }; |
| 1052 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1053 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1054 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1055 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1056 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1057 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1058 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1059 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1060 | { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1061 | char *token, *o = data; |
| 1062 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1063 | unsigned long mask = (unsigned long)-1; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1064 | struct cgroup_subsys *ss; |
| 1065 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1066 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1067 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1068 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1069 | #ifdef CONFIG_CPUSETS |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1070 | mask = ~(1UL << cpuset_cgrp_id); |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1071 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1072 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1073 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1074 | |
| 1075 | while ((token = strsep(&o, ",")) != NULL) { |
| 1076 | if (!*token) |
| 1077 | return -EINVAL; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1078 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1079 | /* Explicitly have no subsystems */ |
| 1080 | opts->none = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1081 | continue; |
| 1082 | } |
| 1083 | if (!strcmp(token, "all")) { |
| 1084 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1085 | if (one_ss) |
| 1086 | return -EINVAL; |
| 1087 | all_ss = true; |
| 1088 | continue; |
| 1089 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1090 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1091 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1092 | continue; |
| 1093 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1094 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1095 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1096 | continue; |
| 1097 | } |
| 1098 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1099 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1100 | continue; |
| 1101 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1102 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1103 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1104 | continue; |
| 1105 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1106 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1107 | /* Specifying two release agents is forbidden */ |
| 1108 | if (opts->release_agent) |
| 1109 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1110 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1111 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1112 | if (!opts->release_agent) |
| 1113 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1114 | continue; |
| 1115 | } |
| 1116 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1117 | const char *name = token + 5; |
| 1118 | /* Can't specify an empty name */ |
| 1119 | if (!strlen(name)) |
| 1120 | return -EINVAL; |
| 1121 | /* Must match [\w.-]+ */ |
| 1122 | for (i = 0; i < strlen(name); i++) { |
| 1123 | char c = name[i]; |
| 1124 | if (isalnum(c)) |
| 1125 | continue; |
| 1126 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1127 | continue; |
| 1128 | return -EINVAL; |
| 1129 | } |
| 1130 | /* Specifying two names is forbidden */ |
| 1131 | if (opts->name) |
| 1132 | return -EINVAL; |
| 1133 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1134 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1135 | GFP_KERNEL); |
| 1136 | if (!opts->name) |
| 1137 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1138 | |
| 1139 | continue; |
| 1140 | } |
| 1141 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1142 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1143 | if (strcmp(token, ss->name)) |
| 1144 | continue; |
| 1145 | if (ss->disabled) |
| 1146 | continue; |
| 1147 | |
| 1148 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1149 | if (all_ss) |
| 1150 | return -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1151 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1152 | one_ss = true; |
| 1153 | |
| 1154 | break; |
| 1155 | } |
| 1156 | if (i == CGROUP_SUBSYS_COUNT) |
| 1157 | return -ENOENT; |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1162 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1163 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1164 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1165 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1166 | for_each_subsys(ss, i) |
| 1167 | if (!ss->disabled) |
| 1168 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1169 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1170 | /* Consistency checks */ |
| 1171 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1172 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1173 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1174 | |
| 1175 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1176 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1177 | return -EINVAL; |
| 1178 | } |
| 1179 | |
| 1180 | if (opts->cpuset_clone_children) { |
| 1181 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1182 | return -EINVAL; |
| 1183 | } |
| 1184 | } |
| 1185 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1186 | /* |
| 1187 | * Option noprefix was introduced just for backward compatibility |
| 1188 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1189 | * the cpuset subsystem. |
| 1190 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1191 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1192 | return -EINVAL; |
| 1193 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1194 | |
| 1195 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1196 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1197 | return -EINVAL; |
| 1198 | |
| 1199 | /* |
| 1200 | * We either have to specify by name or by subsystems. (So all |
| 1201 | * empty hierarchies must have a name). |
| 1202 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1203 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1204 | return -EINVAL; |
| 1205 | |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1210 | { |
| 1211 | int ret = 0; |
| 1212 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1213 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1214 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1215 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1216 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1217 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1218 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1219 | return -EINVAL; |
| 1220 | } |
| 1221 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1222 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1223 | mutex_lock(&cgroup_mutex); |
| 1224 | |
| 1225 | /* See what subsystems are wanted */ |
| 1226 | ret = parse_cgroupfs_options(data, &opts); |
| 1227 | if (ret) |
| 1228 | goto out_unlock; |
| 1229 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1230 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1231 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1232 | task_tgid_nr(current), current->comm); |
| 1233 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1234 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1235 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1236 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1237 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1238 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1239 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1240 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1241 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1242 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1243 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1244 | goto out_unlock; |
| 1245 | } |
| 1246 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1247 | /* remounting is not allowed for populated hierarchies */ |
| 1248 | if (root->number_of_cgroups > 1) { |
| 1249 | ret = -EBUSY; |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1250 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1251 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1252 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1253 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1254 | if (ret) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1255 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1256 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1257 | if (opts.release_agent) { |
| 1258 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1259 | strcpy(root->release_agent_path, opts.release_agent); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1260 | spin_unlock(&release_agent_path_lock); |
| 1261 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1262 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1263 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1264 | kfree(opts.name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1265 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1266 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1267 | return ret; |
| 1268 | } |
| 1269 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1270 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1271 | .statfs = simple_statfs, |
| 1272 | .drop_inode = generic_delete_inode, |
| 1273 | .show_options = cgroup_show_options, |
| 1274 | .remount_fs = cgroup_remount, |
| 1275 | }; |
| 1276 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1277 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1278 | { |
| 1279 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1280 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1281 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1282 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1283 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1284 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1285 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1286 | cgrp->dummy_css.cgroup = cgrp; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1287 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1288 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1289 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1290 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1291 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1292 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1293 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1294 | INIT_LIST_HEAD(&root->root_list); |
| 1295 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1296 | cgrp->root = root; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 1297 | RCU_INIT_POINTER(cgrp->name, &root_cgroup_name); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1298 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1299 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1302 | 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] | 1303 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1304 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1305 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1306 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1307 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1308 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 1309 | GFP_KERNEL); |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1310 | if (id < 0) |
| 1311 | return id; |
| 1312 | |
| 1313 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1314 | return 0; |
| 1315 | } |
| 1316 | |
| 1317 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1318 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1319 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1320 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1321 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1322 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1323 | root->hierarchy_id = 0; |
| 1324 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1327 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1328 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1329 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1330 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1331 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1332 | /* If we asked for a name then it must match */ |
| 1333 | if (opts->name && strcmp(opts->name, root->name)) |
| 1334 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1335 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1336 | /* |
| 1337 | * If we asked for subsystems (or explicitly for no |
| 1338 | * subsystems) then they must match |
| 1339 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1340 | if ((opts->subsys_mask || opts->none) |
| 1341 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1342 | return 0; |
| 1343 | |
| 1344 | return 1; |
| 1345 | } |
| 1346 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1347 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1348 | { |
| 1349 | struct cgroupfs_root *root; |
| 1350 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1351 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1352 | return NULL; |
| 1353 | |
| 1354 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1355 | if (!root) |
| 1356 | return ERR_PTR(-ENOMEM); |
| 1357 | |
| 1358 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1359 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1360 | /* |
| 1361 | * We need to set @root->subsys_mask now so that @root can be |
| 1362 | * matched by cgroup_test_super() before it finishes |
| 1363 | * initialization; otherwise, competing mounts with the same |
| 1364 | * options may try to bind the same subsystems instead of waiting |
| 1365 | * for the first one leading to unexpected mount errors. |
| 1366 | * SUBSYS_BOUND will be set once actual binding is complete. |
| 1367 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1368 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1369 | root->flags = opts->flags; |
| 1370 | if (opts->release_agent) |
| 1371 | strcpy(root->release_agent_path, opts->release_agent); |
| 1372 | if (opts->name) |
| 1373 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1374 | if (opts->cpuset_clone_children) |
| 1375 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1376 | return root; |
| 1377 | } |
| 1378 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1379 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1380 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1381 | if (root) { |
| 1382 | /* hierarhcy ID shoulid already have been released */ |
| 1383 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1384 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1385 | idr_destroy(&root->cgroup_idr); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1386 | kfree(root); |
| 1387 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1390 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1391 | { |
| 1392 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1393 | struct cgroup_sb_opts *opts = data; |
| 1394 | |
| 1395 | /* If we don't have a new root, we can't set up a new sb */ |
| 1396 | if (!opts->new_root) |
| 1397 | return -EINVAL; |
| 1398 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1399 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1400 | |
| 1401 | ret = set_anon_super(sb, NULL); |
| 1402 | if (ret) |
| 1403 | return ret; |
| 1404 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1405 | sb->s_fs_info = opts->new_root; |
| 1406 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1407 | |
| 1408 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1409 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1410 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1411 | sb->s_op = &cgroup_ops; |
| 1412 | |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1417 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1418 | static const struct dentry_operations cgroup_dops = { |
| 1419 | .d_iput = cgroup_diput, |
Al Viro | b26d4cd | 2013-10-25 18:47:37 -0400 | [diff] [blame] | 1420 | .d_delete = always_delete_dentry, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1421 | }; |
| 1422 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1423 | struct inode *inode = |
| 1424 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1425 | |
| 1426 | if (!inode) |
| 1427 | return -ENOMEM; |
| 1428 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1429 | inode->i_fop = &simple_dir_operations; |
| 1430 | inode->i_op = &cgroup_dir_inode_operations; |
| 1431 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1432 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1433 | sb->s_root = d_make_root(inode); |
| 1434 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1435 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1436 | /* for everything else we want ->d_op set */ |
| 1437 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1438 | return 0; |
| 1439 | } |
| 1440 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1441 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1442 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1443 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1444 | { |
| 1445 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1446 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1447 | int ret = 0; |
| 1448 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1449 | struct cgroupfs_root *new_root; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1450 | struct list_head tmp_links; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1451 | struct inode *inode; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1452 | const struct cred *cred; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1453 | |
| 1454 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1455 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1456 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1457 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1458 | if (ret) |
| 1459 | goto out_err; |
| 1460 | |
| 1461 | /* |
| 1462 | * Allocate a new cgroup root. We may not need it if we're |
| 1463 | * reusing an existing hierarchy. |
| 1464 | */ |
| 1465 | new_root = cgroup_root_from_opts(&opts); |
| 1466 | if (IS_ERR(new_root)) { |
| 1467 | ret = PTR_ERR(new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1468 | goto out_err; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1469 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1470 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1471 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1472 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1473 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1474 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1475 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1476 | cgroup_free_root(opts.new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1477 | goto out_err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1480 | root = sb->s_fs_info; |
| 1481 | BUG_ON(!root); |
| 1482 | if (root == opts.new_root) { |
| 1483 | /* We used the new root structure, so this is a new hierarchy */ |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1484 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1485 | struct cgroupfs_root *existing_root; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1486 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1487 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1488 | |
| 1489 | BUG_ON(sb->s_root != NULL); |
| 1490 | |
| 1491 | ret = cgroup_get_rootdir(sb); |
| 1492 | if (ret) |
| 1493 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1494 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1495 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1496 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1497 | mutex_lock(&cgroup_mutex); |
| 1498 | |
Tejun Heo | eb46bf8 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 1499 | ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL); |
| 1500 | if (ret < 0) |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1501 | goto unlock_drop; |
Tejun Heo | eb46bf8 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 1502 | root_cgrp->id = ret; |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1503 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1504 | /* Check for name clashes with existing mounts */ |
| 1505 | ret = -EBUSY; |
| 1506 | if (strlen(root->name)) |
| 1507 | for_each_active_root(existing_root) |
| 1508 | if (!strcmp(existing_root->name, root->name)) |
| 1509 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1510 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1511 | /* |
| 1512 | * We're accessing css_set_count without locking |
| 1513 | * css_set_lock here, but that's OK - it can only be |
| 1514 | * increased by someone holding cgroup_lock, and |
| 1515 | * that's us. The worst that can happen is that we |
| 1516 | * have some link structures left over |
| 1517 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1518 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1519 | if (ret) |
| 1520 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1521 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1522 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1523 | ret = cgroup_init_root_id(root, 2, 0); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1524 | if (ret) |
| 1525 | goto unlock_drop; |
| 1526 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1527 | sb->s_root->d_fsdata = root_cgrp; |
| 1528 | root_cgrp->dentry = sb->s_root; |
| 1529 | |
| 1530 | /* |
| 1531 | * We're inside get_sb() and will call lookup_one_len() to |
| 1532 | * create the root files, which doesn't work if SELinux is |
| 1533 | * in use. The following cred dancing somehow works around |
| 1534 | * it. See 2ce9738ba ("cgroupfs: use init_cred when |
| 1535 | * populating new cgroupfs mount") for more details. |
| 1536 | */ |
| 1537 | cred = override_creds(&init_cred); |
| 1538 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1539 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1540 | if (ret) |
| 1541 | goto rm_base_files; |
| 1542 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1543 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1544 | if (ret) |
| 1545 | goto rm_base_files; |
| 1546 | |
| 1547 | revert_creds(cred); |
| 1548 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1549 | /* |
| 1550 | * There must be no failure case after here, since rebinding |
| 1551 | * takes care of subsystems' refcounts, which are explicitly |
| 1552 | * dropped in the failure exit path. |
| 1553 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1554 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1555 | list_add(&root->root_list, &cgroup_roots); |
| 1556 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1557 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1558 | /* Link the top cgroup in this hierarchy into all |
| 1559 | * the css_set objects */ |
| 1560 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1561 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1562 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1563 | write_unlock(&css_set_lock); |
| 1564 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1565 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1566 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1567 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1568 | BUG_ON(root->number_of_cgroups != 1); |
| 1569 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1570 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1571 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1572 | } else { |
| 1573 | /* |
| 1574 | * We re-used an existing hierarchy - the new root (if |
| 1575 | * any) is not needed |
| 1576 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1577 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1578 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1579 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1580 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1581 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1582 | ret = -EINVAL; |
| 1583 | goto drop_new_super; |
| 1584 | } else { |
| 1585 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1586 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1587 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1590 | kfree(opts.release_agent); |
| 1591 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1592 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1593 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1594 | rm_base_files: |
| 1595 | free_cgrp_cset_links(&tmp_links); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1596 | cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1597 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1598 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1599 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1600 | mutex_unlock(&cgroup_mutex); |
| 1601 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1602 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1603 | deactivate_locked_super(sb); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1604 | out_err: |
| 1605 | kfree(opts.release_agent); |
| 1606 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1607 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1608 | } |
| 1609 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 1610 | static void cgroup_kill_sb(struct super_block *sb) |
| 1611 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1612 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1613 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1614 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1615 | int ret; |
| 1616 | |
| 1617 | BUG_ON(!root); |
| 1618 | |
| 1619 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1620 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1621 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1622 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1623 | mutex_lock(&cgroup_mutex); |
| 1624 | |
| 1625 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1626 | if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { |
| 1627 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
| 1628 | /* Shouldn't be able to fail ... */ |
| 1629 | BUG_ON(ret); |
| 1630 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1631 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1632 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1633 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1634 | * root cgroup |
| 1635 | */ |
| 1636 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1637 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1638 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1639 | list_del(&link->cset_link); |
| 1640 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1641 | kfree(link); |
| 1642 | } |
| 1643 | write_unlock(&css_set_lock); |
| 1644 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1645 | if (!list_empty(&root->root_list)) { |
| 1646 | list_del(&root->root_list); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1647 | cgroup_root_count--; |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1648 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1649 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1650 | cgroup_exit_root_id(root); |
| 1651 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1652 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1653 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1654 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1655 | simple_xattrs_free(&cgrp->xattrs); |
| 1656 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1657 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1658 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | static struct file_system_type cgroup_fs_type = { |
| 1662 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1663 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1664 | .kill_sb = cgroup_kill_sb, |
| 1665 | }; |
| 1666 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1667 | static struct kobject *cgroup_kobj; |
| 1668 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1669 | /** |
| 1670 | * cgroup_path - generate the path of a cgroup |
| 1671 | * @cgrp: the cgroup in question |
| 1672 | * @buf: the buffer to write the path into |
| 1673 | * @buflen: the length of the buffer |
| 1674 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1675 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1676 | * |
| 1677 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1678 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1679 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1680 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1681 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1682 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1683 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1684 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1685 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1686 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1687 | if (!cgrp->parent) { |
| 1688 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1689 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1690 | return 0; |
| 1691 | } |
| 1692 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1693 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1694 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1695 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1696 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1697 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1698 | const char *name = cgroup_name(cgrp); |
| 1699 | int len; |
| 1700 | |
| 1701 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1702 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1703 | goto out; |
| 1704 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1705 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1706 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1707 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1708 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1709 | |
| 1710 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1711 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1712 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1713 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1714 | out: |
| 1715 | rcu_read_unlock(); |
| 1716 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1717 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1718 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1719 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1720 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1721 | * 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] | 1722 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1723 | * @buf: the buffer to write the path into |
| 1724 | * @buflen: the length of the buffer |
| 1725 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1726 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1727 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1728 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1729 | * cgroup controller callbacks. |
| 1730 | * |
| 1731 | * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short. |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1732 | */ |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1733 | 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] | 1734 | { |
| 1735 | struct cgroupfs_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1736 | struct cgroup *cgrp; |
| 1737 | int hierarchy_id = 1, ret = 0; |
| 1738 | |
| 1739 | if (buflen < 2) |
| 1740 | return -ENAMETOOLONG; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1741 | |
| 1742 | mutex_lock(&cgroup_mutex); |
| 1743 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1744 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1745 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1746 | if (root) { |
| 1747 | cgrp = task_cgroup_from_root(task, root); |
| 1748 | ret = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1749 | } else { |
| 1750 | /* if no hierarchy exists, everyone is in "/" */ |
| 1751 | memcpy(buf, "/", 2); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1755 | return ret; |
| 1756 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1757 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1758 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1759 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1760 | * Control Group taskset |
| 1761 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1762 | struct task_and_cgroup { |
| 1763 | struct task_struct *task; |
| 1764 | struct cgroup *cgrp; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1765 | struct css_set *cset; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1766 | }; |
| 1767 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1768 | struct cgroup_taskset { |
| 1769 | struct task_and_cgroup single; |
| 1770 | struct flex_array *tc_array; |
| 1771 | int tc_array_len; |
| 1772 | int idx; |
| 1773 | struct cgroup *cur_cgrp; |
| 1774 | }; |
| 1775 | |
| 1776 | /** |
| 1777 | * cgroup_taskset_first - reset taskset and return the first task |
| 1778 | * @tset: taskset of interest |
| 1779 | * |
| 1780 | * @tset iteration is initialized and the first task is returned. |
| 1781 | */ |
| 1782 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1783 | { |
| 1784 | if (tset->tc_array) { |
| 1785 | tset->idx = 0; |
| 1786 | return cgroup_taskset_next(tset); |
| 1787 | } else { |
| 1788 | tset->cur_cgrp = tset->single.cgrp; |
| 1789 | return tset->single.task; |
| 1790 | } |
| 1791 | } |
| 1792 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1793 | |
| 1794 | /** |
| 1795 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1796 | * @tset: taskset of interest |
| 1797 | * |
| 1798 | * Return the next task in @tset. Iteration must have been initialized |
| 1799 | * with cgroup_taskset_first(). |
| 1800 | */ |
| 1801 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1802 | { |
| 1803 | struct task_and_cgroup *tc; |
| 1804 | |
| 1805 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1806 | return NULL; |
| 1807 | |
| 1808 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1809 | tset->cur_cgrp = tc->cgrp; |
| 1810 | return tc->task; |
| 1811 | } |
| 1812 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1813 | |
| 1814 | /** |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1815 | * cgroup_taskset_cur_css - return the matching css for the current task |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1816 | * @tset: taskset of interest |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1817 | * @subsys_id: the ID of the target subsystem |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1818 | * |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1819 | * Return the css for the current (last returned) task of @tset for |
| 1820 | * subsystem specified by @subsys_id. This function must be preceded by |
| 1821 | * either cgroup_taskset_first() or cgroup_taskset_next(). |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1822 | */ |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1823 | struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset, |
| 1824 | int subsys_id) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1825 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1826 | return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1827 | } |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1828 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1829 | |
| 1830 | /** |
| 1831 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1832 | * @tset: taskset of interest |
| 1833 | */ |
| 1834 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1835 | { |
| 1836 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1837 | } |
| 1838 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1839 | |
| 1840 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1841 | /* |
| 1842 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1843 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1844 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1845 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1846 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1847 | struct task_struct *tsk, |
| 1848 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1849 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1850 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1851 | |
| 1852 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1853 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1854 | * setting such that we can't race against cgroup_exit() changing the |
| 1855 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1856 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1857 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1858 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1859 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1860 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1861 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1862 | task_unlock(tsk); |
| 1863 | |
| 1864 | /* Update the css_set linked lists if we're using them */ |
| 1865 | write_lock(&css_set_lock); |
| 1866 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1867 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1868 | write_unlock(&css_set_lock); |
| 1869 | |
| 1870 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1871 | * We just gained a reference on old_cset by taking it from the |
| 1872 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1873 | * 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] | 1874 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1875 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1876 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1879 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1880 | * 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] | 1881 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1882 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1883 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1884 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1885 | * 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] | 1886 | * 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] | 1887 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1888 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1889 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1890 | { |
| 1891 | int retval, i, group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1892 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1893 | struct cgroup_subsys_state *css, *failed_css = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1894 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1895 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1896 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1897 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1898 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1899 | |
| 1900 | /* |
| 1901 | * step 0: in order to do expensive, possibly blocking operations for |
| 1902 | * every thread, we cannot iterate the thread group list, since it needs |
| 1903 | * 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] | 1904 | * group - group_rwsem prevents new threads from appearing, and if |
| 1905 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1906 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1907 | if (threadgroup) |
| 1908 | group_size = get_nr_threads(tsk); |
| 1909 | else |
| 1910 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1911 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1912 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1913 | if (!group) |
| 1914 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1915 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 1916 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1917 | if (retval) |
| 1918 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1919 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1920 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1921 | /* |
| 1922 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 1923 | * already PF_EXITING could be freed from underneath us unless we |
| 1924 | * take an rcu_read_lock. |
| 1925 | */ |
| 1926 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1927 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1928 | struct task_and_cgroup ent; |
| 1929 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1930 | /* @tsk either already exited or can't exit until the end */ |
| 1931 | if (tsk->flags & PF_EXITING) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1932 | goto next; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1933 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1934 | /* as per above, nr_threads may decrease, but not increase. */ |
| 1935 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1936 | ent.task = tsk; |
| 1937 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 1938 | /* nothing to do if this task is already in the cgroup */ |
| 1939 | if (ent.cgrp == cgrp) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1940 | goto next; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1941 | /* |
| 1942 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 1943 | * earlier, but it's good form to communicate our expectations. |
| 1944 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1945 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1946 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1947 | i++; |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1948 | next: |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1949 | if (!threadgroup) |
| 1950 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1951 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1952 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1953 | /* remember the number of threads in the array for later. */ |
| 1954 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1955 | tset.tc_array = group; |
| 1956 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1957 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1958 | /* methods shouldn't be called if no task is actually migrating */ |
| 1959 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 1960 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 1961 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1962 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1963 | /* |
| 1964 | * step 1: check that we can legitimately attach to the cgroup. |
| 1965 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1966 | for_each_css(css, i, cgrp) { |
| 1967 | if (css->ss->can_attach) { |
| 1968 | retval = css->ss->can_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1969 | if (retval) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1970 | failed_css = css; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1971 | goto out_cancel_attach; |
| 1972 | } |
| 1973 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | /* |
| 1977 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 1978 | * we use find_css_set, which allocates a new one if necessary. |
| 1979 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1980 | for (i = 0; i < group_size; i++) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1981 | struct css_set *old_cset; |
| 1982 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1983 | tc = flex_array_get(group, i); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1984 | old_cset = task_css_set(tc->task); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1985 | tc->cset = find_css_set(old_cset, cgrp); |
| 1986 | if (!tc->cset) { |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1987 | retval = -ENOMEM; |
| 1988 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 1993 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 1994 | * proceed to move all tasks to the new cgroup. There are no |
| 1995 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1996 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1997 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1998 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1999 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2000 | } |
| 2001 | /* nothing is sensitive to fork() after this point. */ |
| 2002 | |
| 2003 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2004 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2005 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2006 | for_each_css(css, i, cgrp) |
| 2007 | if (css->ss->attach) |
| 2008 | css->ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2009 | |
| 2010 | /* |
| 2011 | * step 5: success! and cleanup |
| 2012 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2013 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2014 | out_put_css_set_refs: |
| 2015 | if (retval) { |
| 2016 | for (i = 0; i < group_size; i++) { |
| 2017 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2018 | if (!tc->cset) |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2019 | break; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2020 | put_css_set(tc->cset); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2021 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2022 | } |
| 2023 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2024 | if (retval) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2025 | for_each_css(css, i, cgrp) { |
| 2026 | if (css == failed_css) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2027 | break; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2028 | if (css->ss->cancel_attach) |
| 2029 | css->ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2030 | } |
| 2031 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2032 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2033 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2034 | return retval; |
| 2035 | } |
| 2036 | |
| 2037 | /* |
| 2038 | * 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] | 2039 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2040 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2041 | */ |
| 2042 | 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] | 2043 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2044 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2045 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2046 | int ret; |
| 2047 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2048 | if (!cgroup_lock_live_group(cgrp)) |
| 2049 | return -ENODEV; |
| 2050 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2051 | retry_find_task: |
| 2052 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2053 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2054 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2055 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2056 | rcu_read_unlock(); |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2057 | ret = -ESRCH; |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2058 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2059 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2060 | /* |
| 2061 | * even if we're attaching all tasks in the thread group, we |
| 2062 | * only need to check permissions on one of them. |
| 2063 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2064 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2065 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2066 | !uid_eq(cred->euid, tcred->uid) && |
| 2067 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2068 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2069 | ret = -EACCES; |
| 2070 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2071 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2072 | } else |
| 2073 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2074 | |
| 2075 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2076 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2077 | |
| 2078 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2079 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2080 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2081 | * with no rt_runtime allocated. Just say no. |
| 2082 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2083 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2084 | ret = -EINVAL; |
| 2085 | rcu_read_unlock(); |
| 2086 | goto out_unlock_cgroup; |
| 2087 | } |
| 2088 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2089 | get_task_struct(tsk); |
| 2090 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2091 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2092 | threadgroup_lock(tsk); |
| 2093 | if (threadgroup) { |
| 2094 | if (!thread_group_leader(tsk)) { |
| 2095 | /* |
| 2096 | * a race with de_thread from another thread's exec() |
| 2097 | * may strip us of our leadership, if this happens, |
| 2098 | * there is no choice but to throw this task away and |
| 2099 | * try again; this is |
| 2100 | * "double-double-toil-and-trouble-check locking". |
| 2101 | */ |
| 2102 | threadgroup_unlock(tsk); |
| 2103 | put_task_struct(tsk); |
| 2104 | goto retry_find_task; |
| 2105 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2109 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2110 | threadgroup_unlock(tsk); |
| 2111 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2112 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2113 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2114 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2115 | return ret; |
| 2116 | } |
| 2117 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2118 | /** |
| 2119 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2120 | * @from: attach to all cgroups of a given task |
| 2121 | * @tsk: the task to be attached |
| 2122 | */ |
| 2123 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2124 | { |
| 2125 | struct cgroupfs_root *root; |
| 2126 | int retval = 0; |
| 2127 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2128 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2129 | for_each_active_root(root) { |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2130 | struct cgroup *from_cgrp = task_cgroup_from_root(from, root); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2131 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2132 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2133 | if (retval) |
| 2134 | break; |
| 2135 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2136 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2137 | |
| 2138 | return retval; |
| 2139 | } |
| 2140 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2141 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2142 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2143 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2144 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2145 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2148 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2149 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2150 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2151 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2154 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
| 2155 | struct cftype *cft, const char *buffer) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2156 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2157 | BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2158 | if (strlen(buffer) >= PATH_MAX) |
| 2159 | return -EINVAL; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2160 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2161 | return -ENODEV; |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2162 | spin_lock(&release_agent_path_lock); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2163 | strcpy(css->cgroup->root->release_agent_path, buffer); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2164 | spin_unlock(&release_agent_path_lock); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2165 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2166 | return 0; |
| 2167 | } |
| 2168 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2169 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2170 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2171 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2172 | |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2173 | if (!cgroup_lock_live_group(cgrp)) |
| 2174 | return -ENODEV; |
| 2175 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2176 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2177 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2178 | return 0; |
| 2179 | } |
| 2180 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2181 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2182 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2183 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2184 | |
| 2185 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2186 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2187 | } |
| 2188 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2189 | /* A buffer size big enough for numbers or short strings */ |
| 2190 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2191 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2192 | 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] | 2193 | size_t nbytes, loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2194 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2195 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2196 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2197 | struct cgroup_subsys_state *css = cfe->css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2198 | size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1; |
| 2199 | char *buf; |
| 2200 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2201 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2202 | if (nbytes >= max_bytes) |
| 2203 | return -E2BIG; |
| 2204 | |
| 2205 | buf = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2206 | if (!buf) |
| 2207 | return -ENOMEM; |
| 2208 | |
| 2209 | if (copy_from_user(buf, userbuf, nbytes)) { |
| 2210 | ret = -EFAULT; |
| 2211 | goto out_free; |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2212 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2213 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2214 | buf[nbytes] = '\0'; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2215 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2216 | if (cft->write_string) { |
| 2217 | ret = cft->write_string(css, cft, strstrip(buf)); |
| 2218 | } else if (cft->write_u64) { |
| 2219 | unsigned long long v; |
| 2220 | ret = kstrtoull(buf, 0, &v); |
| 2221 | if (!ret) |
| 2222 | ret = cft->write_u64(css, cft, v); |
| 2223 | } else if (cft->write_s64) { |
| 2224 | long long v; |
| 2225 | ret = kstrtoll(buf, 0, &v); |
| 2226 | if (!ret) |
| 2227 | ret = cft->write_s64(css, cft, v); |
| 2228 | } else if (cft->trigger) { |
| 2229 | ret = cft->trigger(css, (unsigned int)cft->private); |
| 2230 | } else { |
| 2231 | ret = -EINVAL; |
| 2232 | } |
| 2233 | out_free: |
| 2234 | kfree(buf); |
| 2235 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2236 | } |
| 2237 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2238 | /* |
| 2239 | * seqfile ops/methods for returning structured data. Currently just |
| 2240 | * supports string->u64 maps, but can be extended in future. |
| 2241 | */ |
| 2242 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2243 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2244 | { |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2245 | struct cftype *cft = seq_cft(seq); |
| 2246 | |
| 2247 | if (cft->seq_start) { |
| 2248 | return cft->seq_start(seq, ppos); |
| 2249 | } else { |
| 2250 | /* |
| 2251 | * The same behavior and code as single_open(). Returns |
| 2252 | * !NULL if pos is at the beginning; otherwise, NULL. |
| 2253 | */ |
| 2254 | return NULL + !*ppos; |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 2259 | { |
| 2260 | struct cftype *cft = seq_cft(seq); |
| 2261 | |
| 2262 | if (cft->seq_next) { |
| 2263 | return cft->seq_next(seq, v, ppos); |
| 2264 | } else { |
| 2265 | /* |
| 2266 | * The same behavior and code as single_open(), always |
| 2267 | * terminate after the initial read. |
| 2268 | */ |
| 2269 | ++*ppos; |
| 2270 | return NULL; |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 2275 | { |
| 2276 | struct cftype *cft = seq_cft(seq); |
| 2277 | |
| 2278 | if (cft->seq_stop) |
| 2279 | cft->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2283 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2284 | struct cftype *cft = seq_cft(m); |
| 2285 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2286 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2287 | if (cft->seq_show) |
| 2288 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2289 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2290 | if (cft->read_u64) |
| 2291 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 2292 | else if (cft->read_s64) |
| 2293 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 2294 | else |
| 2295 | return -EINVAL; |
| 2296 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2297 | } |
| 2298 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2299 | static struct seq_operations cgroup_seq_operations = { |
| 2300 | .start = cgroup_seqfile_start, |
| 2301 | .next = cgroup_seqfile_next, |
| 2302 | .stop = cgroup_seqfile_stop, |
| 2303 | .show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2304 | }; |
| 2305 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2306 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2307 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2308 | struct cfent *cfe = __d_cfe(file->f_dentry); |
| 2309 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2310 | struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent); |
| 2311 | struct cgroup_subsys_state *css; |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2312 | struct cgroup_open_file *of; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2313 | int err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2314 | |
| 2315 | err = generic_file_open(inode, file); |
| 2316 | if (err) |
| 2317 | return err; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2318 | |
| 2319 | /* |
| 2320 | * If the file belongs to a subsystem, pin the css. Will be |
| 2321 | * unpinned either on open failure or release. This ensures that |
| 2322 | * @css stays alive for all file operations. |
| 2323 | */ |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2324 | rcu_read_lock(); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2325 | css = cgroup_css(cgrp, cft->ss); |
| 2326 | if (cft->ss && !css_tryget(css)) |
| 2327 | css = NULL; |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2328 | rcu_read_unlock(); |
| 2329 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2330 | if (!css) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2331 | return -ENODEV; |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2332 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2333 | /* |
| 2334 | * @cfe->css is used by read/write/close to determine the |
| 2335 | * associated css. @file->private_data would be a better place but |
| 2336 | * that's already used by seqfile. Multiple accessors may use it |
| 2337 | * simultaneously which is okay as the association never changes. |
| 2338 | */ |
| 2339 | WARN_ON_ONCE(cfe->css && cfe->css != css); |
| 2340 | cfe->css = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2341 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2342 | of = __seq_open_private(file, &cgroup_seq_operations, |
| 2343 | sizeof(struct cgroup_open_file)); |
| 2344 | if (of) { |
| 2345 | of->cfe = cfe; |
| 2346 | return 0; |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2347 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2348 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2349 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2350 | css_put(css); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2351 | return -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2355 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2356 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2357 | struct cgroup_subsys_state *css = cfe->css; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2358 | |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2359 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2360 | css_put(css); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2361 | return seq_release_private(inode, file); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | /* |
| 2365 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2366 | */ |
| 2367 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2368 | struct inode *new_dir, struct dentry *new_dentry) |
| 2369 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2370 | int ret; |
| 2371 | struct cgroup_name *name, *old_name; |
| 2372 | struct cgroup *cgrp; |
| 2373 | |
| 2374 | /* |
| 2375 | * It's convinient to use parent dir's i_mutex to protected |
| 2376 | * cgrp->name. |
| 2377 | */ |
| 2378 | lockdep_assert_held(&old_dir->i_mutex); |
| 2379 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2380 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2381 | return -ENOTDIR; |
| 2382 | if (new_dentry->d_inode) |
| 2383 | return -EEXIST; |
| 2384 | if (old_dir != new_dir) |
| 2385 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2386 | |
| 2387 | cgrp = __d_cgrp(old_dentry); |
| 2388 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2389 | /* |
| 2390 | * This isn't a proper migration and its usefulness is very |
| 2391 | * limited. Disallow if sane_behavior. |
| 2392 | */ |
| 2393 | if (cgroup_sane_behavior(cgrp)) |
| 2394 | return -EPERM; |
| 2395 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2396 | name = cgroup_alloc_name(new_dentry); |
| 2397 | if (!name) |
| 2398 | return -ENOMEM; |
| 2399 | |
| 2400 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2401 | if (ret) { |
| 2402 | kfree(name); |
| 2403 | return ret; |
| 2404 | } |
| 2405 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 2406 | old_name = rcu_dereference_protected(cgrp->name, true); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2407 | rcu_assign_pointer(cgrp->name, name); |
| 2408 | |
| 2409 | kfree_rcu(old_name, rcu_head); |
| 2410 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2411 | } |
| 2412 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2413 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2414 | { |
| 2415 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2416 | return &__d_cgrp(dentry)->xattrs; |
| 2417 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2418 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | static inline int xattr_enabled(struct dentry *dentry) |
| 2422 | { |
| 2423 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2424 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | static bool is_valid_xattr(const char *name) |
| 2428 | { |
| 2429 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2430 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2431 | return true; |
| 2432 | return false; |
| 2433 | } |
| 2434 | |
| 2435 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2436 | const void *val, size_t size, int flags) |
| 2437 | { |
| 2438 | if (!xattr_enabled(dentry)) |
| 2439 | return -EOPNOTSUPP; |
| 2440 | if (!is_valid_xattr(name)) |
| 2441 | return -EINVAL; |
| 2442 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2443 | } |
| 2444 | |
| 2445 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2446 | { |
| 2447 | if (!xattr_enabled(dentry)) |
| 2448 | return -EOPNOTSUPP; |
| 2449 | if (!is_valid_xattr(name)) |
| 2450 | return -EINVAL; |
| 2451 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2452 | } |
| 2453 | |
| 2454 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2455 | void *buf, size_t size) |
| 2456 | { |
| 2457 | if (!xattr_enabled(dentry)) |
| 2458 | return -EOPNOTSUPP; |
| 2459 | if (!is_valid_xattr(name)) |
| 2460 | return -EINVAL; |
| 2461 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2462 | } |
| 2463 | |
| 2464 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2465 | { |
| 2466 | if (!xattr_enabled(dentry)) |
| 2467 | return -EOPNOTSUPP; |
| 2468 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2469 | } |
| 2470 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2471 | static const struct file_operations cgroup_file_operations = { |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2472 | .read = seq_read, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2473 | .write = cgroup_file_write, |
| 2474 | .llseek = generic_file_llseek, |
| 2475 | .open = cgroup_file_open, |
| 2476 | .release = cgroup_file_release, |
| 2477 | }; |
| 2478 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2479 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2480 | .setxattr = cgroup_setxattr, |
| 2481 | .getxattr = cgroup_getxattr, |
| 2482 | .listxattr = cgroup_listxattr, |
| 2483 | .removexattr = cgroup_removexattr, |
| 2484 | }; |
| 2485 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2486 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | 786e144 | 2013-07-14 17:50:23 +0400 | [diff] [blame] | 2487 | .lookup = simple_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2488 | .mkdir = cgroup_mkdir, |
| 2489 | .rmdir = cgroup_rmdir, |
| 2490 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2491 | .setxattr = cgroup_setxattr, |
| 2492 | .getxattr = cgroup_getxattr, |
| 2493 | .listxattr = cgroup_listxattr, |
| 2494 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2495 | }; |
| 2496 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2497 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2498 | struct super_block *sb) |
| 2499 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2500 | struct inode *inode; |
| 2501 | |
| 2502 | if (!dentry) |
| 2503 | return -ENOENT; |
| 2504 | if (dentry->d_inode) |
| 2505 | return -EEXIST; |
| 2506 | |
| 2507 | inode = cgroup_new_inode(mode, sb); |
| 2508 | if (!inode) |
| 2509 | return -ENOMEM; |
| 2510 | |
| 2511 | if (S_ISDIR(mode)) { |
| 2512 | inode->i_op = &cgroup_dir_inode_operations; |
| 2513 | inode->i_fop = &simple_dir_operations; |
| 2514 | |
| 2515 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2516 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2517 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2518 | |
Tejun Heo | b8a2df6 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2519 | /* |
| 2520 | * Control reaches here with cgroup_mutex held. |
| 2521 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2522 | * want to populate it immediately without releasing |
| 2523 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2524 | * yet, trylock will always succeed without affecting |
| 2525 | * lockdep checks. |
| 2526 | */ |
| 2527 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2528 | } else if (S_ISREG(mode)) { |
| 2529 | inode->i_size = 0; |
| 2530 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2531 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2532 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2533 | d_instantiate(dentry, inode); |
| 2534 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2535 | return 0; |
| 2536 | } |
| 2537 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2538 | /** |
| 2539 | * cgroup_file_mode - deduce file mode of a control file |
| 2540 | * @cft: the control file in question |
| 2541 | * |
| 2542 | * returns cft->mode if ->mode is not 0 |
| 2543 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2544 | * returns S_IRUGO if it has only a read handler |
| 2545 | * returns S_IWUSR if it has only a write hander |
| 2546 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2547 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2548 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2549 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2550 | |
| 2551 | if (cft->mode) |
| 2552 | return cft->mode; |
| 2553 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2554 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2555 | mode |= S_IRUGO; |
| 2556 | |
Tejun Heo | 6e0755b | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2557 | if (cft->write_u64 || cft->write_s64 || cft->write_string || |
| 2558 | cft->trigger) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2559 | mode |= S_IWUSR; |
| 2560 | |
| 2561 | return mode; |
| 2562 | } |
| 2563 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2564 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2565 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2566 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2567 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2568 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2569 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2570 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2571 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2572 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2573 | |
Tejun Heo | 9fa4db3 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2574 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 2575 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2576 | strcpy(name, cft->ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2577 | strcat(name, "."); |
| 2578 | } |
| 2579 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2580 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2581 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2582 | |
| 2583 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2584 | if (!cfe) |
| 2585 | return -ENOMEM; |
| 2586 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2587 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2588 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2589 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2590 | goto out; |
| 2591 | } |
| 2592 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2593 | cfe->type = (void *)cft; |
| 2594 | cfe->dentry = dentry; |
| 2595 | dentry->d_fsdata = cfe; |
| 2596 | simple_xattrs_init(&cfe->xattrs); |
| 2597 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2598 | mode = cgroup_file_mode(cft); |
| 2599 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2600 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2601 | list_add_tail(&cfe->node, &parent->files); |
| 2602 | cfe = NULL; |
| 2603 | } |
| 2604 | dput(dentry); |
| 2605 | out: |
| 2606 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2607 | return error; |
| 2608 | } |
| 2609 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2610 | /** |
| 2611 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2612 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2613 | * @cfts: array of cftypes to be added |
| 2614 | * @is_add: whether to add or remove |
| 2615 | * |
| 2616 | * 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] | 2617 | * For removals, this function never fails. If addition fails, this |
| 2618 | * function doesn't remove files already added. The caller is responsible |
| 2619 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2620 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2621 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2622 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2623 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2624 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2625 | int ret; |
| 2626 | |
| 2627 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 2628 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2629 | |
| 2630 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2631 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2632 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2633 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2634 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2635 | continue; |
| 2636 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2637 | continue; |
| 2638 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2639 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2640 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2641 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2642 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2643 | cft->name, ret); |
| 2644 | return ret; |
| 2645 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2646 | } else { |
| 2647 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2648 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2649 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2650 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2651 | } |
| 2652 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2653 | static void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2654 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2655 | { |
| 2656 | /* |
| 2657 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2658 | * the existing cgroups under cgroup_mutex and create files. |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2659 | * Instead, we use css_for_each_descendant_pre() and drop RCU read |
| 2660 | * lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2661 | */ |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2662 | mutex_lock(&cgroup_mutex); |
| 2663 | } |
| 2664 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2665 | static int cgroup_cfts_commit(struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2666 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2667 | { |
| 2668 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2669 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2670 | struct cgroup *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2671 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2672 | struct dentry *prev = NULL; |
| 2673 | struct inode *inode; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2674 | struct cgroup_subsys_state *css; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2675 | u64 update_before; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2676 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2677 | |
| 2678 | /* %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] | 2679 | if (!cfts || ss->root == &cgroup_dummy_root || |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2680 | !atomic_inc_not_zero(&sb->s_active)) { |
| 2681 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2682 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2685 | /* |
| 2686 | * All cgroups which are created after we drop cgroup_mutex will |
| 2687 | * 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] | 2688 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2689 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2690 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2691 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2692 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2693 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2694 | struct cgroup *cgrp = css->cgroup; |
| 2695 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2696 | if (cgroup_is_dead(cgrp)) |
| 2697 | continue; |
| 2698 | |
| 2699 | inode = cgrp->dentry->d_inode; |
| 2700 | dget(cgrp->dentry); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2701 | dput(prev); |
| 2702 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2703 | |
Tejun Heo | 48573a8 | 2014-02-08 10:26:34 -0500 | [diff] [blame] | 2704 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2705 | mutex_lock(&inode->i_mutex); |
| 2706 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2707 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2708 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2709 | mutex_unlock(&inode->i_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2710 | if (ret) |
| 2711 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2712 | } |
Tejun Heo | 48573a8 | 2014-02-08 10:26:34 -0500 | [diff] [blame] | 2713 | mutex_unlock(&cgroup_mutex); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2714 | dput(prev); |
| 2715 | deactivate_super(sb); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2716 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
| 2719 | /** |
| 2720 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2721 | * @ss: target cgroup subsystem |
| 2722 | * @cfts: zero-length name terminated array of cftypes |
| 2723 | * |
| 2724 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2725 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2726 | * have them too. This function can be called anytime whether @ss is |
| 2727 | * attached or not. |
| 2728 | * |
| 2729 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2730 | * function currently returns 0 as long as @cfts registration is successful |
| 2731 | * even if some file creation attempts on existing cgroups fail. |
| 2732 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2733 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2734 | { |
| 2735 | struct cftype_set *set; |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2736 | struct cftype *cft; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2737 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2738 | |
| 2739 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2740 | if (!set) |
| 2741 | return -ENOMEM; |
| 2742 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2743 | for (cft = cfts; cft->name[0] != '\0'; cft++) |
| 2744 | cft->ss = ss; |
| 2745 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2746 | cgroup_cfts_prepare(); |
| 2747 | set->cfts = cfts; |
| 2748 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2749 | ret = cgroup_cfts_commit(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2750 | if (ret) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2751 | cgroup_rm_cftypes(cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2752 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2753 | } |
| 2754 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2755 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2756 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2757 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2758 | * @cfts: zero-length name terminated array of cftypes |
| 2759 | * |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2760 | * Unregister @cfts. Files described by @cfts are removed from all |
| 2761 | * existing cgroups and all future cgroups won't have them either. This |
| 2762 | * function can be called anytime whether @cfts' subsys is attached or not. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2763 | * |
| 2764 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2765 | * registered. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2766 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2767 | int cgroup_rm_cftypes(struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2768 | { |
| 2769 | struct cftype_set *set; |
| 2770 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2771 | if (!cfts || !cfts[0].ss) |
| 2772 | return -ENOENT; |
| 2773 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2774 | cgroup_cfts_prepare(); |
| 2775 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2776 | list_for_each_entry(set, &cfts[0].ss->cftsets, node) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2777 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2778 | list_del(&set->node); |
| 2779 | kfree(set); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2780 | cgroup_cfts_commit(cfts, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2781 | return 0; |
| 2782 | } |
| 2783 | } |
| 2784 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2785 | cgroup_cfts_commit(NULL, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2786 | return -ENOENT; |
| 2787 | } |
| 2788 | |
| 2789 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2790 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2791 | * @cgrp: the cgroup in question |
| 2792 | * |
| 2793 | * Return the number of tasks in the cgroup. |
| 2794 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2795 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2796 | { |
| 2797 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2798 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2799 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2800 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2801 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2802 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2803 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2804 | return count; |
| 2805 | } |
| 2806 | |
| 2807 | /* |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2808 | * To reduce the fork() overhead for systems that are not actually using |
| 2809 | * their cgroups capability, we don't maintain the lists running through |
| 2810 | * 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] | 2811 | * words after the first call to css_task_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2812 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2813 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2814 | { |
| 2815 | struct task_struct *p, *g; |
| 2816 | write_lock(&css_set_lock); |
| 2817 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2818 | /* |
| 2819 | * We need tasklist_lock because RCU is not safe against |
| 2820 | * while_each_thread(). Besides, a forking task that has passed |
| 2821 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2822 | * is not guaranteed to have its child immediately visible in the |
| 2823 | * tasklist if we walk through it with RCU. |
| 2824 | */ |
| 2825 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2826 | do_each_thread(g, p) { |
| 2827 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2828 | /* |
| 2829 | * We should check if the process is exiting, otherwise |
| 2830 | * it will race with cgroup_exit() in that the list |
| 2831 | * entry won't be deleted though the process has exited. |
| 2832 | */ |
| 2833 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2834 | list_add(&p->cg_list, &task_css_set(p)->tasks); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2835 | task_unlock(p); |
| 2836 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2837 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2838 | write_unlock(&css_set_lock); |
| 2839 | } |
| 2840 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2841 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2842 | * css_next_child - find the next child of a given css |
| 2843 | * @pos_css: the current position (%NULL to initiate traversal) |
| 2844 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2845 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2846 | * 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] | 2847 | * under either cgroup_mutex or RCU read lock. The only requirement is |
| 2848 | * that @parent_css and @pos_css are accessible. The next sibling is |
| 2849 | * guaranteed to be returned regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2850 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2851 | struct cgroup_subsys_state * |
| 2852 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 2853 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2854 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2855 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 2856 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2857 | struct cgroup *next; |
| 2858 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2859 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2860 | |
| 2861 | /* |
| 2862 | * @pos could already have been removed. Once a cgroup is removed, |
| 2863 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 2864 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 2865 | * before the cgroup is taken off the ->sibling list, if we see it |
| 2866 | * unasserted, it's guaranteed that the next sibling hasn't |
| 2867 | * finished its grace period even if it's already removed, and thus |
| 2868 | * safe to dereference from this RCU critical section. If |
| 2869 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 2870 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2871 | * |
| 2872 | * If @pos is dead, its next pointer can't be dereferenced; |
| 2873 | * however, as each cgroup is given a monotonically increasing |
| 2874 | * unique serial number and always appended to the sibling list, |
| 2875 | * the next one can be found by walking the parent's children until |
| 2876 | * we see a cgroup with higher serial number than @pos's. While |
| 2877 | * this path can be slower, it's taken only when either the current |
| 2878 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2879 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2880 | if (!pos) { |
| 2881 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 2882 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2883 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2884 | } else { |
| 2885 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 2886 | if (next->serial_nr > pos->serial_nr) |
| 2887 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2888 | } |
| 2889 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2890 | if (&next->sibling == &cgrp->children) |
| 2891 | return NULL; |
| 2892 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2893 | return cgroup_css(next, parent_css->ss); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2894 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2895 | EXPORT_SYMBOL_GPL(css_next_child); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2896 | |
| 2897 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2898 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2899 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2900 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2901 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2902 | * 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] | 2903 | * to visit for pre-order traversal of @root's descendants. @root is |
| 2904 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2905 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2906 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2907 | * doesn't require the whole traversal to be contained in a single critical |
| 2908 | * section. This function will return the correct next descendant as long |
| 2909 | * 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] | 2910 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2911 | struct cgroup_subsys_state * |
| 2912 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 2913 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2914 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2915 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2916 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2917 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2918 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2919 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2920 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2921 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2922 | |
| 2923 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2924 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2925 | if (next) |
| 2926 | return next; |
| 2927 | |
| 2928 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2929 | while (pos != root) { |
| 2930 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2931 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2932 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2933 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2934 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2935 | |
| 2936 | return NULL; |
| 2937 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2938 | EXPORT_SYMBOL_GPL(css_next_descendant_pre); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2939 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2940 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2941 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 2942 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2943 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2944 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 2945 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2946 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2947 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2948 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2949 | * doesn't require the whole traversal to be contained in a single critical |
| 2950 | * section. This function will return the correct rightmost descendant as |
| 2951 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2952 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2953 | struct cgroup_subsys_state * |
| 2954 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2955 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2956 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2957 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2958 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2959 | |
| 2960 | do { |
| 2961 | last = pos; |
| 2962 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 2963 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2964 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2965 | pos = tmp; |
| 2966 | } while (pos); |
| 2967 | |
| 2968 | return last; |
| 2969 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2970 | EXPORT_SYMBOL_GPL(css_rightmost_descendant); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2971 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2972 | static struct cgroup_subsys_state * |
| 2973 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2974 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2975 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2976 | |
| 2977 | do { |
| 2978 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2979 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2980 | } while (pos); |
| 2981 | |
| 2982 | return last; |
| 2983 | } |
| 2984 | |
| 2985 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2986 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2987 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2988 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2989 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2990 | * 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] | 2991 | * to visit for post-order traversal of @root's descendants. @root is |
| 2992 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2993 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2994 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2995 | * doesn't require the whole traversal to be contained in a single critical |
| 2996 | * section. This function will return the correct next descendant as long |
| 2997 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 2998 | * @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2999 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3000 | struct cgroup_subsys_state * |
| 3001 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 3002 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3003 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3004 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3005 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3006 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3007 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 3008 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 3009 | if (!pos) |
| 3010 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3011 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3012 | /* if we visited @root, we're done */ |
| 3013 | if (pos == root) |
| 3014 | return NULL; |
| 3015 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3016 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3017 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3018 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3019 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3020 | |
| 3021 | /* no sibling left, visit parent */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3022 | return css_parent(pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3023 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3024 | EXPORT_SYMBOL_GPL(css_next_descendant_post); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3025 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3026 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3027 | * 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] | 3028 | * @it: the iterator to advance |
| 3029 | * |
| 3030 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3031 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3032 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3033 | { |
| 3034 | struct list_head *l = it->cset_link; |
| 3035 | struct cgrp_cset_link *link; |
| 3036 | struct css_set *cset; |
| 3037 | |
| 3038 | /* Advance to the next non-empty css_set */ |
| 3039 | do { |
| 3040 | l = l->next; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3041 | if (l == &it->origin_css->cgroup->cset_links) { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3042 | it->cset_link = NULL; |
| 3043 | return; |
| 3044 | } |
| 3045 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3046 | cset = link->cset; |
| 3047 | } while (list_empty(&cset->tasks)); |
| 3048 | it->cset_link = l; |
| 3049 | it->task = cset->tasks.next; |
| 3050 | } |
| 3051 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3052 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3053 | * css_task_iter_start - initiate task iteration |
| 3054 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3055 | * @it: the task iterator to use |
| 3056 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3057 | * Initiate iteration through the tasks of @css. The caller can call |
| 3058 | * css_task_iter_next() to walk through the tasks until the function |
| 3059 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 3060 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3061 | * |
| 3062 | * Note that this function acquires a lock which is released when the |
| 3063 | * iteration finishes. The caller can't sleep while iteration is in |
| 3064 | * progress. |
| 3065 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3066 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 3067 | struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3068 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3069 | { |
| 3070 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3071 | * The first time anyone tries to iterate across a css, we need to |
| 3072 | * enable the list linking each css_set to its tasks, and fix up |
| 3073 | * all existing tasks. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3074 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3075 | if (!use_task_css_set_links) |
| 3076 | cgroup_enable_task_cg_lists(); |
| 3077 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3078 | read_lock(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3079 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3080 | it->origin_css = css; |
| 3081 | it->cset_link = &css->cgroup->cset_links; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3082 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3083 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3084 | } |
| 3085 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3086 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3087 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3088 | * @it: the task iterator being iterated |
| 3089 | * |
| 3090 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3091 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 3092 | * reaches the end. |
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 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3095 | { |
| 3096 | struct task_struct *res; |
| 3097 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3098 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3099 | |
| 3100 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3101 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3102 | return NULL; |
| 3103 | res = list_entry(l, struct task_struct, cg_list); |
| 3104 | /* Advance iterator to find next entry */ |
| 3105 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3106 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3107 | if (l == &link->cset->tasks) { |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3108 | /* |
| 3109 | * We reached the end of this task list - move on to the |
| 3110 | * next cgrp_cset_link. |
| 3111 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3112 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3113 | } else { |
| 3114 | it->task = l; |
| 3115 | } |
| 3116 | return res; |
| 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_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3121 | * @it: the task iterator to finish |
| 3122 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3123 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3124 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3125 | void css_task_iter_end(struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3126 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3127 | { |
| 3128 | read_unlock(&css_set_lock); |
| 3129 | } |
| 3130 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3131 | static inline int started_after_time(struct task_struct *t1, |
| 3132 | struct timespec *time, |
| 3133 | struct task_struct *t2) |
| 3134 | { |
| 3135 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3136 | if (start_diff > 0) { |
| 3137 | return 1; |
| 3138 | } else if (start_diff < 0) { |
| 3139 | return 0; |
| 3140 | } else { |
| 3141 | /* |
| 3142 | * Arbitrarily, if two processes started at the same |
| 3143 | * time, we'll say that the lower pointer value |
| 3144 | * started first. Note that t2 may have exited by now |
| 3145 | * so this may not be a valid pointer any longer, but |
| 3146 | * that's fine - it still serves to distinguish |
| 3147 | * between two tasks started (effectively) simultaneously. |
| 3148 | */ |
| 3149 | return t1 > t2; |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | /* |
| 3154 | * This function is a callback from heap_insert() and is used to order |
| 3155 | * the heap. |
| 3156 | * In this case we order the heap in descending task start time. |
| 3157 | */ |
| 3158 | static inline int started_after(void *p1, void *p2) |
| 3159 | { |
| 3160 | struct task_struct *t1 = p1; |
| 3161 | struct task_struct *t2 = p2; |
| 3162 | return started_after_time(t1, &t2->start_time, t2); |
| 3163 | } |
| 3164 | |
| 3165 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3166 | * css_scan_tasks - iterate though all the tasks in a css |
| 3167 | * @css: the css to iterate tasks of |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3168 | * @test: optional test callback |
| 3169 | * @process: process callback |
| 3170 | * @data: data passed to @test and @process |
| 3171 | * @heap: optional pre-allocated heap used for task iteration |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3172 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3173 | * Iterate through all the tasks in @css, calling @test for each, and if it |
| 3174 | * returns %true, call @process for it also. |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3175 | * |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3176 | * @test may be NULL, meaning always true (select all tasks), which |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3177 | * effectively duplicates css_task_iter_{start,next,end}() but does not |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3178 | * lock css_set_lock for the call to @process. |
| 3179 | * |
| 3180 | * 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] | 3181 | * of @css for the duration of this call. This function may or may not |
| 3182 | * call @process for tasks that exit or move to a different css during the |
| 3183 | * call, or are forked or move into the css during the call. |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3184 | * |
| 3185 | * Note that @test may be called with locks held, and may in some |
| 3186 | * situations be called multiple times for the same task, so it should be |
| 3187 | * cheap. |
| 3188 | * |
| 3189 | * If @heap is non-NULL, a heap has been pre-allocated and will be used for |
| 3190 | * heap operations (and its "gt" member will be overwritten), else a |
| 3191 | * temporary heap will be used (allocation of which may cause this function |
| 3192 | * to fail). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3193 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3194 | int css_scan_tasks(struct cgroup_subsys_state *css, |
| 3195 | bool (*test)(struct task_struct *, void *), |
| 3196 | void (*process)(struct task_struct *, void *), |
| 3197 | void *data, struct ptr_heap *heap) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3198 | { |
| 3199 | int retval, i; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3200 | struct css_task_iter it; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3201 | struct task_struct *p, *dropped; |
| 3202 | /* Never dereference latest_task, since it's not refcounted */ |
| 3203 | struct task_struct *latest_task = NULL; |
| 3204 | struct ptr_heap tmp_heap; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3205 | struct timespec latest_time = { 0, 0 }; |
| 3206 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3207 | if (heap) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3208 | /* The caller supplied our heap and pre-allocated its memory */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3209 | heap->gt = &started_after; |
| 3210 | } else { |
| 3211 | /* We need to allocate our own heap memory */ |
| 3212 | heap = &tmp_heap; |
| 3213 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3214 | if (retval) |
| 3215 | /* cannot allocate the heap */ |
| 3216 | return retval; |
| 3217 | } |
| 3218 | |
| 3219 | again: |
| 3220 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3221 | * Scan tasks in the css, using the @test callback to determine |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3222 | * which are of interest, and invoking @process callback on the |
| 3223 | * ones which need an update. Since we don't want to hold any |
| 3224 | * locks during the task updates, gather tasks to be processed in a |
| 3225 | * heap structure. The heap is sorted by descending task start |
| 3226 | * time. If the statically-sized heap fills up, we overflow tasks |
| 3227 | * that started later, and in future iterations only consider tasks |
| 3228 | * that started after the latest task in the previous pass. This |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3229 | * guarantees forward progress and that we don't miss any tasks. |
| 3230 | */ |
| 3231 | heap->size = 0; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3232 | css_task_iter_start(css, &it); |
| 3233 | while ((p = css_task_iter_next(&it))) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3234 | /* |
| 3235 | * Only affect tasks that qualify per the caller's callback, |
| 3236 | * if he provided one |
| 3237 | */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3238 | if (test && !test(p, data)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3239 | continue; |
| 3240 | /* |
| 3241 | * Only process tasks that started after the last task |
| 3242 | * we processed |
| 3243 | */ |
| 3244 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3245 | continue; |
| 3246 | dropped = heap_insert(heap, p); |
| 3247 | if (dropped == NULL) { |
| 3248 | /* |
| 3249 | * The new task was inserted; the heap wasn't |
| 3250 | * previously full |
| 3251 | */ |
| 3252 | get_task_struct(p); |
| 3253 | } else if (dropped != p) { |
| 3254 | /* |
| 3255 | * The new task was inserted, and pushed out a |
| 3256 | * different task |
| 3257 | */ |
| 3258 | get_task_struct(p); |
| 3259 | put_task_struct(dropped); |
| 3260 | } |
| 3261 | /* |
| 3262 | * Else the new task was newer than anything already in |
| 3263 | * the heap and wasn't inserted |
| 3264 | */ |
| 3265 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3266 | css_task_iter_end(&it); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3267 | |
| 3268 | if (heap->size) { |
| 3269 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3270 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3271 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3272 | latest_time = q->start_time; |
| 3273 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3274 | } |
| 3275 | /* Process the task per the caller's callback */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3276 | process(q, data); |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3277 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3278 | } |
| 3279 | /* |
| 3280 | * If we had to process any tasks at all, scan again |
| 3281 | * in case some of them were in the middle of forking |
| 3282 | * children that didn't get processed. |
| 3283 | * Not the most efficient way to do it, but it avoids |
| 3284 | * having to take callback_mutex in the fork path |
| 3285 | */ |
| 3286 | goto again; |
| 3287 | } |
| 3288 | if (heap == &tmp_heap) |
| 3289 | heap_free(&tmp_heap); |
| 3290 | return 0; |
| 3291 | } |
| 3292 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3293 | static void cgroup_transfer_one_task(struct task_struct *task, void *data) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3294 | { |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3295 | struct cgroup *new_cgroup = data; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3296 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3297 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3298 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3299 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | /** |
| 3303 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3304 | * @to: cgroup to which the tasks will be moved |
| 3305 | * @from: cgroup in which the tasks currently reside |
| 3306 | */ |
| 3307 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3308 | { |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3309 | return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task, |
| 3310 | to, NULL); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3311 | } |
| 3312 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3313 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3314 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3315 | * |
| 3316 | * Reading this file can return large amounts of data if a cgroup has |
| 3317 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3318 | * but we cannot guarantee that the information we produce is correct |
| 3319 | * unless we produce it entirely atomically. |
| 3320 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3321 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3322 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3323 | /* which pidlist file are we talking about? */ |
| 3324 | enum cgroup_filetype { |
| 3325 | CGROUP_FILE_PROCS, |
| 3326 | CGROUP_FILE_TASKS, |
| 3327 | }; |
| 3328 | |
| 3329 | /* |
| 3330 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3331 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3332 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3333 | * to the cgroup. |
| 3334 | */ |
| 3335 | struct cgroup_pidlist { |
| 3336 | /* |
| 3337 | * used to find which pidlist is wanted. doesn't change as long as |
| 3338 | * this particular list stays in the list. |
| 3339 | */ |
| 3340 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3341 | /* array of xids */ |
| 3342 | pid_t *list; |
| 3343 | /* how many elements the above list has */ |
| 3344 | int length; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3345 | /* each of these stored in a list by its cgroup */ |
| 3346 | struct list_head links; |
| 3347 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3348 | struct cgroup *owner; |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3349 | /* for delayed destruction */ |
| 3350 | struct delayed_work destroy_dwork; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3351 | }; |
| 3352 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3353 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3354 | * The following two functions "fix" the issue where there are more pids |
| 3355 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3356 | * TODO: replace with a kernel-wide solution to this problem |
| 3357 | */ |
| 3358 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3359 | static void *pidlist_allocate(int count) |
| 3360 | { |
| 3361 | if (PIDLIST_TOO_LARGE(count)) |
| 3362 | return vmalloc(count * sizeof(pid_t)); |
| 3363 | else |
| 3364 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3365 | } |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3366 | |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3367 | static void pidlist_free(void *p) |
| 3368 | { |
| 3369 | if (is_vmalloc_addr(p)) |
| 3370 | vfree(p); |
| 3371 | else |
| 3372 | kfree(p); |
| 3373 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3374 | |
| 3375 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3376 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 3377 | * should be left afterwards. |
| 3378 | */ |
| 3379 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 3380 | { |
| 3381 | struct cgroup_pidlist *l, *tmp_l; |
| 3382 | |
| 3383 | mutex_lock(&cgrp->pidlist_mutex); |
| 3384 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 3385 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 3386 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3387 | |
| 3388 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 3389 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 3390 | } |
| 3391 | |
| 3392 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 3393 | { |
| 3394 | struct delayed_work *dwork = to_delayed_work(work); |
| 3395 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 3396 | destroy_dwork); |
| 3397 | struct cgroup_pidlist *tofree = NULL; |
| 3398 | |
| 3399 | mutex_lock(&l->owner->pidlist_mutex); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3400 | |
| 3401 | /* |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3402 | * Destroy iff we didn't get queued again. The state won't change |
| 3403 | * as destroy_dwork can only be queued while locked. |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3404 | */ |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3405 | if (!delayed_work_pending(dwork)) { |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3406 | list_del(&l->links); |
| 3407 | pidlist_free(l->list); |
| 3408 | put_pid_ns(l->key.ns); |
| 3409 | tofree = l; |
| 3410 | } |
| 3411 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3412 | mutex_unlock(&l->owner->pidlist_mutex); |
| 3413 | kfree(tofree); |
| 3414 | } |
| 3415 | |
| 3416 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3417 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3418 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3419 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3420 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3421 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3422 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3423 | |
| 3424 | /* |
| 3425 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3426 | * edge cases first; no work needs to be done for either |
| 3427 | */ |
| 3428 | if (length == 0 || length == 1) |
| 3429 | return length; |
| 3430 | /* src and dest walk down the list; dest counts unique elements */ |
| 3431 | for (src = 1; src < length; src++) { |
| 3432 | /* find next unique element */ |
| 3433 | while (list[src] == list[src-1]) { |
| 3434 | src++; |
| 3435 | if (src == length) |
| 3436 | goto after; |
| 3437 | } |
| 3438 | /* dest always points to where the next unique element goes */ |
| 3439 | list[dest] = list[src]; |
| 3440 | dest++; |
| 3441 | } |
| 3442 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3443 | return dest; |
| 3444 | } |
| 3445 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3446 | /* |
| 3447 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 3448 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 3449 | * different per namespace, each namespace needs differently sorted list, |
| 3450 | * making it impossible to use, for example, single rbtree of member tasks |
| 3451 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 3452 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 3453 | * pidlists keyed by cgroup and namespace. |
| 3454 | * |
| 3455 | * All this extra complexity was caused by the original implementation |
| 3456 | * committing to an entirely unnecessary property. In the long term, we |
| 3457 | * want to do away with it. Explicitly scramble sort order if |
| 3458 | * sane_behavior so that no such expectation exists in the new interface. |
| 3459 | * |
| 3460 | * Scrambling is done by swapping every two consecutive bits, which is |
| 3461 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 3462 | */ |
| 3463 | static pid_t pid_fry(pid_t pid) |
| 3464 | { |
| 3465 | unsigned a = pid & 0x55555555; |
| 3466 | unsigned b = pid & 0xAAAAAAAA; |
| 3467 | |
| 3468 | return (a << 1) | (b >> 1); |
| 3469 | } |
| 3470 | |
| 3471 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 3472 | { |
| 3473 | if (cgroup_sane_behavior(cgrp)) |
| 3474 | return pid_fry(pid); |
| 3475 | else |
| 3476 | return pid; |
| 3477 | } |
| 3478 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3479 | static int cmppid(const void *a, const void *b) |
| 3480 | { |
| 3481 | return *(pid_t *)a - *(pid_t *)b; |
| 3482 | } |
| 3483 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3484 | static int fried_cmppid(const void *a, const void *b) |
| 3485 | { |
| 3486 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 3487 | } |
| 3488 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3489 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3490 | enum cgroup_filetype type) |
| 3491 | { |
| 3492 | struct cgroup_pidlist *l; |
| 3493 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3494 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3495 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3496 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3497 | |
| 3498 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 3499 | if (l->key.type == type && l->key.ns == ns) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3500 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3501 | return NULL; |
| 3502 | } |
| 3503 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3504 | /* |
| 3505 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3506 | * returns with the lock on that pidlist already held, and takes care |
| 3507 | * of the use count, or returns NULL with no locks held if we're out of |
| 3508 | * memory. |
| 3509 | */ |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3510 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 3511 | enum cgroup_filetype type) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3512 | { |
| 3513 | struct cgroup_pidlist *l; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3514 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3515 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3516 | |
| 3517 | l = cgroup_pidlist_find(cgrp, type); |
| 3518 | if (l) |
| 3519 | return l; |
| 3520 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3521 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3522 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3523 | if (!l) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3524 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3525 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3526 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3527 | l->key.type = type; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3528 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 3529 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3530 | l->owner = cgrp; |
| 3531 | list_add(&l->links, &cgrp->pidlists); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3532 | return l; |
| 3533 | } |
| 3534 | |
| 3535 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3536 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3537 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3538 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3539 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3540 | { |
| 3541 | pid_t *array; |
| 3542 | int length; |
| 3543 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3544 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3545 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3546 | struct cgroup_pidlist *l; |
| 3547 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3548 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3549 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3550 | /* |
| 3551 | * If cgroup gets more users after we read count, we won't have |
| 3552 | * enough space - tough. This race is indistinguishable to the |
| 3553 | * caller from the case that the additional cgroup users didn't |
| 3554 | * show up until sometime later on. |
| 3555 | */ |
| 3556 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3557 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3558 | if (!array) |
| 3559 | return -ENOMEM; |
| 3560 | /* now, populate the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3561 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3562 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3563 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3564 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3565 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3566 | if (type == CGROUP_FILE_PROCS) |
| 3567 | pid = task_tgid_vnr(tsk); |
| 3568 | else |
| 3569 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3570 | if (pid > 0) /* make sure to only use valid results */ |
| 3571 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3572 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3573 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3574 | length = n; |
| 3575 | /* now sort & (if procs) strip out duplicates */ |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3576 | if (cgroup_sane_behavior(cgrp)) |
| 3577 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 3578 | else |
| 3579 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3580 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3581 | length = pidlist_uniq(array, length); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3582 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3583 | l = cgroup_pidlist_find_create(cgrp, type); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3584 | if (!l) { |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3585 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3586 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3587 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3588 | } |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3589 | |
| 3590 | /* store array, freeing old if necessary */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3591 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3592 | l->list = array; |
| 3593 | l->length = length; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3594 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3595 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3596 | } |
| 3597 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3598 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3599 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3600 | * @stats: cgroupstats to fill information into |
| 3601 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3602 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3603 | * |
| 3604 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3605 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3606 | */ |
| 3607 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3608 | { |
| 3609 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3610 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3611 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3612 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3613 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3614 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3615 | * Validate dentry by checking the superblock operations, |
| 3616 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3617 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3618 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3619 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3620 | goto err; |
| 3621 | |
| 3622 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3623 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3624 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3625 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3626 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3627 | switch (tsk->state) { |
| 3628 | case TASK_RUNNING: |
| 3629 | stats->nr_running++; |
| 3630 | break; |
| 3631 | case TASK_INTERRUPTIBLE: |
| 3632 | stats->nr_sleeping++; |
| 3633 | break; |
| 3634 | case TASK_UNINTERRUPTIBLE: |
| 3635 | stats->nr_uninterruptible++; |
| 3636 | break; |
| 3637 | case TASK_STOPPED: |
| 3638 | stats->nr_stopped++; |
| 3639 | break; |
| 3640 | default: |
| 3641 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3642 | stats->nr_io_wait++; |
| 3643 | break; |
| 3644 | } |
| 3645 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3646 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3647 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3648 | err: |
| 3649 | return ret; |
| 3650 | } |
| 3651 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3652 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3653 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3654 | * 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] | 3655 | * 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] | 3656 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3657 | */ |
| 3658 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3659 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3660 | { |
| 3661 | /* |
| 3662 | * Initially we receive a position value that corresponds to |
| 3663 | * one more than the last pid shown (or 0 on the first call or |
| 3664 | * after a seek to the start). Use a binary-search to find the |
| 3665 | * next pid to display, if any |
| 3666 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3667 | struct cgroup_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3668 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3669 | struct cgroup_pidlist *l; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3670 | enum cgroup_filetype type = seq_cft(s)->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3671 | int index = 0, pid = *pos; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3672 | int *iter, ret; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3673 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3674 | mutex_lock(&cgrp->pidlist_mutex); |
| 3675 | |
| 3676 | /* |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3677 | * !NULL @of->priv indicates that this isn't the first start() |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3678 | * after open. If the matching pidlist is around, we can use that. |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3679 | * 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] | 3680 | * could already have been destroyed. |
| 3681 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3682 | if (of->priv) |
| 3683 | of->priv = cgroup_pidlist_find(cgrp, type); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3684 | |
| 3685 | /* |
| 3686 | * Either this is the first start() after open or the matching |
| 3687 | * pidlist has been destroyed inbetween. Create a new one. |
| 3688 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3689 | if (!of->priv) { |
| 3690 | ret = pidlist_array_load(cgrp, type, |
| 3691 | (struct cgroup_pidlist **)&of->priv); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3692 | if (ret) |
| 3693 | return ERR_PTR(ret); |
| 3694 | } |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3695 | l = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3696 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3697 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3698 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3699 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3700 | while (index < end) { |
| 3701 | int mid = (index + end) / 2; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3702 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3703 | index = mid; |
| 3704 | break; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3705 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3706 | index = mid + 1; |
| 3707 | else |
| 3708 | end = mid; |
| 3709 | } |
| 3710 | } |
| 3711 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3712 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3713 | return NULL; |
| 3714 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3715 | iter = l->list + index; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3716 | *pos = cgroup_pid_fry(cgrp, *iter); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3717 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3718 | } |
| 3719 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3720 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3721 | { |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3722 | struct cgroup_open_file *of = s->private; |
| 3723 | struct cgroup_pidlist *l = of->priv; |
Tejun Heo | 6223685 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3724 | |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3725 | if (l) |
| 3726 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3727 | CGROUP_PIDLIST_DESTROY_DELAY); |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3728 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3729 | } |
| 3730 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3731 | 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] | 3732 | { |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3733 | struct cgroup_open_file *of = s->private; |
| 3734 | struct cgroup_pidlist *l = of->priv; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3735 | pid_t *p = v; |
| 3736 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3737 | /* |
| 3738 | * Advance to the next pid in the array. If this goes off the |
| 3739 | * end, we're done |
| 3740 | */ |
| 3741 | p++; |
| 3742 | if (p >= end) { |
| 3743 | return NULL; |
| 3744 | } else { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3745 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3746 | return p; |
| 3747 | } |
| 3748 | } |
| 3749 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3750 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3751 | { |
| 3752 | return seq_printf(s, "%d\n", *(int *)v); |
| 3753 | } |
| 3754 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3755 | /* |
| 3756 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3757 | * independent of whether it's tasks or procs |
| 3758 | */ |
| 3759 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3760 | .start = cgroup_pidlist_start, |
| 3761 | .stop = cgroup_pidlist_stop, |
| 3762 | .next = cgroup_pidlist_next, |
| 3763 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3764 | }; |
| 3765 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3766 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3767 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3768 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3769 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3770 | } |
| 3771 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3772 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3773 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3774 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3775 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3776 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3777 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3778 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3779 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3780 | return 0; |
| 3781 | } |
| 3782 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3783 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3784 | * When dput() is called asynchronously, if umount has been done and |
| 3785 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3786 | * there's a small window that vfs will see the root dentry with non-zero |
| 3787 | * refcnt and trigger BUG(). |
| 3788 | * |
| 3789 | * That's why we hold a reference before dput() and drop it right after. |
| 3790 | */ |
| 3791 | static void cgroup_dput(struct cgroup *cgrp) |
| 3792 | { |
| 3793 | struct super_block *sb = cgrp->root->sb; |
| 3794 | |
| 3795 | atomic_inc(&sb->s_active); |
| 3796 | dput(cgrp->dentry); |
| 3797 | deactivate_super(sb); |
| 3798 | } |
| 3799 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3800 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 3801 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3802 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3803 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3804 | } |
| 3805 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3806 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 3807 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3808 | { |
| 3809 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3810 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3811 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3812 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3813 | return 0; |
| 3814 | } |
| 3815 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3816 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3817 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3818 | .name = "cgroup.procs", |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3819 | .seq_start = cgroup_pidlist_start, |
| 3820 | .seq_next = cgroup_pidlist_next, |
| 3821 | .seq_stop = cgroup_pidlist_stop, |
| 3822 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3823 | .private = CGROUP_FILE_PROCS, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3824 | .write_u64 = cgroup_procs_write, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3825 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3826 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3827 | { |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3828 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3829 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3830 | .read_u64 = cgroup_clone_children_read, |
| 3831 | .write_u64 = cgroup_clone_children_write, |
| 3832 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3833 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3834 | .name = "cgroup.sane_behavior", |
| 3835 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3836 | .seq_show = cgroup_sane_behavior_show, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3837 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3838 | |
| 3839 | /* |
| 3840 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 3841 | * don't exist if sane_behavior. If you're depending on these, be |
| 3842 | * prepared to be burned. |
| 3843 | */ |
| 3844 | { |
| 3845 | .name = "tasks", |
| 3846 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3847 | .seq_start = cgroup_pidlist_start, |
| 3848 | .seq_next = cgroup_pidlist_next, |
| 3849 | .seq_stop = cgroup_pidlist_stop, |
| 3850 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3851 | .private = CGROUP_FILE_TASKS, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3852 | .write_u64 = cgroup_tasks_write, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3853 | .mode = S_IRUGO | S_IWUSR, |
| 3854 | }, |
| 3855 | { |
| 3856 | .name = "notify_on_release", |
| 3857 | .flags = CFTYPE_INSANE, |
| 3858 | .read_u64 = cgroup_read_notify_on_release, |
| 3859 | .write_u64 = cgroup_write_notify_on_release, |
| 3860 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3861 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3862 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 3863 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3864 | .seq_show = cgroup_release_agent_show, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3865 | .write_string = cgroup_release_agent_write, |
| 3866 | .max_write_len = PATH_MAX, |
| 3867 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3868 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3869 | }; |
| 3870 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3871 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3872 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3873 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3874 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3875 | * |
| 3876 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3877 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3878 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3879 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3880 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3881 | int i, ret = 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3882 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3883 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3884 | for_each_subsys(ss, i) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3885 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3886 | |
| 3887 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3888 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3889 | |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3890 | list_for_each_entry(set, &ss->cftsets, node) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3891 | ret = cgroup_addrm_files(cgrp, set->cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3892 | if (ret < 0) |
| 3893 | goto err; |
| 3894 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3895 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3896 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3897 | err: |
| 3898 | cgroup_clear_dir(cgrp, subsys_mask); |
| 3899 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3900 | } |
| 3901 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3902 | /* |
| 3903 | * css destruction is four-stage process. |
| 3904 | * |
| 3905 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 3906 | * Implemented in kill_css(). |
| 3907 | * |
| 3908 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
| 3909 | * and thus css_tryget() is guaranteed to fail, the css can be offlined |
| 3910 | * by invoking offline_css(). After offlining, the base ref is put. |
| 3911 | * Implemented in css_killed_work_fn(). |
| 3912 | * |
| 3913 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 3914 | * accessors are inside RCU read sections. css_release() schedules the |
| 3915 | * RCU callback. |
| 3916 | * |
| 3917 | * 4. After the grace period, the css can be freed. Implemented in |
| 3918 | * css_free_work_fn(). |
| 3919 | * |
| 3920 | * It is actually hairier because both step 2 and 4 require process context |
| 3921 | * and thus involve punting to css->destroy_work adding two additional |
| 3922 | * steps to the already complex sequence. |
| 3923 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3924 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3925 | { |
| 3926 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3927 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3928 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3929 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3930 | if (css->parent) |
| 3931 | css_put(css->parent); |
| 3932 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3933 | css->ss->css_free(css); |
| 3934 | cgroup_dput(cgrp); |
| 3935 | } |
| 3936 | |
| 3937 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 3938 | { |
| 3939 | struct cgroup_subsys_state *css = |
| 3940 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 3941 | |
| 3942 | /* |
| 3943 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 3944 | * css_put(). dput() requires process context which we don't have. |
| 3945 | */ |
| 3946 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 3947 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3948 | } |
| 3949 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3950 | static void css_release(struct percpu_ref *ref) |
| 3951 | { |
| 3952 | struct cgroup_subsys_state *css = |
| 3953 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 3954 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3955 | rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3956 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3957 | } |
| 3958 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3959 | static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss, |
| 3960 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3961 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3962 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 3963 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3964 | css->flags = 0; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3965 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3966 | if (cgrp->parent) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3967 | css->parent = cgroup_css(cgrp->parent, ss); |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3968 | else |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3969 | css->flags |= CSS_ROOT; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3970 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3971 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3972 | } |
| 3973 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 3974 | /* 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] | 3975 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3976 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3977 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3978 | int ret = 0; |
| 3979 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3980 | lockdep_assert_held(&cgroup_mutex); |
| 3981 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3982 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3983 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3984 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3985 | css->flags |= CSS_ONLINE; |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3986 | css->cgroup->nr_css++; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3987 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3988 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3989 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3990 | } |
| 3991 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 3992 | /* 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] | 3993 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3994 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3995 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3996 | |
| 3997 | lockdep_assert_held(&cgroup_mutex); |
| 3998 | |
| 3999 | if (!(css->flags & CSS_ONLINE)) |
| 4000 | return; |
| 4001 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4002 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4003 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4004 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4005 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4006 | css->cgroup->nr_css--; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4007 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4008 | } |
| 4009 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4010 | /** |
| 4011 | * create_css - create a cgroup_subsys_state |
| 4012 | * @cgrp: the cgroup new css will be associated with |
| 4013 | * @ss: the subsys of new css |
| 4014 | * |
| 4015 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
| 4016 | * css is online and installed in @cgrp with all interface files created. |
| 4017 | * Returns 0 on success, -errno on failure. |
| 4018 | */ |
| 4019 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss) |
| 4020 | { |
| 4021 | struct cgroup *parent = cgrp->parent; |
| 4022 | struct cgroup_subsys_state *css; |
| 4023 | int err; |
| 4024 | |
| 4025 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 4026 | lockdep_assert_held(&cgroup_mutex); |
| 4027 | |
| 4028 | css = ss->css_alloc(cgroup_css(parent, ss)); |
| 4029 | if (IS_ERR(css)) |
| 4030 | return PTR_ERR(css); |
| 4031 | |
| 4032 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4033 | if (err) |
| 4034 | goto err_free; |
| 4035 | |
| 4036 | init_css(css, ss, cgrp); |
| 4037 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4038 | err = cgroup_populate_dir(cgrp, 1 << ss->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4039 | if (err) |
| 4040 | goto err_free; |
| 4041 | |
| 4042 | err = online_css(css); |
| 4043 | if (err) |
| 4044 | goto err_free; |
| 4045 | |
| 4046 | dget(cgrp->dentry); |
| 4047 | css_get(css->parent); |
| 4048 | |
| 4049 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4050 | parent->parent) { |
| 4051 | 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", |
| 4052 | current->comm, current->pid, ss->name); |
| 4053 | if (!strcmp(ss->name, "memory")) |
| 4054 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4055 | ss->warned_broken_hierarchy = true; |
| 4056 | } |
| 4057 | |
| 4058 | return 0; |
| 4059 | |
| 4060 | err_free: |
| 4061 | percpu_ref_cancel_init(&css->refcnt); |
| 4062 | ss->css_free(css); |
| 4063 | return err; |
| 4064 | } |
| 4065 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4066 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4067 | * cgroup_create - create a cgroup |
| 4068 | * @parent: cgroup that will be parent of the new cgroup |
| 4069 | * @dentry: dentry of the new cgroup |
| 4070 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4071 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4072 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4073 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4074 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4075 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4076 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4077 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4078 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4079 | struct cgroupfs_root *root = parent->root; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 4080 | int ssid, err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4081 | struct cgroup_subsys *ss; |
| 4082 | struct super_block *sb = root->sb; |
| 4083 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4084 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4085 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4086 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4087 | return -ENOMEM; |
| 4088 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4089 | name = cgroup_alloc_name(dentry); |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 4090 | if (!name) { |
| 4091 | err = -ENOMEM; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4092 | goto err_free_cgrp; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 4093 | } |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4094 | rcu_assign_pointer(cgrp->name, name); |
| 4095 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4096 | /* |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4097 | * Only live parents can have children. Note that the liveliness |
| 4098 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4099 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4100 | * anyway so that locking is contained inside cgroup proper and we |
| 4101 | * don't get nasty surprises if we ever grow another caller. |
| 4102 | */ |
| 4103 | if (!cgroup_lock_live_group(parent)) { |
| 4104 | err = -ENODEV; |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4105 | goto err_free_name; |
| 4106 | } |
| 4107 | |
| 4108 | /* |
| 4109 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4110 | * a half-baked cgroup. |
| 4111 | */ |
| 4112 | cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL); |
| 4113 | if (cgrp->id < 0) { |
| 4114 | err = -ENOMEM; |
| 4115 | goto err_unlock; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4116 | } |
| 4117 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4118 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4119 | * get deleted on unmount if there are child cgroups. This |
| 4120 | * can be done outside cgroup_mutex, since the sb can't |
| 4121 | * disappear while someone has an open control file on the |
| 4122 | * fs */ |
| 4123 | atomic_inc(&sb->s_active); |
| 4124 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4125 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4126 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4127 | dentry->d_fsdata = cgrp; |
| 4128 | cgrp->dentry = dentry; |
| 4129 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4130 | cgrp->parent = parent; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4131 | cgrp->dummy_css.parent = &parent->dummy_css; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4132 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4133 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4134 | if (notify_on_release(parent)) |
| 4135 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4136 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4137 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4138 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4139 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4140 | /* |
| 4141 | * Create directory. cgroup_create_file() returns with the new |
| 4142 | * directory locked on success so that it can be populated without |
| 4143 | * dropping cgroup_mutex. |
| 4144 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4145 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4146 | if (err < 0) |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4147 | goto err_free_id; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4148 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4149 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4150 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4151 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4152 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4153 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4154 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4155 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4156 | /* hold a ref to the parent's dentry */ |
| 4157 | dget(parent->dentry); |
| 4158 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4159 | /* |
| 4160 | * @cgrp is now fully operational. If something fails after this |
| 4161 | * point, it'll be released via the normal destruction path. |
| 4162 | */ |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4163 | idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 4164 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4165 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4166 | if (err) |
| 4167 | goto err_destroy; |
| 4168 | |
Tejun Heo | 9d403e9 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4169 | /* let's create and online css's */ |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4170 | for_each_subsys(ss, ssid) { |
| 4171 | if (root->subsys_mask & (1 << ssid)) { |
| 4172 | err = create_css(cgrp, ss); |
| 4173 | if (err) |
| 4174 | goto err_destroy; |
| 4175 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4176 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4177 | |
| 4178 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4179 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4180 | |
| 4181 | return 0; |
| 4182 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4183 | err_free_id: |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4184 | idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4185 | /* Release the reference count that we took on the superblock */ |
| 4186 | deactivate_super(sb); |
| 4187 | err_unlock: |
| 4188 | mutex_unlock(&cgroup_mutex); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4189 | err_free_name: |
| 4190 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4191 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4192 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4193 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4194 | |
| 4195 | err_destroy: |
| 4196 | cgroup_destroy_locked(cgrp); |
| 4197 | mutex_unlock(&cgroup_mutex); |
| 4198 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4199 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4200 | } |
| 4201 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4202 | 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] | 4203 | { |
| 4204 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4205 | |
| 4206 | /* the vfs holds inode->i_mutex already */ |
| 4207 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4208 | } |
| 4209 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4210 | /* |
| 4211 | * This is called when the refcnt of a css is confirmed to be killed. |
| 4212 | * css_tryget() is now guaranteed to fail. |
| 4213 | */ |
| 4214 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4215 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4216 | struct cgroup_subsys_state *css = |
| 4217 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 4218 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4219 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4220 | mutex_lock(&cgroup_mutex); |
| 4221 | |
| 4222 | /* |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4223 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4224 | * initate destruction. |
| 4225 | */ |
| 4226 | offline_css(css); |
| 4227 | |
| 4228 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4229 | * If @cgrp is marked dead, it's waiting for refs of all css's to |
| 4230 | * be disabled before proceeding to the second phase of cgroup |
| 4231 | * destruction. If we are the last one, kick it off. |
| 4232 | */ |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4233 | if (!cgrp->nr_css && cgroup_is_dead(cgrp)) |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4234 | cgroup_destroy_css_killed(cgrp); |
| 4235 | |
| 4236 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4237 | |
| 4238 | /* |
| 4239 | * Put the css refs from kill_css(). Each css holds an extra |
| 4240 | * reference to the cgroup's dentry and cgroup removal proceeds |
| 4241 | * regardless of css refs. On the last put of each css, whenever |
| 4242 | * that may be, the extra dentry ref is put so that dentry |
| 4243 | * destruction happens only after all css's are released. |
| 4244 | */ |
| 4245 | css_put(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4246 | } |
| 4247 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4248 | /* css kill confirmation processing requires process context, bounce */ |
| 4249 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4250 | { |
| 4251 | struct cgroup_subsys_state *css = |
| 4252 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4253 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4254 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4255 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | /** |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4259 | * kill_css - destroy a css |
| 4260 | * @css: css to destroy |
| 4261 | * |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4262 | * This function initiates destruction of @css by removing cgroup interface |
| 4263 | * files and putting its base reference. ->css_offline() will be invoked |
| 4264 | * asynchronously once css_tryget() is guaranteed to fail and when the |
| 4265 | * reference count reaches zero, @css will be released. |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4266 | */ |
| 4267 | static void kill_css(struct cgroup_subsys_state *css) |
| 4268 | { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4269 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4270 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4271 | /* |
| 4272 | * Killing would put the base ref, but we need to keep it alive |
| 4273 | * until after ->css_offline(). |
| 4274 | */ |
| 4275 | css_get(css); |
| 4276 | |
| 4277 | /* |
| 4278 | * cgroup core guarantees that, by the time ->css_offline() is |
| 4279 | * invoked, no new css reference will be given out via |
| 4280 | * css_tryget(). We can't simply call percpu_ref_kill() and |
| 4281 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 4282 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 4283 | * |
| 4284 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4285 | * css is confirmed to be seen as killed on all CPUs. |
| 4286 | */ |
| 4287 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | /** |
| 4291 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4292 | * @cgrp: cgroup to be destroyed |
| 4293 | * |
| 4294 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4295 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4296 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4297 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4298 | * the following two steps. |
| 4299 | * |
| 4300 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4301 | * userland visible parts and start killing the percpu refcnts of |
| 4302 | * css's. Set up so that the next stage will be kicked off once all |
| 4303 | * the percpu refcnts are confirmed to be killed. |
| 4304 | * |
| 4305 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4306 | * rest of destruction. Once all cgroup references are gone, the |
| 4307 | * cgroup is RCU-freed. |
| 4308 | * |
| 4309 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4310 | * the userland is concerned and a new cgroup with the same name may be |
| 4311 | * created. As cgroup doesn't care about the names internally, this |
| 4312 | * doesn't cause any problem. |
| 4313 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4314 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4315 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4316 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4317 | struct dentry *d = cgrp->dentry; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4318 | struct cgroup_subsys_state *css; |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4319 | struct cgroup *child; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4320 | bool empty; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4321 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4322 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4323 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4324 | lockdep_assert_held(&cgroup_mutex); |
| 4325 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4326 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4327 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4328 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4329 | */ |
| 4330 | read_lock(&css_set_lock); |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4331 | empty = list_empty(&cgrp->cset_links); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4332 | read_unlock(&css_set_lock); |
| 4333 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4334 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4335 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4336 | /* |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4337 | * Make sure there's no live children. We can't test ->children |
| 4338 | * emptiness as dead children linger on it while being destroyed; |
| 4339 | * otherwise, "rmdir parent/child parent" may fail with -EBUSY. |
| 4340 | */ |
| 4341 | empty = true; |
| 4342 | rcu_read_lock(); |
| 4343 | list_for_each_entry_rcu(child, &cgrp->children, sibling) { |
| 4344 | empty = cgroup_is_dead(child); |
| 4345 | if (!empty) |
| 4346 | break; |
| 4347 | } |
| 4348 | rcu_read_unlock(); |
| 4349 | if (!empty) |
| 4350 | return -EBUSY; |
| 4351 | |
| 4352 | /* |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4353 | * Initiate massacre of all css's. cgroup_destroy_css_killed() |
| 4354 | * will be invoked to perform the rest of destruction once the |
| 4355 | * percpu refs of all css's are confirmed to be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4356 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4357 | for_each_css(css, ssid, cgrp) |
| 4358 | kill_css(css); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4359 | |
| 4360 | /* |
| 4361 | * Mark @cgrp dead. This prevents further task migration and child |
| 4362 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4363 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4364 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4365 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4366 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4367 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4368 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4369 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4370 | raw_spin_lock(&release_list_lock); |
| 4371 | if (!list_empty(&cgrp->release_list)) |
| 4372 | list_del_init(&cgrp->release_list); |
| 4373 | raw_spin_unlock(&release_list_lock); |
| 4374 | |
| 4375 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4376 | * If @cgrp has css's attached, the second stage of cgroup |
| 4377 | * destruction is kicked off from css_killed_work_fn() after the |
| 4378 | * refs of all attached css's are killed. If @cgrp doesn't have |
| 4379 | * any css, we kick it off here. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4380 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4381 | if (!cgrp->nr_css) |
| 4382 | cgroup_destroy_css_killed(cgrp); |
| 4383 | |
| 4384 | /* |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4385 | * Clear the base files and remove @cgrp directory. The removal |
| 4386 | * puts the base ref but we aren't quite done with @cgrp yet, so |
| 4387 | * hold onto it. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4388 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4389 | cgroup_addrm_files(cgrp, cgroup_base_files, false); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4390 | dget(d); |
| 4391 | cgroup_d_remove_dir(d); |
| 4392 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4393 | return 0; |
| 4394 | }; |
| 4395 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4396 | /** |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4397 | * cgroup_destroy_css_killed - the second step of cgroup destruction |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4398 | * @work: cgroup->destroy_free_work |
| 4399 | * |
| 4400 | * 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] | 4401 | * destroyed after all css's are offlined and performs the rest of |
| 4402 | * destruction. This is the second step of destruction described in the |
| 4403 | * comment above cgroup_destroy_locked(). |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4404 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4405 | static void cgroup_destroy_css_killed(struct cgroup *cgrp) |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4406 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4407 | struct cgroup *parent = cgrp->parent; |
| 4408 | struct dentry *d = cgrp->dentry; |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4409 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4410 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4411 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4412 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4413 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4414 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4415 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4416 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4417 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4418 | check_for_release(parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4419 | } |
| 4420 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4421 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4422 | { |
| 4423 | int ret; |
| 4424 | |
| 4425 | mutex_lock(&cgroup_mutex); |
| 4426 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4427 | mutex_unlock(&cgroup_mutex); |
| 4428 | |
| 4429 | return ret; |
| 4430 | } |
| 4431 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4432 | static void __init cgroup_init_cftsets(struct cgroup_subsys *ss) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4433 | { |
| 4434 | INIT_LIST_HEAD(&ss->cftsets); |
| 4435 | |
| 4436 | /* |
| 4437 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4438 | * deregistration. |
| 4439 | */ |
| 4440 | if (ss->base_cftypes) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4441 | struct cftype *cft; |
| 4442 | |
| 4443 | for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++) |
| 4444 | cft->ss = ss; |
| 4445 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4446 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4447 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4448 | } |
| 4449 | } |
| 4450 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4451 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4452 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4453 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4454 | |
| 4455 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4456 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4457 | mutex_lock(&cgroup_mutex); |
| 4458 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4459 | /* init base cftset */ |
| 4460 | cgroup_init_cftsets(ss); |
| 4461 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4462 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4463 | ss->root = &cgroup_dummy_root; |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4464 | css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4465 | /* We don't handle early failures gracefully */ |
| 4466 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4467 | init_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4468 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4469 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4470 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4471 | * newly registered, all tasks and hence the |
| 4472 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4473 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4474 | |
| 4475 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4476 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4477 | /* At system boot, before all subsystems have been |
| 4478 | * registered, no tasks have been forked, so we don't |
| 4479 | * need to invoke fork callbacks here. */ |
| 4480 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4481 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4482 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4483 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4484 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4488 | * cgroup_init_early - cgroup initialization at system boot |
| 4489 | * |
| 4490 | * Initialize cgroups at system boot, and initialize any |
| 4491 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4492 | */ |
| 4493 | int __init cgroup_init_early(void) |
| 4494 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4495 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4496 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4497 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4498 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4499 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4500 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4501 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4502 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4503 | init_cgroup_root(&cgroup_dummy_root); |
| 4504 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4505 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4506 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4507 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4508 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4509 | 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] | 4510 | 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] | 4511 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4512 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4513 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4514 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n", |
| 4515 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4516 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4517 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 4518 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
| 4519 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4520 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4521 | ss->name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4522 | |
| 4523 | if (ss->early_init) |
| 4524 | cgroup_init_subsys(ss); |
| 4525 | } |
| 4526 | return 0; |
| 4527 | } |
| 4528 | |
| 4529 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4530 | * cgroup_init - cgroup initialization |
| 4531 | * |
| 4532 | * Register cgroup filesystem and /proc file, and initialize |
| 4533 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4534 | */ |
| 4535 | int __init cgroup_init(void) |
| 4536 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4537 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4538 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4539 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4540 | |
| 4541 | err = bdi_init(&cgroup_backing_dev_info); |
| 4542 | if (err) |
| 4543 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4544 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4545 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4546 | if (!ss->early_init) |
| 4547 | cgroup_init_subsys(ss); |
| 4548 | } |
| 4549 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4550 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4551 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4552 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4553 | /* Add init_css_set to the hash table */ |
| 4554 | key = css_set_hash(init_css_set.subsys); |
| 4555 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4556 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4557 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4558 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4559 | err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top, |
| 4560 | 0, 1, GFP_KERNEL); |
| 4561 | BUG_ON(err < 0); |
| 4562 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4563 | mutex_unlock(&cgroup_mutex); |
| 4564 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4565 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4566 | if (!cgroup_kobj) { |
| 4567 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4568 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | err = register_filesystem(&cgroup_fs_type); |
| 4572 | if (err < 0) { |
| 4573 | kobject_put(cgroup_kobj); |
| 4574 | goto out; |
| 4575 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4576 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4577 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4578 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4579 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4580 | if (err) |
| 4581 | bdi_destroy(&cgroup_backing_dev_info); |
| 4582 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4583 | return err; |
| 4584 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4585 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4586 | static int __init cgroup_wq_init(void) |
| 4587 | { |
| 4588 | /* |
| 4589 | * There isn't much point in executing destruction path in |
| 4590 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Hugh Dickins | ab3f5fa | 2014-02-06 15:56:01 -0800 | [diff] [blame] | 4591 | * |
| 4592 | * XXX: Must be ordered to make sure parent is offlined after |
| 4593 | * children. The ordering requirement is for memcg where a |
| 4594 | * parent's offline may wait for a child's leading to deadlock. In |
| 4595 | * the long term, this should be fixed from memcg side. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4596 | * |
| 4597 | * We would prefer to do this in cgroup_init() above, but that |
| 4598 | * is called before init_workqueues(): so leave this until after. |
| 4599 | */ |
Hugh Dickins | ab3f5fa | 2014-02-06 15:56:01 -0800 | [diff] [blame] | 4600 | cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4601 | BUG_ON(!cgroup_destroy_wq); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 4602 | |
| 4603 | /* |
| 4604 | * Used to destroy pidlists and separate to serve as flush domain. |
| 4605 | * Cap @max_active to 1 too. |
| 4606 | */ |
| 4607 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 4608 | 0, 1); |
| 4609 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 4610 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4611 | return 0; |
| 4612 | } |
| 4613 | core_initcall(cgroup_wq_init); |
| 4614 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4615 | /* |
| 4616 | * proc_cgroup_show() |
| 4617 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4618 | * - Used for /proc/<pid>/cgroup. |
| 4619 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4620 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4621 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4622 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4623 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4624 | * cgroup to top_cgroup. |
| 4625 | */ |
| 4626 | |
| 4627 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4628 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4629 | { |
| 4630 | struct pid *pid; |
| 4631 | struct task_struct *tsk; |
| 4632 | char *buf; |
| 4633 | int retval; |
| 4634 | struct cgroupfs_root *root; |
| 4635 | |
| 4636 | retval = -ENOMEM; |
| 4637 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4638 | if (!buf) |
| 4639 | goto out; |
| 4640 | |
| 4641 | retval = -ESRCH; |
| 4642 | pid = m->private; |
| 4643 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4644 | if (!tsk) |
| 4645 | goto out_free; |
| 4646 | |
| 4647 | retval = 0; |
| 4648 | |
| 4649 | mutex_lock(&cgroup_mutex); |
| 4650 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4651 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4652 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4653 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4654 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4655 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4656 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4657 | for_each_subsys(ss, ssid) |
| 4658 | if (root->subsys_mask & (1 << ssid)) |
| 4659 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4660 | if (strlen(root->name)) |
| 4661 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4662 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4663 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4664 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4665 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4666 | if (retval < 0) |
| 4667 | goto out_unlock; |
| 4668 | seq_puts(m, buf); |
| 4669 | seq_putc(m, '\n'); |
| 4670 | } |
| 4671 | |
| 4672 | out_unlock: |
| 4673 | mutex_unlock(&cgroup_mutex); |
| 4674 | put_task_struct(tsk); |
| 4675 | out_free: |
| 4676 | kfree(buf); |
| 4677 | out: |
| 4678 | return retval; |
| 4679 | } |
| 4680 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4681 | /* Display information about each subsystem and each hierarchy */ |
| 4682 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4683 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4684 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4685 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4686 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4687 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4688 | /* |
| 4689 | * ideally we don't want subsystems moving around while we do this. |
| 4690 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4691 | * subsys/hierarchy state. |
| 4692 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4693 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4694 | |
| 4695 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4696 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4697 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4698 | ss->root->number_of_cgroups, !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4699 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4700 | mutex_unlock(&cgroup_mutex); |
| 4701 | return 0; |
| 4702 | } |
| 4703 | |
| 4704 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4705 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4706 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4707 | } |
| 4708 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4709 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4710 | .open = cgroupstats_open, |
| 4711 | .read = seq_read, |
| 4712 | .llseek = seq_lseek, |
| 4713 | .release = single_release, |
| 4714 | }; |
| 4715 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4716 | /** |
| 4717 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4718 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4719 | * |
| 4720 | * Description: A task inherits its parent's cgroup at fork(). |
| 4721 | * |
| 4722 | * A pointer to the shared css_set was automatically copied in |
| 4723 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4724 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 4725 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 4726 | * have already changed current->cgroups, allowing the previously |
| 4727 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4728 | * |
| 4729 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 4730 | * task, and the passed argument 'child' points to the child task. |
| 4731 | */ |
| 4732 | void cgroup_fork(struct task_struct *child) |
| 4733 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4734 | task_lock(current); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4735 | get_css_set(task_css_set(current)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4736 | child->cgroups = current->cgroups; |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4737 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4738 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4742 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4743 | * @child: the task in question |
| 4744 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4745 | * Adds the task to the list running through its css_set if necessary and |
| 4746 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4747 | * 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] | 4748 | * 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] | 4749 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4750 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4751 | void cgroup_post_fork(struct task_struct *child) |
| 4752 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4753 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4754 | int i; |
| 4755 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4756 | /* |
| 4757 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 4758 | * under the tasklist_lock and we read it here after we added the child |
| 4759 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 4760 | * yet in the tasklist when we walked through it from |
| 4761 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 4762 | * should be visible now due to the paired locking and barriers implied |
| 4763 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 4764 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 4765 | * lock on fork. |
| 4766 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4767 | if (use_task_css_set_links) { |
| 4768 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4769 | task_lock(child); |
| 4770 | if (list_empty(&child->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4771 | list_add(&child->cg_list, &task_css_set(child)->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4772 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4773 | write_unlock(&css_set_lock); |
| 4774 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4775 | |
| 4776 | /* |
| 4777 | * Call ss->fork(). This must happen after @child is linked on |
| 4778 | * css_set; otherwise, @child might change state between ->fork() |
| 4779 | * and addition to css_set. |
| 4780 | */ |
| 4781 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4782 | for_each_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4783 | if (ss->fork) |
| 4784 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4785 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4786 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4787 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4788 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4789 | * cgroup_exit - detach cgroup from exiting task |
| 4790 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4791 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4792 | * |
| 4793 | * Description: Detach cgroup from @tsk and release it. |
| 4794 | * |
| 4795 | * Note that cgroups marked notify_on_release force every task in |
| 4796 | * them to take the global cgroup_mutex mutex when exiting. |
| 4797 | * This could impact scaling on very large systems. Be reluctant to |
| 4798 | * use notify_on_release cgroups where very high task exit scaling |
| 4799 | * is required on large systems. |
| 4800 | * |
| 4801 | * the_top_cgroup_hack: |
| 4802 | * |
| 4803 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 4804 | * |
| 4805 | * We call cgroup_exit() while the task is still competent to |
| 4806 | * handle notify_on_release(), then leave the task attached to the |
| 4807 | * root cgroup in each hierarchy for the remainder of its exit. |
| 4808 | * |
| 4809 | * To do this properly, we would increment the reference count on |
| 4810 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 4811 | * code we would add a second cgroup function call, to drop that |
| 4812 | * reference. This would just create an unnecessary hot spot on |
| 4813 | * the top_cgroup reference count, to no avail. |
| 4814 | * |
| 4815 | * Normally, holding a reference to a cgroup without bumping its |
| 4816 | * count is unsafe. The cgroup could go away, or someone could |
| 4817 | * attach us to a different cgroup, decrementing the count on |
| 4818 | * the first cgroup that we never incremented. But in this case, |
| 4819 | * 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] | 4820 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 4821 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4822 | */ |
| 4823 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 4824 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4825 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4826 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4827 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4828 | |
| 4829 | /* |
| 4830 | * Unlink from the css_set task list if necessary. |
| 4831 | * Optimistically check cg_list before taking |
| 4832 | * css_set_lock |
| 4833 | */ |
| 4834 | if (!list_empty(&tsk->cg_list)) { |
| 4835 | write_lock(&css_set_lock); |
| 4836 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4837 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4838 | write_unlock(&css_set_lock); |
| 4839 | } |
| 4840 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4841 | /* Reassign the task to the init_css_set. */ |
| 4842 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4843 | cset = task_css_set(tsk); |
| 4844 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4845 | |
| 4846 | if (run_callbacks && need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4847 | /* see cgroup_post_fork() for details */ |
| 4848 | for_each_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4849 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4850 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 4851 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4852 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4853 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4854 | } |
| 4855 | } |
| 4856 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4857 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4858 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4859 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4860 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4861 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4862 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4863 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4864 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4865 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4866 | /* |
| 4867 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4868 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4869 | * it now |
| 4870 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4871 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4872 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4873 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4874 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4875 | list_empty(&cgrp->release_list)) { |
| 4876 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4877 | need_schedule_work = 1; |
| 4878 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4879 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4880 | if (need_schedule_work) |
| 4881 | schedule_work(&release_agent_work); |
| 4882 | } |
| 4883 | } |
| 4884 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4885 | /* |
| 4886 | * Notify userspace when a cgroup is released, by running the |
| 4887 | * configured release agent with the name of the cgroup (path |
| 4888 | * relative to the root of cgroup file system) as the argument. |
| 4889 | * |
| 4890 | * Most likely, this user command will try to rmdir this cgroup. |
| 4891 | * |
| 4892 | * This races with the possibility that some other task will be |
| 4893 | * attached to this cgroup before it is removed, or that some other |
| 4894 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 4895 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 4896 | * unused, and this cgroup will be reprieved from its death sentence, |
| 4897 | * to continue to serve a useful existence. Next time it's released, |
| 4898 | * we will get notified again, if it still has 'notify_on_release' set. |
| 4899 | * |
| 4900 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 4901 | * means only wait until the task is successfully execve()'d. The |
| 4902 | * separate release agent task is forked by call_usermodehelper(), |
| 4903 | * then control in this thread returns here, without waiting for the |
| 4904 | * release agent task. We don't bother to wait because the caller of |
| 4905 | * this routine has no use for the exit status of the release agent |
| 4906 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4907 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4908 | static void cgroup_release_agent(struct work_struct *work) |
| 4909 | { |
| 4910 | BUG_ON(work != &release_agent_work); |
| 4911 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4912 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4913 | while (!list_empty(&release_list)) { |
| 4914 | char *argv[3], *envp[3]; |
| 4915 | int i; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4916 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4917 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4918 | struct cgroup, |
| 4919 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4920 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4921 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4922 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4923 | if (!pathbuf) |
| 4924 | goto continue_free; |
| 4925 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 4926 | goto continue_free; |
| 4927 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 4928 | if (!agentbuf) |
| 4929 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4930 | |
| 4931 | i = 0; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4932 | argv[i++] = agentbuf; |
| 4933 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4934 | argv[i] = NULL; |
| 4935 | |
| 4936 | i = 0; |
| 4937 | /* minimal command environment */ |
| 4938 | envp[i++] = "HOME=/"; |
| 4939 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 4940 | envp[i] = NULL; |
| 4941 | |
| 4942 | /* Drop the lock while we invoke the usermode helper, |
| 4943 | * since the exec could involve hitting disk and hence |
| 4944 | * be a slow process */ |
| 4945 | mutex_unlock(&cgroup_mutex); |
| 4946 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4947 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4948 | continue_free: |
| 4949 | kfree(pathbuf); |
| 4950 | kfree(agentbuf); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4951 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4952 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4953 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4954 | mutex_unlock(&cgroup_mutex); |
| 4955 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4956 | |
| 4957 | static int __init cgroup_disable(char *str) |
| 4958 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4959 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4960 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4961 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4962 | |
| 4963 | while ((token = strsep(&str, ",")) != NULL) { |
| 4964 | if (!*token) |
| 4965 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4966 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4967 | for_each_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4968 | if (!strcmp(token, ss->name)) { |
| 4969 | ss->disabled = 1; |
| 4970 | printk(KERN_INFO "Disabling %s control group" |
| 4971 | " subsystem\n", ss->name); |
| 4972 | break; |
| 4973 | } |
| 4974 | } |
| 4975 | } |
| 4976 | return 1; |
| 4977 | } |
| 4978 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4979 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4980 | /** |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame^] | 4981 | * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4982 | * @dentry: directory dentry of interest |
| 4983 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4984 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame^] | 4985 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 4986 | * to get the corresponding css and return it. If such css doesn't exist |
| 4987 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4988 | */ |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame^] | 4989 | struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry, |
| 4990 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4991 | { |
| 4992 | struct cgroup *cgrp; |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame^] | 4993 | struct cgroup_subsys_state *css; |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4994 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4995 | /* is @dentry a cgroup dir? */ |
| 4996 | if (!dentry->d_inode || |
| 4997 | dentry->d_inode->i_op != &cgroup_dir_inode_operations) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4998 | return ERR_PTR(-EBADF); |
| 4999 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame^] | 5000 | rcu_read_lock(); |
| 5001 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5002 | cgrp = __d_cgrp(dentry); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame^] | 5003 | css = cgroup_css(cgrp, ss); |
| 5004 | |
| 5005 | if (!css || !css_tryget(css)) |
| 5006 | css = ERR_PTR(-ENOENT); |
| 5007 | |
| 5008 | rcu_read_unlock(); |
| 5009 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5010 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5011 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5012 | /** |
| 5013 | * css_from_id - lookup css by id |
| 5014 | * @id: the cgroup id |
| 5015 | * @ss: cgroup subsys to be looked into |
| 5016 | * |
| 5017 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5018 | * Should be called under rcu_read_lock(). |
| 5019 | */ |
| 5020 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5021 | { |
| 5022 | struct cgroup *cgrp; |
| 5023 | |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 5024 | cgroup_assert_mutex_or_rcu_locked(); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5025 | |
| 5026 | cgrp = idr_find(&ss->root->cgroup_idr, id); |
| 5027 | if (cgrp) |
Tejun Heo | d162596 | 2013-08-27 14:27:23 -0400 | [diff] [blame] | 5028 | return cgroup_css(cgrp, ss); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5029 | return NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5030 | } |
| 5031 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5032 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5033 | static struct cgroup_subsys_state * |
| 5034 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5035 | { |
| 5036 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5037 | |
| 5038 | if (!css) |
| 5039 | return ERR_PTR(-ENOMEM); |
| 5040 | |
| 5041 | return css; |
| 5042 | } |
| 5043 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5044 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5045 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5046 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5047 | } |
| 5048 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5049 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 5050 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5051 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5052 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5053 | } |
| 5054 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5055 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 5056 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5057 | { |
| 5058 | return (u64)(unsigned long)current->cgroups; |
| 5059 | } |
| 5060 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5061 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5062 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5063 | { |
| 5064 | u64 count; |
| 5065 | |
| 5066 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5067 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5068 | rcu_read_unlock(); |
| 5069 | return count; |
| 5070 | } |
| 5071 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5072 | 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] | 5073 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5074 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5075 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5076 | |
| 5077 | read_lock(&css_set_lock); |
| 5078 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5079 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5080 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5081 | struct cgroup *c = link->cgrp; |
| 5082 | const char *name; |
| 5083 | |
| 5084 | if (c->dentry) |
| 5085 | name = c->dentry->d_name.name; |
| 5086 | else |
| 5087 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5088 | seq_printf(seq, "Root %d group %s\n", |
| 5089 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5090 | } |
| 5091 | rcu_read_unlock(); |
| 5092 | read_unlock(&css_set_lock); |
| 5093 | return 0; |
| 5094 | } |
| 5095 | |
| 5096 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5097 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5098 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5099 | struct cgroup_subsys_state *css = seq_css(seq); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5100 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5101 | |
| 5102 | read_lock(&css_set_lock); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5103 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5104 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5105 | struct task_struct *task; |
| 5106 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5107 | seq_printf(seq, "css_set %p\n", cset); |
| 5108 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5109 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5110 | seq_puts(seq, " ...\n"); |
| 5111 | break; |
| 5112 | } else { |
| 5113 | seq_printf(seq, " task %d\n", |
| 5114 | task_pid_vnr(task)); |
| 5115 | } |
| 5116 | } |
| 5117 | } |
| 5118 | read_unlock(&css_set_lock); |
| 5119 | return 0; |
| 5120 | } |
| 5121 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5122 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5123 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5124 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5125 | } |
| 5126 | |
| 5127 | static struct cftype debug_files[] = { |
| 5128 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5129 | .name = "taskcount", |
| 5130 | .read_u64 = debug_taskcount_read, |
| 5131 | }, |
| 5132 | |
| 5133 | { |
| 5134 | .name = "current_css_set", |
| 5135 | .read_u64 = current_css_set_read, |
| 5136 | }, |
| 5137 | |
| 5138 | { |
| 5139 | .name = "current_css_set_refcount", |
| 5140 | .read_u64 = current_css_set_refcount_read, |
| 5141 | }, |
| 5142 | |
| 5143 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5144 | .name = "current_css_set_cg_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5145 | .seq_show = current_css_set_cg_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5146 | }, |
| 5147 | |
| 5148 | { |
| 5149 | .name = "cgroup_css_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5150 | .seq_show = cgroup_css_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5151 | }, |
| 5152 | |
| 5153 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5154 | .name = "releasable", |
| 5155 | .read_u64 = releasable_read, |
| 5156 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5157 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5158 | { } /* terminate */ |
| 5159 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5160 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5161 | struct cgroup_subsys debug_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5162 | .css_alloc = debug_css_alloc, |
| 5163 | .css_free = debug_css_free, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5164 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5165 | }; |
| 5166 | #endif /* CONFIG_CGROUP_DEBUG */ |