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 | |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 30 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 31 | #include <linux/cgroup.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 32 | #include <linux/cred.h> |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 33 | #include <linux/ctype.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 34 | #include <linux/errno.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 35 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 36 | #include <linux/kernel.h> |
| 37 | #include <linux/list.h> |
| 38 | #include <linux/mm.h> |
| 39 | #include <linux/mutex.h> |
| 40 | #include <linux/mount.h> |
| 41 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 42 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 43 | #include <linux/rcupdate.h> |
| 44 | #include <linux/sched.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 45 | #include <linux/slab.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 46 | #include <linux/spinlock.h> |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 47 | #include <linux/rwsem.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 48 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 49 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 50 | #include <linux/kmod.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 51 | #include <linux/delayacct.h> |
| 52 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 53 | #include <linux/hashtable.h> |
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 */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 57 | #include <linux/kthread.h> |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 58 | #include <linux/delay.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 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 82 | /* |
| 83 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 84 | * hierarchy must be performed while holding it. |
| 85 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 86 | * css_set_rwsem protects task->cgroups pointer, the list of css_set |
| 87 | * objects, and the chain of tasks off each css_set. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 88 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 89 | * These locks are exported if CONFIG_PROVE_RCU so that accessors in |
| 90 | * cgroup.h can use them for lockdep annotations. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 91 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 92 | #ifdef CONFIG_PROVE_RCU |
| 93 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 94 | DECLARE_RWSEM(css_set_rwsem); |
| 95 | EXPORT_SYMBOL_GPL(cgroup_mutex); |
| 96 | EXPORT_SYMBOL_GPL(css_set_rwsem); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 97 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 98 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 99 | static DECLARE_RWSEM(css_set_rwsem); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 100 | #endif |
| 101 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 102 | /* |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 103 | * Protects cgroup_idr and css_idr so that IDs can be released without |
| 104 | * grabbing cgroup_mutex. |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 105 | */ |
| 106 | static DEFINE_SPINLOCK(cgroup_idr_lock); |
| 107 | |
| 108 | /* |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 109 | * Protects cgroup_subsys->release_agent_path. Modifying it also requires |
| 110 | * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock. |
| 111 | */ |
| 112 | static DEFINE_SPINLOCK(release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 113 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 114 | #define cgroup_assert_mutexes_or_rcu_locked() \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 115 | rcu_lockdep_assert(rcu_read_lock_held() || \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 116 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 117 | lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 118 | "cgroup_[tree_]mutex or RCU read lock required"); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 119 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 120 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 121 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 122 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 123 | * destruction work items don't end up filling up max_active of system_wq |
| 124 | * which may lead to deadlock. |
| 125 | */ |
| 126 | static struct workqueue_struct *cgroup_destroy_wq; |
| 127 | |
| 128 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 129 | * pidlist destructions need to be flushed on cgroup destruction. Use a |
| 130 | * separate workqueue as flush domain. |
| 131 | */ |
| 132 | static struct workqueue_struct *cgroup_pidlist_destroy_wq; |
| 133 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 134 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 135 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 136 | static struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 137 | #include <linux/cgroup_subsys.h> |
| 138 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 139 | #undef SUBSYS |
| 140 | |
| 141 | /* array of cgroup subsystem names */ |
| 142 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 143 | static const char *cgroup_subsys_name[] = { |
| 144 | #include <linux/cgroup_subsys.h> |
| 145 | }; |
| 146 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 147 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 148 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 149 | * The default hierarchy, reserved for the subsystems that are otherwise |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 150 | * unattached - it never has more than a single cgroup, and all tasks are |
| 151 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 152 | */ |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 153 | struct cgroup_root cgrp_dfl_root; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 154 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 155 | /* |
| 156 | * The default hierarchy always exists but is hidden until mounted for the |
| 157 | * first time. This is for backward compatibility. |
| 158 | */ |
| 159 | static bool cgrp_dfl_root_visible; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 160 | |
| 161 | /* The list of hierarchy roots */ |
| 162 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 163 | static LIST_HEAD(cgroup_roots); |
| 164 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 165 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 166 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 167 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 168 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 169 | /* |
| 170 | * Assign a monotonically increasing serial number to cgroups. It |
| 171 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 172 | * numbers. Also, as cgroups are always appended to the parent's |
| 173 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 174 | * the ascending serial number order on the list. Protected by |
| 175 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 176 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 177 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 178 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 179 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 180 | * check for fork/exit handlers to call. This avoids us having to do |
| 181 | * extra work in the fork/exit path if none of the subsystems need to |
| 182 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 183 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 184 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 185 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 186 | static struct cftype cgroup_base_files[]; |
| 187 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 188 | static void cgroup_put(struct cgroup *cgrp); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 189 | static int rebind_subsystems(struct cgroup_root *dst_root, |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 190 | unsigned int ss_mask); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 191 | static void cgroup_destroy_css_killed(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 192 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 193 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss); |
| 194 | static void kill_css(struct cgroup_subsys_state *css); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 195 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 196 | bool is_add); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 197 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 198 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 199 | /* IDR wrappers which synchronize using cgroup_idr_lock */ |
| 200 | static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end, |
| 201 | gfp_t gfp_mask) |
| 202 | { |
| 203 | int ret; |
| 204 | |
| 205 | idr_preload(gfp_mask); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 206 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 207 | ret = idr_alloc(idr, ptr, start, end, gfp_mask); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 208 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 209 | idr_preload_end(); |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id) |
| 214 | { |
| 215 | void *ret; |
| 216 | |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 217 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 218 | ret = idr_replace(idr, ptr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 219 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | static void cgroup_idr_remove(struct idr *idr, int id) |
| 224 | { |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 225 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 226 | idr_remove(idr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 227 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 230 | /** |
| 231 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 232 | * @cgrp: the cgroup of interest |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 233 | * @ss: the subsystem of interest (%NULL returns the dummy_css) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 234 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 235 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 236 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 237 | * the caller is responsible for pinning the returned css if it wants to |
| 238 | * keep accessing it outside the said locks. This function may return |
| 239 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 240 | */ |
| 241 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 242 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 243 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 244 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 245 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 246 | lockdep_is_held(&cgroup_tree_mutex) || |
| 247 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 248 | else |
| 249 | return &cgrp->dummy_css; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 250 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 251 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 252 | /** |
| 253 | * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem |
| 254 | * @cgrp: the cgroup of interest |
| 255 | * @ss: the subsystem of interest (%NULL returns the dummy_css) |
| 256 | * |
| 257 | * Similar to cgroup_css() but returns the effctive css, which is defined |
| 258 | * as the matching css of the nearest ancestor including self which has @ss |
| 259 | * enabled. If @ss is associated with the hierarchy @cgrp is on, this |
| 260 | * function is guaranteed to return non-NULL css. |
| 261 | */ |
| 262 | static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp, |
| 263 | struct cgroup_subsys *ss) |
| 264 | { |
| 265 | lockdep_assert_held(&cgroup_mutex); |
| 266 | |
| 267 | if (!ss) |
| 268 | return &cgrp->dummy_css; |
| 269 | |
| 270 | if (!(cgrp->root->subsys_mask & (1 << ss->id))) |
| 271 | return NULL; |
| 272 | |
| 273 | while (cgrp->parent && |
| 274 | !(cgrp->parent->child_subsys_mask & (1 << ss->id))) |
| 275 | cgrp = cgrp->parent; |
| 276 | |
| 277 | return cgroup_css(cgrp, ss); |
| 278 | } |
| 279 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 280 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 281 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 282 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 283 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 286 | struct cgroup_subsys_state *seq_css(struct seq_file *seq) |
| 287 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 288 | struct kernfs_open_file *of = seq->private; |
| 289 | struct cgroup *cgrp = of->kn->parent->priv; |
| 290 | struct cftype *cft = seq_cft(seq); |
| 291 | |
| 292 | /* |
| 293 | * This is open and unprotected implementation of cgroup_css(). |
| 294 | * seq_css() is only called from a kernfs file operation which has |
| 295 | * an active reference on the file. Because all the subsystem |
| 296 | * files are drained before a css is disassociated with a cgroup, |
| 297 | * the matching css from the cgroup's subsys table is guaranteed to |
| 298 | * be and stay valid until the enclosing operation is complete. |
| 299 | */ |
| 300 | if (cft->ss) |
| 301 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 302 | else |
| 303 | return &cgrp->dummy_css; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 304 | } |
| 305 | EXPORT_SYMBOL_GPL(seq_css); |
| 306 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 307 | /** |
| 308 | * cgroup_is_descendant - test ancestry |
| 309 | * @cgrp: the cgroup to be tested |
| 310 | * @ancestor: possible ancestor of @cgrp |
| 311 | * |
| 312 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 313 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 314 | * and @ancestor are accessible. |
| 315 | */ |
| 316 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 317 | { |
| 318 | while (cgrp) { |
| 319 | if (cgrp == ancestor) |
| 320 | return true; |
| 321 | cgrp = cgrp->parent; |
| 322 | } |
| 323 | return false; |
| 324 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 325 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 326 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 327 | { |
| 328 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 329 | (1 << CGRP_RELEASABLE) | |
| 330 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 331 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 334 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 335 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 336 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 339 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 340 | * for_each_css - iterate all css's of a cgroup |
| 341 | * @css: the iteration cursor |
| 342 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 343 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 344 | * |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 345 | * Should be called under cgroup_[tree_]mutex. |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 346 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 347 | #define for_each_css(css, ssid, cgrp) \ |
| 348 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 349 | if (!((css) = rcu_dereference_check( \ |
| 350 | (cgrp)->subsys[(ssid)], \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 351 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 352 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 353 | else |
| 354 | |
| 355 | /** |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 356 | * for_each_e_css - iterate all effective css's of a cgroup |
| 357 | * @css: the iteration cursor |
| 358 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 359 | * @cgrp: the target cgroup to iterate css's of |
| 360 | * |
| 361 | * Should be called under cgroup_[tree_]mutex. |
| 362 | */ |
| 363 | #define for_each_e_css(css, ssid, cgrp) \ |
| 364 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 365 | if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \ |
| 366 | ; \ |
| 367 | else |
| 368 | |
| 369 | /** |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 370 | * for_each_subsys - iterate all enabled cgroup subsystems |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 371 | * @ss: the iteration cursor |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 372 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 373 | */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 374 | #define for_each_subsys(ss, ssid) \ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 375 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \ |
| 376 | (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 377 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 378 | /* iterate across the hierarchies */ |
| 379 | #define for_each_root(root) \ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 380 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 381 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 382 | /* iterate over child cgrps, lock should be held throughout iteration */ |
| 383 | #define cgroup_for_each_live_child(child, cgrp) \ |
| 384 | list_for_each_entry((child), &(cgrp)->children, sibling) \ |
| 385 | if (({ lockdep_assert_held(&cgroup_tree_mutex); \ |
| 386 | cgroup_is_dead(child); })) \ |
| 387 | ; \ |
| 388 | else |
| 389 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 390 | /** |
| 391 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 392 | * @cgrp: the cgroup to be checked for liveness |
| 393 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 394 | * On success, returns true; the mutex should be later unlocked. On |
| 395 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 396 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 397 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 398 | { |
| 399 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 400 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 401 | mutex_unlock(&cgroup_mutex); |
| 402 | return false; |
| 403 | } |
| 404 | return true; |
| 405 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 406 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 407 | /* the list of cgroups eligible for automatic release. Protected by |
| 408 | * release_list_lock */ |
| 409 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 410 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 411 | static void cgroup_release_agent(struct work_struct *work); |
| 412 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 413 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 414 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 415 | /* |
| 416 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 417 | * belong to different cgroups on different hierarchies. In the other |
| 418 | * direction, a css_set is naturally associated with multiple cgroups. |
| 419 | * This M:N relationship is represented by the following link structure |
| 420 | * which exists for each association and allows traversing the associations |
| 421 | * from both sides. |
| 422 | */ |
| 423 | struct cgrp_cset_link { |
| 424 | /* the cgroup and css_set this link associates */ |
| 425 | struct cgroup *cgrp; |
| 426 | struct css_set *cset; |
| 427 | |
| 428 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 429 | struct list_head cset_link; |
| 430 | |
| 431 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 432 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 433 | }; |
| 434 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 435 | /* |
| 436 | * The default css_set - used by init and its children prior to any |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 437 | * hierarchies being mounted. It contains a pointer to the root state |
| 438 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 439 | * reference-counted, to improve performance when child cgroups |
| 440 | * haven't been created. |
| 441 | */ |
Tejun Heo | 5024ae2 | 2014-05-07 21:31:17 -0400 | [diff] [blame] | 442 | struct css_set init_css_set = { |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 443 | .refcount = ATOMIC_INIT(1), |
| 444 | .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), |
| 445 | .tasks = LIST_HEAD_INIT(init_css_set.tasks), |
| 446 | .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), |
| 447 | .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node), |
| 448 | .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), |
| 449 | }; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 450 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 451 | static int css_set_count = 1; /* 1 for init_css_set */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 452 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 453 | /** |
| 454 | * cgroup_update_populated - updated populated count of a cgroup |
| 455 | * @cgrp: the target cgroup |
| 456 | * @populated: inc or dec populated count |
| 457 | * |
| 458 | * @cgrp is either getting the first task (css_set) or losing the last. |
| 459 | * Update @cgrp->populated_cnt accordingly. The count is propagated |
| 460 | * towards root so that a given cgroup's populated_cnt is zero iff the |
| 461 | * cgroup and all its descendants are empty. |
| 462 | * |
| 463 | * @cgrp's interface file "cgroup.populated" is zero if |
| 464 | * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt |
| 465 | * changes from or to zero, userland is notified that the content of the |
| 466 | * interface file has changed. This can be used to detect when @cgrp and |
| 467 | * its descendants become populated or empty. |
| 468 | */ |
| 469 | static void cgroup_update_populated(struct cgroup *cgrp, bool populated) |
| 470 | { |
| 471 | lockdep_assert_held(&css_set_rwsem); |
| 472 | |
| 473 | do { |
| 474 | bool trigger; |
| 475 | |
| 476 | if (populated) |
| 477 | trigger = !cgrp->populated_cnt++; |
| 478 | else |
| 479 | trigger = !--cgrp->populated_cnt; |
| 480 | |
| 481 | if (!trigger) |
| 482 | break; |
| 483 | |
| 484 | if (cgrp->populated_kn) |
| 485 | kernfs_notify(cgrp->populated_kn); |
| 486 | cgrp = cgrp->parent; |
| 487 | } while (cgrp); |
| 488 | } |
| 489 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 490 | /* |
| 491 | * hash table for cgroup groups. This improves the performance to find |
| 492 | * an existing css_set. This hash doesn't (currently) take into |
| 493 | * account cgroups in empty hierarchies. |
| 494 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 495 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 496 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 497 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 498 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 499 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 500 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 501 | struct cgroup_subsys *ss; |
| 502 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 503 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 504 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 505 | key += (unsigned long)css[i]; |
| 506 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 507 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 508 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 511 | static void put_css_set_locked(struct css_set *cset, bool taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 512 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 513 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 514 | struct cgroup_subsys *ss; |
| 515 | int ssid; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 516 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 517 | lockdep_assert_held(&css_set_rwsem); |
| 518 | |
| 519 | if (!atomic_dec_and_test(&cset->refcount)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 520 | return; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 521 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 522 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 523 | for_each_subsys(ss, ssid) |
| 524 | list_del(&cset->e_cset_node[ssid]); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 525 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 526 | css_set_count--; |
| 527 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 528 | 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] | 529 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 530 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 531 | list_del(&link->cset_link); |
| 532 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 533 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 534 | /* @cgrp can't go away while we're holding css_set_rwsem */ |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 535 | if (list_empty(&cgrp->cset_links)) { |
| 536 | cgroup_update_populated(cgrp, false); |
| 537 | if (notify_on_release(cgrp)) { |
| 538 | if (taskexit) |
| 539 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 540 | check_for_release(cgrp); |
| 541 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 542 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 543 | |
| 544 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 545 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 546 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 547 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 550 | static void put_css_set(struct css_set *cset, bool taskexit) |
| 551 | { |
| 552 | /* |
| 553 | * Ensure that the refcount doesn't hit zero while any readers |
| 554 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 555 | * rwlock |
| 556 | */ |
| 557 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
| 558 | return; |
| 559 | |
| 560 | down_write(&css_set_rwsem); |
| 561 | put_css_set_locked(cset, taskexit); |
| 562 | up_write(&css_set_rwsem); |
| 563 | } |
| 564 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 565 | /* |
| 566 | * refcounted get/put for css_set objects |
| 567 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 568 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 569 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 570 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 573 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 574 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 575 | * @cset: candidate css_set being tested |
| 576 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 577 | * @new_cgrp: cgroup that's being entered by the task |
| 578 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 579 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 580 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 581 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 582 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 583 | static bool compare_css_sets(struct css_set *cset, |
| 584 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 585 | struct cgroup *new_cgrp, |
| 586 | struct cgroup_subsys_state *template[]) |
| 587 | { |
| 588 | struct list_head *l1, *l2; |
| 589 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 590 | /* |
| 591 | * On the default hierarchy, there can be csets which are |
| 592 | * associated with the same set of cgroups but different csses. |
| 593 | * Let's first ensure that csses match. |
| 594 | */ |
| 595 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 596 | return false; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 597 | |
| 598 | /* |
| 599 | * Compare cgroup pointers in order to distinguish between |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 600 | * different cgroups in hierarchies. As different cgroups may |
| 601 | * share the same effective css, this comparison is always |
| 602 | * necessary. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 603 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 604 | l1 = &cset->cgrp_links; |
| 605 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 606 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 607 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 608 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 609 | |
| 610 | l1 = l1->next; |
| 611 | l2 = l2->next; |
| 612 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 613 | if (l1 == &cset->cgrp_links) { |
| 614 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 615 | break; |
| 616 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 617 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 618 | } |
| 619 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 620 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 621 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 622 | cgrp1 = link1->cgrp; |
| 623 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 624 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 625 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 626 | |
| 627 | /* |
| 628 | * If this hierarchy is the hierarchy of the cgroup |
| 629 | * that's changing, then we need to check that this |
| 630 | * css_set points to the new cgroup; if it's any other |
| 631 | * hierarchy, then this css_set should point to the |
| 632 | * same cgroup as the old css_set. |
| 633 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 634 | if (cgrp1->root == new_cgrp->root) { |
| 635 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 636 | return false; |
| 637 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 638 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 639 | return false; |
| 640 | } |
| 641 | } |
| 642 | return true; |
| 643 | } |
| 644 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 645 | /** |
| 646 | * find_existing_css_set - init css array and find the matching css_set |
| 647 | * @old_cset: the css_set that we're using before the cgroup transition |
| 648 | * @cgrp: the cgroup that we're moving into |
| 649 | * @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] | 650 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 651 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 652 | struct cgroup *cgrp, |
| 653 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 654 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 655 | struct cgroup_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 656 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 657 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 658 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 659 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 660 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 661 | /* |
| 662 | * Build the set of subsystem state objects that we want to see in the |
| 663 | * new css_set. while subsystems can change globally, the entries here |
| 664 | * won't change, so no need for locking. |
| 665 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 666 | for_each_subsys(ss, i) { |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 667 | if (root->subsys_mask & (1UL << i)) { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 668 | /* |
| 669 | * @ss is in this hierarchy, so we want the |
| 670 | * effective css from @cgrp. |
| 671 | */ |
| 672 | template[i] = cgroup_e_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 673 | } else { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 674 | /* |
| 675 | * @ss is not in this hierarchy, so we don't want |
| 676 | * to change the css. |
| 677 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 678 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 682 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 683 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 684 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 685 | continue; |
| 686 | |
| 687 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 688 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 689 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 690 | |
| 691 | /* No existing cgroup group matched */ |
| 692 | return NULL; |
| 693 | } |
| 694 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 695 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 696 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 697 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 698 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 699 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 700 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 701 | kfree(link); |
| 702 | } |
| 703 | } |
| 704 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 705 | /** |
| 706 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 707 | * @count: the number of links to allocate |
| 708 | * @tmp_links: list_head the allocated links are put on |
| 709 | * |
| 710 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 711 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 712 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 713 | 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] | 714 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 715 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 716 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 717 | |
| 718 | INIT_LIST_HEAD(tmp_links); |
| 719 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 720 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 721 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 722 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 723 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 724 | return -ENOMEM; |
| 725 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 726 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 727 | } |
| 728 | return 0; |
| 729 | } |
| 730 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 731 | /** |
| 732 | * 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] | 733 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 734 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 735 | * @cgrp: the destination cgroup |
| 736 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 737 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 738 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 739 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 740 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 741 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 742 | BUG_ON(list_empty(tmp_links)); |
Tejun Heo | 6803c00 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 743 | |
| 744 | if (cgroup_on_dfl(cgrp)) |
| 745 | cset->dfl_cgrp = cgrp; |
| 746 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 747 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 748 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 749 | link->cgrp = cgrp; |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 750 | |
| 751 | if (list_empty(&cgrp->cset_links)) |
| 752 | cgroup_update_populated(cgrp, true); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 753 | list_move(&link->cset_link, &cgrp->cset_links); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 754 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 755 | /* |
| 756 | * Always add links to the tail of the list so that the list |
| 757 | * is sorted by order of hierarchy creation |
| 758 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 759 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 760 | } |
| 761 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 762 | /** |
| 763 | * find_css_set - return a new css_set with one cgroup updated |
| 764 | * @old_cset: the baseline css_set |
| 765 | * @cgrp: the cgroup to be updated |
| 766 | * |
| 767 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 768 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 769 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 770 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 771 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 772 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 773 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 774 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 775 | struct list_head tmp_links; |
| 776 | struct cgrp_cset_link *link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 777 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 778 | unsigned long key; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 779 | int ssid; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 780 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 781 | lockdep_assert_held(&cgroup_mutex); |
| 782 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 783 | /* First see if we already have a cgroup group that matches |
| 784 | * the desired set */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 785 | down_read(&css_set_rwsem); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 786 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 787 | if (cset) |
| 788 | get_css_set(cset); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 789 | up_read(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 790 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 791 | if (cset) |
| 792 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 793 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 794 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 795 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 796 | return NULL; |
| 797 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 798 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 799 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 800 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 801 | return NULL; |
| 802 | } |
| 803 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 804 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 805 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 806 | INIT_LIST_HEAD(&cset->tasks); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 807 | INIT_LIST_HEAD(&cset->mg_tasks); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 808 | INIT_LIST_HEAD(&cset->mg_preload_node); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 809 | INIT_LIST_HEAD(&cset->mg_node); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 810 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 811 | |
| 812 | /* Copy the set of subsystem state objects generated in |
| 813 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 814 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 815 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 816 | down_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 817 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 818 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 819 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 820 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 821 | if (c->root == cgrp->root) |
| 822 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 823 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 824 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 825 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 826 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 827 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 828 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 829 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 830 | /* Add @cset to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 831 | key = css_set_hash(cset->subsys); |
| 832 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 833 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 834 | for_each_subsys(ss, ssid) |
| 835 | list_add_tail(&cset->e_cset_node[ssid], |
| 836 | &cset->subsys[ssid]->cgroup->e_csets[ssid]); |
| 837 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 838 | up_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 839 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 840 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 843 | static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 844 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 845 | struct cgroup *root_cgrp = kf_root->kn->priv; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 846 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 847 | return root_cgrp->root; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 848 | } |
| 849 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 850 | static int cgroup_init_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 851 | { |
| 852 | int id; |
| 853 | |
| 854 | lockdep_assert_held(&cgroup_mutex); |
| 855 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 856 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 857 | if (id < 0) |
| 858 | return id; |
| 859 | |
| 860 | root->hierarchy_id = id; |
| 861 | return 0; |
| 862 | } |
| 863 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 864 | static void cgroup_exit_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 865 | { |
| 866 | lockdep_assert_held(&cgroup_mutex); |
| 867 | |
| 868 | if (root->hierarchy_id) { |
| 869 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
| 870 | root->hierarchy_id = 0; |
| 871 | } |
| 872 | } |
| 873 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 874 | static void cgroup_free_root(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 875 | { |
| 876 | if (root) { |
| 877 | /* hierarhcy ID shoulid already have been released */ |
| 878 | WARN_ON_ONCE(root->hierarchy_id); |
| 879 | |
| 880 | idr_destroy(&root->cgroup_idr); |
| 881 | kfree(root); |
| 882 | } |
| 883 | } |
| 884 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 885 | static void cgroup_destroy_root(struct cgroup_root *root) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 886 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 887 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 888 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 889 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 890 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 891 | mutex_lock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 892 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 893 | BUG_ON(atomic_read(&root->nr_cgrps)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 894 | BUG_ON(!list_empty(&cgrp->children)); |
| 895 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 896 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 897 | rebind_subsystems(&cgrp_dfl_root, root->subsys_mask); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 898 | |
| 899 | /* |
| 900 | * Release all the links from cset_links to this hierarchy's |
| 901 | * root cgroup |
| 902 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 903 | down_write(&css_set_rwsem); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 904 | |
| 905 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 906 | list_del(&link->cset_link); |
| 907 | list_del(&link->cgrp_link); |
| 908 | kfree(link); |
| 909 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 910 | up_write(&css_set_rwsem); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 911 | |
| 912 | if (!list_empty(&root->root_list)) { |
| 913 | list_del(&root->root_list); |
| 914 | cgroup_root_count--; |
| 915 | } |
| 916 | |
| 917 | cgroup_exit_root_id(root); |
| 918 | |
| 919 | mutex_unlock(&cgroup_mutex); |
| 920 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 921 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 922 | kernfs_destroy_root(root->kf_root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 923 | cgroup_free_root(root); |
| 924 | } |
| 925 | |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 926 | /* look up cgroup associated with given css_set on the specified hierarchy */ |
| 927 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 928 | struct cgroup_root *root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 929 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 930 | struct cgroup *res = NULL; |
| 931 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 932 | lockdep_assert_held(&cgroup_mutex); |
| 933 | lockdep_assert_held(&css_set_rwsem); |
| 934 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 935 | if (cset == &init_css_set) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 936 | res = &root->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 937 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 938 | struct cgrp_cset_link *link; |
| 939 | |
| 940 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 941 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 942 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 943 | if (c->root == root) { |
| 944 | res = c; |
| 945 | break; |
| 946 | } |
| 947 | } |
| 948 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 949 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 950 | BUG_ON(!res); |
| 951 | return res; |
| 952 | } |
| 953 | |
| 954 | /* |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 955 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 956 | * called with cgroup_mutex and css_set_rwsem held. |
| 957 | */ |
| 958 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 959 | struct cgroup_root *root) |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 960 | { |
| 961 | /* |
| 962 | * No need to lock the task - since we hold cgroup_mutex the |
| 963 | * task can't change groups, so the only thing that can happen |
| 964 | * is that it exits and its css is set back to init_css_set. |
| 965 | */ |
| 966 | return cset_cgroup_from_root(task_css_set(task), root); |
| 967 | } |
| 968 | |
| 969 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 970 | * A task must hold cgroup_mutex to modify cgroups. |
| 971 | * |
| 972 | * Any task can increment and decrement the count field without lock. |
| 973 | * So in general, code holding cgroup_mutex can't rely on the count |
| 974 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 975 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 976 | * means that no tasks are currently attached, therefore there is no |
| 977 | * way a task attached to that cgroup can fork (the other way to |
| 978 | * increment the count). So code holding cgroup_mutex can safely |
| 979 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 980 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 981 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 982 | * needs that mutex. |
| 983 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 984 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 985 | * (usually) take cgroup_mutex. These are the two most performance |
| 986 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 987 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 988 | * 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] | 989 | * to the release agent with the name of the cgroup (path relative to |
| 990 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 991 | * |
| 992 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 993 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 994 | * tasks in the system use _some_ cgroup, and since there is always at |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 995 | * least one task in the system (init, pid == 1), therefore, root cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 996 | * always has either children cgroups and/or using tasks. So we don't |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 997 | * need a special hack to ensure that root cgroup cannot be deleted. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 998 | * |
| 999 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1000 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1001 | */ |
| 1002 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1003 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1004 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1005 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 1006 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1007 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 1008 | char *buf) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1009 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1010 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 1011 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) |
| 1012 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s", |
| 1013 | cft->ss->name, cft->name); |
| 1014 | else |
| 1015 | strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
| 1016 | return buf; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1019 | /** |
| 1020 | * cgroup_file_mode - deduce file mode of a control file |
| 1021 | * @cft: the control file in question |
| 1022 | * |
| 1023 | * returns cft->mode if ->mode is not 0 |
| 1024 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 1025 | * returns S_IRUGO if it has only a read handler |
| 1026 | * returns S_IWUSR if it has only a write hander |
| 1027 | */ |
| 1028 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1029 | { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1030 | umode_t mode = 0; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1031 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1032 | if (cft->mode) |
| 1033 | return cft->mode; |
| 1034 | |
| 1035 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 1036 | mode |= S_IRUGO; |
| 1037 | |
| 1038 | if (cft->write_u64 || cft->write_s64 || cft->write_string || |
| 1039 | cft->trigger) |
| 1040 | mode |= S_IWUSR; |
| 1041 | |
| 1042 | return mode; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1043 | } |
| 1044 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1045 | static void cgroup_free_fn(struct work_struct *work) |
| 1046 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 1047 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1048 | |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1049 | atomic_dec(&cgrp->root->nr_cgrps); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 1050 | cgroup_pidlist_destroy_all(cgrp); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1051 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1052 | if (cgrp->parent) { |
| 1053 | /* |
| 1054 | * We get a ref to the parent, and put the ref when this |
| 1055 | * cgroup is being freed, so it's guaranteed that the |
| 1056 | * parent won't be destroyed before its children. |
| 1057 | */ |
| 1058 | cgroup_put(cgrp->parent); |
| 1059 | kernfs_put(cgrp->kn); |
| 1060 | kfree(cgrp); |
| 1061 | } else { |
| 1062 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1063 | * This is root cgroup's refcnt reaching zero, which |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1064 | * indicates that the root should be released. |
| 1065 | */ |
| 1066 | cgroup_destroy_root(cgrp->root); |
| 1067 | } |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static void cgroup_free_rcu(struct rcu_head *head) |
| 1071 | { |
| 1072 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 1073 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 1074 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 1075 | queue_work(cgroup_destroy_wq, &cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1076 | } |
| 1077 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1078 | static void cgroup_get(struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1079 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1080 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
| 1081 | WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0); |
| 1082 | atomic_inc(&cgrp->refcnt); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1085 | static void cgroup_put(struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1086 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1087 | if (!atomic_dec_and_test(&cgrp->refcnt)) |
| 1088 | return; |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1089 | if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp))) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1090 | return; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1091 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1092 | cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1093 | cgrp->id = -1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1094 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1095 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 1098 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1099 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1100 | char name[CGROUP_FILE_NAME_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1101 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1102 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1103 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1106 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 1107 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 1108 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1109 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 1110 | */ |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1111 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1112 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1113 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1114 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1115 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1116 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 1117 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1118 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1119 | if (!(subsys_mask & (1 << i))) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1120 | continue; |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 1121 | list_for_each_entry(cfts, &ss->cfts, node) |
| 1122 | cgroup_addrm_files(cgrp, cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1123 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1126 | static int rebind_subsystems(struct cgroup_root *dst_root, unsigned int ss_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1127 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1128 | struct cgroup_subsys *ss; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1129 | int ssid, i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1130 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1131 | lockdep_assert_held(&cgroup_tree_mutex); |
| 1132 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1133 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1134 | for_each_subsys(ss, ssid) { |
| 1135 | if (!(ss_mask & (1 << ssid))) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1136 | continue; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1137 | |
Tejun Heo | 7fd8c56 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1138 | /* if @ss has non-root csses attached to it, can't move */ |
| 1139 | if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss))) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1140 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1141 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1142 | /* can't move between two non-dummy roots either */ |
Tejun Heo | 7fd8c56 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1143 | if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1144 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1147 | ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask); |
| 1148 | if (ret) { |
| 1149 | if (dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1150 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1151 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1152 | /* |
| 1153 | * Rebinding back to the default root is not allowed to |
| 1154 | * fail. Using both default and non-default roots should |
| 1155 | * be rare. Moving subsystems back and forth even more so. |
| 1156 | * Just warn about it and continue. |
| 1157 | */ |
| 1158 | if (cgrp_dfl_root_visible) { |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1159 | pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n", |
Jianyu Zhan | a2a1f9e | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1160 | ret, ss_mask); |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1161 | pr_warn("you may retry by moving them to a different hierarchy and unbinding\n"); |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1162 | } |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1163 | } |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1164 | |
| 1165 | /* |
| 1166 | * Nothing can fail from this point on. Remove files for the |
| 1167 | * removed subsystems and rebind each subsystem. |
| 1168 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1169 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1170 | for_each_subsys(ss, ssid) |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1171 | if (ss_mask & (1 << ssid)) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1172 | cgroup_clear_dir(&ss->root->cgrp, 1 << ssid); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1173 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1174 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1175 | for_each_subsys(ss, ssid) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1176 | struct cgroup_root *src_root; |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1177 | struct cgroup_subsys_state *css; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1178 | struct css_set *cset; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1179 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1180 | if (!(ss_mask & (1 << ssid))) |
| 1181 | continue; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1182 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1183 | src_root = ss->root; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1184 | css = cgroup_css(&src_root->cgrp, ss); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1185 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1186 | WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1187 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1188 | RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL); |
| 1189 | rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1190 | ss->root = dst_root; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1191 | css->cgroup = &dst_root->cgrp; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1192 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1193 | down_write(&css_set_rwsem); |
| 1194 | hash_for_each(css_set_table, i, cset, hlist) |
| 1195 | list_move_tail(&cset->e_cset_node[ss->id], |
| 1196 | &dst_root->cgrp.e_csets[ss->id]); |
| 1197 | up_write(&css_set_rwsem); |
| 1198 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1199 | src_root->subsys_mask &= ~(1 << ssid); |
| 1200 | src_root->cgrp.child_subsys_mask &= ~(1 << ssid); |
| 1201 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1202 | /* default hierarchy doesn't enable controllers by default */ |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1203 | dst_root->subsys_mask |= 1 << ssid; |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1204 | if (dst_root != &cgrp_dfl_root) |
| 1205 | dst_root->cgrp.child_subsys_mask |= 1 << ssid; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1206 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1207 | if (ss->bind) |
| 1208 | ss->bind(css); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1209 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1210 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1211 | kernfs_activate(dst_root->cgrp.kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1212 | return 0; |
| 1213 | } |
| 1214 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1215 | static int cgroup_show_options(struct seq_file *seq, |
| 1216 | struct kernfs_root *kf_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1217 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1218 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1219 | struct cgroup_subsys *ss; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1220 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1221 | |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1222 | for_each_subsys(ss, ssid) |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1223 | if (root->subsys_mask & (1 << ssid)) |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1224 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1225 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1226 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1227 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1228 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1229 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1230 | seq_puts(seq, ",xattr"); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1231 | |
| 1232 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1233 | if (strlen(root->release_agent_path)) |
| 1234 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1235 | spin_unlock(&release_agent_path_lock); |
| 1236 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1237 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1238 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1239 | if (strlen(root->name)) |
| 1240 | seq_printf(seq, ",name=%s", root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | struct cgroup_sb_opts { |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1245 | unsigned int subsys_mask; |
| 1246 | unsigned int flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1247 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1248 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1249 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1250 | /* User explicitly requested empty subsystem */ |
| 1251 | bool none; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1252 | }; |
| 1253 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1254 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1255 | { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1256 | char *token, *o = data; |
| 1257 | bool all_ss = false, one_ss = false; |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1258 | unsigned int mask = -1U; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1259 | struct cgroup_subsys *ss; |
| 1260 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1261 | |
| 1262 | #ifdef CONFIG_CPUSETS |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1263 | mask = ~(1U << cpuset_cgrp_id); |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1264 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1265 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1266 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1267 | |
| 1268 | while ((token = strsep(&o, ",")) != NULL) { |
| 1269 | if (!*token) |
| 1270 | return -EINVAL; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1271 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1272 | /* Explicitly have no subsystems */ |
| 1273 | opts->none = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1274 | continue; |
| 1275 | } |
| 1276 | if (!strcmp(token, "all")) { |
| 1277 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1278 | if (one_ss) |
| 1279 | return -EINVAL; |
| 1280 | all_ss = true; |
| 1281 | continue; |
| 1282 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1283 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1284 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1285 | continue; |
| 1286 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1287 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1288 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1289 | continue; |
| 1290 | } |
| 1291 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1292 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1293 | continue; |
| 1294 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1295 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1296 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1297 | continue; |
| 1298 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1299 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1300 | /* Specifying two release agents is forbidden */ |
| 1301 | if (opts->release_agent) |
| 1302 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1303 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1304 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1305 | if (!opts->release_agent) |
| 1306 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1307 | continue; |
| 1308 | } |
| 1309 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1310 | const char *name = token + 5; |
| 1311 | /* Can't specify an empty name */ |
| 1312 | if (!strlen(name)) |
| 1313 | return -EINVAL; |
| 1314 | /* Must match [\w.-]+ */ |
| 1315 | for (i = 0; i < strlen(name); i++) { |
| 1316 | char c = name[i]; |
| 1317 | if (isalnum(c)) |
| 1318 | continue; |
| 1319 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1320 | continue; |
| 1321 | return -EINVAL; |
| 1322 | } |
| 1323 | /* Specifying two names is forbidden */ |
| 1324 | if (opts->name) |
| 1325 | return -EINVAL; |
| 1326 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1327 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1328 | GFP_KERNEL); |
| 1329 | if (!opts->name) |
| 1330 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1331 | |
| 1332 | continue; |
| 1333 | } |
| 1334 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1335 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1336 | if (strcmp(token, ss->name)) |
| 1337 | continue; |
| 1338 | if (ss->disabled) |
| 1339 | continue; |
| 1340 | |
| 1341 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1342 | if (all_ss) |
| 1343 | return -EINVAL; |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1344 | opts->subsys_mask |= (1 << i); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1345 | one_ss = true; |
| 1346 | |
| 1347 | break; |
| 1348 | } |
| 1349 | if (i == CGROUP_SUBSYS_COUNT) |
| 1350 | return -ENOENT; |
| 1351 | } |
| 1352 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1353 | /* Consistency checks */ |
| 1354 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1355 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1356 | pr_warn("sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1357 | |
Tejun Heo | d3ba07c | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1358 | if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) || |
| 1359 | opts->cpuset_clone_children || opts->release_agent || |
| 1360 | opts->name) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1361 | pr_err("sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n"); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1362 | return -EINVAL; |
| 1363 | } |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1364 | } else { |
| 1365 | /* |
| 1366 | * If the 'all' option was specified select all the |
| 1367 | * subsystems, otherwise if 'none', 'name=' and a subsystem |
| 1368 | * name options were not specified, let's default to 'all' |
| 1369 | */ |
| 1370 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1371 | for_each_subsys(ss, i) |
| 1372 | if (!ss->disabled) |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1373 | opts->subsys_mask |= (1 << i); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1374 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1375 | /* |
| 1376 | * We either have to specify by name or by subsystems. (So |
| 1377 | * all empty hierarchies must have a name). |
| 1378 | */ |
| 1379 | if (!opts->subsys_mask && !opts->name) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1380 | return -EINVAL; |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1383 | /* |
| 1384 | * Option noprefix was introduced just for backward compatibility |
| 1385 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1386 | * the cpuset subsystem. |
| 1387 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1388 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1389 | return -EINVAL; |
| 1390 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1391 | |
| 1392 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1393 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1394 | return -EINVAL; |
| 1395 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
| 1398 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1399 | static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1400 | { |
| 1401 | int ret = 0; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1402 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1403 | struct cgroup_sb_opts opts; |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1404 | unsigned int added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1405 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1406 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1407 | pr_err("sane_behavior: remount is not allowed\n"); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1408 | return -EINVAL; |
| 1409 | } |
| 1410 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1411 | mutex_lock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1412 | mutex_lock(&cgroup_mutex); |
| 1413 | |
| 1414 | /* See what subsystems are wanted */ |
| 1415 | ret = parse_cgroupfs_options(data, &opts); |
| 1416 | if (ret) |
| 1417 | goto out_unlock; |
| 1418 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1419 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1420 | pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n", |
Jianyu Zhan | a2a1f9e | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1421 | task_tgid_nr(current), current->comm); |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1422 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1423 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1424 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1425 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1426 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1427 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1428 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1429 | pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n", |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1430 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1431 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1432 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1433 | goto out_unlock; |
| 1434 | } |
| 1435 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1436 | /* remounting is not allowed for populated hierarchies */ |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1437 | if (!list_empty(&root->cgrp.children)) { |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1438 | ret = -EBUSY; |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1439 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1440 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1441 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1442 | ret = rebind_subsystems(root, added_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1443 | if (ret) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1444 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1445 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1446 | rebind_subsystems(&cgrp_dfl_root, removed_mask); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1447 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1448 | if (opts.release_agent) { |
| 1449 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1450 | strcpy(root->release_agent_path, opts.release_agent); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1451 | spin_unlock(&release_agent_path_lock); |
| 1452 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1453 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1454 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1455 | kfree(opts.name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1456 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1457 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1458 | return ret; |
| 1459 | } |
| 1460 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1461 | /* |
| 1462 | * To reduce the fork() overhead for systems that are not actually using |
| 1463 | * their cgroups capability, we don't maintain the lists running through |
| 1464 | * each css_set to its tasks until we see the list actually used - in other |
| 1465 | * words after the first mount. |
| 1466 | */ |
| 1467 | static bool use_task_css_set_links __read_mostly; |
| 1468 | |
| 1469 | static void cgroup_enable_task_cg_lists(void) |
| 1470 | { |
| 1471 | struct task_struct *p, *g; |
| 1472 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1473 | down_write(&css_set_rwsem); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1474 | |
| 1475 | if (use_task_css_set_links) |
| 1476 | goto out_unlock; |
| 1477 | |
| 1478 | use_task_css_set_links = true; |
| 1479 | |
| 1480 | /* |
| 1481 | * We need tasklist_lock because RCU is not safe against |
| 1482 | * while_each_thread(). Besides, a forking task that has passed |
| 1483 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 1484 | * is not guaranteed to have its child immediately visible in the |
| 1485 | * tasklist if we walk through it with RCU. |
| 1486 | */ |
| 1487 | read_lock(&tasklist_lock); |
| 1488 | do_each_thread(g, p) { |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1489 | WARN_ON_ONCE(!list_empty(&p->cg_list) || |
| 1490 | task_css_set(p) != &init_css_set); |
| 1491 | |
| 1492 | /* |
| 1493 | * We should check if the process is exiting, otherwise |
| 1494 | * it will race with cgroup_exit() in that the list |
| 1495 | * entry won't be deleted though the process has exited. |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1496 | * Do it while holding siglock so that we don't end up |
| 1497 | * racing against cgroup_exit(). |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1498 | */ |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1499 | spin_lock_irq(&p->sighand->siglock); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1500 | if (!(p->flags & PF_EXITING)) { |
| 1501 | struct css_set *cset = task_css_set(p); |
| 1502 | |
| 1503 | list_add(&p->cg_list, &cset->tasks); |
| 1504 | get_css_set(cset); |
| 1505 | } |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1506 | spin_unlock_irq(&p->sighand->siglock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1507 | } while_each_thread(g, p); |
| 1508 | read_unlock(&tasklist_lock); |
| 1509 | out_unlock: |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1510 | up_write(&css_set_rwsem); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1511 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1512 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1513 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1514 | { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1515 | struct cgroup_subsys *ss; |
| 1516 | int ssid; |
| 1517 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1518 | atomic_set(&cgrp->refcnt, 1); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1519 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1520 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1521 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1522 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1523 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1524 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1525 | cgrp->dummy_css.cgroup = cgrp; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1526 | |
| 1527 | for_each_subsys(ss, ssid) |
| 1528 | INIT_LIST_HEAD(&cgrp->e_csets[ssid]); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1529 | |
| 1530 | init_waitqueue_head(&cgrp->offline_waitq); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1531 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1532 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1533 | static void init_cgroup_root(struct cgroup_root *root, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1534 | struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1535 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1536 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1537 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1538 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1539 | atomic_set(&root->nr_cgrps, 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1540 | cgrp->root = root; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1541 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1542 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1543 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1544 | root->flags = opts->flags; |
| 1545 | if (opts->release_agent) |
| 1546 | strcpy(root->release_agent_path, opts->release_agent); |
| 1547 | if (opts->name) |
| 1548 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1549 | if (opts->cpuset_clone_children) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1550 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1553 | static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1554 | { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1555 | LIST_HEAD(tmp_links); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1556 | struct cgroup *root_cgrp = &root->cgrp; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1557 | struct css_set *cset; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1558 | int i, ret; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1559 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1560 | lockdep_assert_held(&cgroup_tree_mutex); |
| 1561 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1562 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1563 | ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_NOWAIT); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1564 | if (ret < 0) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1565 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1566 | root_cgrp->id = ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1567 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1568 | /* |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1569 | * We're accessing css_set_count without locking css_set_rwsem here, |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1570 | * but that's OK - it can only be increased by someone holding |
| 1571 | * cgroup_lock, and that's us. The worst that can happen is that we |
| 1572 | * have some link structures left over |
| 1573 | */ |
| 1574 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1575 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1576 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1577 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1578 | ret = cgroup_init_root_id(root); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1579 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1580 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1581 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1582 | root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops, |
| 1583 | KERNFS_ROOT_CREATE_DEACTIVATED, |
| 1584 | root_cgrp); |
| 1585 | if (IS_ERR(root->kf_root)) { |
| 1586 | ret = PTR_ERR(root->kf_root); |
| 1587 | goto exit_root_id; |
| 1588 | } |
| 1589 | root_cgrp->kn = root->kf_root->kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1590 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1591 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
| 1592 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1593 | goto destroy_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1594 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1595 | ret = rebind_subsystems(root, ss_mask); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1596 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1597 | goto destroy_root; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1598 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1599 | /* |
| 1600 | * There must be no failure case after here, since rebinding takes |
| 1601 | * care of subsystems' refcounts, which are explicitly dropped in |
| 1602 | * the failure exit path. |
| 1603 | */ |
| 1604 | list_add(&root->root_list, &cgroup_roots); |
| 1605 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1606 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1607 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1608 | * Link the root cgroup in this hierarchy into all the css_set |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1609 | * objects. |
| 1610 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1611 | down_write(&css_set_rwsem); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1612 | hash_for_each(css_set_table, i, cset, hlist) |
| 1613 | link_css_set(&tmp_links, cset, root_cgrp); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1614 | up_write(&css_set_rwsem); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1615 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1616 | BUG_ON(!list_empty(&root_cgrp->children)); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1617 | BUG_ON(atomic_read(&root->nr_cgrps) != 1); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1618 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1619 | kernfs_activate(root_cgrp->kn); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1620 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1621 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1622 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1623 | destroy_root: |
| 1624 | kernfs_destroy_root(root->kf_root); |
| 1625 | root->kf_root = NULL; |
| 1626 | exit_root_id: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1627 | cgroup_exit_root_id(root); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1628 | out: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1629 | free_cgrp_cset_links(&tmp_links); |
| 1630 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1633 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1634 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1635 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1636 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1637 | struct cgroup_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1638 | struct cgroup_sb_opts opts; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1639 | struct dentry *dentry; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1640 | int ret; |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 1641 | bool new_sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1642 | |
| 1643 | /* |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1644 | * The first time anyone tries to mount a cgroup, enable the list |
| 1645 | * linking each css_set to its tasks and fix up all existing tasks. |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1646 | */ |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1647 | if (!use_task_css_set_links) |
| 1648 | cgroup_enable_task_cg_lists(); |
Li Zefan | e37a06f | 2014-04-17 13:53:08 +0800 | [diff] [blame] | 1649 | |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1650 | mutex_lock(&cgroup_tree_mutex); |
| 1651 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1652 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1653 | /* First find the desired set of subsystems */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1654 | ret = parse_cgroupfs_options(data, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1655 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1656 | goto out_unlock; |
Li Zefan | e37a06f | 2014-04-17 13:53:08 +0800 | [diff] [blame] | 1657 | retry: |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1658 | /* look for a matching existing root */ |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1659 | if (!opts.subsys_mask && !opts.none && !opts.name) { |
| 1660 | cgrp_dfl_root_visible = true; |
| 1661 | root = &cgrp_dfl_root; |
| 1662 | cgroup_get(&root->cgrp); |
| 1663 | ret = 0; |
| 1664 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1667 | for_each_root(root) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1668 | bool name_match = false; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1669 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1670 | if (root == &cgrp_dfl_root) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1671 | continue; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1672 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1673 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1674 | * If we asked for a name then it must match. Also, if |
| 1675 | * name matches but sybsys_mask doesn't, we should fail. |
| 1676 | * Remember whether name matched. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1677 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1678 | if (opts.name) { |
| 1679 | if (strcmp(opts.name, root->name)) |
| 1680 | continue; |
| 1681 | name_match = true; |
| 1682 | } |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1683 | |
| 1684 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1685 | * If we asked for subsystems (or explicitly for no |
| 1686 | * subsystems) then they must match. |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1687 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1688 | if ((opts.subsys_mask || opts.none) && |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1689 | (opts.subsys_mask != root->subsys_mask)) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1690 | if (!name_match) |
| 1691 | continue; |
| 1692 | ret = -EBUSY; |
| 1693 | goto out_unlock; |
| 1694 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1695 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1696 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1697 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1698 | pr_err("sane_behavior: new mount options should match the existing superblock\n"); |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1699 | ret = -EINVAL; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1700 | goto out_unlock; |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1701 | } else { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1702 | pr_warn("new mount options do not match the existing superblock, will be ignored\n"); |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1703 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1704 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1705 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1706 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1707 | * A root's lifetime is governed by its root cgroup. Zero |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1708 | * ref indicate that the root is being destroyed. Wait for |
| 1709 | * destruction to complete so that the subsystems are free. |
| 1710 | * We can use wait_queue for the wait but this path is |
| 1711 | * super cold. Let's just sleep for a bit and retry. |
| 1712 | */ |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1713 | if (!atomic_inc_not_zero(&root->cgrp.refcnt)) { |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1714 | mutex_unlock(&cgroup_mutex); |
| 1715 | mutex_unlock(&cgroup_tree_mutex); |
| 1716 | msleep(10); |
Li Zefan | e37a06f | 2014-04-17 13:53:08 +0800 | [diff] [blame] | 1717 | mutex_lock(&cgroup_tree_mutex); |
| 1718 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1719 | goto retry; |
| 1720 | } |
| 1721 | |
| 1722 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1723 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1724 | } |
| 1725 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1726 | /* |
| 1727 | * No such thing, create a new one. name= matching without subsys |
| 1728 | * specification is allowed for already existing hierarchies but we |
| 1729 | * can't create new one without subsys specification. |
| 1730 | */ |
| 1731 | if (!opts.subsys_mask && !opts.none) { |
| 1732 | ret = -EINVAL; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1733 | goto out_unlock; |
| 1734 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1735 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1736 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1737 | if (!root) { |
| 1738 | ret = -ENOMEM; |
| 1739 | goto out_unlock; |
| 1740 | } |
| 1741 | |
| 1742 | init_cgroup_root(root, &opts); |
| 1743 | |
Tejun Heo | 3558557 | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1744 | ret = cgroup_setup_root(root, opts.subsys_mask); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1745 | if (ret) |
| 1746 | cgroup_free_root(root); |
| 1747 | |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1748 | out_unlock: |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1749 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1750 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1751 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1752 | kfree(opts.release_agent); |
| 1753 | kfree(opts.name); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1754 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1755 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1756 | return ERR_PTR(ret); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1757 | |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 1758 | dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb); |
| 1759 | if (IS_ERR(dentry) || !new_sb) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1760 | cgroup_put(&root->cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1761 | return dentry; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 1764 | static void cgroup_kill_sb(struct super_block *sb) |
| 1765 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1766 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1767 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1768 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1769 | cgroup_put(&root->cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1770 | kernfs_kill_sb(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | static struct file_system_type cgroup_fs_type = { |
| 1774 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1775 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1776 | .kill_sb = cgroup_kill_sb, |
| 1777 | }; |
| 1778 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1779 | static struct kobject *cgroup_kobj; |
| 1780 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1781 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1782 | * 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] | 1783 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1784 | * @buf: the buffer to write the path into |
| 1785 | * @buflen: the length of the buffer |
| 1786 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1787 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1788 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1789 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1790 | * cgroup controller callbacks. |
| 1791 | * |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1792 | * Return value is the same as kernfs_path(). |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1793 | */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1794 | char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1795 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1796 | struct cgroup_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1797 | struct cgroup *cgrp; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1798 | int hierarchy_id = 1; |
| 1799 | char *path = NULL; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1800 | |
| 1801 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1802 | down_read(&css_set_rwsem); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1803 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1804 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1805 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1806 | if (root) { |
| 1807 | cgrp = task_cgroup_from_root(task, root); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1808 | path = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1809 | } else { |
| 1810 | /* if no hierarchy exists, everyone is in "/" */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1811 | if (strlcpy(buf, "/", buflen) < buflen) |
| 1812 | path = buf; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1815 | up_read(&css_set_rwsem); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1816 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1817 | return path; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 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 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1821 | /* used to track tasks and other necessary states during migration */ |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1822 | struct cgroup_taskset { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1823 | /* the src and dst cset list running through cset->mg_node */ |
| 1824 | struct list_head src_csets; |
| 1825 | struct list_head dst_csets; |
| 1826 | |
| 1827 | /* |
| 1828 | * Fields for cgroup_taskset_*() iteration. |
| 1829 | * |
| 1830 | * Before migration is committed, the target migration tasks are on |
| 1831 | * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of |
| 1832 | * the csets on ->dst_csets. ->csets point to either ->src_csets |
| 1833 | * or ->dst_csets depending on whether migration is committed. |
| 1834 | * |
| 1835 | * ->cur_csets and ->cur_task point to the current task position |
| 1836 | * during iteration. |
| 1837 | */ |
| 1838 | struct list_head *csets; |
| 1839 | struct css_set *cur_cset; |
| 1840 | struct task_struct *cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1841 | }; |
| 1842 | |
| 1843 | /** |
| 1844 | * cgroup_taskset_first - reset taskset and return the first task |
| 1845 | * @tset: taskset of interest |
| 1846 | * |
| 1847 | * @tset iteration is initialized and the first task is returned. |
| 1848 | */ |
| 1849 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1850 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1851 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); |
| 1852 | tset->cur_task = NULL; |
| 1853 | |
| 1854 | return cgroup_taskset_next(tset); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1855 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1856 | |
| 1857 | /** |
| 1858 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1859 | * @tset: taskset of interest |
| 1860 | * |
| 1861 | * Return the next task in @tset. Iteration must have been initialized |
| 1862 | * with cgroup_taskset_first(). |
| 1863 | */ |
| 1864 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1865 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1866 | struct css_set *cset = tset->cur_cset; |
| 1867 | struct task_struct *task = tset->cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1868 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1869 | while (&cset->mg_node != tset->csets) { |
| 1870 | if (!task) |
| 1871 | task = list_first_entry(&cset->mg_tasks, |
| 1872 | struct task_struct, cg_list); |
| 1873 | else |
| 1874 | task = list_next_entry(task, cg_list); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1875 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1876 | if (&task->cg_list != &cset->mg_tasks) { |
| 1877 | tset->cur_cset = cset; |
| 1878 | tset->cur_task = task; |
| 1879 | return task; |
| 1880 | } |
| 1881 | |
| 1882 | cset = list_next_entry(cset, mg_node); |
| 1883 | task = NULL; |
| 1884 | } |
| 1885 | |
| 1886 | return NULL; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1887 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1888 | |
| 1889 | /** |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1890 | * cgroup_task_migrate - move a task from one cgroup to another. |
Fabian Frederick | 6010694 | 2014-05-05 20:08:13 +0200 | [diff] [blame] | 1891 | * @old_cgrp: the cgroup @tsk is being migrated from |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1892 | * @tsk: the task being migrated |
| 1893 | * @new_cset: the new css_set @tsk is being attached to |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1894 | * |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1895 | * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1896 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1897 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1898 | struct task_struct *tsk, |
| 1899 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1900 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1901 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1902 | |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1903 | lockdep_assert_held(&cgroup_mutex); |
| 1904 | lockdep_assert_held(&css_set_rwsem); |
| 1905 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1906 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1907 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1908 | * setting such that we can't race against cgroup_exit() changing the |
| 1909 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1910 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1911 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1912 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1913 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1914 | get_css_set(new_cset); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1915 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1916 | |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 1917 | /* |
| 1918 | * Use move_tail so that cgroup_taskset_first() still returns the |
| 1919 | * leader after migration. This works because cgroup_migrate() |
| 1920 | * ensures that the dst_cset of the leader is the first on the |
| 1921 | * tset's dst_csets list. |
| 1922 | */ |
| 1923 | list_move_tail(&tsk->cg_list, &new_cset->mg_tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1924 | |
| 1925 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1926 | * We just gained a reference on old_cset by taking it from the |
| 1927 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1928 | * 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] | 1929 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1930 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1931 | put_css_set_locked(old_cset, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1934 | /** |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1935 | * cgroup_migrate_finish - cleanup after attach |
| 1936 | * @preloaded_csets: list of preloaded css_sets |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1937 | * |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1938 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See |
| 1939 | * those functions for details. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1940 | */ |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1941 | static void cgroup_migrate_finish(struct list_head *preloaded_csets) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1942 | { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1943 | struct css_set *cset, *tmp_cset; |
| 1944 | |
| 1945 | lockdep_assert_held(&cgroup_mutex); |
| 1946 | |
| 1947 | down_write(&css_set_rwsem); |
| 1948 | list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) { |
| 1949 | cset->mg_src_cgrp = NULL; |
| 1950 | cset->mg_dst_cset = NULL; |
| 1951 | list_del_init(&cset->mg_preload_node); |
| 1952 | put_css_set_locked(cset, false); |
| 1953 | } |
| 1954 | up_write(&css_set_rwsem); |
| 1955 | } |
| 1956 | |
| 1957 | /** |
| 1958 | * cgroup_migrate_add_src - add a migration source css_set |
| 1959 | * @src_cset: the source css_set to add |
| 1960 | * @dst_cgrp: the destination cgroup |
| 1961 | * @preloaded_csets: list of preloaded css_sets |
| 1962 | * |
| 1963 | * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin |
| 1964 | * @src_cset and add it to @preloaded_csets, which should later be cleaned |
| 1965 | * up by cgroup_migrate_finish(). |
| 1966 | * |
| 1967 | * This function may be called without holding threadgroup_lock even if the |
| 1968 | * target is a process. Threads may be created and destroyed but as long |
| 1969 | * as cgroup_mutex is not dropped, no new css_set can be put into play and |
| 1970 | * the preloaded css_sets are guaranteed to cover all migrations. |
| 1971 | */ |
| 1972 | static void cgroup_migrate_add_src(struct css_set *src_cset, |
| 1973 | struct cgroup *dst_cgrp, |
| 1974 | struct list_head *preloaded_csets) |
| 1975 | { |
| 1976 | struct cgroup *src_cgrp; |
| 1977 | |
| 1978 | lockdep_assert_held(&cgroup_mutex); |
| 1979 | lockdep_assert_held(&css_set_rwsem); |
| 1980 | |
| 1981 | src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); |
| 1982 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1983 | if (!list_empty(&src_cset->mg_preload_node)) |
| 1984 | return; |
| 1985 | |
| 1986 | WARN_ON(src_cset->mg_src_cgrp); |
| 1987 | WARN_ON(!list_empty(&src_cset->mg_tasks)); |
| 1988 | WARN_ON(!list_empty(&src_cset->mg_node)); |
| 1989 | |
| 1990 | src_cset->mg_src_cgrp = src_cgrp; |
| 1991 | get_css_set(src_cset); |
| 1992 | list_add(&src_cset->mg_preload_node, preloaded_csets); |
| 1993 | } |
| 1994 | |
| 1995 | /** |
| 1996 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1997 | * @dst_cgrp: the destination cgroup (may be %NULL) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1998 | * @preloaded_csets: list of preloaded source css_sets |
| 1999 | * |
| 2000 | * Tasks are about to be moved to @dst_cgrp and all the source css_sets |
| 2001 | * have been preloaded to @preloaded_csets. This function looks up and |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2002 | * pins all destination css_sets, links each to its source, and append them |
| 2003 | * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each |
| 2004 | * source css_set is assumed to be its cgroup on the default hierarchy. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2005 | * |
| 2006 | * This function must be called after cgroup_migrate_add_src() has been |
| 2007 | * called on each migration source css_set. After migration is performed |
| 2008 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on |
| 2009 | * @preloaded_csets. |
| 2010 | */ |
| 2011 | static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp, |
| 2012 | struct list_head *preloaded_csets) |
| 2013 | { |
| 2014 | LIST_HEAD(csets); |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2015 | struct css_set *src_cset, *tmp_cset; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2016 | |
| 2017 | lockdep_assert_held(&cgroup_mutex); |
| 2018 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2019 | /* |
| 2020 | * Except for the root, child_subsys_mask must be zero for a cgroup |
| 2021 | * with tasks so that child cgroups don't compete against tasks. |
| 2022 | */ |
| 2023 | if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent && |
| 2024 | dst_cgrp->child_subsys_mask) |
| 2025 | return -EBUSY; |
| 2026 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2027 | /* look up the dst cset for each src cset and link it to src */ |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2028 | list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2029 | struct css_set *dst_cset; |
| 2030 | |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2031 | dst_cset = find_css_set(src_cset, |
| 2032 | dst_cgrp ?: src_cset->dfl_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2033 | if (!dst_cset) |
| 2034 | goto err; |
| 2035 | |
| 2036 | WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset); |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2037 | |
| 2038 | /* |
| 2039 | * If src cset equals dst, it's noop. Drop the src. |
| 2040 | * cgroup_migrate() will skip the cset too. Note that we |
| 2041 | * can't handle src == dst as some nodes are used by both. |
| 2042 | */ |
| 2043 | if (src_cset == dst_cset) { |
| 2044 | src_cset->mg_src_cgrp = NULL; |
| 2045 | list_del_init(&src_cset->mg_preload_node); |
| 2046 | put_css_set(src_cset, false); |
| 2047 | put_css_set(dst_cset, false); |
| 2048 | continue; |
| 2049 | } |
| 2050 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2051 | src_cset->mg_dst_cset = dst_cset; |
| 2052 | |
| 2053 | if (list_empty(&dst_cset->mg_preload_node)) |
| 2054 | list_add(&dst_cset->mg_preload_node, &csets); |
| 2055 | else |
| 2056 | put_css_set(dst_cset, false); |
| 2057 | } |
| 2058 | |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2059 | list_splice_tail(&csets, preloaded_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2060 | return 0; |
| 2061 | err: |
| 2062 | cgroup_migrate_finish(&csets); |
| 2063 | return -ENOMEM; |
| 2064 | } |
| 2065 | |
| 2066 | /** |
| 2067 | * cgroup_migrate - migrate a process or task to a cgroup |
| 2068 | * @cgrp: the destination cgroup |
| 2069 | * @leader: the leader of the process or the task to migrate |
| 2070 | * @threadgroup: whether @leader points to the whole process or a single task |
| 2071 | * |
| 2072 | * Migrate a process or task denoted by @leader to @cgrp. If migrating a |
| 2073 | * process, the caller must be holding threadgroup_lock of @leader. The |
| 2074 | * caller is also responsible for invoking cgroup_migrate_add_src() and |
| 2075 | * cgroup_migrate_prepare_dst() on the targets before invoking this |
| 2076 | * function and following up with cgroup_migrate_finish(). |
| 2077 | * |
| 2078 | * As long as a controller's ->can_attach() doesn't fail, this function is |
| 2079 | * guaranteed to succeed. This means that, excluding ->can_attach() |
| 2080 | * failure, when migrating multiple targets, the success or failure can be |
| 2081 | * decided for all targets by invoking group_migrate_prepare_dst() before |
| 2082 | * actually starting migrating. |
| 2083 | */ |
| 2084 | static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader, |
| 2085 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2086 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2087 | struct cgroup_taskset tset = { |
| 2088 | .src_csets = LIST_HEAD_INIT(tset.src_csets), |
| 2089 | .dst_csets = LIST_HEAD_INIT(tset.dst_csets), |
| 2090 | .csets = &tset.src_csets, |
| 2091 | }; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2092 | struct cgroup_subsys_state *css, *failed_css = NULL; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2093 | struct css_set *cset, *tmp_cset; |
| 2094 | struct task_struct *task, *tmp_task; |
| 2095 | int i, ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2096 | |
| 2097 | /* |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2098 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2099 | * already PF_EXITING could be freed from underneath us unless we |
| 2100 | * take an rcu_read_lock. |
| 2101 | */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2102 | down_write(&css_set_rwsem); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2103 | rcu_read_lock(); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2104 | task = leader; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2105 | do { |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2106 | /* @task either already exited or can't exit until the end */ |
| 2107 | if (task->flags & PF_EXITING) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2108 | goto next; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2109 | |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2110 | /* leave @task alone if post_fork() hasn't linked it yet */ |
| 2111 | if (list_empty(&task->cg_list)) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2112 | goto next; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2113 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2114 | cset = task_css_set(task); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2115 | if (!cset->mg_src_cgrp) |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2116 | goto next; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2117 | |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2118 | /* |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 2119 | * cgroup_taskset_first() must always return the leader. |
| 2120 | * Take care to avoid disturbing the ordering. |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2121 | */ |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 2122 | list_move_tail(&task->cg_list, &cset->mg_tasks); |
| 2123 | if (list_empty(&cset->mg_node)) |
| 2124 | list_add_tail(&cset->mg_node, &tset.src_csets); |
| 2125 | if (list_empty(&cset->mg_dst_cset->mg_node)) |
| 2126 | list_move_tail(&cset->mg_dst_cset->mg_node, |
| 2127 | &tset.dst_csets); |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2128 | next: |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2129 | if (!threadgroup) |
| 2130 | break; |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2131 | } while_each_thread(leader, task); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2132 | rcu_read_unlock(); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2133 | up_write(&css_set_rwsem); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2134 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2135 | /* methods shouldn't be called if no task is actually migrating */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2136 | if (list_empty(&tset.src_csets)) |
| 2137 | return 0; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2138 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2139 | /* check that we can legitimately attach to the cgroup */ |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 2140 | for_each_e_css(css, i, cgrp) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2141 | if (css->ss->can_attach) { |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2142 | ret = css->ss->can_attach(css, &tset); |
| 2143 | if (ret) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2144 | failed_css = css; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2145 | goto out_cancel_attach; |
| 2146 | } |
| 2147 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | /* |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2151 | * Now that we're guaranteed success, proceed to move all tasks to |
| 2152 | * the new cgroup. There are no failure cases after here, so this |
| 2153 | * is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2154 | */ |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 2155 | down_write(&css_set_rwsem); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2156 | list_for_each_entry(cset, &tset.src_csets, mg_node) { |
| 2157 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) |
| 2158 | cgroup_task_migrate(cset->mg_src_cgrp, task, |
| 2159 | cset->mg_dst_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2160 | } |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 2161 | up_write(&css_set_rwsem); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2162 | |
| 2163 | /* |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2164 | * Migration is committed, all target tasks are now on dst_csets. |
| 2165 | * Nothing is sensitive to fork() after this point. Notify |
| 2166 | * controllers that migration is complete. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2167 | */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2168 | tset.csets = &tset.dst_csets; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2169 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 2170 | for_each_e_css(css, i, cgrp) |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2171 | if (css->ss->attach) |
| 2172 | css->ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2173 | |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2174 | ret = 0; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2175 | goto out_release_tset; |
| 2176 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2177 | out_cancel_attach: |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 2178 | for_each_e_css(css, i, cgrp) { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2179 | if (css == failed_css) |
| 2180 | break; |
| 2181 | if (css->ss->cancel_attach) |
| 2182 | css->ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2183 | } |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2184 | out_release_tset: |
| 2185 | down_write(&css_set_rwsem); |
| 2186 | list_splice_init(&tset.dst_csets, &tset.src_csets); |
| 2187 | list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) { |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 2188 | list_splice_tail_init(&cset->mg_tasks, &cset->tasks); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2189 | list_del_init(&cset->mg_node); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2190 | } |
| 2191 | up_write(&css_set_rwsem); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2192 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2195 | /** |
| 2196 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
| 2197 | * @dst_cgrp: the cgroup to attach to |
| 2198 | * @leader: the task or the leader of the threadgroup to be attached |
| 2199 | * @threadgroup: attach the whole threadgroup? |
| 2200 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2201 | * Call holding cgroup_mutex and threadgroup_lock of @leader. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2202 | */ |
| 2203 | static int cgroup_attach_task(struct cgroup *dst_cgrp, |
| 2204 | struct task_struct *leader, bool threadgroup) |
| 2205 | { |
| 2206 | LIST_HEAD(preloaded_csets); |
| 2207 | struct task_struct *task; |
| 2208 | int ret; |
| 2209 | |
| 2210 | /* look up all src csets */ |
| 2211 | down_read(&css_set_rwsem); |
| 2212 | rcu_read_lock(); |
| 2213 | task = leader; |
| 2214 | do { |
| 2215 | cgroup_migrate_add_src(task_css_set(task), dst_cgrp, |
| 2216 | &preloaded_csets); |
| 2217 | if (!threadgroup) |
| 2218 | break; |
| 2219 | } while_each_thread(leader, task); |
| 2220 | rcu_read_unlock(); |
| 2221 | up_read(&css_set_rwsem); |
| 2222 | |
| 2223 | /* prepare dst csets and commit */ |
| 2224 | ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets); |
| 2225 | if (!ret) |
| 2226 | ret = cgroup_migrate(dst_cgrp, leader, threadgroup); |
| 2227 | |
| 2228 | cgroup_migrate_finish(&preloaded_csets); |
| 2229 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | * 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] | 2234 | * function to attach either it or all tasks in its threadgroup. Will lock |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2235 | * cgroup_mutex and threadgroup. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2236 | */ |
| 2237 | 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] | 2238 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2239 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2240 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2241 | int ret; |
| 2242 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2243 | if (!cgroup_lock_live_group(cgrp)) |
| 2244 | return -ENODEV; |
| 2245 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2246 | retry_find_task: |
| 2247 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2248 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2249 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2250 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2251 | rcu_read_unlock(); |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2252 | ret = -ESRCH; |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2253 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2254 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2255 | /* |
| 2256 | * even if we're attaching all tasks in the thread group, we |
| 2257 | * only need to check permissions on one of them. |
| 2258 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2259 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2260 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2261 | !uid_eq(cred->euid, tcred->uid) && |
| 2262 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2263 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2264 | ret = -EACCES; |
| 2265 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2266 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2267 | } else |
| 2268 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2269 | |
| 2270 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2271 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2272 | |
| 2273 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2274 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2275 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2276 | * with no rt_runtime allocated. Just say no. |
| 2277 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2278 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2279 | ret = -EINVAL; |
| 2280 | rcu_read_unlock(); |
| 2281 | goto out_unlock_cgroup; |
| 2282 | } |
| 2283 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2284 | get_task_struct(tsk); |
| 2285 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2286 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2287 | threadgroup_lock(tsk); |
| 2288 | if (threadgroup) { |
| 2289 | if (!thread_group_leader(tsk)) { |
| 2290 | /* |
| 2291 | * a race with de_thread from another thread's exec() |
| 2292 | * may strip us of our leadership, if this happens, |
| 2293 | * there is no choice but to throw this task away and |
| 2294 | * try again; this is |
| 2295 | * "double-double-toil-and-trouble-check locking". |
| 2296 | */ |
| 2297 | threadgroup_unlock(tsk); |
| 2298 | put_task_struct(tsk); |
| 2299 | goto retry_find_task; |
| 2300 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2304 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2305 | threadgroup_unlock(tsk); |
| 2306 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2307 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2308 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2309 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2310 | return ret; |
| 2311 | } |
| 2312 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2313 | /** |
| 2314 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2315 | * @from: attach to all cgroups of a given task |
| 2316 | * @tsk: the task to be attached |
| 2317 | */ |
| 2318 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2319 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2320 | struct cgroup_root *root; |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2321 | int retval = 0; |
| 2322 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2323 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 2324 | for_each_root(root) { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2325 | struct cgroup *from_cgrp; |
| 2326 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2327 | if (root == &cgrp_dfl_root) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 2328 | continue; |
| 2329 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2330 | down_read(&css_set_rwsem); |
| 2331 | from_cgrp = task_cgroup_from_root(from, root); |
| 2332 | up_read(&css_set_rwsem); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2333 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2334 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2335 | if (retval) |
| 2336 | break; |
| 2337 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2338 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2339 | |
| 2340 | return retval; |
| 2341 | } |
| 2342 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2343 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2344 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2345 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2346 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2347 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2348 | } |
| 2349 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2350 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2351 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2352 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2353 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2354 | } |
| 2355 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2356 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
Tejun Heo | 4d3bb51 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2357 | struct cftype *cft, char *buffer) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2358 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2359 | struct cgroup_root *root = css->cgroup->root; |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2360 | |
| 2361 | BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2362 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2363 | return -ENODEV; |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2364 | spin_lock(&release_agent_path_lock); |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2365 | strlcpy(root->release_agent_path, buffer, |
| 2366 | sizeof(root->release_agent_path)); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2367 | spin_unlock(&release_agent_path_lock); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2368 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2369 | return 0; |
| 2370 | } |
| 2371 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2372 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2373 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2374 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2375 | |
Tejun Heo | 46cfeb0 | 2014-05-13 12:11:00 -0400 | [diff] [blame^] | 2376 | spin_lock(&release_agent_path_lock); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2377 | seq_puts(seq, cgrp->root->release_agent_path); |
Tejun Heo | 46cfeb0 | 2014-05-13 12:11:00 -0400 | [diff] [blame^] | 2378 | spin_unlock(&release_agent_path_lock); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2379 | seq_putc(seq, '\n'); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2380 | return 0; |
| 2381 | } |
| 2382 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2383 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2384 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2385 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2386 | |
| 2387 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2388 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2389 | } |
| 2390 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2391 | static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask) |
| 2392 | { |
| 2393 | struct cgroup_subsys *ss; |
| 2394 | bool printed = false; |
| 2395 | int ssid; |
| 2396 | |
| 2397 | for_each_subsys(ss, ssid) { |
| 2398 | if (ss_mask & (1 << ssid)) { |
| 2399 | if (printed) |
| 2400 | seq_putc(seq, ' '); |
| 2401 | seq_printf(seq, "%s", ss->name); |
| 2402 | printed = true; |
| 2403 | } |
| 2404 | } |
| 2405 | if (printed) |
| 2406 | seq_putc(seq, '\n'); |
| 2407 | } |
| 2408 | |
| 2409 | /* show controllers which are currently attached to the default hierarchy */ |
| 2410 | static int cgroup_root_controllers_show(struct seq_file *seq, void *v) |
| 2411 | { |
| 2412 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2413 | |
| 2414 | cgroup_print_ss_mask(seq, cgrp->root->subsys_mask); |
| 2415 | return 0; |
| 2416 | } |
| 2417 | |
| 2418 | /* show controllers which are enabled from the parent */ |
| 2419 | static int cgroup_controllers_show(struct seq_file *seq, void *v) |
| 2420 | { |
| 2421 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2422 | |
| 2423 | cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask); |
| 2424 | return 0; |
| 2425 | } |
| 2426 | |
| 2427 | /* show controllers which are enabled for a given cgroup's children */ |
| 2428 | static int cgroup_subtree_control_show(struct seq_file *seq, void *v) |
| 2429 | { |
| 2430 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2431 | |
| 2432 | cgroup_print_ss_mask(seq, cgrp->child_subsys_mask); |
| 2433 | return 0; |
| 2434 | } |
| 2435 | |
| 2436 | /** |
| 2437 | * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy |
| 2438 | * @cgrp: root of the subtree to update csses for |
| 2439 | * |
| 2440 | * @cgrp's child_subsys_mask has changed and its subtree's (self excluded) |
| 2441 | * css associations need to be updated accordingly. This function looks up |
| 2442 | * all css_sets which are attached to the subtree, creates the matching |
| 2443 | * updated css_sets and migrates the tasks to the new ones. |
| 2444 | */ |
| 2445 | static int cgroup_update_dfl_csses(struct cgroup *cgrp) |
| 2446 | { |
| 2447 | LIST_HEAD(preloaded_csets); |
| 2448 | struct cgroup_subsys_state *css; |
| 2449 | struct css_set *src_cset; |
| 2450 | int ret; |
| 2451 | |
| 2452 | lockdep_assert_held(&cgroup_tree_mutex); |
| 2453 | lockdep_assert_held(&cgroup_mutex); |
| 2454 | |
| 2455 | /* look up all csses currently attached to @cgrp's subtree */ |
| 2456 | down_read(&css_set_rwsem); |
| 2457 | css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) { |
| 2458 | struct cgrp_cset_link *link; |
| 2459 | |
| 2460 | /* self is not affected by child_subsys_mask change */ |
| 2461 | if (css->cgroup == cgrp) |
| 2462 | continue; |
| 2463 | |
| 2464 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) |
| 2465 | cgroup_migrate_add_src(link->cset, cgrp, |
| 2466 | &preloaded_csets); |
| 2467 | } |
| 2468 | up_read(&css_set_rwsem); |
| 2469 | |
| 2470 | /* NULL dst indicates self on default hierarchy */ |
| 2471 | ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets); |
| 2472 | if (ret) |
| 2473 | goto out_finish; |
| 2474 | |
| 2475 | list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) { |
| 2476 | struct task_struct *last_task = NULL, *task; |
| 2477 | |
| 2478 | /* src_csets precede dst_csets, break on the first dst_cset */ |
| 2479 | if (!src_cset->mg_src_cgrp) |
| 2480 | break; |
| 2481 | |
| 2482 | /* |
| 2483 | * All tasks in src_cset need to be migrated to the |
| 2484 | * matching dst_cset. Empty it process by process. We |
| 2485 | * walk tasks but migrate processes. The leader might even |
| 2486 | * belong to a different cset but such src_cset would also |
| 2487 | * be among the target src_csets because the default |
| 2488 | * hierarchy enforces per-process membership. |
| 2489 | */ |
| 2490 | while (true) { |
| 2491 | down_read(&css_set_rwsem); |
| 2492 | task = list_first_entry_or_null(&src_cset->tasks, |
| 2493 | struct task_struct, cg_list); |
| 2494 | if (task) { |
| 2495 | task = task->group_leader; |
| 2496 | WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp); |
| 2497 | get_task_struct(task); |
| 2498 | } |
| 2499 | up_read(&css_set_rwsem); |
| 2500 | |
| 2501 | if (!task) |
| 2502 | break; |
| 2503 | |
| 2504 | /* guard against possible infinite loop */ |
| 2505 | if (WARN(last_task == task, |
| 2506 | "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n")) |
| 2507 | goto out_finish; |
| 2508 | last_task = task; |
| 2509 | |
| 2510 | threadgroup_lock(task); |
| 2511 | /* raced against de_thread() from another thread? */ |
| 2512 | if (!thread_group_leader(task)) { |
| 2513 | threadgroup_unlock(task); |
| 2514 | put_task_struct(task); |
| 2515 | continue; |
| 2516 | } |
| 2517 | |
| 2518 | ret = cgroup_migrate(src_cset->dfl_cgrp, task, true); |
| 2519 | |
| 2520 | threadgroup_unlock(task); |
| 2521 | put_task_struct(task); |
| 2522 | |
| 2523 | if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret)) |
| 2524 | goto out_finish; |
| 2525 | } |
| 2526 | } |
| 2527 | |
| 2528 | out_finish: |
| 2529 | cgroup_migrate_finish(&preloaded_csets); |
| 2530 | return ret; |
| 2531 | } |
| 2532 | |
| 2533 | /* change the enabled child controllers for a cgroup in the default hierarchy */ |
| 2534 | static int cgroup_subtree_control_write(struct cgroup_subsys_state *dummy_css, |
| 2535 | struct cftype *cft, char *buffer) |
| 2536 | { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2537 | unsigned int enable = 0, disable = 0; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2538 | struct cgroup *cgrp = dummy_css->cgroup, *child; |
| 2539 | struct cgroup_subsys *ss; |
| 2540 | char *tok, *p; |
| 2541 | int ssid, ret; |
| 2542 | |
| 2543 | /* |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2544 | * Parse input - space separated list of subsystem names prefixed |
| 2545 | * with either + or -. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2546 | */ |
| 2547 | p = buffer; |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2548 | while ((tok = strsep(&p, " "))) { |
| 2549 | if (tok[0] == '\0') |
| 2550 | continue; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2551 | for_each_subsys(ss, ssid) { |
| 2552 | if (ss->disabled || strcmp(tok + 1, ss->name)) |
| 2553 | continue; |
| 2554 | |
| 2555 | if (*tok == '+') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2556 | enable |= 1 << ssid; |
| 2557 | disable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2558 | } else if (*tok == '-') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2559 | disable |= 1 << ssid; |
| 2560 | enable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2561 | } else { |
| 2562 | return -EINVAL; |
| 2563 | } |
| 2564 | break; |
| 2565 | } |
| 2566 | if (ssid == CGROUP_SUBSYS_COUNT) |
| 2567 | return -EINVAL; |
| 2568 | } |
| 2569 | |
| 2570 | /* |
| 2571 | * We're gonna grab cgroup_tree_mutex which nests outside kernfs |
| 2572 | * active_ref. cgroup_lock_live_group() already provides enough |
| 2573 | * protection. Ensure @cgrp stays accessible and break the |
| 2574 | * active_ref protection. |
| 2575 | */ |
| 2576 | cgroup_get(cgrp); |
| 2577 | kernfs_break_active_protection(cgrp->control_kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2578 | |
| 2579 | mutex_lock(&cgroup_tree_mutex); |
| 2580 | |
| 2581 | for_each_subsys(ss, ssid) { |
| 2582 | if (enable & (1 << ssid)) { |
| 2583 | if (cgrp->child_subsys_mask & (1 << ssid)) { |
| 2584 | enable &= ~(1 << ssid); |
| 2585 | continue; |
| 2586 | } |
| 2587 | |
| 2588 | /* |
| 2589 | * Because css offlining is asynchronous, userland |
| 2590 | * might try to re-enable the same controller while |
| 2591 | * the previous instance is still around. In such |
| 2592 | * cases, wait till it's gone using offline_waitq. |
| 2593 | */ |
| 2594 | cgroup_for_each_live_child(child, cgrp) { |
Tejun Heo | 0cee8b7 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2595 | DEFINE_WAIT(wait); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2596 | |
| 2597 | if (!cgroup_css(child, ss)) |
| 2598 | continue; |
| 2599 | |
Tejun Heo | 0cee8b7 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2600 | cgroup_get(child); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2601 | prepare_to_wait(&child->offline_waitq, &wait, |
| 2602 | TASK_UNINTERRUPTIBLE); |
| 2603 | mutex_unlock(&cgroup_tree_mutex); |
| 2604 | schedule(); |
| 2605 | finish_wait(&child->offline_waitq, &wait); |
Tejun Heo | 0cee8b7 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2606 | cgroup_put(child); |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2607 | |
| 2608 | ret = restart_syscall(); |
| 2609 | goto out_unbreak; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | /* unavailable or not enabled on the parent? */ |
| 2613 | if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) || |
| 2614 | (cgrp->parent && |
| 2615 | !(cgrp->parent->child_subsys_mask & (1 << ssid)))) { |
| 2616 | ret = -ENOENT; |
| 2617 | goto out_unlock_tree; |
| 2618 | } |
| 2619 | } else if (disable & (1 << ssid)) { |
| 2620 | if (!(cgrp->child_subsys_mask & (1 << ssid))) { |
| 2621 | disable &= ~(1 << ssid); |
| 2622 | continue; |
| 2623 | } |
| 2624 | |
| 2625 | /* a child has it enabled? */ |
| 2626 | cgroup_for_each_live_child(child, cgrp) { |
| 2627 | if (child->child_subsys_mask & (1 << ssid)) { |
| 2628 | ret = -EBUSY; |
| 2629 | goto out_unlock_tree; |
| 2630 | } |
| 2631 | } |
| 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | if (!enable && !disable) { |
| 2636 | ret = 0; |
| 2637 | goto out_unlock_tree; |
| 2638 | } |
| 2639 | |
| 2640 | if (!cgroup_lock_live_group(cgrp)) { |
| 2641 | ret = -ENODEV; |
| 2642 | goto out_unlock_tree; |
| 2643 | } |
| 2644 | |
| 2645 | /* |
| 2646 | * Except for the root, child_subsys_mask must be zero for a cgroup |
| 2647 | * with tasks so that child cgroups don't compete against tasks. |
| 2648 | */ |
| 2649 | if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) { |
| 2650 | ret = -EBUSY; |
| 2651 | goto out_unlock; |
| 2652 | } |
| 2653 | |
| 2654 | /* |
| 2655 | * Create csses for enables and update child_subsys_mask. This |
| 2656 | * changes cgroup_e_css() results which in turn makes the |
| 2657 | * subsequent cgroup_update_dfl_csses() associate all tasks in the |
| 2658 | * subtree to the updated csses. |
| 2659 | */ |
| 2660 | for_each_subsys(ss, ssid) { |
| 2661 | if (!(enable & (1 << ssid))) |
| 2662 | continue; |
| 2663 | |
| 2664 | cgroup_for_each_live_child(child, cgrp) { |
| 2665 | ret = create_css(child, ss); |
| 2666 | if (ret) |
| 2667 | goto err_undo_css; |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | cgrp->child_subsys_mask |= enable; |
| 2672 | cgrp->child_subsys_mask &= ~disable; |
| 2673 | |
| 2674 | ret = cgroup_update_dfl_csses(cgrp); |
| 2675 | if (ret) |
| 2676 | goto err_undo_css; |
| 2677 | |
| 2678 | /* all tasks are now migrated away from the old csses, kill them */ |
| 2679 | for_each_subsys(ss, ssid) { |
| 2680 | if (!(disable & (1 << ssid))) |
| 2681 | continue; |
| 2682 | |
| 2683 | cgroup_for_each_live_child(child, cgrp) |
| 2684 | kill_css(cgroup_css(child, ss)); |
| 2685 | } |
| 2686 | |
| 2687 | kernfs_activate(cgrp->kn); |
| 2688 | ret = 0; |
| 2689 | out_unlock: |
| 2690 | mutex_unlock(&cgroup_mutex); |
| 2691 | out_unlock_tree: |
| 2692 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2693 | out_unbreak: |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2694 | kernfs_unbreak_active_protection(cgrp->control_kn); |
| 2695 | cgroup_put(cgrp); |
| 2696 | return ret; |
| 2697 | |
| 2698 | err_undo_css: |
| 2699 | cgrp->child_subsys_mask &= ~enable; |
| 2700 | cgrp->child_subsys_mask |= disable; |
| 2701 | |
| 2702 | for_each_subsys(ss, ssid) { |
| 2703 | if (!(enable & (1 << ssid))) |
| 2704 | continue; |
| 2705 | |
| 2706 | cgroup_for_each_live_child(child, cgrp) { |
| 2707 | struct cgroup_subsys_state *css = cgroup_css(child, ss); |
| 2708 | if (css) |
| 2709 | kill_css(css); |
| 2710 | } |
| 2711 | } |
| 2712 | goto out_unlock; |
| 2713 | } |
| 2714 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 2715 | static int cgroup_populated_show(struct seq_file *seq, void *v) |
| 2716 | { |
| 2717 | seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt); |
| 2718 | return 0; |
| 2719 | } |
| 2720 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2721 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 2722 | size_t nbytes, loff_t off) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2723 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2724 | struct cgroup *cgrp = of->kn->parent->priv; |
| 2725 | struct cftype *cft = of->kn->priv; |
| 2726 | struct cgroup_subsys_state *css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2727 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2728 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2729 | /* |
| 2730 | * kernfs guarantees that a file isn't deleted with operations in |
| 2731 | * flight, which means that the matching css is and stays alive and |
| 2732 | * doesn't need to be pinned. The RCU locking is not necessary |
| 2733 | * either. It's just for the convenience of using cgroup_css(). |
| 2734 | */ |
| 2735 | rcu_read_lock(); |
| 2736 | css = cgroup_css(cgrp, cft->ss); |
| 2737 | rcu_read_unlock(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2738 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2739 | if (cft->write_string) { |
| 2740 | ret = cft->write_string(css, cft, strstrip(buf)); |
| 2741 | } else if (cft->write_u64) { |
| 2742 | unsigned long long v; |
| 2743 | ret = kstrtoull(buf, 0, &v); |
| 2744 | if (!ret) |
| 2745 | ret = cft->write_u64(css, cft, v); |
| 2746 | } else if (cft->write_s64) { |
| 2747 | long long v; |
| 2748 | ret = kstrtoll(buf, 0, &v); |
| 2749 | if (!ret) |
| 2750 | ret = cft->write_s64(css, cft, v); |
| 2751 | } else if (cft->trigger) { |
| 2752 | ret = cft->trigger(css, (unsigned int)cft->private); |
| 2753 | } else { |
| 2754 | ret = -EINVAL; |
| 2755 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2756 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2757 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2760 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2761 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2762 | return seq_cft(seq)->seq_start(seq, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 2766 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2767 | return seq_cft(seq)->seq_next(seq, v, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 2771 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2772 | seq_cft(seq)->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2776 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2777 | struct cftype *cft = seq_cft(m); |
| 2778 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2779 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2780 | if (cft->seq_show) |
| 2781 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2782 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2783 | if (cft->read_u64) |
| 2784 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 2785 | else if (cft->read_s64) |
| 2786 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 2787 | else |
| 2788 | return -EINVAL; |
| 2789 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2790 | } |
| 2791 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2792 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 2793 | .atomic_write_len = PAGE_SIZE, |
| 2794 | .write = cgroup_file_write, |
| 2795 | .seq_show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2796 | }; |
| 2797 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2798 | static struct kernfs_ops cgroup_kf_ops = { |
| 2799 | .atomic_write_len = PAGE_SIZE, |
| 2800 | .write = cgroup_file_write, |
| 2801 | .seq_start = cgroup_seqfile_start, |
| 2802 | .seq_next = cgroup_seqfile_next, |
| 2803 | .seq_stop = cgroup_seqfile_stop, |
| 2804 | .seq_show = cgroup_seqfile_show, |
| 2805 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2806 | |
| 2807 | /* |
| 2808 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2809 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2810 | static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent, |
| 2811 | const char *new_name_str) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2812 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2813 | struct cgroup *cgrp = kn->priv; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2814 | int ret; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2815 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2816 | if (kernfs_type(kn) != KERNFS_DIR) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2817 | return -ENOTDIR; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2818 | if (kn->parent != new_parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2819 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2820 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2821 | /* |
| 2822 | * This isn't a proper migration and its usefulness is very |
| 2823 | * limited. Disallow if sane_behavior. |
| 2824 | */ |
| 2825 | if (cgroup_sane_behavior(cgrp)) |
| 2826 | return -EPERM; |
| 2827 | |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 2828 | /* |
| 2829 | * We're gonna grab cgroup_tree_mutex which nests outside kernfs |
| 2830 | * active_ref. kernfs_rename() doesn't require active_ref |
| 2831 | * protection. Break them before grabbing cgroup_tree_mutex. |
| 2832 | */ |
| 2833 | kernfs_break_active_protection(new_parent); |
| 2834 | kernfs_break_active_protection(kn); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2835 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2836 | mutex_lock(&cgroup_tree_mutex); |
| 2837 | mutex_lock(&cgroup_mutex); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2838 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2839 | ret = kernfs_rename(kn, new_parent, new_name_str); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2840 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2841 | mutex_unlock(&cgroup_mutex); |
| 2842 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2843 | |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 2844 | kernfs_unbreak_active_protection(kn); |
| 2845 | kernfs_unbreak_active_protection(new_parent); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2846 | return ret; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2847 | } |
| 2848 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2849 | /* set uid and gid of cgroup dirs and files to that of the creator */ |
| 2850 | static int cgroup_kn_set_ugid(struct kernfs_node *kn) |
| 2851 | { |
| 2852 | struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, |
| 2853 | .ia_uid = current_fsuid(), |
| 2854 | .ia_gid = current_fsgid(), }; |
| 2855 | |
| 2856 | if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && |
| 2857 | gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) |
| 2858 | return 0; |
| 2859 | |
| 2860 | return kernfs_setattr(kn, &iattr); |
| 2861 | } |
| 2862 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2863 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2864 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2865 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2866 | struct kernfs_node *kn; |
| 2867 | struct lock_class_key *key = NULL; |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2868 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2869 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2870 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 2871 | key = &cft->lockdep_key; |
| 2872 | #endif |
| 2873 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
| 2874 | cgroup_file_mode(cft), 0, cft->kf_ops, cft, |
| 2875 | NULL, false, key); |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2876 | if (IS_ERR(kn)) |
| 2877 | return PTR_ERR(kn); |
| 2878 | |
| 2879 | ret = cgroup_kn_set_ugid(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2880 | if (ret) { |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2881 | kernfs_remove(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2882 | return ret; |
| 2883 | } |
| 2884 | |
| 2885 | if (cft->seq_show == cgroup_subtree_control_show) |
| 2886 | cgrp->control_kn = kn; |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 2887 | else if (cft->seq_show == cgroup_populated_show) |
| 2888 | cgrp->populated_kn = kn; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2889 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2890 | } |
| 2891 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2892 | /** |
| 2893 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2894 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2895 | * @cfts: array of cftypes to be added |
| 2896 | * @is_add: whether to add or remove |
| 2897 | * |
| 2898 | * 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] | 2899 | * For removals, this function never fails. If addition fails, this |
| 2900 | * function doesn't remove files already added. The caller is responsible |
| 2901 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2902 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2903 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2904 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2905 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2906 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2907 | int ret; |
| 2908 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2909 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2910 | |
| 2911 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2912 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 8cbbf2c | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 2913 | if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) |
| 2914 | continue; |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2915 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2916 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2917 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2918 | continue; |
| 2919 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2920 | continue; |
| 2921 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2922 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2923 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2924 | if (ret) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 2925 | pr_warn("%s: failed to add %s, err=%d\n", |
| 2926 | __func__, cft->name, ret); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2927 | return ret; |
| 2928 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2929 | } else { |
| 2930 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2931 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2932 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2933 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2936 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2937 | { |
| 2938 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2939 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2940 | struct cgroup *root = &ss->root->cgrp; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2941 | struct cgroup_subsys_state *css; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2942 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2943 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2944 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2945 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2946 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2947 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2948 | struct cgroup *cgrp = css->cgroup; |
| 2949 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2950 | if (cgroup_is_dead(cgrp)) |
| 2951 | continue; |
| 2952 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2953 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2954 | if (ret) |
| 2955 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2956 | } |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2957 | |
| 2958 | if (is_add && !ret) |
| 2959 | kernfs_activate(root->kn); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2960 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2961 | } |
| 2962 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2963 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 2964 | { |
| 2965 | struct cftype *cft; |
| 2966 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2967 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2968 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 2969 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 2970 | kfree(cft->kf_ops); |
| 2971 | cft->kf_ops = NULL; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2972 | cft->ss = NULL; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2973 | } |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2974 | } |
| 2975 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2976 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2977 | { |
| 2978 | struct cftype *cft; |
| 2979 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2980 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2981 | struct kernfs_ops *kf_ops; |
| 2982 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2983 | WARN_ON(cft->ss || cft->kf_ops); |
| 2984 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2985 | if (cft->seq_start) |
| 2986 | kf_ops = &cgroup_kf_ops; |
| 2987 | else |
| 2988 | kf_ops = &cgroup_kf_single_ops; |
| 2989 | |
| 2990 | /* |
| 2991 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 2992 | * make a copy of kf_ops to set its atomic_write_len. |
| 2993 | */ |
| 2994 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 2995 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 2996 | if (!kf_ops) { |
| 2997 | cgroup_exit_cftypes(cfts); |
| 2998 | return -ENOMEM; |
| 2999 | } |
| 3000 | kf_ops->atomic_write_len = cft->max_write_len; |
| 3001 | } |
| 3002 | |
| 3003 | cft->kf_ops = kf_ops; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3004 | cft->ss = ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | return 0; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3008 | } |
| 3009 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3010 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 3011 | { |
| 3012 | lockdep_assert_held(&cgroup_tree_mutex); |
| 3013 | |
| 3014 | if (!cfts || !cfts[0].ss) |
| 3015 | return -ENOENT; |
| 3016 | |
| 3017 | list_del(&cfts->node); |
| 3018 | cgroup_apply_cftypes(cfts, false); |
| 3019 | cgroup_exit_cftypes(cfts); |
| 3020 | return 0; |
| 3021 | } |
| 3022 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3023 | /** |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3024 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 3025 | * @cfts: zero-length name terminated array of cftypes |
| 3026 | * |
| 3027 | * Unregister @cfts. Files described by @cfts are removed from all |
| 3028 | * existing cgroups and all future cgroups won't have them either. This |
| 3029 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 3030 | * |
| 3031 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 3032 | * registered. |
| 3033 | */ |
| 3034 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 3035 | { |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3036 | int ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3037 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3038 | mutex_lock(&cgroup_tree_mutex); |
| 3039 | ret = cgroup_rm_cftypes_locked(cfts); |
| 3040 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3041 | return ret; |
| 3042 | } |
| 3043 | |
| 3044 | /** |
| 3045 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 3046 | * @ss: target cgroup subsystem |
| 3047 | * @cfts: zero-length name terminated array of cftypes |
| 3048 | * |
| 3049 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 3050 | * existing cgroups to which @ss is attached and all future cgroups will |
| 3051 | * have them too. This function can be called anytime whether @ss is |
| 3052 | * attached or not. |
| 3053 | * |
| 3054 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 3055 | * function currently returns 0 as long as @cfts registration is successful |
| 3056 | * even if some file creation attempts on existing cgroups fail. |
| 3057 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 3058 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3059 | { |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3060 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3061 | |
Li Zefan | dc5736e | 2014-02-17 10:41:50 +0800 | [diff] [blame] | 3062 | if (!cfts || cfts[0].name[0] == '\0') |
| 3063 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3064 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3065 | ret = cgroup_init_cftypes(ss, cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3066 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3067 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3068 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3069 | mutex_lock(&cgroup_tree_mutex); |
| 3070 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3071 | list_add_tail(&cfts->node, &ss->cfts); |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3072 | ret = cgroup_apply_cftypes(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3073 | if (ret) |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3074 | cgroup_rm_cftypes_locked(cfts); |
| 3075 | |
| 3076 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3077 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3078 | } |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3079 | |
| 3080 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3081 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 3082 | * @cgrp: the cgroup in question |
| 3083 | * |
| 3084 | * Return the number of tasks in the cgroup. |
| 3085 | */ |
Tejun Heo | 07bc356 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3086 | static int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3087 | { |
| 3088 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3089 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3090 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3091 | down_read(&css_set_rwsem); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3092 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 3093 | count += atomic_read(&link->cset->refcount); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3094 | up_read(&css_set_rwsem); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3095 | return count; |
| 3096 | } |
| 3097 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3098 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3099 | * css_next_child - find the next child of a given css |
| 3100 | * @pos_css: the current position (%NULL to initiate traversal) |
| 3101 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3102 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3103 | * 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] | 3104 | * under either cgroup_mutex or RCU read lock. The only requirement is |
| 3105 | * that @parent_css and @pos_css are accessible. The next sibling is |
| 3106 | * guaranteed to be returned regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3107 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3108 | struct cgroup_subsys_state * |
| 3109 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 3110 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3111 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3112 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 3113 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3114 | struct cgroup *next; |
| 3115 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3116 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3117 | |
| 3118 | /* |
| 3119 | * @pos could already have been removed. Once a cgroup is removed, |
| 3120 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3121 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 3122 | * before the cgroup is taken off the ->sibling list, if we see it |
| 3123 | * unasserted, it's guaranteed that the next sibling hasn't |
| 3124 | * finished its grace period even if it's already removed, and thus |
| 3125 | * safe to dereference from this RCU critical section. If |
| 3126 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 3127 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3128 | * |
| 3129 | * If @pos is dead, its next pointer can't be dereferenced; |
| 3130 | * however, as each cgroup is given a monotonically increasing |
| 3131 | * unique serial number and always appended to the sibling list, |
| 3132 | * the next one can be found by walking the parent's children until |
| 3133 | * we see a cgroup with higher serial number than @pos's. While |
| 3134 | * this path can be slower, it's taken only when either the current |
| 3135 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3136 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3137 | if (!pos) { |
| 3138 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 3139 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3140 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3141 | } else { |
| 3142 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 3143 | if (next->serial_nr > pos->serial_nr) |
| 3144 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3145 | } |
| 3146 | |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3147 | /* |
| 3148 | * @next, if not pointing to the head, can be dereferenced and is |
| 3149 | * the next sibling; however, it might have @ss disabled. If so, |
| 3150 | * fast-forward to the next enabled one. |
| 3151 | */ |
| 3152 | while (&next->sibling != &cgrp->children) { |
| 3153 | struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss); |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3154 | |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3155 | if (next_css) |
| 3156 | return next_css; |
| 3157 | next = list_entry_rcu(next->sibling.next, struct cgroup, sibling); |
| 3158 | } |
| 3159 | return NULL; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3160 | } |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3161 | |
| 3162 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3163 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3164 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3165 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3166 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3167 | * 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] | 3168 | * to visit for pre-order traversal of @root's descendants. @root is |
| 3169 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3170 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3171 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3172 | * doesn't require the whole traversal to be contained in a single critical |
| 3173 | * section. This function will return the correct next descendant as long |
| 3174 | * 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] | 3175 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3176 | struct cgroup_subsys_state * |
| 3177 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 3178 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3179 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3180 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3181 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3182 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3183 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3184 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3185 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3186 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3187 | |
| 3188 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3189 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3190 | if (next) |
| 3191 | return next; |
| 3192 | |
| 3193 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3194 | while (pos != root) { |
| 3195 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3196 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3197 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3198 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3199 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3200 | |
| 3201 | return NULL; |
| 3202 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3203 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3204 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3205 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 3206 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3207 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3208 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 3209 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3210 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3211 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3212 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3213 | * doesn't require the whole traversal to be contained in a single critical |
| 3214 | * section. This function will return the correct rightmost descendant as |
| 3215 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3216 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3217 | struct cgroup_subsys_state * |
| 3218 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3219 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3220 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3221 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3222 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3223 | |
| 3224 | do { |
| 3225 | last = pos; |
| 3226 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3227 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3228 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3229 | pos = tmp; |
| 3230 | } while (pos); |
| 3231 | |
| 3232 | return last; |
| 3233 | } |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3234 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3235 | static struct cgroup_subsys_state * |
| 3236 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3237 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3238 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3239 | |
| 3240 | do { |
| 3241 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3242 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3243 | } while (pos); |
| 3244 | |
| 3245 | return last; |
| 3246 | } |
| 3247 | |
| 3248 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3249 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3250 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3251 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3252 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3253 | * 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] | 3254 | * to visit for post-order traversal of @root's descendants. @root is |
| 3255 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3256 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3257 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3258 | * doesn't require the whole traversal to be contained in a single critical |
| 3259 | * section. This function will return the correct next descendant as long |
| 3260 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 3261 | * @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3262 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3263 | struct cgroup_subsys_state * |
| 3264 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 3265 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3266 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3267 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3268 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3269 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3270 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 3271 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 3272 | if (!pos) |
| 3273 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3274 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3275 | /* if we visited @root, we're done */ |
| 3276 | if (pos == root) |
| 3277 | return NULL; |
| 3278 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3279 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3280 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3281 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3282 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3283 | |
| 3284 | /* no sibling left, visit parent */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3285 | return css_parent(pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3286 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3287 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3288 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3289 | * 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] | 3290 | * @it: the iterator to advance |
| 3291 | * |
| 3292 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3293 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3294 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3295 | { |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3296 | struct list_head *l = it->cset_pos; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3297 | struct cgrp_cset_link *link; |
| 3298 | struct css_set *cset; |
| 3299 | |
| 3300 | /* Advance to the next non-empty css_set */ |
| 3301 | do { |
| 3302 | l = l->next; |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3303 | if (l == it->cset_head) { |
| 3304 | it->cset_pos = NULL; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3305 | return; |
| 3306 | } |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3307 | |
| 3308 | if (it->ss) { |
| 3309 | cset = container_of(l, struct css_set, |
| 3310 | e_cset_node[it->ss->id]); |
| 3311 | } else { |
| 3312 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3313 | cset = link->cset; |
| 3314 | } |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3315 | } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks)); |
| 3316 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3317 | it->cset_pos = l; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3318 | |
| 3319 | if (!list_empty(&cset->tasks)) |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3320 | it->task_pos = cset->tasks.next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3321 | else |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3322 | it->task_pos = cset->mg_tasks.next; |
| 3323 | |
| 3324 | it->tasks_head = &cset->tasks; |
| 3325 | it->mg_tasks_head = &cset->mg_tasks; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3326 | } |
| 3327 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3328 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3329 | * css_task_iter_start - initiate task iteration |
| 3330 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3331 | * @it: the task iterator to use |
| 3332 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3333 | * Initiate iteration through the tasks of @css. The caller can call |
| 3334 | * css_task_iter_next() to walk through the tasks until the function |
| 3335 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 3336 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3337 | * |
| 3338 | * Note that this function acquires a lock which is released when the |
| 3339 | * iteration finishes. The caller can't sleep while iteration is in |
| 3340 | * progress. |
| 3341 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3342 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 3343 | struct css_task_iter *it) |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3344 | __acquires(css_set_rwsem) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3345 | { |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 3346 | /* no one should try to iterate before mounting cgroups */ |
| 3347 | WARN_ON_ONCE(!use_task_css_set_links); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3348 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3349 | down_read(&css_set_rwsem); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3350 | |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3351 | it->ss = css->ss; |
| 3352 | |
| 3353 | if (it->ss) |
| 3354 | it->cset_pos = &css->cgroup->e_csets[css->ss->id]; |
| 3355 | else |
| 3356 | it->cset_pos = &css->cgroup->cset_links; |
| 3357 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3358 | it->cset_head = it->cset_pos; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3359 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3360 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3361 | } |
| 3362 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3363 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3364 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3365 | * @it: the task iterator being iterated |
| 3366 | * |
| 3367 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3368 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 3369 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3370 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3371 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3372 | { |
| 3373 | struct task_struct *res; |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3374 | struct list_head *l = it->task_pos; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3375 | |
| 3376 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3377 | if (!it->cset_pos) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3378 | return NULL; |
| 3379 | res = list_entry(l, struct task_struct, cg_list); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3380 | |
| 3381 | /* |
| 3382 | * Advance iterator to find next entry. cset->tasks is consumed |
| 3383 | * first and then ->mg_tasks. After ->mg_tasks, we move onto the |
| 3384 | * next cset. |
| 3385 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3386 | l = l->next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3387 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3388 | if (l == it->tasks_head) |
| 3389 | l = it->mg_tasks_head->next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3390 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3391 | if (l == it->mg_tasks_head) |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3392 | css_advance_task_iter(it); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3393 | else |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3394 | it->task_pos = l; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3395 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3396 | return res; |
| 3397 | } |
| 3398 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3399 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3400 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3401 | * @it: the task iterator to finish |
| 3402 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3403 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3404 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3405 | void css_task_iter_end(struct css_task_iter *it) |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3406 | __releases(css_set_rwsem) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3407 | { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3408 | up_read(&css_set_rwsem); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | /** |
| 3412 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3413 | * @to: cgroup to which the tasks will be moved |
| 3414 | * @from: cgroup in which the tasks currently reside |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3415 | * |
| 3416 | * Locking rules between cgroup_post_fork() and the migration path |
| 3417 | * guarantee that, if a task is forking while being migrated, the new child |
| 3418 | * is guaranteed to be either visible in the source cgroup after the |
| 3419 | * parent's migration is complete or put into the target cgroup. No task |
| 3420 | * can slip out of migration through forking. |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3421 | */ |
| 3422 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3423 | { |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3424 | LIST_HEAD(preloaded_csets); |
| 3425 | struct cgrp_cset_link *link; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3426 | struct css_task_iter it; |
| 3427 | struct task_struct *task; |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3428 | int ret; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3429 | |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3430 | mutex_lock(&cgroup_mutex); |
| 3431 | |
| 3432 | /* all tasks in @from are being moved, all csets are source */ |
| 3433 | down_read(&css_set_rwsem); |
| 3434 | list_for_each_entry(link, &from->cset_links, cset_link) |
| 3435 | cgroup_migrate_add_src(link->cset, to, &preloaded_csets); |
| 3436 | up_read(&css_set_rwsem); |
| 3437 | |
| 3438 | ret = cgroup_migrate_prepare_dst(to, &preloaded_csets); |
| 3439 | if (ret) |
| 3440 | goto out_err; |
| 3441 | |
| 3442 | /* |
| 3443 | * Migrate tasks one-by-one until @form is empty. This fails iff |
| 3444 | * ->can_attach() fails. |
| 3445 | */ |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3446 | do { |
| 3447 | css_task_iter_start(&from->dummy_css, &it); |
| 3448 | task = css_task_iter_next(&it); |
| 3449 | if (task) |
| 3450 | get_task_struct(task); |
| 3451 | css_task_iter_end(&it); |
| 3452 | |
| 3453 | if (task) { |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3454 | ret = cgroup_migrate(to, task, false); |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3455 | put_task_struct(task); |
| 3456 | } |
| 3457 | } while (task && !ret); |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3458 | out_err: |
| 3459 | cgroup_migrate_finish(&preloaded_csets); |
| 3460 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3461 | return ret; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3464 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3465 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3466 | * |
| 3467 | * Reading this file can return large amounts of data if a cgroup has |
| 3468 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3469 | * but we cannot guarantee that the information we produce is correct |
| 3470 | * unless we produce it entirely atomically. |
| 3471 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3472 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3473 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3474 | /* which pidlist file are we talking about? */ |
| 3475 | enum cgroup_filetype { |
| 3476 | CGROUP_FILE_PROCS, |
| 3477 | CGROUP_FILE_TASKS, |
| 3478 | }; |
| 3479 | |
| 3480 | /* |
| 3481 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3482 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3483 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3484 | * to the cgroup. |
| 3485 | */ |
| 3486 | struct cgroup_pidlist { |
| 3487 | /* |
| 3488 | * used to find which pidlist is wanted. doesn't change as long as |
| 3489 | * this particular list stays in the list. |
| 3490 | */ |
| 3491 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3492 | /* array of xids */ |
| 3493 | pid_t *list; |
| 3494 | /* how many elements the above list has */ |
| 3495 | int length; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3496 | /* each of these stored in a list by its cgroup */ |
| 3497 | struct list_head links; |
| 3498 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3499 | struct cgroup *owner; |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3500 | /* for delayed destruction */ |
| 3501 | struct delayed_work destroy_dwork; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3502 | }; |
| 3503 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3504 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3505 | * The following two functions "fix" the issue where there are more pids |
| 3506 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3507 | * TODO: replace with a kernel-wide solution to this problem |
| 3508 | */ |
| 3509 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3510 | static void *pidlist_allocate(int count) |
| 3511 | { |
| 3512 | if (PIDLIST_TOO_LARGE(count)) |
| 3513 | return vmalloc(count * sizeof(pid_t)); |
| 3514 | else |
| 3515 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3516 | } |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3517 | |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3518 | static void pidlist_free(void *p) |
| 3519 | { |
| 3520 | if (is_vmalloc_addr(p)) |
| 3521 | vfree(p); |
| 3522 | else |
| 3523 | kfree(p); |
| 3524 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3525 | |
| 3526 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3527 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 3528 | * should be left afterwards. |
| 3529 | */ |
| 3530 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 3531 | { |
| 3532 | struct cgroup_pidlist *l, *tmp_l; |
| 3533 | |
| 3534 | mutex_lock(&cgrp->pidlist_mutex); |
| 3535 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 3536 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 3537 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3538 | |
| 3539 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 3540 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 3541 | } |
| 3542 | |
| 3543 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 3544 | { |
| 3545 | struct delayed_work *dwork = to_delayed_work(work); |
| 3546 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 3547 | destroy_dwork); |
| 3548 | struct cgroup_pidlist *tofree = NULL; |
| 3549 | |
| 3550 | mutex_lock(&l->owner->pidlist_mutex); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3551 | |
| 3552 | /* |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3553 | * Destroy iff we didn't get queued again. The state won't change |
| 3554 | * as destroy_dwork can only be queued while locked. |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3555 | */ |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3556 | if (!delayed_work_pending(dwork)) { |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3557 | list_del(&l->links); |
| 3558 | pidlist_free(l->list); |
| 3559 | put_pid_ns(l->key.ns); |
| 3560 | tofree = l; |
| 3561 | } |
| 3562 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3563 | mutex_unlock(&l->owner->pidlist_mutex); |
| 3564 | kfree(tofree); |
| 3565 | } |
| 3566 | |
| 3567 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3568 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3569 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3570 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3571 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3572 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3573 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3574 | |
| 3575 | /* |
| 3576 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3577 | * edge cases first; no work needs to be done for either |
| 3578 | */ |
| 3579 | if (length == 0 || length == 1) |
| 3580 | return length; |
| 3581 | /* src and dest walk down the list; dest counts unique elements */ |
| 3582 | for (src = 1; src < length; src++) { |
| 3583 | /* find next unique element */ |
| 3584 | while (list[src] == list[src-1]) { |
| 3585 | src++; |
| 3586 | if (src == length) |
| 3587 | goto after; |
| 3588 | } |
| 3589 | /* dest always points to where the next unique element goes */ |
| 3590 | list[dest] = list[src]; |
| 3591 | dest++; |
| 3592 | } |
| 3593 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3594 | return dest; |
| 3595 | } |
| 3596 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3597 | /* |
| 3598 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 3599 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 3600 | * different per namespace, each namespace needs differently sorted list, |
| 3601 | * making it impossible to use, for example, single rbtree of member tasks |
| 3602 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 3603 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 3604 | * pidlists keyed by cgroup and namespace. |
| 3605 | * |
| 3606 | * All this extra complexity was caused by the original implementation |
| 3607 | * committing to an entirely unnecessary property. In the long term, we |
| 3608 | * want to do away with it. Explicitly scramble sort order if |
| 3609 | * sane_behavior so that no such expectation exists in the new interface. |
| 3610 | * |
| 3611 | * Scrambling is done by swapping every two consecutive bits, which is |
| 3612 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 3613 | */ |
| 3614 | static pid_t pid_fry(pid_t pid) |
| 3615 | { |
| 3616 | unsigned a = pid & 0x55555555; |
| 3617 | unsigned b = pid & 0xAAAAAAAA; |
| 3618 | |
| 3619 | return (a << 1) | (b >> 1); |
| 3620 | } |
| 3621 | |
| 3622 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 3623 | { |
| 3624 | if (cgroup_sane_behavior(cgrp)) |
| 3625 | return pid_fry(pid); |
| 3626 | else |
| 3627 | return pid; |
| 3628 | } |
| 3629 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3630 | static int cmppid(const void *a, const void *b) |
| 3631 | { |
| 3632 | return *(pid_t *)a - *(pid_t *)b; |
| 3633 | } |
| 3634 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3635 | static int fried_cmppid(const void *a, const void *b) |
| 3636 | { |
| 3637 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 3638 | } |
| 3639 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3640 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3641 | enum cgroup_filetype type) |
| 3642 | { |
| 3643 | struct cgroup_pidlist *l; |
| 3644 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3645 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3646 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3647 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3648 | |
| 3649 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 3650 | if (l->key.type == type && l->key.ns == ns) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3651 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3652 | return NULL; |
| 3653 | } |
| 3654 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3655 | /* |
| 3656 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3657 | * returns with the lock on that pidlist already held, and takes care |
| 3658 | * of the use count, or returns NULL with no locks held if we're out of |
| 3659 | * memory. |
| 3660 | */ |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3661 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 3662 | enum cgroup_filetype type) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3663 | { |
| 3664 | struct cgroup_pidlist *l; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3665 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3666 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3667 | |
| 3668 | l = cgroup_pidlist_find(cgrp, type); |
| 3669 | if (l) |
| 3670 | return l; |
| 3671 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3672 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3673 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3674 | if (!l) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3675 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3676 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3677 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3678 | l->key.type = type; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3679 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 3680 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3681 | l->owner = cgrp; |
| 3682 | list_add(&l->links, &cgrp->pidlists); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3683 | return l; |
| 3684 | } |
| 3685 | |
| 3686 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3687 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3688 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3689 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3690 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3691 | { |
| 3692 | pid_t *array; |
| 3693 | int length; |
| 3694 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3695 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3696 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3697 | struct cgroup_pidlist *l; |
| 3698 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3699 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3700 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3701 | /* |
| 3702 | * If cgroup gets more users after we read count, we won't have |
| 3703 | * enough space - tough. This race is indistinguishable to the |
| 3704 | * caller from the case that the additional cgroup users didn't |
| 3705 | * show up until sometime later on. |
| 3706 | */ |
| 3707 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3708 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3709 | if (!array) |
| 3710 | return -ENOMEM; |
| 3711 | /* now, populate the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3712 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3713 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3714 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3715 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3716 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3717 | if (type == CGROUP_FILE_PROCS) |
| 3718 | pid = task_tgid_vnr(tsk); |
| 3719 | else |
| 3720 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3721 | if (pid > 0) /* make sure to only use valid results */ |
| 3722 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3723 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3724 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3725 | length = n; |
| 3726 | /* now sort & (if procs) strip out duplicates */ |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3727 | if (cgroup_sane_behavior(cgrp)) |
| 3728 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 3729 | else |
| 3730 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3731 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3732 | length = pidlist_uniq(array, length); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3733 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3734 | l = cgroup_pidlist_find_create(cgrp, type); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3735 | if (!l) { |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3736 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3737 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3738 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3739 | } |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3740 | |
| 3741 | /* store array, freeing old if necessary */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3742 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3743 | l->list = array; |
| 3744 | l->length = length; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3745 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3746 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3747 | } |
| 3748 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3749 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3750 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3751 | * @stats: cgroupstats to fill information into |
| 3752 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3753 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3754 | * |
| 3755 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3756 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3757 | */ |
| 3758 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3759 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3760 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3761 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3762 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3763 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3764 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3765 | /* it should be kernfs_node belonging to cgroupfs and is a directory */ |
| 3766 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 3767 | kernfs_type(kn) != KERNFS_DIR) |
| 3768 | return -EINVAL; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3769 | |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3770 | mutex_lock(&cgroup_mutex); |
| 3771 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3772 | /* |
| 3773 | * We aren't being called from kernfs and there's no guarantee on |
| 3774 | * @kn->priv's validity. For this and css_tryget_from_dir(), |
| 3775 | * @kn->priv is RCU safe. Let's do the RCU dancing. |
| 3776 | */ |
| 3777 | rcu_read_lock(); |
| 3778 | cgrp = rcu_dereference(kn->priv); |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3779 | if (!cgrp || cgroup_is_dead(cgrp)) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3780 | rcu_read_unlock(); |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3781 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3782 | return -ENOENT; |
| 3783 | } |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3784 | rcu_read_unlock(); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3785 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3786 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3787 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3788 | switch (tsk->state) { |
| 3789 | case TASK_RUNNING: |
| 3790 | stats->nr_running++; |
| 3791 | break; |
| 3792 | case TASK_INTERRUPTIBLE: |
| 3793 | stats->nr_sleeping++; |
| 3794 | break; |
| 3795 | case TASK_UNINTERRUPTIBLE: |
| 3796 | stats->nr_uninterruptible++; |
| 3797 | break; |
| 3798 | case TASK_STOPPED: |
| 3799 | stats->nr_stopped++; |
| 3800 | break; |
| 3801 | default: |
| 3802 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3803 | stats->nr_io_wait++; |
| 3804 | break; |
| 3805 | } |
| 3806 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3807 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3808 | |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3809 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3810 | return 0; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3811 | } |
| 3812 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3813 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3814 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3815 | * 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] | 3816 | * 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] | 3817 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3818 | */ |
| 3819 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3820 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3821 | { |
| 3822 | /* |
| 3823 | * Initially we receive a position value that corresponds to |
| 3824 | * one more than the last pid shown (or 0 on the first call or |
| 3825 | * after a seek to the start). Use a binary-search to find the |
| 3826 | * next pid to display, if any |
| 3827 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3828 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3829 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3830 | struct cgroup_pidlist *l; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3831 | enum cgroup_filetype type = seq_cft(s)->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3832 | int index = 0, pid = *pos; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3833 | int *iter, ret; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3834 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3835 | mutex_lock(&cgrp->pidlist_mutex); |
| 3836 | |
| 3837 | /* |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3838 | * !NULL @of->priv indicates that this isn't the first start() |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3839 | * after open. If the matching pidlist is around, we can use that. |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3840 | * 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] | 3841 | * could already have been destroyed. |
| 3842 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3843 | if (of->priv) |
| 3844 | of->priv = cgroup_pidlist_find(cgrp, type); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3845 | |
| 3846 | /* |
| 3847 | * Either this is the first start() after open or the matching |
| 3848 | * pidlist has been destroyed inbetween. Create a new one. |
| 3849 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3850 | if (!of->priv) { |
| 3851 | ret = pidlist_array_load(cgrp, type, |
| 3852 | (struct cgroup_pidlist **)&of->priv); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3853 | if (ret) |
| 3854 | return ERR_PTR(ret); |
| 3855 | } |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3856 | l = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3857 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3858 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3859 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3860 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3861 | while (index < end) { |
| 3862 | int mid = (index + end) / 2; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3863 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3864 | index = mid; |
| 3865 | break; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3866 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3867 | index = mid + 1; |
| 3868 | else |
| 3869 | end = mid; |
| 3870 | } |
| 3871 | } |
| 3872 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3873 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3874 | return NULL; |
| 3875 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3876 | iter = l->list + index; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3877 | *pos = cgroup_pid_fry(cgrp, *iter); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3878 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3879 | } |
| 3880 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3881 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3882 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3883 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3884 | struct cgroup_pidlist *l = of->priv; |
Tejun Heo | 6223685 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3885 | |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3886 | if (l) |
| 3887 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3888 | CGROUP_PIDLIST_DESTROY_DELAY); |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3889 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3890 | } |
| 3891 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3892 | 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] | 3893 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3894 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3895 | struct cgroup_pidlist *l = of->priv; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3896 | pid_t *p = v; |
| 3897 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3898 | /* |
| 3899 | * Advance to the next pid in the array. If this goes off the |
| 3900 | * end, we're done |
| 3901 | */ |
| 3902 | p++; |
| 3903 | if (p >= end) { |
| 3904 | return NULL; |
| 3905 | } else { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3906 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3907 | return p; |
| 3908 | } |
| 3909 | } |
| 3910 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3911 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3912 | { |
| 3913 | return seq_printf(s, "%d\n", *(int *)v); |
| 3914 | } |
| 3915 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3916 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3917 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3918 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3919 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3920 | } |
| 3921 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3922 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3923 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3924 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3925 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3926 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3927 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3928 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3929 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3930 | return 0; |
| 3931 | } |
| 3932 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3933 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 3934 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3935 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3936 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3937 | } |
| 3938 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3939 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 3940 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3941 | { |
| 3942 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3943 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3944 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3945 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3946 | return 0; |
| 3947 | } |
| 3948 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3949 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3950 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3951 | .name = "cgroup.procs", |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3952 | .seq_start = cgroup_pidlist_start, |
| 3953 | .seq_next = cgroup_pidlist_next, |
| 3954 | .seq_stop = cgroup_pidlist_stop, |
| 3955 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3956 | .private = CGROUP_FILE_PROCS, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3957 | .write_u64 = cgroup_procs_write, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3958 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3959 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3960 | { |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3961 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3962 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3963 | .read_u64 = cgroup_clone_children_read, |
| 3964 | .write_u64 = cgroup_clone_children_write, |
| 3965 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3966 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3967 | .name = "cgroup.sane_behavior", |
| 3968 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3969 | .seq_show = cgroup_sane_behavior_show, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3970 | }, |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3971 | { |
| 3972 | .name = "cgroup.controllers", |
| 3973 | .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT, |
| 3974 | .seq_show = cgroup_root_controllers_show, |
| 3975 | }, |
| 3976 | { |
| 3977 | .name = "cgroup.controllers", |
| 3978 | .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT, |
| 3979 | .seq_show = cgroup_controllers_show, |
| 3980 | }, |
| 3981 | { |
| 3982 | .name = "cgroup.subtree_control", |
| 3983 | .flags = CFTYPE_ONLY_ON_DFL, |
| 3984 | .seq_show = cgroup_subtree_control_show, |
| 3985 | .write_string = cgroup_subtree_control_write, |
| 3986 | }, |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3987 | { |
| 3988 | .name = "cgroup.populated", |
| 3989 | .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT, |
| 3990 | .seq_show = cgroup_populated_show, |
| 3991 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3992 | |
| 3993 | /* |
| 3994 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 3995 | * don't exist if sane_behavior. If you're depending on these, be |
| 3996 | * prepared to be burned. |
| 3997 | */ |
| 3998 | { |
| 3999 | .name = "tasks", |
| 4000 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4001 | .seq_start = cgroup_pidlist_start, |
| 4002 | .seq_next = cgroup_pidlist_next, |
| 4003 | .seq_stop = cgroup_pidlist_stop, |
| 4004 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4005 | .private = CGROUP_FILE_TASKS, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4006 | .write_u64 = cgroup_tasks_write, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4007 | .mode = S_IRUGO | S_IWUSR, |
| 4008 | }, |
| 4009 | { |
| 4010 | .name = "notify_on_release", |
| 4011 | .flags = CFTYPE_INSANE, |
| 4012 | .read_u64 = cgroup_read_notify_on_release, |
| 4013 | .write_u64 = cgroup_write_notify_on_release, |
| 4014 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4015 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4016 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4017 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4018 | .seq_show = cgroup_release_agent_show, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4019 | .write_string = cgroup_release_agent_write, |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4020 | .max_write_len = PATH_MAX - 1, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4021 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4022 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4023 | }; |
| 4024 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4025 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4026 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4027 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4028 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4029 | * |
| 4030 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4031 | */ |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4032 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4033 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4034 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4035 | int i, ret = 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4036 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4037 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4038 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4039 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4040 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4041 | if (!(subsys_mask & (1 << i))) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4042 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4043 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4044 | list_for_each_entry(cfts, &ss->cfts, node) { |
| 4045 | ret = cgroup_addrm_files(cgrp, cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4046 | if (ret < 0) |
| 4047 | goto err; |
| 4048 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4049 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4050 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4051 | err: |
| 4052 | cgroup_clear_dir(cgrp, subsys_mask); |
| 4053 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4054 | } |
| 4055 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4056 | /* |
| 4057 | * css destruction is four-stage process. |
| 4058 | * |
| 4059 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 4060 | * Implemented in kill_css(). |
| 4061 | * |
| 4062 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
| 4063 | * and thus css_tryget() is guaranteed to fail, the css can be offlined |
| 4064 | * by invoking offline_css(). After offlining, the base ref is put. |
| 4065 | * Implemented in css_killed_work_fn(). |
| 4066 | * |
| 4067 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 4068 | * accessors are inside RCU read sections. css_release() schedules the |
| 4069 | * RCU callback. |
| 4070 | * |
| 4071 | * 4. After the grace period, the css can be freed. Implemented in |
| 4072 | * css_free_work_fn(). |
| 4073 | * |
| 4074 | * It is actually hairier because both step 2 and 4 require process context |
| 4075 | * and thus involve punting to css->destroy_work adding two additional |
| 4076 | * steps to the already complex sequence. |
| 4077 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4078 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4079 | { |
| 4080 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4081 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4082 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4083 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4084 | if (css->parent) |
| 4085 | css_put(css->parent); |
| 4086 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4087 | css->ss->css_free(css); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4088 | cgroup_put(cgrp); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 4092 | { |
| 4093 | struct cgroup_subsys_state *css = |
| 4094 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 4095 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4096 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4097 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4098 | } |
| 4099 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4100 | static void css_release(struct percpu_ref *ref) |
| 4101 | { |
| 4102 | struct cgroup_subsys_state *css = |
| 4103 | container_of(ref, struct cgroup_subsys_state, refcnt); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4104 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4105 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4106 | cgroup_idr_remove(&ss->css_idr, css->id); |
| 4107 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4108 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4109 | } |
| 4110 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4111 | static void init_and_link_css(struct cgroup_subsys_state *css, |
| 4112 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4113 | { |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4114 | cgroup_get(cgrp); |
| 4115 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4116 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4117 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4118 | css->flags = 0; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4119 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4120 | if (cgrp->parent) { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4121 | css->parent = cgroup_css(cgrp->parent, ss); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4122 | css_get(css->parent); |
| 4123 | } else { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4124 | css->flags |= CSS_ROOT; |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4125 | } |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4126 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4127 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4128 | } |
| 4129 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4130 | /* 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] | 4131 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4132 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4133 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4134 | int ret = 0; |
| 4135 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4136 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4137 | lockdep_assert_held(&cgroup_mutex); |
| 4138 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4139 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4140 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4141 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4142 | css->flags |= CSS_ONLINE; |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4143 | css->cgroup->nr_css++; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4144 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4145 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4146 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4147 | } |
| 4148 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4149 | /* 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] | 4150 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4151 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4152 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4153 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4154 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4155 | lockdep_assert_held(&cgroup_mutex); |
| 4156 | |
| 4157 | if (!(css->flags & CSS_ONLINE)) |
| 4158 | return; |
| 4159 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4160 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4161 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4162 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4163 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4164 | css->cgroup->nr_css--; |
Tejun Heo | e329780 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4165 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4166 | |
| 4167 | wake_up_all(&css->cgroup->offline_waitq); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4168 | } |
| 4169 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4170 | /** |
| 4171 | * create_css - create a cgroup_subsys_state |
| 4172 | * @cgrp: the cgroup new css will be associated with |
| 4173 | * @ss: the subsys of new css |
| 4174 | * |
| 4175 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
| 4176 | * css is online and installed in @cgrp with all interface files created. |
| 4177 | * Returns 0 on success, -errno on failure. |
| 4178 | */ |
| 4179 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss) |
| 4180 | { |
| 4181 | struct cgroup *parent = cgrp->parent; |
| 4182 | struct cgroup_subsys_state *css; |
| 4183 | int err; |
| 4184 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4185 | lockdep_assert_held(&cgroup_mutex); |
| 4186 | |
| 4187 | css = ss->css_alloc(cgroup_css(parent, ss)); |
| 4188 | if (IS_ERR(css)) |
| 4189 | return PTR_ERR(css); |
| 4190 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4191 | init_and_link_css(css, ss, cgrp); |
Tejun Heo | a2bed82 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4192 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4193 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4194 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4195 | goto err_free_css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4196 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4197 | err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT); |
| 4198 | if (err < 0) |
| 4199 | goto err_free_percpu_ref; |
| 4200 | css->id = err; |
| 4201 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4202 | err = cgroup_populate_dir(cgrp, 1 << ss->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4203 | if (err) |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4204 | goto err_free_id; |
| 4205 | |
| 4206 | /* @css is ready to be brought online now, make it visible */ |
| 4207 | cgroup_idr_replace(&ss->css_idr, css, css->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4208 | |
| 4209 | err = online_css(css); |
| 4210 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4211 | goto err_clear_dir; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4212 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4213 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4214 | parent->parent) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4215 | pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n", |
Jianyu Zhan | a2a1f9e | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4216 | current->comm, current->pid, ss->name); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4217 | if (!strcmp(ss->name, "memory")) |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4218 | pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n"); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4219 | ss->warned_broken_hierarchy = true; |
| 4220 | } |
| 4221 | |
| 4222 | return 0; |
| 4223 | |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4224 | err_clear_dir: |
Linus Torvalds | 32d01dc | 2014-04-03 13:05:42 -0700 | [diff] [blame] | 4225 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4226 | err_free_id: |
| 4227 | cgroup_idr_remove(&ss->css_idr, css->id); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4228 | err_free_percpu_ref: |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4229 | percpu_ref_cancel_init(&css->refcnt); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4230 | err_free_css: |
Tejun Heo | a2bed82 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4231 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4232 | return err; |
| 4233 | } |
| 4234 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4235 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4236 | * cgroup_create - create a cgroup |
| 4237 | * @parent: cgroup that will be parent of the new cgroup |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4238 | * @name: name of the new cgroup |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4239 | * @mode: mode to set on new cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4240 | */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4241 | static long cgroup_create(struct cgroup *parent, const char *name, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4242 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4243 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4244 | struct cgroup *cgrp; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4245 | struct cgroup_root *root = parent->root; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 4246 | int ssid, err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4247 | struct cgroup_subsys *ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4248 | struct kernfs_node *kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4249 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4250 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4251 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4252 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4253 | return -ENOMEM; |
| 4254 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4255 | mutex_lock(&cgroup_tree_mutex); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4256 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4257 | /* |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4258 | * Only live parents can have children. Note that the liveliness |
| 4259 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4260 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4261 | * anyway so that locking is contained inside cgroup proper and we |
| 4262 | * don't get nasty surprises if we ever grow another caller. |
| 4263 | */ |
| 4264 | if (!cgroup_lock_live_group(parent)) { |
| 4265 | err = -ENODEV; |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4266 | goto err_unlock_tree; |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | /* |
| 4270 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4271 | * a half-baked cgroup. |
| 4272 | */ |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4273 | cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4274 | if (cgrp->id < 0) { |
| 4275 | err = -ENOMEM; |
| 4276 | goto err_unlock; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4277 | } |
| 4278 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4279 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4280 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4281 | cgrp->parent = parent; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4282 | cgrp->dummy_css.parent = &parent->dummy_css; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4283 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4284 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4285 | if (notify_on_release(parent)) |
| 4286 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4287 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4288 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4289 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4290 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4291 | /* create the directory */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4292 | kn = kernfs_create_dir(parent->kn, name, mode, cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4293 | if (IS_ERR(kn)) { |
| 4294 | err = PTR_ERR(kn); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4295 | goto err_free_id; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4296 | } |
| 4297 | cgrp->kn = kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4298 | |
Tejun Heo | 6f30558 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4299 | /* |
| 4300 | * This extra ref will be put in cgroup_free_fn() and guarantees |
| 4301 | * that @cgrp->kn is always accessible. |
| 4302 | */ |
| 4303 | kernfs_get(kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4304 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4305 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4306 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4307 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4308 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4309 | atomic_inc(&root->nr_cgrps); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4310 | cgroup_get(parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4311 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4312 | /* |
| 4313 | * @cgrp is now fully operational. If something fails after this |
| 4314 | * point, it'll be released via the normal destruction path. |
| 4315 | */ |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4316 | cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4317 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 4318 | err = cgroup_kn_set_ugid(kn); |
| 4319 | if (err) |
| 4320 | goto err_destroy; |
| 4321 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4322 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4323 | if (err) |
| 4324 | goto err_destroy; |
| 4325 | |
Tejun Heo | 9d403e9 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4326 | /* let's create and online css's */ |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4327 | for_each_subsys(ss, ssid) { |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4328 | if (parent->child_subsys_mask & (1 << ssid)) { |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4329 | err = create_css(cgrp, ss); |
| 4330 | if (err) |
| 4331 | goto err_destroy; |
| 4332 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4333 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4334 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4335 | /* |
| 4336 | * On the default hierarchy, a child doesn't automatically inherit |
| 4337 | * child_subsys_mask from the parent. Each is configured manually. |
| 4338 | */ |
| 4339 | if (!cgroup_on_dfl(cgrp)) |
| 4340 | cgrp->child_subsys_mask = parent->child_subsys_mask; |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4341 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4342 | kernfs_activate(kn); |
| 4343 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4344 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4345 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4346 | |
| 4347 | return 0; |
| 4348 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4349 | err_free_id: |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4350 | cgroup_idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4351 | err_unlock: |
| 4352 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4353 | err_unlock_tree: |
| 4354 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4355 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4356 | return err; |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4357 | |
| 4358 | err_destroy: |
| 4359 | cgroup_destroy_locked(cgrp); |
| 4360 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4361 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4362 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4363 | } |
| 4364 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4365 | static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, |
| 4366 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4367 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4368 | struct cgroup *parent = parent_kn->priv; |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 4369 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4370 | |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 4371 | /* |
| 4372 | * cgroup_create() grabs cgroup_tree_mutex which nests outside |
| 4373 | * kernfs active_ref and cgroup_create() already synchronizes |
| 4374 | * properly against removal through cgroup_lock_live_group(). |
| 4375 | * Break it before calling cgroup_create(). |
| 4376 | */ |
| 4377 | cgroup_get(parent); |
| 4378 | kernfs_break_active_protection(parent_kn); |
| 4379 | |
| 4380 | ret = cgroup_create(parent, name, mode); |
| 4381 | |
| 4382 | kernfs_unbreak_active_protection(parent_kn); |
| 4383 | cgroup_put(parent); |
| 4384 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4385 | } |
| 4386 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4387 | /* |
| 4388 | * This is called when the refcnt of a css is confirmed to be killed. |
| 4389 | * css_tryget() is now guaranteed to fail. |
| 4390 | */ |
| 4391 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4392 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4393 | struct cgroup_subsys_state *css = |
| 4394 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 4395 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4396 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4397 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4398 | mutex_lock(&cgroup_mutex); |
| 4399 | |
| 4400 | /* |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4401 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4402 | * initate destruction. |
| 4403 | */ |
| 4404 | offline_css(css); |
| 4405 | |
| 4406 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4407 | * If @cgrp is marked dead, it's waiting for refs of all css's to |
| 4408 | * be disabled before proceeding to the second phase of cgroup |
| 4409 | * destruction. If we are the last one, kick it off. |
| 4410 | */ |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4411 | if (!cgrp->nr_css && cgroup_is_dead(cgrp)) |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4412 | cgroup_destroy_css_killed(cgrp); |
| 4413 | |
| 4414 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4415 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4416 | |
| 4417 | /* |
| 4418 | * Put the css refs from kill_css(). Each css holds an extra |
| 4419 | * reference to the cgroup's dentry and cgroup removal proceeds |
| 4420 | * regardless of css refs. On the last put of each css, whenever |
| 4421 | * that may be, the extra dentry ref is put so that dentry |
| 4422 | * destruction happens only after all css's are released. |
| 4423 | */ |
| 4424 | css_put(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4427 | /* css kill confirmation processing requires process context, bounce */ |
| 4428 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4429 | { |
| 4430 | struct cgroup_subsys_state *css = |
| 4431 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4432 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4433 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4434 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4435 | } |
| 4436 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4437 | /** |
| 4438 | * kill_css - destroy a css |
| 4439 | * @css: css to destroy |
| 4440 | * |
| 4441 | * This function initiates destruction of @css by removing cgroup interface |
| 4442 | * files and putting its base reference. ->css_offline() will be invoked |
| 4443 | * asynchronously once css_tryget() is guaranteed to fail and when the |
| 4444 | * reference count reaches zero, @css will be released. |
| 4445 | */ |
| 4446 | static void kill_css(struct cgroup_subsys_state *css) |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4447 | { |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4448 | lockdep_assert_held(&cgroup_tree_mutex); |
| 4449 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4450 | /* |
| 4451 | * This must happen before css is disassociated with its cgroup. |
| 4452 | * See seq_css() for details. |
| 4453 | */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4454 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4455 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4456 | /* |
| 4457 | * Killing would put the base ref, but we need to keep it alive |
| 4458 | * until after ->css_offline(). |
| 4459 | */ |
| 4460 | css_get(css); |
| 4461 | |
| 4462 | /* |
| 4463 | * cgroup core guarantees that, by the time ->css_offline() is |
| 4464 | * invoked, no new css reference will be given out via |
| 4465 | * css_tryget(). We can't simply call percpu_ref_kill() and |
| 4466 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 4467 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 4468 | * |
| 4469 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4470 | * css is confirmed to be seen as killed on all CPUs. |
| 4471 | */ |
| 4472 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | /** |
| 4476 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4477 | * @cgrp: cgroup to be destroyed |
| 4478 | * |
| 4479 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4480 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4481 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4482 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4483 | * the following two steps. |
| 4484 | * |
| 4485 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4486 | * userland visible parts and start killing the percpu refcnts of |
| 4487 | * css's. Set up so that the next stage will be kicked off once all |
| 4488 | * the percpu refcnts are confirmed to be killed. |
| 4489 | * |
| 4490 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4491 | * rest of destruction. Once all cgroup references are gone, the |
| 4492 | * cgroup is RCU-freed. |
| 4493 | * |
| 4494 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4495 | * the userland is concerned and a new cgroup with the same name may be |
| 4496 | * created. As cgroup doesn't care about the names internally, this |
| 4497 | * doesn't cause any problem. |
| 4498 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4499 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4500 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4501 | { |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4502 | struct cgroup *child; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4503 | struct cgroup_subsys_state *css; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4504 | bool empty; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4505 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4506 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4507 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4508 | lockdep_assert_held(&cgroup_mutex); |
| 4509 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4510 | /* |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4511 | * css_set_rwsem synchronizes access to ->cset_links and prevents |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4512 | * @cgrp from being removed while put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4513 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4514 | down_read(&css_set_rwsem); |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4515 | empty = list_empty(&cgrp->cset_links); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4516 | up_read(&css_set_rwsem); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4517 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4518 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4519 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4520 | /* |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4521 | * Make sure there's no live children. We can't test ->children |
| 4522 | * emptiness as dead children linger on it while being destroyed; |
| 4523 | * otherwise, "rmdir parent/child parent" may fail with -EBUSY. |
| 4524 | */ |
| 4525 | empty = true; |
| 4526 | rcu_read_lock(); |
| 4527 | list_for_each_entry_rcu(child, &cgrp->children, sibling) { |
| 4528 | empty = cgroup_is_dead(child); |
| 4529 | if (!empty) |
| 4530 | break; |
| 4531 | } |
| 4532 | rcu_read_unlock(); |
| 4533 | if (!empty) |
| 4534 | return -EBUSY; |
| 4535 | |
| 4536 | /* |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4537 | * Mark @cgrp dead. This prevents further task migration and child |
| 4538 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4539 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4540 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4541 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4542 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4543 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4544 | |
Tejun Heo | 5d77381 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4545 | /* |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4546 | * Initiate massacre of all css's. cgroup_destroy_css_killed() |
| 4547 | * will be invoked to perform the rest of destruction once the |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4548 | * percpu refs of all css's are confirmed to be killed. This |
| 4549 | * involves removing the subsystem's files, drop cgroup_mutex. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4550 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4551 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4552 | for_each_css(css, ssid, cgrp) |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4553 | kill_css(css); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4554 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4555 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4556 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4557 | raw_spin_lock(&release_list_lock); |
| 4558 | if (!list_empty(&cgrp->release_list)) |
| 4559 | list_del_init(&cgrp->release_list); |
| 4560 | raw_spin_unlock(&release_list_lock); |
| 4561 | |
| 4562 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4563 | * If @cgrp has css's attached, the second stage of cgroup |
| 4564 | * destruction is kicked off from css_killed_work_fn() after the |
| 4565 | * refs of all attached css's are killed. If @cgrp doesn't have |
| 4566 | * any css, we kick it off here. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4567 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4568 | if (!cgrp->nr_css) |
| 4569 | cgroup_destroy_css_killed(cgrp); |
| 4570 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4571 | /* remove @cgrp directory along with the base files */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4572 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4573 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4574 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4575 | * There are two control paths which try to determine cgroup from |
| 4576 | * dentry without going through kernfs - cgroupstats_build() and |
| 4577 | * css_tryget_from_dir(). Those are supported by RCU protecting |
| 4578 | * clearing of cgrp->kn->priv backpointer, which should happen |
| 4579 | * after all files under it have been removed. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4580 | */ |
Tejun Heo | 6f30558 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4581 | kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4582 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4583 | |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4584 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4585 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4586 | return 0; |
| 4587 | }; |
| 4588 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4589 | /** |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4590 | * cgroup_destroy_css_killed - the second step of cgroup destruction |
Fabian Frederick | 6010694 | 2014-05-05 20:08:13 +0200 | [diff] [blame] | 4591 | * @cgrp: the cgroup whose csses have just finished offlining |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4592 | * |
| 4593 | * 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] | 4594 | * destroyed after all css's are offlined and performs the rest of |
| 4595 | * destruction. This is the second step of destruction described in the |
| 4596 | * comment above cgroup_destroy_locked(). |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4597 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4598 | static void cgroup_destroy_css_killed(struct cgroup *cgrp) |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4599 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4600 | struct cgroup *parent = cgrp->parent; |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4601 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4602 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4603 | lockdep_assert_held(&cgroup_mutex); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4604 | |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4605 | /* delete this cgroup from parent->children */ |
| 4606 | list_del_rcu(&cgrp->sibling); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4607 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4608 | cgroup_put(cgrp); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4609 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4610 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4611 | check_for_release(parent); |
| 4612 | } |
| 4613 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4614 | static int cgroup_rmdir(struct kernfs_node *kn) |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4615 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4616 | struct cgroup *cgrp = kn->priv; |
| 4617 | int ret = 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4618 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4619 | /* |
| 4620 | * This is self-destruction but @kn can't be removed while this |
| 4621 | * callback is in progress. Let's break active protection. Once |
| 4622 | * the protection is broken, @cgrp can be destroyed at any point. |
| 4623 | * Pin it so that it stays accessible. |
| 4624 | */ |
| 4625 | cgroup_get(cgrp); |
| 4626 | kernfs_break_active_protection(kn); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4627 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4628 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4629 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4630 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4631 | /* |
| 4632 | * @cgrp might already have been destroyed while we're trying to |
| 4633 | * grab the mutexes. |
| 4634 | */ |
| 4635 | if (!cgroup_is_dead(cgrp)) |
| 4636 | ret = cgroup_destroy_locked(cgrp); |
| 4637 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4638 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4639 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4640 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4641 | kernfs_unbreak_active_protection(kn); |
| 4642 | cgroup_put(cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4643 | return ret; |
| 4644 | } |
| 4645 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4646 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
| 4647 | .remount_fs = cgroup_remount, |
| 4648 | .show_options = cgroup_show_options, |
| 4649 | .mkdir = cgroup_mkdir, |
| 4650 | .rmdir = cgroup_rmdir, |
| 4651 | .rename = cgroup_rename, |
| 4652 | }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4653 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4654 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4655 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4656 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4657 | |
| 4658 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4659 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4660 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4661 | mutex_lock(&cgroup_mutex); |
| 4662 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4663 | idr_init(&ss->css_idr); |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4664 | INIT_LIST_HEAD(&ss->cfts); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4665 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4666 | /* Create the root cgroup state for this subsystem */ |
| 4667 | ss->root = &cgrp_dfl_root; |
| 4668 | css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4669 | /* We don't handle early failures gracefully */ |
| 4670 | BUG_ON(IS_ERR(css)); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4671 | init_and_link_css(css, ss, &cgrp_dfl_root.cgrp); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4672 | if (early) { |
| 4673 | /* idr_alloc() can't be called safely during early init */ |
| 4674 | css->id = 1; |
| 4675 | } else { |
| 4676 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL); |
| 4677 | BUG_ON(css->id < 0); |
| 4678 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4679 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4680 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4681 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4682 | * newly registered, all tasks and hence the |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4683 | * init_css_set is in the subsystem's root cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4684 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4685 | |
| 4686 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4687 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4688 | /* At system boot, before all subsystems have been |
| 4689 | * registered, no tasks have been forked, so we don't |
| 4690 | * need to invoke fork callbacks here. */ |
| 4691 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4692 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4693 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4694 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4695 | cgrp_dfl_root.subsys_mask |= 1 << ss->id; |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4696 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4697 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4698 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4702 | * cgroup_init_early - cgroup initialization at system boot |
| 4703 | * |
| 4704 | * Initialize cgroups at system boot, and initialize any |
| 4705 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4706 | */ |
| 4707 | int __init cgroup_init_early(void) |
| 4708 | { |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 4709 | static struct cgroup_sb_opts __initdata opts = |
| 4710 | { .flags = CGRP_ROOT_SANE_BEHAVIOR }; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4711 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4712 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4713 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4714 | init_cgroup_root(&cgrp_dfl_root, &opts); |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4715 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4716 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4717 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4718 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4719 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n", |
| 4720 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4721 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4722 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 4723 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4724 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4725 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4726 | ss->name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4727 | |
| 4728 | if (ss->early_init) |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4729 | cgroup_init_subsys(ss, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4730 | } |
| 4731 | return 0; |
| 4732 | } |
| 4733 | |
| 4734 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4735 | * cgroup_init - cgroup initialization |
| 4736 | * |
| 4737 | * Register cgroup filesystem and /proc file, and initialize |
| 4738 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4739 | */ |
| 4740 | int __init cgroup_init(void) |
| 4741 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4742 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4743 | unsigned long key; |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4744 | int ssid, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4745 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4746 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4747 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4748 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4749 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4750 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4751 | /* Add init_css_set to the hash table */ |
| 4752 | key = css_set_hash(init_css_set.subsys); |
| 4753 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4754 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4755 | BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4756 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4757 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4758 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4759 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4760 | for_each_subsys(ss, ssid) { |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4761 | if (ss->early_init) { |
| 4762 | struct cgroup_subsys_state *css = |
| 4763 | init_css_set.subsys[ss->id]; |
| 4764 | |
| 4765 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, |
| 4766 | GFP_KERNEL); |
| 4767 | BUG_ON(css->id < 0); |
| 4768 | } else { |
| 4769 | cgroup_init_subsys(ss, false); |
| 4770 | } |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4771 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4772 | list_add_tail(&init_css_set.e_cset_node[ssid], |
| 4773 | &cgrp_dfl_root.cgrp.e_csets[ssid]); |
| 4774 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4775 | /* |
| 4776 | * cftype registration needs kmalloc and can't be done |
| 4777 | * during early_init. Register base cftypes separately. |
| 4778 | */ |
| 4779 | if (ss->base_cftypes) |
| 4780 | WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes)); |
| 4781 | } |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4782 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4783 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4784 | if (!cgroup_kobj) |
| 4785 | return -ENOMEM; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4786 | |
| 4787 | err = register_filesystem(&cgroup_fs_type); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4788 | if (err < 0) { |
| 4789 | kobject_put(cgroup_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4790 | return err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4794 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4795 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4796 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4797 | static int __init cgroup_wq_init(void) |
| 4798 | { |
| 4799 | /* |
| 4800 | * There isn't much point in executing destruction path in |
| 4801 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 4802 | * Use 1 for @max_active. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4803 | * |
| 4804 | * We would prefer to do this in cgroup_init() above, but that |
| 4805 | * is called before init_workqueues(): so leave this until after. |
| 4806 | */ |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 4807 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4808 | BUG_ON(!cgroup_destroy_wq); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 4809 | |
| 4810 | /* |
| 4811 | * Used to destroy pidlists and separate to serve as flush domain. |
| 4812 | * Cap @max_active to 1 too. |
| 4813 | */ |
| 4814 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 4815 | 0, 1); |
| 4816 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 4817 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4818 | return 0; |
| 4819 | } |
| 4820 | core_initcall(cgroup_wq_init); |
| 4821 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4822 | /* |
| 4823 | * proc_cgroup_show() |
| 4824 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4825 | * - Used for /proc/<pid>/cgroup. |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4826 | */ |
| 4827 | |
| 4828 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4829 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4830 | { |
| 4831 | struct pid *pid; |
| 4832 | struct task_struct *tsk; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4833 | char *buf, *path; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4834 | int retval; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4835 | struct cgroup_root *root; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4836 | |
| 4837 | retval = -ENOMEM; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4838 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4839 | if (!buf) |
| 4840 | goto out; |
| 4841 | |
| 4842 | retval = -ESRCH; |
| 4843 | pid = m->private; |
| 4844 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4845 | if (!tsk) |
| 4846 | goto out_free; |
| 4847 | |
| 4848 | retval = 0; |
| 4849 | |
| 4850 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4851 | down_read(&css_set_rwsem); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4852 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4853 | for_each_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4854 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4855 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4856 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4857 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 4858 | if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4859 | continue; |
| 4860 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4861 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4862 | for_each_subsys(ss, ssid) |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4863 | if (root->subsys_mask & (1 << ssid)) |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4864 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4865 | if (strlen(root->name)) |
| 4866 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4867 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4868 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4869 | cgrp = task_cgroup_from_root(tsk, root); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4870 | path = cgroup_path(cgrp, buf, PATH_MAX); |
| 4871 | if (!path) { |
| 4872 | retval = -ENAMETOOLONG; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4873 | goto out_unlock; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4874 | } |
| 4875 | seq_puts(m, path); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4876 | seq_putc(m, '\n'); |
| 4877 | } |
| 4878 | |
| 4879 | out_unlock: |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4880 | up_read(&css_set_rwsem); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4881 | mutex_unlock(&cgroup_mutex); |
| 4882 | put_task_struct(tsk); |
| 4883 | out_free: |
| 4884 | kfree(buf); |
| 4885 | out: |
| 4886 | return retval; |
| 4887 | } |
| 4888 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4889 | /* Display information about each subsystem and each hierarchy */ |
| 4890 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4891 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4892 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4893 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4894 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4895 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4896 | /* |
| 4897 | * ideally we don't want subsystems moving around while we do this. |
| 4898 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4899 | * subsys/hierarchy state. |
| 4900 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4901 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4902 | |
| 4903 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4904 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4905 | ss->name, ss->root->hierarchy_id, |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4906 | atomic_read(&ss->root->nr_cgrps), !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4907 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4908 | mutex_unlock(&cgroup_mutex); |
| 4909 | return 0; |
| 4910 | } |
| 4911 | |
| 4912 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4913 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4914 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4915 | } |
| 4916 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4917 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4918 | .open = cgroupstats_open, |
| 4919 | .read = seq_read, |
| 4920 | .llseek = seq_lseek, |
| 4921 | .release = single_release, |
| 4922 | }; |
| 4923 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4924 | /** |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4925 | * cgroup_fork - initialize cgroup related fields during copy_process() |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4926 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4927 | * |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4928 | * A task is associated with the init_css_set until cgroup_post_fork() |
| 4929 | * attaches it to the parent's css_set. Empty cg_list indicates that |
| 4930 | * @child isn't holding reference to its css_set. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4931 | */ |
| 4932 | void cgroup_fork(struct task_struct *child) |
| 4933 | { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4934 | RCU_INIT_POINTER(child->cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4935 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4939 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4940 | * @child: the task in question |
| 4941 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4942 | * Adds the task to the list running through its css_set if necessary and |
| 4943 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4944 | * 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] | 4945 | * 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] | 4946 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4947 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4948 | void cgroup_post_fork(struct task_struct *child) |
| 4949 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4950 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4951 | int i; |
| 4952 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4953 | /* |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4954 | * This may race against cgroup_enable_task_cg_links(). As that |
| 4955 | * function sets use_task_css_set_links before grabbing |
| 4956 | * tasklist_lock and we just went through tasklist_lock to add |
| 4957 | * @child, it's guaranteed that either we see the set |
| 4958 | * use_task_css_set_links or cgroup_enable_task_cg_lists() sees |
| 4959 | * @child during its iteration. |
| 4960 | * |
| 4961 | * If we won the race, @child is associated with %current's |
| 4962 | * css_set. Grabbing css_set_rwsem guarantees both that the |
| 4963 | * association is stable, and, on completion of the parent's |
| 4964 | * migration, @child is visible in the source of migration or |
| 4965 | * already in the destination cgroup. This guarantee is necessary |
| 4966 | * when implementing operations which need to migrate all tasks of |
| 4967 | * a cgroup to another. |
| 4968 | * |
| 4969 | * Note that if we lose to cgroup_enable_task_cg_links(), @child |
| 4970 | * will remain in init_css_set. This is safe because all tasks are |
| 4971 | * in the init_css_set before cg_links is enabled and there's no |
| 4972 | * operation which transfers all tasks out of init_css_set. |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4973 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4974 | if (use_task_css_set_links) { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4975 | struct css_set *cset; |
| 4976 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4977 | down_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4978 | cset = task_css_set(current); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4979 | if (list_empty(&child->cg_list)) { |
| 4980 | rcu_assign_pointer(child->cgroups, cset); |
| 4981 | list_add(&child->cg_list, &cset->tasks); |
| 4982 | get_css_set(cset); |
| 4983 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4984 | up_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4985 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4986 | |
| 4987 | /* |
| 4988 | * Call ss->fork(). This must happen after @child is linked on |
| 4989 | * css_set; otherwise, @child might change state between ->fork() |
| 4990 | * and addition to css_set. |
| 4991 | */ |
| 4992 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4993 | for_each_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4994 | if (ss->fork) |
| 4995 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4996 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4997 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4998 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4999 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5000 | * cgroup_exit - detach cgroup from exiting task |
| 5001 | * @tsk: pointer to task_struct of exiting process |
| 5002 | * |
| 5003 | * Description: Detach cgroup from @tsk and release it. |
| 5004 | * |
| 5005 | * Note that cgroups marked notify_on_release force every task in |
| 5006 | * them to take the global cgroup_mutex mutex when exiting. |
| 5007 | * This could impact scaling on very large systems. Be reluctant to |
| 5008 | * use notify_on_release cgroups where very high task exit scaling |
| 5009 | * is required on large systems. |
| 5010 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5011 | * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We |
| 5012 | * call cgroup_exit() while the task is still competent to handle |
| 5013 | * notify_on_release(), then leave the task attached to the root cgroup in |
| 5014 | * each hierarchy for the remainder of its exit. No need to bother with |
| 5015 | * init_css_set refcnting. init_css_set never goes away and we can't race |
Li Zefan | e8604cb | 2014-03-28 15:18:27 +0800 | [diff] [blame] | 5016 | * with migration path - PF_EXITING is visible to migration path. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5017 | */ |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 5018 | void cgroup_exit(struct task_struct *tsk) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5019 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5020 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5021 | struct css_set *cset; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5022 | bool put_cset = false; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5023 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5024 | |
| 5025 | /* |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5026 | * Unlink from @tsk from its css_set. As migration path can't race |
| 5027 | * with us, we can check cg_list without grabbing css_set_rwsem. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5028 | */ |
| 5029 | if (!list_empty(&tsk->cg_list)) { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5030 | down_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5031 | list_del_init(&tsk->cg_list); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5032 | up_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5033 | put_cset = true; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5034 | } |
| 5035 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5036 | /* Reassign the task to the init_css_set. */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5037 | cset = task_css_set(tsk); |
| 5038 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5039 | |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 5040 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5041 | /* see cgroup_post_fork() for details */ |
| 5042 | for_each_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5043 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5044 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 5045 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5046 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5047 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5048 | } |
| 5049 | } |
| 5050 | } |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5051 | |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5052 | if (put_cset) |
| 5053 | put_css_set(cset, true); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5054 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5055 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5056 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5057 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5058 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5059 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5060 | /* |
| 5061 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5062 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5063 | * it now |
| 5064 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5065 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5066 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5067 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5068 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5069 | list_empty(&cgrp->release_list)) { |
| 5070 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5071 | need_schedule_work = 1; |
| 5072 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5073 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5074 | if (need_schedule_work) |
| 5075 | schedule_work(&release_agent_work); |
| 5076 | } |
| 5077 | } |
| 5078 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5079 | /* |
| 5080 | * Notify userspace when a cgroup is released, by running the |
| 5081 | * configured release agent with the name of the cgroup (path |
| 5082 | * relative to the root of cgroup file system) as the argument. |
| 5083 | * |
| 5084 | * Most likely, this user command will try to rmdir this cgroup. |
| 5085 | * |
| 5086 | * This races with the possibility that some other task will be |
| 5087 | * attached to this cgroup before it is removed, or that some other |
| 5088 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5089 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5090 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5091 | * to continue to serve a useful existence. Next time it's released, |
| 5092 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5093 | * |
| 5094 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5095 | * means only wait until the task is successfully execve()'d. The |
| 5096 | * separate release agent task is forked by call_usermodehelper(), |
| 5097 | * then control in this thread returns here, without waiting for the |
| 5098 | * release agent task. We don't bother to wait because the caller of |
| 5099 | * this routine has no use for the exit status of the release agent |
| 5100 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5101 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5102 | static void cgroup_release_agent(struct work_struct *work) |
| 5103 | { |
| 5104 | BUG_ON(work != &release_agent_work); |
| 5105 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5106 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5107 | while (!list_empty(&release_list)) { |
| 5108 | char *argv[3], *envp[3]; |
| 5109 | int i; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5110 | char *pathbuf = NULL, *agentbuf = NULL, *path; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5111 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5112 | struct cgroup, |
| 5113 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5114 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5115 | raw_spin_unlock(&release_list_lock); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5116 | pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5117 | if (!pathbuf) |
| 5118 | goto continue_free; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5119 | path = cgroup_path(cgrp, pathbuf, PATH_MAX); |
| 5120 | if (!path) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5121 | goto continue_free; |
| 5122 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5123 | if (!agentbuf) |
| 5124 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5125 | |
| 5126 | i = 0; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5127 | argv[i++] = agentbuf; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5128 | argv[i++] = path; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5129 | argv[i] = NULL; |
| 5130 | |
| 5131 | i = 0; |
| 5132 | /* minimal command environment */ |
| 5133 | envp[i++] = "HOME=/"; |
| 5134 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5135 | envp[i] = NULL; |
| 5136 | |
| 5137 | /* Drop the lock while we invoke the usermode helper, |
| 5138 | * since the exec could involve hitting disk and hence |
| 5139 | * be a slow process */ |
| 5140 | mutex_unlock(&cgroup_mutex); |
| 5141 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5142 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5143 | continue_free: |
| 5144 | kfree(pathbuf); |
| 5145 | kfree(agentbuf); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5146 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5147 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5148 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5149 | mutex_unlock(&cgroup_mutex); |
| 5150 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5151 | |
| 5152 | static int __init cgroup_disable(char *str) |
| 5153 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5154 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5155 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5156 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5157 | |
| 5158 | while ((token = strsep(&str, ",")) != NULL) { |
| 5159 | if (!*token) |
| 5160 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5161 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5162 | for_each_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5163 | if (!strcmp(token, ss->name)) { |
| 5164 | ss->disabled = 1; |
| 5165 | printk(KERN_INFO "Disabling %s control group" |
| 5166 | " subsystem\n", ss->name); |
| 5167 | break; |
| 5168 | } |
| 5169 | } |
| 5170 | } |
| 5171 | return 1; |
| 5172 | } |
| 5173 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5174 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5175 | /** |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5176 | * 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] | 5177 | * @dentry: directory dentry of interest |
| 5178 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5179 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5180 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 5181 | * to get the corresponding css and return it. If such css doesn't exist |
| 5182 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5183 | */ |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5184 | struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry, |
| 5185 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5186 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5187 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
| 5188 | struct cgroup_subsys_state *css = NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5189 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5190 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5191 | /* is @dentry a cgroup dir? */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5192 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 5193 | kernfs_type(kn) != KERNFS_DIR) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5194 | return ERR_PTR(-EBADF); |
| 5195 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5196 | rcu_read_lock(); |
| 5197 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5198 | /* |
| 5199 | * This path doesn't originate from kernfs and @kn could already |
| 5200 | * have been or be removed at any point. @kn->priv is RCU |
| 5201 | * protected for this access. See destroy_locked() for details. |
| 5202 | */ |
| 5203 | cgrp = rcu_dereference(kn->priv); |
| 5204 | if (cgrp) |
| 5205 | css = cgroup_css(cgrp, ss); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5206 | |
| 5207 | if (!css || !css_tryget(css)) |
| 5208 | css = ERR_PTR(-ENOENT); |
| 5209 | |
| 5210 | rcu_read_unlock(); |
| 5211 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5212 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5213 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5214 | /** |
| 5215 | * css_from_id - lookup css by id |
| 5216 | * @id: the cgroup id |
| 5217 | * @ss: cgroup subsys to be looked into |
| 5218 | * |
| 5219 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5220 | * Should be called under rcu_read_lock(). |
| 5221 | */ |
| 5222 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5223 | { |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 5224 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5225 | return idr_find(&ss->css_idr, id); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5226 | } |
| 5227 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5228 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5229 | static struct cgroup_subsys_state * |
| 5230 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5231 | { |
| 5232 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5233 | |
| 5234 | if (!css) |
| 5235 | return ERR_PTR(-ENOMEM); |
| 5236 | |
| 5237 | return css; |
| 5238 | } |
| 5239 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5240 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5241 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5242 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5243 | } |
| 5244 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5245 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 5246 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5247 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5248 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5249 | } |
| 5250 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5251 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 5252 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5253 | { |
| 5254 | return (u64)(unsigned long)current->cgroups; |
| 5255 | } |
| 5256 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5257 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5258 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5259 | { |
| 5260 | u64 count; |
| 5261 | |
| 5262 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5263 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5264 | rcu_read_unlock(); |
| 5265 | return count; |
| 5266 | } |
| 5267 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5268 | 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] | 5269 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5270 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5271 | struct css_set *cset; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5272 | char *name_buf; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5273 | |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5274 | name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL); |
| 5275 | if (!name_buf) |
| 5276 | return -ENOMEM; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5277 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5278 | down_read(&css_set_rwsem); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5279 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5280 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5281 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5282 | struct cgroup *c = link->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5283 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 5284 | cgroup_name(c, name_buf, NAME_MAX + 1); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5285 | seq_printf(seq, "Root %d group %s\n", |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 5286 | c->root->hierarchy_id, name_buf); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5287 | } |
| 5288 | rcu_read_unlock(); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5289 | up_read(&css_set_rwsem); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5290 | kfree(name_buf); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5291 | return 0; |
| 5292 | } |
| 5293 | |
| 5294 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5295 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5296 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5297 | struct cgroup_subsys_state *css = seq_css(seq); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5298 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5299 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5300 | down_read(&css_set_rwsem); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5301 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5302 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5303 | struct task_struct *task; |
| 5304 | int count = 0; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 5305 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5306 | seq_printf(seq, "css_set %p\n", cset); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 5307 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5308 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 5309 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 5310 | goto overflow; |
| 5311 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5312 | } |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 5313 | |
| 5314 | list_for_each_entry(task, &cset->mg_tasks, cg_list) { |
| 5315 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 5316 | goto overflow; |
| 5317 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
| 5318 | } |
| 5319 | continue; |
| 5320 | overflow: |
| 5321 | seq_puts(seq, " ...\n"); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5322 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5323 | up_read(&css_set_rwsem); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5324 | return 0; |
| 5325 | } |
| 5326 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5327 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5328 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5329 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5330 | } |
| 5331 | |
| 5332 | static struct cftype debug_files[] = { |
| 5333 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5334 | .name = "taskcount", |
| 5335 | .read_u64 = debug_taskcount_read, |
| 5336 | }, |
| 5337 | |
| 5338 | { |
| 5339 | .name = "current_css_set", |
| 5340 | .read_u64 = current_css_set_read, |
| 5341 | }, |
| 5342 | |
| 5343 | { |
| 5344 | .name = "current_css_set_refcount", |
| 5345 | .read_u64 = current_css_set_refcount_read, |
| 5346 | }, |
| 5347 | |
| 5348 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5349 | .name = "current_css_set_cg_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5350 | .seq_show = current_css_set_cg_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5351 | }, |
| 5352 | |
| 5353 | { |
| 5354 | .name = "cgroup_css_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5355 | .seq_show = cgroup_css_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5356 | }, |
| 5357 | |
| 5358 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5359 | .name = "releasable", |
| 5360 | .read_u64 = releasable_read, |
| 5361 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5362 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5363 | { } /* terminate */ |
| 5364 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5365 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5366 | struct cgroup_subsys debug_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5367 | .css_alloc = debug_css_alloc, |
| 5368 | .css_free = debug_css_free, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5369 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5370 | }; |
| 5371 | #endif /* CONFIG_CGROUP_DEBUG */ |