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 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 70 | #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ |
| 71 | MAX_CFTYPE_NAME + 2) |
| 72 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 73 | /* |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 74 | * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file |
| 75 | * creation/removal and hierarchy changing operations including cgroup |
| 76 | * creation, removal, css association and controller rebinding. This outer |
| 77 | * lock is needed mainly to resolve the circular dependency between kernfs |
| 78 | * active ref and cgroup_mutex. cgroup_tree_mutex nests above both. |
| 79 | */ |
| 80 | static DEFINE_MUTEX(cgroup_tree_mutex); |
| 81 | |
| 82 | /* |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 83 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 84 | * hierarchy must be performed while holding it. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 85 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 86 | #ifdef CONFIG_PROVE_RCU |
| 87 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 88 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 89 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 90 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 91 | #endif |
| 92 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 93 | /* |
| 94 | * Protects cgroup_subsys->release_agent_path. Modifying it also requires |
| 95 | * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock. |
| 96 | */ |
| 97 | static DEFINE_SPINLOCK(release_agent_path_lock); |
| 98 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 99 | #define cgroup_assert_mutexes_or_rcu_locked() \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 100 | rcu_lockdep_assert(rcu_read_lock_held() || \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 101 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 102 | lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 103 | "cgroup_[tree_]mutex or RCU read lock required"); |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 104 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 105 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 106 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 107 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 108 | * destruction work items don't end up filling up max_active of system_wq |
| 109 | * which may lead to deadlock. |
| 110 | */ |
| 111 | static struct workqueue_struct *cgroup_destroy_wq; |
| 112 | |
| 113 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 114 | * pidlist destructions need to be flushed on cgroup destruction. Use a |
| 115 | * separate workqueue as flush domain. |
| 116 | */ |
| 117 | static struct workqueue_struct *cgroup_pidlist_destroy_wq; |
| 118 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 119 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 120 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 121 | static struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 122 | #include <linux/cgroup_subsys.h> |
| 123 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 124 | #undef SUBSYS |
| 125 | |
| 126 | /* array of cgroup subsystem names */ |
| 127 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 128 | static const char *cgroup_subsys_name[] = { |
| 129 | #include <linux/cgroup_subsys.h> |
| 130 | }; |
| 131 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 132 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 133 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 134 | * The dummy hierarchy, reserved for the subsystems that are otherwise |
| 135 | * unattached - it never has more than a single cgroup, and all tasks are |
| 136 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 137 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 138 | static struct cgroupfs_root cgroup_dummy_root; |
| 139 | |
| 140 | /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ |
| 141 | static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 142 | |
| 143 | /* The list of hierarchy roots */ |
| 144 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 145 | static LIST_HEAD(cgroup_roots); |
| 146 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 147 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 148 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 149 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 150 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 151 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 152 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 153 | /* |
| 154 | * Assign a monotonically increasing serial number to cgroups. It |
| 155 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 156 | * numbers. Also, as cgroups are always appended to the parent's |
| 157 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 158 | * the ascending serial number order on the list. Protected by |
| 159 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 160 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 161 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 162 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 163 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 164 | * check for fork/exit handlers to call. This avoids us having to do |
| 165 | * extra work in the fork/exit path if none of the subsystems need to |
| 166 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 167 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 168 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 169 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 170 | static struct cftype cgroup_base_files[]; |
| 171 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 172 | static void cgroup_destroy_css_killed(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 173 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 174 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 175 | bool is_add); |
Tejun Heo | e605b36 | 2013-11-27 18:16:21 -0500 | [diff] [blame] | 176 | static int cgroup_file_release(struct inode *inode, struct file *file); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 177 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 178 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 179 | /** |
| 180 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 181 | * @cgrp: the cgroup of interest |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 182 | * @ss: the subsystem of interest (%NULL returns the dummy_css) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 183 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 184 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 185 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 186 | * the caller is responsible for pinning the returned css if it wants to |
| 187 | * keep accessing it outside the said locks. This function may return |
| 188 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 189 | */ |
| 190 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 191 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 192 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 193 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 194 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 195 | lockdep_is_held(&cgroup_tree_mutex) || |
| 196 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 197 | else |
| 198 | return &cgrp->dummy_css; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 199 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 200 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 201 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 202 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 203 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 204 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 207 | /** |
| 208 | * cgroup_is_descendant - test ancestry |
| 209 | * @cgrp: the cgroup to be tested |
| 210 | * @ancestor: possible ancestor of @cgrp |
| 211 | * |
| 212 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 213 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 214 | * and @ancestor are accessible. |
| 215 | */ |
| 216 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 217 | { |
| 218 | while (cgrp) { |
| 219 | if (cgrp == ancestor) |
| 220 | return true; |
| 221 | cgrp = cgrp->parent; |
| 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 226 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 227 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 228 | { |
| 229 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 230 | (1 << CGRP_RELEASABLE) | |
| 231 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 232 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 235 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 236 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 237 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 240 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 241 | * for_each_css - iterate all css's of a cgroup |
| 242 | * @css: the iteration cursor |
| 243 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 244 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 245 | * |
| 246 | * Should be called under cgroup_mutex. |
| 247 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 248 | #define for_each_css(css, ssid, cgrp) \ |
| 249 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 250 | if (!((css) = rcu_dereference_check( \ |
| 251 | (cgrp)->subsys[(ssid)], \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 252 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 253 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 254 | else |
| 255 | |
| 256 | /** |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 257 | * for_each_subsys - iterate all enabled cgroup subsystems |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 258 | * @ss: the iteration cursor |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 259 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 260 | */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 261 | #define for_each_subsys(ss, ssid) \ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 262 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \ |
| 263 | (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 264 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 265 | /* iterate across the active hierarchies */ |
| 266 | #define for_each_active_root(root) \ |
| 267 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 268 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 269 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 270 | { |
| 271 | return dentry->d_fsdata; |
| 272 | } |
| 273 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 274 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 275 | { |
| 276 | return dentry->d_fsdata; |
| 277 | } |
| 278 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 279 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 280 | { |
| 281 | return __d_cfe(dentry)->type; |
| 282 | } |
| 283 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 284 | /** |
| 285 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 286 | * @cgrp: the cgroup to be checked for liveness |
| 287 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 288 | * On success, returns true; the mutex should be later unlocked. On |
| 289 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 290 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 291 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 292 | { |
| 293 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 294 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 295 | mutex_unlock(&cgroup_mutex); |
| 296 | return false; |
| 297 | } |
| 298 | return true; |
| 299 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 300 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 301 | /* the list of cgroups eligible for automatic release. Protected by |
| 302 | * release_list_lock */ |
| 303 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 304 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 305 | static void cgroup_release_agent(struct work_struct *work); |
| 306 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 307 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 308 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 309 | /* |
| 310 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 311 | * belong to different cgroups on different hierarchies. In the other |
| 312 | * direction, a css_set is naturally associated with multiple cgroups. |
| 313 | * This M:N relationship is represented by the following link structure |
| 314 | * which exists for each association and allows traversing the associations |
| 315 | * from both sides. |
| 316 | */ |
| 317 | struct cgrp_cset_link { |
| 318 | /* the cgroup and css_set this link associates */ |
| 319 | struct cgroup *cgrp; |
| 320 | struct css_set *cset; |
| 321 | |
| 322 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 323 | struct list_head cset_link; |
| 324 | |
| 325 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 326 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | /* The default css_set - used by init and its children prior to any |
| 330 | * hierarchies being mounted. It contains a pointer to the root state |
| 331 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 332 | * reference-counted, to improve performance when child cgroups |
| 333 | * haven't been created. |
| 334 | */ |
| 335 | |
| 336 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 337 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 338 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 339 | /* |
| 340 | * css_set_lock protects the list of css_set objects, and the chain of |
| 341 | * tasks off each css_set. Nests outside task->alloc_lock due to |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 342 | * css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 343 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 344 | static DEFINE_RWLOCK(css_set_lock); |
| 345 | static int css_set_count; |
| 346 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 347 | /* |
| 348 | * hash table for cgroup groups. This improves the performance to find |
| 349 | * an existing css_set. This hash doesn't (currently) take into |
| 350 | * account cgroups in empty hierarchies. |
| 351 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 352 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 353 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 354 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 355 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 356 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 357 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 358 | struct cgroup_subsys *ss; |
| 359 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 360 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 361 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 362 | key += (unsigned long)css[i]; |
| 363 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 364 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 365 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 368 | /* |
| 369 | * 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] | 370 | * until after the first call to css_task_iter_start(). This reduces the |
| 371 | * fork()/exit() overhead for people who have cgroups compiled into their |
| 372 | * kernel but not actually in use. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 373 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 374 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 375 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 376 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 377 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 378 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 379 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 380 | /* |
| 381 | * Ensure that the refcount doesn't hit zero while any readers |
| 382 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 383 | * rwlock |
| 384 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 385 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 386 | return; |
| 387 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 388 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 389 | write_unlock(&css_set_lock); |
| 390 | return; |
| 391 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 392 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 393 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 394 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 395 | css_set_count--; |
| 396 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 397 | 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] | 398 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 399 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 400 | list_del(&link->cset_link); |
| 401 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 402 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 403 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 404 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 405 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 406 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 407 | check_for_release(cgrp); |
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 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 411 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 412 | |
| 413 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 414 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* |
| 418 | * refcounted get/put for css_set objects |
| 419 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 420 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 421 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 422 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 425 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 426 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 427 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 430 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 431 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 432 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 435 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 436 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 437 | * @cset: candidate css_set being tested |
| 438 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 439 | * @new_cgrp: cgroup that's being entered by the task |
| 440 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 441 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 442 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 443 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 444 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 445 | static bool compare_css_sets(struct css_set *cset, |
| 446 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 447 | struct cgroup *new_cgrp, |
| 448 | struct cgroup_subsys_state *template[]) |
| 449 | { |
| 450 | struct list_head *l1, *l2; |
| 451 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 452 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 453 | /* Not all subsystems matched */ |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Compare cgroup pointers in order to distinguish between |
| 459 | * different cgroups in heirarchies with no subsystems. We |
| 460 | * could get by with just this check alone (and skip the |
| 461 | * memcmp above) but on most setups the memcmp check will |
| 462 | * avoid the need for this more expensive check on almost all |
| 463 | * candidates. |
| 464 | */ |
| 465 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 466 | l1 = &cset->cgrp_links; |
| 467 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 468 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 469 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 470 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 471 | |
| 472 | l1 = l1->next; |
| 473 | l2 = l2->next; |
| 474 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 475 | if (l1 == &cset->cgrp_links) { |
| 476 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 477 | break; |
| 478 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 479 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 480 | } |
| 481 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 482 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 483 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 484 | cgrp1 = link1->cgrp; |
| 485 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 486 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 487 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 488 | |
| 489 | /* |
| 490 | * If this hierarchy is the hierarchy of the cgroup |
| 491 | * that's changing, then we need to check that this |
| 492 | * css_set points to the new cgroup; if it's any other |
| 493 | * hierarchy, then this css_set should point to the |
| 494 | * same cgroup as the old css_set. |
| 495 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 496 | if (cgrp1->root == new_cgrp->root) { |
| 497 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 498 | return false; |
| 499 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 500 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 501 | return false; |
| 502 | } |
| 503 | } |
| 504 | return true; |
| 505 | } |
| 506 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 507 | /** |
| 508 | * find_existing_css_set - init css array and find the matching css_set |
| 509 | * @old_cset: the css_set that we're using before the cgroup transition |
| 510 | * @cgrp: the cgroup that we're moving into |
| 511 | * @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] | 512 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 513 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 514 | struct cgroup *cgrp, |
| 515 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 516 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 517 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 518 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 519 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 520 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 521 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 522 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 523 | /* |
| 524 | * Build the set of subsystem state objects that we want to see in the |
| 525 | * new css_set. while subsystems can change globally, the entries here |
| 526 | * won't change, so no need for locking. |
| 527 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 528 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 529 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 530 | /* Subsystem is in this hierarchy. So we want |
| 531 | * the subsystem state from the new |
| 532 | * cgroup */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 533 | template[i] = cgroup_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 534 | } else { |
| 535 | /* Subsystem is not in this hierarchy, so we |
| 536 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 537 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 541 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 542 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 543 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 544 | continue; |
| 545 | |
| 546 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 547 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 548 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 549 | |
| 550 | /* No existing cgroup group matched */ |
| 551 | return NULL; |
| 552 | } |
| 553 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 554 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 555 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 556 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 557 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 558 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 559 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 560 | kfree(link); |
| 561 | } |
| 562 | } |
| 563 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 564 | /** |
| 565 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 566 | * @count: the number of links to allocate |
| 567 | * @tmp_links: list_head the allocated links are put on |
| 568 | * |
| 569 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 570 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 571 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 572 | 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] | 573 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 574 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 575 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 576 | |
| 577 | INIT_LIST_HEAD(tmp_links); |
| 578 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 579 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 580 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 581 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 582 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 583 | return -ENOMEM; |
| 584 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 585 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 586 | } |
| 587 | return 0; |
| 588 | } |
| 589 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 590 | /** |
| 591 | * 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] | 592 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 593 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 594 | * @cgrp: the destination cgroup |
| 595 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 596 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 597 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 598 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 599 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 600 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 601 | BUG_ON(list_empty(tmp_links)); |
| 602 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 603 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 604 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 605 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 606 | /* |
| 607 | * Always add links to the tail of the list so that the list |
| 608 | * is sorted by order of hierarchy creation |
| 609 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 610 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 613 | /** |
| 614 | * find_css_set - return a new css_set with one cgroup updated |
| 615 | * @old_cset: the baseline css_set |
| 616 | * @cgrp: the cgroup to be updated |
| 617 | * |
| 618 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 619 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 620 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 621 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 622 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 623 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 624 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 625 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 626 | struct list_head tmp_links; |
| 627 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 628 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 629 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 630 | lockdep_assert_held(&cgroup_mutex); |
| 631 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 632 | /* First see if we already have a cgroup group that matches |
| 633 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 634 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 635 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 636 | if (cset) |
| 637 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 638 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 639 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 640 | if (cset) |
| 641 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 642 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 643 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 644 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 645 | return NULL; |
| 646 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 647 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 648 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 649 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 650 | return NULL; |
| 651 | } |
| 652 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 653 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 654 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 655 | INIT_LIST_HEAD(&cset->tasks); |
| 656 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 657 | |
| 658 | /* Copy the set of subsystem state objects generated in |
| 659 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 660 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 661 | |
| 662 | write_lock(&css_set_lock); |
| 663 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 664 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 665 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 666 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 667 | if (c->root == cgrp->root) |
| 668 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 669 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 670 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 671 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 672 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 673 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 674 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 675 | |
| 676 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 677 | key = css_set_hash(cset->subsys); |
| 678 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 679 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 680 | write_unlock(&css_set_lock); |
| 681 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 682 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 685 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 686 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 687 | * called with cgroup_mutex held. |
| 688 | */ |
| 689 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 690 | struct cgroupfs_root *root) |
| 691 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 692 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 693 | struct cgroup *res = NULL; |
| 694 | |
| 695 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 696 | read_lock(&css_set_lock); |
| 697 | /* |
| 698 | * No need to lock the task - since we hold cgroup_mutex the |
| 699 | * task can't change groups, so the only thing that can happen |
| 700 | * is that it exits and its css is set back to init_css_set. |
| 701 | */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 702 | cset = task_css_set(task); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 703 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 704 | res = &root->top_cgroup; |
| 705 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 706 | struct cgrp_cset_link *link; |
| 707 | |
| 708 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 709 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 710 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 711 | if (c->root == root) { |
| 712 | res = c; |
| 713 | break; |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | read_unlock(&css_set_lock); |
| 718 | BUG_ON(!res); |
| 719 | return res; |
| 720 | } |
| 721 | |
| 722 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 723 | * There is one global cgroup mutex. We also require taking |
| 724 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 725 | * See "The task_lock() exception", at the end of this comment. |
| 726 | * |
| 727 | * A task must hold cgroup_mutex to modify cgroups. |
| 728 | * |
| 729 | * Any task can increment and decrement the count field without lock. |
| 730 | * So in general, code holding cgroup_mutex can't rely on the count |
| 731 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 732 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 733 | * means that no tasks are currently attached, therefore there is no |
| 734 | * way a task attached to that cgroup can fork (the other way to |
| 735 | * increment the count). So code holding cgroup_mutex can safely |
| 736 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 737 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 738 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 739 | * needs that mutex. |
| 740 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 741 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 742 | * (usually) take cgroup_mutex. These are the two most performance |
| 743 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 744 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 745 | * 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] | 746 | * to the release agent with the name of the cgroup (path relative to |
| 747 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 748 | * |
| 749 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 750 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 751 | * tasks in the system use _some_ cgroup, and since there is always at |
| 752 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 753 | * always has either children cgroups and/or using tasks. So we don't |
| 754 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 755 | * |
| 756 | * The task_lock() exception |
| 757 | * |
| 758 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 759 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 760 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 761 | * several performance critical places that need to reference |
| 762 | * task->cgroup without the expense of grabbing a system global |
| 763 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 764 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 765 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 766 | * the task_struct routinely used for such matters. |
| 767 | * |
| 768 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 769 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 770 | */ |
| 771 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 772 | /* |
| 773 | * A couple of forward declarations required, due to cyclic reference loop: |
| 774 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 775 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 776 | * -> cgroup_mkdir. |
| 777 | */ |
| 778 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 779 | 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] | 780 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 781 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 782 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 783 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 784 | |
| 785 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 786 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 787 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 788 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 789 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 790 | 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] | 791 | { |
| 792 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 793 | |
| 794 | if (inode) { |
Tejun Heo | b166492 | 2014-02-11 11:52:49 -0500 | [diff] [blame^] | 795 | do { |
| 796 | /* ino 0 is reserved for dummy_root */ |
| 797 | inode->i_ino = get_next_ino(); |
| 798 | } while (!inode->i_ino); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 799 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 800 | inode->i_uid = current_fsuid(); |
| 801 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 802 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 803 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 804 | } |
| 805 | return inode; |
| 806 | } |
| 807 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 808 | static struct cgroup_name *cgroup_alloc_name(const char *name_str) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 809 | { |
| 810 | struct cgroup_name *name; |
| 811 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 812 | name = kmalloc(sizeof(*name) + strlen(name_str) + 1, GFP_KERNEL); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 813 | if (!name) |
| 814 | return NULL; |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 815 | strcpy(name->name, name_str); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 816 | return name; |
| 817 | } |
| 818 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 819 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 820 | char *buf) |
| 821 | { |
| 822 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 823 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) |
| 824 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s", |
| 825 | cft->ss->name, cft->name); |
| 826 | else |
| 827 | strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
| 828 | return buf; |
| 829 | } |
| 830 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 831 | static void cgroup_free_fn(struct work_struct *work) |
| 832 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 833 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 834 | |
| 835 | mutex_lock(&cgroup_mutex); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 836 | cgrp->root->number_of_cgroups--; |
| 837 | mutex_unlock(&cgroup_mutex); |
| 838 | |
| 839 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 840 | * We get a ref to the parent's dentry, and put the ref when |
| 841 | * this cgroup is being freed, so it's guaranteed that the |
| 842 | * parent won't be destroyed before its children. |
| 843 | */ |
| 844 | dput(cgrp->parent->dentry); |
| 845 | |
| 846 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 847 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 848 | * created the cgroup. This will free cgrp->root, if we are |
| 849 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 850 | */ |
| 851 | deactivate_super(cgrp->root->sb); |
| 852 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 853 | cgroup_pidlist_destroy_all(cgrp); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 854 | |
| 855 | simple_xattrs_free(&cgrp->xattrs); |
| 856 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 857 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 858 | kfree(cgrp); |
| 859 | } |
| 860 | |
| 861 | static void cgroup_free_rcu(struct rcu_head *head) |
| 862 | { |
| 863 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 864 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 865 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 866 | queue_work(cgroup_destroy_wq, &cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 867 | } |
| 868 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 869 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 870 | { |
| 871 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 872 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 873 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 874 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 875 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | c1a7150 | 2013-12-17 11:13:39 +0800 | [diff] [blame] | 876 | |
| 877 | /* |
| 878 | * XXX: cgrp->id is only used to look up css's. As cgroup |
| 879 | * and css's lifetimes will be decoupled, it should be made |
| 880 | * per-subsystem and moved to css->id so that lookups are |
| 881 | * successful until the target css is released. |
| 882 | */ |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 883 | mutex_lock(&cgroup_mutex); |
Li Zefan | c1a7150 | 2013-12-17 11:13:39 +0800 | [diff] [blame] | 884 | idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 885 | mutex_unlock(&cgroup_mutex); |
Li Zefan | c1a7150 | 2013-12-17 11:13:39 +0800 | [diff] [blame] | 886 | cgrp->id = -1; |
| 887 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 888 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 889 | } else { |
| 890 | struct cfent *cfe = __d_cfe(dentry); |
| 891 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 892 | |
| 893 | WARN_ONCE(!list_empty(&cfe->node) && |
| 894 | cgrp != &cgrp->root->top_cgroup, |
| 895 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 896 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 897 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 898 | } |
| 899 | iput(inode); |
| 900 | } |
| 901 | |
| 902 | static void remove_dir(struct dentry *d) |
| 903 | { |
| 904 | struct dentry *parent = dget(d->d_parent); |
| 905 | |
| 906 | d_delete(d); |
| 907 | simple_rmdir(parent->d_inode, d); |
| 908 | dput(parent); |
| 909 | } |
| 910 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 911 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 912 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 913 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 914 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 915 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 916 | lockdep_assert_held(&cgroup_tree_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 917 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 918 | /* |
| 919 | * If we're doing cleanup due to failure of cgroup_create(), |
| 920 | * the corresponding @cfe may not exist. |
| 921 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 922 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 923 | struct dentry *d = cfe->dentry; |
| 924 | |
| 925 | if (cft && cfe->type != cft) |
| 926 | continue; |
| 927 | |
| 928 | dget(d); |
| 929 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 930 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 931 | list_del_init(&cfe->node); |
| 932 | dput(d); |
| 933 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 934 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 935 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 938 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 939 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 940 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 941 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 942 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 943 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 944 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 945 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 946 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 947 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 948 | for_each_subsys(ss, i) { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 949 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 950 | |
| 951 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 952 | continue; |
| 953 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 954 | cgroup_addrm_files(cgrp, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 955 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /* |
| 959 | * NOTE : the dentry must have been dget()'ed |
| 960 | */ |
| 961 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 962 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 963 | struct dentry *parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 964 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 965 | parent = dentry->d_parent; |
| 966 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 967 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 968 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 969 | spin_unlock(&dentry->d_lock); |
| 970 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 971 | remove_dir(dentry); |
| 972 | } |
| 973 | |
| 974 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 975 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 976 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 977 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 978 | struct cgroup_subsys *ss; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 979 | int i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 980 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 981 | lockdep_assert_held(&cgroup_tree_mutex); |
| 982 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 983 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 984 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 985 | for_each_subsys(ss, i) |
| 986 | if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root) |
| 987 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 988 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 989 | ret = cgroup_populate_dir(cgrp, added_mask); |
| 990 | if (ret) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 991 | return ret; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 992 | |
| 993 | /* |
| 994 | * Nothing can fail from this point on. Remove files for the |
| 995 | * removed subsystems and rebind each subsystem. |
| 996 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 997 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 998 | cgroup_clear_dir(cgrp, removed_mask); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 999 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1000 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1001 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1002 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1003 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1004 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1005 | /* We're binding this subsystem to this hierarchy */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1006 | BUG_ON(cgroup_css(cgrp, ss)); |
| 1007 | BUG_ON(!cgroup_css(cgroup_dummy_top, ss)); |
| 1008 | BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1009 | |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1010 | rcu_assign_pointer(cgrp->subsys[i], |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1011 | cgroup_css(cgroup_dummy_top, ss)); |
| 1012 | cgroup_css(cgrp, ss)->cgroup = cgrp; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1013 | |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1014 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1015 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1016 | ss->bind(cgroup_css(cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1017 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1018 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1019 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1020 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1021 | /* We're removing this subsystem */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1022 | BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss)); |
| 1023 | BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1024 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1025 | if (ss->bind) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1026 | ss->bind(cgroup_css(cgroup_dummy_top, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1027 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1028 | cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1029 | RCU_INIT_POINTER(cgrp->subsys[i], NULL); |
| 1030 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1031 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1032 | root->subsys_mask &= ~bit; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1033 | } |
| 1034 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1035 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1036 | /* |
| 1037 | * Mark @root has finished binding subsystems. @root->subsys_mask |
| 1038 | * now matches the bound subsystems. |
| 1039 | */ |
| 1040 | root->flags |= CGRP_ROOT_SUBSYS_BOUND; |
| 1041 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1042 | return 0; |
| 1043 | } |
| 1044 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1045 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1046 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1047 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1048 | struct cgroup_subsys *ss; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1049 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1050 | |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1051 | for_each_subsys(ss, ssid) |
| 1052 | if (root->subsys_mask & (1 << ssid)) |
| 1053 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1054 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1055 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1056 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1057 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1058 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1059 | seq_puts(seq, ",xattr"); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1060 | |
| 1061 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1062 | if (strlen(root->release_agent_path)) |
| 1063 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1064 | spin_unlock(&release_agent_path_lock); |
| 1065 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1066 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1067 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1068 | if (strlen(root->name)) |
| 1069 | seq_printf(seq, ",name=%s", root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1074 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1075 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1076 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1077 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1078 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1079 | /* User explicitly requested empty subsystem */ |
| 1080 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1081 | |
| 1082 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1083 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1084 | }; |
| 1085 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1086 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1087 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1088 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1089 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1090 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1091 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1092 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1093 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1094 | char *token, *o = data; |
| 1095 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1096 | unsigned long mask = (unsigned long)-1; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1097 | struct cgroup_subsys *ss; |
| 1098 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1099 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1100 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1101 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1102 | #ifdef CONFIG_CPUSETS |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1103 | mask = ~(1UL << cpuset_cgrp_id); |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1104 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1105 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1106 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1107 | |
| 1108 | while ((token = strsep(&o, ",")) != NULL) { |
| 1109 | if (!*token) |
| 1110 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1111 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1112 | /* Explicitly have no subsystems */ |
| 1113 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1114 | continue; |
| 1115 | } |
| 1116 | if (!strcmp(token, "all")) { |
| 1117 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1118 | if (one_ss) |
| 1119 | return -EINVAL; |
| 1120 | all_ss = true; |
| 1121 | continue; |
| 1122 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1123 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1124 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1125 | continue; |
| 1126 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1127 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1128 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1129 | continue; |
| 1130 | } |
| 1131 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1132 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1133 | continue; |
| 1134 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1135 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1136 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1137 | continue; |
| 1138 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1139 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1140 | /* Specifying two release agents is forbidden */ |
| 1141 | if (opts->release_agent) |
| 1142 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1143 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1144 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1145 | if (!opts->release_agent) |
| 1146 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1147 | continue; |
| 1148 | } |
| 1149 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1150 | const char *name = token + 5; |
| 1151 | /* Can't specify an empty name */ |
| 1152 | if (!strlen(name)) |
| 1153 | return -EINVAL; |
| 1154 | /* Must match [\w.-]+ */ |
| 1155 | for (i = 0; i < strlen(name); i++) { |
| 1156 | char c = name[i]; |
| 1157 | if (isalnum(c)) |
| 1158 | continue; |
| 1159 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1160 | continue; |
| 1161 | return -EINVAL; |
| 1162 | } |
| 1163 | /* Specifying two names is forbidden */ |
| 1164 | if (opts->name) |
| 1165 | return -EINVAL; |
| 1166 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1167 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1168 | GFP_KERNEL); |
| 1169 | if (!opts->name) |
| 1170 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1171 | |
| 1172 | continue; |
| 1173 | } |
| 1174 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1175 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1176 | if (strcmp(token, ss->name)) |
| 1177 | continue; |
| 1178 | if (ss->disabled) |
| 1179 | continue; |
| 1180 | |
| 1181 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1182 | if (all_ss) |
| 1183 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1184 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1185 | one_ss = true; |
| 1186 | |
| 1187 | break; |
| 1188 | } |
| 1189 | if (i == CGROUP_SUBSYS_COUNT) |
| 1190 | return -ENOENT; |
| 1191 | } |
| 1192 | |
| 1193 | /* |
| 1194 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1195 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1196 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1197 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1198 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1199 | for_each_subsys(ss, i) |
| 1200 | if (!ss->disabled) |
| 1201 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1202 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1203 | /* Consistency checks */ |
| 1204 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1205 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1206 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1207 | |
| 1208 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1209 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1210 | return -EINVAL; |
| 1211 | } |
| 1212 | |
| 1213 | if (opts->cpuset_clone_children) { |
| 1214 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1215 | return -EINVAL; |
| 1216 | } |
| 1217 | } |
| 1218 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1219 | /* |
| 1220 | * Option noprefix was introduced just for backward compatibility |
| 1221 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1222 | * the cpuset subsystem. |
| 1223 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1224 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1225 | return -EINVAL; |
| 1226 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1227 | |
| 1228 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1229 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1230 | return -EINVAL; |
| 1231 | |
| 1232 | /* |
| 1233 | * We either have to specify by name or by subsystems. (So all |
| 1234 | * empty hierarchies must have a name). |
| 1235 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1236 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1237 | return -EINVAL; |
| 1238 | |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1243 | { |
| 1244 | int ret = 0; |
| 1245 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1246 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1247 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1248 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1249 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1250 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1251 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1252 | return -EINVAL; |
| 1253 | } |
| 1254 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1255 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1256 | mutex_lock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1257 | mutex_lock(&cgroup_mutex); |
| 1258 | |
| 1259 | /* See what subsystems are wanted */ |
| 1260 | ret = parse_cgroupfs_options(data, &opts); |
| 1261 | if (ret) |
| 1262 | goto out_unlock; |
| 1263 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1264 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1265 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1266 | task_tgid_nr(current), current->comm); |
| 1267 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1268 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1269 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1270 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1271 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1272 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1273 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1274 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1275 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1276 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1277 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1278 | goto out_unlock; |
| 1279 | } |
| 1280 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1281 | /* remounting is not allowed for populated hierarchies */ |
| 1282 | if (root->number_of_cgroups > 1) { |
| 1283 | ret = -EBUSY; |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1284 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1285 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1286 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1287 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1288 | if (ret) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1289 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1290 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1291 | if (opts.release_agent) { |
| 1292 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1293 | strcpy(root->release_agent_path, opts.release_agent); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1294 | spin_unlock(&release_agent_path_lock); |
| 1295 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1296 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1297 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1298 | kfree(opts.name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1299 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1300 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1301 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1302 | return ret; |
| 1303 | } |
| 1304 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1305 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1306 | .statfs = simple_statfs, |
| 1307 | .drop_inode = generic_delete_inode, |
| 1308 | .show_options = cgroup_show_options, |
| 1309 | .remount_fs = cgroup_remount, |
| 1310 | }; |
| 1311 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1312 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1313 | { |
| 1314 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1315 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1316 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1317 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1318 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1319 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1320 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1321 | cgrp->dummy_css.cgroup = cgrp; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1322 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1323 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1324 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1325 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1326 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1327 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1328 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1329 | INIT_LIST_HEAD(&root->root_list); |
| 1330 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1331 | cgrp->root = root; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 1332 | RCU_INIT_POINTER(cgrp->name, &root_cgroup_name); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1333 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1334 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1337 | 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] | 1338 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1339 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1340 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1341 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1342 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1343 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 1344 | GFP_KERNEL); |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1345 | if (id < 0) |
| 1346 | return id; |
| 1347 | |
| 1348 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1349 | return 0; |
| 1350 | } |
| 1351 | |
| 1352 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1353 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1354 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1355 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1356 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1357 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1358 | root->hierarchy_id = 0; |
| 1359 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1362 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1363 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1364 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1365 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1366 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1367 | /* If we asked for a name then it must match */ |
| 1368 | if (opts->name && strcmp(opts->name, root->name)) |
| 1369 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1370 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1371 | /* |
| 1372 | * If we asked for subsystems (or explicitly for no |
| 1373 | * subsystems) then they must match |
| 1374 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1375 | if ((opts->subsys_mask || opts->none) |
| 1376 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1377 | return 0; |
| 1378 | |
| 1379 | return 1; |
| 1380 | } |
| 1381 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1382 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1383 | { |
| 1384 | struct cgroupfs_root *root; |
| 1385 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1386 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1387 | return NULL; |
| 1388 | |
| 1389 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1390 | if (!root) |
| 1391 | return ERR_PTR(-ENOMEM); |
| 1392 | |
| 1393 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1394 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1395 | /* |
| 1396 | * We need to set @root->subsys_mask now so that @root can be |
| 1397 | * matched by cgroup_test_super() before it finishes |
| 1398 | * initialization; otherwise, competing mounts with the same |
| 1399 | * options may try to bind the same subsystems instead of waiting |
| 1400 | * for the first one leading to unexpected mount errors. |
| 1401 | * SUBSYS_BOUND will be set once actual binding is complete. |
| 1402 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1403 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1404 | root->flags = opts->flags; |
| 1405 | if (opts->release_agent) |
| 1406 | strcpy(root->release_agent_path, opts->release_agent); |
| 1407 | if (opts->name) |
| 1408 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1409 | if (opts->cpuset_clone_children) |
| 1410 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1411 | return root; |
| 1412 | } |
| 1413 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1414 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1415 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1416 | if (root) { |
| 1417 | /* hierarhcy ID shoulid already have been released */ |
| 1418 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1419 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1420 | idr_destroy(&root->cgroup_idr); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1421 | kfree(root); |
| 1422 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1425 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1426 | { |
| 1427 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1428 | struct cgroup_sb_opts *opts = data; |
| 1429 | |
| 1430 | /* If we don't have a new root, we can't set up a new sb */ |
| 1431 | if (!opts->new_root) |
| 1432 | return -EINVAL; |
| 1433 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1434 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1435 | |
| 1436 | ret = set_anon_super(sb, NULL); |
| 1437 | if (ret) |
| 1438 | return ret; |
| 1439 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1440 | sb->s_fs_info = opts->new_root; |
| 1441 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1442 | |
| 1443 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1444 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1445 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1446 | sb->s_op = &cgroup_ops; |
| 1447 | |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1452 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1453 | static const struct dentry_operations cgroup_dops = { |
| 1454 | .d_iput = cgroup_diput, |
Al Viro | b26d4cd | 2013-10-25 18:47:37 -0400 | [diff] [blame] | 1455 | .d_delete = always_delete_dentry, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1456 | }; |
| 1457 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1458 | struct inode *inode = |
| 1459 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1460 | |
| 1461 | if (!inode) |
| 1462 | return -ENOMEM; |
| 1463 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1464 | inode->i_fop = &simple_dir_operations; |
| 1465 | inode->i_op = &cgroup_dir_inode_operations; |
| 1466 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1467 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1468 | sb->s_root = d_make_root(inode); |
| 1469 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1470 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1471 | /* for everything else we want ->d_op set */ |
| 1472 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1473 | return 0; |
| 1474 | } |
| 1475 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1476 | static int cgroup_setup_root(struct cgroupfs_root *root) |
| 1477 | { |
| 1478 | LIST_HEAD(tmp_links); |
| 1479 | struct super_block *sb = root->sb; |
| 1480 | struct cgroup *root_cgrp = &root->top_cgroup; |
| 1481 | struct cgroupfs_root *existing_root; |
| 1482 | struct css_set *cset; |
| 1483 | struct inode *inode; |
| 1484 | const struct cred *cred; |
| 1485 | int i, ret; |
| 1486 | |
| 1487 | lockdep_assert_held(&cgroup_tree_mutex); |
| 1488 | lockdep_assert_held(&cgroup_mutex); |
| 1489 | BUG_ON(sb->s_root != NULL); |
| 1490 | |
| 1491 | mutex_unlock(&cgroup_mutex); |
| 1492 | mutex_unlock(&cgroup_tree_mutex); |
| 1493 | |
| 1494 | ret = cgroup_get_rootdir(sb); |
| 1495 | if (ret) { |
| 1496 | mutex_lock(&cgroup_tree_mutex); |
| 1497 | mutex_lock(&cgroup_mutex); |
| 1498 | return ret; |
| 1499 | } |
| 1500 | inode = sb->s_root->d_inode; |
| 1501 | |
| 1502 | mutex_lock(&inode->i_mutex); |
| 1503 | mutex_lock(&cgroup_tree_mutex); |
| 1504 | mutex_lock(&cgroup_mutex); |
| 1505 | |
| 1506 | ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL); |
| 1507 | if (ret < 0) |
| 1508 | goto out_unlock; |
| 1509 | root_cgrp->id = ret; |
| 1510 | |
| 1511 | /* check for name clashes with existing mounts */ |
| 1512 | ret = -EBUSY; |
| 1513 | if (strlen(root->name)) |
| 1514 | for_each_active_root(existing_root) |
| 1515 | if (!strcmp(existing_root->name, root->name)) |
| 1516 | goto out_unlock; |
| 1517 | |
| 1518 | /* |
| 1519 | * We're accessing css_set_count without locking css_set_lock here, |
| 1520 | * but that's OK - it can only be increased by someone holding |
| 1521 | * cgroup_lock, and that's us. The worst that can happen is that we |
| 1522 | * have some link structures left over |
| 1523 | */ |
| 1524 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
| 1525 | if (ret) |
| 1526 | goto out_unlock; |
| 1527 | |
| 1528 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1529 | ret = cgroup_init_root_id(root, 2, 0); |
| 1530 | if (ret) |
| 1531 | goto out_unlock; |
| 1532 | |
| 1533 | sb->s_root->d_fsdata = root_cgrp; |
| 1534 | root_cgrp->dentry = sb->s_root; |
| 1535 | |
| 1536 | /* |
| 1537 | * We're inside get_sb() and will call lookup_one_len() to create |
| 1538 | * the root files, which doesn't work if SELinux is in use. The |
| 1539 | * following cred dancing somehow works around it. See 2ce9738ba |
| 1540 | * ("cgroupfs: use init_cred when populating new cgroupfs mount") |
| 1541 | * for more details. |
| 1542 | */ |
| 1543 | cred = override_creds(&init_cred); |
| 1544 | |
| 1545 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
| 1546 | if (ret) |
| 1547 | goto rm_base_files; |
| 1548 | |
| 1549 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
| 1550 | if (ret) |
| 1551 | goto rm_base_files; |
| 1552 | |
| 1553 | revert_creds(cred); |
| 1554 | |
| 1555 | /* |
| 1556 | * There must be no failure case after here, since rebinding takes |
| 1557 | * care of subsystems' refcounts, which are explicitly dropped in |
| 1558 | * the failure exit path. |
| 1559 | */ |
| 1560 | list_add(&root->root_list, &cgroup_roots); |
| 1561 | cgroup_root_count++; |
| 1562 | |
| 1563 | /* |
| 1564 | * Link the top cgroup in this hierarchy into all the css_set |
| 1565 | * objects. |
| 1566 | */ |
| 1567 | write_lock(&css_set_lock); |
| 1568 | hash_for_each(css_set_table, i, cset, hlist) |
| 1569 | link_css_set(&tmp_links, cset, root_cgrp); |
| 1570 | write_unlock(&css_set_lock); |
| 1571 | |
| 1572 | BUG_ON(!list_empty(&root_cgrp->children)); |
| 1573 | BUG_ON(root->number_of_cgroups != 1); |
| 1574 | |
| 1575 | ret = 0; |
| 1576 | goto out_unlock; |
| 1577 | |
| 1578 | rm_base_files: |
| 1579 | cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false); |
| 1580 | revert_creds(cred); |
| 1581 | cgroup_exit_root_id(root); |
| 1582 | out_unlock: |
| 1583 | mutex_unlock(&inode->i_mutex); |
| 1584 | free_cgrp_cset_links(&tmp_links); |
| 1585 | return ret; |
| 1586 | } |
| 1587 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1588 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1589 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1590 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1591 | { |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1592 | struct super_block *sb = NULL; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1593 | struct cgroupfs_root *root = NULL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1594 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1595 | struct cgroupfs_root *new_root; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1596 | int ret; |
| 1597 | |
| 1598 | mutex_lock(&cgroup_tree_mutex); |
| 1599 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1600 | |
| 1601 | /* First find the desired set of subsystems */ |
| 1602 | ret = parse_cgroupfs_options(data, &opts); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1603 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1604 | goto out_unlock; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1605 | |
| 1606 | /* |
| 1607 | * Allocate a new cgroup root. We may not need it if we're |
| 1608 | * reusing an existing hierarchy. |
| 1609 | */ |
| 1610 | new_root = cgroup_root_from_opts(&opts); |
| 1611 | if (IS_ERR(new_root)) { |
| 1612 | ret = PTR_ERR(new_root); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1613 | goto out_unlock; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1614 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1615 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1616 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1617 | /* Locate an existing or new sb for this hierarchy */ |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1618 | mutex_unlock(&cgroup_mutex); |
| 1619 | mutex_unlock(&cgroup_tree_mutex); |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1620 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1621 | mutex_lock(&cgroup_tree_mutex); |
| 1622 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1623 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1624 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1625 | cgroup_free_root(opts.new_root); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1626 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1629 | root = sb->s_fs_info; |
| 1630 | BUG_ON(!root); |
| 1631 | if (root == opts.new_root) { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1632 | ret = cgroup_setup_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1633 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1634 | goto out_unlock; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1635 | } else { |
| 1636 | /* |
| 1637 | * We re-used an existing hierarchy - the new root (if |
| 1638 | * any) is not needed |
| 1639 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1640 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1641 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1642 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1643 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1644 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1645 | ret = -EINVAL; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1646 | goto out_unlock; |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1647 | } else { |
| 1648 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1649 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1650 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1653 | ret = 0; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1654 | out_unlock: |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1655 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1656 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1657 | |
| 1658 | if (ret && !IS_ERR_OR_NULL(sb)) |
| 1659 | deactivate_locked_super(sb); |
| 1660 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1661 | kfree(opts.release_agent); |
| 1662 | kfree(opts.name); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1663 | |
| 1664 | if (!ret) |
| 1665 | return dget(sb->s_root); |
| 1666 | else |
| 1667 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 1670 | static void cgroup_kill_sb(struct super_block *sb) |
| 1671 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1672 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1673 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1674 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1675 | int ret; |
| 1676 | |
| 1677 | BUG_ON(!root); |
| 1678 | |
| 1679 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1680 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1681 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1682 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1683 | mutex_lock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1684 | mutex_lock(&cgroup_mutex); |
| 1685 | |
| 1686 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1687 | if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { |
| 1688 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
| 1689 | /* Shouldn't be able to fail ... */ |
| 1690 | BUG_ON(ret); |
| 1691 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1692 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1693 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1694 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1695 | * root cgroup |
| 1696 | */ |
| 1697 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1698 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1699 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1700 | list_del(&link->cset_link); |
| 1701 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1702 | kfree(link); |
| 1703 | } |
| 1704 | write_unlock(&css_set_lock); |
| 1705 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1706 | if (!list_empty(&root->root_list)) { |
| 1707 | list_del(&root->root_list); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1708 | cgroup_root_count--; |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1709 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1710 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1711 | cgroup_exit_root_id(root); |
| 1712 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1713 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1714 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1715 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1716 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1717 | simple_xattrs_free(&cgrp->xattrs); |
| 1718 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1719 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1720 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | static struct file_system_type cgroup_fs_type = { |
| 1724 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1725 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1726 | .kill_sb = cgroup_kill_sb, |
| 1727 | }; |
| 1728 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1729 | static struct kobject *cgroup_kobj; |
| 1730 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1731 | /** |
| 1732 | * cgroup_path - generate the path of a cgroup |
| 1733 | * @cgrp: the cgroup in question |
| 1734 | * @buf: the buffer to write the path into |
| 1735 | * @buflen: the length of the buffer |
| 1736 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1737 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1738 | * |
| 1739 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1740 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1741 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1742 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1743 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1744 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1745 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1746 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1747 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1748 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1749 | if (!cgrp->parent) { |
| 1750 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1751 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1752 | return 0; |
| 1753 | } |
| 1754 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1755 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1756 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1757 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1758 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1759 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1760 | const char *name = cgroup_name(cgrp); |
| 1761 | int len; |
| 1762 | |
| 1763 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1764 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1765 | goto out; |
| 1766 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1767 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1768 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1769 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1770 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1771 | |
| 1772 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1773 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1774 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1775 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1776 | out: |
| 1777 | rcu_read_unlock(); |
| 1778 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1779 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1780 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1781 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1782 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1783 | * 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] | 1784 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1785 | * @buf: the buffer to write the path into |
| 1786 | * @buflen: the length of the buffer |
| 1787 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1788 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1789 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1790 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1791 | * cgroup controller callbacks. |
| 1792 | * |
| 1793 | * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short. |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1794 | */ |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1795 | 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] | 1796 | { |
| 1797 | struct cgroupfs_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1798 | struct cgroup *cgrp; |
| 1799 | int hierarchy_id = 1, ret = 0; |
| 1800 | |
| 1801 | if (buflen < 2) |
| 1802 | return -ENAMETOOLONG; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1803 | |
| 1804 | mutex_lock(&cgroup_mutex); |
| 1805 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1806 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1807 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1808 | if (root) { |
| 1809 | cgrp = task_cgroup_from_root(task, root); |
| 1810 | ret = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1811 | } else { |
| 1812 | /* if no hierarchy exists, everyone is in "/" */ |
| 1813 | memcpy(buf, "/", 2); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1817 | return ret; |
| 1818 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1819 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1820 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1821 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1822 | * Control Group taskset |
| 1823 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1824 | struct task_and_cgroup { |
| 1825 | struct task_struct *task; |
| 1826 | struct cgroup *cgrp; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1827 | struct css_set *cset; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1828 | }; |
| 1829 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1830 | struct cgroup_taskset { |
| 1831 | struct task_and_cgroup single; |
| 1832 | struct flex_array *tc_array; |
| 1833 | int tc_array_len; |
| 1834 | int idx; |
| 1835 | struct cgroup *cur_cgrp; |
| 1836 | }; |
| 1837 | |
| 1838 | /** |
| 1839 | * cgroup_taskset_first - reset taskset and return the first task |
| 1840 | * @tset: taskset of interest |
| 1841 | * |
| 1842 | * @tset iteration is initialized and the first task is returned. |
| 1843 | */ |
| 1844 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1845 | { |
| 1846 | if (tset->tc_array) { |
| 1847 | tset->idx = 0; |
| 1848 | return cgroup_taskset_next(tset); |
| 1849 | } else { |
| 1850 | tset->cur_cgrp = tset->single.cgrp; |
| 1851 | return tset->single.task; |
| 1852 | } |
| 1853 | } |
| 1854 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1855 | |
| 1856 | /** |
| 1857 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1858 | * @tset: taskset of interest |
| 1859 | * |
| 1860 | * Return the next task in @tset. Iteration must have been initialized |
| 1861 | * with cgroup_taskset_first(). |
| 1862 | */ |
| 1863 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1864 | { |
| 1865 | struct task_and_cgroup *tc; |
| 1866 | |
| 1867 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1868 | return NULL; |
| 1869 | |
| 1870 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1871 | tset->cur_cgrp = tc->cgrp; |
| 1872 | return tc->task; |
| 1873 | } |
| 1874 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1875 | |
| 1876 | /** |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1877 | * cgroup_taskset_cur_css - return the matching css for the current task |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1878 | * @tset: taskset of interest |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1879 | * @subsys_id: the ID of the target subsystem |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1880 | * |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1881 | * Return the css for the current (last returned) task of @tset for |
| 1882 | * subsystem specified by @subsys_id. This function must be preceded by |
| 1883 | * either cgroup_taskset_first() or cgroup_taskset_next(). |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1884 | */ |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1885 | struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset, |
| 1886 | int subsys_id) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1887 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 1888 | return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1889 | } |
Tejun Heo | d99c872 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 1890 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1891 | |
| 1892 | /** |
| 1893 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1894 | * @tset: taskset of interest |
| 1895 | */ |
| 1896 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1897 | { |
| 1898 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1899 | } |
| 1900 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1901 | |
| 1902 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1903 | /* |
| 1904 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1905 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1906 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1907 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1908 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1909 | struct task_struct *tsk, |
| 1910 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1911 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1912 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1913 | |
| 1914 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1915 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1916 | * setting such that we can't race against cgroup_exit() changing the |
| 1917 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1918 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1919 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1920 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1921 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1922 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1923 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1924 | task_unlock(tsk); |
| 1925 | |
| 1926 | /* Update the css_set linked lists if we're using them */ |
| 1927 | write_lock(&css_set_lock); |
| 1928 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1929 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1930 | write_unlock(&css_set_lock); |
| 1931 | |
| 1932 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1933 | * We just gained a reference on old_cset by taking it from the |
| 1934 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1935 | * 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] | 1936 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1937 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1938 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1939 | } |
| 1940 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1941 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1942 | * 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] | 1943 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1944 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1945 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1946 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1947 | * 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] | 1948 | * 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] | 1949 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1950 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1951 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1952 | { |
| 1953 | int retval, i, group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1954 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1955 | struct cgroup_subsys_state *css, *failed_css = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1956 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1957 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1958 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1959 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1960 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1961 | |
| 1962 | /* |
| 1963 | * step 0: in order to do expensive, possibly blocking operations for |
| 1964 | * every thread, we cannot iterate the thread group list, since it needs |
| 1965 | * 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] | 1966 | * group - group_rwsem prevents new threads from appearing, and if |
| 1967 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1968 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1969 | if (threadgroup) |
| 1970 | group_size = get_nr_threads(tsk); |
| 1971 | else |
| 1972 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1973 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1974 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1975 | if (!group) |
| 1976 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1977 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 1978 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1979 | if (retval) |
| 1980 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1981 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1982 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1983 | /* |
| 1984 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 1985 | * already PF_EXITING could be freed from underneath us unless we |
| 1986 | * take an rcu_read_lock. |
| 1987 | */ |
| 1988 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1989 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1990 | struct task_and_cgroup ent; |
| 1991 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1992 | /* @tsk either already exited or can't exit until the end */ |
| 1993 | if (tsk->flags & PF_EXITING) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1994 | goto next; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1995 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1996 | /* as per above, nr_threads may decrease, but not increase. */ |
| 1997 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1998 | ent.task = tsk; |
| 1999 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2000 | /* nothing to do if this task is already in the cgroup */ |
| 2001 | if (ent.cgrp == cgrp) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2002 | goto next; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2003 | /* |
| 2004 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2005 | * earlier, but it's good form to communicate our expectations. |
| 2006 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2007 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2008 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2009 | i++; |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2010 | next: |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2011 | if (!threadgroup) |
| 2012 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2013 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2014 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2015 | /* remember the number of threads in the array for later. */ |
| 2016 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2017 | tset.tc_array = group; |
| 2018 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2019 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2020 | /* methods shouldn't be called if no task is actually migrating */ |
| 2021 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2022 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2023 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2024 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2025 | /* |
| 2026 | * step 1: check that we can legitimately attach to the cgroup. |
| 2027 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2028 | for_each_css(css, i, cgrp) { |
| 2029 | if (css->ss->can_attach) { |
| 2030 | retval = css->ss->can_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2031 | if (retval) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2032 | failed_css = css; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2033 | goto out_cancel_attach; |
| 2034 | } |
| 2035 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | /* |
| 2039 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2040 | * we use find_css_set, which allocates a new one if necessary. |
| 2041 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2042 | for (i = 0; i < group_size; i++) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2043 | struct css_set *old_cset; |
| 2044 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2045 | tc = flex_array_get(group, i); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2046 | old_cset = task_css_set(tc->task); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2047 | tc->cset = find_css_set(old_cset, cgrp); |
| 2048 | if (!tc->cset) { |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2049 | retval = -ENOMEM; |
| 2050 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2055 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2056 | * proceed to move all tasks to the new cgroup. There are no |
| 2057 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2058 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2059 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2060 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2061 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2062 | } |
| 2063 | /* nothing is sensitive to fork() after this point. */ |
| 2064 | |
| 2065 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2066 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2067 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2068 | for_each_css(css, i, cgrp) |
| 2069 | if (css->ss->attach) |
| 2070 | css->ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2071 | |
| 2072 | /* |
| 2073 | * step 5: success! and cleanup |
| 2074 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2075 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2076 | out_put_css_set_refs: |
| 2077 | if (retval) { |
| 2078 | for (i = 0; i < group_size; i++) { |
| 2079 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2080 | if (!tc->cset) |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2081 | break; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2082 | put_css_set(tc->cset); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2083 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2084 | } |
| 2085 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2086 | if (retval) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2087 | for_each_css(css, i, cgrp) { |
| 2088 | if (css == failed_css) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2089 | break; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2090 | if (css->ss->cancel_attach) |
| 2091 | css->ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2092 | } |
| 2093 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2094 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2095 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2096 | return retval; |
| 2097 | } |
| 2098 | |
| 2099 | /* |
| 2100 | * 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] | 2101 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2102 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2103 | */ |
| 2104 | 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] | 2105 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2106 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2107 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2108 | int ret; |
| 2109 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2110 | if (!cgroup_lock_live_group(cgrp)) |
| 2111 | return -ENODEV; |
| 2112 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2113 | retry_find_task: |
| 2114 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2115 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2116 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2117 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2118 | rcu_read_unlock(); |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2119 | ret = -ESRCH; |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2120 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2121 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2122 | /* |
| 2123 | * even if we're attaching all tasks in the thread group, we |
| 2124 | * only need to check permissions on one of them. |
| 2125 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2126 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2127 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2128 | !uid_eq(cred->euid, tcred->uid) && |
| 2129 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2130 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2131 | ret = -EACCES; |
| 2132 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2133 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2134 | } else |
| 2135 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2136 | |
| 2137 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2138 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2139 | |
| 2140 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2141 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2142 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2143 | * with no rt_runtime allocated. Just say no. |
| 2144 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2145 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2146 | ret = -EINVAL; |
| 2147 | rcu_read_unlock(); |
| 2148 | goto out_unlock_cgroup; |
| 2149 | } |
| 2150 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2151 | get_task_struct(tsk); |
| 2152 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2153 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2154 | threadgroup_lock(tsk); |
| 2155 | if (threadgroup) { |
| 2156 | if (!thread_group_leader(tsk)) { |
| 2157 | /* |
| 2158 | * a race with de_thread from another thread's exec() |
| 2159 | * may strip us of our leadership, if this happens, |
| 2160 | * there is no choice but to throw this task away and |
| 2161 | * try again; this is |
| 2162 | * "double-double-toil-and-trouble-check locking". |
| 2163 | */ |
| 2164 | threadgroup_unlock(tsk); |
| 2165 | put_task_struct(tsk); |
| 2166 | goto retry_find_task; |
| 2167 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2171 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2172 | threadgroup_unlock(tsk); |
| 2173 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2174 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2175 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2176 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2177 | return ret; |
| 2178 | } |
| 2179 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2180 | /** |
| 2181 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2182 | * @from: attach to all cgroups of a given task |
| 2183 | * @tsk: the task to be attached |
| 2184 | */ |
| 2185 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2186 | { |
| 2187 | struct cgroupfs_root *root; |
| 2188 | int retval = 0; |
| 2189 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2190 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2191 | for_each_active_root(root) { |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2192 | struct cgroup *from_cgrp = task_cgroup_from_root(from, root); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2193 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2194 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2195 | if (retval) |
| 2196 | break; |
| 2197 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2198 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2199 | |
| 2200 | return retval; |
| 2201 | } |
| 2202 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2203 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2204 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2205 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2206 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2207 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2208 | } |
| 2209 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2210 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2211 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2212 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2213 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2214 | } |
| 2215 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2216 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
| 2217 | struct cftype *cft, const char *buffer) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2218 | { |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2219 | struct cgroupfs_root *root = css->cgroup->root; |
| 2220 | |
| 2221 | BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2222 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2223 | return -ENODEV; |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2224 | spin_lock(&release_agent_path_lock); |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2225 | strlcpy(root->release_agent_path, buffer, |
| 2226 | sizeof(root->release_agent_path)); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2227 | spin_unlock(&release_agent_path_lock); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2228 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2229 | return 0; |
| 2230 | } |
| 2231 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2232 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2233 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2234 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2235 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2236 | if (!cgroup_lock_live_group(cgrp)) |
| 2237 | return -ENODEV; |
| 2238 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2239 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2240 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2241 | return 0; |
| 2242 | } |
| 2243 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2244 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2245 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2246 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2247 | |
| 2248 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2249 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2250 | } |
| 2251 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2252 | static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf, |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2253 | size_t nbytes, loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2254 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2255 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2256 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2257 | struct cgroup_subsys_state *css = cfe->css; |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2258 | size_t max_bytes = max(cft->max_write_len, PAGE_SIZE); |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2259 | char *buf; |
| 2260 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2261 | |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2262 | if (nbytes > max_bytes) |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2263 | return -E2BIG; |
| 2264 | |
| 2265 | buf = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2266 | if (!buf) |
| 2267 | return -ENOMEM; |
| 2268 | |
| 2269 | if (copy_from_user(buf, userbuf, nbytes)) { |
| 2270 | ret = -EFAULT; |
| 2271 | goto out_free; |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2272 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2273 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2274 | buf[nbytes] = '\0'; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2275 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2276 | if (cft->write_string) { |
| 2277 | ret = cft->write_string(css, cft, strstrip(buf)); |
| 2278 | } else if (cft->write_u64) { |
| 2279 | unsigned long long v; |
| 2280 | ret = kstrtoull(buf, 0, &v); |
| 2281 | if (!ret) |
| 2282 | ret = cft->write_u64(css, cft, v); |
| 2283 | } else if (cft->write_s64) { |
| 2284 | long long v; |
| 2285 | ret = kstrtoll(buf, 0, &v); |
| 2286 | if (!ret) |
| 2287 | ret = cft->write_s64(css, cft, v); |
| 2288 | } else if (cft->trigger) { |
| 2289 | ret = cft->trigger(css, (unsigned int)cft->private); |
| 2290 | } else { |
| 2291 | ret = -EINVAL; |
| 2292 | } |
| 2293 | out_free: |
| 2294 | kfree(buf); |
| 2295 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2298 | /* |
| 2299 | * seqfile ops/methods for returning structured data. Currently just |
| 2300 | * supports string->u64 maps, but can be extended in future. |
| 2301 | */ |
| 2302 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2303 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2304 | { |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2305 | struct cftype *cft = seq_cft(seq); |
| 2306 | |
| 2307 | if (cft->seq_start) { |
| 2308 | return cft->seq_start(seq, ppos); |
| 2309 | } else { |
| 2310 | /* |
| 2311 | * The same behavior and code as single_open(). Returns |
| 2312 | * !NULL if pos is at the beginning; otherwise, NULL. |
| 2313 | */ |
| 2314 | return NULL + !*ppos; |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 2319 | { |
| 2320 | struct cftype *cft = seq_cft(seq); |
| 2321 | |
| 2322 | if (cft->seq_next) { |
| 2323 | return cft->seq_next(seq, v, ppos); |
| 2324 | } else { |
| 2325 | /* |
| 2326 | * The same behavior and code as single_open(), always |
| 2327 | * terminate after the initial read. |
| 2328 | */ |
| 2329 | ++*ppos; |
| 2330 | return NULL; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 2335 | { |
| 2336 | struct cftype *cft = seq_cft(seq); |
| 2337 | |
| 2338 | if (cft->seq_stop) |
| 2339 | cft->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2343 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2344 | struct cftype *cft = seq_cft(m); |
| 2345 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2346 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2347 | if (cft->seq_show) |
| 2348 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2349 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2350 | if (cft->read_u64) |
| 2351 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 2352 | else if (cft->read_s64) |
| 2353 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 2354 | else |
| 2355 | return -EINVAL; |
| 2356 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2359 | static struct seq_operations cgroup_seq_operations = { |
| 2360 | .start = cgroup_seqfile_start, |
| 2361 | .next = cgroup_seqfile_next, |
| 2362 | .stop = cgroup_seqfile_stop, |
| 2363 | .show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2364 | }; |
| 2365 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2366 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2367 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2368 | struct cfent *cfe = __d_cfe(file->f_dentry); |
| 2369 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2370 | struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent); |
| 2371 | struct cgroup_subsys_state *css; |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2372 | struct cgroup_open_file *of; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2373 | int err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2374 | |
| 2375 | err = generic_file_open(inode, file); |
| 2376 | if (err) |
| 2377 | return err; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2378 | |
| 2379 | /* |
| 2380 | * If the file belongs to a subsystem, pin the css. Will be |
| 2381 | * unpinned either on open failure or release. This ensures that |
| 2382 | * @css stays alive for all file operations. |
| 2383 | */ |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2384 | rcu_read_lock(); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2385 | css = cgroup_css(cgrp, cft->ss); |
| 2386 | if (cft->ss && !css_tryget(css)) |
| 2387 | css = NULL; |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2388 | rcu_read_unlock(); |
| 2389 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2390 | if (!css) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2391 | return -ENODEV; |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2392 | |
Tejun Heo | 0bfb4aa | 2013-08-15 11:42:36 -0400 | [diff] [blame] | 2393 | /* |
| 2394 | * @cfe->css is used by read/write/close to determine the |
| 2395 | * associated css. @file->private_data would be a better place but |
| 2396 | * that's already used by seqfile. Multiple accessors may use it |
| 2397 | * simultaneously which is okay as the association never changes. |
| 2398 | */ |
| 2399 | WARN_ON_ONCE(cfe->css && cfe->css != css); |
| 2400 | cfe->css = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2401 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2402 | of = __seq_open_private(file, &cgroup_seq_operations, |
| 2403 | sizeof(struct cgroup_open_file)); |
| 2404 | if (of) { |
| 2405 | of->cfe = cfe; |
| 2406 | return 0; |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2407 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2408 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2409 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2410 | css_put(css); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2411 | return -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2415 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2416 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Tejun Heo | 105347b | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 2417 | struct cgroup_subsys_state *css = cfe->css; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2418 | |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2419 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2420 | css_put(css); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2421 | return seq_release_private(inode, file); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | /* |
| 2425 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2426 | */ |
| 2427 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2428 | struct inode *new_dir, struct dentry *new_dentry) |
| 2429 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2430 | int ret; |
| 2431 | struct cgroup_name *name, *old_name; |
| 2432 | struct cgroup *cgrp; |
| 2433 | |
| 2434 | /* |
| 2435 | * It's convinient to use parent dir's i_mutex to protected |
| 2436 | * cgrp->name. |
| 2437 | */ |
| 2438 | lockdep_assert_held(&old_dir->i_mutex); |
| 2439 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2440 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2441 | return -ENOTDIR; |
| 2442 | if (new_dentry->d_inode) |
| 2443 | return -EEXIST; |
| 2444 | if (old_dir != new_dir) |
| 2445 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2446 | |
| 2447 | cgrp = __d_cgrp(old_dentry); |
| 2448 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2449 | /* |
| 2450 | * This isn't a proper migration and its usefulness is very |
| 2451 | * limited. Disallow if sane_behavior. |
| 2452 | */ |
| 2453 | if (cgroup_sane_behavior(cgrp)) |
| 2454 | return -EPERM; |
| 2455 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2456 | name = cgroup_alloc_name(new_dentry->d_name.name); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2457 | if (!name) |
| 2458 | return -ENOMEM; |
| 2459 | |
| 2460 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2461 | if (ret) { |
| 2462 | kfree(name); |
| 2463 | return ret; |
| 2464 | } |
| 2465 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 2466 | old_name = rcu_dereference_protected(cgrp->name, true); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2467 | rcu_assign_pointer(cgrp->name, name); |
| 2468 | |
| 2469 | kfree_rcu(old_name, rcu_head); |
| 2470 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2473 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2474 | { |
| 2475 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2476 | return &__d_cgrp(dentry)->xattrs; |
| 2477 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2478 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | static inline int xattr_enabled(struct dentry *dentry) |
| 2482 | { |
| 2483 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2484 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | static bool is_valid_xattr(const char *name) |
| 2488 | { |
| 2489 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2490 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2491 | return true; |
| 2492 | return false; |
| 2493 | } |
| 2494 | |
| 2495 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2496 | const void *val, size_t size, int flags) |
| 2497 | { |
| 2498 | if (!xattr_enabled(dentry)) |
| 2499 | return -EOPNOTSUPP; |
| 2500 | if (!is_valid_xattr(name)) |
| 2501 | return -EINVAL; |
| 2502 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2503 | } |
| 2504 | |
| 2505 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2506 | { |
| 2507 | if (!xattr_enabled(dentry)) |
| 2508 | return -EOPNOTSUPP; |
| 2509 | if (!is_valid_xattr(name)) |
| 2510 | return -EINVAL; |
| 2511 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2512 | } |
| 2513 | |
| 2514 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2515 | void *buf, size_t size) |
| 2516 | { |
| 2517 | if (!xattr_enabled(dentry)) |
| 2518 | return -EOPNOTSUPP; |
| 2519 | if (!is_valid_xattr(name)) |
| 2520 | return -EINVAL; |
| 2521 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2522 | } |
| 2523 | |
| 2524 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2525 | { |
| 2526 | if (!xattr_enabled(dentry)) |
| 2527 | return -EOPNOTSUPP; |
| 2528 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2529 | } |
| 2530 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2531 | static const struct file_operations cgroup_file_operations = { |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2532 | .read = seq_read, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2533 | .write = cgroup_file_write, |
| 2534 | .llseek = generic_file_llseek, |
| 2535 | .open = cgroup_file_open, |
| 2536 | .release = cgroup_file_release, |
| 2537 | }; |
| 2538 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2539 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2540 | .setxattr = cgroup_setxattr, |
| 2541 | .getxattr = cgroup_getxattr, |
| 2542 | .listxattr = cgroup_listxattr, |
| 2543 | .removexattr = cgroup_removexattr, |
| 2544 | }; |
| 2545 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2546 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | 786e144 | 2013-07-14 17:50:23 +0400 | [diff] [blame] | 2547 | .lookup = simple_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2548 | .mkdir = cgroup_mkdir, |
| 2549 | .rmdir = cgroup_rmdir, |
| 2550 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2551 | .setxattr = cgroup_setxattr, |
| 2552 | .getxattr = cgroup_getxattr, |
| 2553 | .listxattr = cgroup_listxattr, |
| 2554 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2555 | }; |
| 2556 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2557 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2558 | struct super_block *sb) |
| 2559 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2560 | struct inode *inode; |
| 2561 | |
| 2562 | if (!dentry) |
| 2563 | return -ENOENT; |
| 2564 | if (dentry->d_inode) |
| 2565 | return -EEXIST; |
| 2566 | |
| 2567 | inode = cgroup_new_inode(mode, sb); |
| 2568 | if (!inode) |
| 2569 | return -ENOMEM; |
| 2570 | |
| 2571 | if (S_ISDIR(mode)) { |
| 2572 | inode->i_op = &cgroup_dir_inode_operations; |
| 2573 | inode->i_fop = &simple_dir_operations; |
| 2574 | |
| 2575 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2576 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2577 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2578 | |
Tejun Heo | b8a2df6a | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2579 | /* |
| 2580 | * Control reaches here with cgroup_mutex held. |
| 2581 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2582 | * want to populate it immediately without releasing |
| 2583 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2584 | * yet, trylock will always succeed without affecting |
| 2585 | * lockdep checks. |
| 2586 | */ |
| 2587 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2588 | } else if (S_ISREG(mode)) { |
| 2589 | inode->i_size = 0; |
| 2590 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2591 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2592 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2593 | d_instantiate(dentry, inode); |
| 2594 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2595 | return 0; |
| 2596 | } |
| 2597 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2598 | /** |
| 2599 | * cgroup_file_mode - deduce file mode of a control file |
| 2600 | * @cft: the control file in question |
| 2601 | * |
| 2602 | * returns cft->mode if ->mode is not 0 |
| 2603 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2604 | * returns S_IRUGO if it has only a read handler |
| 2605 | * returns S_IWUSR if it has only a write hander |
| 2606 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2607 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2608 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2609 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2610 | |
| 2611 | if (cft->mode) |
| 2612 | return cft->mode; |
| 2613 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2614 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2615 | mode |= S_IRUGO; |
| 2616 | |
Tejun Heo | 6e0755b | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2617 | if (cft->write_u64 || cft->write_s64 || cft->write_string || |
| 2618 | cft->trigger) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2619 | mode |= S_IWUSR; |
| 2620 | |
| 2621 | return mode; |
| 2622 | } |
| 2623 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2624 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2625 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2626 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2627 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2628 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2629 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2630 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2631 | umode_t mode; |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2632 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2633 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2634 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2635 | |
| 2636 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2637 | if (!cfe) |
| 2638 | return -ENOMEM; |
| 2639 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2640 | cgroup_file_name(cgrp, cft, name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2641 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2642 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2643 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2644 | goto out; |
| 2645 | } |
| 2646 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2647 | cfe->type = (void *)cft; |
| 2648 | cfe->dentry = dentry; |
| 2649 | dentry->d_fsdata = cfe; |
| 2650 | simple_xattrs_init(&cfe->xattrs); |
| 2651 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2652 | mode = cgroup_file_mode(cft); |
| 2653 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2654 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2655 | list_add_tail(&cfe->node, &parent->files); |
| 2656 | cfe = NULL; |
| 2657 | } |
| 2658 | dput(dentry); |
| 2659 | out: |
| 2660 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2661 | return error; |
| 2662 | } |
| 2663 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2664 | /** |
| 2665 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2666 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2667 | * @cfts: array of cftypes to be added |
| 2668 | * @is_add: whether to add or remove |
| 2669 | * |
| 2670 | * 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] | 2671 | * For removals, this function never fails. If addition fails, this |
| 2672 | * function doesn't remove files already added. The caller is responsible |
| 2673 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2674 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2675 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2676 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2677 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2678 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2679 | int ret; |
| 2680 | |
| 2681 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2682 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2683 | |
| 2684 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2685 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2686 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2687 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2688 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2689 | continue; |
| 2690 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2691 | continue; |
| 2692 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2693 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2694 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2695 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2696 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2697 | cft->name, ret); |
| 2698 | return ret; |
| 2699 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2700 | } else { |
| 2701 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2702 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2703 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2704 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2705 | } |
| 2706 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2707 | static void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2708 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2709 | { |
| 2710 | /* |
| 2711 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2712 | * the existing cgroups under cgroup_mutex and create files. |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2713 | * Instead, we use css_for_each_descendant_pre() and drop RCU read |
| 2714 | * lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2715 | */ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2716 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2717 | mutex_lock(&cgroup_mutex); |
| 2718 | } |
| 2719 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2720 | static int cgroup_cfts_commit(struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2721 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2722 | { |
| 2723 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2724 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2725 | struct cgroup *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2726 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2727 | struct dentry *prev = NULL; |
| 2728 | struct inode *inode; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2729 | struct cgroup_subsys_state *css; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2730 | u64 update_before; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2731 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2732 | |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2733 | mutex_unlock(&cgroup_mutex); |
| 2734 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2735 | /* %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] | 2736 | if (!cfts || ss->root == &cgroup_dummy_root || |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2737 | !atomic_inc_not_zero(&sb->s_active)) { |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2738 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2739 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2740 | } |
| 2741 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2742 | /* |
| 2743 | * All cgroups which are created after we drop cgroup_mutex will |
| 2744 | * 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] | 2745 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2746 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2747 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2748 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2749 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2750 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2751 | struct cgroup *cgrp = css->cgroup; |
| 2752 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2753 | if (cgroup_is_dead(cgrp)) |
| 2754 | continue; |
| 2755 | |
| 2756 | inode = cgrp->dentry->d_inode; |
| 2757 | dget(cgrp->dentry); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2758 | dput(prev); |
| 2759 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2760 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2761 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2762 | mutex_lock(&inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2763 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2764 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2765 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2766 | mutex_unlock(&inode->i_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2767 | if (ret) |
| 2768 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2769 | } |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2770 | mutex_unlock(&cgroup_tree_mutex); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2771 | dput(prev); |
| 2772 | deactivate_super(sb); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2773 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2776 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 2777 | { |
| 2778 | struct cftype *cft; |
| 2779 | |
| 2780 | for (cft = cfts; cft->name[0] != '\0'; cft++) |
| 2781 | cft->ss = NULL; |
| 2782 | } |
| 2783 | |
| 2784 | static void cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 2785 | { |
| 2786 | struct cftype *cft; |
| 2787 | |
| 2788 | for (cft = cfts; cft->name[0] != '\0'; cft++) |
| 2789 | cft->ss = ss; |
| 2790 | } |
| 2791 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2792 | /** |
| 2793 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2794 | * @ss: target cgroup subsystem |
| 2795 | * @cfts: zero-length name terminated array of cftypes |
| 2796 | * |
| 2797 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2798 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2799 | * have them too. This function can be called anytime whether @ss is |
| 2800 | * attached or not. |
| 2801 | * |
| 2802 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2803 | * function currently returns 0 as long as @cfts registration is successful |
| 2804 | * even if some file creation attempts on existing cgroups fail. |
| 2805 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2806 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2807 | { |
| 2808 | struct cftype_set *set; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2809 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2810 | |
| 2811 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2812 | if (!set) |
| 2813 | return -ENOMEM; |
| 2814 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2815 | cgroup_init_cftypes(ss, cfts); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2816 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2817 | cgroup_cfts_prepare(); |
| 2818 | set->cfts = cfts; |
| 2819 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2820 | ret = cgroup_cfts_commit(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2821 | if (ret) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2822 | cgroup_rm_cftypes(cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2823 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2824 | } |
| 2825 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2826 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2827 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2828 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2829 | * @cfts: zero-length name terminated array of cftypes |
| 2830 | * |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2831 | * Unregister @cfts. Files described by @cfts are removed from all |
| 2832 | * existing cgroups and all future cgroups won't have them either. This |
| 2833 | * function can be called anytime whether @cfts' subsys is attached or not. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2834 | * |
| 2835 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2836 | * registered. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2837 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2838 | int cgroup_rm_cftypes(struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2839 | { |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2840 | struct cftype *found = NULL; |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2841 | struct cftype_set *set; |
| 2842 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2843 | if (!cfts || !cfts[0].ss) |
| 2844 | return -ENOENT; |
| 2845 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2846 | cgroup_cfts_prepare(); |
| 2847 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2848 | list_for_each_entry(set, &cfts[0].ss->cftsets, node) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2849 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2850 | list_del(&set->node); |
| 2851 | kfree(set); |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2852 | found = cfts; |
| 2853 | break; |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2854 | } |
| 2855 | } |
| 2856 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2857 | cgroup_cfts_commit(found, false); |
| 2858 | cgroup_exit_cftypes(cfts); |
| 2859 | return found ? 0 : -ENOENT; |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2863 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2864 | * @cgrp: the cgroup in question |
| 2865 | * |
| 2866 | * Return the number of tasks in the cgroup. |
| 2867 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2868 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2869 | { |
| 2870 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2871 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2872 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2873 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2874 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2875 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2876 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2877 | return count; |
| 2878 | } |
| 2879 | |
| 2880 | /* |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2881 | * To reduce the fork() overhead for systems that are not actually using |
| 2882 | * their cgroups capability, we don't maintain the lists running through |
| 2883 | * 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] | 2884 | * words after the first call to css_task_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2885 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2886 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2887 | { |
| 2888 | struct task_struct *p, *g; |
| 2889 | write_lock(&css_set_lock); |
| 2890 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2891 | /* |
| 2892 | * We need tasklist_lock because RCU is not safe against |
| 2893 | * while_each_thread(). Besides, a forking task that has passed |
| 2894 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2895 | * is not guaranteed to have its child immediately visible in the |
| 2896 | * tasklist if we walk through it with RCU. |
| 2897 | */ |
| 2898 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2899 | do_each_thread(g, p) { |
| 2900 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2901 | /* |
| 2902 | * We should check if the process is exiting, otherwise |
| 2903 | * it will race with cgroup_exit() in that the list |
| 2904 | * entry won't be deleted though the process has exited. |
| 2905 | */ |
| 2906 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2907 | list_add(&p->cg_list, &task_css_set(p)->tasks); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2908 | task_unlock(p); |
| 2909 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2910 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2911 | write_unlock(&css_set_lock); |
| 2912 | } |
| 2913 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2914 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2915 | * css_next_child - find the next child of a given css |
| 2916 | * @pos_css: the current position (%NULL to initiate traversal) |
| 2917 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2918 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2919 | * 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] | 2920 | * under either cgroup_mutex or RCU read lock. The only requirement is |
| 2921 | * that @parent_css and @pos_css are accessible. The next sibling is |
| 2922 | * guaranteed to be returned regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2923 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2924 | struct cgroup_subsys_state * |
| 2925 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 2926 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2927 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2928 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 2929 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2930 | struct cgroup *next; |
| 2931 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2932 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2933 | |
| 2934 | /* |
| 2935 | * @pos could already have been removed. Once a cgroup is removed, |
| 2936 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 2937 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 2938 | * before the cgroup is taken off the ->sibling list, if we see it |
| 2939 | * unasserted, it's guaranteed that the next sibling hasn't |
| 2940 | * finished its grace period even if it's already removed, and thus |
| 2941 | * safe to dereference from this RCU critical section. If |
| 2942 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 2943 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2944 | * |
| 2945 | * If @pos is dead, its next pointer can't be dereferenced; |
| 2946 | * however, as each cgroup is given a monotonically increasing |
| 2947 | * unique serial number and always appended to the sibling list, |
| 2948 | * the next one can be found by walking the parent's children until |
| 2949 | * we see a cgroup with higher serial number than @pos's. While |
| 2950 | * this path can be slower, it's taken only when either the current |
| 2951 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2952 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2953 | if (!pos) { |
| 2954 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 2955 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2956 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2957 | } else { |
| 2958 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 2959 | if (next->serial_nr > pos->serial_nr) |
| 2960 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2961 | } |
| 2962 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2963 | if (&next->sibling == &cgrp->children) |
| 2964 | return NULL; |
| 2965 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2966 | return cgroup_css(next, parent_css->ss); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2967 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2968 | EXPORT_SYMBOL_GPL(css_next_child); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2969 | |
| 2970 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2971 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2972 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2973 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2974 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2975 | * 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] | 2976 | * to visit for pre-order traversal of @root's descendants. @root is |
| 2977 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2978 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2979 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2980 | * doesn't require the whole traversal to be contained in a single critical |
| 2981 | * section. This function will return the correct next descendant as long |
| 2982 | * 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] | 2983 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2984 | struct cgroup_subsys_state * |
| 2985 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 2986 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2987 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2988 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2989 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2990 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2991 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2992 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2993 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2994 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2995 | |
| 2996 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2997 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2998 | if (next) |
| 2999 | return next; |
| 3000 | |
| 3001 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3002 | while (pos != root) { |
| 3003 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3004 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3005 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3006 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3007 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3008 | |
| 3009 | return NULL; |
| 3010 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3011 | EXPORT_SYMBOL_GPL(css_next_descendant_pre); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3012 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3013 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3014 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 3015 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3016 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3017 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 3018 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3019 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3020 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3021 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3022 | * doesn't require the whole traversal to be contained in a single critical |
| 3023 | * section. This function will return the correct rightmost descendant as |
| 3024 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3025 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3026 | struct cgroup_subsys_state * |
| 3027 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3028 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3029 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3030 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3031 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3032 | |
| 3033 | do { |
| 3034 | last = pos; |
| 3035 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3036 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3037 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3038 | pos = tmp; |
| 3039 | } while (pos); |
| 3040 | |
| 3041 | return last; |
| 3042 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3043 | EXPORT_SYMBOL_GPL(css_rightmost_descendant); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3044 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3045 | static struct cgroup_subsys_state * |
| 3046 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3047 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3048 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3049 | |
| 3050 | do { |
| 3051 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3052 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3053 | } while (pos); |
| 3054 | |
| 3055 | return last; |
| 3056 | } |
| 3057 | |
| 3058 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3059 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3060 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3061 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3062 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3063 | * 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] | 3064 | * to visit for post-order traversal of @root's descendants. @root is |
| 3065 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3066 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3067 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3068 | * doesn't require the whole traversal to be contained in a single critical |
| 3069 | * section. This function will return the correct next descendant as long |
| 3070 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 3071 | * @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3072 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3073 | struct cgroup_subsys_state * |
| 3074 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 3075 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3076 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3077 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3078 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3079 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3080 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 3081 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 3082 | if (!pos) |
| 3083 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3084 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3085 | /* if we visited @root, we're done */ |
| 3086 | if (pos == root) |
| 3087 | return NULL; |
| 3088 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3089 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3090 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3091 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3092 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3093 | |
| 3094 | /* no sibling left, visit parent */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3095 | return css_parent(pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3096 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3097 | EXPORT_SYMBOL_GPL(css_next_descendant_post); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 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_advance_task_iter - advance a task itererator to the next css_set |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3101 | * @it: the iterator to advance |
| 3102 | * |
| 3103 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3104 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3105 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3106 | { |
| 3107 | struct list_head *l = it->cset_link; |
| 3108 | struct cgrp_cset_link *link; |
| 3109 | struct css_set *cset; |
| 3110 | |
| 3111 | /* Advance to the next non-empty css_set */ |
| 3112 | do { |
| 3113 | l = l->next; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3114 | if (l == &it->origin_css->cgroup->cset_links) { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3115 | it->cset_link = NULL; |
| 3116 | return; |
| 3117 | } |
| 3118 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3119 | cset = link->cset; |
| 3120 | } while (list_empty(&cset->tasks)); |
| 3121 | it->cset_link = l; |
| 3122 | it->task = cset->tasks.next; |
| 3123 | } |
| 3124 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3125 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3126 | * css_task_iter_start - initiate task iteration |
| 3127 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3128 | * @it: the task iterator to use |
| 3129 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3130 | * Initiate iteration through the tasks of @css. The caller can call |
| 3131 | * css_task_iter_next() to walk through the tasks until the function |
| 3132 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 3133 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3134 | * |
| 3135 | * Note that this function acquires a lock which is released when the |
| 3136 | * iteration finishes. The caller can't sleep while iteration is in |
| 3137 | * progress. |
| 3138 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3139 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 3140 | struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3141 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3142 | { |
| 3143 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3144 | * The first time anyone tries to iterate across a css, we need to |
| 3145 | * enable the list linking each css_set to its tasks, and fix up |
| 3146 | * all existing tasks. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3147 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3148 | if (!use_task_css_set_links) |
| 3149 | cgroup_enable_task_cg_lists(); |
| 3150 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3151 | read_lock(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3152 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3153 | it->origin_css = css; |
| 3154 | it->cset_link = &css->cgroup->cset_links; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3155 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3156 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3157 | } |
| 3158 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3159 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3160 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3161 | * @it: the task iterator being iterated |
| 3162 | * |
| 3163 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3164 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 3165 | * reaches the end. |
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 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3168 | { |
| 3169 | struct task_struct *res; |
| 3170 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3171 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3172 | |
| 3173 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3174 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3175 | return NULL; |
| 3176 | res = list_entry(l, struct task_struct, cg_list); |
| 3177 | /* Advance iterator to find next entry */ |
| 3178 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3179 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3180 | if (l == &link->cset->tasks) { |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3181 | /* |
| 3182 | * We reached the end of this task list - move on to the |
| 3183 | * next cgrp_cset_link. |
| 3184 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3185 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3186 | } else { |
| 3187 | it->task = l; |
| 3188 | } |
| 3189 | return res; |
| 3190 | } |
| 3191 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3192 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3193 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3194 | * @it: the task iterator to finish |
| 3195 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3196 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3197 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3198 | void css_task_iter_end(struct css_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3199 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3200 | { |
| 3201 | read_unlock(&css_set_lock); |
| 3202 | } |
| 3203 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3204 | static inline int started_after_time(struct task_struct *t1, |
| 3205 | struct timespec *time, |
| 3206 | struct task_struct *t2) |
| 3207 | { |
| 3208 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3209 | if (start_diff > 0) { |
| 3210 | return 1; |
| 3211 | } else if (start_diff < 0) { |
| 3212 | return 0; |
| 3213 | } else { |
| 3214 | /* |
| 3215 | * Arbitrarily, if two processes started at the same |
| 3216 | * time, we'll say that the lower pointer value |
| 3217 | * started first. Note that t2 may have exited by now |
| 3218 | * so this may not be a valid pointer any longer, but |
| 3219 | * that's fine - it still serves to distinguish |
| 3220 | * between two tasks started (effectively) simultaneously. |
| 3221 | */ |
| 3222 | return t1 > t2; |
| 3223 | } |
| 3224 | } |
| 3225 | |
| 3226 | /* |
| 3227 | * This function is a callback from heap_insert() and is used to order |
| 3228 | * the heap. |
| 3229 | * In this case we order the heap in descending task start time. |
| 3230 | */ |
| 3231 | static inline int started_after(void *p1, void *p2) |
| 3232 | { |
| 3233 | struct task_struct *t1 = p1; |
| 3234 | struct task_struct *t2 = p2; |
| 3235 | return started_after_time(t1, &t2->start_time, t2); |
| 3236 | } |
| 3237 | |
| 3238 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3239 | * css_scan_tasks - iterate though all the tasks in a css |
| 3240 | * @css: the css to iterate tasks of |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3241 | * @test: optional test callback |
| 3242 | * @process: process callback |
| 3243 | * @data: data passed to @test and @process |
| 3244 | * @heap: optional pre-allocated heap used for task iteration |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3245 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3246 | * Iterate through all the tasks in @css, calling @test for each, and if it |
| 3247 | * returns %true, call @process for it also. |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3248 | * |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3249 | * @test may be NULL, meaning always true (select all tasks), which |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3250 | * effectively duplicates css_task_iter_{start,next,end}() but does not |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3251 | * lock css_set_lock for the call to @process. |
| 3252 | * |
| 3253 | * 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] | 3254 | * of @css for the duration of this call. This function may or may not |
| 3255 | * call @process for tasks that exit or move to a different css during the |
| 3256 | * call, or are forked or move into the css during the call. |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3257 | * |
| 3258 | * Note that @test may be called with locks held, and may in some |
| 3259 | * situations be called multiple times for the same task, so it should be |
| 3260 | * cheap. |
| 3261 | * |
| 3262 | * If @heap is non-NULL, a heap has been pre-allocated and will be used for |
| 3263 | * heap operations (and its "gt" member will be overwritten), else a |
| 3264 | * temporary heap will be used (allocation of which may cause this function |
| 3265 | * to fail). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3266 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3267 | int css_scan_tasks(struct cgroup_subsys_state *css, |
| 3268 | bool (*test)(struct task_struct *, void *), |
| 3269 | void (*process)(struct task_struct *, void *), |
| 3270 | void *data, struct ptr_heap *heap) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3271 | { |
| 3272 | int retval, i; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3273 | struct css_task_iter it; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3274 | struct task_struct *p, *dropped; |
| 3275 | /* Never dereference latest_task, since it's not refcounted */ |
| 3276 | struct task_struct *latest_task = NULL; |
| 3277 | struct ptr_heap tmp_heap; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3278 | struct timespec latest_time = { 0, 0 }; |
| 3279 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3280 | if (heap) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3281 | /* The caller supplied our heap and pre-allocated its memory */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3282 | heap->gt = &started_after; |
| 3283 | } else { |
| 3284 | /* We need to allocate our own heap memory */ |
| 3285 | heap = &tmp_heap; |
| 3286 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3287 | if (retval) |
| 3288 | /* cannot allocate the heap */ |
| 3289 | return retval; |
| 3290 | } |
| 3291 | |
| 3292 | again: |
| 3293 | /* |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3294 | * Scan tasks in the css, using the @test callback to determine |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3295 | * which are of interest, and invoking @process callback on the |
| 3296 | * ones which need an update. Since we don't want to hold any |
| 3297 | * locks during the task updates, gather tasks to be processed in a |
| 3298 | * heap structure. The heap is sorted by descending task start |
| 3299 | * time. If the statically-sized heap fills up, we overflow tasks |
| 3300 | * that started later, and in future iterations only consider tasks |
| 3301 | * that started after the latest task in the previous pass. This |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3302 | * guarantees forward progress and that we don't miss any tasks. |
| 3303 | */ |
| 3304 | heap->size = 0; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3305 | css_task_iter_start(css, &it); |
| 3306 | while ((p = css_task_iter_next(&it))) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3307 | /* |
| 3308 | * Only affect tasks that qualify per the caller's callback, |
| 3309 | * if he provided one |
| 3310 | */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3311 | if (test && !test(p, data)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3312 | continue; |
| 3313 | /* |
| 3314 | * Only process tasks that started after the last task |
| 3315 | * we processed |
| 3316 | */ |
| 3317 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3318 | continue; |
| 3319 | dropped = heap_insert(heap, p); |
| 3320 | if (dropped == NULL) { |
| 3321 | /* |
| 3322 | * The new task was inserted; the heap wasn't |
| 3323 | * previously full |
| 3324 | */ |
| 3325 | get_task_struct(p); |
| 3326 | } else if (dropped != p) { |
| 3327 | /* |
| 3328 | * The new task was inserted, and pushed out a |
| 3329 | * different task |
| 3330 | */ |
| 3331 | get_task_struct(p); |
| 3332 | put_task_struct(dropped); |
| 3333 | } |
| 3334 | /* |
| 3335 | * Else the new task was newer than anything already in |
| 3336 | * the heap and wasn't inserted |
| 3337 | */ |
| 3338 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3339 | css_task_iter_end(&it); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3340 | |
| 3341 | if (heap->size) { |
| 3342 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3343 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3344 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3345 | latest_time = q->start_time; |
| 3346 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3347 | } |
| 3348 | /* Process the task per the caller's callback */ |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3349 | process(q, data); |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3350 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3351 | } |
| 3352 | /* |
| 3353 | * If we had to process any tasks at all, scan again |
| 3354 | * in case some of them were in the middle of forking |
| 3355 | * children that didn't get processed. |
| 3356 | * Not the most efficient way to do it, but it avoids |
| 3357 | * having to take callback_mutex in the fork path |
| 3358 | */ |
| 3359 | goto again; |
| 3360 | } |
| 3361 | if (heap == &tmp_heap) |
| 3362 | heap_free(&tmp_heap); |
| 3363 | return 0; |
| 3364 | } |
| 3365 | |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3366 | static void cgroup_transfer_one_task(struct task_struct *task, void *data) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3367 | { |
Tejun Heo | e535837 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3368 | struct cgroup *new_cgroup = data; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3369 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3370 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3371 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3372 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | /** |
| 3376 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3377 | * @to: cgroup to which the tasks will be moved |
| 3378 | * @from: cgroup in which the tasks currently reside |
| 3379 | */ |
| 3380 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3381 | { |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3382 | return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task, |
| 3383 | to, NULL); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3384 | } |
| 3385 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3386 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3387 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3388 | * |
| 3389 | * Reading this file can return large amounts of data if a cgroup has |
| 3390 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3391 | * but we cannot guarantee that the information we produce is correct |
| 3392 | * unless we produce it entirely atomically. |
| 3393 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3394 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3395 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3396 | /* which pidlist file are we talking about? */ |
| 3397 | enum cgroup_filetype { |
| 3398 | CGROUP_FILE_PROCS, |
| 3399 | CGROUP_FILE_TASKS, |
| 3400 | }; |
| 3401 | |
| 3402 | /* |
| 3403 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3404 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3405 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3406 | * to the cgroup. |
| 3407 | */ |
| 3408 | struct cgroup_pidlist { |
| 3409 | /* |
| 3410 | * used to find which pidlist is wanted. doesn't change as long as |
| 3411 | * this particular list stays in the list. |
| 3412 | */ |
| 3413 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3414 | /* array of xids */ |
| 3415 | pid_t *list; |
| 3416 | /* how many elements the above list has */ |
| 3417 | int length; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3418 | /* each of these stored in a list by its cgroup */ |
| 3419 | struct list_head links; |
| 3420 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3421 | struct cgroup *owner; |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3422 | /* for delayed destruction */ |
| 3423 | struct delayed_work destroy_dwork; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3424 | }; |
| 3425 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3426 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3427 | * The following two functions "fix" the issue where there are more pids |
| 3428 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3429 | * TODO: replace with a kernel-wide solution to this problem |
| 3430 | */ |
| 3431 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3432 | static void *pidlist_allocate(int count) |
| 3433 | { |
| 3434 | if (PIDLIST_TOO_LARGE(count)) |
| 3435 | return vmalloc(count * sizeof(pid_t)); |
| 3436 | else |
| 3437 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3438 | } |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3439 | |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3440 | static void pidlist_free(void *p) |
| 3441 | { |
| 3442 | if (is_vmalloc_addr(p)) |
| 3443 | vfree(p); |
| 3444 | else |
| 3445 | kfree(p); |
| 3446 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3447 | |
| 3448 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3449 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 3450 | * should be left afterwards. |
| 3451 | */ |
| 3452 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 3453 | { |
| 3454 | struct cgroup_pidlist *l, *tmp_l; |
| 3455 | |
| 3456 | mutex_lock(&cgrp->pidlist_mutex); |
| 3457 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 3458 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 3459 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3460 | |
| 3461 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 3462 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 3463 | } |
| 3464 | |
| 3465 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 3466 | { |
| 3467 | struct delayed_work *dwork = to_delayed_work(work); |
| 3468 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 3469 | destroy_dwork); |
| 3470 | struct cgroup_pidlist *tofree = NULL; |
| 3471 | |
| 3472 | mutex_lock(&l->owner->pidlist_mutex); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3473 | |
| 3474 | /* |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3475 | * Destroy iff we didn't get queued again. The state won't change |
| 3476 | * as destroy_dwork can only be queued while locked. |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3477 | */ |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3478 | if (!delayed_work_pending(dwork)) { |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3479 | list_del(&l->links); |
| 3480 | pidlist_free(l->list); |
| 3481 | put_pid_ns(l->key.ns); |
| 3482 | tofree = l; |
| 3483 | } |
| 3484 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3485 | mutex_unlock(&l->owner->pidlist_mutex); |
| 3486 | kfree(tofree); |
| 3487 | } |
| 3488 | |
| 3489 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3490 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3491 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3492 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3493 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3494 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3495 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3496 | |
| 3497 | /* |
| 3498 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3499 | * edge cases first; no work needs to be done for either |
| 3500 | */ |
| 3501 | if (length == 0 || length == 1) |
| 3502 | return length; |
| 3503 | /* src and dest walk down the list; dest counts unique elements */ |
| 3504 | for (src = 1; src < length; src++) { |
| 3505 | /* find next unique element */ |
| 3506 | while (list[src] == list[src-1]) { |
| 3507 | src++; |
| 3508 | if (src == length) |
| 3509 | goto after; |
| 3510 | } |
| 3511 | /* dest always points to where the next unique element goes */ |
| 3512 | list[dest] = list[src]; |
| 3513 | dest++; |
| 3514 | } |
| 3515 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3516 | return dest; |
| 3517 | } |
| 3518 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3519 | /* |
| 3520 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 3521 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 3522 | * different per namespace, each namespace needs differently sorted list, |
| 3523 | * making it impossible to use, for example, single rbtree of member tasks |
| 3524 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 3525 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 3526 | * pidlists keyed by cgroup and namespace. |
| 3527 | * |
| 3528 | * All this extra complexity was caused by the original implementation |
| 3529 | * committing to an entirely unnecessary property. In the long term, we |
| 3530 | * want to do away with it. Explicitly scramble sort order if |
| 3531 | * sane_behavior so that no such expectation exists in the new interface. |
| 3532 | * |
| 3533 | * Scrambling is done by swapping every two consecutive bits, which is |
| 3534 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 3535 | */ |
| 3536 | static pid_t pid_fry(pid_t pid) |
| 3537 | { |
| 3538 | unsigned a = pid & 0x55555555; |
| 3539 | unsigned b = pid & 0xAAAAAAAA; |
| 3540 | |
| 3541 | return (a << 1) | (b >> 1); |
| 3542 | } |
| 3543 | |
| 3544 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 3545 | { |
| 3546 | if (cgroup_sane_behavior(cgrp)) |
| 3547 | return pid_fry(pid); |
| 3548 | else |
| 3549 | return pid; |
| 3550 | } |
| 3551 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3552 | static int cmppid(const void *a, const void *b) |
| 3553 | { |
| 3554 | return *(pid_t *)a - *(pid_t *)b; |
| 3555 | } |
| 3556 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3557 | static int fried_cmppid(const void *a, const void *b) |
| 3558 | { |
| 3559 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 3560 | } |
| 3561 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3562 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3563 | enum cgroup_filetype type) |
| 3564 | { |
| 3565 | struct cgroup_pidlist *l; |
| 3566 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3567 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3568 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3569 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3570 | |
| 3571 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 3572 | if (l->key.type == type && l->key.ns == ns) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3573 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3574 | return NULL; |
| 3575 | } |
| 3576 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3577 | /* |
| 3578 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3579 | * returns with the lock on that pidlist already held, and takes care |
| 3580 | * of the use count, or returns NULL with no locks held if we're out of |
| 3581 | * memory. |
| 3582 | */ |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3583 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 3584 | enum cgroup_filetype type) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3585 | { |
| 3586 | struct cgroup_pidlist *l; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3587 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3588 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3589 | |
| 3590 | l = cgroup_pidlist_find(cgrp, type); |
| 3591 | if (l) |
| 3592 | return l; |
| 3593 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3594 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3595 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3596 | if (!l) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3597 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3598 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3599 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3600 | l->key.type = type; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3601 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 3602 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3603 | l->owner = cgrp; |
| 3604 | list_add(&l->links, &cgrp->pidlists); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3605 | return l; |
| 3606 | } |
| 3607 | |
| 3608 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3609 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3610 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3611 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3612 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3613 | { |
| 3614 | pid_t *array; |
| 3615 | int length; |
| 3616 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3617 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3618 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3619 | struct cgroup_pidlist *l; |
| 3620 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3621 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3622 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3623 | /* |
| 3624 | * If cgroup gets more users after we read count, we won't have |
| 3625 | * enough space - tough. This race is indistinguishable to the |
| 3626 | * caller from the case that the additional cgroup users didn't |
| 3627 | * show up until sometime later on. |
| 3628 | */ |
| 3629 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3630 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3631 | if (!array) |
| 3632 | return -ENOMEM; |
| 3633 | /* now, populate the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3634 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3635 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3636 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3637 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3638 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3639 | if (type == CGROUP_FILE_PROCS) |
| 3640 | pid = task_tgid_vnr(tsk); |
| 3641 | else |
| 3642 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3643 | if (pid > 0) /* make sure to only use valid results */ |
| 3644 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3645 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3646 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3647 | length = n; |
| 3648 | /* now sort & (if procs) strip out duplicates */ |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3649 | if (cgroup_sane_behavior(cgrp)) |
| 3650 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 3651 | else |
| 3652 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3653 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3654 | length = pidlist_uniq(array, length); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3655 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3656 | l = cgroup_pidlist_find_create(cgrp, type); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3657 | if (!l) { |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3658 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3659 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3660 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3661 | } |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3662 | |
| 3663 | /* store array, freeing old if necessary */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3664 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3665 | l->list = array; |
| 3666 | l->length = length; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3667 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3668 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3669 | } |
| 3670 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3671 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3672 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3673 | * @stats: cgroupstats to fill information into |
| 3674 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3675 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3676 | * |
| 3677 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3678 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3679 | */ |
| 3680 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3681 | { |
| 3682 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3683 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3684 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3685 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3686 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3687 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3688 | * Validate dentry by checking the superblock operations, |
| 3689 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3690 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3691 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3692 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3693 | goto err; |
| 3694 | |
| 3695 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3696 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3697 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3698 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3699 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3700 | switch (tsk->state) { |
| 3701 | case TASK_RUNNING: |
| 3702 | stats->nr_running++; |
| 3703 | break; |
| 3704 | case TASK_INTERRUPTIBLE: |
| 3705 | stats->nr_sleeping++; |
| 3706 | break; |
| 3707 | case TASK_UNINTERRUPTIBLE: |
| 3708 | stats->nr_uninterruptible++; |
| 3709 | break; |
| 3710 | case TASK_STOPPED: |
| 3711 | stats->nr_stopped++; |
| 3712 | break; |
| 3713 | default: |
| 3714 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3715 | stats->nr_io_wait++; |
| 3716 | break; |
| 3717 | } |
| 3718 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3719 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3720 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3721 | err: |
| 3722 | return ret; |
| 3723 | } |
| 3724 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3725 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3726 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3727 | * 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] | 3728 | * 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] | 3729 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3730 | */ |
| 3731 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3732 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3733 | { |
| 3734 | /* |
| 3735 | * Initially we receive a position value that corresponds to |
| 3736 | * one more than the last pid shown (or 0 on the first call or |
| 3737 | * after a seek to the start). Use a binary-search to find the |
| 3738 | * next pid to display, if any |
| 3739 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3740 | struct cgroup_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3741 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3742 | struct cgroup_pidlist *l; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3743 | enum cgroup_filetype type = seq_cft(s)->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3744 | int index = 0, pid = *pos; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3745 | int *iter, ret; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3746 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3747 | mutex_lock(&cgrp->pidlist_mutex); |
| 3748 | |
| 3749 | /* |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3750 | * !NULL @of->priv indicates that this isn't the first start() |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3751 | * after open. If the matching pidlist is around, we can use that. |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3752 | * 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] | 3753 | * could already have been destroyed. |
| 3754 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3755 | if (of->priv) |
| 3756 | of->priv = cgroup_pidlist_find(cgrp, type); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3757 | |
| 3758 | /* |
| 3759 | * Either this is the first start() after open or the matching |
| 3760 | * pidlist has been destroyed inbetween. Create a new one. |
| 3761 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3762 | if (!of->priv) { |
| 3763 | ret = pidlist_array_load(cgrp, type, |
| 3764 | (struct cgroup_pidlist **)&of->priv); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3765 | if (ret) |
| 3766 | return ERR_PTR(ret); |
| 3767 | } |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3768 | l = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3769 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3770 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3771 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3772 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3773 | while (index < end) { |
| 3774 | int mid = (index + end) / 2; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3775 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3776 | index = mid; |
| 3777 | break; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3778 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3779 | index = mid + 1; |
| 3780 | else |
| 3781 | end = mid; |
| 3782 | } |
| 3783 | } |
| 3784 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3785 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3786 | return NULL; |
| 3787 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3788 | iter = l->list + index; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3789 | *pos = cgroup_pid_fry(cgrp, *iter); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3790 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3791 | } |
| 3792 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3793 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3794 | { |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3795 | struct cgroup_open_file *of = s->private; |
| 3796 | struct cgroup_pidlist *l = of->priv; |
Tejun Heo | 6223685 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3797 | |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3798 | if (l) |
| 3799 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3800 | CGROUP_PIDLIST_DESTROY_DELAY); |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3801 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3802 | } |
| 3803 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3804 | 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] | 3805 | { |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3806 | struct cgroup_open_file *of = s->private; |
| 3807 | struct cgroup_pidlist *l = of->priv; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3808 | pid_t *p = v; |
| 3809 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3810 | /* |
| 3811 | * Advance to the next pid in the array. If this goes off the |
| 3812 | * end, we're done |
| 3813 | */ |
| 3814 | p++; |
| 3815 | if (p >= end) { |
| 3816 | return NULL; |
| 3817 | } else { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3818 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3819 | return p; |
| 3820 | } |
| 3821 | } |
| 3822 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3823 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3824 | { |
| 3825 | return seq_printf(s, "%d\n", *(int *)v); |
| 3826 | } |
| 3827 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3828 | /* |
| 3829 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3830 | * independent of whether it's tasks or procs |
| 3831 | */ |
| 3832 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3833 | .start = cgroup_pidlist_start, |
| 3834 | .stop = cgroup_pidlist_stop, |
| 3835 | .next = cgroup_pidlist_next, |
| 3836 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3837 | }; |
| 3838 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3839 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3840 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3841 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3842 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3843 | } |
| 3844 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3845 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3846 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3847 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3848 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3849 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3850 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3851 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3852 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3853 | return 0; |
| 3854 | } |
| 3855 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3856 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3857 | * When dput() is called asynchronously, if umount has been done and |
| 3858 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3859 | * there's a small window that vfs will see the root dentry with non-zero |
| 3860 | * refcnt and trigger BUG(). |
| 3861 | * |
| 3862 | * That's why we hold a reference before dput() and drop it right after. |
| 3863 | */ |
| 3864 | static void cgroup_dput(struct cgroup *cgrp) |
| 3865 | { |
| 3866 | struct super_block *sb = cgrp->root->sb; |
| 3867 | |
| 3868 | atomic_inc(&sb->s_active); |
| 3869 | dput(cgrp->dentry); |
| 3870 | deactivate_super(sb); |
| 3871 | } |
| 3872 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3873 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 3874 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3875 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3876 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3877 | } |
| 3878 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3879 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 3880 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3881 | { |
| 3882 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3883 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3884 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3885 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3886 | return 0; |
| 3887 | } |
| 3888 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3889 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3890 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3891 | .name = "cgroup.procs", |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3892 | .seq_start = cgroup_pidlist_start, |
| 3893 | .seq_next = cgroup_pidlist_next, |
| 3894 | .seq_stop = cgroup_pidlist_stop, |
| 3895 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3896 | .private = CGROUP_FILE_PROCS, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3897 | .write_u64 = cgroup_procs_write, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3898 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3899 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3900 | { |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3901 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3902 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3903 | .read_u64 = cgroup_clone_children_read, |
| 3904 | .write_u64 = cgroup_clone_children_write, |
| 3905 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3906 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3907 | .name = "cgroup.sane_behavior", |
| 3908 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3909 | .seq_show = cgroup_sane_behavior_show, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3910 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3911 | |
| 3912 | /* |
| 3913 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 3914 | * don't exist if sane_behavior. If you're depending on these, be |
| 3915 | * prepared to be burned. |
| 3916 | */ |
| 3917 | { |
| 3918 | .name = "tasks", |
| 3919 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3920 | .seq_start = cgroup_pidlist_start, |
| 3921 | .seq_next = cgroup_pidlist_next, |
| 3922 | .seq_stop = cgroup_pidlist_stop, |
| 3923 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3924 | .private = CGROUP_FILE_TASKS, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3925 | .write_u64 = cgroup_tasks_write, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3926 | .mode = S_IRUGO | S_IWUSR, |
| 3927 | }, |
| 3928 | { |
| 3929 | .name = "notify_on_release", |
| 3930 | .flags = CFTYPE_INSANE, |
| 3931 | .read_u64 = cgroup_read_notify_on_release, |
| 3932 | .write_u64 = cgroup_write_notify_on_release, |
| 3933 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3934 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3935 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 3936 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3937 | .seq_show = cgroup_release_agent_show, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3938 | .write_string = cgroup_release_agent_write, |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3939 | .max_write_len = PATH_MAX - 1, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3940 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3941 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3942 | }; |
| 3943 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3944 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3945 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3946 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3947 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3948 | * |
| 3949 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3950 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3951 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3952 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3953 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3954 | int i, ret = 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3955 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3956 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3957 | for_each_subsys(ss, i) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3958 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3959 | |
| 3960 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3961 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3962 | |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3963 | list_for_each_entry(set, &ss->cftsets, node) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3964 | ret = cgroup_addrm_files(cgrp, set->cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3965 | if (ret < 0) |
| 3966 | goto err; |
| 3967 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3968 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3969 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3970 | err: |
| 3971 | cgroup_clear_dir(cgrp, subsys_mask); |
| 3972 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3973 | } |
| 3974 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3975 | /* |
| 3976 | * css destruction is four-stage process. |
| 3977 | * |
| 3978 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 3979 | * Implemented in kill_css(). |
| 3980 | * |
| 3981 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
| 3982 | * and thus css_tryget() is guaranteed to fail, the css can be offlined |
| 3983 | * by invoking offline_css(). After offlining, the base ref is put. |
| 3984 | * Implemented in css_killed_work_fn(). |
| 3985 | * |
| 3986 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 3987 | * accessors are inside RCU read sections. css_release() schedules the |
| 3988 | * RCU callback. |
| 3989 | * |
| 3990 | * 4. After the grace period, the css can be freed. Implemented in |
| 3991 | * css_free_work_fn(). |
| 3992 | * |
| 3993 | * It is actually hairier because both step 2 and 4 require process context |
| 3994 | * and thus involve punting to css->destroy_work adding two additional |
| 3995 | * steps to the already complex sequence. |
| 3996 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3997 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3998 | { |
| 3999 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4000 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4001 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4002 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4003 | if (css->parent) |
| 4004 | css_put(css->parent); |
| 4005 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4006 | css->ss->css_free(css); |
| 4007 | cgroup_dput(cgrp); |
| 4008 | } |
| 4009 | |
| 4010 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 4011 | { |
| 4012 | struct cgroup_subsys_state *css = |
| 4013 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 4014 | |
| 4015 | /* |
| 4016 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4017 | * css_put(). dput() requires process context which we don't have. |
| 4018 | */ |
| 4019 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4020 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4021 | } |
| 4022 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4023 | static void css_release(struct percpu_ref *ref) |
| 4024 | { |
| 4025 | struct cgroup_subsys_state *css = |
| 4026 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4027 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4028 | rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4029 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4030 | } |
| 4031 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4032 | static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss, |
| 4033 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4034 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4035 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4036 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4037 | css->flags = 0; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4038 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4039 | if (cgrp->parent) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4040 | css->parent = cgroup_css(cgrp->parent, ss); |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4041 | else |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4042 | css->flags |= CSS_ROOT; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4043 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4044 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4045 | } |
| 4046 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4047 | /* 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] | 4048 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4049 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4050 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4051 | int ret = 0; |
| 4052 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4053 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4054 | lockdep_assert_held(&cgroup_mutex); |
| 4055 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4056 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4057 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4058 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4059 | css->flags |= CSS_ONLINE; |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4060 | css->cgroup->nr_css++; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4061 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4062 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4063 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4064 | } |
| 4065 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4066 | /* 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] | 4067 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4068 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4069 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4070 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4071 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4072 | lockdep_assert_held(&cgroup_mutex); |
| 4073 | |
| 4074 | if (!(css->flags & CSS_ONLINE)) |
| 4075 | return; |
| 4076 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4077 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4078 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4079 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4080 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4081 | css->cgroup->nr_css--; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4082 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4083 | } |
| 4084 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4085 | /** |
| 4086 | * create_css - create a cgroup_subsys_state |
| 4087 | * @cgrp: the cgroup new css will be associated with |
| 4088 | * @ss: the subsys of new css |
| 4089 | * |
| 4090 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
| 4091 | * css is online and installed in @cgrp with all interface files created. |
| 4092 | * Returns 0 on success, -errno on failure. |
| 4093 | */ |
| 4094 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss) |
| 4095 | { |
| 4096 | struct cgroup *parent = cgrp->parent; |
| 4097 | struct cgroup_subsys_state *css; |
| 4098 | int err; |
| 4099 | |
| 4100 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 4101 | lockdep_assert_held(&cgroup_mutex); |
| 4102 | |
| 4103 | css = ss->css_alloc(cgroup_css(parent, ss)); |
| 4104 | if (IS_ERR(css)) |
| 4105 | return PTR_ERR(css); |
| 4106 | |
| 4107 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4108 | if (err) |
| 4109 | goto err_free; |
| 4110 | |
| 4111 | init_css(css, ss, cgrp); |
| 4112 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4113 | err = cgroup_populate_dir(cgrp, 1 << ss->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4114 | if (err) |
| 4115 | goto err_free; |
| 4116 | |
| 4117 | err = online_css(css); |
| 4118 | if (err) |
| 4119 | goto err_free; |
| 4120 | |
| 4121 | dget(cgrp->dentry); |
| 4122 | css_get(css->parent); |
| 4123 | |
| 4124 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4125 | parent->parent) { |
| 4126 | 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", |
| 4127 | current->comm, current->pid, ss->name); |
| 4128 | if (!strcmp(ss->name, "memory")) |
| 4129 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4130 | ss->warned_broken_hierarchy = true; |
| 4131 | } |
| 4132 | |
| 4133 | return 0; |
| 4134 | |
| 4135 | err_free: |
| 4136 | percpu_ref_cancel_init(&css->refcnt); |
| 4137 | ss->css_free(css); |
| 4138 | return err; |
| 4139 | } |
| 4140 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4141 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4142 | * cgroup_create - create a cgroup |
| 4143 | * @parent: cgroup that will be parent of the new cgroup |
| 4144 | * @dentry: dentry of the new cgroup |
| 4145 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4146 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4147 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4148 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4149 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4150 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4151 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4152 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4153 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4154 | struct cgroupfs_root *root = parent->root; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 4155 | int ssid, err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4156 | struct cgroup_subsys *ss; |
| 4157 | struct super_block *sb = root->sb; |
| 4158 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4159 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4160 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4161 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4162 | return -ENOMEM; |
| 4163 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4164 | name = cgroup_alloc_name(dentry->d_name.name); |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 4165 | if (!name) { |
| 4166 | err = -ENOMEM; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4167 | goto err_free_cgrp; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 4168 | } |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4169 | rcu_assign_pointer(cgrp->name, name); |
| 4170 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4171 | mutex_lock(&cgroup_tree_mutex); |
| 4172 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4173 | /* |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4174 | * Only live parents can have children. Note that the liveliness |
| 4175 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4176 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4177 | * anyway so that locking is contained inside cgroup proper and we |
| 4178 | * don't get nasty surprises if we ever grow another caller. |
| 4179 | */ |
| 4180 | if (!cgroup_lock_live_group(parent)) { |
| 4181 | err = -ENODEV; |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4182 | goto err_unlock_tree; |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | /* |
| 4186 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4187 | * a half-baked cgroup. |
| 4188 | */ |
| 4189 | cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL); |
| 4190 | if (cgrp->id < 0) { |
| 4191 | err = -ENOMEM; |
| 4192 | goto err_unlock; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4193 | } |
| 4194 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4195 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4196 | * get deleted on unmount if there are child cgroups. This |
| 4197 | * can be done outside cgroup_mutex, since the sb can't |
| 4198 | * disappear while someone has an open control file on the |
| 4199 | * fs */ |
| 4200 | atomic_inc(&sb->s_active); |
| 4201 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4202 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4203 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4204 | dentry->d_fsdata = cgrp; |
| 4205 | cgrp->dentry = dentry; |
| 4206 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4207 | cgrp->parent = parent; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4208 | cgrp->dummy_css.parent = &parent->dummy_css; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4209 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4210 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4211 | if (notify_on_release(parent)) |
| 4212 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4213 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4214 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4215 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4216 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4217 | /* |
| 4218 | * Create directory. cgroup_create_file() returns with the new |
| 4219 | * directory locked on success so that it can be populated without |
| 4220 | * dropping cgroup_mutex. |
| 4221 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4222 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4223 | if (err < 0) |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4224 | goto err_free_id; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4225 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4226 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4227 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4228 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4229 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4230 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4231 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4232 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4233 | /* hold a ref to the parent's dentry */ |
| 4234 | dget(parent->dentry); |
| 4235 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4236 | /* |
| 4237 | * @cgrp is now fully operational. If something fails after this |
| 4238 | * point, it'll be released via the normal destruction path. |
| 4239 | */ |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4240 | idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 4241 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4242 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4243 | if (err) |
| 4244 | goto err_destroy; |
| 4245 | |
Tejun Heo | 9d403e9 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4246 | /* let's create and online css's */ |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4247 | for_each_subsys(ss, ssid) { |
| 4248 | if (root->subsys_mask & (1 << ssid)) { |
| 4249 | err = create_css(cgrp, ss); |
| 4250 | if (err) |
| 4251 | goto err_destroy; |
| 4252 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4253 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4254 | |
| 4255 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4256 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4257 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4258 | |
| 4259 | return 0; |
| 4260 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4261 | err_free_id: |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4262 | idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4263 | /* Release the reference count that we took on the superblock */ |
| 4264 | deactivate_super(sb); |
| 4265 | err_unlock: |
| 4266 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4267 | err_unlock_tree: |
| 4268 | mutex_unlock(&cgroup_tree_mutex); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4269 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4270 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4271 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4272 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4273 | |
| 4274 | err_destroy: |
| 4275 | cgroup_destroy_locked(cgrp); |
| 4276 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4277 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4278 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4279 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4280 | } |
| 4281 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4282 | 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] | 4283 | { |
| 4284 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4285 | |
| 4286 | /* the vfs holds inode->i_mutex already */ |
| 4287 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4288 | } |
| 4289 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4290 | /* |
| 4291 | * This is called when the refcnt of a css is confirmed to be killed. |
| 4292 | * css_tryget() is now guaranteed to fail. |
| 4293 | */ |
| 4294 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4295 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4296 | struct cgroup_subsys_state *css = |
| 4297 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 4298 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4299 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4300 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4301 | mutex_lock(&cgroup_mutex); |
| 4302 | |
| 4303 | /* |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4304 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4305 | * initate destruction. |
| 4306 | */ |
| 4307 | offline_css(css); |
| 4308 | |
| 4309 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4310 | * If @cgrp is marked dead, it's waiting for refs of all css's to |
| 4311 | * be disabled before proceeding to the second phase of cgroup |
| 4312 | * destruction. If we are the last one, kick it off. |
| 4313 | */ |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4314 | if (!cgrp->nr_css && cgroup_is_dead(cgrp)) |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4315 | cgroup_destroy_css_killed(cgrp); |
| 4316 | |
| 4317 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4318 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4319 | |
| 4320 | /* |
| 4321 | * Put the css refs from kill_css(). Each css holds an extra |
| 4322 | * reference to the cgroup's dentry and cgroup removal proceeds |
| 4323 | * regardless of css refs. On the last put of each css, whenever |
| 4324 | * that may be, the extra dentry ref is put so that dentry |
| 4325 | * destruction happens only after all css's are released. |
| 4326 | */ |
| 4327 | css_put(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4328 | } |
| 4329 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4330 | /* css kill confirmation processing requires process context, bounce */ |
| 4331 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4332 | { |
| 4333 | struct cgroup_subsys_state *css = |
| 4334 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4335 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4336 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4337 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | /** |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4341 | * kill_css - destroy a css |
| 4342 | * @css: css to destroy |
| 4343 | * |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4344 | * This function initiates destruction of @css by removing cgroup interface |
| 4345 | * files and putting its base reference. ->css_offline() will be invoked |
| 4346 | * asynchronously once css_tryget() is guaranteed to fail and when the |
| 4347 | * reference count reaches zero, @css will be released. |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4348 | */ |
| 4349 | static void kill_css(struct cgroup_subsys_state *css) |
| 4350 | { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4351 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4352 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4353 | /* |
| 4354 | * Killing would put the base ref, but we need to keep it alive |
| 4355 | * until after ->css_offline(). |
| 4356 | */ |
| 4357 | css_get(css); |
| 4358 | |
| 4359 | /* |
| 4360 | * cgroup core guarantees that, by the time ->css_offline() is |
| 4361 | * invoked, no new css reference will be given out via |
| 4362 | * css_tryget(). We can't simply call percpu_ref_kill() and |
| 4363 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 4364 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 4365 | * |
| 4366 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4367 | * css is confirmed to be seen as killed on all CPUs. |
| 4368 | */ |
| 4369 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | /** |
| 4373 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4374 | * @cgrp: cgroup to be destroyed |
| 4375 | * |
| 4376 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4377 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4378 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4379 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4380 | * the following two steps. |
| 4381 | * |
| 4382 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4383 | * userland visible parts and start killing the percpu refcnts of |
| 4384 | * css's. Set up so that the next stage will be kicked off once all |
| 4385 | * the percpu refcnts are confirmed to be killed. |
| 4386 | * |
| 4387 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4388 | * rest of destruction. Once all cgroup references are gone, the |
| 4389 | * cgroup is RCU-freed. |
| 4390 | * |
| 4391 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4392 | * the userland is concerned and a new cgroup with the same name may be |
| 4393 | * created. As cgroup doesn't care about the names internally, this |
| 4394 | * doesn't cause any problem. |
| 4395 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4396 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4397 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4398 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4399 | struct dentry *d = cgrp->dentry; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4400 | struct cgroup_subsys_state *css; |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4401 | struct cgroup *child; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4402 | bool empty; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4403 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4404 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4405 | lockdep_assert_held(&d->d_inode->i_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4406 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4407 | lockdep_assert_held(&cgroup_mutex); |
| 4408 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4409 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4410 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4411 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4412 | */ |
| 4413 | read_lock(&css_set_lock); |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4414 | empty = list_empty(&cgrp->cset_links); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4415 | read_unlock(&css_set_lock); |
| 4416 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4417 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4418 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4419 | /* |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4420 | * Make sure there's no live children. We can't test ->children |
| 4421 | * emptiness as dead children linger on it while being destroyed; |
| 4422 | * otherwise, "rmdir parent/child parent" may fail with -EBUSY. |
| 4423 | */ |
| 4424 | empty = true; |
| 4425 | rcu_read_lock(); |
| 4426 | list_for_each_entry_rcu(child, &cgrp->children, sibling) { |
| 4427 | empty = cgroup_is_dead(child); |
| 4428 | if (!empty) |
| 4429 | break; |
| 4430 | } |
| 4431 | rcu_read_unlock(); |
| 4432 | if (!empty) |
| 4433 | return -EBUSY; |
| 4434 | |
| 4435 | /* |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4436 | * Initiate massacre of all css's. cgroup_destroy_css_killed() |
| 4437 | * will be invoked to perform the rest of destruction once the |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4438 | * percpu refs of all css's are confirmed to be killed. This |
| 4439 | * involves removing the subsystem's files, drop cgroup_mutex. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4440 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4441 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4442 | for_each_css(css, ssid, cgrp) |
| 4443 | kill_css(css); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4444 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4445 | |
| 4446 | /* |
| 4447 | * Mark @cgrp dead. This prevents further task migration and child |
| 4448 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4449 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4450 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4451 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4452 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4453 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4454 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4455 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4456 | raw_spin_lock(&release_list_lock); |
| 4457 | if (!list_empty(&cgrp->release_list)) |
| 4458 | list_del_init(&cgrp->release_list); |
| 4459 | raw_spin_unlock(&release_list_lock); |
| 4460 | |
| 4461 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4462 | * If @cgrp has css's attached, the second stage of cgroup |
| 4463 | * destruction is kicked off from css_killed_work_fn() after the |
| 4464 | * refs of all attached css's are killed. If @cgrp doesn't have |
| 4465 | * any css, we kick it off here. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4466 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4467 | if (!cgrp->nr_css) |
| 4468 | cgroup_destroy_css_killed(cgrp); |
| 4469 | |
| 4470 | /* |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4471 | * Clear the base files and remove @cgrp directory. The removal |
| 4472 | * puts the base ref but we aren't quite done with @cgrp yet, so |
| 4473 | * hold onto it. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4474 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4475 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4476 | cgroup_addrm_files(cgrp, cgroup_base_files, false); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4477 | dget(d); |
| 4478 | cgroup_d_remove_dir(d); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4479 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4480 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4481 | return 0; |
| 4482 | }; |
| 4483 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4484 | /** |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4485 | * cgroup_destroy_css_killed - the second step of cgroup destruction |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4486 | * @work: cgroup->destroy_free_work |
| 4487 | * |
| 4488 | * 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] | 4489 | * destroyed after all css's are offlined and performs the rest of |
| 4490 | * destruction. This is the second step of destruction described in the |
| 4491 | * comment above cgroup_destroy_locked(). |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4492 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4493 | static void cgroup_destroy_css_killed(struct cgroup *cgrp) |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4494 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4495 | struct cgroup *parent = cgrp->parent; |
| 4496 | struct dentry *d = cgrp->dentry; |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4497 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4498 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4499 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4500 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4501 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4502 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4503 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4504 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4505 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4506 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4507 | check_for_release(parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4508 | } |
| 4509 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4510 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4511 | { |
| 4512 | int ret; |
| 4513 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4514 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4515 | mutex_lock(&cgroup_mutex); |
| 4516 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4517 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4518 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4519 | |
| 4520 | return ret; |
| 4521 | } |
| 4522 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4523 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4524 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4525 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4526 | |
| 4527 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4528 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4529 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4530 | mutex_lock(&cgroup_mutex); |
| 4531 | |
Tejun Heo | de00ffa | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4532 | INIT_LIST_HEAD(&ss->cftsets); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4533 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4534 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4535 | ss->root = &cgroup_dummy_root; |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4536 | css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4537 | /* We don't handle early failures gracefully */ |
| 4538 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4539 | init_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4540 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4541 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4542 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4543 | * newly registered, all tasks and hence the |
| 4544 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4545 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4546 | |
| 4547 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4548 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4549 | /* At system boot, before all subsystems have been |
| 4550 | * registered, no tasks have been forked, so we don't |
| 4551 | * need to invoke fork callbacks here. */ |
| 4552 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4553 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4554 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4555 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4556 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4557 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4561 | * cgroup_init_early - cgroup initialization at system boot |
| 4562 | * |
| 4563 | * Initialize cgroups at system boot, and initialize any |
| 4564 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4565 | */ |
| 4566 | int __init cgroup_init_early(void) |
| 4567 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4568 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4569 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4570 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4571 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4572 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4573 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4574 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4575 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4576 | init_cgroup_root(&cgroup_dummy_root); |
| 4577 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4578 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4579 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4580 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4581 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4582 | 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] | 4583 | 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] | 4584 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4585 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4586 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4587 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n", |
| 4588 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4589 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4590 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 4591 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
| 4592 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4593 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4594 | ss->name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4595 | |
| 4596 | if (ss->early_init) |
| 4597 | cgroup_init_subsys(ss); |
| 4598 | } |
| 4599 | return 0; |
| 4600 | } |
| 4601 | |
| 4602 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4603 | * cgroup_init - cgroup initialization |
| 4604 | * |
| 4605 | * Register cgroup filesystem and /proc file, and initialize |
| 4606 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4607 | */ |
| 4608 | int __init cgroup_init(void) |
| 4609 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4610 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4611 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4612 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4613 | |
| 4614 | err = bdi_init(&cgroup_backing_dev_info); |
| 4615 | if (err) |
| 4616 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4617 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4618 | cgroup_init_cftypes(NULL, cgroup_base_files); |
| 4619 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4620 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4621 | if (!ss->early_init) |
| 4622 | cgroup_init_subsys(ss); |
Tejun Heo | de00ffa | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4623 | |
| 4624 | /* |
| 4625 | * cftype registration needs kmalloc and can't be done |
| 4626 | * during early_init. Register base cftypes separately. |
| 4627 | */ |
| 4628 | if (ss->base_cftypes) |
| 4629 | WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4630 | } |
| 4631 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4632 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4633 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4634 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4635 | /* Add init_css_set to the hash table */ |
| 4636 | key = css_set_hash(init_css_set.subsys); |
| 4637 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4638 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4639 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4640 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4641 | err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top, |
| 4642 | 0, 1, GFP_KERNEL); |
| 4643 | BUG_ON(err < 0); |
| 4644 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4645 | mutex_unlock(&cgroup_mutex); |
| 4646 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4647 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4648 | if (!cgroup_kobj) { |
| 4649 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4650 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | err = register_filesystem(&cgroup_fs_type); |
| 4654 | if (err < 0) { |
| 4655 | kobject_put(cgroup_kobj); |
| 4656 | goto out; |
| 4657 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4658 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4659 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4660 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4661 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4662 | if (err) |
| 4663 | bdi_destroy(&cgroup_backing_dev_info); |
| 4664 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4665 | return err; |
| 4666 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4667 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4668 | static int __init cgroup_wq_init(void) |
| 4669 | { |
| 4670 | /* |
| 4671 | * There isn't much point in executing destruction path in |
| 4672 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Hugh Dickins | ab3f5fa | 2014-02-06 15:56:01 -0800 | [diff] [blame] | 4673 | * |
| 4674 | * XXX: Must be ordered to make sure parent is offlined after |
| 4675 | * children. The ordering requirement is for memcg where a |
| 4676 | * parent's offline may wait for a child's leading to deadlock. In |
| 4677 | * the long term, this should be fixed from memcg side. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4678 | * |
| 4679 | * We would prefer to do this in cgroup_init() above, but that |
| 4680 | * is called before init_workqueues(): so leave this until after. |
| 4681 | */ |
Hugh Dickins | ab3f5fa | 2014-02-06 15:56:01 -0800 | [diff] [blame] | 4682 | cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4683 | BUG_ON(!cgroup_destroy_wq); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 4684 | |
| 4685 | /* |
| 4686 | * Used to destroy pidlists and separate to serve as flush domain. |
| 4687 | * Cap @max_active to 1 too. |
| 4688 | */ |
| 4689 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 4690 | 0, 1); |
| 4691 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 4692 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4693 | return 0; |
| 4694 | } |
| 4695 | core_initcall(cgroup_wq_init); |
| 4696 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4697 | /* |
| 4698 | * proc_cgroup_show() |
| 4699 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4700 | * - Used for /proc/<pid>/cgroup. |
| 4701 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4702 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4703 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4704 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4705 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4706 | * cgroup to top_cgroup. |
| 4707 | */ |
| 4708 | |
| 4709 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4710 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4711 | { |
| 4712 | struct pid *pid; |
| 4713 | struct task_struct *tsk; |
| 4714 | char *buf; |
| 4715 | int retval; |
| 4716 | struct cgroupfs_root *root; |
| 4717 | |
| 4718 | retval = -ENOMEM; |
| 4719 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4720 | if (!buf) |
| 4721 | goto out; |
| 4722 | |
| 4723 | retval = -ESRCH; |
| 4724 | pid = m->private; |
| 4725 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4726 | if (!tsk) |
| 4727 | goto out_free; |
| 4728 | |
| 4729 | retval = 0; |
| 4730 | |
| 4731 | mutex_lock(&cgroup_mutex); |
| 4732 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4733 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4734 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4735 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4736 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4737 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4738 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4739 | for_each_subsys(ss, ssid) |
| 4740 | if (root->subsys_mask & (1 << ssid)) |
| 4741 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4742 | if (strlen(root->name)) |
| 4743 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4744 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4745 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4746 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4747 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4748 | if (retval < 0) |
| 4749 | goto out_unlock; |
| 4750 | seq_puts(m, buf); |
| 4751 | seq_putc(m, '\n'); |
| 4752 | } |
| 4753 | |
| 4754 | out_unlock: |
| 4755 | mutex_unlock(&cgroup_mutex); |
| 4756 | put_task_struct(tsk); |
| 4757 | out_free: |
| 4758 | kfree(buf); |
| 4759 | out: |
| 4760 | return retval; |
| 4761 | } |
| 4762 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4763 | /* Display information about each subsystem and each hierarchy */ |
| 4764 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4765 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4766 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4767 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4768 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4769 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4770 | /* |
| 4771 | * ideally we don't want subsystems moving around while we do this. |
| 4772 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4773 | * subsys/hierarchy state. |
| 4774 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4775 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4776 | |
| 4777 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4778 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4779 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4780 | ss->root->number_of_cgroups, !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4781 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4782 | mutex_unlock(&cgroup_mutex); |
| 4783 | return 0; |
| 4784 | } |
| 4785 | |
| 4786 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4787 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4788 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4789 | } |
| 4790 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4791 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4792 | .open = cgroupstats_open, |
| 4793 | .read = seq_read, |
| 4794 | .llseek = seq_lseek, |
| 4795 | .release = single_release, |
| 4796 | }; |
| 4797 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4798 | /** |
| 4799 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4800 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4801 | * |
| 4802 | * Description: A task inherits its parent's cgroup at fork(). |
| 4803 | * |
| 4804 | * A pointer to the shared css_set was automatically copied in |
| 4805 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4806 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 4807 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 4808 | * have already changed current->cgroups, allowing the previously |
| 4809 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4810 | * |
| 4811 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 4812 | * task, and the passed argument 'child' points to the child task. |
| 4813 | */ |
| 4814 | void cgroup_fork(struct task_struct *child) |
| 4815 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4816 | task_lock(current); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4817 | get_css_set(task_css_set(current)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4818 | child->cgroups = current->cgroups; |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4819 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4820 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4821 | } |
| 4822 | |
| 4823 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4824 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4825 | * @child: the task in question |
| 4826 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4827 | * Adds the task to the list running through its css_set if necessary and |
| 4828 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4829 | * 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] | 4830 | * 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] | 4831 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4832 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4833 | void cgroup_post_fork(struct task_struct *child) |
| 4834 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4835 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4836 | int i; |
| 4837 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4838 | /* |
| 4839 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 4840 | * under the tasklist_lock and we read it here after we added the child |
| 4841 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 4842 | * yet in the tasklist when we walked through it from |
| 4843 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 4844 | * should be visible now due to the paired locking and barriers implied |
| 4845 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 4846 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 4847 | * lock on fork. |
| 4848 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4849 | if (use_task_css_set_links) { |
| 4850 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4851 | task_lock(child); |
| 4852 | if (list_empty(&child->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4853 | list_add(&child->cg_list, &task_css_set(child)->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4854 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4855 | write_unlock(&css_set_lock); |
| 4856 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4857 | |
| 4858 | /* |
| 4859 | * Call ss->fork(). This must happen after @child is linked on |
| 4860 | * css_set; otherwise, @child might change state between ->fork() |
| 4861 | * and addition to css_set. |
| 4862 | */ |
| 4863 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4864 | for_each_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4865 | if (ss->fork) |
| 4866 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4867 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4868 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4869 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4870 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4871 | * cgroup_exit - detach cgroup from exiting task |
| 4872 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4873 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4874 | * |
| 4875 | * Description: Detach cgroup from @tsk and release it. |
| 4876 | * |
| 4877 | * Note that cgroups marked notify_on_release force every task in |
| 4878 | * them to take the global cgroup_mutex mutex when exiting. |
| 4879 | * This could impact scaling on very large systems. Be reluctant to |
| 4880 | * use notify_on_release cgroups where very high task exit scaling |
| 4881 | * is required on large systems. |
| 4882 | * |
| 4883 | * the_top_cgroup_hack: |
| 4884 | * |
| 4885 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 4886 | * |
| 4887 | * We call cgroup_exit() while the task is still competent to |
| 4888 | * handle notify_on_release(), then leave the task attached to the |
| 4889 | * root cgroup in each hierarchy for the remainder of its exit. |
| 4890 | * |
| 4891 | * To do this properly, we would increment the reference count on |
| 4892 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 4893 | * code we would add a second cgroup function call, to drop that |
| 4894 | * reference. This would just create an unnecessary hot spot on |
| 4895 | * the top_cgroup reference count, to no avail. |
| 4896 | * |
| 4897 | * Normally, holding a reference to a cgroup without bumping its |
| 4898 | * count is unsafe. The cgroup could go away, or someone could |
| 4899 | * attach us to a different cgroup, decrementing the count on |
| 4900 | * the first cgroup that we never incremented. But in this case, |
| 4901 | * 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] | 4902 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 4903 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4904 | */ |
| 4905 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 4906 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4907 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4908 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4909 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4910 | |
| 4911 | /* |
| 4912 | * Unlink from the css_set task list if necessary. |
| 4913 | * Optimistically check cg_list before taking |
| 4914 | * css_set_lock |
| 4915 | */ |
| 4916 | if (!list_empty(&tsk->cg_list)) { |
| 4917 | write_lock(&css_set_lock); |
| 4918 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4919 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4920 | write_unlock(&css_set_lock); |
| 4921 | } |
| 4922 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4923 | /* Reassign the task to the init_css_set. */ |
| 4924 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4925 | cset = task_css_set(tsk); |
| 4926 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4927 | |
| 4928 | if (run_callbacks && need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4929 | /* see cgroup_post_fork() for details */ |
| 4930 | for_each_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4931 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4932 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 4933 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4934 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4935 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4936 | } |
| 4937 | } |
| 4938 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4939 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4940 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4941 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4942 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4943 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4944 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4945 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4946 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4947 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4948 | /* |
| 4949 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4950 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4951 | * it now |
| 4952 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4953 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4954 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4955 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4956 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4957 | list_empty(&cgrp->release_list)) { |
| 4958 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4959 | need_schedule_work = 1; |
| 4960 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4961 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4962 | if (need_schedule_work) |
| 4963 | schedule_work(&release_agent_work); |
| 4964 | } |
| 4965 | } |
| 4966 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4967 | /* |
| 4968 | * Notify userspace when a cgroup is released, by running the |
| 4969 | * configured release agent with the name of the cgroup (path |
| 4970 | * relative to the root of cgroup file system) as the argument. |
| 4971 | * |
| 4972 | * Most likely, this user command will try to rmdir this cgroup. |
| 4973 | * |
| 4974 | * This races with the possibility that some other task will be |
| 4975 | * attached to this cgroup before it is removed, or that some other |
| 4976 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 4977 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 4978 | * unused, and this cgroup will be reprieved from its death sentence, |
| 4979 | * to continue to serve a useful existence. Next time it's released, |
| 4980 | * we will get notified again, if it still has 'notify_on_release' set. |
| 4981 | * |
| 4982 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 4983 | * means only wait until the task is successfully execve()'d. The |
| 4984 | * separate release agent task is forked by call_usermodehelper(), |
| 4985 | * then control in this thread returns here, without waiting for the |
| 4986 | * release agent task. We don't bother to wait because the caller of |
| 4987 | * this routine has no use for the exit status of the release agent |
| 4988 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4989 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4990 | static void cgroup_release_agent(struct work_struct *work) |
| 4991 | { |
| 4992 | BUG_ON(work != &release_agent_work); |
| 4993 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4994 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4995 | while (!list_empty(&release_list)) { |
| 4996 | char *argv[3], *envp[3]; |
| 4997 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4998 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4999 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5000 | struct cgroup, |
| 5001 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5002 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5003 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5004 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5005 | if (!pathbuf) |
| 5006 | goto continue_free; |
| 5007 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5008 | goto continue_free; |
| 5009 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5010 | if (!agentbuf) |
| 5011 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5012 | |
| 5013 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5014 | argv[i++] = agentbuf; |
| 5015 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5016 | argv[i] = NULL; |
| 5017 | |
| 5018 | i = 0; |
| 5019 | /* minimal command environment */ |
| 5020 | envp[i++] = "HOME=/"; |
| 5021 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5022 | envp[i] = NULL; |
| 5023 | |
| 5024 | /* Drop the lock while we invoke the usermode helper, |
| 5025 | * since the exec could involve hitting disk and hence |
| 5026 | * be a slow process */ |
| 5027 | mutex_unlock(&cgroup_mutex); |
| 5028 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5029 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5030 | continue_free: |
| 5031 | kfree(pathbuf); |
| 5032 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5033 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5034 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5035 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5036 | mutex_unlock(&cgroup_mutex); |
| 5037 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5038 | |
| 5039 | static int __init cgroup_disable(char *str) |
| 5040 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5041 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5042 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5043 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5044 | |
| 5045 | while ((token = strsep(&str, ",")) != NULL) { |
| 5046 | if (!*token) |
| 5047 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5048 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5049 | for_each_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5050 | if (!strcmp(token, ss->name)) { |
| 5051 | ss->disabled = 1; |
| 5052 | printk(KERN_INFO "Disabling %s control group" |
| 5053 | " subsystem\n", ss->name); |
| 5054 | break; |
| 5055 | } |
| 5056 | } |
| 5057 | } |
| 5058 | return 1; |
| 5059 | } |
| 5060 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5061 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5062 | /** |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5063 | * 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] | 5064 | * @dentry: directory dentry of interest |
| 5065 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5066 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5067 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 5068 | * to get the corresponding css and return it. If such css doesn't exist |
| 5069 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5070 | */ |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5071 | struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry, |
| 5072 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5073 | { |
| 5074 | struct cgroup *cgrp; |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5075 | struct cgroup_subsys_state *css; |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5076 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5077 | /* is @dentry a cgroup dir? */ |
| 5078 | if (!dentry->d_inode || |
| 5079 | dentry->d_inode->i_op != &cgroup_dir_inode_operations) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5080 | return ERR_PTR(-EBADF); |
| 5081 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5082 | rcu_read_lock(); |
| 5083 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5084 | cgrp = __d_cgrp(dentry); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5085 | css = cgroup_css(cgrp, ss); |
| 5086 | |
| 5087 | if (!css || !css_tryget(css)) |
| 5088 | css = ERR_PTR(-ENOENT); |
| 5089 | |
| 5090 | rcu_read_unlock(); |
| 5091 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5092 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5093 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5094 | /** |
| 5095 | * css_from_id - lookup css by id |
| 5096 | * @id: the cgroup id |
| 5097 | * @ss: cgroup subsys to be looked into |
| 5098 | * |
| 5099 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5100 | * Should be called under rcu_read_lock(). |
| 5101 | */ |
| 5102 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5103 | { |
| 5104 | struct cgroup *cgrp; |
| 5105 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5106 | cgroup_assert_mutexes_or_rcu_locked(); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5107 | |
| 5108 | cgrp = idr_find(&ss->root->cgroup_idr, id); |
| 5109 | if (cgrp) |
Tejun Heo | d162596 | 2013-08-27 14:27:23 -0400 | [diff] [blame] | 5110 | return cgroup_css(cgrp, ss); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5111 | return NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5112 | } |
| 5113 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5114 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5115 | static struct cgroup_subsys_state * |
| 5116 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5117 | { |
| 5118 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5119 | |
| 5120 | if (!css) |
| 5121 | return ERR_PTR(-ENOMEM); |
| 5122 | |
| 5123 | return css; |
| 5124 | } |
| 5125 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5126 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5127 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5128 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5129 | } |
| 5130 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5131 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 5132 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5133 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5134 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5135 | } |
| 5136 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5137 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 5138 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5139 | { |
| 5140 | return (u64)(unsigned long)current->cgroups; |
| 5141 | } |
| 5142 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5143 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5144 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5145 | { |
| 5146 | u64 count; |
| 5147 | |
| 5148 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5149 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5150 | rcu_read_unlock(); |
| 5151 | return count; |
| 5152 | } |
| 5153 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5154 | 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] | 5155 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5156 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5157 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5158 | |
| 5159 | read_lock(&css_set_lock); |
| 5160 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5161 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5162 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5163 | struct cgroup *c = link->cgrp; |
| 5164 | const char *name; |
| 5165 | |
| 5166 | if (c->dentry) |
| 5167 | name = c->dentry->d_name.name; |
| 5168 | else |
| 5169 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5170 | seq_printf(seq, "Root %d group %s\n", |
| 5171 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5172 | } |
| 5173 | rcu_read_unlock(); |
| 5174 | read_unlock(&css_set_lock); |
| 5175 | return 0; |
| 5176 | } |
| 5177 | |
| 5178 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5179 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5180 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5181 | struct cgroup_subsys_state *css = seq_css(seq); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5182 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5183 | |
| 5184 | read_lock(&css_set_lock); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5185 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5186 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5187 | struct task_struct *task; |
| 5188 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5189 | seq_printf(seq, "css_set %p\n", cset); |
| 5190 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5191 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5192 | seq_puts(seq, " ...\n"); |
| 5193 | break; |
| 5194 | } else { |
| 5195 | seq_printf(seq, " task %d\n", |
| 5196 | task_pid_vnr(task)); |
| 5197 | } |
| 5198 | } |
| 5199 | } |
| 5200 | read_unlock(&css_set_lock); |
| 5201 | return 0; |
| 5202 | } |
| 5203 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5204 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5205 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5206 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5207 | } |
| 5208 | |
| 5209 | static struct cftype debug_files[] = { |
| 5210 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5211 | .name = "taskcount", |
| 5212 | .read_u64 = debug_taskcount_read, |
| 5213 | }, |
| 5214 | |
| 5215 | { |
| 5216 | .name = "current_css_set", |
| 5217 | .read_u64 = current_css_set_read, |
| 5218 | }, |
| 5219 | |
| 5220 | { |
| 5221 | .name = "current_css_set_refcount", |
| 5222 | .read_u64 = current_css_set_refcount_read, |
| 5223 | }, |
| 5224 | |
| 5225 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5226 | .name = "current_css_set_cg_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5227 | .seq_show = current_css_set_cg_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5228 | }, |
| 5229 | |
| 5230 | { |
| 5231 | .name = "cgroup_css_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5232 | .seq_show = cgroup_css_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5233 | }, |
| 5234 | |
| 5235 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5236 | .name = "releasable", |
| 5237 | .read_u64 = releasable_read, |
| 5238 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5239 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5240 | { } /* terminate */ |
| 5241 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5242 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5243 | struct cgroup_subsys debug_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5244 | .css_alloc = debug_css_alloc, |
| 5245 | .css_free = debug_css_free, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5246 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5247 | }; |
| 5248 | #endif /* CONFIG_CGROUP_DEBUG */ |