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 | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 71 | * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file |
| 72 | * creation/removal and hierarchy changing operations including cgroup |
| 73 | * creation, removal, css association and controller rebinding. This outer |
| 74 | * lock is needed mainly to resolve the circular dependency between kernfs |
| 75 | * active ref and cgroup_mutex. cgroup_tree_mutex nests above both. |
| 76 | */ |
| 77 | static DEFINE_MUTEX(cgroup_tree_mutex); |
| 78 | |
| 79 | /* |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 80 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 81 | * hierarchy must be performed while holding it. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 82 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 83 | #ifdef CONFIG_PROVE_RCU |
| 84 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 85 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 86 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 87 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 88 | #endif |
| 89 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 90 | /* |
| 91 | * Protects cgroup_subsys->release_agent_path. Modifying it also requires |
| 92 | * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock. |
| 93 | */ |
| 94 | static DEFINE_SPINLOCK(release_agent_path_lock); |
| 95 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 96 | #define cgroup_assert_mutexes_or_rcu_locked() \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 97 | rcu_lockdep_assert(rcu_read_lock_held() || \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 98 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 99 | lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 100 | "cgroup_[tree_]mutex or RCU read lock required"); |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 101 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 102 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 103 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 104 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 105 | * destruction work items don't end up filling up max_active of system_wq |
| 106 | * which may lead to deadlock. |
| 107 | */ |
| 108 | static struct workqueue_struct *cgroup_destroy_wq; |
| 109 | |
| 110 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 111 | * pidlist destructions need to be flushed on cgroup destruction. Use a |
| 112 | * separate workqueue as flush domain. |
| 113 | */ |
| 114 | static struct workqueue_struct *cgroup_pidlist_destroy_wq; |
| 115 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 116 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 117 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 118 | static struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 119 | #include <linux/cgroup_subsys.h> |
| 120 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 121 | #undef SUBSYS |
| 122 | |
| 123 | /* array of cgroup subsystem names */ |
| 124 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 125 | static const char *cgroup_subsys_name[] = { |
| 126 | #include <linux/cgroup_subsys.h> |
| 127 | }; |
| 128 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 129 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 130 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 131 | * The dummy hierarchy, reserved for the subsystems that are otherwise |
| 132 | * unattached - it never has more than a single cgroup, and all tasks are |
| 133 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 134 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 135 | static struct cgroupfs_root cgroup_dummy_root; |
| 136 | |
| 137 | /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ |
| 138 | static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 139 | |
| 140 | /* The list of hierarchy roots */ |
| 141 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 142 | static LIST_HEAD(cgroup_roots); |
| 143 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 144 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 145 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 146 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 147 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 148 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 149 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 150 | /* |
| 151 | * Assign a monotonically increasing serial number to cgroups. It |
| 152 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 153 | * numbers. Also, as cgroups are always appended to the parent's |
| 154 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 155 | * the ascending serial number order on the list. Protected by |
| 156 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 157 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 158 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 159 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 160 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 161 | * check for fork/exit handlers to call. This avoids us having to do |
| 162 | * extra work in the fork/exit path if none of the subsystems need to |
| 163 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 164 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 165 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 166 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 167 | static struct cftype cgroup_base_files[]; |
| 168 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 169 | static void cgroup_destroy_css_killed(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 170 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 171 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 172 | bool is_add); |
Tejun Heo | e605b36 | 2013-11-27 18:16:21 -0500 | [diff] [blame] | 173 | static int cgroup_file_release(struct inode *inode, struct file *file); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 174 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 175 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 176 | /** |
| 177 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 178 | * @cgrp: the cgroup of interest |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 179 | * @ss: the subsystem of interest (%NULL returns the dummy_css) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 180 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 181 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 182 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 183 | * the caller is responsible for pinning the returned css if it wants to |
| 184 | * keep accessing it outside the said locks. This function may return |
| 185 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 186 | */ |
| 187 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 188 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 189 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 190 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 191 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 192 | lockdep_is_held(&cgroup_tree_mutex) || |
| 193 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 194 | else |
| 195 | return &cgrp->dummy_css; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 196 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 197 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 198 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 199 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 200 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 201 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 204 | /** |
| 205 | * cgroup_is_descendant - test ancestry |
| 206 | * @cgrp: the cgroup to be tested |
| 207 | * @ancestor: possible ancestor of @cgrp |
| 208 | * |
| 209 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 210 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 211 | * and @ancestor are accessible. |
| 212 | */ |
| 213 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 214 | { |
| 215 | while (cgrp) { |
| 216 | if (cgrp == ancestor) |
| 217 | return true; |
| 218 | cgrp = cgrp->parent; |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 223 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 224 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 225 | { |
| 226 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 227 | (1 << CGRP_RELEASABLE) | |
| 228 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 229 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 232 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 233 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 234 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 237 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 238 | * for_each_css - iterate all css's of a cgroup |
| 239 | * @css: the iteration cursor |
| 240 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 241 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 242 | * |
| 243 | * Should be called under cgroup_mutex. |
| 244 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 245 | #define for_each_css(css, ssid, cgrp) \ |
| 246 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 247 | if (!((css) = rcu_dereference_check( \ |
| 248 | (cgrp)->subsys[(ssid)], \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 249 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 250 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 251 | else |
| 252 | |
| 253 | /** |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 254 | * for_each_subsys - iterate all enabled cgroup subsystems |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 255 | * @ss: the iteration cursor |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 256 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 257 | */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 258 | #define for_each_subsys(ss, ssid) \ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 259 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \ |
| 260 | (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 261 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 262 | /* iterate across the active hierarchies */ |
| 263 | #define for_each_active_root(root) \ |
| 264 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 265 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 266 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 267 | { |
| 268 | return dentry->d_fsdata; |
| 269 | } |
| 270 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 271 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 272 | { |
| 273 | return dentry->d_fsdata; |
| 274 | } |
| 275 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 276 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 277 | { |
| 278 | return __d_cfe(dentry)->type; |
| 279 | } |
| 280 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 281 | /** |
| 282 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 283 | * @cgrp: the cgroup to be checked for liveness |
| 284 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 285 | * On success, returns true; the mutex should be later unlocked. On |
| 286 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 287 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 288 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 289 | { |
| 290 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 291 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 292 | mutex_unlock(&cgroup_mutex); |
| 293 | return false; |
| 294 | } |
| 295 | return true; |
| 296 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 297 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 298 | /* the list of cgroups eligible for automatic release. Protected by |
| 299 | * release_list_lock */ |
| 300 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 301 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 302 | static void cgroup_release_agent(struct work_struct *work); |
| 303 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 304 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 305 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 306 | /* |
| 307 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 308 | * belong to different cgroups on different hierarchies. In the other |
| 309 | * direction, a css_set is naturally associated with multiple cgroups. |
| 310 | * This M:N relationship is represented by the following link structure |
| 311 | * which exists for each association and allows traversing the associations |
| 312 | * from both sides. |
| 313 | */ |
| 314 | struct cgrp_cset_link { |
| 315 | /* the cgroup and css_set this link associates */ |
| 316 | struct cgroup *cgrp; |
| 317 | struct css_set *cset; |
| 318 | |
| 319 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 320 | struct list_head cset_link; |
| 321 | |
| 322 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 323 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | /* The default css_set - used by init and its children prior to any |
| 327 | * hierarchies being mounted. It contains a pointer to the root state |
| 328 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 329 | * reference-counted, to improve performance when child cgroups |
| 330 | * haven't been created. |
| 331 | */ |
| 332 | |
| 333 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 334 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 335 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 336 | /* |
| 337 | * css_set_lock protects the list of css_set objects, and the chain of |
| 338 | * tasks off each css_set. Nests outside task->alloc_lock due to |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 339 | * css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 340 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 341 | static DEFINE_RWLOCK(css_set_lock); |
| 342 | static int css_set_count; |
| 343 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 344 | /* |
| 345 | * hash table for cgroup groups. This improves the performance to find |
| 346 | * an existing css_set. This hash doesn't (currently) take into |
| 347 | * account cgroups in empty hierarchies. |
| 348 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 349 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 350 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 351 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 352 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 353 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 354 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 355 | struct cgroup_subsys *ss; |
| 356 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 357 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 358 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 359 | key += (unsigned long)css[i]; |
| 360 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 361 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 362 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 365 | /* |
| 366 | * 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] | 367 | * until after the first call to css_task_iter_start(). This reduces the |
| 368 | * fork()/exit() overhead for people who have cgroups compiled into their |
| 369 | * kernel but not actually in use. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 370 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 371 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 372 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 373 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 374 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 375 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 376 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 377 | /* |
| 378 | * Ensure that the refcount doesn't hit zero while any readers |
| 379 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 380 | * rwlock |
| 381 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 382 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 383 | return; |
| 384 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 385 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 386 | write_unlock(&css_set_lock); |
| 387 | return; |
| 388 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 389 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 390 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 391 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 392 | css_set_count--; |
| 393 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 394 | 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] | 395 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 396 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 397 | list_del(&link->cset_link); |
| 398 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 399 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 400 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 401 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 402 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 403 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 404 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 405 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 406 | |
| 407 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 408 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 409 | |
| 410 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 411 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* |
| 415 | * refcounted get/put for css_set objects |
| 416 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 417 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 418 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 419 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 422 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 423 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 424 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 427 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 428 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 429 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 432 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 433 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 434 | * @cset: candidate css_set being tested |
| 435 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 436 | * @new_cgrp: cgroup that's being entered by the task |
| 437 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 438 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 439 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 440 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 441 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 442 | static bool compare_css_sets(struct css_set *cset, |
| 443 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 444 | struct cgroup *new_cgrp, |
| 445 | struct cgroup_subsys_state *template[]) |
| 446 | { |
| 447 | struct list_head *l1, *l2; |
| 448 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 449 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 450 | /* Not all subsystems matched */ |
| 451 | return false; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Compare cgroup pointers in order to distinguish between |
| 456 | * different cgroups in heirarchies with no subsystems. We |
| 457 | * could get by with just this check alone (and skip the |
| 458 | * memcmp above) but on most setups the memcmp check will |
| 459 | * avoid the need for this more expensive check on almost all |
| 460 | * candidates. |
| 461 | */ |
| 462 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 463 | l1 = &cset->cgrp_links; |
| 464 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 465 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 466 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 467 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 468 | |
| 469 | l1 = l1->next; |
| 470 | l2 = l2->next; |
| 471 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 472 | if (l1 == &cset->cgrp_links) { |
| 473 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 474 | break; |
| 475 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 476 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 477 | } |
| 478 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 479 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 480 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 481 | cgrp1 = link1->cgrp; |
| 482 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 483 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 484 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 485 | |
| 486 | /* |
| 487 | * If this hierarchy is the hierarchy of the cgroup |
| 488 | * that's changing, then we need to check that this |
| 489 | * css_set points to the new cgroup; if it's any other |
| 490 | * hierarchy, then this css_set should point to the |
| 491 | * same cgroup as the old css_set. |
| 492 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 493 | if (cgrp1->root == new_cgrp->root) { |
| 494 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 495 | return false; |
| 496 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 497 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 498 | return false; |
| 499 | } |
| 500 | } |
| 501 | return true; |
| 502 | } |
| 503 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 504 | /** |
| 505 | * find_existing_css_set - init css array and find the matching css_set |
| 506 | * @old_cset: the css_set that we're using before the cgroup transition |
| 507 | * @cgrp: the cgroup that we're moving into |
| 508 | * @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] | 509 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 510 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 511 | struct cgroup *cgrp, |
| 512 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 513 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 514 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 515 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 516 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 517 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 518 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 519 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 520 | /* |
| 521 | * Build the set of subsystem state objects that we want to see in the |
| 522 | * new css_set. while subsystems can change globally, the entries here |
| 523 | * won't change, so no need for locking. |
| 524 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 525 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 526 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 527 | /* Subsystem is in this hierarchy. So we want |
| 528 | * the subsystem state from the new |
| 529 | * cgroup */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 530 | template[i] = cgroup_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 531 | } else { |
| 532 | /* Subsystem is not in this hierarchy, so we |
| 533 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 534 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 538 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 539 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 540 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 541 | continue; |
| 542 | |
| 543 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 544 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 545 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 546 | |
| 547 | /* No existing cgroup group matched */ |
| 548 | return NULL; |
| 549 | } |
| 550 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 551 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 552 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 553 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 554 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 555 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 556 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 557 | kfree(link); |
| 558 | } |
| 559 | } |
| 560 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 561 | /** |
| 562 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 563 | * @count: the number of links to allocate |
| 564 | * @tmp_links: list_head the allocated links are put on |
| 565 | * |
| 566 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 567 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 568 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 569 | 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] | 570 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 571 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 572 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 573 | |
| 574 | INIT_LIST_HEAD(tmp_links); |
| 575 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 576 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 577 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 578 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 579 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 580 | return -ENOMEM; |
| 581 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 582 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 583 | } |
| 584 | return 0; |
| 585 | } |
| 586 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 587 | /** |
| 588 | * 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] | 589 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 590 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 591 | * @cgrp: the destination cgroup |
| 592 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 593 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 594 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 595 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 596 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 597 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 598 | BUG_ON(list_empty(tmp_links)); |
| 599 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 600 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 601 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 602 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 603 | /* |
| 604 | * Always add links to the tail of the list so that the list |
| 605 | * is sorted by order of hierarchy creation |
| 606 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 607 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 610 | /** |
| 611 | * find_css_set - return a new css_set with one cgroup updated |
| 612 | * @old_cset: the baseline css_set |
| 613 | * @cgrp: the cgroup to be updated |
| 614 | * |
| 615 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 616 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 617 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 618 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 619 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 620 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 621 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 622 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 623 | struct list_head tmp_links; |
| 624 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 625 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 626 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 627 | lockdep_assert_held(&cgroup_mutex); |
| 628 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 629 | /* First see if we already have a cgroup group that matches |
| 630 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 631 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 632 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 633 | if (cset) |
| 634 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 635 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 636 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 637 | if (cset) |
| 638 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 639 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 640 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 641 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 642 | return NULL; |
| 643 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 644 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 645 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 646 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 647 | return NULL; |
| 648 | } |
| 649 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 650 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 651 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 652 | INIT_LIST_HEAD(&cset->tasks); |
| 653 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 654 | |
| 655 | /* Copy the set of subsystem state objects generated in |
| 656 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 657 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 658 | |
| 659 | write_lock(&css_set_lock); |
| 660 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 661 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 662 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 663 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 664 | if (c->root == cgrp->root) |
| 665 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 666 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 667 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 668 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 669 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 670 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 671 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 672 | |
| 673 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 674 | key = css_set_hash(cset->subsys); |
| 675 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 676 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 677 | write_unlock(&css_set_lock); |
| 678 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 679 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 682 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 683 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 684 | * called with cgroup_mutex held. |
| 685 | */ |
| 686 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 687 | struct cgroupfs_root *root) |
| 688 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 689 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 690 | struct cgroup *res = NULL; |
| 691 | |
| 692 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 693 | read_lock(&css_set_lock); |
| 694 | /* |
| 695 | * No need to lock the task - since we hold cgroup_mutex the |
| 696 | * task can't change groups, so the only thing that can happen |
| 697 | * is that it exits and its css is set back to init_css_set. |
| 698 | */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 699 | cset = task_css_set(task); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 700 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 701 | res = &root->top_cgroup; |
| 702 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 703 | struct cgrp_cset_link *link; |
| 704 | |
| 705 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 706 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 707 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 708 | if (c->root == root) { |
| 709 | res = c; |
| 710 | break; |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | read_unlock(&css_set_lock); |
| 715 | BUG_ON(!res); |
| 716 | return res; |
| 717 | } |
| 718 | |
| 719 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 720 | * There is one global cgroup mutex. We also require taking |
| 721 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 722 | * See "The task_lock() exception", at the end of this comment. |
| 723 | * |
| 724 | * A task must hold cgroup_mutex to modify cgroups. |
| 725 | * |
| 726 | * Any task can increment and decrement the count field without lock. |
| 727 | * So in general, code holding cgroup_mutex can't rely on the count |
| 728 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 729 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 730 | * means that no tasks are currently attached, therefore there is no |
| 731 | * way a task attached to that cgroup can fork (the other way to |
| 732 | * increment the count). So code holding cgroup_mutex can safely |
| 733 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 734 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 735 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 736 | * needs that mutex. |
| 737 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 738 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 739 | * (usually) take cgroup_mutex. These are the two most performance |
| 740 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 741 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 742 | * 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] | 743 | * to the release agent with the name of the cgroup (path relative to |
| 744 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 745 | * |
| 746 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 747 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 748 | * tasks in the system use _some_ cgroup, and since there is always at |
| 749 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 750 | * always has either children cgroups and/or using tasks. So we don't |
| 751 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 752 | * |
| 753 | * The task_lock() exception |
| 754 | * |
| 755 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 756 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 757 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 758 | * several performance critical places that need to reference |
| 759 | * task->cgroup without the expense of grabbing a system global |
| 760 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 761 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 762 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 763 | * the task_struct routinely used for such matters. |
| 764 | * |
| 765 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 766 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 767 | */ |
| 768 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 769 | /* |
| 770 | * A couple of forward declarations required, due to cyclic reference loop: |
| 771 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 772 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 773 | * -> cgroup_mkdir. |
| 774 | */ |
| 775 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 776 | 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] | 777 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 778 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 779 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 780 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 781 | |
| 782 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 783 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 784 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 785 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 786 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 787 | 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] | 788 | { |
| 789 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 790 | |
| 791 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 792 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 793 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 794 | inode->i_uid = current_fsuid(); |
| 795 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 796 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 797 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 798 | } |
| 799 | return inode; |
| 800 | } |
| 801 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 802 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 803 | { |
| 804 | struct cgroup_name *name; |
| 805 | |
| 806 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 807 | if (!name) |
| 808 | return NULL; |
| 809 | strcpy(name->name, dentry->d_name.name); |
| 810 | return name; |
| 811 | } |
| 812 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 813 | static void cgroup_free_fn(struct work_struct *work) |
| 814 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 815 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 816 | |
| 817 | mutex_lock(&cgroup_mutex); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 818 | cgrp->root->number_of_cgroups--; |
| 819 | mutex_unlock(&cgroup_mutex); |
| 820 | |
| 821 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 822 | * We get a ref to the parent's dentry, and put the ref when |
| 823 | * this cgroup is being freed, so it's guaranteed that the |
| 824 | * parent won't be destroyed before its children. |
| 825 | */ |
| 826 | dput(cgrp->parent->dentry); |
| 827 | |
| 828 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 829 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 830 | * created the cgroup. This will free cgrp->root, if we are |
| 831 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 832 | */ |
| 833 | deactivate_super(cgrp->root->sb); |
| 834 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 835 | cgroup_pidlist_destroy_all(cgrp); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 836 | |
| 837 | simple_xattrs_free(&cgrp->xattrs); |
| 838 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 839 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 840 | kfree(cgrp); |
| 841 | } |
| 842 | |
| 843 | static void cgroup_free_rcu(struct rcu_head *head) |
| 844 | { |
| 845 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 846 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 847 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 848 | queue_work(cgroup_destroy_wq, &cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 849 | } |
| 850 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 851 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 852 | { |
| 853 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 854 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 855 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 856 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 857 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | c1a7150 | 2013-12-17 11:13:39 +0800 | [diff] [blame] | 858 | |
| 859 | /* |
| 860 | * XXX: cgrp->id is only used to look up css's. As cgroup |
| 861 | * and css's lifetimes will be decoupled, it should be made |
| 862 | * per-subsystem and moved to css->id so that lookups are |
| 863 | * successful until the target css is released. |
| 864 | */ |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 865 | mutex_lock(&cgroup_mutex); |
Li Zefan | c1a7150 | 2013-12-17 11:13:39 +0800 | [diff] [blame] | 866 | idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 867 | mutex_unlock(&cgroup_mutex); |
Li Zefan | c1a7150 | 2013-12-17 11:13:39 +0800 | [diff] [blame] | 868 | cgrp->id = -1; |
| 869 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 870 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 871 | } else { |
| 872 | struct cfent *cfe = __d_cfe(dentry); |
| 873 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 874 | |
| 875 | WARN_ONCE(!list_empty(&cfe->node) && |
| 876 | cgrp != &cgrp->root->top_cgroup, |
| 877 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 878 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 879 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 880 | } |
| 881 | iput(inode); |
| 882 | } |
| 883 | |
| 884 | static void remove_dir(struct dentry *d) |
| 885 | { |
| 886 | struct dentry *parent = dget(d->d_parent); |
| 887 | |
| 888 | d_delete(d); |
| 889 | simple_rmdir(parent->d_inode, d); |
| 890 | dput(parent); |
| 891 | } |
| 892 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 893 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 894 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 895 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 896 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 897 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 898 | lockdep_assert_held(&cgroup_tree_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 899 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 900 | /* |
| 901 | * If we're doing cleanup due to failure of cgroup_create(), |
| 902 | * the corresponding @cfe may not exist. |
| 903 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 904 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 905 | struct dentry *d = cfe->dentry; |
| 906 | |
| 907 | if (cft && cfe->type != cft) |
| 908 | continue; |
| 909 | |
| 910 | dget(d); |
| 911 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 912 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 913 | list_del_init(&cfe->node); |
| 914 | dput(d); |
| 915 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 916 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 917 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 920 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 921 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 922 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 923 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 924 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 925 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 926 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 927 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 928 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 929 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 930 | for_each_subsys(ss, i) { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 931 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 932 | |
| 933 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 934 | continue; |
| 935 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 936 | cgroup_addrm_files(cgrp, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 937 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | /* |
| 941 | * NOTE : the dentry must have been dget()'ed |
| 942 | */ |
| 943 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 944 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 945 | struct dentry *parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 946 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 947 | parent = dentry->d_parent; |
| 948 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 949 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 950 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 951 | spin_unlock(&dentry->d_lock); |
| 952 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 953 | remove_dir(dentry); |
| 954 | } |
| 955 | |
| 956 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 957 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 958 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 959 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 960 | struct cgroup_subsys *ss; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 961 | int i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 962 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 963 | lockdep_assert_held(&cgroup_tree_mutex); |
| 964 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 965 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 966 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 967 | for_each_subsys(ss, i) |
| 968 | if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root) |
| 969 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 970 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 971 | ret = cgroup_populate_dir(cgrp, added_mask); |
| 972 | if (ret) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 973 | return ret; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 974 | |
| 975 | /* |
| 976 | * Nothing can fail from this point on. Remove files for the |
| 977 | * removed subsystems and rebind each subsystem. |
| 978 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 979 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 980 | cgroup_clear_dir(cgrp, removed_mask); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 981 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 982 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 983 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 984 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 985 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 986 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 987 | /* We're binding this subsystem to this hierarchy */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 988 | BUG_ON(cgroup_css(cgrp, ss)); |
| 989 | BUG_ON(!cgroup_css(cgroup_dummy_top, ss)); |
| 990 | BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 991 | |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 992 | rcu_assign_pointer(cgrp->subsys[i], |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 993 | cgroup_css(cgroup_dummy_top, ss)); |
| 994 | cgroup_css(cgrp, ss)->cgroup = cgrp; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 995 | |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 996 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 997 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 998 | ss->bind(cgroup_css(cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 999 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1000 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1001 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1002 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1003 | /* We're removing this subsystem */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1004 | BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss)); |
| 1005 | BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1006 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1007 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1008 | ss->bind(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1009 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1010 | cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1011 | RCU_INIT_POINTER(cgrp->subsys[i], NULL); |
| 1012 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1013 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1014 | root->subsys_mask &= ~bit; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1015 | } |
| 1016 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1017 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1018 | /* |
| 1019 | * Mark @root has finished binding subsystems. @root->subsys_mask |
| 1020 | * now matches the bound subsystems. |
| 1021 | */ |
| 1022 | root->flags |= CGRP_ROOT_SUBSYS_BOUND; |
| 1023 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1024 | return 0; |
| 1025 | } |
| 1026 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1027 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1028 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1029 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1030 | struct cgroup_subsys *ss; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1031 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1032 | |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1033 | for_each_subsys(ss, ssid) |
| 1034 | if (root->subsys_mask & (1 << ssid)) |
| 1035 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1036 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1037 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1038 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1039 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1040 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1041 | seq_puts(seq, ",xattr"); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1042 | |
| 1043 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1044 | if (strlen(root->release_agent_path)) |
| 1045 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1046 | spin_unlock(&release_agent_path_lock); |
| 1047 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1048 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1049 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1050 | if (strlen(root->name)) |
| 1051 | seq_printf(seq, ",name=%s", root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1056 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1057 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1058 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1059 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1060 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1061 | /* User explicitly requested empty subsystem */ |
| 1062 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1063 | |
| 1064 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1065 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1066 | }; |
| 1067 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1068 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1069 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1070 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1071 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1072 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1073 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1074 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1075 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1076 | char *token, *o = data; |
| 1077 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1078 | unsigned long mask = (unsigned long)-1; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1079 | struct cgroup_subsys *ss; |
| 1080 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1081 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1082 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1083 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1084 | #ifdef CONFIG_CPUSETS |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1085 | mask = ~(1UL << cpuset_cgrp_id); |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1086 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1087 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1088 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1089 | |
| 1090 | while ((token = strsep(&o, ",")) != NULL) { |
| 1091 | if (!*token) |
| 1092 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1093 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1094 | /* Explicitly have no subsystems */ |
| 1095 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1096 | continue; |
| 1097 | } |
| 1098 | if (!strcmp(token, "all")) { |
| 1099 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1100 | if (one_ss) |
| 1101 | return -EINVAL; |
| 1102 | all_ss = true; |
| 1103 | continue; |
| 1104 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1105 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1106 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1107 | continue; |
| 1108 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1109 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1110 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1111 | continue; |
| 1112 | } |
| 1113 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1114 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1115 | continue; |
| 1116 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1117 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1118 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1119 | continue; |
| 1120 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1121 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1122 | /* Specifying two release agents is forbidden */ |
| 1123 | if (opts->release_agent) |
| 1124 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1125 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1126 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1127 | if (!opts->release_agent) |
| 1128 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1129 | continue; |
| 1130 | } |
| 1131 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1132 | const char *name = token + 5; |
| 1133 | /* Can't specify an empty name */ |
| 1134 | if (!strlen(name)) |
| 1135 | return -EINVAL; |
| 1136 | /* Must match [\w.-]+ */ |
| 1137 | for (i = 0; i < strlen(name); i++) { |
| 1138 | char c = name[i]; |
| 1139 | if (isalnum(c)) |
| 1140 | continue; |
| 1141 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1142 | continue; |
| 1143 | return -EINVAL; |
| 1144 | } |
| 1145 | /* Specifying two names is forbidden */ |
| 1146 | if (opts->name) |
| 1147 | return -EINVAL; |
| 1148 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1149 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1150 | GFP_KERNEL); |
| 1151 | if (!opts->name) |
| 1152 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1153 | |
| 1154 | continue; |
| 1155 | } |
| 1156 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1157 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1158 | if (strcmp(token, ss->name)) |
| 1159 | continue; |
| 1160 | if (ss->disabled) |
| 1161 | continue; |
| 1162 | |
| 1163 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1164 | if (all_ss) |
| 1165 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1166 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1167 | one_ss = true; |
| 1168 | |
| 1169 | break; |
| 1170 | } |
| 1171 | if (i == CGROUP_SUBSYS_COUNT) |
| 1172 | return -ENOENT; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1177 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1178 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1179 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1180 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1181 | for_each_subsys(ss, i) |
| 1182 | if (!ss->disabled) |
| 1183 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1184 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1185 | /* Consistency checks */ |
| 1186 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1187 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1188 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1189 | |
| 1190 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1191 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1192 | return -EINVAL; |
| 1193 | } |
| 1194 | |
| 1195 | if (opts->cpuset_clone_children) { |
| 1196 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1197 | return -EINVAL; |
| 1198 | } |
| 1199 | } |
| 1200 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1201 | /* |
| 1202 | * Option noprefix was introduced just for backward compatibility |
| 1203 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1204 | * the cpuset subsystem. |
| 1205 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1206 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1207 | return -EINVAL; |
| 1208 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1209 | |
| 1210 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1211 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1212 | return -EINVAL; |
| 1213 | |
| 1214 | /* |
| 1215 | * We either have to specify by name or by subsystems. (So all |
| 1216 | * empty hierarchies must have a name). |
| 1217 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1218 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1219 | return -EINVAL; |
| 1220 | |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1225 | { |
| 1226 | int ret = 0; |
| 1227 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1228 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1229 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1230 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1231 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1232 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1233 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1234 | return -EINVAL; |
| 1235 | } |
| 1236 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1237 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1238 | mutex_lock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1239 | mutex_lock(&cgroup_mutex); |
| 1240 | |
| 1241 | /* See what subsystems are wanted */ |
| 1242 | ret = parse_cgroupfs_options(data, &opts); |
| 1243 | if (ret) |
| 1244 | goto out_unlock; |
| 1245 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1246 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1247 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1248 | task_tgid_nr(current), current->comm); |
| 1249 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1250 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1251 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1252 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1253 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1254 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1255 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1256 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1257 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1258 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1259 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1260 | goto out_unlock; |
| 1261 | } |
| 1262 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1263 | /* remounting is not allowed for populated hierarchies */ |
| 1264 | if (root->number_of_cgroups > 1) { |
| 1265 | ret = -EBUSY; |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1266 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1267 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1268 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1269 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1270 | if (ret) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1271 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1272 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1273 | if (opts.release_agent) { |
| 1274 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1275 | strcpy(root->release_agent_path, opts.release_agent); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1276 | spin_unlock(&release_agent_path_lock); |
| 1277 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1278 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1279 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1280 | kfree(opts.name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1281 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1282 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1283 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1284 | return ret; |
| 1285 | } |
| 1286 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1287 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1288 | .statfs = simple_statfs, |
| 1289 | .drop_inode = generic_delete_inode, |
| 1290 | .show_options = cgroup_show_options, |
| 1291 | .remount_fs = cgroup_remount, |
| 1292 | }; |
| 1293 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1294 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1295 | { |
| 1296 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1297 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1298 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1299 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1300 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1301 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1302 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1303 | cgrp->dummy_css.cgroup = cgrp; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1304 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1305 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1306 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1307 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1308 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1309 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1310 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1311 | INIT_LIST_HEAD(&root->root_list); |
| 1312 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1313 | cgrp->root = root; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 1314 | RCU_INIT_POINTER(cgrp->name, &root_cgroup_name); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1315 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1316 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1319 | 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] | 1320 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1321 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1322 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1323 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1324 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1325 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 1326 | GFP_KERNEL); |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1327 | if (id < 0) |
| 1328 | return id; |
| 1329 | |
| 1330 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1335 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1336 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1337 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1338 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1339 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1340 | root->hierarchy_id = 0; |
| 1341 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1344 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1345 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1346 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1347 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1348 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1349 | /* If we asked for a name then it must match */ |
| 1350 | if (opts->name && strcmp(opts->name, root->name)) |
| 1351 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1352 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1353 | /* |
| 1354 | * If we asked for subsystems (or explicitly for no |
| 1355 | * subsystems) then they must match |
| 1356 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1357 | if ((opts->subsys_mask || opts->none) |
| 1358 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1359 | return 0; |
| 1360 | |
| 1361 | return 1; |
| 1362 | } |
| 1363 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1364 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1365 | { |
| 1366 | struct cgroupfs_root *root; |
| 1367 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1368 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1369 | return NULL; |
| 1370 | |
| 1371 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1372 | if (!root) |
| 1373 | return ERR_PTR(-ENOMEM); |
| 1374 | |
| 1375 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1376 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1377 | /* |
| 1378 | * We need to set @root->subsys_mask now so that @root can be |
| 1379 | * matched by cgroup_test_super() before it finishes |
| 1380 | * initialization; otherwise, competing mounts with the same |
| 1381 | * options may try to bind the same subsystems instead of waiting |
| 1382 | * for the first one leading to unexpected mount errors. |
| 1383 | * SUBSYS_BOUND will be set once actual binding is complete. |
| 1384 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1385 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1386 | root->flags = opts->flags; |
| 1387 | if (opts->release_agent) |
| 1388 | strcpy(root->release_agent_path, opts->release_agent); |
| 1389 | if (opts->name) |
| 1390 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1391 | if (opts->cpuset_clone_children) |
| 1392 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1393 | return root; |
| 1394 | } |
| 1395 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1396 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1397 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1398 | if (root) { |
| 1399 | /* hierarhcy ID shoulid already have been released */ |
| 1400 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1401 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1402 | idr_destroy(&root->cgroup_idr); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1403 | kfree(root); |
| 1404 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1407 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1408 | { |
| 1409 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1410 | struct cgroup_sb_opts *opts = data; |
| 1411 | |
| 1412 | /* If we don't have a new root, we can't set up a new sb */ |
| 1413 | if (!opts->new_root) |
| 1414 | return -EINVAL; |
| 1415 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1416 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1417 | |
| 1418 | ret = set_anon_super(sb, NULL); |
| 1419 | if (ret) |
| 1420 | return ret; |
| 1421 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1422 | sb->s_fs_info = opts->new_root; |
| 1423 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1424 | |
| 1425 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1426 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1427 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1428 | sb->s_op = &cgroup_ops; |
| 1429 | |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
| 1433 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1434 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1435 | static const struct dentry_operations cgroup_dops = { |
| 1436 | .d_iput = cgroup_diput, |
Al Viro | b26d4cd | 2013-10-25 18:47:37 -0400 | [diff] [blame] | 1437 | .d_delete = always_delete_dentry, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1438 | }; |
| 1439 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1440 | struct inode *inode = |
| 1441 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1442 | |
| 1443 | if (!inode) |
| 1444 | return -ENOMEM; |
| 1445 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1446 | inode->i_fop = &simple_dir_operations; |
| 1447 | inode->i_op = &cgroup_dir_inode_operations; |
| 1448 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1449 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1450 | sb->s_root = d_make_root(inode); |
| 1451 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1452 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1453 | /* for everything else we want ->d_op set */ |
| 1454 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1455 | return 0; |
| 1456 | } |
| 1457 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame^] | 1458 | static int cgroup_setup_root(struct cgroupfs_root *root) |
| 1459 | { |
| 1460 | LIST_HEAD(tmp_links); |
| 1461 | struct super_block *sb = root->sb; |
| 1462 | struct cgroup *root_cgrp = &root->top_cgroup; |
| 1463 | struct cgroupfs_root *existing_root; |
| 1464 | struct css_set *cset; |
| 1465 | struct inode *inode; |
| 1466 | const struct cred *cred; |
| 1467 | int i, ret; |
| 1468 | |
| 1469 | lockdep_assert_held(&cgroup_tree_mutex); |
| 1470 | lockdep_assert_held(&cgroup_mutex); |
| 1471 | BUG_ON(sb->s_root != NULL); |
| 1472 | |
| 1473 | mutex_unlock(&cgroup_mutex); |
| 1474 | mutex_unlock(&cgroup_tree_mutex); |
| 1475 | |
| 1476 | ret = cgroup_get_rootdir(sb); |
| 1477 | if (ret) { |
| 1478 | mutex_lock(&cgroup_tree_mutex); |
| 1479 | mutex_lock(&cgroup_mutex); |
| 1480 | return ret; |
| 1481 | } |
| 1482 | inode = sb->s_root->d_inode; |
| 1483 | |
| 1484 | mutex_lock(&inode->i_mutex); |
| 1485 | mutex_lock(&cgroup_tree_mutex); |
| 1486 | mutex_lock(&cgroup_mutex); |
| 1487 | |
| 1488 | ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL); |
| 1489 | if (ret < 0) |
| 1490 | goto out_unlock; |
| 1491 | root_cgrp->id = ret; |
| 1492 | |
| 1493 | /* check for name clashes with existing mounts */ |
| 1494 | ret = -EBUSY; |
| 1495 | if (strlen(root->name)) |
| 1496 | for_each_active_root(existing_root) |
| 1497 | if (!strcmp(existing_root->name, root->name)) |
| 1498 | goto out_unlock; |
| 1499 | |
| 1500 | /* |
| 1501 | * We're accessing css_set_count without locking css_set_lock here, |
| 1502 | * but that's OK - it can only be increased by someone holding |
| 1503 | * cgroup_lock, and that's us. The worst that can happen is that we |
| 1504 | * have some link structures left over |
| 1505 | */ |
| 1506 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
| 1507 | if (ret) |
| 1508 | goto out_unlock; |
| 1509 | |
| 1510 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1511 | ret = cgroup_init_root_id(root, 2, 0); |
| 1512 | if (ret) |
| 1513 | goto out_unlock; |
| 1514 | |
| 1515 | sb->s_root->d_fsdata = root_cgrp; |
| 1516 | root_cgrp->dentry = sb->s_root; |
| 1517 | |
| 1518 | /* |
| 1519 | * We're inside get_sb() and will call lookup_one_len() to create |
| 1520 | * the root files, which doesn't work if SELinux is in use. The |
| 1521 | * following cred dancing somehow works around it. See 2ce9738ba |
| 1522 | * ("cgroupfs: use init_cred when populating new cgroupfs mount") |
| 1523 | * for more details. |
| 1524 | */ |
| 1525 | cred = override_creds(&init_cred); |
| 1526 | |
| 1527 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
| 1528 | if (ret) |
| 1529 | goto rm_base_files; |
| 1530 | |
| 1531 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
| 1532 | if (ret) |
| 1533 | goto rm_base_files; |
| 1534 | |
| 1535 | revert_creds(cred); |
| 1536 | |
| 1537 | /* |
| 1538 | * There must be no failure case after here, since rebinding takes |
| 1539 | * care of subsystems' refcounts, which are explicitly dropped in |
| 1540 | * the failure exit path. |
| 1541 | */ |
| 1542 | list_add(&root->root_list, &cgroup_roots); |
| 1543 | cgroup_root_count++; |
| 1544 | |
| 1545 | /* |
| 1546 | * Link the top cgroup in this hierarchy into all the css_set |
| 1547 | * objects. |
| 1548 | */ |
| 1549 | write_lock(&css_set_lock); |
| 1550 | hash_for_each(css_set_table, i, cset, hlist) |
| 1551 | link_css_set(&tmp_links, cset, root_cgrp); |
| 1552 | write_unlock(&css_set_lock); |
| 1553 | |
| 1554 | BUG_ON(!list_empty(&root_cgrp->children)); |
| 1555 | BUG_ON(root->number_of_cgroups != 1); |
| 1556 | |
| 1557 | ret = 0; |
| 1558 | goto out_unlock; |
| 1559 | |
| 1560 | rm_base_files: |
| 1561 | cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false); |
| 1562 | revert_creds(cred); |
| 1563 | cgroup_exit_root_id(root); |
| 1564 | out_unlock: |
| 1565 | mutex_unlock(&inode->i_mutex); |
| 1566 | free_cgrp_cset_links(&tmp_links); |
| 1567 | return ret; |
| 1568 | } |
| 1569 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1570 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1571 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1572 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1573 | { |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1574 | struct super_block *sb = NULL; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1575 | struct cgroupfs_root *root = NULL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1576 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1577 | struct cgroupfs_root *new_root; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1578 | int ret; |
| 1579 | |
| 1580 | mutex_lock(&cgroup_tree_mutex); |
| 1581 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1582 | |
| 1583 | /* First find the desired set of subsystems */ |
| 1584 | ret = parse_cgroupfs_options(data, &opts); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1585 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1586 | goto out_unlock; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1587 | |
| 1588 | /* |
| 1589 | * Allocate a new cgroup root. We may not need it if we're |
| 1590 | * reusing an existing hierarchy. |
| 1591 | */ |
| 1592 | new_root = cgroup_root_from_opts(&opts); |
| 1593 | if (IS_ERR(new_root)) { |
| 1594 | ret = PTR_ERR(new_root); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1595 | goto out_unlock; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1596 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1597 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1598 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1599 | /* Locate an existing or new sb for this hierarchy */ |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1600 | mutex_unlock(&cgroup_mutex); |
| 1601 | mutex_unlock(&cgroup_tree_mutex); |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1602 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1603 | mutex_lock(&cgroup_tree_mutex); |
| 1604 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1605 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1606 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1607 | cgroup_free_root(opts.new_root); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1608 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1611 | root = sb->s_fs_info; |
| 1612 | BUG_ON(!root); |
| 1613 | if (root == opts.new_root) { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame^] | 1614 | ret = cgroup_setup_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1615 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1616 | goto out_unlock; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1617 | } else { |
| 1618 | /* |
| 1619 | * We re-used an existing hierarchy - the new root (if |
| 1620 | * any) is not needed |
| 1621 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1622 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1623 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1624 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1625 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1626 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1627 | ret = -EINVAL; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1628 | goto out_unlock; |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1629 | } else { |
| 1630 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1631 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1632 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1635 | ret = 0; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1636 | out_unlock: |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1637 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1638 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1639 | |
| 1640 | if (ret && !IS_ERR_OR_NULL(sb)) |
| 1641 | deactivate_locked_super(sb); |
| 1642 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1643 | kfree(opts.release_agent); |
| 1644 | kfree(opts.name); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1645 | |
| 1646 | if (!ret) |
| 1647 | return dget(sb->s_root); |
| 1648 | else |
| 1649 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 1652 | static void cgroup_kill_sb(struct super_block *sb) |
| 1653 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1654 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1655 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1656 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1657 | int ret; |
| 1658 | |
| 1659 | BUG_ON(!root); |
| 1660 | |
| 1661 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1662 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1663 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1664 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1665 | mutex_lock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1666 | mutex_lock(&cgroup_mutex); |
| 1667 | |
| 1668 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1669 | if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { |
| 1670 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
| 1671 | /* Shouldn't be able to fail ... */ |
| 1672 | BUG_ON(ret); |
| 1673 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1674 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1675 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1676 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1677 | * root cgroup |
| 1678 | */ |
| 1679 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1680 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1681 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1682 | list_del(&link->cset_link); |
| 1683 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1684 | kfree(link); |
| 1685 | } |
| 1686 | write_unlock(&css_set_lock); |
| 1687 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1688 | if (!list_empty(&root->root_list)) { |
| 1689 | list_del(&root->root_list); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1690 | cgroup_root_count--; |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1691 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1692 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1693 | cgroup_exit_root_id(root); |
| 1694 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1695 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1696 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1697 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1698 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1699 | simple_xattrs_free(&cgrp->xattrs); |
| 1700 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1701 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1702 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | static struct file_system_type cgroup_fs_type = { |
| 1706 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1707 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1708 | .kill_sb = cgroup_kill_sb, |
| 1709 | }; |
| 1710 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1711 | static struct kobject *cgroup_kobj; |
| 1712 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1713 | /** |
| 1714 | * cgroup_path - generate the path of a cgroup |
| 1715 | * @cgrp: the cgroup in question |
| 1716 | * @buf: the buffer to write the path into |
| 1717 | * @buflen: the length of the buffer |
| 1718 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1719 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1720 | * |
| 1721 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1722 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1723 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1724 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1725 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1726 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1727 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1728 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1729 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1730 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1731 | if (!cgrp->parent) { |
| 1732 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1733 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1734 | return 0; |
| 1735 | } |
| 1736 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1737 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1738 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1739 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1740 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1741 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1742 | const char *name = cgroup_name(cgrp); |
| 1743 | int len; |
| 1744 | |
| 1745 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1746 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1747 | goto out; |
| 1748 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1749 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1750 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1751 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1752 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1753 | |
| 1754 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1755 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1756 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1757 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1758 | out: |
| 1759 | rcu_read_unlock(); |
| 1760 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1761 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1762 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1763 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1764 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1765 | * 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] | 1766 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1767 | * @buf: the buffer to write the path into |
| 1768 | * @buflen: the length of the buffer |
| 1769 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1770 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1771 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1772 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1773 | * cgroup controller callbacks. |
| 1774 | * |
| 1775 | * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short. |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1776 | */ |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1777 | 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] | 1778 | { |
| 1779 | struct cgroupfs_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1780 | struct cgroup *cgrp; |
| 1781 | int hierarchy_id = 1, ret = 0; |
| 1782 | |
| 1783 | if (buflen < 2) |
| 1784 | return -ENAMETOOLONG; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1785 | |
| 1786 | mutex_lock(&cgroup_mutex); |
| 1787 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1788 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1789 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1790 | if (root) { |
| 1791 | cgrp = task_cgroup_from_root(task, root); |
| 1792 | ret = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1793 | } else { |
| 1794 | /* if no hierarchy exists, everyone is in "/" */ |
| 1795 | memcpy(buf, "/", 2); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1799 | return ret; |
| 1800 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1801 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1802 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1803 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1804 | * Control Group taskset |
| 1805 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1806 | struct task_and_cgroup { |
| 1807 | struct task_struct *task; |
| 1808 | struct cgroup *cgrp; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1809 | struct css_set *cset; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1810 | }; |
| 1811 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1812 | struct cgroup_taskset { |
| 1813 | struct task_and_cgroup single; |
| 1814 | struct flex_array *tc_array; |
| 1815 | int tc_array_len; |
| 1816 | int idx; |
| 1817 | struct cgroup *cur_cgrp; |
| 1818 | }; |
| 1819 | |
| 1820 | /** |
| 1821 | * cgroup_taskset_first - reset taskset and return the first task |
| 1822 | * @tset: taskset of interest |
| 1823 | * |
| 1824 | * @tset iteration is initialized and the first task is returned. |
| 1825 | */ |
| 1826 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1827 | { |
| 1828 | if (tset->tc_array) { |
| 1829 | tset->idx = 0; |
| 1830 | return cgroup_taskset_next(tset); |
| 1831 | } else { |
| 1832 | tset->cur_cgrp = tset->single.cgrp; |
| 1833 | return tset->single.task; |
| 1834 | } |
| 1835 | } |
| 1836 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1837 | |
| 1838 | /** |
| 1839 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1840 | * @tset: taskset of interest |
| 1841 | * |
| 1842 | * Return the next task in @tset. Iteration must have been initialized |
| 1843 | * with cgroup_taskset_first(). |
| 1844 | */ |
| 1845 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1846 | { |
| 1847 | struct task_and_cgroup *tc; |
| 1848 | |
| 1849 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1850 | return NULL; |
| 1851 | |
| 1852 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1853 | tset->cur_cgrp = tc->cgrp; |
| 1854 | return tc->task; |
| 1855 | } |
| 1856 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1857 | |
| 1858 | /** |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1859 | * cgroup_taskset_cur_css - return the matching css for the current task |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1860 | * @tset: taskset of interest |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1861 | * @subsys_id: the ID of the target subsystem |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1862 | * |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1863 | * Return the css for the current (last returned) task of @tset for |
| 1864 | * subsystem specified by @subsys_id. This function must be preceded by |
| 1865 | * either cgroup_taskset_first() or cgroup_taskset_next(). |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1866 | */ |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1867 | struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset, |
| 1868 | int subsys_id) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1869 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1870 | return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1871 | } |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1872 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1873 | |
| 1874 | /** |
| 1875 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1876 | * @tset: taskset of interest |
| 1877 | */ |
| 1878 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1879 | { |
| 1880 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1881 | } |
| 1882 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1883 | |
| 1884 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1885 | /* |
| 1886 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1887 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1888 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1889 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1890 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1891 | struct task_struct *tsk, |
| 1892 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1893 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1894 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1895 | |
| 1896 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1897 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1898 | * setting such that we can't race against cgroup_exit() changing the |
| 1899 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1900 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1901 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1902 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1903 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1904 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1905 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1906 | task_unlock(tsk); |
| 1907 | |
| 1908 | /* Update the css_set linked lists if we're using them */ |
| 1909 | write_lock(&css_set_lock); |
| 1910 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1911 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1912 | write_unlock(&css_set_lock); |
| 1913 | |
| 1914 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1915 | * We just gained a reference on old_cset by taking it from the |
| 1916 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1917 | * 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] | 1918 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1919 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1920 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1923 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1924 | * 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] | 1925 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1926 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1927 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1928 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1929 | * 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] | 1930 | * 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] | 1931 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1932 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1933 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1934 | { |
| 1935 | int retval, i, group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1936 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1937 | struct cgroup_subsys_state *css, *failed_css = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1938 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1939 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1940 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1941 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1942 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1943 | |
| 1944 | /* |
| 1945 | * step 0: in order to do expensive, possibly blocking operations for |
| 1946 | * every thread, we cannot iterate the thread group list, since it needs |
| 1947 | * 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] | 1948 | * group - group_rwsem prevents new threads from appearing, and if |
| 1949 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1950 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1951 | if (threadgroup) |
| 1952 | group_size = get_nr_threads(tsk); |
| 1953 | else |
| 1954 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1955 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1956 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1957 | if (!group) |
| 1958 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1959 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 1960 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1961 | if (retval) |
| 1962 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1963 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1964 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1965 | /* |
| 1966 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 1967 | * already PF_EXITING could be freed from underneath us unless we |
| 1968 | * take an rcu_read_lock. |
| 1969 | */ |
| 1970 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1971 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1972 | struct task_and_cgroup ent; |
| 1973 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1974 | /* @tsk either already exited or can't exit until the end */ |
| 1975 | if (tsk->flags & PF_EXITING) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1976 | goto next; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1977 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1978 | /* as per above, nr_threads may decrease, but not increase. */ |
| 1979 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1980 | ent.task = tsk; |
| 1981 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 1982 | /* nothing to do if this task is already in the cgroup */ |
| 1983 | if (ent.cgrp == cgrp) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1984 | goto next; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1985 | /* |
| 1986 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 1987 | * earlier, but it's good form to communicate our expectations. |
| 1988 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1989 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1990 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1991 | i++; |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1992 | next: |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1993 | if (!threadgroup) |
| 1994 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1995 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1996 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1997 | /* remember the number of threads in the array for later. */ |
| 1998 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1999 | tset.tc_array = group; |
| 2000 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2001 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2002 | /* methods shouldn't be called if no task is actually migrating */ |
| 2003 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2004 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2005 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2006 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2007 | /* |
| 2008 | * step 1: check that we can legitimately attach to the cgroup. |
| 2009 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2010 | for_each_css(css, i, cgrp) { |
| 2011 | if (css->ss->can_attach) { |
| 2012 | retval = css->ss->can_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2013 | if (retval) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2014 | failed_css = css; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2015 | goto out_cancel_attach; |
| 2016 | } |
| 2017 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2022 | * we use find_css_set, which allocates a new one if necessary. |
| 2023 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2024 | for (i = 0; i < group_size; i++) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2025 | struct css_set *old_cset; |
| 2026 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2027 | tc = flex_array_get(group, i); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2028 | old_cset = task_css_set(tc->task); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2029 | tc->cset = find_css_set(old_cset, cgrp); |
| 2030 | if (!tc->cset) { |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2031 | retval = -ENOMEM; |
| 2032 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2037 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2038 | * proceed to move all tasks to the new cgroup. There are no |
| 2039 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2040 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2041 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2042 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2043 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2044 | } |
| 2045 | /* nothing is sensitive to fork() after this point. */ |
| 2046 | |
| 2047 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2048 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2049 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2050 | for_each_css(css, i, cgrp) |
| 2051 | if (css->ss->attach) |
| 2052 | css->ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2053 | |
| 2054 | /* |
| 2055 | * step 5: success! and cleanup |
| 2056 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2057 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2058 | out_put_css_set_refs: |
| 2059 | if (retval) { |
| 2060 | for (i = 0; i < group_size; i++) { |
| 2061 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2062 | if (!tc->cset) |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2063 | break; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2064 | put_css_set(tc->cset); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2065 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2066 | } |
| 2067 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2068 | if (retval) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2069 | for_each_css(css, i, cgrp) { |
| 2070 | if (css == failed_css) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2071 | break; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2072 | if (css->ss->cancel_attach) |
| 2073 | css->ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2074 | } |
| 2075 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2076 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2077 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2078 | return retval; |
| 2079 | } |
| 2080 | |
| 2081 | /* |
| 2082 | * 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] | 2083 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2084 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2085 | */ |
| 2086 | 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] | 2087 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2088 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2089 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2090 | int ret; |
| 2091 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2092 | if (!cgroup_lock_live_group(cgrp)) |
| 2093 | return -ENODEV; |
| 2094 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2095 | retry_find_task: |
| 2096 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2097 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2098 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2099 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2100 | rcu_read_unlock(); |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2101 | ret = -ESRCH; |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2102 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2103 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2104 | /* |
| 2105 | * even if we're attaching all tasks in the thread group, we |
| 2106 | * only need to check permissions on one of them. |
| 2107 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2108 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2109 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2110 | !uid_eq(cred->euid, tcred->uid) && |
| 2111 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2112 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2113 | ret = -EACCES; |
| 2114 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2115 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2116 | } else |
| 2117 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2118 | |
| 2119 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2120 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2121 | |
| 2122 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2123 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2124 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2125 | * with no rt_runtime allocated. Just say no. |
| 2126 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2127 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2128 | ret = -EINVAL; |
| 2129 | rcu_read_unlock(); |
| 2130 | goto out_unlock_cgroup; |
| 2131 | } |
| 2132 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2133 | get_task_struct(tsk); |
| 2134 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2135 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2136 | threadgroup_lock(tsk); |
| 2137 | if (threadgroup) { |
| 2138 | if (!thread_group_leader(tsk)) { |
| 2139 | /* |
| 2140 | * a race with de_thread from another thread's exec() |
| 2141 | * may strip us of our leadership, if this happens, |
| 2142 | * there is no choice but to throw this task away and |
| 2143 | * try again; this is |
| 2144 | * "double-double-toil-and-trouble-check locking". |
| 2145 | */ |
| 2146 | threadgroup_unlock(tsk); |
| 2147 | put_task_struct(tsk); |
| 2148 | goto retry_find_task; |
| 2149 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2153 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2154 | threadgroup_unlock(tsk); |
| 2155 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2156 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2157 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2158 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2159 | return ret; |
| 2160 | } |
| 2161 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2162 | /** |
| 2163 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2164 | * @from: attach to all cgroups of a given task |
| 2165 | * @tsk: the task to be attached |
| 2166 | */ |
| 2167 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2168 | { |
| 2169 | struct cgroupfs_root *root; |
| 2170 | int retval = 0; |
| 2171 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2172 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2173 | for_each_active_root(root) { |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2174 | struct cgroup *from_cgrp = task_cgroup_from_root(from, root); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2175 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2176 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2177 | if (retval) |
| 2178 | break; |
| 2179 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2180 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2181 | |
| 2182 | return retval; |
| 2183 | } |
| 2184 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2185 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2186 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2187 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2188 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2189 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2192 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2193 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2194 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2195 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2198 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
| 2199 | struct cftype *cft, const char *buffer) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2200 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2201 | BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2202 | if (strlen(buffer) >= PATH_MAX) |
| 2203 | return -EINVAL; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2204 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2205 | return -ENODEV; |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2206 | spin_lock(&release_agent_path_lock); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2207 | strcpy(css->cgroup->root->release_agent_path, buffer); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2208 | spin_unlock(&release_agent_path_lock); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2209 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2210 | return 0; |
| 2211 | } |
| 2212 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2213 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2214 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2215 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2216 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2217 | if (!cgroup_lock_live_group(cgrp)) |
| 2218 | return -ENODEV; |
| 2219 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2220 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2221 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2222 | return 0; |
| 2223 | } |
| 2224 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2225 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2226 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2227 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2228 | |
| 2229 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2230 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2231 | } |
| 2232 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2233 | /* A buffer size big enough for numbers or short strings */ |
| 2234 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2235 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2236 | 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] | 2237 | size_t nbytes, loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2238 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2239 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2240 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2241 | struct cgroup_subsys_state *css = cfe->css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2242 | size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1; |
| 2243 | char *buf; |
| 2244 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2245 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2246 | if (nbytes >= max_bytes) |
| 2247 | return -E2BIG; |
| 2248 | |
| 2249 | buf = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2250 | if (!buf) |
| 2251 | return -ENOMEM; |
| 2252 | |
| 2253 | if (copy_from_user(buf, userbuf, nbytes)) { |
| 2254 | ret = -EFAULT; |
| 2255 | goto out_free; |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2256 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2257 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2258 | buf[nbytes] = '\0'; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2259 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2260 | if (cft->write_string) { |
| 2261 | ret = cft->write_string(css, cft, strstrip(buf)); |
| 2262 | } else if (cft->write_u64) { |
| 2263 | unsigned long long v; |
| 2264 | ret = kstrtoull(buf, 0, &v); |
| 2265 | if (!ret) |
| 2266 | ret = cft->write_u64(css, cft, v); |
| 2267 | } else if (cft->write_s64) { |
| 2268 | long long v; |
| 2269 | ret = kstrtoll(buf, 0, &v); |
| 2270 | if (!ret) |
| 2271 | ret = cft->write_s64(css, cft, v); |
| 2272 | } else if (cft->trigger) { |
| 2273 | ret = cft->trigger(css, (unsigned int)cft->private); |
| 2274 | } else { |
| 2275 | ret = -EINVAL; |
| 2276 | } |
| 2277 | out_free: |
| 2278 | kfree(buf); |
| 2279 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2282 | /* |
| 2283 | * seqfile ops/methods for returning structured data. Currently just |
| 2284 | * supports string->u64 maps, but can be extended in future. |
| 2285 | */ |
| 2286 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2287 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2288 | { |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2289 | struct cftype *cft = seq_cft(seq); |
| 2290 | |
| 2291 | if (cft->seq_start) { |
| 2292 | return cft->seq_start(seq, ppos); |
| 2293 | } else { |
| 2294 | /* |
| 2295 | * The same behavior and code as single_open(). Returns |
| 2296 | * !NULL if pos is at the beginning; otherwise, NULL. |
| 2297 | */ |
| 2298 | return NULL + !*ppos; |
| 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 2303 | { |
| 2304 | struct cftype *cft = seq_cft(seq); |
| 2305 | |
| 2306 | if (cft->seq_next) { |
| 2307 | return cft->seq_next(seq, v, ppos); |
| 2308 | } else { |
| 2309 | /* |
| 2310 | * The same behavior and code as single_open(), always |
| 2311 | * terminate after the initial read. |
| 2312 | */ |
| 2313 | ++*ppos; |
| 2314 | return NULL; |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 2319 | { |
| 2320 | struct cftype *cft = seq_cft(seq); |
| 2321 | |
| 2322 | if (cft->seq_stop) |
| 2323 | cft->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2327 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2328 | struct cftype *cft = seq_cft(m); |
| 2329 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2330 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2331 | if (cft->seq_show) |
| 2332 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2333 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2334 | if (cft->read_u64) |
| 2335 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 2336 | else if (cft->read_s64) |
| 2337 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 2338 | else |
| 2339 | return -EINVAL; |
| 2340 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2341 | } |
| 2342 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2343 | static struct seq_operations cgroup_seq_operations = { |
| 2344 | .start = cgroup_seqfile_start, |
| 2345 | .next = cgroup_seqfile_next, |
| 2346 | .stop = cgroup_seqfile_stop, |
| 2347 | .show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2348 | }; |
| 2349 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2350 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2351 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2352 | struct cfent *cfe = __d_cfe(file->f_dentry); |
| 2353 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2354 | struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent); |
| 2355 | struct cgroup_subsys_state *css; |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2356 | struct cgroup_open_file *of; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2357 | int err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2358 | |
| 2359 | err = generic_file_open(inode, file); |
| 2360 | if (err) |
| 2361 | return err; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2362 | |
| 2363 | /* |
| 2364 | * If the file belongs to a subsystem, pin the css. Will be |
| 2365 | * unpinned either on open failure or release. This ensures that |
| 2366 | * @css stays alive for all file operations. |
| 2367 | */ |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2368 | rcu_read_lock(); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2369 | css = cgroup_css(cgrp, cft->ss); |
| 2370 | if (cft->ss && !css_tryget(css)) |
| 2371 | css = NULL; |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2372 | rcu_read_unlock(); |
| 2373 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2374 | if (!css) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2375 | return -ENODEV; |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2376 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2377 | /* |
| 2378 | * @cfe->css is used by read/write/close to determine the |
| 2379 | * associated css. @file->private_data would be a better place but |
| 2380 | * that's already used by seqfile. Multiple accessors may use it |
| 2381 | * simultaneously which is okay as the association never changes. |
| 2382 | */ |
| 2383 | WARN_ON_ONCE(cfe->css && cfe->css != css); |
| 2384 | cfe->css = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2385 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2386 | of = __seq_open_private(file, &cgroup_seq_operations, |
| 2387 | sizeof(struct cgroup_open_file)); |
| 2388 | if (of) { |
| 2389 | of->cfe = cfe; |
| 2390 | return 0; |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2391 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2392 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2393 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2394 | css_put(css); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2395 | return -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2399 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2400 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2401 | struct cgroup_subsys_state *css = cfe->css; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2402 | |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2403 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2404 | css_put(css); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2405 | return seq_release_private(inode, file); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | /* |
| 2409 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2410 | */ |
| 2411 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2412 | struct inode *new_dir, struct dentry *new_dentry) |
| 2413 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2414 | int ret; |
| 2415 | struct cgroup_name *name, *old_name; |
| 2416 | struct cgroup *cgrp; |
| 2417 | |
| 2418 | /* |
| 2419 | * It's convinient to use parent dir's i_mutex to protected |
| 2420 | * cgrp->name. |
| 2421 | */ |
| 2422 | lockdep_assert_held(&old_dir->i_mutex); |
| 2423 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2424 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2425 | return -ENOTDIR; |
| 2426 | if (new_dentry->d_inode) |
| 2427 | return -EEXIST; |
| 2428 | if (old_dir != new_dir) |
| 2429 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2430 | |
| 2431 | cgrp = __d_cgrp(old_dentry); |
| 2432 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2433 | /* |
| 2434 | * This isn't a proper migration and its usefulness is very |
| 2435 | * limited. Disallow if sane_behavior. |
| 2436 | */ |
| 2437 | if (cgroup_sane_behavior(cgrp)) |
| 2438 | return -EPERM; |
| 2439 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2440 | name = cgroup_alloc_name(new_dentry); |
| 2441 | if (!name) |
| 2442 | return -ENOMEM; |
| 2443 | |
| 2444 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2445 | if (ret) { |
| 2446 | kfree(name); |
| 2447 | return ret; |
| 2448 | } |
| 2449 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 2450 | old_name = rcu_dereference_protected(cgrp->name, true); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2451 | rcu_assign_pointer(cgrp->name, name); |
| 2452 | |
| 2453 | kfree_rcu(old_name, rcu_head); |
| 2454 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2455 | } |
| 2456 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2457 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2458 | { |
| 2459 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2460 | return &__d_cgrp(dentry)->xattrs; |
| 2461 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2462 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | static inline int xattr_enabled(struct dentry *dentry) |
| 2466 | { |
| 2467 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2468 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | static bool is_valid_xattr(const char *name) |
| 2472 | { |
| 2473 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2474 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2475 | return true; |
| 2476 | return false; |
| 2477 | } |
| 2478 | |
| 2479 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2480 | const void *val, size_t size, int flags) |
| 2481 | { |
| 2482 | if (!xattr_enabled(dentry)) |
| 2483 | return -EOPNOTSUPP; |
| 2484 | if (!is_valid_xattr(name)) |
| 2485 | return -EINVAL; |
| 2486 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2487 | } |
| 2488 | |
| 2489 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2490 | { |
| 2491 | if (!xattr_enabled(dentry)) |
| 2492 | return -EOPNOTSUPP; |
| 2493 | if (!is_valid_xattr(name)) |
| 2494 | return -EINVAL; |
| 2495 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2496 | } |
| 2497 | |
| 2498 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2499 | void *buf, size_t size) |
| 2500 | { |
| 2501 | if (!xattr_enabled(dentry)) |
| 2502 | return -EOPNOTSUPP; |
| 2503 | if (!is_valid_xattr(name)) |
| 2504 | return -EINVAL; |
| 2505 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2506 | } |
| 2507 | |
| 2508 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2509 | { |
| 2510 | if (!xattr_enabled(dentry)) |
| 2511 | return -EOPNOTSUPP; |
| 2512 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2513 | } |
| 2514 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2515 | static const struct file_operations cgroup_file_operations = { |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2516 | .read = seq_read, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2517 | .write = cgroup_file_write, |
| 2518 | .llseek = generic_file_llseek, |
| 2519 | .open = cgroup_file_open, |
| 2520 | .release = cgroup_file_release, |
| 2521 | }; |
| 2522 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2523 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2524 | .setxattr = cgroup_setxattr, |
| 2525 | .getxattr = cgroup_getxattr, |
| 2526 | .listxattr = cgroup_listxattr, |
| 2527 | .removexattr = cgroup_removexattr, |
| 2528 | }; |
| 2529 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2530 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | 786e144 | 2013-07-14 17:50:23 +0400 | [diff] [blame] | 2531 | .lookup = simple_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2532 | .mkdir = cgroup_mkdir, |
| 2533 | .rmdir = cgroup_rmdir, |
| 2534 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2535 | .setxattr = cgroup_setxattr, |
| 2536 | .getxattr = cgroup_getxattr, |
| 2537 | .listxattr = cgroup_listxattr, |
| 2538 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2539 | }; |
| 2540 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2541 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2542 | struct super_block *sb) |
| 2543 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2544 | struct inode *inode; |
| 2545 | |
| 2546 | if (!dentry) |
| 2547 | return -ENOENT; |
| 2548 | if (dentry->d_inode) |
| 2549 | return -EEXIST; |
| 2550 | |
| 2551 | inode = cgroup_new_inode(mode, sb); |
| 2552 | if (!inode) |
| 2553 | return -ENOMEM; |
| 2554 | |
| 2555 | if (S_ISDIR(mode)) { |
| 2556 | inode->i_op = &cgroup_dir_inode_operations; |
| 2557 | inode->i_fop = &simple_dir_operations; |
| 2558 | |
| 2559 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2560 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2561 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2562 | |
Tejun Heo | b8a2df6a | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2563 | /* |
| 2564 | * Control reaches here with cgroup_mutex held. |
| 2565 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2566 | * want to populate it immediately without releasing |
| 2567 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2568 | * yet, trylock will always succeed without affecting |
| 2569 | * lockdep checks. |
| 2570 | */ |
| 2571 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2572 | } else if (S_ISREG(mode)) { |
| 2573 | inode->i_size = 0; |
| 2574 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2575 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2576 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2577 | d_instantiate(dentry, inode); |
| 2578 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2579 | return 0; |
| 2580 | } |
| 2581 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2582 | /** |
| 2583 | * cgroup_file_mode - deduce file mode of a control file |
| 2584 | * @cft: the control file in question |
| 2585 | * |
| 2586 | * returns cft->mode if ->mode is not 0 |
| 2587 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2588 | * returns S_IRUGO if it has only a read handler |
| 2589 | * returns S_IWUSR if it has only a write hander |
| 2590 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2591 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2592 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2593 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2594 | |
| 2595 | if (cft->mode) |
| 2596 | return cft->mode; |
| 2597 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2598 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2599 | mode |= S_IRUGO; |
| 2600 | |
Tejun Heo | 6e0755b | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2601 | if (cft->write_u64 || cft->write_s64 || cft->write_string || |
| 2602 | cft->trigger) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2603 | mode |= S_IWUSR; |
| 2604 | |
| 2605 | return mode; |
| 2606 | } |
| 2607 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2608 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2609 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2610 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2611 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2612 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2613 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2614 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2615 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2616 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2617 | |
Tejun Heo | 9fa4db3 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2618 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 2619 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2620 | strcpy(name, cft->ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2621 | strcat(name, "."); |
| 2622 | } |
| 2623 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2624 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2625 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2626 | |
| 2627 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2628 | if (!cfe) |
| 2629 | return -ENOMEM; |
| 2630 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2631 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2632 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2633 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2634 | goto out; |
| 2635 | } |
| 2636 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2637 | cfe->type = (void *)cft; |
| 2638 | cfe->dentry = dentry; |
| 2639 | dentry->d_fsdata = cfe; |
| 2640 | simple_xattrs_init(&cfe->xattrs); |
| 2641 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2642 | mode = cgroup_file_mode(cft); |
| 2643 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2644 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2645 | list_add_tail(&cfe->node, &parent->files); |
| 2646 | cfe = NULL; |
| 2647 | } |
| 2648 | dput(dentry); |
| 2649 | out: |
| 2650 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2651 | return error; |
| 2652 | } |
| 2653 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2654 | /** |
| 2655 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2656 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2657 | * @cfts: array of cftypes to be added |
| 2658 | * @is_add: whether to add or remove |
| 2659 | * |
| 2660 | * 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] | 2661 | * For removals, this function never fails. If addition fails, this |
| 2662 | * function doesn't remove files already added. The caller is responsible |
| 2663 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2664 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2665 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2666 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2667 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2668 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2669 | int ret; |
| 2670 | |
| 2671 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2672 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2673 | |
| 2674 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2675 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2676 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2677 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2678 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2679 | continue; |
| 2680 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2681 | continue; |
| 2682 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2683 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2684 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2685 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2686 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2687 | cft->name, ret); |
| 2688 | return ret; |
| 2689 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2690 | } else { |
| 2691 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2692 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2693 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2694 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2695 | } |
| 2696 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2697 | static void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2698 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2699 | { |
| 2700 | /* |
| 2701 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2702 | * the existing cgroups under cgroup_mutex and create files. |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2703 | * Instead, we use css_for_each_descendant_pre() and drop RCU read |
| 2704 | * lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2705 | */ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2706 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2707 | mutex_lock(&cgroup_mutex); |
| 2708 | } |
| 2709 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2710 | static int cgroup_cfts_commit(struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2711 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2712 | { |
| 2713 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2714 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2715 | struct cgroup *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2716 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2717 | struct dentry *prev = NULL; |
| 2718 | struct inode *inode; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2719 | struct cgroup_subsys_state *css; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2720 | u64 update_before; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2721 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2722 | |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2723 | mutex_unlock(&cgroup_mutex); |
| 2724 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2725 | /* %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] | 2726 | if (!cfts || ss->root == &cgroup_dummy_root || |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2727 | !atomic_inc_not_zero(&sb->s_active)) { |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2728 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2729 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2730 | } |
| 2731 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2732 | /* |
| 2733 | * All cgroups which are created after we drop cgroup_mutex will |
| 2734 | * 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] | 2735 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2736 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2737 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2738 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2739 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2740 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2741 | struct cgroup *cgrp = css->cgroup; |
| 2742 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2743 | if (cgroup_is_dead(cgrp)) |
| 2744 | continue; |
| 2745 | |
| 2746 | inode = cgrp->dentry->d_inode; |
| 2747 | dget(cgrp->dentry); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2748 | dput(prev); |
| 2749 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2750 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2751 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2752 | mutex_lock(&inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2753 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2754 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2755 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2756 | mutex_unlock(&inode->i_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2757 | if (ret) |
| 2758 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2759 | } |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2760 | mutex_unlock(&cgroup_tree_mutex); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2761 | dput(prev); |
| 2762 | deactivate_super(sb); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2763 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | /** |
| 2767 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2768 | * @ss: target cgroup subsystem |
| 2769 | * @cfts: zero-length name terminated array of cftypes |
| 2770 | * |
| 2771 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2772 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2773 | * have them too. This function can be called anytime whether @ss is |
| 2774 | * attached or not. |
| 2775 | * |
| 2776 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2777 | * function currently returns 0 as long as @cfts registration is successful |
| 2778 | * even if some file creation attempts on existing cgroups fail. |
| 2779 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2780 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2781 | { |
| 2782 | struct cftype_set *set; |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2783 | struct cftype *cft; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2784 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2785 | |
| 2786 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2787 | if (!set) |
| 2788 | return -ENOMEM; |
| 2789 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2790 | for (cft = cfts; cft->name[0] != '\0'; cft++) |
| 2791 | cft->ss = ss; |
| 2792 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2793 | cgroup_cfts_prepare(); |
| 2794 | set->cfts = cfts; |
| 2795 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2796 | ret = cgroup_cfts_commit(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2797 | if (ret) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2798 | cgroup_rm_cftypes(cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2799 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2800 | } |
| 2801 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2802 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2803 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2804 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2805 | * @cfts: zero-length name terminated array of cftypes |
| 2806 | * |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2807 | * Unregister @cfts. Files described by @cfts are removed from all |
| 2808 | * existing cgroups and all future cgroups won't have them either. This |
| 2809 | * function can be called anytime whether @cfts' subsys is attached or not. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2810 | * |
| 2811 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2812 | * registered. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2813 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2814 | int cgroup_rm_cftypes(struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2815 | { |
| 2816 | struct cftype_set *set; |
| 2817 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2818 | if (!cfts || !cfts[0].ss) |
| 2819 | return -ENOENT; |
| 2820 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2821 | cgroup_cfts_prepare(); |
| 2822 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2823 | list_for_each_entry(set, &cfts[0].ss->cftsets, node) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2824 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2825 | list_del(&set->node); |
| 2826 | kfree(set); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2827 | cgroup_cfts_commit(cfts, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2828 | return 0; |
| 2829 | } |
| 2830 | } |
| 2831 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2832 | cgroup_cfts_commit(NULL, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2833 | return -ENOENT; |
| 2834 | } |
| 2835 | |
| 2836 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2837 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2838 | * @cgrp: the cgroup in question |
| 2839 | * |
| 2840 | * Return the number of tasks in the cgroup. |
| 2841 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2842 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2843 | { |
| 2844 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2845 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2846 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2847 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2848 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2849 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2850 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2851 | return count; |
| 2852 | } |
| 2853 | |
| 2854 | /* |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2855 | * To reduce the fork() overhead for systems that are not actually using |
| 2856 | * their cgroups capability, we don't maintain the lists running through |
| 2857 | * 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] | 2858 | * words after the first call to css_task_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2859 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2860 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2861 | { |
| 2862 | struct task_struct *p, *g; |
| 2863 | write_lock(&css_set_lock); |
| 2864 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2865 | /* |
| 2866 | * We need tasklist_lock because RCU is not safe against |
| 2867 | * while_each_thread(). Besides, a forking task that has passed |
| 2868 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2869 | * is not guaranteed to have its child immediately visible in the |
| 2870 | * tasklist if we walk through it with RCU. |
| 2871 | */ |
| 2872 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2873 | do_each_thread(g, p) { |
| 2874 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2875 | /* |
| 2876 | * We should check if the process is exiting, otherwise |
| 2877 | * it will race with cgroup_exit() in that the list |
| 2878 | * entry won't be deleted though the process has exited. |
| 2879 | */ |
| 2880 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2881 | list_add(&p->cg_list, &task_css_set(p)->tasks); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2882 | task_unlock(p); |
| 2883 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2884 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2885 | write_unlock(&css_set_lock); |
| 2886 | } |
| 2887 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2888 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2889 | * css_next_child - find the next child of a given css |
| 2890 | * @pos_css: the current position (%NULL to initiate traversal) |
| 2891 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2892 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2893 | * 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] | 2894 | * under either cgroup_mutex or RCU read lock. The only requirement is |
| 2895 | * that @parent_css and @pos_css are accessible. The next sibling is |
| 2896 | * guaranteed to be returned regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2897 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2898 | struct cgroup_subsys_state * |
| 2899 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 2900 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2901 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2902 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 2903 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2904 | struct cgroup *next; |
| 2905 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2906 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2907 | |
| 2908 | /* |
| 2909 | * @pos could already have been removed. Once a cgroup is removed, |
| 2910 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 2911 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 2912 | * before the cgroup is taken off the ->sibling list, if we see it |
| 2913 | * unasserted, it's guaranteed that the next sibling hasn't |
| 2914 | * finished its grace period even if it's already removed, and thus |
| 2915 | * safe to dereference from this RCU critical section. If |
| 2916 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 2917 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2918 | * |
| 2919 | * If @pos is dead, its next pointer can't be dereferenced; |
| 2920 | * however, as each cgroup is given a monotonically increasing |
| 2921 | * unique serial number and always appended to the sibling list, |
| 2922 | * the next one can be found by walking the parent's children until |
| 2923 | * we see a cgroup with higher serial number than @pos's. While |
| 2924 | * this path can be slower, it's taken only when either the current |
| 2925 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2926 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2927 | if (!pos) { |
| 2928 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 2929 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2930 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2931 | } else { |
| 2932 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 2933 | if (next->serial_nr > pos->serial_nr) |
| 2934 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2935 | } |
| 2936 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2937 | if (&next->sibling == &cgrp->children) |
| 2938 | return NULL; |
| 2939 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2940 | return cgroup_css(next, parent_css->ss); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2941 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2942 | EXPORT_SYMBOL_GPL(css_next_child); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2943 | |
| 2944 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2945 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2946 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2947 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2948 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2949 | * 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] | 2950 | * to visit for pre-order traversal of @root's descendants. @root is |
| 2951 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2952 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2953 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2954 | * doesn't require the whole traversal to be contained in a single critical |
| 2955 | * section. This function will return the correct next descendant as long |
| 2956 | * 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] | 2957 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2958 | struct cgroup_subsys_state * |
| 2959 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 2960 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2961 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2962 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2963 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2964 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2965 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2966 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2967 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2968 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2969 | |
| 2970 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2971 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2972 | if (next) |
| 2973 | return next; |
| 2974 | |
| 2975 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2976 | while (pos != root) { |
| 2977 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2978 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2979 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2980 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2981 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2982 | |
| 2983 | return NULL; |
| 2984 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2985 | EXPORT_SYMBOL_GPL(css_next_descendant_pre); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2986 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2987 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2988 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 2989 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2990 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2991 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 2992 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2993 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2994 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2995 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2996 | * doesn't require the whole traversal to be contained in a single critical |
| 2997 | * section. This function will return the correct rightmost descendant as |
| 2998 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2999 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3000 | struct cgroup_subsys_state * |
| 3001 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3002 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3003 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3004 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3005 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3006 | |
| 3007 | do { |
| 3008 | last = pos; |
| 3009 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3010 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3011 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3012 | pos = tmp; |
| 3013 | } while (pos); |
| 3014 | |
| 3015 | return last; |
| 3016 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3017 | EXPORT_SYMBOL_GPL(css_rightmost_descendant); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3018 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3019 | static struct cgroup_subsys_state * |
| 3020 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3021 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3022 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3023 | |
| 3024 | do { |
| 3025 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3026 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3027 | } while (pos); |
| 3028 | |
| 3029 | return last; |
| 3030 | } |
| 3031 | |
| 3032 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3033 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3034 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3035 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3036 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3037 | * 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] | 3038 | * to visit for post-order traversal of @root's descendants. @root is |
| 3039 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3040 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3041 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3042 | * doesn't require the whole traversal to be contained in a single critical |
| 3043 | * section. This function will return the correct next descendant as long |
| 3044 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 3045 | * @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3046 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3047 | struct cgroup_subsys_state * |
| 3048 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 3049 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3050 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3051 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3052 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3053 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3054 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 3055 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 3056 | if (!pos) |
| 3057 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3058 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3059 | /* if we visited @root, we're done */ |
| 3060 | if (pos == root) |
| 3061 | return NULL; |
| 3062 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3063 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3064 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3065 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3066 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3067 | |
| 3068 | /* no sibling left, visit parent */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3069 | return css_parent(pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3070 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3071 | EXPORT_SYMBOL_GPL(css_next_descendant_post); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3072 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3073 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3074 | * 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] | 3075 | * @it: the iterator to advance |
| 3076 | * |
| 3077 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3078 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3079 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3080 | { |
| 3081 | struct list_head *l = it->cset_link; |
| 3082 | struct cgrp_cset_link *link; |
| 3083 | struct css_set *cset; |
| 3084 | |
| 3085 | /* Advance to the next non-empty css_set */ |
| 3086 | do { |
| 3087 | l = l->next; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3088 | if (l == &it->origin_css->cgroup->cset_links) { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3089 | it->cset_link = NULL; |
| 3090 | return; |
| 3091 | } |
| 3092 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3093 | cset = link->cset; |
| 3094 | } while (list_empty(&cset->tasks)); |
| 3095 | it->cset_link = l; |
| 3096 | it->task = cset->tasks.next; |
| 3097 | } |
| 3098 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3099 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3100 | * css_task_iter_start - initiate task iteration |
| 3101 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3102 | * @it: the task iterator to use |
| 3103 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3104 | * Initiate iteration through the tasks of @css. The caller can call |
| 3105 | * css_task_iter_next() to walk through the tasks until the function |
| 3106 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 3107 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3108 | * |
| 3109 | * Note that this function acquires a lock which is released when the |
| 3110 | * iteration finishes. The caller can't sleep while iteration is in |
| 3111 | * progress. |
| 3112 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3113 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 3114 | struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3115 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3116 | { |
| 3117 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3118 | * The first time anyone tries to iterate across a css, we need to |
| 3119 | * enable the list linking each css_set to its tasks, and fix up |
| 3120 | * all existing tasks. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3121 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3122 | if (!use_task_css_set_links) |
| 3123 | cgroup_enable_task_cg_lists(); |
| 3124 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3125 | read_lock(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3126 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3127 | it->origin_css = css; |
| 3128 | it->cset_link = &css->cgroup->cset_links; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3129 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3130 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3131 | } |
| 3132 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3133 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3134 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3135 | * @it: the task iterator being iterated |
| 3136 | * |
| 3137 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3138 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 3139 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3140 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3141 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3142 | { |
| 3143 | struct task_struct *res; |
| 3144 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3145 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3146 | |
| 3147 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3148 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3149 | return NULL; |
| 3150 | res = list_entry(l, struct task_struct, cg_list); |
| 3151 | /* Advance iterator to find next entry */ |
| 3152 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3153 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3154 | if (l == &link->cset->tasks) { |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3155 | /* |
| 3156 | * We reached the end of this task list - move on to the |
| 3157 | * next cgrp_cset_link. |
| 3158 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3159 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3160 | } else { |
| 3161 | it->task = l; |
| 3162 | } |
| 3163 | return res; |
| 3164 | } |
| 3165 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3166 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3167 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3168 | * @it: the task iterator to finish |
| 3169 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3170 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3171 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3172 | void css_task_iter_end(struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3173 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3174 | { |
| 3175 | read_unlock(&css_set_lock); |
| 3176 | } |
| 3177 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3178 | static inline int started_after_time(struct task_struct *t1, |
| 3179 | struct timespec *time, |
| 3180 | struct task_struct *t2) |
| 3181 | { |
| 3182 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3183 | if (start_diff > 0) { |
| 3184 | return 1; |
| 3185 | } else if (start_diff < 0) { |
| 3186 | return 0; |
| 3187 | } else { |
| 3188 | /* |
| 3189 | * Arbitrarily, if two processes started at the same |
| 3190 | * time, we'll say that the lower pointer value |
| 3191 | * started first. Note that t2 may have exited by now |
| 3192 | * so this may not be a valid pointer any longer, but |
| 3193 | * that's fine - it still serves to distinguish |
| 3194 | * between two tasks started (effectively) simultaneously. |
| 3195 | */ |
| 3196 | return t1 > t2; |
| 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | /* |
| 3201 | * This function is a callback from heap_insert() and is used to order |
| 3202 | * the heap. |
| 3203 | * In this case we order the heap in descending task start time. |
| 3204 | */ |
| 3205 | static inline int started_after(void *p1, void *p2) |
| 3206 | { |
| 3207 | struct task_struct *t1 = p1; |
| 3208 | struct task_struct *t2 = p2; |
| 3209 | return started_after_time(t1, &t2->start_time, t2); |
| 3210 | } |
| 3211 | |
| 3212 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3213 | * css_scan_tasks - iterate though all the tasks in a css |
| 3214 | * @css: the css to iterate tasks of |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3215 | * @test: optional test callback |
| 3216 | * @process: process callback |
| 3217 | * @data: data passed to @test and @process |
| 3218 | * @heap: optional pre-allocated heap used for task iteration |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3219 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3220 | * Iterate through all the tasks in @css, calling @test for each, and if it |
| 3221 | * returns %true, call @process for it also. |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3222 | * |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3223 | * @test may be NULL, meaning always true (select all tasks), which |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3224 | * effectively duplicates css_task_iter_{start,next,end}() but does not |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3225 | * lock css_set_lock for the call to @process. |
| 3226 | * |
| 3227 | * 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] | 3228 | * of @css for the duration of this call. This function may or may not |
| 3229 | * call @process for tasks that exit or move to a different css during the |
| 3230 | * call, or are forked or move into the css during the call. |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3231 | * |
| 3232 | * Note that @test may be called with locks held, and may in some |
| 3233 | * situations be called multiple times for the same task, so it should be |
| 3234 | * cheap. |
| 3235 | * |
| 3236 | * If @heap is non-NULL, a heap has been pre-allocated and will be used for |
| 3237 | * heap operations (and its "gt" member will be overwritten), else a |
| 3238 | * temporary heap will be used (allocation of which may cause this function |
| 3239 | * to fail). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3240 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3241 | int css_scan_tasks(struct cgroup_subsys_state *css, |
| 3242 | bool (*test)(struct task_struct *, void *), |
| 3243 | void (*process)(struct task_struct *, void *), |
| 3244 | void *data, struct ptr_heap *heap) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3245 | { |
| 3246 | int retval, i; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3247 | struct css_task_iter it; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3248 | struct task_struct *p, *dropped; |
| 3249 | /* Never dereference latest_task, since it's not refcounted */ |
| 3250 | struct task_struct *latest_task = NULL; |
| 3251 | struct ptr_heap tmp_heap; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3252 | struct timespec latest_time = { 0, 0 }; |
| 3253 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3254 | if (heap) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3255 | /* The caller supplied our heap and pre-allocated its memory */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3256 | heap->gt = &started_after; |
| 3257 | } else { |
| 3258 | /* We need to allocate our own heap memory */ |
| 3259 | heap = &tmp_heap; |
| 3260 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3261 | if (retval) |
| 3262 | /* cannot allocate the heap */ |
| 3263 | return retval; |
| 3264 | } |
| 3265 | |
| 3266 | again: |
| 3267 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3268 | * Scan tasks in the css, using the @test callback to determine |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3269 | * which are of interest, and invoking @process callback on the |
| 3270 | * ones which need an update. Since we don't want to hold any |
| 3271 | * locks during the task updates, gather tasks to be processed in a |
| 3272 | * heap structure. The heap is sorted by descending task start |
| 3273 | * time. If the statically-sized heap fills up, we overflow tasks |
| 3274 | * that started later, and in future iterations only consider tasks |
| 3275 | * that started after the latest task in the previous pass. This |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3276 | * guarantees forward progress and that we don't miss any tasks. |
| 3277 | */ |
| 3278 | heap->size = 0; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3279 | css_task_iter_start(css, &it); |
| 3280 | while ((p = css_task_iter_next(&it))) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3281 | /* |
| 3282 | * Only affect tasks that qualify per the caller's callback, |
| 3283 | * if he provided one |
| 3284 | */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3285 | if (test && !test(p, data)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3286 | continue; |
| 3287 | /* |
| 3288 | * Only process tasks that started after the last task |
| 3289 | * we processed |
| 3290 | */ |
| 3291 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3292 | continue; |
| 3293 | dropped = heap_insert(heap, p); |
| 3294 | if (dropped == NULL) { |
| 3295 | /* |
| 3296 | * The new task was inserted; the heap wasn't |
| 3297 | * previously full |
| 3298 | */ |
| 3299 | get_task_struct(p); |
| 3300 | } else if (dropped != p) { |
| 3301 | /* |
| 3302 | * The new task was inserted, and pushed out a |
| 3303 | * different task |
| 3304 | */ |
| 3305 | get_task_struct(p); |
| 3306 | put_task_struct(dropped); |
| 3307 | } |
| 3308 | /* |
| 3309 | * Else the new task was newer than anything already in |
| 3310 | * the heap and wasn't inserted |
| 3311 | */ |
| 3312 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3313 | css_task_iter_end(&it); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3314 | |
| 3315 | if (heap->size) { |
| 3316 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3317 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3318 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3319 | latest_time = q->start_time; |
| 3320 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3321 | } |
| 3322 | /* Process the task per the caller's callback */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3323 | process(q, data); |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3324 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3325 | } |
| 3326 | /* |
| 3327 | * If we had to process any tasks at all, scan again |
| 3328 | * in case some of them were in the middle of forking |
| 3329 | * children that didn't get processed. |
| 3330 | * Not the most efficient way to do it, but it avoids |
| 3331 | * having to take callback_mutex in the fork path |
| 3332 | */ |
| 3333 | goto again; |
| 3334 | } |
| 3335 | if (heap == &tmp_heap) |
| 3336 | heap_free(&tmp_heap); |
| 3337 | return 0; |
| 3338 | } |
| 3339 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3340 | static void cgroup_transfer_one_task(struct task_struct *task, void *data) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3341 | { |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3342 | struct cgroup *new_cgroup = data; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3343 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3344 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3345 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3346 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | /** |
| 3350 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3351 | * @to: cgroup to which the tasks will be moved |
| 3352 | * @from: cgroup in which the tasks currently reside |
| 3353 | */ |
| 3354 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3355 | { |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3356 | return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task, |
| 3357 | to, NULL); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3358 | } |
| 3359 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3360 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3361 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3362 | * |
| 3363 | * Reading this file can return large amounts of data if a cgroup has |
| 3364 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3365 | * but we cannot guarantee that the information we produce is correct |
| 3366 | * unless we produce it entirely atomically. |
| 3367 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3368 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3369 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3370 | /* which pidlist file are we talking about? */ |
| 3371 | enum cgroup_filetype { |
| 3372 | CGROUP_FILE_PROCS, |
| 3373 | CGROUP_FILE_TASKS, |
| 3374 | }; |
| 3375 | |
| 3376 | /* |
| 3377 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3378 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3379 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3380 | * to the cgroup. |
| 3381 | */ |
| 3382 | struct cgroup_pidlist { |
| 3383 | /* |
| 3384 | * used to find which pidlist is wanted. doesn't change as long as |
| 3385 | * this particular list stays in the list. |
| 3386 | */ |
| 3387 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3388 | /* array of xids */ |
| 3389 | pid_t *list; |
| 3390 | /* how many elements the above list has */ |
| 3391 | int length; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3392 | /* each of these stored in a list by its cgroup */ |
| 3393 | struct list_head links; |
| 3394 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3395 | struct cgroup *owner; |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3396 | /* for delayed destruction */ |
| 3397 | struct delayed_work destroy_dwork; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3398 | }; |
| 3399 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3400 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3401 | * The following two functions "fix" the issue where there are more pids |
| 3402 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3403 | * TODO: replace with a kernel-wide solution to this problem |
| 3404 | */ |
| 3405 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3406 | static void *pidlist_allocate(int count) |
| 3407 | { |
| 3408 | if (PIDLIST_TOO_LARGE(count)) |
| 3409 | return vmalloc(count * sizeof(pid_t)); |
| 3410 | else |
| 3411 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3412 | } |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3413 | |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3414 | static void pidlist_free(void *p) |
| 3415 | { |
| 3416 | if (is_vmalloc_addr(p)) |
| 3417 | vfree(p); |
| 3418 | else |
| 3419 | kfree(p); |
| 3420 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3421 | |
| 3422 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3423 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 3424 | * should be left afterwards. |
| 3425 | */ |
| 3426 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 3427 | { |
| 3428 | struct cgroup_pidlist *l, *tmp_l; |
| 3429 | |
| 3430 | mutex_lock(&cgrp->pidlist_mutex); |
| 3431 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 3432 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 3433 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3434 | |
| 3435 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 3436 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 3437 | } |
| 3438 | |
| 3439 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 3440 | { |
| 3441 | struct delayed_work *dwork = to_delayed_work(work); |
| 3442 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 3443 | destroy_dwork); |
| 3444 | struct cgroup_pidlist *tofree = NULL; |
| 3445 | |
| 3446 | mutex_lock(&l->owner->pidlist_mutex); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3447 | |
| 3448 | /* |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3449 | * Destroy iff we didn't get queued again. The state won't change |
| 3450 | * as destroy_dwork can only be queued while locked. |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3451 | */ |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3452 | if (!delayed_work_pending(dwork)) { |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3453 | list_del(&l->links); |
| 3454 | pidlist_free(l->list); |
| 3455 | put_pid_ns(l->key.ns); |
| 3456 | tofree = l; |
| 3457 | } |
| 3458 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3459 | mutex_unlock(&l->owner->pidlist_mutex); |
| 3460 | kfree(tofree); |
| 3461 | } |
| 3462 | |
| 3463 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3464 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3465 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3466 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3467 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3468 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3469 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3470 | |
| 3471 | /* |
| 3472 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3473 | * edge cases first; no work needs to be done for either |
| 3474 | */ |
| 3475 | if (length == 0 || length == 1) |
| 3476 | return length; |
| 3477 | /* src and dest walk down the list; dest counts unique elements */ |
| 3478 | for (src = 1; src < length; src++) { |
| 3479 | /* find next unique element */ |
| 3480 | while (list[src] == list[src-1]) { |
| 3481 | src++; |
| 3482 | if (src == length) |
| 3483 | goto after; |
| 3484 | } |
| 3485 | /* dest always points to where the next unique element goes */ |
| 3486 | list[dest] = list[src]; |
| 3487 | dest++; |
| 3488 | } |
| 3489 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3490 | return dest; |
| 3491 | } |
| 3492 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3493 | /* |
| 3494 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 3495 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 3496 | * different per namespace, each namespace needs differently sorted list, |
| 3497 | * making it impossible to use, for example, single rbtree of member tasks |
| 3498 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 3499 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 3500 | * pidlists keyed by cgroup and namespace. |
| 3501 | * |
| 3502 | * All this extra complexity was caused by the original implementation |
| 3503 | * committing to an entirely unnecessary property. In the long term, we |
| 3504 | * want to do away with it. Explicitly scramble sort order if |
| 3505 | * sane_behavior so that no such expectation exists in the new interface. |
| 3506 | * |
| 3507 | * Scrambling is done by swapping every two consecutive bits, which is |
| 3508 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 3509 | */ |
| 3510 | static pid_t pid_fry(pid_t pid) |
| 3511 | { |
| 3512 | unsigned a = pid & 0x55555555; |
| 3513 | unsigned b = pid & 0xAAAAAAAA; |
| 3514 | |
| 3515 | return (a << 1) | (b >> 1); |
| 3516 | } |
| 3517 | |
| 3518 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 3519 | { |
| 3520 | if (cgroup_sane_behavior(cgrp)) |
| 3521 | return pid_fry(pid); |
| 3522 | else |
| 3523 | return pid; |
| 3524 | } |
| 3525 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3526 | static int cmppid(const void *a, const void *b) |
| 3527 | { |
| 3528 | return *(pid_t *)a - *(pid_t *)b; |
| 3529 | } |
| 3530 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3531 | static int fried_cmppid(const void *a, const void *b) |
| 3532 | { |
| 3533 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 3534 | } |
| 3535 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3536 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3537 | enum cgroup_filetype type) |
| 3538 | { |
| 3539 | struct cgroup_pidlist *l; |
| 3540 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3541 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3542 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3543 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3544 | |
| 3545 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 3546 | if (l->key.type == type && l->key.ns == ns) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3547 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3548 | return NULL; |
| 3549 | } |
| 3550 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3551 | /* |
| 3552 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3553 | * returns with the lock on that pidlist already held, and takes care |
| 3554 | * of the use count, or returns NULL with no locks held if we're out of |
| 3555 | * memory. |
| 3556 | */ |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3557 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 3558 | enum cgroup_filetype type) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3559 | { |
| 3560 | struct cgroup_pidlist *l; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3561 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3562 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3563 | |
| 3564 | l = cgroup_pidlist_find(cgrp, type); |
| 3565 | if (l) |
| 3566 | return l; |
| 3567 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3568 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3569 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3570 | if (!l) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3571 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3572 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3573 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3574 | l->key.type = type; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3575 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 3576 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3577 | l->owner = cgrp; |
| 3578 | list_add(&l->links, &cgrp->pidlists); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3579 | return l; |
| 3580 | } |
| 3581 | |
| 3582 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3583 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3584 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3585 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3586 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3587 | { |
| 3588 | pid_t *array; |
| 3589 | int length; |
| 3590 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3591 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3592 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3593 | struct cgroup_pidlist *l; |
| 3594 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3595 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3596 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3597 | /* |
| 3598 | * If cgroup gets more users after we read count, we won't have |
| 3599 | * enough space - tough. This race is indistinguishable to the |
| 3600 | * caller from the case that the additional cgroup users didn't |
| 3601 | * show up until sometime later on. |
| 3602 | */ |
| 3603 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3604 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3605 | if (!array) |
| 3606 | return -ENOMEM; |
| 3607 | /* now, populate the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3608 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3609 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3610 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3611 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3612 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3613 | if (type == CGROUP_FILE_PROCS) |
| 3614 | pid = task_tgid_vnr(tsk); |
| 3615 | else |
| 3616 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3617 | if (pid > 0) /* make sure to only use valid results */ |
| 3618 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3619 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3620 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3621 | length = n; |
| 3622 | /* now sort & (if procs) strip out duplicates */ |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3623 | if (cgroup_sane_behavior(cgrp)) |
| 3624 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 3625 | else |
| 3626 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3627 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3628 | length = pidlist_uniq(array, length); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3629 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3630 | l = cgroup_pidlist_find_create(cgrp, type); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3631 | if (!l) { |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3632 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3633 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3634 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3635 | } |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3636 | |
| 3637 | /* store array, freeing old if necessary */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3638 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3639 | l->list = array; |
| 3640 | l->length = length; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3641 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3642 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3643 | } |
| 3644 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3645 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3646 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3647 | * @stats: cgroupstats to fill information into |
| 3648 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3649 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3650 | * |
| 3651 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3652 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3653 | */ |
| 3654 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3655 | { |
| 3656 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3657 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3658 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3659 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3660 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3661 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3662 | * Validate dentry by checking the superblock operations, |
| 3663 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3664 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3665 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3666 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3667 | goto err; |
| 3668 | |
| 3669 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3670 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3671 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3672 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3673 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3674 | switch (tsk->state) { |
| 3675 | case TASK_RUNNING: |
| 3676 | stats->nr_running++; |
| 3677 | break; |
| 3678 | case TASK_INTERRUPTIBLE: |
| 3679 | stats->nr_sleeping++; |
| 3680 | break; |
| 3681 | case TASK_UNINTERRUPTIBLE: |
| 3682 | stats->nr_uninterruptible++; |
| 3683 | break; |
| 3684 | case TASK_STOPPED: |
| 3685 | stats->nr_stopped++; |
| 3686 | break; |
| 3687 | default: |
| 3688 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3689 | stats->nr_io_wait++; |
| 3690 | break; |
| 3691 | } |
| 3692 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3693 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3694 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3695 | err: |
| 3696 | return ret; |
| 3697 | } |
| 3698 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3699 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3700 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3701 | * 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] | 3702 | * 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] | 3703 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3704 | */ |
| 3705 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3706 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3707 | { |
| 3708 | /* |
| 3709 | * Initially we receive a position value that corresponds to |
| 3710 | * one more than the last pid shown (or 0 on the first call or |
| 3711 | * after a seek to the start). Use a binary-search to find the |
| 3712 | * next pid to display, if any |
| 3713 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3714 | struct cgroup_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3715 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3716 | struct cgroup_pidlist *l; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3717 | enum cgroup_filetype type = seq_cft(s)->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3718 | int index = 0, pid = *pos; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3719 | int *iter, ret; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3720 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3721 | mutex_lock(&cgrp->pidlist_mutex); |
| 3722 | |
| 3723 | /* |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3724 | * !NULL @of->priv indicates that this isn't the first start() |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3725 | * after open. If the matching pidlist is around, we can use that. |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3726 | * 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] | 3727 | * could already have been destroyed. |
| 3728 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3729 | if (of->priv) |
| 3730 | of->priv = cgroup_pidlist_find(cgrp, type); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3731 | |
| 3732 | /* |
| 3733 | * Either this is the first start() after open or the matching |
| 3734 | * pidlist has been destroyed inbetween. Create a new one. |
| 3735 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3736 | if (!of->priv) { |
| 3737 | ret = pidlist_array_load(cgrp, type, |
| 3738 | (struct cgroup_pidlist **)&of->priv); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3739 | if (ret) |
| 3740 | return ERR_PTR(ret); |
| 3741 | } |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3742 | l = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3743 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3744 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3745 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3746 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3747 | while (index < end) { |
| 3748 | int mid = (index + end) / 2; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3749 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3750 | index = mid; |
| 3751 | break; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3752 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3753 | index = mid + 1; |
| 3754 | else |
| 3755 | end = mid; |
| 3756 | } |
| 3757 | } |
| 3758 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3759 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3760 | return NULL; |
| 3761 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3762 | iter = l->list + index; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3763 | *pos = cgroup_pid_fry(cgrp, *iter); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3764 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3765 | } |
| 3766 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3767 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3768 | { |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3769 | struct cgroup_open_file *of = s->private; |
| 3770 | struct cgroup_pidlist *l = of->priv; |
Tejun Heo | 6223685 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3771 | |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3772 | if (l) |
| 3773 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3774 | CGROUP_PIDLIST_DESTROY_DELAY); |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3775 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3776 | } |
| 3777 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3778 | 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] | 3779 | { |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3780 | struct cgroup_open_file *of = s->private; |
| 3781 | struct cgroup_pidlist *l = of->priv; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3782 | pid_t *p = v; |
| 3783 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3784 | /* |
| 3785 | * Advance to the next pid in the array. If this goes off the |
| 3786 | * end, we're done |
| 3787 | */ |
| 3788 | p++; |
| 3789 | if (p >= end) { |
| 3790 | return NULL; |
| 3791 | } else { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3792 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3793 | return p; |
| 3794 | } |
| 3795 | } |
| 3796 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3797 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3798 | { |
| 3799 | return seq_printf(s, "%d\n", *(int *)v); |
| 3800 | } |
| 3801 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3802 | /* |
| 3803 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3804 | * independent of whether it's tasks or procs |
| 3805 | */ |
| 3806 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3807 | .start = cgroup_pidlist_start, |
| 3808 | .stop = cgroup_pidlist_stop, |
| 3809 | .next = cgroup_pidlist_next, |
| 3810 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3811 | }; |
| 3812 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3813 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3814 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3815 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3816 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3817 | } |
| 3818 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3819 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3820 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3821 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3822 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3823 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3824 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3825 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3826 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3827 | return 0; |
| 3828 | } |
| 3829 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3830 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3831 | * When dput() is called asynchronously, if umount has been done and |
| 3832 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3833 | * there's a small window that vfs will see the root dentry with non-zero |
| 3834 | * refcnt and trigger BUG(). |
| 3835 | * |
| 3836 | * That's why we hold a reference before dput() and drop it right after. |
| 3837 | */ |
| 3838 | static void cgroup_dput(struct cgroup *cgrp) |
| 3839 | { |
| 3840 | struct super_block *sb = cgrp->root->sb; |
| 3841 | |
| 3842 | atomic_inc(&sb->s_active); |
| 3843 | dput(cgrp->dentry); |
| 3844 | deactivate_super(sb); |
| 3845 | } |
| 3846 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3847 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 3848 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3849 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3850 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3851 | } |
| 3852 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3853 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 3854 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3855 | { |
| 3856 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3857 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3858 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3859 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3860 | return 0; |
| 3861 | } |
| 3862 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3863 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3864 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3865 | .name = "cgroup.procs", |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3866 | .seq_start = cgroup_pidlist_start, |
| 3867 | .seq_next = cgroup_pidlist_next, |
| 3868 | .seq_stop = cgroup_pidlist_stop, |
| 3869 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3870 | .private = CGROUP_FILE_PROCS, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3871 | .write_u64 = cgroup_procs_write, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3872 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3873 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3874 | { |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3875 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3876 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3877 | .read_u64 = cgroup_clone_children_read, |
| 3878 | .write_u64 = cgroup_clone_children_write, |
| 3879 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3880 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3881 | .name = "cgroup.sane_behavior", |
| 3882 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3883 | .seq_show = cgroup_sane_behavior_show, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3884 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3885 | |
| 3886 | /* |
| 3887 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 3888 | * don't exist if sane_behavior. If you're depending on these, be |
| 3889 | * prepared to be burned. |
| 3890 | */ |
| 3891 | { |
| 3892 | .name = "tasks", |
| 3893 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3894 | .seq_start = cgroup_pidlist_start, |
| 3895 | .seq_next = cgroup_pidlist_next, |
| 3896 | .seq_stop = cgroup_pidlist_stop, |
| 3897 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3898 | .private = CGROUP_FILE_TASKS, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3899 | .write_u64 = cgroup_tasks_write, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3900 | .mode = S_IRUGO | S_IWUSR, |
| 3901 | }, |
| 3902 | { |
| 3903 | .name = "notify_on_release", |
| 3904 | .flags = CFTYPE_INSANE, |
| 3905 | .read_u64 = cgroup_read_notify_on_release, |
| 3906 | .write_u64 = cgroup_write_notify_on_release, |
| 3907 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3908 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3909 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 3910 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3911 | .seq_show = cgroup_release_agent_show, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3912 | .write_string = cgroup_release_agent_write, |
| 3913 | .max_write_len = PATH_MAX, |
| 3914 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3915 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3916 | }; |
| 3917 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3918 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3919 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3920 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3921 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3922 | * |
| 3923 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3924 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3925 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3926 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3927 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3928 | int i, ret = 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3929 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3930 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3931 | for_each_subsys(ss, i) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3932 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3933 | |
| 3934 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3935 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3936 | |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3937 | list_for_each_entry(set, &ss->cftsets, node) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3938 | ret = cgroup_addrm_files(cgrp, set->cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3939 | if (ret < 0) |
| 3940 | goto err; |
| 3941 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3942 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3943 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3944 | err: |
| 3945 | cgroup_clear_dir(cgrp, subsys_mask); |
| 3946 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3947 | } |
| 3948 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3949 | /* |
| 3950 | * css destruction is four-stage process. |
| 3951 | * |
| 3952 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 3953 | * Implemented in kill_css(). |
| 3954 | * |
| 3955 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
| 3956 | * and thus css_tryget() is guaranteed to fail, the css can be offlined |
| 3957 | * by invoking offline_css(). After offlining, the base ref is put. |
| 3958 | * Implemented in css_killed_work_fn(). |
| 3959 | * |
| 3960 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 3961 | * accessors are inside RCU read sections. css_release() schedules the |
| 3962 | * RCU callback. |
| 3963 | * |
| 3964 | * 4. After the grace period, the css can be freed. Implemented in |
| 3965 | * css_free_work_fn(). |
| 3966 | * |
| 3967 | * It is actually hairier because both step 2 and 4 require process context |
| 3968 | * and thus involve punting to css->destroy_work adding two additional |
| 3969 | * steps to the already complex sequence. |
| 3970 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3971 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3972 | { |
| 3973 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3974 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3975 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3976 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3977 | if (css->parent) |
| 3978 | css_put(css->parent); |
| 3979 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3980 | css->ss->css_free(css); |
| 3981 | cgroup_dput(cgrp); |
| 3982 | } |
| 3983 | |
| 3984 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 3985 | { |
| 3986 | struct cgroup_subsys_state *css = |
| 3987 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 3988 | |
| 3989 | /* |
| 3990 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 3991 | * css_put(). dput() requires process context which we don't have. |
| 3992 | */ |
| 3993 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 3994 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3995 | } |
| 3996 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3997 | static void css_release(struct percpu_ref *ref) |
| 3998 | { |
| 3999 | struct cgroup_subsys_state *css = |
| 4000 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4001 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4002 | rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4003 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4004 | } |
| 4005 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4006 | static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss, |
| 4007 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4008 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4009 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4010 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4011 | css->flags = 0; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4012 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4013 | if (cgrp->parent) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4014 | css->parent = cgroup_css(cgrp->parent, ss); |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4015 | else |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4016 | css->flags |= CSS_ROOT; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4017 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4018 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4019 | } |
| 4020 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4021 | /* 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] | 4022 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4023 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4024 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4025 | int ret = 0; |
| 4026 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4027 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4028 | lockdep_assert_held(&cgroup_mutex); |
| 4029 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4030 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4031 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4032 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4033 | css->flags |= CSS_ONLINE; |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4034 | css->cgroup->nr_css++; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4035 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4036 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4037 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4038 | } |
| 4039 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4040 | /* 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] | 4041 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4042 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4043 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4044 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4045 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4046 | lockdep_assert_held(&cgroup_mutex); |
| 4047 | |
| 4048 | if (!(css->flags & CSS_ONLINE)) |
| 4049 | return; |
| 4050 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4051 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4052 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4053 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4054 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4055 | css->cgroup->nr_css--; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4056 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4057 | } |
| 4058 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4059 | /** |
| 4060 | * create_css - create a cgroup_subsys_state |
| 4061 | * @cgrp: the cgroup new css will be associated with |
| 4062 | * @ss: the subsys of new css |
| 4063 | * |
| 4064 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
| 4065 | * css is online and installed in @cgrp with all interface files created. |
| 4066 | * Returns 0 on success, -errno on failure. |
| 4067 | */ |
| 4068 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss) |
| 4069 | { |
| 4070 | struct cgroup *parent = cgrp->parent; |
| 4071 | struct cgroup_subsys_state *css; |
| 4072 | int err; |
| 4073 | |
| 4074 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 4075 | lockdep_assert_held(&cgroup_mutex); |
| 4076 | |
| 4077 | css = ss->css_alloc(cgroup_css(parent, ss)); |
| 4078 | if (IS_ERR(css)) |
| 4079 | return PTR_ERR(css); |
| 4080 | |
| 4081 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4082 | if (err) |
| 4083 | goto err_free; |
| 4084 | |
| 4085 | init_css(css, ss, cgrp); |
| 4086 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4087 | err = cgroup_populate_dir(cgrp, 1 << ss->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4088 | if (err) |
| 4089 | goto err_free; |
| 4090 | |
| 4091 | err = online_css(css); |
| 4092 | if (err) |
| 4093 | goto err_free; |
| 4094 | |
| 4095 | dget(cgrp->dentry); |
| 4096 | css_get(css->parent); |
| 4097 | |
| 4098 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4099 | parent->parent) { |
| 4100 | 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", |
| 4101 | current->comm, current->pid, ss->name); |
| 4102 | if (!strcmp(ss->name, "memory")) |
| 4103 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4104 | ss->warned_broken_hierarchy = true; |
| 4105 | } |
| 4106 | |
| 4107 | return 0; |
| 4108 | |
| 4109 | err_free: |
| 4110 | percpu_ref_cancel_init(&css->refcnt); |
| 4111 | ss->css_free(css); |
| 4112 | return err; |
| 4113 | } |
| 4114 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4115 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4116 | * cgroup_create - create a cgroup |
| 4117 | * @parent: cgroup that will be parent of the new cgroup |
| 4118 | * @dentry: dentry of the new cgroup |
| 4119 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4120 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4121 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4122 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4123 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4124 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4125 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4126 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4127 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4128 | struct cgroupfs_root *root = parent->root; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 4129 | int ssid, err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4130 | struct cgroup_subsys *ss; |
| 4131 | struct super_block *sb = root->sb; |
| 4132 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4133 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4134 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4135 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4136 | return -ENOMEM; |
| 4137 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4138 | name = cgroup_alloc_name(dentry); |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 4139 | if (!name) { |
| 4140 | err = -ENOMEM; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4141 | goto err_free_cgrp; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 4142 | } |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4143 | rcu_assign_pointer(cgrp->name, name); |
| 4144 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4145 | mutex_lock(&cgroup_tree_mutex); |
| 4146 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4147 | /* |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4148 | * Only live parents can have children. Note that the liveliness |
| 4149 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4150 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4151 | * anyway so that locking is contained inside cgroup proper and we |
| 4152 | * don't get nasty surprises if we ever grow another caller. |
| 4153 | */ |
| 4154 | if (!cgroup_lock_live_group(parent)) { |
| 4155 | err = -ENODEV; |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4156 | goto err_unlock_tree; |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4157 | } |
| 4158 | |
| 4159 | /* |
| 4160 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4161 | * a half-baked cgroup. |
| 4162 | */ |
| 4163 | cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL); |
| 4164 | if (cgrp->id < 0) { |
| 4165 | err = -ENOMEM; |
| 4166 | goto err_unlock; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4167 | } |
| 4168 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4169 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4170 | * get deleted on unmount if there are child cgroups. This |
| 4171 | * can be done outside cgroup_mutex, since the sb can't |
| 4172 | * disappear while someone has an open control file on the |
| 4173 | * fs */ |
| 4174 | atomic_inc(&sb->s_active); |
| 4175 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4176 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4177 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4178 | dentry->d_fsdata = cgrp; |
| 4179 | cgrp->dentry = dentry; |
| 4180 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4181 | cgrp->parent = parent; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4182 | cgrp->dummy_css.parent = &parent->dummy_css; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4183 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4184 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4185 | if (notify_on_release(parent)) |
| 4186 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4187 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4188 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4189 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4190 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4191 | /* |
| 4192 | * Create directory. cgroup_create_file() returns with the new |
| 4193 | * directory locked on success so that it can be populated without |
| 4194 | * dropping cgroup_mutex. |
| 4195 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4196 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4197 | if (err < 0) |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4198 | goto err_free_id; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4199 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4200 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4201 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4202 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4203 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4204 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4205 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4206 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4207 | /* hold a ref to the parent's dentry */ |
| 4208 | dget(parent->dentry); |
| 4209 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4210 | /* |
| 4211 | * @cgrp is now fully operational. If something fails after this |
| 4212 | * point, it'll be released via the normal destruction path. |
| 4213 | */ |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4214 | idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 4215 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4216 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4217 | if (err) |
| 4218 | goto err_destroy; |
| 4219 | |
Tejun Heo | 9d403e9 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4220 | /* let's create and online css's */ |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4221 | for_each_subsys(ss, ssid) { |
| 4222 | if (root->subsys_mask & (1 << ssid)) { |
| 4223 | err = create_css(cgrp, ss); |
| 4224 | if (err) |
| 4225 | goto err_destroy; |
| 4226 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4227 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4228 | |
| 4229 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4230 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4231 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4232 | |
| 4233 | return 0; |
| 4234 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4235 | err_free_id: |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4236 | idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4237 | /* Release the reference count that we took on the superblock */ |
| 4238 | deactivate_super(sb); |
| 4239 | err_unlock: |
| 4240 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4241 | err_unlock_tree: |
| 4242 | mutex_unlock(&cgroup_tree_mutex); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4243 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4244 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4245 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4246 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4247 | |
| 4248 | err_destroy: |
| 4249 | cgroup_destroy_locked(cgrp); |
| 4250 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4251 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4252 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4253 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4254 | } |
| 4255 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4256 | 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] | 4257 | { |
| 4258 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4259 | |
| 4260 | /* the vfs holds inode->i_mutex already */ |
| 4261 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4262 | } |
| 4263 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4264 | /* |
| 4265 | * This is called when the refcnt of a css is confirmed to be killed. |
| 4266 | * css_tryget() is now guaranteed to fail. |
| 4267 | */ |
| 4268 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4269 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4270 | struct cgroup_subsys_state *css = |
| 4271 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 4272 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4273 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4274 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4275 | mutex_lock(&cgroup_mutex); |
| 4276 | |
| 4277 | /* |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4278 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4279 | * initate destruction. |
| 4280 | */ |
| 4281 | offline_css(css); |
| 4282 | |
| 4283 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4284 | * If @cgrp is marked dead, it's waiting for refs of all css's to |
| 4285 | * be disabled before proceeding to the second phase of cgroup |
| 4286 | * destruction. If we are the last one, kick it off. |
| 4287 | */ |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4288 | if (!cgrp->nr_css && cgroup_is_dead(cgrp)) |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4289 | cgroup_destroy_css_killed(cgrp); |
| 4290 | |
| 4291 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4292 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4293 | |
| 4294 | /* |
| 4295 | * Put the css refs from kill_css(). Each css holds an extra |
| 4296 | * reference to the cgroup's dentry and cgroup removal proceeds |
| 4297 | * regardless of css refs. On the last put of each css, whenever |
| 4298 | * that may be, the extra dentry ref is put so that dentry |
| 4299 | * destruction happens only after all css's are released. |
| 4300 | */ |
| 4301 | css_put(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4302 | } |
| 4303 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4304 | /* css kill confirmation processing requires process context, bounce */ |
| 4305 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4306 | { |
| 4307 | struct cgroup_subsys_state *css = |
| 4308 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4309 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4310 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4311 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4312 | } |
| 4313 | |
| 4314 | /** |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4315 | * kill_css - destroy a css |
| 4316 | * @css: css to destroy |
| 4317 | * |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4318 | * This function initiates destruction of @css by removing cgroup interface |
| 4319 | * files and putting its base reference. ->css_offline() will be invoked |
| 4320 | * asynchronously once css_tryget() is guaranteed to fail and when the |
| 4321 | * reference count reaches zero, @css will be released. |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4322 | */ |
| 4323 | static void kill_css(struct cgroup_subsys_state *css) |
| 4324 | { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4325 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4326 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4327 | /* |
| 4328 | * Killing would put the base ref, but we need to keep it alive |
| 4329 | * until after ->css_offline(). |
| 4330 | */ |
| 4331 | css_get(css); |
| 4332 | |
| 4333 | /* |
| 4334 | * cgroup core guarantees that, by the time ->css_offline() is |
| 4335 | * invoked, no new css reference will be given out via |
| 4336 | * css_tryget(). We can't simply call percpu_ref_kill() and |
| 4337 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 4338 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 4339 | * |
| 4340 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4341 | * css is confirmed to be seen as killed on all CPUs. |
| 4342 | */ |
| 4343 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4344 | } |
| 4345 | |
| 4346 | /** |
| 4347 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4348 | * @cgrp: cgroup to be destroyed |
| 4349 | * |
| 4350 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4351 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4352 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4353 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4354 | * the following two steps. |
| 4355 | * |
| 4356 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4357 | * userland visible parts and start killing the percpu refcnts of |
| 4358 | * css's. Set up so that the next stage will be kicked off once all |
| 4359 | * the percpu refcnts are confirmed to be killed. |
| 4360 | * |
| 4361 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4362 | * rest of destruction. Once all cgroup references are gone, the |
| 4363 | * cgroup is RCU-freed. |
| 4364 | * |
| 4365 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4366 | * the userland is concerned and a new cgroup with the same name may be |
| 4367 | * created. As cgroup doesn't care about the names internally, this |
| 4368 | * doesn't cause any problem. |
| 4369 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4370 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4371 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4372 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4373 | struct dentry *d = cgrp->dentry; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4374 | struct cgroup_subsys_state *css; |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4375 | struct cgroup *child; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4376 | bool empty; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4377 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4378 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4379 | lockdep_assert_held(&d->d_inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4380 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4381 | lockdep_assert_held(&cgroup_mutex); |
| 4382 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4383 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4384 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4385 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4386 | */ |
| 4387 | read_lock(&css_set_lock); |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4388 | empty = list_empty(&cgrp->cset_links); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4389 | read_unlock(&css_set_lock); |
| 4390 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4391 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4392 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4393 | /* |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4394 | * Make sure there's no live children. We can't test ->children |
| 4395 | * emptiness as dead children linger on it while being destroyed; |
| 4396 | * otherwise, "rmdir parent/child parent" may fail with -EBUSY. |
| 4397 | */ |
| 4398 | empty = true; |
| 4399 | rcu_read_lock(); |
| 4400 | list_for_each_entry_rcu(child, &cgrp->children, sibling) { |
| 4401 | empty = cgroup_is_dead(child); |
| 4402 | if (!empty) |
| 4403 | break; |
| 4404 | } |
| 4405 | rcu_read_unlock(); |
| 4406 | if (!empty) |
| 4407 | return -EBUSY; |
| 4408 | |
| 4409 | /* |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4410 | * Initiate massacre of all css's. cgroup_destroy_css_killed() |
| 4411 | * will be invoked to perform the rest of destruction once the |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4412 | * percpu refs of all css's are confirmed to be killed. This |
| 4413 | * involves removing the subsystem's files, drop cgroup_mutex. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4414 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4415 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4416 | for_each_css(css, ssid, cgrp) |
| 4417 | kill_css(css); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4418 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4419 | |
| 4420 | /* |
| 4421 | * Mark @cgrp dead. This prevents further task migration and child |
| 4422 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4423 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4424 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4425 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4426 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4427 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4428 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4429 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4430 | raw_spin_lock(&release_list_lock); |
| 4431 | if (!list_empty(&cgrp->release_list)) |
| 4432 | list_del_init(&cgrp->release_list); |
| 4433 | raw_spin_unlock(&release_list_lock); |
| 4434 | |
| 4435 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4436 | * If @cgrp has css's attached, the second stage of cgroup |
| 4437 | * destruction is kicked off from css_killed_work_fn() after the |
| 4438 | * refs of all attached css's are killed. If @cgrp doesn't have |
| 4439 | * any css, we kick it off here. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4440 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4441 | if (!cgrp->nr_css) |
| 4442 | cgroup_destroy_css_killed(cgrp); |
| 4443 | |
| 4444 | /* |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4445 | * Clear the base files and remove @cgrp directory. The removal |
| 4446 | * puts the base ref but we aren't quite done with @cgrp yet, so |
| 4447 | * hold onto it. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4448 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4449 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4450 | cgroup_addrm_files(cgrp, cgroup_base_files, false); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4451 | dget(d); |
| 4452 | cgroup_d_remove_dir(d); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4453 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4454 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4455 | return 0; |
| 4456 | }; |
| 4457 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4458 | /** |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4459 | * cgroup_destroy_css_killed - the second step of cgroup destruction |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4460 | * @work: cgroup->destroy_free_work |
| 4461 | * |
| 4462 | * 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] | 4463 | * destroyed after all css's are offlined and performs the rest of |
| 4464 | * destruction. This is the second step of destruction described in the |
| 4465 | * comment above cgroup_destroy_locked(). |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4466 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4467 | static void cgroup_destroy_css_killed(struct cgroup *cgrp) |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4468 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4469 | struct cgroup *parent = cgrp->parent; |
| 4470 | struct dentry *d = cgrp->dentry; |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4471 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4472 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4473 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4474 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4475 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4476 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4477 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4478 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4479 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4480 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4481 | check_for_release(parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4482 | } |
| 4483 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4484 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4485 | { |
| 4486 | int ret; |
| 4487 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4488 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4489 | mutex_lock(&cgroup_mutex); |
| 4490 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4491 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4492 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4493 | |
| 4494 | return ret; |
| 4495 | } |
| 4496 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4497 | static void __init cgroup_init_cftsets(struct cgroup_subsys *ss) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4498 | { |
| 4499 | INIT_LIST_HEAD(&ss->cftsets); |
| 4500 | |
| 4501 | /* |
| 4502 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4503 | * deregistration. |
| 4504 | */ |
| 4505 | if (ss->base_cftypes) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4506 | struct cftype *cft; |
| 4507 | |
| 4508 | for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++) |
| 4509 | cft->ss = ss; |
| 4510 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4511 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4512 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4513 | } |
| 4514 | } |
| 4515 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4516 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4517 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4518 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4519 | |
| 4520 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4521 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4522 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4523 | mutex_lock(&cgroup_mutex); |
| 4524 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4525 | /* init base cftset */ |
| 4526 | cgroup_init_cftsets(ss); |
| 4527 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4528 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4529 | ss->root = &cgroup_dummy_root; |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4530 | css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4531 | /* We don't handle early failures gracefully */ |
| 4532 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4533 | init_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4534 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4535 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4536 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4537 | * newly registered, all tasks and hence the |
| 4538 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4539 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4540 | |
| 4541 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4542 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4543 | /* At system boot, before all subsystems have been |
| 4544 | * registered, no tasks have been forked, so we don't |
| 4545 | * need to invoke fork callbacks here. */ |
| 4546 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4547 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4548 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4549 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4550 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4551 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4552 | } |
| 4553 | |
| 4554 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4555 | * cgroup_init_early - cgroup initialization at system boot |
| 4556 | * |
| 4557 | * Initialize cgroups at system boot, and initialize any |
| 4558 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4559 | */ |
| 4560 | int __init cgroup_init_early(void) |
| 4561 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4562 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4563 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4564 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4565 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4566 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4567 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4568 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4569 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4570 | init_cgroup_root(&cgroup_dummy_root); |
| 4571 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4572 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4573 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4574 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4575 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4576 | 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] | 4577 | 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] | 4578 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4579 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4580 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4581 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n", |
| 4582 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4583 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4584 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 4585 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
| 4586 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4587 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4588 | ss->name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4589 | |
| 4590 | if (ss->early_init) |
| 4591 | cgroup_init_subsys(ss); |
| 4592 | } |
| 4593 | return 0; |
| 4594 | } |
| 4595 | |
| 4596 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4597 | * cgroup_init - cgroup initialization |
| 4598 | * |
| 4599 | * Register cgroup filesystem and /proc file, and initialize |
| 4600 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4601 | */ |
| 4602 | int __init cgroup_init(void) |
| 4603 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4604 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4605 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4606 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4607 | |
| 4608 | err = bdi_init(&cgroup_backing_dev_info); |
| 4609 | if (err) |
| 4610 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4611 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4612 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4613 | if (!ss->early_init) |
| 4614 | cgroup_init_subsys(ss); |
| 4615 | } |
| 4616 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4617 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4618 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4619 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4620 | /* Add init_css_set to the hash table */ |
| 4621 | key = css_set_hash(init_css_set.subsys); |
| 4622 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4623 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4624 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4625 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4626 | err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top, |
| 4627 | 0, 1, GFP_KERNEL); |
| 4628 | BUG_ON(err < 0); |
| 4629 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4630 | mutex_unlock(&cgroup_mutex); |
| 4631 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4632 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4633 | if (!cgroup_kobj) { |
| 4634 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4635 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | err = register_filesystem(&cgroup_fs_type); |
| 4639 | if (err < 0) { |
| 4640 | kobject_put(cgroup_kobj); |
| 4641 | goto out; |
| 4642 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4643 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4644 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4645 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4646 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4647 | if (err) |
| 4648 | bdi_destroy(&cgroup_backing_dev_info); |
| 4649 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4650 | return err; |
| 4651 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4652 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4653 | static int __init cgroup_wq_init(void) |
| 4654 | { |
| 4655 | /* |
| 4656 | * There isn't much point in executing destruction path in |
| 4657 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Hugh Dickins | ab3f5fa | 2014-02-06 15:56:01 -0800 | [diff] [blame] | 4658 | * |
| 4659 | * XXX: Must be ordered to make sure parent is offlined after |
| 4660 | * children. The ordering requirement is for memcg where a |
| 4661 | * parent's offline may wait for a child's leading to deadlock. In |
| 4662 | * the long term, this should be fixed from memcg side. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4663 | * |
| 4664 | * We would prefer to do this in cgroup_init() above, but that |
| 4665 | * is called before init_workqueues(): so leave this until after. |
| 4666 | */ |
Hugh Dickins | ab3f5fa | 2014-02-06 15:56:01 -0800 | [diff] [blame] | 4667 | cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4668 | BUG_ON(!cgroup_destroy_wq); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 4669 | |
| 4670 | /* |
| 4671 | * Used to destroy pidlists and separate to serve as flush domain. |
| 4672 | * Cap @max_active to 1 too. |
| 4673 | */ |
| 4674 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 4675 | 0, 1); |
| 4676 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 4677 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4678 | return 0; |
| 4679 | } |
| 4680 | core_initcall(cgroup_wq_init); |
| 4681 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4682 | /* |
| 4683 | * proc_cgroup_show() |
| 4684 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4685 | * - Used for /proc/<pid>/cgroup. |
| 4686 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4687 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4688 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4689 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4690 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4691 | * cgroup to top_cgroup. |
| 4692 | */ |
| 4693 | |
| 4694 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4695 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4696 | { |
| 4697 | struct pid *pid; |
| 4698 | struct task_struct *tsk; |
| 4699 | char *buf; |
| 4700 | int retval; |
| 4701 | struct cgroupfs_root *root; |
| 4702 | |
| 4703 | retval = -ENOMEM; |
| 4704 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4705 | if (!buf) |
| 4706 | goto out; |
| 4707 | |
| 4708 | retval = -ESRCH; |
| 4709 | pid = m->private; |
| 4710 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4711 | if (!tsk) |
| 4712 | goto out_free; |
| 4713 | |
| 4714 | retval = 0; |
| 4715 | |
| 4716 | mutex_lock(&cgroup_mutex); |
| 4717 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4718 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4719 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4720 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4721 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4722 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4723 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4724 | for_each_subsys(ss, ssid) |
| 4725 | if (root->subsys_mask & (1 << ssid)) |
| 4726 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4727 | if (strlen(root->name)) |
| 4728 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4729 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4730 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4731 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4732 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4733 | if (retval < 0) |
| 4734 | goto out_unlock; |
| 4735 | seq_puts(m, buf); |
| 4736 | seq_putc(m, '\n'); |
| 4737 | } |
| 4738 | |
| 4739 | out_unlock: |
| 4740 | mutex_unlock(&cgroup_mutex); |
| 4741 | put_task_struct(tsk); |
| 4742 | out_free: |
| 4743 | kfree(buf); |
| 4744 | out: |
| 4745 | return retval; |
| 4746 | } |
| 4747 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4748 | /* Display information about each subsystem and each hierarchy */ |
| 4749 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4750 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4751 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4752 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4753 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4754 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4755 | /* |
| 4756 | * ideally we don't want subsystems moving around while we do this. |
| 4757 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4758 | * subsys/hierarchy state. |
| 4759 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4760 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4761 | |
| 4762 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4763 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4764 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4765 | ss->root->number_of_cgroups, !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4766 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4767 | mutex_unlock(&cgroup_mutex); |
| 4768 | return 0; |
| 4769 | } |
| 4770 | |
| 4771 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4772 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4773 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4774 | } |
| 4775 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4776 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4777 | .open = cgroupstats_open, |
| 4778 | .read = seq_read, |
| 4779 | .llseek = seq_lseek, |
| 4780 | .release = single_release, |
| 4781 | }; |
| 4782 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4783 | /** |
| 4784 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4785 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4786 | * |
| 4787 | * Description: A task inherits its parent's cgroup at fork(). |
| 4788 | * |
| 4789 | * A pointer to the shared css_set was automatically copied in |
| 4790 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4791 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 4792 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 4793 | * have already changed current->cgroups, allowing the previously |
| 4794 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4795 | * |
| 4796 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 4797 | * task, and the passed argument 'child' points to the child task. |
| 4798 | */ |
| 4799 | void cgroup_fork(struct task_struct *child) |
| 4800 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4801 | task_lock(current); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4802 | get_css_set(task_css_set(current)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4803 | child->cgroups = current->cgroups; |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4804 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4805 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4809 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4810 | * @child: the task in question |
| 4811 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4812 | * Adds the task to the list running through its css_set if necessary and |
| 4813 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4814 | * 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] | 4815 | * 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] | 4816 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4817 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4818 | void cgroup_post_fork(struct task_struct *child) |
| 4819 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4820 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4821 | int i; |
| 4822 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4823 | /* |
| 4824 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 4825 | * under the tasklist_lock and we read it here after we added the child |
| 4826 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 4827 | * yet in the tasklist when we walked through it from |
| 4828 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 4829 | * should be visible now due to the paired locking and barriers implied |
| 4830 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 4831 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 4832 | * lock on fork. |
| 4833 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4834 | if (use_task_css_set_links) { |
| 4835 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4836 | task_lock(child); |
| 4837 | if (list_empty(&child->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4838 | list_add(&child->cg_list, &task_css_set(child)->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4839 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4840 | write_unlock(&css_set_lock); |
| 4841 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4842 | |
| 4843 | /* |
| 4844 | * Call ss->fork(). This must happen after @child is linked on |
| 4845 | * css_set; otherwise, @child might change state between ->fork() |
| 4846 | * and addition to css_set. |
| 4847 | */ |
| 4848 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4849 | for_each_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4850 | if (ss->fork) |
| 4851 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4852 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4853 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4854 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4855 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4856 | * cgroup_exit - detach cgroup from exiting task |
| 4857 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4858 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4859 | * |
| 4860 | * Description: Detach cgroup from @tsk and release it. |
| 4861 | * |
| 4862 | * Note that cgroups marked notify_on_release force every task in |
| 4863 | * them to take the global cgroup_mutex mutex when exiting. |
| 4864 | * This could impact scaling on very large systems. Be reluctant to |
| 4865 | * use notify_on_release cgroups where very high task exit scaling |
| 4866 | * is required on large systems. |
| 4867 | * |
| 4868 | * the_top_cgroup_hack: |
| 4869 | * |
| 4870 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 4871 | * |
| 4872 | * We call cgroup_exit() while the task is still competent to |
| 4873 | * handle notify_on_release(), then leave the task attached to the |
| 4874 | * root cgroup in each hierarchy for the remainder of its exit. |
| 4875 | * |
| 4876 | * To do this properly, we would increment the reference count on |
| 4877 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 4878 | * code we would add a second cgroup function call, to drop that |
| 4879 | * reference. This would just create an unnecessary hot spot on |
| 4880 | * the top_cgroup reference count, to no avail. |
| 4881 | * |
| 4882 | * Normally, holding a reference to a cgroup without bumping its |
| 4883 | * count is unsafe. The cgroup could go away, or someone could |
| 4884 | * attach us to a different cgroup, decrementing the count on |
| 4885 | * the first cgroup that we never incremented. But in this case, |
| 4886 | * 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] | 4887 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 4888 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4889 | */ |
| 4890 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 4891 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4892 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4893 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4894 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4895 | |
| 4896 | /* |
| 4897 | * Unlink from the css_set task list if necessary. |
| 4898 | * Optimistically check cg_list before taking |
| 4899 | * css_set_lock |
| 4900 | */ |
| 4901 | if (!list_empty(&tsk->cg_list)) { |
| 4902 | write_lock(&css_set_lock); |
| 4903 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4904 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4905 | write_unlock(&css_set_lock); |
| 4906 | } |
| 4907 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4908 | /* Reassign the task to the init_css_set. */ |
| 4909 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4910 | cset = task_css_set(tsk); |
| 4911 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4912 | |
| 4913 | if (run_callbacks && need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4914 | /* see cgroup_post_fork() for details */ |
| 4915 | for_each_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4916 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4917 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 4918 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4919 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4920 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4921 | } |
| 4922 | } |
| 4923 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4924 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4925 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4926 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4927 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4928 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4929 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4930 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4931 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4932 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4933 | /* |
| 4934 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4935 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4936 | * it now |
| 4937 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4938 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4939 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4940 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4941 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4942 | list_empty(&cgrp->release_list)) { |
| 4943 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4944 | need_schedule_work = 1; |
| 4945 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4946 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4947 | if (need_schedule_work) |
| 4948 | schedule_work(&release_agent_work); |
| 4949 | } |
| 4950 | } |
| 4951 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4952 | /* |
| 4953 | * Notify userspace when a cgroup is released, by running the |
| 4954 | * configured release agent with the name of the cgroup (path |
| 4955 | * relative to the root of cgroup file system) as the argument. |
| 4956 | * |
| 4957 | * Most likely, this user command will try to rmdir this cgroup. |
| 4958 | * |
| 4959 | * This races with the possibility that some other task will be |
| 4960 | * attached to this cgroup before it is removed, or that some other |
| 4961 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 4962 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 4963 | * unused, and this cgroup will be reprieved from its death sentence, |
| 4964 | * to continue to serve a useful existence. Next time it's released, |
| 4965 | * we will get notified again, if it still has 'notify_on_release' set. |
| 4966 | * |
| 4967 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 4968 | * means only wait until the task is successfully execve()'d. The |
| 4969 | * separate release agent task is forked by call_usermodehelper(), |
| 4970 | * then control in this thread returns here, without waiting for the |
| 4971 | * release agent task. We don't bother to wait because the caller of |
| 4972 | * this routine has no use for the exit status of the release agent |
| 4973 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4974 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4975 | static void cgroup_release_agent(struct work_struct *work) |
| 4976 | { |
| 4977 | BUG_ON(work != &release_agent_work); |
| 4978 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4979 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4980 | while (!list_empty(&release_list)) { |
| 4981 | char *argv[3], *envp[3]; |
| 4982 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4983 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4984 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4985 | struct cgroup, |
| 4986 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4987 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4988 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4989 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4990 | if (!pathbuf) |
| 4991 | goto continue_free; |
| 4992 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 4993 | goto continue_free; |
| 4994 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 4995 | if (!agentbuf) |
| 4996 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4997 | |
| 4998 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4999 | argv[i++] = agentbuf; |
| 5000 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5001 | argv[i] = NULL; |
| 5002 | |
| 5003 | i = 0; |
| 5004 | /* minimal command environment */ |
| 5005 | envp[i++] = "HOME=/"; |
| 5006 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5007 | envp[i] = NULL; |
| 5008 | |
| 5009 | /* Drop the lock while we invoke the usermode helper, |
| 5010 | * since the exec could involve hitting disk and hence |
| 5011 | * be a slow process */ |
| 5012 | mutex_unlock(&cgroup_mutex); |
| 5013 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5014 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5015 | continue_free: |
| 5016 | kfree(pathbuf); |
| 5017 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5018 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5019 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5020 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5021 | mutex_unlock(&cgroup_mutex); |
| 5022 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5023 | |
| 5024 | static int __init cgroup_disable(char *str) |
| 5025 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5026 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5027 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5028 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5029 | |
| 5030 | while ((token = strsep(&str, ",")) != NULL) { |
| 5031 | if (!*token) |
| 5032 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5033 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5034 | for_each_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5035 | if (!strcmp(token, ss->name)) { |
| 5036 | ss->disabled = 1; |
| 5037 | printk(KERN_INFO "Disabling %s control group" |
| 5038 | " subsystem\n", ss->name); |
| 5039 | break; |
| 5040 | } |
| 5041 | } |
| 5042 | } |
| 5043 | return 1; |
| 5044 | } |
| 5045 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5046 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5047 | /** |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5048 | * 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] | 5049 | * @dentry: directory dentry of interest |
| 5050 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5051 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5052 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 5053 | * to get the corresponding css and return it. If such css doesn't exist |
| 5054 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5055 | */ |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5056 | struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry, |
| 5057 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5058 | { |
| 5059 | struct cgroup *cgrp; |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5060 | struct cgroup_subsys_state *css; |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5061 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5062 | /* is @dentry a cgroup dir? */ |
| 5063 | if (!dentry->d_inode || |
| 5064 | dentry->d_inode->i_op != &cgroup_dir_inode_operations) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5065 | return ERR_PTR(-EBADF); |
| 5066 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5067 | rcu_read_lock(); |
| 5068 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5069 | cgrp = __d_cgrp(dentry); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5070 | css = cgroup_css(cgrp, ss); |
| 5071 | |
| 5072 | if (!css || !css_tryget(css)) |
| 5073 | css = ERR_PTR(-ENOENT); |
| 5074 | |
| 5075 | rcu_read_unlock(); |
| 5076 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5077 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5078 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5079 | /** |
| 5080 | * css_from_id - lookup css by id |
| 5081 | * @id: the cgroup id |
| 5082 | * @ss: cgroup subsys to be looked into |
| 5083 | * |
| 5084 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5085 | * Should be called under rcu_read_lock(). |
| 5086 | */ |
| 5087 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5088 | { |
| 5089 | struct cgroup *cgrp; |
| 5090 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5091 | cgroup_assert_mutexes_or_rcu_locked(); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5092 | |
| 5093 | cgrp = idr_find(&ss->root->cgroup_idr, id); |
| 5094 | if (cgrp) |
Tejun Heo | d162596 | 2013-08-27 14:27:23 -0400 | [diff] [blame] | 5095 | return cgroup_css(cgrp, ss); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5096 | return NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5097 | } |
| 5098 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5099 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5100 | static struct cgroup_subsys_state * |
| 5101 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5102 | { |
| 5103 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5104 | |
| 5105 | if (!css) |
| 5106 | return ERR_PTR(-ENOMEM); |
| 5107 | |
| 5108 | return css; |
| 5109 | } |
| 5110 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5111 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5112 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5113 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5114 | } |
| 5115 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5116 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 5117 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5118 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5119 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5120 | } |
| 5121 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5122 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 5123 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5124 | { |
| 5125 | return (u64)(unsigned long)current->cgroups; |
| 5126 | } |
| 5127 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5128 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5129 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5130 | { |
| 5131 | u64 count; |
| 5132 | |
| 5133 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5134 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5135 | rcu_read_unlock(); |
| 5136 | return count; |
| 5137 | } |
| 5138 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5139 | 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] | 5140 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5141 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5142 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5143 | |
| 5144 | read_lock(&css_set_lock); |
| 5145 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5146 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5147 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5148 | struct cgroup *c = link->cgrp; |
| 5149 | const char *name; |
| 5150 | |
| 5151 | if (c->dentry) |
| 5152 | name = c->dentry->d_name.name; |
| 5153 | else |
| 5154 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5155 | seq_printf(seq, "Root %d group %s\n", |
| 5156 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5157 | } |
| 5158 | rcu_read_unlock(); |
| 5159 | read_unlock(&css_set_lock); |
| 5160 | return 0; |
| 5161 | } |
| 5162 | |
| 5163 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5164 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5165 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5166 | struct cgroup_subsys_state *css = seq_css(seq); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5167 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5168 | |
| 5169 | read_lock(&css_set_lock); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5170 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5171 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5172 | struct task_struct *task; |
| 5173 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5174 | seq_printf(seq, "css_set %p\n", cset); |
| 5175 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5176 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5177 | seq_puts(seq, " ...\n"); |
| 5178 | break; |
| 5179 | } else { |
| 5180 | seq_printf(seq, " task %d\n", |
| 5181 | task_pid_vnr(task)); |
| 5182 | } |
| 5183 | } |
| 5184 | } |
| 5185 | read_unlock(&css_set_lock); |
| 5186 | return 0; |
| 5187 | } |
| 5188 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5189 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5190 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5191 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5192 | } |
| 5193 | |
| 5194 | static struct cftype debug_files[] = { |
| 5195 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5196 | .name = "taskcount", |
| 5197 | .read_u64 = debug_taskcount_read, |
| 5198 | }, |
| 5199 | |
| 5200 | { |
| 5201 | .name = "current_css_set", |
| 5202 | .read_u64 = current_css_set_read, |
| 5203 | }, |
| 5204 | |
| 5205 | { |
| 5206 | .name = "current_css_set_refcount", |
| 5207 | .read_u64 = current_css_set_refcount_read, |
| 5208 | }, |
| 5209 | |
| 5210 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5211 | .name = "current_css_set_cg_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5212 | .seq_show = current_css_set_cg_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5213 | }, |
| 5214 | |
| 5215 | { |
| 5216 | .name = "cgroup_css_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5217 | .seq_show = cgroup_css_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5218 | }, |
| 5219 | |
| 5220 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5221 | .name = "releasable", |
| 5222 | .read_u64 = releasable_read, |
| 5223 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5224 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5225 | { } /* terminate */ |
| 5226 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5227 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5228 | struct cgroup_subsys debug_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5229 | .css_alloc = debug_css_alloc, |
| 5230 | .css_free = debug_css_free, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5231 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5232 | }; |
| 5233 | #endif /* CONFIG_CGROUP_DEBUG */ |