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 | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame^] | 286 | struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 287 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 288 | struct cgroup *cgrp = of->kn->parent->priv; |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame^] | 289 | struct cftype *cft = of_cft(of); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * This is open and unprotected implementation of cgroup_css(). |
| 293 | * seq_css() is only called from a kernfs file operation which has |
| 294 | * an active reference on the file. Because all the subsystem |
| 295 | * files are drained before a css is disassociated with a cgroup, |
| 296 | * the matching css from the cgroup's subsys table is guaranteed to |
| 297 | * be and stay valid until the enclosing operation is complete. |
| 298 | */ |
| 299 | if (cft->ss) |
| 300 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 301 | else |
| 302 | return &cgrp->dummy_css; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 303 | } |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame^] | 304 | EXPORT_SYMBOL_GPL(of_css); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 305 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 306 | /** |
| 307 | * cgroup_is_descendant - test ancestry |
| 308 | * @cgrp: the cgroup to be tested |
| 309 | * @ancestor: possible ancestor of @cgrp |
| 310 | * |
| 311 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 312 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 313 | * and @ancestor are accessible. |
| 314 | */ |
| 315 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 316 | { |
| 317 | while (cgrp) { |
| 318 | if (cgrp == ancestor) |
| 319 | return true; |
| 320 | cgrp = cgrp->parent; |
| 321 | } |
| 322 | return false; |
| 323 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 324 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 325 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 326 | { |
| 327 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 328 | (1 << CGRP_RELEASABLE) | |
| 329 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 330 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 333 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 334 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 335 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 338 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 339 | * for_each_css - iterate all css's of a cgroup |
| 340 | * @css: the iteration cursor |
| 341 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 342 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 343 | * |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 344 | * Should be called under cgroup_[tree_]mutex. |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 345 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 346 | #define for_each_css(css, ssid, cgrp) \ |
| 347 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 348 | if (!((css) = rcu_dereference_check( \ |
| 349 | (cgrp)->subsys[(ssid)], \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 350 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 351 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 352 | else |
| 353 | |
| 354 | /** |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 355 | * for_each_e_css - iterate all effective css's of a cgroup |
| 356 | * @css: the iteration cursor |
| 357 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 358 | * @cgrp: the target cgroup to iterate css's of |
| 359 | * |
| 360 | * Should be called under cgroup_[tree_]mutex. |
| 361 | */ |
| 362 | #define for_each_e_css(css, ssid, cgrp) \ |
| 363 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 364 | if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \ |
| 365 | ; \ |
| 366 | else |
| 367 | |
| 368 | /** |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 369 | * for_each_subsys - iterate all enabled cgroup subsystems |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 370 | * @ss: the iteration cursor |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 371 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 372 | */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 373 | #define for_each_subsys(ss, ssid) \ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 374 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \ |
| 375 | (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 376 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 377 | /* iterate across the hierarchies */ |
| 378 | #define for_each_root(root) \ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 379 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 380 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 381 | /* iterate over child cgrps, lock should be held throughout iteration */ |
| 382 | #define cgroup_for_each_live_child(child, cgrp) \ |
| 383 | list_for_each_entry((child), &(cgrp)->children, sibling) \ |
| 384 | if (({ lockdep_assert_held(&cgroup_tree_mutex); \ |
| 385 | cgroup_is_dead(child); })) \ |
| 386 | ; \ |
| 387 | else |
| 388 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 389 | /** |
| 390 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 391 | * @cgrp: the cgroup to be checked for liveness |
| 392 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 393 | * On success, returns true; the mutex should be later unlocked. On |
| 394 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 395 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 396 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 397 | { |
| 398 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 399 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 400 | mutex_unlock(&cgroup_mutex); |
| 401 | return false; |
| 402 | } |
| 403 | return true; |
| 404 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 405 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 406 | /* the list of cgroups eligible for automatic release. Protected by |
| 407 | * release_list_lock */ |
| 408 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 409 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 410 | static void cgroup_release_agent(struct work_struct *work); |
| 411 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 412 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 413 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 414 | /* |
| 415 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 416 | * belong to different cgroups on different hierarchies. In the other |
| 417 | * direction, a css_set is naturally associated with multiple cgroups. |
| 418 | * This M:N relationship is represented by the following link structure |
| 419 | * which exists for each association and allows traversing the associations |
| 420 | * from both sides. |
| 421 | */ |
| 422 | struct cgrp_cset_link { |
| 423 | /* the cgroup and css_set this link associates */ |
| 424 | struct cgroup *cgrp; |
| 425 | struct css_set *cset; |
| 426 | |
| 427 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 428 | struct list_head cset_link; |
| 429 | |
| 430 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 431 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 432 | }; |
| 433 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 434 | /* |
| 435 | * 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] | 436 | * hierarchies being mounted. It contains a pointer to the root state |
| 437 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 438 | * reference-counted, to improve performance when child cgroups |
| 439 | * haven't been created. |
| 440 | */ |
Tejun Heo | 5024ae2 | 2014-05-07 21:31:17 -0400 | [diff] [blame] | 441 | struct css_set init_css_set = { |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 442 | .refcount = ATOMIC_INIT(1), |
| 443 | .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), |
| 444 | .tasks = LIST_HEAD_INIT(init_css_set.tasks), |
| 445 | .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), |
| 446 | .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node), |
| 447 | .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), |
| 448 | }; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 449 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 450 | static int css_set_count = 1; /* 1 for init_css_set */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 451 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 452 | /** |
| 453 | * cgroup_update_populated - updated populated count of a cgroup |
| 454 | * @cgrp: the target cgroup |
| 455 | * @populated: inc or dec populated count |
| 456 | * |
| 457 | * @cgrp is either getting the first task (css_set) or losing the last. |
| 458 | * Update @cgrp->populated_cnt accordingly. The count is propagated |
| 459 | * towards root so that a given cgroup's populated_cnt is zero iff the |
| 460 | * cgroup and all its descendants are empty. |
| 461 | * |
| 462 | * @cgrp's interface file "cgroup.populated" is zero if |
| 463 | * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt |
| 464 | * changes from or to zero, userland is notified that the content of the |
| 465 | * interface file has changed. This can be used to detect when @cgrp and |
| 466 | * its descendants become populated or empty. |
| 467 | */ |
| 468 | static void cgroup_update_populated(struct cgroup *cgrp, bool populated) |
| 469 | { |
| 470 | lockdep_assert_held(&css_set_rwsem); |
| 471 | |
| 472 | do { |
| 473 | bool trigger; |
| 474 | |
| 475 | if (populated) |
| 476 | trigger = !cgrp->populated_cnt++; |
| 477 | else |
| 478 | trigger = !--cgrp->populated_cnt; |
| 479 | |
| 480 | if (!trigger) |
| 481 | break; |
| 482 | |
| 483 | if (cgrp->populated_kn) |
| 484 | kernfs_notify(cgrp->populated_kn); |
| 485 | cgrp = cgrp->parent; |
| 486 | } while (cgrp); |
| 487 | } |
| 488 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 489 | /* |
| 490 | * hash table for cgroup groups. This improves the performance to find |
| 491 | * an existing css_set. This hash doesn't (currently) take into |
| 492 | * account cgroups in empty hierarchies. |
| 493 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 494 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 495 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 496 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 497 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 498 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 499 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 500 | struct cgroup_subsys *ss; |
| 501 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 502 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 503 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 504 | key += (unsigned long)css[i]; |
| 505 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 506 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 507 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 510 | static void put_css_set_locked(struct css_set *cset, bool taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 511 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 512 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 513 | struct cgroup_subsys *ss; |
| 514 | int ssid; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 515 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 516 | lockdep_assert_held(&css_set_rwsem); |
| 517 | |
| 518 | if (!atomic_dec_and_test(&cset->refcount)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 519 | return; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 520 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 521 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 522 | for_each_subsys(ss, ssid) |
| 523 | list_del(&cset->e_cset_node[ssid]); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 524 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 525 | css_set_count--; |
| 526 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 527 | 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] | 528 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 529 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 530 | list_del(&link->cset_link); |
| 531 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 532 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 533 | /* @cgrp can't go away while we're holding css_set_rwsem */ |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 534 | if (list_empty(&cgrp->cset_links)) { |
| 535 | cgroup_update_populated(cgrp, false); |
| 536 | if (notify_on_release(cgrp)) { |
| 537 | if (taskexit) |
| 538 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 539 | check_for_release(cgrp); |
| 540 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 541 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 542 | |
| 543 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 544 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 545 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 546 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 549 | static void put_css_set(struct css_set *cset, bool taskexit) |
| 550 | { |
| 551 | /* |
| 552 | * Ensure that the refcount doesn't hit zero while any readers |
| 553 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 554 | * rwlock |
| 555 | */ |
| 556 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
| 557 | return; |
| 558 | |
| 559 | down_write(&css_set_rwsem); |
| 560 | put_css_set_locked(cset, taskexit); |
| 561 | up_write(&css_set_rwsem); |
| 562 | } |
| 563 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 564 | /* |
| 565 | * refcounted get/put for css_set objects |
| 566 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 567 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 568 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 569 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 572 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 573 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 574 | * @cset: candidate css_set being tested |
| 575 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 576 | * @new_cgrp: cgroup that's being entered by the task |
| 577 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 578 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 579 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 580 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 581 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 582 | static bool compare_css_sets(struct css_set *cset, |
| 583 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 584 | struct cgroup *new_cgrp, |
| 585 | struct cgroup_subsys_state *template[]) |
| 586 | { |
| 587 | struct list_head *l1, *l2; |
| 588 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 589 | /* |
| 590 | * On the default hierarchy, there can be csets which are |
| 591 | * associated with the same set of cgroups but different csses. |
| 592 | * Let's first ensure that csses match. |
| 593 | */ |
| 594 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 595 | return false; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 596 | |
| 597 | /* |
| 598 | * Compare cgroup pointers in order to distinguish between |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 599 | * different cgroups in hierarchies. As different cgroups may |
| 600 | * share the same effective css, this comparison is always |
| 601 | * necessary. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 602 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 603 | l1 = &cset->cgrp_links; |
| 604 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 605 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 606 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 607 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 608 | |
| 609 | l1 = l1->next; |
| 610 | l2 = l2->next; |
| 611 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 612 | if (l1 == &cset->cgrp_links) { |
| 613 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 614 | break; |
| 615 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 616 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 617 | } |
| 618 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 619 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 620 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 621 | cgrp1 = link1->cgrp; |
| 622 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 623 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 624 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 625 | |
| 626 | /* |
| 627 | * If this hierarchy is the hierarchy of the cgroup |
| 628 | * that's changing, then we need to check that this |
| 629 | * css_set points to the new cgroup; if it's any other |
| 630 | * hierarchy, then this css_set should point to the |
| 631 | * same cgroup as the old css_set. |
| 632 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 633 | if (cgrp1->root == new_cgrp->root) { |
| 634 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 635 | return false; |
| 636 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 637 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 638 | return false; |
| 639 | } |
| 640 | } |
| 641 | return true; |
| 642 | } |
| 643 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 644 | /** |
| 645 | * find_existing_css_set - init css array and find the matching css_set |
| 646 | * @old_cset: the css_set that we're using before the cgroup transition |
| 647 | * @cgrp: the cgroup that we're moving into |
| 648 | * @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] | 649 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 650 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 651 | struct cgroup *cgrp, |
| 652 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 653 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 654 | struct cgroup_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 655 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 656 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 657 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 658 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 659 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 660 | /* |
| 661 | * Build the set of subsystem state objects that we want to see in the |
| 662 | * new css_set. while subsystems can change globally, the entries here |
| 663 | * won't change, so no need for locking. |
| 664 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 665 | for_each_subsys(ss, i) { |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 666 | if (root->subsys_mask & (1UL << i)) { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 667 | /* |
| 668 | * @ss is in this hierarchy, so we want the |
| 669 | * effective css from @cgrp. |
| 670 | */ |
| 671 | template[i] = cgroup_e_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 672 | } else { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 673 | /* |
| 674 | * @ss is not in this hierarchy, so we don't want |
| 675 | * to change the css. |
| 676 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 677 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 681 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 682 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 683 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 684 | continue; |
| 685 | |
| 686 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 687 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 688 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 689 | |
| 690 | /* No existing cgroup group matched */ |
| 691 | return NULL; |
| 692 | } |
| 693 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 694 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 695 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 696 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 697 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 698 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 699 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 700 | kfree(link); |
| 701 | } |
| 702 | } |
| 703 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 704 | /** |
| 705 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 706 | * @count: the number of links to allocate |
| 707 | * @tmp_links: list_head the allocated links are put on |
| 708 | * |
| 709 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 710 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 711 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 712 | 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] | 713 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 714 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 715 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 716 | |
| 717 | INIT_LIST_HEAD(tmp_links); |
| 718 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 719 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 720 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 721 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 722 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 723 | return -ENOMEM; |
| 724 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 725 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 726 | } |
| 727 | return 0; |
| 728 | } |
| 729 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 730 | /** |
| 731 | * 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] | 732 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 733 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 734 | * @cgrp: the destination cgroup |
| 735 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 736 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 737 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 738 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 739 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 740 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 741 | BUG_ON(list_empty(tmp_links)); |
Tejun Heo | 6803c00 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 742 | |
| 743 | if (cgroup_on_dfl(cgrp)) |
| 744 | cset->dfl_cgrp = cgrp; |
| 745 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 746 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 747 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 748 | link->cgrp = cgrp; |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 749 | |
| 750 | if (list_empty(&cgrp->cset_links)) |
| 751 | cgroup_update_populated(cgrp, true); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 752 | list_move(&link->cset_link, &cgrp->cset_links); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 753 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 754 | /* |
| 755 | * Always add links to the tail of the list so that the list |
| 756 | * is sorted by order of hierarchy creation |
| 757 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 758 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 759 | } |
| 760 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 761 | /** |
| 762 | * find_css_set - return a new css_set with one cgroup updated |
| 763 | * @old_cset: the baseline css_set |
| 764 | * @cgrp: the cgroup to be updated |
| 765 | * |
| 766 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 767 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 768 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 769 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 770 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 771 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 772 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 773 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 774 | struct list_head tmp_links; |
| 775 | struct cgrp_cset_link *link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 776 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 777 | unsigned long key; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 778 | int ssid; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 779 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 780 | lockdep_assert_held(&cgroup_mutex); |
| 781 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 782 | /* First see if we already have a cgroup group that matches |
| 783 | * the desired set */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 784 | down_read(&css_set_rwsem); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 785 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 786 | if (cset) |
| 787 | get_css_set(cset); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 788 | up_read(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 789 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 790 | if (cset) |
| 791 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 792 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 793 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 794 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 795 | return NULL; |
| 796 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 797 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 798 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 799 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 800 | return NULL; |
| 801 | } |
| 802 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 803 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 804 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 805 | INIT_LIST_HEAD(&cset->tasks); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 806 | INIT_LIST_HEAD(&cset->mg_tasks); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 807 | INIT_LIST_HEAD(&cset->mg_preload_node); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 808 | INIT_LIST_HEAD(&cset->mg_node); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 809 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 810 | |
| 811 | /* Copy the set of subsystem state objects generated in |
| 812 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 813 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 814 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 815 | down_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 816 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 817 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 818 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 819 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 820 | if (c->root == cgrp->root) |
| 821 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 822 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 823 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 824 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 825 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 826 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 827 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 828 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 829 | /* Add @cset to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 830 | key = css_set_hash(cset->subsys); |
| 831 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 832 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 833 | for_each_subsys(ss, ssid) |
| 834 | list_add_tail(&cset->e_cset_node[ssid], |
| 835 | &cset->subsys[ssid]->cgroup->e_csets[ssid]); |
| 836 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 837 | up_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 838 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 839 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 842 | 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] | 843 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 844 | struct cgroup *root_cgrp = kf_root->kn->priv; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 845 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 846 | return root_cgrp->root; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 847 | } |
| 848 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 849 | static int cgroup_init_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 850 | { |
| 851 | int id; |
| 852 | |
| 853 | lockdep_assert_held(&cgroup_mutex); |
| 854 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 855 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 856 | if (id < 0) |
| 857 | return id; |
| 858 | |
| 859 | root->hierarchy_id = id; |
| 860 | return 0; |
| 861 | } |
| 862 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 863 | static void cgroup_exit_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 864 | { |
| 865 | lockdep_assert_held(&cgroup_mutex); |
| 866 | |
| 867 | if (root->hierarchy_id) { |
| 868 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
| 869 | root->hierarchy_id = 0; |
| 870 | } |
| 871 | } |
| 872 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 873 | static void cgroup_free_root(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 874 | { |
| 875 | if (root) { |
| 876 | /* hierarhcy ID shoulid already have been released */ |
| 877 | WARN_ON_ONCE(root->hierarchy_id); |
| 878 | |
| 879 | idr_destroy(&root->cgroup_idr); |
| 880 | kfree(root); |
| 881 | } |
| 882 | } |
| 883 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 884 | static void cgroup_destroy_root(struct cgroup_root *root) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 885 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 886 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 887 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 888 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 889 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 890 | mutex_lock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 891 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 892 | BUG_ON(atomic_read(&root->nr_cgrps)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 893 | BUG_ON(!list_empty(&cgrp->children)); |
| 894 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 895 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 896 | rebind_subsystems(&cgrp_dfl_root, root->subsys_mask); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 897 | |
| 898 | /* |
| 899 | * Release all the links from cset_links to this hierarchy's |
| 900 | * root cgroup |
| 901 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 902 | down_write(&css_set_rwsem); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 903 | |
| 904 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 905 | list_del(&link->cset_link); |
| 906 | list_del(&link->cgrp_link); |
| 907 | kfree(link); |
| 908 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 909 | up_write(&css_set_rwsem); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 910 | |
| 911 | if (!list_empty(&root->root_list)) { |
| 912 | list_del(&root->root_list); |
| 913 | cgroup_root_count--; |
| 914 | } |
| 915 | |
| 916 | cgroup_exit_root_id(root); |
| 917 | |
| 918 | mutex_unlock(&cgroup_mutex); |
| 919 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 920 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 921 | kernfs_destroy_root(root->kf_root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 922 | cgroup_free_root(root); |
| 923 | } |
| 924 | |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 925 | /* look up cgroup associated with given css_set on the specified hierarchy */ |
| 926 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 927 | struct cgroup_root *root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 928 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 929 | struct cgroup *res = NULL; |
| 930 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 931 | lockdep_assert_held(&cgroup_mutex); |
| 932 | lockdep_assert_held(&css_set_rwsem); |
| 933 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 934 | if (cset == &init_css_set) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 935 | res = &root->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 936 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 937 | struct cgrp_cset_link *link; |
| 938 | |
| 939 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 940 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 941 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 942 | if (c->root == root) { |
| 943 | res = c; |
| 944 | break; |
| 945 | } |
| 946 | } |
| 947 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 948 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 949 | BUG_ON(!res); |
| 950 | return res; |
| 951 | } |
| 952 | |
| 953 | /* |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 954 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 955 | * called with cgroup_mutex and css_set_rwsem held. |
| 956 | */ |
| 957 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 958 | struct cgroup_root *root) |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 959 | { |
| 960 | /* |
| 961 | * No need to lock the task - since we hold cgroup_mutex the |
| 962 | * task can't change groups, so the only thing that can happen |
| 963 | * is that it exits and its css is set back to init_css_set. |
| 964 | */ |
| 965 | return cset_cgroup_from_root(task_css_set(task), root); |
| 966 | } |
| 967 | |
| 968 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 969 | * A task must hold cgroup_mutex to modify cgroups. |
| 970 | * |
| 971 | * Any task can increment and decrement the count field without lock. |
| 972 | * So in general, code holding cgroup_mutex can't rely on the count |
| 973 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 974 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 975 | * means that no tasks are currently attached, therefore there is no |
| 976 | * way a task attached to that cgroup can fork (the other way to |
| 977 | * increment the count). So code holding cgroup_mutex can safely |
| 978 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 979 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 980 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 981 | * needs that mutex. |
| 982 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 983 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 984 | * (usually) take cgroup_mutex. These are the two most performance |
| 985 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 986 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 987 | * 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] | 988 | * to the release agent with the name of the cgroup (path relative to |
| 989 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 990 | * |
| 991 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 992 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 993 | * 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] | 994 | * least one task in the system (init, pid == 1), therefore, root cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 995 | * 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] | 996 | * need a special hack to ensure that root cgroup cannot be deleted. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 997 | * |
| 998 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 999 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1000 | */ |
| 1001 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1002 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1003 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1004 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 1005 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1006 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 1007 | char *buf) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1008 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1009 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 1010 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) |
| 1011 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s", |
| 1012 | cft->ss->name, cft->name); |
| 1013 | else |
| 1014 | strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
| 1015 | return buf; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1018 | /** |
| 1019 | * cgroup_file_mode - deduce file mode of a control file |
| 1020 | * @cft: the control file in question |
| 1021 | * |
| 1022 | * returns cft->mode if ->mode is not 0 |
| 1023 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 1024 | * returns S_IRUGO if it has only a read handler |
| 1025 | * returns S_IWUSR if it has only a write hander |
| 1026 | */ |
| 1027 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1028 | { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1029 | umode_t mode = 0; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1030 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1031 | if (cft->mode) |
| 1032 | return cft->mode; |
| 1033 | |
| 1034 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 1035 | mode |= S_IRUGO; |
| 1036 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame^] | 1037 | if (cft->write_u64 || cft->write_s64 || cft->write || |
| 1038 | cft->write_string || cft->trigger) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1039 | mode |= S_IWUSR; |
| 1040 | |
| 1041 | return mode; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1044 | static void cgroup_free_fn(struct work_struct *work) |
| 1045 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 1046 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1047 | |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1048 | atomic_dec(&cgrp->root->nr_cgrps); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 1049 | cgroup_pidlist_destroy_all(cgrp); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1050 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1051 | if (cgrp->parent) { |
| 1052 | /* |
| 1053 | * We get a ref to the parent, and put the ref when this |
| 1054 | * cgroup is being freed, so it's guaranteed that the |
| 1055 | * parent won't be destroyed before its children. |
| 1056 | */ |
| 1057 | cgroup_put(cgrp->parent); |
| 1058 | kernfs_put(cgrp->kn); |
| 1059 | kfree(cgrp); |
| 1060 | } else { |
| 1061 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1062 | * This is root cgroup's refcnt reaching zero, which |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1063 | * indicates that the root should be released. |
| 1064 | */ |
| 1065 | cgroup_destroy_root(cgrp->root); |
| 1066 | } |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | static void cgroup_free_rcu(struct rcu_head *head) |
| 1070 | { |
| 1071 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 1072 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 1073 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 1074 | queue_work(cgroup_destroy_wq, &cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1075 | } |
| 1076 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1077 | static void cgroup_get(struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1078 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1079 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
| 1080 | WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0); |
| 1081 | atomic_inc(&cgrp->refcnt); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1084 | static void cgroup_put(struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1085 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1086 | if (!atomic_dec_and_test(&cgrp->refcnt)) |
| 1087 | return; |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1088 | if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp))) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1089 | return; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1090 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1091 | cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1092 | cgrp->id = -1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1093 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1094 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 1097 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1098 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1099 | char name[CGROUP_FILE_NAME_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1100 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1101 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1102 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1105 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 1106 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 1107 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1108 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 1109 | */ |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1110 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1111 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1112 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1113 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1114 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1115 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 1116 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1117 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1118 | if (!(subsys_mask & (1 << i))) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1119 | continue; |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 1120 | list_for_each_entry(cfts, &ss->cfts, node) |
| 1121 | cgroup_addrm_files(cgrp, cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1122 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1125 | 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] | 1126 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1127 | struct cgroup_subsys *ss; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1128 | int ssid, i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1129 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1130 | lockdep_assert_held(&cgroup_tree_mutex); |
| 1131 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1132 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1133 | for_each_subsys(ss, ssid) { |
| 1134 | if (!(ss_mask & (1 << ssid))) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1135 | continue; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1136 | |
Tejun Heo | 7fd8c56 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1137 | /* if @ss has non-root csses attached to it, can't move */ |
| 1138 | if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss))) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1139 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1140 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1141 | /* can't move between two non-dummy roots either */ |
Tejun Heo | 7fd8c56 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1142 | if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1143 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1146 | ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask); |
| 1147 | if (ret) { |
| 1148 | if (dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1149 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1150 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1151 | /* |
| 1152 | * Rebinding back to the default root is not allowed to |
| 1153 | * fail. Using both default and non-default roots should |
| 1154 | * be rare. Moving subsystems back and forth even more so. |
| 1155 | * Just warn about it and continue. |
| 1156 | */ |
| 1157 | if (cgrp_dfl_root_visible) { |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1158 | 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] | 1159 | ret, ss_mask); |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1160 | 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] | 1161 | } |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1162 | } |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1163 | |
| 1164 | /* |
| 1165 | * Nothing can fail from this point on. Remove files for the |
| 1166 | * removed subsystems and rebind each subsystem. |
| 1167 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1168 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1169 | for_each_subsys(ss, ssid) |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1170 | if (ss_mask & (1 << ssid)) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1171 | cgroup_clear_dir(&ss->root->cgrp, 1 << ssid); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1172 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1173 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1174 | for_each_subsys(ss, ssid) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1175 | struct cgroup_root *src_root; |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1176 | struct cgroup_subsys_state *css; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1177 | struct css_set *cset; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1178 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1179 | if (!(ss_mask & (1 << ssid))) |
| 1180 | continue; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1181 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1182 | src_root = ss->root; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1183 | css = cgroup_css(&src_root->cgrp, ss); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1184 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1185 | WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1186 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1187 | RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL); |
| 1188 | rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1189 | ss->root = dst_root; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1190 | css->cgroup = &dst_root->cgrp; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1191 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1192 | down_write(&css_set_rwsem); |
| 1193 | hash_for_each(css_set_table, i, cset, hlist) |
| 1194 | list_move_tail(&cset->e_cset_node[ss->id], |
| 1195 | &dst_root->cgrp.e_csets[ss->id]); |
| 1196 | up_write(&css_set_rwsem); |
| 1197 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1198 | src_root->subsys_mask &= ~(1 << ssid); |
| 1199 | src_root->cgrp.child_subsys_mask &= ~(1 << ssid); |
| 1200 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1201 | /* default hierarchy doesn't enable controllers by default */ |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1202 | dst_root->subsys_mask |= 1 << ssid; |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1203 | if (dst_root != &cgrp_dfl_root) |
| 1204 | dst_root->cgrp.child_subsys_mask |= 1 << ssid; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1205 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1206 | if (ss->bind) |
| 1207 | ss->bind(css); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1208 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1209 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1210 | kernfs_activate(dst_root->cgrp.kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1211 | return 0; |
| 1212 | } |
| 1213 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1214 | static int cgroup_show_options(struct seq_file *seq, |
| 1215 | struct kernfs_root *kf_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1216 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1217 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1218 | struct cgroup_subsys *ss; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1219 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1220 | |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1221 | for_each_subsys(ss, ssid) |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1222 | if (root->subsys_mask & (1 << ssid)) |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1223 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1224 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1225 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1226 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1227 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1228 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1229 | seq_puts(seq, ",xattr"); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1230 | |
| 1231 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1232 | if (strlen(root->release_agent_path)) |
| 1233 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1234 | spin_unlock(&release_agent_path_lock); |
| 1235 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1236 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1237 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1238 | if (strlen(root->name)) |
| 1239 | seq_printf(seq, ",name=%s", root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | struct cgroup_sb_opts { |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1244 | unsigned int subsys_mask; |
| 1245 | unsigned int flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1246 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1247 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1248 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1249 | /* User explicitly requested empty subsystem */ |
| 1250 | bool none; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1251 | }; |
| 1252 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1253 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1254 | { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1255 | char *token, *o = data; |
| 1256 | bool all_ss = false, one_ss = false; |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1257 | unsigned int mask = -1U; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1258 | struct cgroup_subsys *ss; |
| 1259 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1260 | |
| 1261 | #ifdef CONFIG_CPUSETS |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1262 | mask = ~(1U << cpuset_cgrp_id); |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1263 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1264 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1265 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1266 | |
| 1267 | while ((token = strsep(&o, ",")) != NULL) { |
| 1268 | if (!*token) |
| 1269 | return -EINVAL; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1270 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1271 | /* Explicitly have no subsystems */ |
| 1272 | opts->none = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1273 | continue; |
| 1274 | } |
| 1275 | if (!strcmp(token, "all")) { |
| 1276 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1277 | if (one_ss) |
| 1278 | return -EINVAL; |
| 1279 | all_ss = true; |
| 1280 | continue; |
| 1281 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1282 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1283 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1284 | continue; |
| 1285 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1286 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1287 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1288 | continue; |
| 1289 | } |
| 1290 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1291 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1292 | continue; |
| 1293 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1294 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1295 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1296 | continue; |
| 1297 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1298 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1299 | /* Specifying two release agents is forbidden */ |
| 1300 | if (opts->release_agent) |
| 1301 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1302 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1303 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1304 | if (!opts->release_agent) |
| 1305 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1306 | continue; |
| 1307 | } |
| 1308 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1309 | const char *name = token + 5; |
| 1310 | /* Can't specify an empty name */ |
| 1311 | if (!strlen(name)) |
| 1312 | return -EINVAL; |
| 1313 | /* Must match [\w.-]+ */ |
| 1314 | for (i = 0; i < strlen(name); i++) { |
| 1315 | char c = name[i]; |
| 1316 | if (isalnum(c)) |
| 1317 | continue; |
| 1318 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1319 | continue; |
| 1320 | return -EINVAL; |
| 1321 | } |
| 1322 | /* Specifying two names is forbidden */ |
| 1323 | if (opts->name) |
| 1324 | return -EINVAL; |
| 1325 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1326 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1327 | GFP_KERNEL); |
| 1328 | if (!opts->name) |
| 1329 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1330 | |
| 1331 | continue; |
| 1332 | } |
| 1333 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1334 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1335 | if (strcmp(token, ss->name)) |
| 1336 | continue; |
| 1337 | if (ss->disabled) |
| 1338 | continue; |
| 1339 | |
| 1340 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1341 | if (all_ss) |
| 1342 | return -EINVAL; |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1343 | opts->subsys_mask |= (1 << i); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1344 | one_ss = true; |
| 1345 | |
| 1346 | break; |
| 1347 | } |
| 1348 | if (i == CGROUP_SUBSYS_COUNT) |
| 1349 | return -ENOENT; |
| 1350 | } |
| 1351 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1352 | /* Consistency checks */ |
| 1353 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1354 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1355 | 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] | 1356 | |
Tejun Heo | d3ba07c | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1357 | if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) || |
| 1358 | opts->cpuset_clone_children || opts->release_agent || |
| 1359 | opts->name) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1360 | 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] | 1361 | return -EINVAL; |
| 1362 | } |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1363 | } else { |
| 1364 | /* |
| 1365 | * If the 'all' option was specified select all the |
| 1366 | * subsystems, otherwise if 'none', 'name=' and a subsystem |
| 1367 | * name options were not specified, let's default to 'all' |
| 1368 | */ |
| 1369 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1370 | for_each_subsys(ss, i) |
| 1371 | if (!ss->disabled) |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1372 | opts->subsys_mask |= (1 << i); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1373 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1374 | /* |
| 1375 | * We either have to specify by name or by subsystems. (So |
| 1376 | * all empty hierarchies must have a name). |
| 1377 | */ |
| 1378 | if (!opts->subsys_mask && !opts->name) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1379 | return -EINVAL; |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1382 | /* |
| 1383 | * Option noprefix was introduced just for backward compatibility |
| 1384 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1385 | * the cpuset subsystem. |
| 1386 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1387 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1388 | return -EINVAL; |
| 1389 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1390 | |
| 1391 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1392 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1393 | return -EINVAL; |
| 1394 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1395 | return 0; |
| 1396 | } |
| 1397 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1398 | 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] | 1399 | { |
| 1400 | int ret = 0; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1401 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1402 | struct cgroup_sb_opts opts; |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1403 | unsigned int added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1404 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1405 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1406 | pr_err("sane_behavior: remount is not allowed\n"); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1407 | return -EINVAL; |
| 1408 | } |
| 1409 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1410 | mutex_lock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1411 | mutex_lock(&cgroup_mutex); |
| 1412 | |
| 1413 | /* See what subsystems are wanted */ |
| 1414 | ret = parse_cgroupfs_options(data, &opts); |
| 1415 | if (ret) |
| 1416 | goto out_unlock; |
| 1417 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1418 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1419 | 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] | 1420 | task_tgid_nr(current), current->comm); |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1421 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1422 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1423 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1424 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1425 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1426 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1427 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1428 | 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] | 1429 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1430 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1431 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1432 | goto out_unlock; |
| 1433 | } |
| 1434 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1435 | /* remounting is not allowed for populated hierarchies */ |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1436 | if (!list_empty(&root->cgrp.children)) { |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1437 | ret = -EBUSY; |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1438 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1439 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1440 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1441 | ret = rebind_subsystems(root, added_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1442 | if (ret) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1443 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1444 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1445 | rebind_subsystems(&cgrp_dfl_root, removed_mask); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1446 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1447 | if (opts.release_agent) { |
| 1448 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1449 | strcpy(root->release_agent_path, opts.release_agent); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1450 | spin_unlock(&release_agent_path_lock); |
| 1451 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1452 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1453 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1454 | kfree(opts.name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1455 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1456 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1457 | return ret; |
| 1458 | } |
| 1459 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1460 | /* |
| 1461 | * To reduce the fork() overhead for systems that are not actually using |
| 1462 | * their cgroups capability, we don't maintain the lists running through |
| 1463 | * each css_set to its tasks until we see the list actually used - in other |
| 1464 | * words after the first mount. |
| 1465 | */ |
| 1466 | static bool use_task_css_set_links __read_mostly; |
| 1467 | |
| 1468 | static void cgroup_enable_task_cg_lists(void) |
| 1469 | { |
| 1470 | struct task_struct *p, *g; |
| 1471 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1472 | down_write(&css_set_rwsem); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1473 | |
| 1474 | if (use_task_css_set_links) |
| 1475 | goto out_unlock; |
| 1476 | |
| 1477 | use_task_css_set_links = true; |
| 1478 | |
| 1479 | /* |
| 1480 | * We need tasklist_lock because RCU is not safe against |
| 1481 | * while_each_thread(). Besides, a forking task that has passed |
| 1482 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 1483 | * is not guaranteed to have its child immediately visible in the |
| 1484 | * tasklist if we walk through it with RCU. |
| 1485 | */ |
| 1486 | read_lock(&tasklist_lock); |
| 1487 | do_each_thread(g, p) { |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1488 | WARN_ON_ONCE(!list_empty(&p->cg_list) || |
| 1489 | task_css_set(p) != &init_css_set); |
| 1490 | |
| 1491 | /* |
| 1492 | * We should check if the process is exiting, otherwise |
| 1493 | * it will race with cgroup_exit() in that the list |
| 1494 | * entry won't be deleted though the process has exited. |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1495 | * Do it while holding siglock so that we don't end up |
| 1496 | * racing against cgroup_exit(). |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1497 | */ |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1498 | spin_lock_irq(&p->sighand->siglock); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1499 | if (!(p->flags & PF_EXITING)) { |
| 1500 | struct css_set *cset = task_css_set(p); |
| 1501 | |
| 1502 | list_add(&p->cg_list, &cset->tasks); |
| 1503 | get_css_set(cset); |
| 1504 | } |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1505 | spin_unlock_irq(&p->sighand->siglock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1506 | } while_each_thread(g, p); |
| 1507 | read_unlock(&tasklist_lock); |
| 1508 | out_unlock: |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1509 | up_write(&css_set_rwsem); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1510 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1511 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1512 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1513 | { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1514 | struct cgroup_subsys *ss; |
| 1515 | int ssid; |
| 1516 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1517 | atomic_set(&cgrp->refcnt, 1); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1518 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1519 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1520 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1521 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1522 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1523 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1524 | cgrp->dummy_css.cgroup = cgrp; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1525 | |
| 1526 | for_each_subsys(ss, ssid) |
| 1527 | INIT_LIST_HEAD(&cgrp->e_csets[ssid]); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1528 | |
| 1529 | init_waitqueue_head(&cgrp->offline_waitq); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1530 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1531 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1532 | static void init_cgroup_root(struct cgroup_root *root, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1533 | struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1534 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1535 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1536 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1537 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1538 | atomic_set(&root->nr_cgrps, 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1539 | cgrp->root = root; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1540 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1541 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1542 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1543 | root->flags = opts->flags; |
| 1544 | if (opts->release_agent) |
| 1545 | strcpy(root->release_agent_path, opts->release_agent); |
| 1546 | if (opts->name) |
| 1547 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1548 | if (opts->cpuset_clone_children) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1549 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1550 | } |
| 1551 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1552 | 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] | 1553 | { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1554 | LIST_HEAD(tmp_links); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1555 | struct cgroup *root_cgrp = &root->cgrp; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1556 | struct css_set *cset; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1557 | int i, ret; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1558 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1559 | lockdep_assert_held(&cgroup_tree_mutex); |
| 1560 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1561 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1562 | 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] | 1563 | if (ret < 0) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1564 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1565 | root_cgrp->id = ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1566 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1567 | /* |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1568 | * We're accessing css_set_count without locking css_set_rwsem here, |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1569 | * but that's OK - it can only be increased by someone holding |
| 1570 | * cgroup_lock, and that's us. The worst that can happen is that we |
| 1571 | * have some link structures left over |
| 1572 | */ |
| 1573 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1574 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1575 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1576 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1577 | ret = cgroup_init_root_id(root); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1578 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1579 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1580 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1581 | root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops, |
| 1582 | KERNFS_ROOT_CREATE_DEACTIVATED, |
| 1583 | root_cgrp); |
| 1584 | if (IS_ERR(root->kf_root)) { |
| 1585 | ret = PTR_ERR(root->kf_root); |
| 1586 | goto exit_root_id; |
| 1587 | } |
| 1588 | root_cgrp->kn = root->kf_root->kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1589 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1590 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
| 1591 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1592 | goto destroy_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1593 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1594 | ret = rebind_subsystems(root, ss_mask); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1595 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1596 | goto destroy_root; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1597 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1598 | /* |
| 1599 | * There must be no failure case after here, since rebinding takes |
| 1600 | * care of subsystems' refcounts, which are explicitly dropped in |
| 1601 | * the failure exit path. |
| 1602 | */ |
| 1603 | list_add(&root->root_list, &cgroup_roots); |
| 1604 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1605 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1606 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1607 | * Link the root cgroup in this hierarchy into all the css_set |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1608 | * objects. |
| 1609 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1610 | down_write(&css_set_rwsem); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1611 | hash_for_each(css_set_table, i, cset, hlist) |
| 1612 | link_css_set(&tmp_links, cset, root_cgrp); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1613 | up_write(&css_set_rwsem); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1614 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1615 | BUG_ON(!list_empty(&root_cgrp->children)); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1616 | BUG_ON(atomic_read(&root->nr_cgrps) != 1); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1617 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1618 | kernfs_activate(root_cgrp->kn); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1619 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1620 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1621 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1622 | destroy_root: |
| 1623 | kernfs_destroy_root(root->kf_root); |
| 1624 | root->kf_root = NULL; |
| 1625 | exit_root_id: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1626 | cgroup_exit_root_id(root); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1627 | out: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1628 | free_cgrp_cset_links(&tmp_links); |
| 1629 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1632 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1633 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1634 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1635 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1636 | struct cgroup_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1637 | struct cgroup_sb_opts opts; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1638 | struct dentry *dentry; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1639 | int ret; |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 1640 | bool new_sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1641 | |
| 1642 | /* |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1643 | * The first time anyone tries to mount a cgroup, enable the list |
| 1644 | * 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] | 1645 | */ |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1646 | if (!use_task_css_set_links) |
| 1647 | cgroup_enable_task_cg_lists(); |
Li Zefan | e37a06f | 2014-04-17 13:53:08 +0800 | [diff] [blame] | 1648 | |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1649 | mutex_lock(&cgroup_tree_mutex); |
| 1650 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1651 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1652 | /* First find the desired set of subsystems */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1653 | ret = parse_cgroupfs_options(data, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1654 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1655 | goto out_unlock; |
Li Zefan | e37a06f | 2014-04-17 13:53:08 +0800 | [diff] [blame] | 1656 | retry: |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1657 | /* look for a matching existing root */ |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1658 | if (!opts.subsys_mask && !opts.none && !opts.name) { |
| 1659 | cgrp_dfl_root_visible = true; |
| 1660 | root = &cgrp_dfl_root; |
| 1661 | cgroup_get(&root->cgrp); |
| 1662 | ret = 0; |
| 1663 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1666 | for_each_root(root) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1667 | bool name_match = false; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1668 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1669 | if (root == &cgrp_dfl_root) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1670 | continue; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1671 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1672 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1673 | * If we asked for a name then it must match. Also, if |
| 1674 | * name matches but sybsys_mask doesn't, we should fail. |
| 1675 | * Remember whether name matched. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1676 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1677 | if (opts.name) { |
| 1678 | if (strcmp(opts.name, root->name)) |
| 1679 | continue; |
| 1680 | name_match = true; |
| 1681 | } |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1682 | |
| 1683 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1684 | * If we asked for subsystems (or explicitly for no |
| 1685 | * subsystems) then they must match. |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1686 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1687 | if ((opts.subsys_mask || opts.none) && |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1688 | (opts.subsys_mask != root->subsys_mask)) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1689 | if (!name_match) |
| 1690 | continue; |
| 1691 | ret = -EBUSY; |
| 1692 | goto out_unlock; |
| 1693 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1694 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1695 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1696 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1697 | 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] | 1698 | ret = -EINVAL; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1699 | goto out_unlock; |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1700 | } else { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1701 | 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] | 1702 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1703 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1704 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1705 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1706 | * A root's lifetime is governed by its root cgroup. Zero |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1707 | * ref indicate that the root is being destroyed. Wait for |
| 1708 | * destruction to complete so that the subsystems are free. |
| 1709 | * We can use wait_queue for the wait but this path is |
| 1710 | * super cold. Let's just sleep for a bit and retry. |
| 1711 | */ |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1712 | if (!atomic_inc_not_zero(&root->cgrp.refcnt)) { |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1713 | mutex_unlock(&cgroup_mutex); |
| 1714 | mutex_unlock(&cgroup_tree_mutex); |
| 1715 | msleep(10); |
Li Zefan | e37a06f | 2014-04-17 13:53:08 +0800 | [diff] [blame] | 1716 | mutex_lock(&cgroup_tree_mutex); |
| 1717 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1718 | goto retry; |
| 1719 | } |
| 1720 | |
| 1721 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1722 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1725 | /* |
| 1726 | * No such thing, create a new one. name= matching without subsys |
| 1727 | * specification is allowed for already existing hierarchies but we |
| 1728 | * can't create new one without subsys specification. |
| 1729 | */ |
| 1730 | if (!opts.subsys_mask && !opts.none) { |
| 1731 | ret = -EINVAL; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1732 | goto out_unlock; |
| 1733 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1734 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1735 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1736 | if (!root) { |
| 1737 | ret = -ENOMEM; |
| 1738 | goto out_unlock; |
| 1739 | } |
| 1740 | |
| 1741 | init_cgroup_root(root, &opts); |
| 1742 | |
Tejun Heo | 3558557 | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1743 | ret = cgroup_setup_root(root, opts.subsys_mask); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1744 | if (ret) |
| 1745 | cgroup_free_root(root); |
| 1746 | |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1747 | out_unlock: |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1748 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1749 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1750 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1751 | kfree(opts.release_agent); |
| 1752 | kfree(opts.name); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1753 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1754 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1755 | return ERR_PTR(ret); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1756 | |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 1757 | dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb); |
| 1758 | if (IS_ERR(dentry) || !new_sb) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1759 | cgroup_put(&root->cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1760 | return dentry; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 1763 | static void cgroup_kill_sb(struct super_block *sb) |
| 1764 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1765 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1766 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1767 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1768 | cgroup_put(&root->cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1769 | kernfs_kill_sb(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | static struct file_system_type cgroup_fs_type = { |
| 1773 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1774 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1775 | .kill_sb = cgroup_kill_sb, |
| 1776 | }; |
| 1777 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1778 | static struct kobject *cgroup_kobj; |
| 1779 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1780 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1781 | * 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] | 1782 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1783 | * @buf: the buffer to write the path into |
| 1784 | * @buflen: the length of the buffer |
| 1785 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1786 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1787 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1788 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1789 | * cgroup controller callbacks. |
| 1790 | * |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1791 | * Return value is the same as kernfs_path(). |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1792 | */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1793 | 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] | 1794 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1795 | struct cgroup_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1796 | struct cgroup *cgrp; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1797 | int hierarchy_id = 1; |
| 1798 | char *path = NULL; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1799 | |
| 1800 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1801 | down_read(&css_set_rwsem); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1802 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1803 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1804 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1805 | if (root) { |
| 1806 | cgrp = task_cgroup_from_root(task, root); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1807 | path = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1808 | } else { |
| 1809 | /* if no hierarchy exists, everyone is in "/" */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1810 | if (strlcpy(buf, "/", buflen) < buflen) |
| 1811 | path = buf; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1814 | up_read(&css_set_rwsem); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1815 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1816 | return path; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1817 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1818 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1819 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1820 | /* used to track tasks and other necessary states during migration */ |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1821 | struct cgroup_taskset { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1822 | /* the src and dst cset list running through cset->mg_node */ |
| 1823 | struct list_head src_csets; |
| 1824 | struct list_head dst_csets; |
| 1825 | |
| 1826 | /* |
| 1827 | * Fields for cgroup_taskset_*() iteration. |
| 1828 | * |
| 1829 | * Before migration is committed, the target migration tasks are on |
| 1830 | * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of |
| 1831 | * the csets on ->dst_csets. ->csets point to either ->src_csets |
| 1832 | * or ->dst_csets depending on whether migration is committed. |
| 1833 | * |
| 1834 | * ->cur_csets and ->cur_task point to the current task position |
| 1835 | * during iteration. |
| 1836 | */ |
| 1837 | struct list_head *csets; |
| 1838 | struct css_set *cur_cset; |
| 1839 | struct task_struct *cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1840 | }; |
| 1841 | |
| 1842 | /** |
| 1843 | * cgroup_taskset_first - reset taskset and return the first task |
| 1844 | * @tset: taskset of interest |
| 1845 | * |
| 1846 | * @tset iteration is initialized and the first task is returned. |
| 1847 | */ |
| 1848 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1849 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1850 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); |
| 1851 | tset->cur_task = NULL; |
| 1852 | |
| 1853 | return cgroup_taskset_next(tset); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1854 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1855 | |
| 1856 | /** |
| 1857 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1858 | * @tset: taskset of interest |
| 1859 | * |
| 1860 | * Return the next task in @tset. Iteration must have been initialized |
| 1861 | * with cgroup_taskset_first(). |
| 1862 | */ |
| 1863 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1864 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1865 | struct css_set *cset = tset->cur_cset; |
| 1866 | struct task_struct *task = tset->cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1867 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1868 | while (&cset->mg_node != tset->csets) { |
| 1869 | if (!task) |
| 1870 | task = list_first_entry(&cset->mg_tasks, |
| 1871 | struct task_struct, cg_list); |
| 1872 | else |
| 1873 | task = list_next_entry(task, cg_list); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1874 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1875 | if (&task->cg_list != &cset->mg_tasks) { |
| 1876 | tset->cur_cset = cset; |
| 1877 | tset->cur_task = task; |
| 1878 | return task; |
| 1879 | } |
| 1880 | |
| 1881 | cset = list_next_entry(cset, mg_node); |
| 1882 | task = NULL; |
| 1883 | } |
| 1884 | |
| 1885 | return NULL; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1886 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1887 | |
| 1888 | /** |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1889 | * cgroup_task_migrate - move a task from one cgroup to another. |
Fabian Frederick | 6010694 | 2014-05-05 20:08:13 +0200 | [diff] [blame] | 1890 | * @old_cgrp: the cgroup @tsk is being migrated from |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1891 | * @tsk: the task being migrated |
| 1892 | * @new_cset: the new css_set @tsk is being attached to |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1893 | * |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1894 | * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1895 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1896 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1897 | struct task_struct *tsk, |
| 1898 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1899 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1900 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1901 | |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1902 | lockdep_assert_held(&cgroup_mutex); |
| 1903 | lockdep_assert_held(&css_set_rwsem); |
| 1904 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1905 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1906 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1907 | * setting such that we can't race against cgroup_exit() changing the |
| 1908 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1909 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1910 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1911 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1912 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1913 | get_css_set(new_cset); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1914 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1915 | |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 1916 | /* |
| 1917 | * Use move_tail so that cgroup_taskset_first() still returns the |
| 1918 | * leader after migration. This works because cgroup_migrate() |
| 1919 | * ensures that the dst_cset of the leader is the first on the |
| 1920 | * tset's dst_csets list. |
| 1921 | */ |
| 1922 | list_move_tail(&tsk->cg_list, &new_cset->mg_tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1923 | |
| 1924 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1925 | * We just gained a reference on old_cset by taking it from the |
| 1926 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1927 | * 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] | 1928 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1929 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1930 | put_css_set_locked(old_cset, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1931 | } |
| 1932 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1933 | /** |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1934 | * cgroup_migrate_finish - cleanup after attach |
| 1935 | * @preloaded_csets: list of preloaded css_sets |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1936 | * |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1937 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See |
| 1938 | * those functions for details. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1939 | */ |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1940 | static void cgroup_migrate_finish(struct list_head *preloaded_csets) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1941 | { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1942 | struct css_set *cset, *tmp_cset; |
| 1943 | |
| 1944 | lockdep_assert_held(&cgroup_mutex); |
| 1945 | |
| 1946 | down_write(&css_set_rwsem); |
| 1947 | list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) { |
| 1948 | cset->mg_src_cgrp = NULL; |
| 1949 | cset->mg_dst_cset = NULL; |
| 1950 | list_del_init(&cset->mg_preload_node); |
| 1951 | put_css_set_locked(cset, false); |
| 1952 | } |
| 1953 | up_write(&css_set_rwsem); |
| 1954 | } |
| 1955 | |
| 1956 | /** |
| 1957 | * cgroup_migrate_add_src - add a migration source css_set |
| 1958 | * @src_cset: the source css_set to add |
| 1959 | * @dst_cgrp: the destination cgroup |
| 1960 | * @preloaded_csets: list of preloaded css_sets |
| 1961 | * |
| 1962 | * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin |
| 1963 | * @src_cset and add it to @preloaded_csets, which should later be cleaned |
| 1964 | * up by cgroup_migrate_finish(). |
| 1965 | * |
| 1966 | * This function may be called without holding threadgroup_lock even if the |
| 1967 | * target is a process. Threads may be created and destroyed but as long |
| 1968 | * as cgroup_mutex is not dropped, no new css_set can be put into play and |
| 1969 | * the preloaded css_sets are guaranteed to cover all migrations. |
| 1970 | */ |
| 1971 | static void cgroup_migrate_add_src(struct css_set *src_cset, |
| 1972 | struct cgroup *dst_cgrp, |
| 1973 | struct list_head *preloaded_csets) |
| 1974 | { |
| 1975 | struct cgroup *src_cgrp; |
| 1976 | |
| 1977 | lockdep_assert_held(&cgroup_mutex); |
| 1978 | lockdep_assert_held(&css_set_rwsem); |
| 1979 | |
| 1980 | src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); |
| 1981 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1982 | if (!list_empty(&src_cset->mg_preload_node)) |
| 1983 | return; |
| 1984 | |
| 1985 | WARN_ON(src_cset->mg_src_cgrp); |
| 1986 | WARN_ON(!list_empty(&src_cset->mg_tasks)); |
| 1987 | WARN_ON(!list_empty(&src_cset->mg_node)); |
| 1988 | |
| 1989 | src_cset->mg_src_cgrp = src_cgrp; |
| 1990 | get_css_set(src_cset); |
| 1991 | list_add(&src_cset->mg_preload_node, preloaded_csets); |
| 1992 | } |
| 1993 | |
| 1994 | /** |
| 1995 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1996 | * @dst_cgrp: the destination cgroup (may be %NULL) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1997 | * @preloaded_csets: list of preloaded source css_sets |
| 1998 | * |
| 1999 | * Tasks are about to be moved to @dst_cgrp and all the source css_sets |
| 2000 | * have been preloaded to @preloaded_csets. This function looks up and |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2001 | * pins all destination css_sets, links each to its source, and append them |
| 2002 | * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each |
| 2003 | * 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] | 2004 | * |
| 2005 | * This function must be called after cgroup_migrate_add_src() has been |
| 2006 | * called on each migration source css_set. After migration is performed |
| 2007 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on |
| 2008 | * @preloaded_csets. |
| 2009 | */ |
| 2010 | static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp, |
| 2011 | struct list_head *preloaded_csets) |
| 2012 | { |
| 2013 | LIST_HEAD(csets); |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2014 | struct css_set *src_cset, *tmp_cset; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2015 | |
| 2016 | lockdep_assert_held(&cgroup_mutex); |
| 2017 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2018 | /* |
| 2019 | * Except for the root, child_subsys_mask must be zero for a cgroup |
| 2020 | * with tasks so that child cgroups don't compete against tasks. |
| 2021 | */ |
| 2022 | if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent && |
| 2023 | dst_cgrp->child_subsys_mask) |
| 2024 | return -EBUSY; |
| 2025 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2026 | /* 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] | 2027 | 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] | 2028 | struct css_set *dst_cset; |
| 2029 | |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2030 | dst_cset = find_css_set(src_cset, |
| 2031 | dst_cgrp ?: src_cset->dfl_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2032 | if (!dst_cset) |
| 2033 | goto err; |
| 2034 | |
| 2035 | 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] | 2036 | |
| 2037 | /* |
| 2038 | * If src cset equals dst, it's noop. Drop the src. |
| 2039 | * cgroup_migrate() will skip the cset too. Note that we |
| 2040 | * can't handle src == dst as some nodes are used by both. |
| 2041 | */ |
| 2042 | if (src_cset == dst_cset) { |
| 2043 | src_cset->mg_src_cgrp = NULL; |
| 2044 | list_del_init(&src_cset->mg_preload_node); |
| 2045 | put_css_set(src_cset, false); |
| 2046 | put_css_set(dst_cset, false); |
| 2047 | continue; |
| 2048 | } |
| 2049 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2050 | src_cset->mg_dst_cset = dst_cset; |
| 2051 | |
| 2052 | if (list_empty(&dst_cset->mg_preload_node)) |
| 2053 | list_add(&dst_cset->mg_preload_node, &csets); |
| 2054 | else |
| 2055 | put_css_set(dst_cset, false); |
| 2056 | } |
| 2057 | |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2058 | list_splice_tail(&csets, preloaded_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2059 | return 0; |
| 2060 | err: |
| 2061 | cgroup_migrate_finish(&csets); |
| 2062 | return -ENOMEM; |
| 2063 | } |
| 2064 | |
| 2065 | /** |
| 2066 | * cgroup_migrate - migrate a process or task to a cgroup |
| 2067 | * @cgrp: the destination cgroup |
| 2068 | * @leader: the leader of the process or the task to migrate |
| 2069 | * @threadgroup: whether @leader points to the whole process or a single task |
| 2070 | * |
| 2071 | * Migrate a process or task denoted by @leader to @cgrp. If migrating a |
| 2072 | * process, the caller must be holding threadgroup_lock of @leader. The |
| 2073 | * caller is also responsible for invoking cgroup_migrate_add_src() and |
| 2074 | * cgroup_migrate_prepare_dst() on the targets before invoking this |
| 2075 | * function and following up with cgroup_migrate_finish(). |
| 2076 | * |
| 2077 | * As long as a controller's ->can_attach() doesn't fail, this function is |
| 2078 | * guaranteed to succeed. This means that, excluding ->can_attach() |
| 2079 | * failure, when migrating multiple targets, the success or failure can be |
| 2080 | * decided for all targets by invoking group_migrate_prepare_dst() before |
| 2081 | * actually starting migrating. |
| 2082 | */ |
| 2083 | static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader, |
| 2084 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2085 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2086 | struct cgroup_taskset tset = { |
| 2087 | .src_csets = LIST_HEAD_INIT(tset.src_csets), |
| 2088 | .dst_csets = LIST_HEAD_INIT(tset.dst_csets), |
| 2089 | .csets = &tset.src_csets, |
| 2090 | }; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2091 | struct cgroup_subsys_state *css, *failed_css = NULL; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2092 | struct css_set *cset, *tmp_cset; |
| 2093 | struct task_struct *task, *tmp_task; |
| 2094 | int i, ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2095 | |
| 2096 | /* |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2097 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2098 | * already PF_EXITING could be freed from underneath us unless we |
| 2099 | * take an rcu_read_lock. |
| 2100 | */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2101 | down_write(&css_set_rwsem); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2102 | rcu_read_lock(); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2103 | task = leader; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2104 | do { |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2105 | /* @task either already exited or can't exit until the end */ |
| 2106 | if (task->flags & PF_EXITING) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2107 | goto next; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2108 | |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2109 | /* leave @task alone if post_fork() hasn't linked it yet */ |
| 2110 | if (list_empty(&task->cg_list)) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2111 | goto next; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2112 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2113 | cset = task_css_set(task); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2114 | if (!cset->mg_src_cgrp) |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2115 | goto next; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2116 | |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2117 | /* |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 2118 | * cgroup_taskset_first() must always return the leader. |
| 2119 | * Take care to avoid disturbing the ordering. |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2120 | */ |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 2121 | list_move_tail(&task->cg_list, &cset->mg_tasks); |
| 2122 | if (list_empty(&cset->mg_node)) |
| 2123 | list_add_tail(&cset->mg_node, &tset.src_csets); |
| 2124 | if (list_empty(&cset->mg_dst_cset->mg_node)) |
| 2125 | list_move_tail(&cset->mg_dst_cset->mg_node, |
| 2126 | &tset.dst_csets); |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2127 | next: |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2128 | if (!threadgroup) |
| 2129 | break; |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2130 | } while_each_thread(leader, task); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2131 | rcu_read_unlock(); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2132 | up_write(&css_set_rwsem); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2133 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2134 | /* methods shouldn't be called if no task is actually migrating */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2135 | if (list_empty(&tset.src_csets)) |
| 2136 | return 0; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2137 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2138 | /* check that we can legitimately attach to the cgroup */ |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 2139 | for_each_e_css(css, i, cgrp) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2140 | if (css->ss->can_attach) { |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2141 | ret = css->ss->can_attach(css, &tset); |
| 2142 | if (ret) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2143 | failed_css = css; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2144 | goto out_cancel_attach; |
| 2145 | } |
| 2146 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | /* |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2150 | * Now that we're guaranteed success, proceed to move all tasks to |
| 2151 | * the new cgroup. There are no failure cases after here, so this |
| 2152 | * is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2153 | */ |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 2154 | down_write(&css_set_rwsem); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2155 | list_for_each_entry(cset, &tset.src_csets, mg_node) { |
| 2156 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) |
| 2157 | cgroup_task_migrate(cset->mg_src_cgrp, task, |
| 2158 | cset->mg_dst_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2159 | } |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 2160 | up_write(&css_set_rwsem); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2161 | |
| 2162 | /* |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2163 | * Migration is committed, all target tasks are now on dst_csets. |
| 2164 | * Nothing is sensitive to fork() after this point. Notify |
| 2165 | * controllers that migration is complete. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2166 | */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2167 | tset.csets = &tset.dst_csets; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2168 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 2169 | for_each_e_css(css, i, cgrp) |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2170 | if (css->ss->attach) |
| 2171 | css->ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2172 | |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2173 | ret = 0; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2174 | goto out_release_tset; |
| 2175 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2176 | out_cancel_attach: |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 2177 | for_each_e_css(css, i, cgrp) { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2178 | if (css == failed_css) |
| 2179 | break; |
| 2180 | if (css->ss->cancel_attach) |
| 2181 | css->ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2182 | } |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2183 | out_release_tset: |
| 2184 | down_write(&css_set_rwsem); |
| 2185 | list_splice_init(&tset.dst_csets, &tset.src_csets); |
| 2186 | 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] | 2187 | list_splice_tail_init(&cset->mg_tasks, &cset->tasks); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2188 | list_del_init(&cset->mg_node); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2189 | } |
| 2190 | up_write(&css_set_rwsem); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2191 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2192 | } |
| 2193 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2194 | /** |
| 2195 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
| 2196 | * @dst_cgrp: the cgroup to attach to |
| 2197 | * @leader: the task or the leader of the threadgroup to be attached |
| 2198 | * @threadgroup: attach the whole threadgroup? |
| 2199 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2200 | * Call holding cgroup_mutex and threadgroup_lock of @leader. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2201 | */ |
| 2202 | static int cgroup_attach_task(struct cgroup *dst_cgrp, |
| 2203 | struct task_struct *leader, bool threadgroup) |
| 2204 | { |
| 2205 | LIST_HEAD(preloaded_csets); |
| 2206 | struct task_struct *task; |
| 2207 | int ret; |
| 2208 | |
| 2209 | /* look up all src csets */ |
| 2210 | down_read(&css_set_rwsem); |
| 2211 | rcu_read_lock(); |
| 2212 | task = leader; |
| 2213 | do { |
| 2214 | cgroup_migrate_add_src(task_css_set(task), dst_cgrp, |
| 2215 | &preloaded_csets); |
| 2216 | if (!threadgroup) |
| 2217 | break; |
| 2218 | } while_each_thread(leader, task); |
| 2219 | rcu_read_unlock(); |
| 2220 | up_read(&css_set_rwsem); |
| 2221 | |
| 2222 | /* prepare dst csets and commit */ |
| 2223 | ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets); |
| 2224 | if (!ret) |
| 2225 | ret = cgroup_migrate(dst_cgrp, leader, threadgroup); |
| 2226 | |
| 2227 | cgroup_migrate_finish(&preloaded_csets); |
| 2228 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | /* |
| 2232 | * 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] | 2233 | * 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] | 2234 | * cgroup_mutex and threadgroup. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2235 | */ |
| 2236 | 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] | 2237 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2238 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2239 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2240 | int ret; |
| 2241 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2242 | if (!cgroup_lock_live_group(cgrp)) |
| 2243 | return -ENODEV; |
| 2244 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2245 | retry_find_task: |
| 2246 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2247 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2248 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2249 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2250 | rcu_read_unlock(); |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2251 | ret = -ESRCH; |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2252 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2253 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2254 | /* |
| 2255 | * even if we're attaching all tasks in the thread group, we |
| 2256 | * only need to check permissions on one of them. |
| 2257 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2258 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2259 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2260 | !uid_eq(cred->euid, tcred->uid) && |
| 2261 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2262 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2263 | ret = -EACCES; |
| 2264 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2265 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2266 | } else |
| 2267 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2268 | |
| 2269 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2270 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2271 | |
| 2272 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2273 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2274 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2275 | * with no rt_runtime allocated. Just say no. |
| 2276 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2277 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2278 | ret = -EINVAL; |
| 2279 | rcu_read_unlock(); |
| 2280 | goto out_unlock_cgroup; |
| 2281 | } |
| 2282 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2283 | get_task_struct(tsk); |
| 2284 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2285 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2286 | threadgroup_lock(tsk); |
| 2287 | if (threadgroup) { |
| 2288 | if (!thread_group_leader(tsk)) { |
| 2289 | /* |
| 2290 | * a race with de_thread from another thread's exec() |
| 2291 | * may strip us of our leadership, if this happens, |
| 2292 | * there is no choice but to throw this task away and |
| 2293 | * try again; this is |
| 2294 | * "double-double-toil-and-trouble-check locking". |
| 2295 | */ |
| 2296 | threadgroup_unlock(tsk); |
| 2297 | put_task_struct(tsk); |
| 2298 | goto retry_find_task; |
| 2299 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2303 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2304 | threadgroup_unlock(tsk); |
| 2305 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2306 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2307 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2308 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2309 | return ret; |
| 2310 | } |
| 2311 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2312 | /** |
| 2313 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2314 | * @from: attach to all cgroups of a given task |
| 2315 | * @tsk: the task to be attached |
| 2316 | */ |
| 2317 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2318 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2319 | struct cgroup_root *root; |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2320 | int retval = 0; |
| 2321 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2322 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 2323 | for_each_root(root) { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2324 | struct cgroup *from_cgrp; |
| 2325 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2326 | if (root == &cgrp_dfl_root) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 2327 | continue; |
| 2328 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2329 | down_read(&css_set_rwsem); |
| 2330 | from_cgrp = task_cgroup_from_root(from, root); |
| 2331 | up_read(&css_set_rwsem); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2332 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2333 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2334 | if (retval) |
| 2335 | break; |
| 2336 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2337 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2338 | |
| 2339 | return retval; |
| 2340 | } |
| 2341 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2342 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2343 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2344 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2345 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2346 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2349 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2350 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2351 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2352 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2353 | } |
| 2354 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2355 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
Tejun Heo | 4d3bb51 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2356 | struct cftype *cft, char *buffer) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2357 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2358 | struct cgroup_root *root = css->cgroup->root; |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2359 | |
| 2360 | BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2361 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2362 | return -ENODEV; |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2363 | spin_lock(&release_agent_path_lock); |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2364 | strlcpy(root->release_agent_path, buffer, |
| 2365 | sizeof(root->release_agent_path)); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2366 | spin_unlock(&release_agent_path_lock); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2367 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2368 | return 0; |
| 2369 | } |
| 2370 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2371 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2372 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2373 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2374 | |
Tejun Heo | 46cfeb0 | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2375 | spin_lock(&release_agent_path_lock); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2376 | seq_puts(seq, cgrp->root->release_agent_path); |
Tejun Heo | 46cfeb0 | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2377 | spin_unlock(&release_agent_path_lock); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2378 | seq_putc(seq, '\n'); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2379 | return 0; |
| 2380 | } |
| 2381 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2382 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2383 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2384 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2385 | |
| 2386 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2387 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2388 | } |
| 2389 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2390 | static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask) |
| 2391 | { |
| 2392 | struct cgroup_subsys *ss; |
| 2393 | bool printed = false; |
| 2394 | int ssid; |
| 2395 | |
| 2396 | for_each_subsys(ss, ssid) { |
| 2397 | if (ss_mask & (1 << ssid)) { |
| 2398 | if (printed) |
| 2399 | seq_putc(seq, ' '); |
| 2400 | seq_printf(seq, "%s", ss->name); |
| 2401 | printed = true; |
| 2402 | } |
| 2403 | } |
| 2404 | if (printed) |
| 2405 | seq_putc(seq, '\n'); |
| 2406 | } |
| 2407 | |
| 2408 | /* show controllers which are currently attached to the default hierarchy */ |
| 2409 | static int cgroup_root_controllers_show(struct seq_file *seq, void *v) |
| 2410 | { |
| 2411 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2412 | |
| 2413 | cgroup_print_ss_mask(seq, cgrp->root->subsys_mask); |
| 2414 | return 0; |
| 2415 | } |
| 2416 | |
| 2417 | /* show controllers which are enabled from the parent */ |
| 2418 | static int cgroup_controllers_show(struct seq_file *seq, void *v) |
| 2419 | { |
| 2420 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2421 | |
| 2422 | cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask); |
| 2423 | return 0; |
| 2424 | } |
| 2425 | |
| 2426 | /* show controllers which are enabled for a given cgroup's children */ |
| 2427 | static int cgroup_subtree_control_show(struct seq_file *seq, void *v) |
| 2428 | { |
| 2429 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2430 | |
| 2431 | cgroup_print_ss_mask(seq, cgrp->child_subsys_mask); |
| 2432 | return 0; |
| 2433 | } |
| 2434 | |
| 2435 | /** |
| 2436 | * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy |
| 2437 | * @cgrp: root of the subtree to update csses for |
| 2438 | * |
| 2439 | * @cgrp's child_subsys_mask has changed and its subtree's (self excluded) |
| 2440 | * css associations need to be updated accordingly. This function looks up |
| 2441 | * all css_sets which are attached to the subtree, creates the matching |
| 2442 | * updated css_sets and migrates the tasks to the new ones. |
| 2443 | */ |
| 2444 | static int cgroup_update_dfl_csses(struct cgroup *cgrp) |
| 2445 | { |
| 2446 | LIST_HEAD(preloaded_csets); |
| 2447 | struct cgroup_subsys_state *css; |
| 2448 | struct css_set *src_cset; |
| 2449 | int ret; |
| 2450 | |
| 2451 | lockdep_assert_held(&cgroup_tree_mutex); |
| 2452 | lockdep_assert_held(&cgroup_mutex); |
| 2453 | |
| 2454 | /* look up all csses currently attached to @cgrp's subtree */ |
| 2455 | down_read(&css_set_rwsem); |
| 2456 | css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) { |
| 2457 | struct cgrp_cset_link *link; |
| 2458 | |
| 2459 | /* self is not affected by child_subsys_mask change */ |
| 2460 | if (css->cgroup == cgrp) |
| 2461 | continue; |
| 2462 | |
| 2463 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) |
| 2464 | cgroup_migrate_add_src(link->cset, cgrp, |
| 2465 | &preloaded_csets); |
| 2466 | } |
| 2467 | up_read(&css_set_rwsem); |
| 2468 | |
| 2469 | /* NULL dst indicates self on default hierarchy */ |
| 2470 | ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets); |
| 2471 | if (ret) |
| 2472 | goto out_finish; |
| 2473 | |
| 2474 | list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) { |
| 2475 | struct task_struct *last_task = NULL, *task; |
| 2476 | |
| 2477 | /* src_csets precede dst_csets, break on the first dst_cset */ |
| 2478 | if (!src_cset->mg_src_cgrp) |
| 2479 | break; |
| 2480 | |
| 2481 | /* |
| 2482 | * All tasks in src_cset need to be migrated to the |
| 2483 | * matching dst_cset. Empty it process by process. We |
| 2484 | * walk tasks but migrate processes. The leader might even |
| 2485 | * belong to a different cset but such src_cset would also |
| 2486 | * be among the target src_csets because the default |
| 2487 | * hierarchy enforces per-process membership. |
| 2488 | */ |
| 2489 | while (true) { |
| 2490 | down_read(&css_set_rwsem); |
| 2491 | task = list_first_entry_or_null(&src_cset->tasks, |
| 2492 | struct task_struct, cg_list); |
| 2493 | if (task) { |
| 2494 | task = task->group_leader; |
| 2495 | WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp); |
| 2496 | get_task_struct(task); |
| 2497 | } |
| 2498 | up_read(&css_set_rwsem); |
| 2499 | |
| 2500 | if (!task) |
| 2501 | break; |
| 2502 | |
| 2503 | /* guard against possible infinite loop */ |
| 2504 | if (WARN(last_task == task, |
| 2505 | "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n")) |
| 2506 | goto out_finish; |
| 2507 | last_task = task; |
| 2508 | |
| 2509 | threadgroup_lock(task); |
| 2510 | /* raced against de_thread() from another thread? */ |
| 2511 | if (!thread_group_leader(task)) { |
| 2512 | threadgroup_unlock(task); |
| 2513 | put_task_struct(task); |
| 2514 | continue; |
| 2515 | } |
| 2516 | |
| 2517 | ret = cgroup_migrate(src_cset->dfl_cgrp, task, true); |
| 2518 | |
| 2519 | threadgroup_unlock(task); |
| 2520 | put_task_struct(task); |
| 2521 | |
| 2522 | if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret)) |
| 2523 | goto out_finish; |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | out_finish: |
| 2528 | cgroup_migrate_finish(&preloaded_csets); |
| 2529 | return ret; |
| 2530 | } |
| 2531 | |
| 2532 | /* change the enabled child controllers for a cgroup in the default hierarchy */ |
| 2533 | static int cgroup_subtree_control_write(struct cgroup_subsys_state *dummy_css, |
| 2534 | struct cftype *cft, char *buffer) |
| 2535 | { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2536 | unsigned int enable = 0, disable = 0; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2537 | struct cgroup *cgrp = dummy_css->cgroup, *child; |
| 2538 | struct cgroup_subsys *ss; |
| 2539 | char *tok, *p; |
| 2540 | int ssid, ret; |
| 2541 | |
| 2542 | /* |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2543 | * Parse input - space separated list of subsystem names prefixed |
| 2544 | * with either + or -. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2545 | */ |
| 2546 | p = buffer; |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2547 | while ((tok = strsep(&p, " "))) { |
| 2548 | if (tok[0] == '\0') |
| 2549 | continue; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2550 | for_each_subsys(ss, ssid) { |
| 2551 | if (ss->disabled || strcmp(tok + 1, ss->name)) |
| 2552 | continue; |
| 2553 | |
| 2554 | if (*tok == '+') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2555 | enable |= 1 << ssid; |
| 2556 | disable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2557 | } else if (*tok == '-') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2558 | disable |= 1 << ssid; |
| 2559 | enable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2560 | } else { |
| 2561 | return -EINVAL; |
| 2562 | } |
| 2563 | break; |
| 2564 | } |
| 2565 | if (ssid == CGROUP_SUBSYS_COUNT) |
| 2566 | return -EINVAL; |
| 2567 | } |
| 2568 | |
| 2569 | /* |
| 2570 | * We're gonna grab cgroup_tree_mutex which nests outside kernfs |
| 2571 | * active_ref. cgroup_lock_live_group() already provides enough |
| 2572 | * protection. Ensure @cgrp stays accessible and break the |
| 2573 | * active_ref protection. |
| 2574 | */ |
| 2575 | cgroup_get(cgrp); |
| 2576 | kernfs_break_active_protection(cgrp->control_kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2577 | |
| 2578 | mutex_lock(&cgroup_tree_mutex); |
| 2579 | |
| 2580 | for_each_subsys(ss, ssid) { |
| 2581 | if (enable & (1 << ssid)) { |
| 2582 | if (cgrp->child_subsys_mask & (1 << ssid)) { |
| 2583 | enable &= ~(1 << ssid); |
| 2584 | continue; |
| 2585 | } |
| 2586 | |
| 2587 | /* |
| 2588 | * Because css offlining is asynchronous, userland |
| 2589 | * might try to re-enable the same controller while |
| 2590 | * the previous instance is still around. In such |
| 2591 | * cases, wait till it's gone using offline_waitq. |
| 2592 | */ |
| 2593 | cgroup_for_each_live_child(child, cgrp) { |
Tejun Heo | 0cee8b7 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2594 | DEFINE_WAIT(wait); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2595 | |
| 2596 | if (!cgroup_css(child, ss)) |
| 2597 | continue; |
| 2598 | |
Tejun Heo | 0cee8b7 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2599 | cgroup_get(child); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2600 | prepare_to_wait(&child->offline_waitq, &wait, |
| 2601 | TASK_UNINTERRUPTIBLE); |
| 2602 | mutex_unlock(&cgroup_tree_mutex); |
| 2603 | schedule(); |
| 2604 | finish_wait(&child->offline_waitq, &wait); |
Tejun Heo | 0cee8b7 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2605 | cgroup_put(child); |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2606 | |
| 2607 | ret = restart_syscall(); |
| 2608 | goto out_unbreak; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | /* unavailable or not enabled on the parent? */ |
| 2612 | if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) || |
| 2613 | (cgrp->parent && |
| 2614 | !(cgrp->parent->child_subsys_mask & (1 << ssid)))) { |
| 2615 | ret = -ENOENT; |
| 2616 | goto out_unlock_tree; |
| 2617 | } |
| 2618 | } else if (disable & (1 << ssid)) { |
| 2619 | if (!(cgrp->child_subsys_mask & (1 << ssid))) { |
| 2620 | disable &= ~(1 << ssid); |
| 2621 | continue; |
| 2622 | } |
| 2623 | |
| 2624 | /* a child has it enabled? */ |
| 2625 | cgroup_for_each_live_child(child, cgrp) { |
| 2626 | if (child->child_subsys_mask & (1 << ssid)) { |
| 2627 | ret = -EBUSY; |
| 2628 | goto out_unlock_tree; |
| 2629 | } |
| 2630 | } |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | if (!enable && !disable) { |
| 2635 | ret = 0; |
| 2636 | goto out_unlock_tree; |
| 2637 | } |
| 2638 | |
| 2639 | if (!cgroup_lock_live_group(cgrp)) { |
| 2640 | ret = -ENODEV; |
| 2641 | goto out_unlock_tree; |
| 2642 | } |
| 2643 | |
| 2644 | /* |
| 2645 | * Except for the root, child_subsys_mask must be zero for a cgroup |
| 2646 | * with tasks so that child cgroups don't compete against tasks. |
| 2647 | */ |
| 2648 | if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) { |
| 2649 | ret = -EBUSY; |
| 2650 | goto out_unlock; |
| 2651 | } |
| 2652 | |
| 2653 | /* |
| 2654 | * Create csses for enables and update child_subsys_mask. This |
| 2655 | * changes cgroup_e_css() results which in turn makes the |
| 2656 | * subsequent cgroup_update_dfl_csses() associate all tasks in the |
| 2657 | * subtree to the updated csses. |
| 2658 | */ |
| 2659 | for_each_subsys(ss, ssid) { |
| 2660 | if (!(enable & (1 << ssid))) |
| 2661 | continue; |
| 2662 | |
| 2663 | cgroup_for_each_live_child(child, cgrp) { |
| 2664 | ret = create_css(child, ss); |
| 2665 | if (ret) |
| 2666 | goto err_undo_css; |
| 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | cgrp->child_subsys_mask |= enable; |
| 2671 | cgrp->child_subsys_mask &= ~disable; |
| 2672 | |
| 2673 | ret = cgroup_update_dfl_csses(cgrp); |
| 2674 | if (ret) |
| 2675 | goto err_undo_css; |
| 2676 | |
| 2677 | /* all tasks are now migrated away from the old csses, kill them */ |
| 2678 | for_each_subsys(ss, ssid) { |
| 2679 | if (!(disable & (1 << ssid))) |
| 2680 | continue; |
| 2681 | |
| 2682 | cgroup_for_each_live_child(child, cgrp) |
| 2683 | kill_css(cgroup_css(child, ss)); |
| 2684 | } |
| 2685 | |
| 2686 | kernfs_activate(cgrp->kn); |
| 2687 | ret = 0; |
| 2688 | out_unlock: |
| 2689 | mutex_unlock(&cgroup_mutex); |
| 2690 | out_unlock_tree: |
| 2691 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2692 | out_unbreak: |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2693 | kernfs_unbreak_active_protection(cgrp->control_kn); |
| 2694 | cgroup_put(cgrp); |
| 2695 | return ret; |
| 2696 | |
| 2697 | err_undo_css: |
| 2698 | cgrp->child_subsys_mask &= ~enable; |
| 2699 | cgrp->child_subsys_mask |= disable; |
| 2700 | |
| 2701 | for_each_subsys(ss, ssid) { |
| 2702 | if (!(enable & (1 << ssid))) |
| 2703 | continue; |
| 2704 | |
| 2705 | cgroup_for_each_live_child(child, cgrp) { |
| 2706 | struct cgroup_subsys_state *css = cgroup_css(child, ss); |
| 2707 | if (css) |
| 2708 | kill_css(css); |
| 2709 | } |
| 2710 | } |
| 2711 | goto out_unlock; |
| 2712 | } |
| 2713 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 2714 | static int cgroup_populated_show(struct seq_file *seq, void *v) |
| 2715 | { |
| 2716 | seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt); |
| 2717 | return 0; |
| 2718 | } |
| 2719 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2720 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 2721 | size_t nbytes, loff_t off) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2722 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2723 | struct cgroup *cgrp = of->kn->parent->priv; |
| 2724 | struct cftype *cft = of->kn->priv; |
| 2725 | struct cgroup_subsys_state *css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2726 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2727 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame^] | 2728 | if (cft->write) |
| 2729 | return cft->write(of, buf, nbytes, off); |
| 2730 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2731 | /* |
| 2732 | * kernfs guarantees that a file isn't deleted with operations in |
| 2733 | * flight, which means that the matching css is and stays alive and |
| 2734 | * doesn't need to be pinned. The RCU locking is not necessary |
| 2735 | * either. It's just for the convenience of using cgroup_css(). |
| 2736 | */ |
| 2737 | rcu_read_lock(); |
| 2738 | css = cgroup_css(cgrp, cft->ss); |
| 2739 | rcu_read_unlock(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2740 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2741 | if (cft->write_string) { |
| 2742 | ret = cft->write_string(css, cft, strstrip(buf)); |
| 2743 | } else if (cft->write_u64) { |
| 2744 | unsigned long long v; |
| 2745 | ret = kstrtoull(buf, 0, &v); |
| 2746 | if (!ret) |
| 2747 | ret = cft->write_u64(css, cft, v); |
| 2748 | } else if (cft->write_s64) { |
| 2749 | long long v; |
| 2750 | ret = kstrtoll(buf, 0, &v); |
| 2751 | if (!ret) |
| 2752 | ret = cft->write_s64(css, cft, v); |
| 2753 | } else if (cft->trigger) { |
| 2754 | ret = cft->trigger(css, (unsigned int)cft->private); |
| 2755 | } else { |
| 2756 | ret = -EINVAL; |
| 2757 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2758 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2759 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2760 | } |
| 2761 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2762 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2763 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2764 | return seq_cft(seq)->seq_start(seq, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 2768 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2769 | return seq_cft(seq)->seq_next(seq, v, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 2773 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2774 | seq_cft(seq)->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2775 | } |
| 2776 | |
| 2777 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2778 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2779 | struct cftype *cft = seq_cft(m); |
| 2780 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2781 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2782 | if (cft->seq_show) |
| 2783 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2784 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2785 | if (cft->read_u64) |
| 2786 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 2787 | else if (cft->read_s64) |
| 2788 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 2789 | else |
| 2790 | return -EINVAL; |
| 2791 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2792 | } |
| 2793 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2794 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 2795 | .atomic_write_len = PAGE_SIZE, |
| 2796 | .write = cgroup_file_write, |
| 2797 | .seq_show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2798 | }; |
| 2799 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2800 | static struct kernfs_ops cgroup_kf_ops = { |
| 2801 | .atomic_write_len = PAGE_SIZE, |
| 2802 | .write = cgroup_file_write, |
| 2803 | .seq_start = cgroup_seqfile_start, |
| 2804 | .seq_next = cgroup_seqfile_next, |
| 2805 | .seq_stop = cgroup_seqfile_stop, |
| 2806 | .seq_show = cgroup_seqfile_show, |
| 2807 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2808 | |
| 2809 | /* |
| 2810 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2811 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2812 | static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent, |
| 2813 | const char *new_name_str) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2814 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2815 | struct cgroup *cgrp = kn->priv; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2816 | int ret; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2817 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2818 | if (kernfs_type(kn) != KERNFS_DIR) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2819 | return -ENOTDIR; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2820 | if (kn->parent != new_parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2821 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2822 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2823 | /* |
| 2824 | * This isn't a proper migration and its usefulness is very |
| 2825 | * limited. Disallow if sane_behavior. |
| 2826 | */ |
| 2827 | if (cgroup_sane_behavior(cgrp)) |
| 2828 | return -EPERM; |
| 2829 | |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 2830 | /* |
| 2831 | * We're gonna grab cgroup_tree_mutex which nests outside kernfs |
| 2832 | * active_ref. kernfs_rename() doesn't require active_ref |
| 2833 | * protection. Break them before grabbing cgroup_tree_mutex. |
| 2834 | */ |
| 2835 | kernfs_break_active_protection(new_parent); |
| 2836 | kernfs_break_active_protection(kn); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2837 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2838 | mutex_lock(&cgroup_tree_mutex); |
| 2839 | mutex_lock(&cgroup_mutex); |
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 | ret = kernfs_rename(kn, new_parent, new_name_str); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2842 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2843 | mutex_unlock(&cgroup_mutex); |
| 2844 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2845 | |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 2846 | kernfs_unbreak_active_protection(kn); |
| 2847 | kernfs_unbreak_active_protection(new_parent); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2848 | return ret; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2849 | } |
| 2850 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2851 | /* set uid and gid of cgroup dirs and files to that of the creator */ |
| 2852 | static int cgroup_kn_set_ugid(struct kernfs_node *kn) |
| 2853 | { |
| 2854 | struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, |
| 2855 | .ia_uid = current_fsuid(), |
| 2856 | .ia_gid = current_fsgid(), }; |
| 2857 | |
| 2858 | if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && |
| 2859 | gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) |
| 2860 | return 0; |
| 2861 | |
| 2862 | return kernfs_setattr(kn, &iattr); |
| 2863 | } |
| 2864 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2865 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2866 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2867 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2868 | struct kernfs_node *kn; |
| 2869 | struct lock_class_key *key = NULL; |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2870 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2871 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2872 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 2873 | key = &cft->lockdep_key; |
| 2874 | #endif |
| 2875 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
| 2876 | cgroup_file_mode(cft), 0, cft->kf_ops, cft, |
| 2877 | NULL, false, key); |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2878 | if (IS_ERR(kn)) |
| 2879 | return PTR_ERR(kn); |
| 2880 | |
| 2881 | ret = cgroup_kn_set_ugid(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2882 | if (ret) { |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2883 | kernfs_remove(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2884 | return ret; |
| 2885 | } |
| 2886 | |
| 2887 | if (cft->seq_show == cgroup_subtree_control_show) |
| 2888 | cgrp->control_kn = kn; |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 2889 | else if (cft->seq_show == cgroup_populated_show) |
| 2890 | cgrp->populated_kn = kn; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2891 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2892 | } |
| 2893 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2894 | /** |
| 2895 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2896 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2897 | * @cfts: array of cftypes to be added |
| 2898 | * @is_add: whether to add or remove |
| 2899 | * |
| 2900 | * 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] | 2901 | * For removals, this function never fails. If addition fails, this |
| 2902 | * function doesn't remove files already added. The caller is responsible |
| 2903 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2904 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2905 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2906 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2907 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2908 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2909 | int ret; |
| 2910 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2911 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2912 | |
| 2913 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2914 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 8cbbf2c | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 2915 | if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) |
| 2916 | continue; |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2917 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2918 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2919 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2920 | continue; |
| 2921 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2922 | continue; |
| 2923 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2924 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2925 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2926 | if (ret) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 2927 | pr_warn("%s: failed to add %s, err=%d\n", |
| 2928 | __func__, cft->name, ret); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2929 | return ret; |
| 2930 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2931 | } else { |
| 2932 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2933 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2934 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2935 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2936 | } |
| 2937 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2938 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2939 | { |
| 2940 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2941 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2942 | struct cgroup *root = &ss->root->cgrp; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2943 | struct cgroup_subsys_state *css; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2944 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2945 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2946 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2947 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2948 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2949 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2950 | struct cgroup *cgrp = css->cgroup; |
| 2951 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2952 | if (cgroup_is_dead(cgrp)) |
| 2953 | continue; |
| 2954 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2955 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2956 | if (ret) |
| 2957 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2958 | } |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2959 | |
| 2960 | if (is_add && !ret) |
| 2961 | kernfs_activate(root->kn); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2962 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2963 | } |
| 2964 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2965 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 2966 | { |
| 2967 | struct cftype *cft; |
| 2968 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2969 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2970 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 2971 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 2972 | kfree(cft->kf_ops); |
| 2973 | cft->kf_ops = NULL; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2974 | cft->ss = NULL; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2975 | } |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2976 | } |
| 2977 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2978 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2979 | { |
| 2980 | struct cftype *cft; |
| 2981 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2982 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2983 | struct kernfs_ops *kf_ops; |
| 2984 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2985 | WARN_ON(cft->ss || cft->kf_ops); |
| 2986 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2987 | if (cft->seq_start) |
| 2988 | kf_ops = &cgroup_kf_ops; |
| 2989 | else |
| 2990 | kf_ops = &cgroup_kf_single_ops; |
| 2991 | |
| 2992 | /* |
| 2993 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 2994 | * make a copy of kf_ops to set its atomic_write_len. |
| 2995 | */ |
| 2996 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 2997 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 2998 | if (!kf_ops) { |
| 2999 | cgroup_exit_cftypes(cfts); |
| 3000 | return -ENOMEM; |
| 3001 | } |
| 3002 | kf_ops->atomic_write_len = cft->max_write_len; |
| 3003 | } |
| 3004 | |
| 3005 | cft->kf_ops = kf_ops; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3006 | cft->ss = ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | return 0; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3010 | } |
| 3011 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3012 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 3013 | { |
| 3014 | lockdep_assert_held(&cgroup_tree_mutex); |
| 3015 | |
| 3016 | if (!cfts || !cfts[0].ss) |
| 3017 | return -ENOENT; |
| 3018 | |
| 3019 | list_del(&cfts->node); |
| 3020 | cgroup_apply_cftypes(cfts, false); |
| 3021 | cgroup_exit_cftypes(cfts); |
| 3022 | return 0; |
| 3023 | } |
| 3024 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3025 | /** |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3026 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 3027 | * @cfts: zero-length name terminated array of cftypes |
| 3028 | * |
| 3029 | * Unregister @cfts. Files described by @cfts are removed from all |
| 3030 | * existing cgroups and all future cgroups won't have them either. This |
| 3031 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 3032 | * |
| 3033 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 3034 | * registered. |
| 3035 | */ |
| 3036 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 3037 | { |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3038 | int ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3039 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3040 | mutex_lock(&cgroup_tree_mutex); |
| 3041 | ret = cgroup_rm_cftypes_locked(cfts); |
| 3042 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3043 | return ret; |
| 3044 | } |
| 3045 | |
| 3046 | /** |
| 3047 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 3048 | * @ss: target cgroup subsystem |
| 3049 | * @cfts: zero-length name terminated array of cftypes |
| 3050 | * |
| 3051 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 3052 | * existing cgroups to which @ss is attached and all future cgroups will |
| 3053 | * have them too. This function can be called anytime whether @ss is |
| 3054 | * attached or not. |
| 3055 | * |
| 3056 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 3057 | * function currently returns 0 as long as @cfts registration is successful |
| 3058 | * even if some file creation attempts on existing cgroups fail. |
| 3059 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 3060 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3061 | { |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3062 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3063 | |
Li Zefan | dc5736e | 2014-02-17 10:41:50 +0800 | [diff] [blame] | 3064 | if (!cfts || cfts[0].name[0] == '\0') |
| 3065 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3066 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3067 | ret = cgroup_init_cftypes(ss, cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3068 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3069 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3070 | |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3071 | mutex_lock(&cgroup_tree_mutex); |
| 3072 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3073 | list_add_tail(&cfts->node, &ss->cfts); |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3074 | ret = cgroup_apply_cftypes(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3075 | if (ret) |
Tejun Heo | 21a2d343 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3076 | cgroup_rm_cftypes_locked(cfts); |
| 3077 | |
| 3078 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3079 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3080 | } |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3081 | |
| 3082 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3083 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 3084 | * @cgrp: the cgroup in question |
| 3085 | * |
| 3086 | * Return the number of tasks in the cgroup. |
| 3087 | */ |
Tejun Heo | 07bc356 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3088 | static int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3089 | { |
| 3090 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3091 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3092 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3093 | down_read(&css_set_rwsem); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3094 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 3095 | count += atomic_read(&link->cset->refcount); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3096 | up_read(&css_set_rwsem); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3097 | return count; |
| 3098 | } |
| 3099 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3100 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3101 | * css_next_child - find the next child of a given css |
| 3102 | * @pos_css: the current position (%NULL to initiate traversal) |
| 3103 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3104 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3105 | * 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] | 3106 | * under either cgroup_mutex or RCU read lock. The only requirement is |
| 3107 | * that @parent_css and @pos_css are accessible. The next sibling is |
| 3108 | * guaranteed to be returned regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3109 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3110 | struct cgroup_subsys_state * |
| 3111 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 3112 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3113 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3114 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 3115 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3116 | struct cgroup *next; |
| 3117 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3118 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3119 | |
| 3120 | /* |
| 3121 | * @pos could already have been removed. Once a cgroup is removed, |
| 3122 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3123 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 3124 | * before the cgroup is taken off the ->sibling list, if we see it |
| 3125 | * unasserted, it's guaranteed that the next sibling hasn't |
| 3126 | * finished its grace period even if it's already removed, and thus |
| 3127 | * safe to dereference from this RCU critical section. If |
| 3128 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 3129 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3130 | * |
| 3131 | * If @pos is dead, its next pointer can't be dereferenced; |
| 3132 | * however, as each cgroup is given a monotonically increasing |
| 3133 | * unique serial number and always appended to the sibling list, |
| 3134 | * the next one can be found by walking the parent's children until |
| 3135 | * we see a cgroup with higher serial number than @pos's. While |
| 3136 | * this path can be slower, it's taken only when either the current |
| 3137 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3138 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3139 | if (!pos) { |
| 3140 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 3141 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3142 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3143 | } else { |
| 3144 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 3145 | if (next->serial_nr > pos->serial_nr) |
| 3146 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3147 | } |
| 3148 | |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3149 | /* |
| 3150 | * @next, if not pointing to the head, can be dereferenced and is |
| 3151 | * the next sibling; however, it might have @ss disabled. If so, |
| 3152 | * fast-forward to the next enabled one. |
| 3153 | */ |
| 3154 | while (&next->sibling != &cgrp->children) { |
| 3155 | struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss); |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3156 | |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3157 | if (next_css) |
| 3158 | return next_css; |
| 3159 | next = list_entry_rcu(next->sibling.next, struct cgroup, sibling); |
| 3160 | } |
| 3161 | return NULL; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3162 | } |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3163 | |
| 3164 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3165 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3166 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3167 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3168 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3169 | * 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] | 3170 | * to visit for pre-order traversal of @root's descendants. @root is |
| 3171 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3172 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3173 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3174 | * doesn't require the whole traversal to be contained in a single critical |
| 3175 | * section. This function will return the correct next descendant as long |
| 3176 | * 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] | 3177 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3178 | struct cgroup_subsys_state * |
| 3179 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 3180 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3181 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3182 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3183 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3184 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3185 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3186 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3187 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3188 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3189 | |
| 3190 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3191 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3192 | if (next) |
| 3193 | return next; |
| 3194 | |
| 3195 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3196 | while (pos != root) { |
| 3197 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3198 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3199 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3200 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3201 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3202 | |
| 3203 | return NULL; |
| 3204 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3205 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3206 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3207 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 3208 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3209 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3210 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 3211 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3212 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3213 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3214 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3215 | * doesn't require the whole traversal to be contained in a single critical |
| 3216 | * section. This function will return the correct rightmost descendant as |
| 3217 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3218 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3219 | struct cgroup_subsys_state * |
| 3220 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3221 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3222 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3223 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3224 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3225 | |
| 3226 | do { |
| 3227 | last = pos; |
| 3228 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3229 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3230 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3231 | pos = tmp; |
| 3232 | } while (pos); |
| 3233 | |
| 3234 | return last; |
| 3235 | } |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3236 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3237 | static struct cgroup_subsys_state * |
| 3238 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3239 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3240 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3241 | |
| 3242 | do { |
| 3243 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3244 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3245 | } while (pos); |
| 3246 | |
| 3247 | return last; |
| 3248 | } |
| 3249 | |
| 3250 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3251 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3252 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3253 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3254 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3255 | * 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] | 3256 | * to visit for post-order traversal of @root's descendants. @root is |
| 3257 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3258 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3259 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3260 | * doesn't require the whole traversal to be contained in a single critical |
| 3261 | * section. This function will return the correct next descendant as long |
| 3262 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 3263 | * @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3264 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3265 | struct cgroup_subsys_state * |
| 3266 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 3267 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3268 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3269 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3270 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3271 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3272 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 3273 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 3274 | if (!pos) |
| 3275 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3276 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3277 | /* if we visited @root, we're done */ |
| 3278 | if (pos == root) |
| 3279 | return NULL; |
| 3280 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3281 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3282 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3283 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3284 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3285 | |
| 3286 | /* no sibling left, visit parent */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3287 | return css_parent(pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3288 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3289 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3290 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3291 | * 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] | 3292 | * @it: the iterator to advance |
| 3293 | * |
| 3294 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3295 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3296 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3297 | { |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3298 | struct list_head *l = it->cset_pos; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3299 | struct cgrp_cset_link *link; |
| 3300 | struct css_set *cset; |
| 3301 | |
| 3302 | /* Advance to the next non-empty css_set */ |
| 3303 | do { |
| 3304 | l = l->next; |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3305 | if (l == it->cset_head) { |
| 3306 | it->cset_pos = NULL; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3307 | return; |
| 3308 | } |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3309 | |
| 3310 | if (it->ss) { |
| 3311 | cset = container_of(l, struct css_set, |
| 3312 | e_cset_node[it->ss->id]); |
| 3313 | } else { |
| 3314 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3315 | cset = link->cset; |
| 3316 | } |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3317 | } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks)); |
| 3318 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3319 | it->cset_pos = l; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3320 | |
| 3321 | if (!list_empty(&cset->tasks)) |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3322 | it->task_pos = cset->tasks.next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3323 | else |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3324 | it->task_pos = cset->mg_tasks.next; |
| 3325 | |
| 3326 | it->tasks_head = &cset->tasks; |
| 3327 | it->mg_tasks_head = &cset->mg_tasks; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3328 | } |
| 3329 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3330 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3331 | * css_task_iter_start - initiate task iteration |
| 3332 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3333 | * @it: the task iterator to use |
| 3334 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3335 | * Initiate iteration through the tasks of @css. The caller can call |
| 3336 | * css_task_iter_next() to walk through the tasks until the function |
| 3337 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 3338 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3339 | * |
| 3340 | * Note that this function acquires a lock which is released when the |
| 3341 | * iteration finishes. The caller can't sleep while iteration is in |
| 3342 | * progress. |
| 3343 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3344 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 3345 | struct css_task_iter *it) |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3346 | __acquires(css_set_rwsem) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3347 | { |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 3348 | /* no one should try to iterate before mounting cgroups */ |
| 3349 | WARN_ON_ONCE(!use_task_css_set_links); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3350 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3351 | down_read(&css_set_rwsem); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3352 | |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3353 | it->ss = css->ss; |
| 3354 | |
| 3355 | if (it->ss) |
| 3356 | it->cset_pos = &css->cgroup->e_csets[css->ss->id]; |
| 3357 | else |
| 3358 | it->cset_pos = &css->cgroup->cset_links; |
| 3359 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3360 | it->cset_head = it->cset_pos; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3361 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3362 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3363 | } |
| 3364 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3365 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3366 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3367 | * @it: the task iterator being iterated |
| 3368 | * |
| 3369 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3370 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 3371 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3372 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3373 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3374 | { |
| 3375 | struct task_struct *res; |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3376 | struct list_head *l = it->task_pos; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3377 | |
| 3378 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3379 | if (!it->cset_pos) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3380 | return NULL; |
| 3381 | res = list_entry(l, struct task_struct, cg_list); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3382 | |
| 3383 | /* |
| 3384 | * Advance iterator to find next entry. cset->tasks is consumed |
| 3385 | * first and then ->mg_tasks. After ->mg_tasks, we move onto the |
| 3386 | * next cset. |
| 3387 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3388 | l = l->next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3389 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3390 | if (l == it->tasks_head) |
| 3391 | l = it->mg_tasks_head->next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3392 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3393 | if (l == it->mg_tasks_head) |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3394 | css_advance_task_iter(it); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3395 | else |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3396 | it->task_pos = l; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3397 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3398 | return res; |
| 3399 | } |
| 3400 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3401 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3402 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3403 | * @it: the task iterator to finish |
| 3404 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3405 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3406 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3407 | void css_task_iter_end(struct css_task_iter *it) |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3408 | __releases(css_set_rwsem) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3409 | { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3410 | up_read(&css_set_rwsem); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | /** |
| 3414 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3415 | * @to: cgroup to which the tasks will be moved |
| 3416 | * @from: cgroup in which the tasks currently reside |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3417 | * |
| 3418 | * Locking rules between cgroup_post_fork() and the migration path |
| 3419 | * guarantee that, if a task is forking while being migrated, the new child |
| 3420 | * is guaranteed to be either visible in the source cgroup after the |
| 3421 | * parent's migration is complete or put into the target cgroup. No task |
| 3422 | * can slip out of migration through forking. |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3423 | */ |
| 3424 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3425 | { |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3426 | LIST_HEAD(preloaded_csets); |
| 3427 | struct cgrp_cset_link *link; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3428 | struct css_task_iter it; |
| 3429 | struct task_struct *task; |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3430 | int ret; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3431 | |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3432 | mutex_lock(&cgroup_mutex); |
| 3433 | |
| 3434 | /* all tasks in @from are being moved, all csets are source */ |
| 3435 | down_read(&css_set_rwsem); |
| 3436 | list_for_each_entry(link, &from->cset_links, cset_link) |
| 3437 | cgroup_migrate_add_src(link->cset, to, &preloaded_csets); |
| 3438 | up_read(&css_set_rwsem); |
| 3439 | |
| 3440 | ret = cgroup_migrate_prepare_dst(to, &preloaded_csets); |
| 3441 | if (ret) |
| 3442 | goto out_err; |
| 3443 | |
| 3444 | /* |
| 3445 | * Migrate tasks one-by-one until @form is empty. This fails iff |
| 3446 | * ->can_attach() fails. |
| 3447 | */ |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3448 | do { |
| 3449 | css_task_iter_start(&from->dummy_css, &it); |
| 3450 | task = css_task_iter_next(&it); |
| 3451 | if (task) |
| 3452 | get_task_struct(task); |
| 3453 | css_task_iter_end(&it); |
| 3454 | |
| 3455 | if (task) { |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3456 | ret = cgroup_migrate(to, task, false); |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3457 | put_task_struct(task); |
| 3458 | } |
| 3459 | } while (task && !ret); |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3460 | out_err: |
| 3461 | cgroup_migrate_finish(&preloaded_csets); |
| 3462 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3463 | return ret; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3464 | } |
| 3465 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3466 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3467 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3468 | * |
| 3469 | * Reading this file can return large amounts of data if a cgroup has |
| 3470 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3471 | * but we cannot guarantee that the information we produce is correct |
| 3472 | * unless we produce it entirely atomically. |
| 3473 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3474 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3475 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3476 | /* which pidlist file are we talking about? */ |
| 3477 | enum cgroup_filetype { |
| 3478 | CGROUP_FILE_PROCS, |
| 3479 | CGROUP_FILE_TASKS, |
| 3480 | }; |
| 3481 | |
| 3482 | /* |
| 3483 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3484 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3485 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3486 | * to the cgroup. |
| 3487 | */ |
| 3488 | struct cgroup_pidlist { |
| 3489 | /* |
| 3490 | * used to find which pidlist is wanted. doesn't change as long as |
| 3491 | * this particular list stays in the list. |
| 3492 | */ |
| 3493 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3494 | /* array of xids */ |
| 3495 | pid_t *list; |
| 3496 | /* how many elements the above list has */ |
| 3497 | int length; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3498 | /* each of these stored in a list by its cgroup */ |
| 3499 | struct list_head links; |
| 3500 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3501 | struct cgroup *owner; |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3502 | /* for delayed destruction */ |
| 3503 | struct delayed_work destroy_dwork; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3504 | }; |
| 3505 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3506 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3507 | * The following two functions "fix" the issue where there are more pids |
| 3508 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3509 | * TODO: replace with a kernel-wide solution to this problem |
| 3510 | */ |
| 3511 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3512 | static void *pidlist_allocate(int count) |
| 3513 | { |
| 3514 | if (PIDLIST_TOO_LARGE(count)) |
| 3515 | return vmalloc(count * sizeof(pid_t)); |
| 3516 | else |
| 3517 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3518 | } |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3519 | |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3520 | static void pidlist_free(void *p) |
| 3521 | { |
| 3522 | if (is_vmalloc_addr(p)) |
| 3523 | vfree(p); |
| 3524 | else |
| 3525 | kfree(p); |
| 3526 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3527 | |
| 3528 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3529 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 3530 | * should be left afterwards. |
| 3531 | */ |
| 3532 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 3533 | { |
| 3534 | struct cgroup_pidlist *l, *tmp_l; |
| 3535 | |
| 3536 | mutex_lock(&cgrp->pidlist_mutex); |
| 3537 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 3538 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 3539 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3540 | |
| 3541 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 3542 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 3543 | } |
| 3544 | |
| 3545 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 3546 | { |
| 3547 | struct delayed_work *dwork = to_delayed_work(work); |
| 3548 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 3549 | destroy_dwork); |
| 3550 | struct cgroup_pidlist *tofree = NULL; |
| 3551 | |
| 3552 | mutex_lock(&l->owner->pidlist_mutex); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3553 | |
| 3554 | /* |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3555 | * Destroy iff we didn't get queued again. The state won't change |
| 3556 | * as destroy_dwork can only be queued while locked. |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3557 | */ |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3558 | if (!delayed_work_pending(dwork)) { |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3559 | list_del(&l->links); |
| 3560 | pidlist_free(l->list); |
| 3561 | put_pid_ns(l->key.ns); |
| 3562 | tofree = l; |
| 3563 | } |
| 3564 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3565 | mutex_unlock(&l->owner->pidlist_mutex); |
| 3566 | kfree(tofree); |
| 3567 | } |
| 3568 | |
| 3569 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3570 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3571 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3572 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3573 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3574 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3575 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3576 | |
| 3577 | /* |
| 3578 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3579 | * edge cases first; no work needs to be done for either |
| 3580 | */ |
| 3581 | if (length == 0 || length == 1) |
| 3582 | return length; |
| 3583 | /* src and dest walk down the list; dest counts unique elements */ |
| 3584 | for (src = 1; src < length; src++) { |
| 3585 | /* find next unique element */ |
| 3586 | while (list[src] == list[src-1]) { |
| 3587 | src++; |
| 3588 | if (src == length) |
| 3589 | goto after; |
| 3590 | } |
| 3591 | /* dest always points to where the next unique element goes */ |
| 3592 | list[dest] = list[src]; |
| 3593 | dest++; |
| 3594 | } |
| 3595 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3596 | return dest; |
| 3597 | } |
| 3598 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3599 | /* |
| 3600 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 3601 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 3602 | * different per namespace, each namespace needs differently sorted list, |
| 3603 | * making it impossible to use, for example, single rbtree of member tasks |
| 3604 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 3605 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 3606 | * pidlists keyed by cgroup and namespace. |
| 3607 | * |
| 3608 | * All this extra complexity was caused by the original implementation |
| 3609 | * committing to an entirely unnecessary property. In the long term, we |
| 3610 | * want to do away with it. Explicitly scramble sort order if |
| 3611 | * sane_behavior so that no such expectation exists in the new interface. |
| 3612 | * |
| 3613 | * Scrambling is done by swapping every two consecutive bits, which is |
| 3614 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 3615 | */ |
| 3616 | static pid_t pid_fry(pid_t pid) |
| 3617 | { |
| 3618 | unsigned a = pid & 0x55555555; |
| 3619 | unsigned b = pid & 0xAAAAAAAA; |
| 3620 | |
| 3621 | return (a << 1) | (b >> 1); |
| 3622 | } |
| 3623 | |
| 3624 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 3625 | { |
| 3626 | if (cgroup_sane_behavior(cgrp)) |
| 3627 | return pid_fry(pid); |
| 3628 | else |
| 3629 | return pid; |
| 3630 | } |
| 3631 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3632 | static int cmppid(const void *a, const void *b) |
| 3633 | { |
| 3634 | return *(pid_t *)a - *(pid_t *)b; |
| 3635 | } |
| 3636 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3637 | static int fried_cmppid(const void *a, const void *b) |
| 3638 | { |
| 3639 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 3640 | } |
| 3641 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3642 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3643 | enum cgroup_filetype type) |
| 3644 | { |
| 3645 | struct cgroup_pidlist *l; |
| 3646 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3647 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3648 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3649 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3650 | |
| 3651 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 3652 | if (l->key.type == type && l->key.ns == ns) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3653 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3654 | return NULL; |
| 3655 | } |
| 3656 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3657 | /* |
| 3658 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3659 | * returns with the lock on that pidlist already held, and takes care |
| 3660 | * of the use count, or returns NULL with no locks held if we're out of |
| 3661 | * memory. |
| 3662 | */ |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3663 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 3664 | enum cgroup_filetype type) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3665 | { |
| 3666 | struct cgroup_pidlist *l; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3667 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3668 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3669 | |
| 3670 | l = cgroup_pidlist_find(cgrp, type); |
| 3671 | if (l) |
| 3672 | return l; |
| 3673 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3674 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3675 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3676 | if (!l) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3677 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3678 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3679 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3680 | l->key.type = type; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3681 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 3682 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3683 | l->owner = cgrp; |
| 3684 | list_add(&l->links, &cgrp->pidlists); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3685 | return l; |
| 3686 | } |
| 3687 | |
| 3688 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3689 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3690 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3691 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3692 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3693 | { |
| 3694 | pid_t *array; |
| 3695 | int length; |
| 3696 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3697 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3698 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3699 | struct cgroup_pidlist *l; |
| 3700 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3701 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3702 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3703 | /* |
| 3704 | * If cgroup gets more users after we read count, we won't have |
| 3705 | * enough space - tough. This race is indistinguishable to the |
| 3706 | * caller from the case that the additional cgroup users didn't |
| 3707 | * show up until sometime later on. |
| 3708 | */ |
| 3709 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3710 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3711 | if (!array) |
| 3712 | return -ENOMEM; |
| 3713 | /* now, populate the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3714 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3715 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3716 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3717 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3718 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3719 | if (type == CGROUP_FILE_PROCS) |
| 3720 | pid = task_tgid_vnr(tsk); |
| 3721 | else |
| 3722 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3723 | if (pid > 0) /* make sure to only use valid results */ |
| 3724 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3725 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3726 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3727 | length = n; |
| 3728 | /* now sort & (if procs) strip out duplicates */ |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3729 | if (cgroup_sane_behavior(cgrp)) |
| 3730 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 3731 | else |
| 3732 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3733 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3734 | length = pidlist_uniq(array, length); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3735 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3736 | l = cgroup_pidlist_find_create(cgrp, type); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3737 | if (!l) { |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3738 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3739 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3740 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3741 | } |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3742 | |
| 3743 | /* store array, freeing old if necessary */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3744 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3745 | l->list = array; |
| 3746 | l->length = length; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3747 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3748 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3749 | } |
| 3750 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3751 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3752 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3753 | * @stats: cgroupstats to fill information into |
| 3754 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3755 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3756 | * |
| 3757 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3758 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3759 | */ |
| 3760 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3761 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3762 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3763 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3764 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3765 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3766 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3767 | /* it should be kernfs_node belonging to cgroupfs and is a directory */ |
| 3768 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 3769 | kernfs_type(kn) != KERNFS_DIR) |
| 3770 | return -EINVAL; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3771 | |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3772 | mutex_lock(&cgroup_mutex); |
| 3773 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3774 | /* |
| 3775 | * We aren't being called from kernfs and there's no guarantee on |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 3776 | * @kn->priv's validity. For this and css_tryget_online_from_dir(), |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3777 | * @kn->priv is RCU safe. Let's do the RCU dancing. |
| 3778 | */ |
| 3779 | rcu_read_lock(); |
| 3780 | cgrp = rcu_dereference(kn->priv); |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3781 | if (!cgrp || cgroup_is_dead(cgrp)) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3782 | rcu_read_unlock(); |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3783 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3784 | return -ENOENT; |
| 3785 | } |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3786 | rcu_read_unlock(); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3787 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3788 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3789 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3790 | switch (tsk->state) { |
| 3791 | case TASK_RUNNING: |
| 3792 | stats->nr_running++; |
| 3793 | break; |
| 3794 | case TASK_INTERRUPTIBLE: |
| 3795 | stats->nr_sleeping++; |
| 3796 | break; |
| 3797 | case TASK_UNINTERRUPTIBLE: |
| 3798 | stats->nr_uninterruptible++; |
| 3799 | break; |
| 3800 | case TASK_STOPPED: |
| 3801 | stats->nr_stopped++; |
| 3802 | break; |
| 3803 | default: |
| 3804 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3805 | stats->nr_io_wait++; |
| 3806 | break; |
| 3807 | } |
| 3808 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3809 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3810 | |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3811 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3812 | return 0; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3813 | } |
| 3814 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3815 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3816 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3817 | * 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] | 3818 | * 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] | 3819 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3820 | */ |
| 3821 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3822 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3823 | { |
| 3824 | /* |
| 3825 | * Initially we receive a position value that corresponds to |
| 3826 | * one more than the last pid shown (or 0 on the first call or |
| 3827 | * after a seek to the start). Use a binary-search to find the |
| 3828 | * next pid to display, if any |
| 3829 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3830 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3831 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3832 | struct cgroup_pidlist *l; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3833 | enum cgroup_filetype type = seq_cft(s)->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3834 | int index = 0, pid = *pos; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3835 | int *iter, ret; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3836 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3837 | mutex_lock(&cgrp->pidlist_mutex); |
| 3838 | |
| 3839 | /* |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3840 | * !NULL @of->priv indicates that this isn't the first start() |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3841 | * after open. If the matching pidlist is around, we can use that. |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3842 | * 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] | 3843 | * could already have been destroyed. |
| 3844 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3845 | if (of->priv) |
| 3846 | of->priv = cgroup_pidlist_find(cgrp, type); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3847 | |
| 3848 | /* |
| 3849 | * Either this is the first start() after open or the matching |
| 3850 | * pidlist has been destroyed inbetween. Create a new one. |
| 3851 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3852 | if (!of->priv) { |
| 3853 | ret = pidlist_array_load(cgrp, type, |
| 3854 | (struct cgroup_pidlist **)&of->priv); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3855 | if (ret) |
| 3856 | return ERR_PTR(ret); |
| 3857 | } |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3858 | l = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3859 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3860 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3861 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3862 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3863 | while (index < end) { |
| 3864 | int mid = (index + end) / 2; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3865 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3866 | index = mid; |
| 3867 | break; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3868 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3869 | index = mid + 1; |
| 3870 | else |
| 3871 | end = mid; |
| 3872 | } |
| 3873 | } |
| 3874 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3875 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3876 | return NULL; |
| 3877 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3878 | iter = l->list + index; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3879 | *pos = cgroup_pid_fry(cgrp, *iter); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3880 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3881 | } |
| 3882 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3883 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3884 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3885 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3886 | struct cgroup_pidlist *l = of->priv; |
Tejun Heo | 6223685 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3887 | |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3888 | if (l) |
| 3889 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3890 | CGROUP_PIDLIST_DESTROY_DELAY); |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3891 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3892 | } |
| 3893 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3894 | 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] | 3895 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3896 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3897 | struct cgroup_pidlist *l = of->priv; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3898 | pid_t *p = v; |
| 3899 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3900 | /* |
| 3901 | * Advance to the next pid in the array. If this goes off the |
| 3902 | * end, we're done |
| 3903 | */ |
| 3904 | p++; |
| 3905 | if (p >= end) { |
| 3906 | return NULL; |
| 3907 | } else { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3908 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3909 | return p; |
| 3910 | } |
| 3911 | } |
| 3912 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3913 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3914 | { |
| 3915 | return seq_printf(s, "%d\n", *(int *)v); |
| 3916 | } |
| 3917 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3918 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3919 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3920 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3921 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3922 | } |
| 3923 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3924 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3925 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3926 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3927 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3928 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3929 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3930 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3931 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3932 | return 0; |
| 3933 | } |
| 3934 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3935 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 3936 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3937 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3938 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3939 | } |
| 3940 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3941 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 3942 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3943 | { |
| 3944 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3945 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3946 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3947 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3948 | return 0; |
| 3949 | } |
| 3950 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3951 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3952 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3953 | .name = "cgroup.procs", |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3954 | .seq_start = cgroup_pidlist_start, |
| 3955 | .seq_next = cgroup_pidlist_next, |
| 3956 | .seq_stop = cgroup_pidlist_stop, |
| 3957 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3958 | .private = CGROUP_FILE_PROCS, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3959 | .write_u64 = cgroup_procs_write, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3960 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3961 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3962 | { |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3963 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3964 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3965 | .read_u64 = cgroup_clone_children_read, |
| 3966 | .write_u64 = cgroup_clone_children_write, |
| 3967 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3968 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3969 | .name = "cgroup.sane_behavior", |
| 3970 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3971 | .seq_show = cgroup_sane_behavior_show, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3972 | }, |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3973 | { |
| 3974 | .name = "cgroup.controllers", |
| 3975 | .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT, |
| 3976 | .seq_show = cgroup_root_controllers_show, |
| 3977 | }, |
| 3978 | { |
| 3979 | .name = "cgroup.controllers", |
| 3980 | .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT, |
| 3981 | .seq_show = cgroup_controllers_show, |
| 3982 | }, |
| 3983 | { |
| 3984 | .name = "cgroup.subtree_control", |
| 3985 | .flags = CFTYPE_ONLY_ON_DFL, |
| 3986 | .seq_show = cgroup_subtree_control_show, |
| 3987 | .write_string = cgroup_subtree_control_write, |
| 3988 | }, |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3989 | { |
| 3990 | .name = "cgroup.populated", |
| 3991 | .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT, |
| 3992 | .seq_show = cgroup_populated_show, |
| 3993 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3994 | |
| 3995 | /* |
| 3996 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 3997 | * don't exist if sane_behavior. If you're depending on these, be |
| 3998 | * prepared to be burned. |
| 3999 | */ |
| 4000 | { |
| 4001 | .name = "tasks", |
| 4002 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4003 | .seq_start = cgroup_pidlist_start, |
| 4004 | .seq_next = cgroup_pidlist_next, |
| 4005 | .seq_stop = cgroup_pidlist_stop, |
| 4006 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4007 | .private = CGROUP_FILE_TASKS, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4008 | .write_u64 = cgroup_tasks_write, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4009 | .mode = S_IRUGO | S_IWUSR, |
| 4010 | }, |
| 4011 | { |
| 4012 | .name = "notify_on_release", |
| 4013 | .flags = CFTYPE_INSANE, |
| 4014 | .read_u64 = cgroup_read_notify_on_release, |
| 4015 | .write_u64 = cgroup_write_notify_on_release, |
| 4016 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4017 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4018 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4019 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4020 | .seq_show = cgroup_release_agent_show, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4021 | .write_string = cgroup_release_agent_write, |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4022 | .max_write_len = PATH_MAX - 1, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4023 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4024 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4025 | }; |
| 4026 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4027 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4028 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4029 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4030 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4031 | * |
| 4032 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4033 | */ |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4034 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4035 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4036 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4037 | int i, ret = 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4038 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4039 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4040 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4041 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4042 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4043 | if (!(subsys_mask & (1 << i))) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4044 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4045 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4046 | list_for_each_entry(cfts, &ss->cfts, node) { |
| 4047 | ret = cgroup_addrm_files(cgrp, cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4048 | if (ret < 0) |
| 4049 | goto err; |
| 4050 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4051 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4052 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4053 | err: |
| 4054 | cgroup_clear_dir(cgrp, subsys_mask); |
| 4055 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4056 | } |
| 4057 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4058 | /* |
| 4059 | * css destruction is four-stage process. |
| 4060 | * |
| 4061 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 4062 | * Implemented in kill_css(). |
| 4063 | * |
| 4064 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4065 | * and thus css_tryget_online() is guaranteed to fail, the css can be |
| 4066 | * offlined by invoking offline_css(). After offlining, the base ref is |
| 4067 | * put. Implemented in css_killed_work_fn(). |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4068 | * |
| 4069 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 4070 | * accessors are inside RCU read sections. css_release() schedules the |
| 4071 | * RCU callback. |
| 4072 | * |
| 4073 | * 4. After the grace period, the css can be freed. Implemented in |
| 4074 | * css_free_work_fn(). |
| 4075 | * |
| 4076 | * It is actually hairier because both step 2 and 4 require process context |
| 4077 | * and thus involve punting to css->destroy_work adding two additional |
| 4078 | * steps to the already complex sequence. |
| 4079 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4080 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4081 | { |
| 4082 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4083 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4084 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4085 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4086 | if (css->parent) |
| 4087 | css_put(css->parent); |
| 4088 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4089 | css->ss->css_free(css); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4090 | cgroup_put(cgrp); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 4094 | { |
| 4095 | struct cgroup_subsys_state *css = |
| 4096 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 4097 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4098 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4099 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4100 | } |
| 4101 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4102 | static void css_release(struct percpu_ref *ref) |
| 4103 | { |
| 4104 | struct cgroup_subsys_state *css = |
| 4105 | container_of(ref, struct cgroup_subsys_state, refcnt); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4106 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4107 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4108 | cgroup_idr_remove(&ss->css_idr, css->id); |
| 4109 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4110 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4111 | } |
| 4112 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4113 | static void init_and_link_css(struct cgroup_subsys_state *css, |
| 4114 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4115 | { |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4116 | cgroup_get(cgrp); |
| 4117 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4118 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4119 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4120 | css->flags = 0; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4121 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4122 | if (cgrp->parent) { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4123 | css->parent = cgroup_css(cgrp->parent, ss); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4124 | css_get(css->parent); |
| 4125 | } else { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4126 | css->flags |= CSS_ROOT; |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4127 | } |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4128 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4129 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4130 | } |
| 4131 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4132 | /* 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] | 4133 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4134 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4135 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4136 | int ret = 0; |
| 4137 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4138 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4139 | lockdep_assert_held(&cgroup_mutex); |
| 4140 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4141 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4142 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4143 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4144 | css->flags |= CSS_ONLINE; |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4145 | css->cgroup->nr_css++; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4146 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4147 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4148 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4149 | } |
| 4150 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4151 | /* 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] | 4152 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4153 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4154 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4155 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4156 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4157 | lockdep_assert_held(&cgroup_mutex); |
| 4158 | |
| 4159 | if (!(css->flags & CSS_ONLINE)) |
| 4160 | return; |
| 4161 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4162 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4163 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4164 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4165 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4166 | css->cgroup->nr_css--; |
Tejun Heo | e329780 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4167 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4168 | |
| 4169 | wake_up_all(&css->cgroup->offline_waitq); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4170 | } |
| 4171 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4172 | /** |
| 4173 | * create_css - create a cgroup_subsys_state |
| 4174 | * @cgrp: the cgroup new css will be associated with |
| 4175 | * @ss: the subsys of new css |
| 4176 | * |
| 4177 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
| 4178 | * css is online and installed in @cgrp with all interface files created. |
| 4179 | * Returns 0 on success, -errno on failure. |
| 4180 | */ |
| 4181 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss) |
| 4182 | { |
| 4183 | struct cgroup *parent = cgrp->parent; |
| 4184 | struct cgroup_subsys_state *css; |
| 4185 | int err; |
| 4186 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4187 | lockdep_assert_held(&cgroup_mutex); |
| 4188 | |
| 4189 | css = ss->css_alloc(cgroup_css(parent, ss)); |
| 4190 | if (IS_ERR(css)) |
| 4191 | return PTR_ERR(css); |
| 4192 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4193 | init_and_link_css(css, ss, cgrp); |
Tejun Heo | a2bed82 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4194 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4195 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4196 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4197 | goto err_free_css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4198 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4199 | err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT); |
| 4200 | if (err < 0) |
| 4201 | goto err_free_percpu_ref; |
| 4202 | css->id = err; |
| 4203 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4204 | err = cgroup_populate_dir(cgrp, 1 << ss->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4205 | if (err) |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4206 | goto err_free_id; |
| 4207 | |
| 4208 | /* @css is ready to be brought online now, make it visible */ |
| 4209 | cgroup_idr_replace(&ss->css_idr, css, css->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4210 | |
| 4211 | err = online_css(css); |
| 4212 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4213 | goto err_clear_dir; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4214 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4215 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4216 | parent->parent) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4217 | 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] | 4218 | current->comm, current->pid, ss->name); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4219 | if (!strcmp(ss->name, "memory")) |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4220 | 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] | 4221 | ss->warned_broken_hierarchy = true; |
| 4222 | } |
| 4223 | |
| 4224 | return 0; |
| 4225 | |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4226 | err_clear_dir: |
Linus Torvalds | 32d01dc | 2014-04-03 13:05:42 -0700 | [diff] [blame] | 4227 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4228 | err_free_id: |
| 4229 | cgroup_idr_remove(&ss->css_idr, css->id); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4230 | err_free_percpu_ref: |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4231 | percpu_ref_cancel_init(&css->refcnt); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4232 | err_free_css: |
Tejun Heo | a2bed82 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4233 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4234 | return err; |
| 4235 | } |
| 4236 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4237 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4238 | * cgroup_create - create a cgroup |
| 4239 | * @parent: cgroup that will be parent of the new cgroup |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4240 | * @name: name of the new cgroup |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4241 | * @mode: mode to set on new cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4242 | */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4243 | static long cgroup_create(struct cgroup *parent, const char *name, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4244 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4245 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4246 | struct cgroup *cgrp; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4247 | struct cgroup_root *root = parent->root; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 4248 | int ssid, err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4249 | struct cgroup_subsys *ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4250 | struct kernfs_node *kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4251 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4252 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4253 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4254 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4255 | return -ENOMEM; |
| 4256 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4257 | mutex_lock(&cgroup_tree_mutex); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4258 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4259 | /* |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4260 | * Only live parents can have children. Note that the liveliness |
| 4261 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4262 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4263 | * anyway so that locking is contained inside cgroup proper and we |
| 4264 | * don't get nasty surprises if we ever grow another caller. |
| 4265 | */ |
| 4266 | if (!cgroup_lock_live_group(parent)) { |
| 4267 | err = -ENODEV; |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4268 | goto err_unlock_tree; |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4269 | } |
| 4270 | |
| 4271 | /* |
| 4272 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4273 | * a half-baked cgroup. |
| 4274 | */ |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4275 | 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] | 4276 | if (cgrp->id < 0) { |
| 4277 | err = -ENOMEM; |
| 4278 | goto err_unlock; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4279 | } |
| 4280 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4281 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4282 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4283 | cgrp->parent = parent; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4284 | cgrp->dummy_css.parent = &parent->dummy_css; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4285 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4286 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4287 | if (notify_on_release(parent)) |
| 4288 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4289 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4290 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4291 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4292 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4293 | /* create the directory */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4294 | kn = kernfs_create_dir(parent->kn, name, mode, cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4295 | if (IS_ERR(kn)) { |
| 4296 | err = PTR_ERR(kn); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4297 | goto err_free_id; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4298 | } |
| 4299 | cgrp->kn = kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4300 | |
Tejun Heo | 6f30558 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4301 | /* |
| 4302 | * This extra ref will be put in cgroup_free_fn() and guarantees |
| 4303 | * that @cgrp->kn is always accessible. |
| 4304 | */ |
| 4305 | kernfs_get(kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4306 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4307 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4308 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4309 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4310 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4311 | atomic_inc(&root->nr_cgrps); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4312 | cgroup_get(parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4313 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4314 | /* |
| 4315 | * @cgrp is now fully operational. If something fails after this |
| 4316 | * point, it'll be released via the normal destruction path. |
| 4317 | */ |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4318 | cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4319 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 4320 | err = cgroup_kn_set_ugid(kn); |
| 4321 | if (err) |
| 4322 | goto err_destroy; |
| 4323 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4324 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4325 | if (err) |
| 4326 | goto err_destroy; |
| 4327 | |
Tejun Heo | 9d403e9 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4328 | /* let's create and online css's */ |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4329 | for_each_subsys(ss, ssid) { |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4330 | if (parent->child_subsys_mask & (1 << ssid)) { |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4331 | err = create_css(cgrp, ss); |
| 4332 | if (err) |
| 4333 | goto err_destroy; |
| 4334 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4335 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4336 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4337 | /* |
| 4338 | * On the default hierarchy, a child doesn't automatically inherit |
| 4339 | * child_subsys_mask from the parent. Each is configured manually. |
| 4340 | */ |
| 4341 | if (!cgroup_on_dfl(cgrp)) |
| 4342 | cgrp->child_subsys_mask = parent->child_subsys_mask; |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4343 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4344 | kernfs_activate(kn); |
| 4345 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4346 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4347 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4348 | |
| 4349 | return 0; |
| 4350 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4351 | err_free_id: |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4352 | cgroup_idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4353 | err_unlock: |
| 4354 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4355 | err_unlock_tree: |
| 4356 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4357 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4358 | return err; |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4359 | |
| 4360 | err_destroy: |
| 4361 | cgroup_destroy_locked(cgrp); |
| 4362 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4363 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4364 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4365 | } |
| 4366 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4367 | static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, |
| 4368 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4369 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4370 | struct cgroup *parent = parent_kn->priv; |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 4371 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4372 | |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 4373 | /* |
| 4374 | * cgroup_create() grabs cgroup_tree_mutex which nests outside |
| 4375 | * kernfs active_ref and cgroup_create() already synchronizes |
| 4376 | * properly against removal through cgroup_lock_live_group(). |
| 4377 | * Break it before calling cgroup_create(). |
| 4378 | */ |
| 4379 | cgroup_get(parent); |
| 4380 | kernfs_break_active_protection(parent_kn); |
| 4381 | |
| 4382 | ret = cgroup_create(parent, name, mode); |
| 4383 | |
| 4384 | kernfs_unbreak_active_protection(parent_kn); |
| 4385 | cgroup_put(parent); |
| 4386 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4387 | } |
| 4388 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4389 | /* |
| 4390 | * This is called when the refcnt of a css is confirmed to be killed. |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4391 | * css_tryget_online() is now guaranteed to fail. |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4392 | */ |
| 4393 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4394 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4395 | struct cgroup_subsys_state *css = |
| 4396 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 4397 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4398 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4399 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4400 | mutex_lock(&cgroup_mutex); |
| 4401 | |
| 4402 | /* |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4403 | * css_tryget_online() is guaranteed to fail now. Tell subsystems |
| 4404 | * to initate destruction. |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4405 | */ |
| 4406 | offline_css(css); |
| 4407 | |
| 4408 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4409 | * If @cgrp is marked dead, it's waiting for refs of all css's to |
| 4410 | * be disabled before proceeding to the second phase of cgroup |
| 4411 | * destruction. If we are the last one, kick it off. |
| 4412 | */ |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4413 | if (!cgrp->nr_css && cgroup_is_dead(cgrp)) |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4414 | cgroup_destroy_css_killed(cgrp); |
| 4415 | |
| 4416 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4417 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4418 | |
| 4419 | /* |
| 4420 | * Put the css refs from kill_css(). Each css holds an extra |
| 4421 | * reference to the cgroup's dentry and cgroup removal proceeds |
| 4422 | * regardless of css refs. On the last put of each css, whenever |
| 4423 | * that may be, the extra dentry ref is put so that dentry |
| 4424 | * destruction happens only after all css's are released. |
| 4425 | */ |
| 4426 | css_put(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4427 | } |
| 4428 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4429 | /* css kill confirmation processing requires process context, bounce */ |
| 4430 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4431 | { |
| 4432 | struct cgroup_subsys_state *css = |
| 4433 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4434 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4435 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4436 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4437 | } |
| 4438 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4439 | /** |
| 4440 | * kill_css - destroy a css |
| 4441 | * @css: css to destroy |
| 4442 | * |
| 4443 | * This function initiates destruction of @css by removing cgroup interface |
| 4444 | * files and putting its base reference. ->css_offline() will be invoked |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4445 | * asynchronously once css_tryget_online() is guaranteed to fail and when |
| 4446 | * the reference count reaches zero, @css will be released. |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4447 | */ |
| 4448 | static void kill_css(struct cgroup_subsys_state *css) |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4449 | { |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4450 | lockdep_assert_held(&cgroup_tree_mutex); |
| 4451 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4452 | /* |
| 4453 | * This must happen before css is disassociated with its cgroup. |
| 4454 | * See seq_css() for details. |
| 4455 | */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4456 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4457 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4458 | /* |
| 4459 | * Killing would put the base ref, but we need to keep it alive |
| 4460 | * until after ->css_offline(). |
| 4461 | */ |
| 4462 | css_get(css); |
| 4463 | |
| 4464 | /* |
| 4465 | * cgroup core guarantees that, by the time ->css_offline() is |
| 4466 | * invoked, no new css reference will be given out via |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4467 | * css_tryget_online(). We can't simply call percpu_ref_kill() and |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4468 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 4469 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 4470 | * |
| 4471 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4472 | * css is confirmed to be seen as killed on all CPUs. |
| 4473 | */ |
| 4474 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4475 | } |
| 4476 | |
| 4477 | /** |
| 4478 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4479 | * @cgrp: cgroup to be destroyed |
| 4480 | * |
| 4481 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4482 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4483 | * guarantee that css_tryget_online() won't succeed by the time |
| 4484 | * ->css_offline() is invoked. To satisfy all the requirements, |
| 4485 | * destruction is implemented in the following two steps. |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4486 | * |
| 4487 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4488 | * userland visible parts and start killing the percpu refcnts of |
| 4489 | * css's. Set up so that the next stage will be kicked off once all |
| 4490 | * the percpu refcnts are confirmed to be killed. |
| 4491 | * |
| 4492 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4493 | * rest of destruction. Once all cgroup references are gone, the |
| 4494 | * cgroup is RCU-freed. |
| 4495 | * |
| 4496 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4497 | * the userland is concerned and a new cgroup with the same name may be |
| 4498 | * created. As cgroup doesn't care about the names internally, this |
| 4499 | * doesn't cause any problem. |
| 4500 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4501 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4502 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4503 | { |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4504 | struct cgroup *child; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4505 | struct cgroup_subsys_state *css; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4506 | bool empty; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4507 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4508 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4509 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4510 | lockdep_assert_held(&cgroup_mutex); |
| 4511 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4512 | /* |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4513 | * css_set_rwsem synchronizes access to ->cset_links and prevents |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4514 | * @cgrp from being removed while put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4515 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4516 | down_read(&css_set_rwsem); |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4517 | empty = list_empty(&cgrp->cset_links); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4518 | up_read(&css_set_rwsem); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4519 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4520 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4521 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4522 | /* |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4523 | * Make sure there's no live children. We can't test ->children |
| 4524 | * emptiness as dead children linger on it while being destroyed; |
| 4525 | * otherwise, "rmdir parent/child parent" may fail with -EBUSY. |
| 4526 | */ |
| 4527 | empty = true; |
| 4528 | rcu_read_lock(); |
| 4529 | list_for_each_entry_rcu(child, &cgrp->children, sibling) { |
| 4530 | empty = cgroup_is_dead(child); |
| 4531 | if (!empty) |
| 4532 | break; |
| 4533 | } |
| 4534 | rcu_read_unlock(); |
| 4535 | if (!empty) |
| 4536 | return -EBUSY; |
| 4537 | |
| 4538 | /* |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4539 | * Mark @cgrp dead. This prevents further task migration and child |
| 4540 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4541 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4542 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4543 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4544 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4545 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4546 | |
Tejun Heo | 5d77381 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4547 | /* |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4548 | * Initiate massacre of all css's. cgroup_destroy_css_killed() |
| 4549 | * will be invoked to perform the rest of destruction once the |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4550 | * percpu refs of all css's are confirmed to be killed. This |
| 4551 | * involves removing the subsystem's files, drop cgroup_mutex. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4552 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4553 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4554 | for_each_css(css, ssid, cgrp) |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4555 | kill_css(css); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4556 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4557 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4558 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4559 | raw_spin_lock(&release_list_lock); |
| 4560 | if (!list_empty(&cgrp->release_list)) |
| 4561 | list_del_init(&cgrp->release_list); |
| 4562 | raw_spin_unlock(&release_list_lock); |
| 4563 | |
| 4564 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4565 | * If @cgrp has css's attached, the second stage of cgroup |
| 4566 | * destruction is kicked off from css_killed_work_fn() after the |
| 4567 | * refs of all attached css's are killed. If @cgrp doesn't have |
| 4568 | * any css, we kick it off here. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4569 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4570 | if (!cgrp->nr_css) |
| 4571 | cgroup_destroy_css_killed(cgrp); |
| 4572 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4573 | /* remove @cgrp directory along with the base files */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4574 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4575 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4576 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4577 | * There are two control paths which try to determine cgroup from |
| 4578 | * dentry without going through kernfs - cgroupstats_build() and |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4579 | * css_tryget_online_from_dir(). Those are supported by RCU |
| 4580 | * protecting clearing of cgrp->kn->priv backpointer, which should |
| 4581 | * happen after all files under it have been removed. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4582 | */ |
Tejun Heo | 6f30558 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4583 | kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4584 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4585 | |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4586 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4587 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4588 | return 0; |
| 4589 | }; |
| 4590 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4591 | /** |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4592 | * cgroup_destroy_css_killed - the second step of cgroup destruction |
Fabian Frederick | 6010694 | 2014-05-05 20:08:13 +0200 | [diff] [blame] | 4593 | * @cgrp: the cgroup whose csses have just finished offlining |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4594 | * |
| 4595 | * 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] | 4596 | * destroyed after all css's are offlined and performs the rest of |
| 4597 | * destruction. This is the second step of destruction described in the |
| 4598 | * comment above cgroup_destroy_locked(). |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4599 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4600 | static void cgroup_destroy_css_killed(struct cgroup *cgrp) |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4601 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4602 | struct cgroup *parent = cgrp->parent; |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4603 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4604 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4605 | lockdep_assert_held(&cgroup_mutex); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4606 | |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4607 | /* delete this cgroup from parent->children */ |
| 4608 | list_del_rcu(&cgrp->sibling); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4609 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4610 | cgroup_put(cgrp); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4611 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4612 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4613 | check_for_release(parent); |
| 4614 | } |
| 4615 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4616 | static int cgroup_rmdir(struct kernfs_node *kn) |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4617 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4618 | struct cgroup *cgrp = kn->priv; |
| 4619 | int ret = 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4620 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4621 | /* |
| 4622 | * This is self-destruction but @kn can't be removed while this |
| 4623 | * callback is in progress. Let's break active protection. Once |
| 4624 | * the protection is broken, @cgrp can be destroyed at any point. |
| 4625 | * Pin it so that it stays accessible. |
| 4626 | */ |
| 4627 | cgroup_get(cgrp); |
| 4628 | kernfs_break_active_protection(kn); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4629 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4630 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4631 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4632 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4633 | /* |
| 4634 | * @cgrp might already have been destroyed while we're trying to |
| 4635 | * grab the mutexes. |
| 4636 | */ |
| 4637 | if (!cgroup_is_dead(cgrp)) |
| 4638 | ret = cgroup_destroy_locked(cgrp); |
| 4639 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4640 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4641 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4642 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4643 | kernfs_unbreak_active_protection(kn); |
| 4644 | cgroup_put(cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4645 | return ret; |
| 4646 | } |
| 4647 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4648 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
| 4649 | .remount_fs = cgroup_remount, |
| 4650 | .show_options = cgroup_show_options, |
| 4651 | .mkdir = cgroup_mkdir, |
| 4652 | .rmdir = cgroup_rmdir, |
| 4653 | .rename = cgroup_rename, |
| 4654 | }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4655 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4656 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4657 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4658 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4659 | |
| 4660 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4661 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4662 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4663 | mutex_lock(&cgroup_mutex); |
| 4664 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4665 | idr_init(&ss->css_idr); |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4666 | INIT_LIST_HEAD(&ss->cfts); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4667 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4668 | /* Create the root cgroup state for this subsystem */ |
| 4669 | ss->root = &cgrp_dfl_root; |
| 4670 | css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4671 | /* We don't handle early failures gracefully */ |
| 4672 | BUG_ON(IS_ERR(css)); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4673 | init_and_link_css(css, ss, &cgrp_dfl_root.cgrp); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4674 | if (early) { |
| 4675 | /* idr_alloc() can't be called safely during early init */ |
| 4676 | css->id = 1; |
| 4677 | } else { |
| 4678 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL); |
| 4679 | BUG_ON(css->id < 0); |
| 4680 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4681 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4682 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4683 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4684 | * newly registered, all tasks and hence the |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4685 | * init_css_set is in the subsystem's root cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4686 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4687 | |
| 4688 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4689 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4690 | /* At system boot, before all subsystems have been |
| 4691 | * registered, no tasks have been forked, so we don't |
| 4692 | * need to invoke fork callbacks here. */ |
| 4693 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4694 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4695 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4696 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4697 | cgrp_dfl_root.subsys_mask |= 1 << ss->id; |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4698 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4699 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4700 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4704 | * cgroup_init_early - cgroup initialization at system boot |
| 4705 | * |
| 4706 | * Initialize cgroups at system boot, and initialize any |
| 4707 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4708 | */ |
| 4709 | int __init cgroup_init_early(void) |
| 4710 | { |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 4711 | static struct cgroup_sb_opts __initdata opts = |
| 4712 | { .flags = CGRP_ROOT_SANE_BEHAVIOR }; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4713 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4714 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4715 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4716 | init_cgroup_root(&cgrp_dfl_root, &opts); |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4717 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4718 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4719 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4720 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4721 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n", |
| 4722 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4723 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4724 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 4725 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4726 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4727 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4728 | ss->name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4729 | |
| 4730 | if (ss->early_init) |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4731 | cgroup_init_subsys(ss, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4732 | } |
| 4733 | return 0; |
| 4734 | } |
| 4735 | |
| 4736 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4737 | * cgroup_init - cgroup initialization |
| 4738 | * |
| 4739 | * Register cgroup filesystem and /proc file, and initialize |
| 4740 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4741 | */ |
| 4742 | int __init cgroup_init(void) |
| 4743 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4744 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4745 | unsigned long key; |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4746 | int ssid, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4747 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4748 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4749 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4750 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4751 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4752 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4753 | /* Add init_css_set to the hash table */ |
| 4754 | key = css_set_hash(init_css_set.subsys); |
| 4755 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4756 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4757 | BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4758 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4759 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4760 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4761 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4762 | for_each_subsys(ss, ssid) { |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4763 | if (ss->early_init) { |
| 4764 | struct cgroup_subsys_state *css = |
| 4765 | init_css_set.subsys[ss->id]; |
| 4766 | |
| 4767 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, |
| 4768 | GFP_KERNEL); |
| 4769 | BUG_ON(css->id < 0); |
| 4770 | } else { |
| 4771 | cgroup_init_subsys(ss, false); |
| 4772 | } |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4773 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4774 | list_add_tail(&init_css_set.e_cset_node[ssid], |
| 4775 | &cgrp_dfl_root.cgrp.e_csets[ssid]); |
| 4776 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4777 | /* |
| 4778 | * cftype registration needs kmalloc and can't be done |
| 4779 | * during early_init. Register base cftypes separately. |
| 4780 | */ |
| 4781 | if (ss->base_cftypes) |
| 4782 | WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes)); |
| 4783 | } |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4784 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4785 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4786 | if (!cgroup_kobj) |
| 4787 | return -ENOMEM; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4788 | |
| 4789 | err = register_filesystem(&cgroup_fs_type); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4790 | if (err < 0) { |
| 4791 | kobject_put(cgroup_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4792 | return err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4796 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4797 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4798 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4799 | static int __init cgroup_wq_init(void) |
| 4800 | { |
| 4801 | /* |
| 4802 | * There isn't much point in executing destruction path in |
| 4803 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 4804 | * Use 1 for @max_active. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4805 | * |
| 4806 | * We would prefer to do this in cgroup_init() above, but that |
| 4807 | * is called before init_workqueues(): so leave this until after. |
| 4808 | */ |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 4809 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4810 | BUG_ON(!cgroup_destroy_wq); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 4811 | |
| 4812 | /* |
| 4813 | * Used to destroy pidlists and separate to serve as flush domain. |
| 4814 | * Cap @max_active to 1 too. |
| 4815 | */ |
| 4816 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 4817 | 0, 1); |
| 4818 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 4819 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4820 | return 0; |
| 4821 | } |
| 4822 | core_initcall(cgroup_wq_init); |
| 4823 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4824 | /* |
| 4825 | * proc_cgroup_show() |
| 4826 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4827 | * - Used for /proc/<pid>/cgroup. |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4828 | */ |
| 4829 | |
| 4830 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4831 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4832 | { |
| 4833 | struct pid *pid; |
| 4834 | struct task_struct *tsk; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4835 | char *buf, *path; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4836 | int retval; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4837 | struct cgroup_root *root; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4838 | |
| 4839 | retval = -ENOMEM; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4840 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4841 | if (!buf) |
| 4842 | goto out; |
| 4843 | |
| 4844 | retval = -ESRCH; |
| 4845 | pid = m->private; |
| 4846 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4847 | if (!tsk) |
| 4848 | goto out_free; |
| 4849 | |
| 4850 | retval = 0; |
| 4851 | |
| 4852 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4853 | down_read(&css_set_rwsem); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4854 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4855 | for_each_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4856 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4857 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4858 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4859 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 4860 | if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4861 | continue; |
| 4862 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4863 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4864 | for_each_subsys(ss, ssid) |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4865 | if (root->subsys_mask & (1 << ssid)) |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4866 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4867 | if (strlen(root->name)) |
| 4868 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4869 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4870 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4871 | cgrp = task_cgroup_from_root(tsk, root); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4872 | path = cgroup_path(cgrp, buf, PATH_MAX); |
| 4873 | if (!path) { |
| 4874 | retval = -ENAMETOOLONG; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4875 | goto out_unlock; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4876 | } |
| 4877 | seq_puts(m, path); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4878 | seq_putc(m, '\n'); |
| 4879 | } |
| 4880 | |
| 4881 | out_unlock: |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4882 | up_read(&css_set_rwsem); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4883 | mutex_unlock(&cgroup_mutex); |
| 4884 | put_task_struct(tsk); |
| 4885 | out_free: |
| 4886 | kfree(buf); |
| 4887 | out: |
| 4888 | return retval; |
| 4889 | } |
| 4890 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4891 | /* Display information about each subsystem and each hierarchy */ |
| 4892 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4893 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4894 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4895 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4896 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4897 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4898 | /* |
| 4899 | * ideally we don't want subsystems moving around while we do this. |
| 4900 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4901 | * subsys/hierarchy state. |
| 4902 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4903 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4904 | |
| 4905 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4906 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4907 | ss->name, ss->root->hierarchy_id, |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4908 | atomic_read(&ss->root->nr_cgrps), !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4909 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4910 | mutex_unlock(&cgroup_mutex); |
| 4911 | return 0; |
| 4912 | } |
| 4913 | |
| 4914 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4915 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4916 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4917 | } |
| 4918 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4919 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4920 | .open = cgroupstats_open, |
| 4921 | .read = seq_read, |
| 4922 | .llseek = seq_lseek, |
| 4923 | .release = single_release, |
| 4924 | }; |
| 4925 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4926 | /** |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4927 | * cgroup_fork - initialize cgroup related fields during copy_process() |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4928 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4929 | * |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4930 | * A task is associated with the init_css_set until cgroup_post_fork() |
| 4931 | * attaches it to the parent's css_set. Empty cg_list indicates that |
| 4932 | * @child isn't holding reference to its css_set. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4933 | */ |
| 4934 | void cgroup_fork(struct task_struct *child) |
| 4935 | { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4936 | RCU_INIT_POINTER(child->cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4937 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4941 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4942 | * @child: the task in question |
| 4943 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4944 | * Adds the task to the list running through its css_set if necessary and |
| 4945 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4946 | * 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] | 4947 | * 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] | 4948 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4949 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4950 | void cgroup_post_fork(struct task_struct *child) |
| 4951 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4952 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4953 | int i; |
| 4954 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4955 | /* |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4956 | * This may race against cgroup_enable_task_cg_links(). As that |
| 4957 | * function sets use_task_css_set_links before grabbing |
| 4958 | * tasklist_lock and we just went through tasklist_lock to add |
| 4959 | * @child, it's guaranteed that either we see the set |
| 4960 | * use_task_css_set_links or cgroup_enable_task_cg_lists() sees |
| 4961 | * @child during its iteration. |
| 4962 | * |
| 4963 | * If we won the race, @child is associated with %current's |
| 4964 | * css_set. Grabbing css_set_rwsem guarantees both that the |
| 4965 | * association is stable, and, on completion of the parent's |
| 4966 | * migration, @child is visible in the source of migration or |
| 4967 | * already in the destination cgroup. This guarantee is necessary |
| 4968 | * when implementing operations which need to migrate all tasks of |
| 4969 | * a cgroup to another. |
| 4970 | * |
| 4971 | * Note that if we lose to cgroup_enable_task_cg_links(), @child |
| 4972 | * will remain in init_css_set. This is safe because all tasks are |
| 4973 | * in the init_css_set before cg_links is enabled and there's no |
| 4974 | * operation which transfers all tasks out of init_css_set. |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4975 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4976 | if (use_task_css_set_links) { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4977 | struct css_set *cset; |
| 4978 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4979 | down_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4980 | cset = task_css_set(current); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4981 | if (list_empty(&child->cg_list)) { |
| 4982 | rcu_assign_pointer(child->cgroups, cset); |
| 4983 | list_add(&child->cg_list, &cset->tasks); |
| 4984 | get_css_set(cset); |
| 4985 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4986 | up_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4987 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4988 | |
| 4989 | /* |
| 4990 | * Call ss->fork(). This must happen after @child is linked on |
| 4991 | * css_set; otherwise, @child might change state between ->fork() |
| 4992 | * and addition to css_set. |
| 4993 | */ |
| 4994 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4995 | for_each_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4996 | if (ss->fork) |
| 4997 | ss->fork(child); |
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 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5000 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5001 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5002 | * cgroup_exit - detach cgroup from exiting task |
| 5003 | * @tsk: pointer to task_struct of exiting process |
| 5004 | * |
| 5005 | * Description: Detach cgroup from @tsk and release it. |
| 5006 | * |
| 5007 | * Note that cgroups marked notify_on_release force every task in |
| 5008 | * them to take the global cgroup_mutex mutex when exiting. |
| 5009 | * This could impact scaling on very large systems. Be reluctant to |
| 5010 | * use notify_on_release cgroups where very high task exit scaling |
| 5011 | * is required on large systems. |
| 5012 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5013 | * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We |
| 5014 | * call cgroup_exit() while the task is still competent to handle |
| 5015 | * notify_on_release(), then leave the task attached to the root cgroup in |
| 5016 | * each hierarchy for the remainder of its exit. No need to bother with |
| 5017 | * 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] | 5018 | * with migration path - PF_EXITING is visible to migration path. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5019 | */ |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 5020 | void cgroup_exit(struct task_struct *tsk) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5021 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5022 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5023 | struct css_set *cset; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5024 | bool put_cset = false; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5025 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5026 | |
| 5027 | /* |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5028 | * Unlink from @tsk from its css_set. As migration path can't race |
| 5029 | * with us, we can check cg_list without grabbing css_set_rwsem. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5030 | */ |
| 5031 | if (!list_empty(&tsk->cg_list)) { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5032 | down_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5033 | list_del_init(&tsk->cg_list); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5034 | up_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5035 | put_cset = true; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5036 | } |
| 5037 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5038 | /* Reassign the task to the init_css_set. */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5039 | cset = task_css_set(tsk); |
| 5040 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5041 | |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 5042 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5043 | /* see cgroup_post_fork() for details */ |
| 5044 | for_each_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5045 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5046 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 5047 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5048 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5049 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5050 | } |
| 5051 | } |
| 5052 | } |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5053 | |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5054 | if (put_cset) |
| 5055 | put_css_set(cset, true); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5056 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5057 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5058 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5059 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5060 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5061 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5062 | /* |
| 5063 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5064 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5065 | * it now |
| 5066 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5067 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5068 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5069 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5070 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5071 | list_empty(&cgrp->release_list)) { |
| 5072 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5073 | need_schedule_work = 1; |
| 5074 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5075 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5076 | if (need_schedule_work) |
| 5077 | schedule_work(&release_agent_work); |
| 5078 | } |
| 5079 | } |
| 5080 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5081 | /* |
| 5082 | * Notify userspace when a cgroup is released, by running the |
| 5083 | * configured release agent with the name of the cgroup (path |
| 5084 | * relative to the root of cgroup file system) as the argument. |
| 5085 | * |
| 5086 | * Most likely, this user command will try to rmdir this cgroup. |
| 5087 | * |
| 5088 | * This races with the possibility that some other task will be |
| 5089 | * attached to this cgroup before it is removed, or that some other |
| 5090 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5091 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5092 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5093 | * to continue to serve a useful existence. Next time it's released, |
| 5094 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5095 | * |
| 5096 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5097 | * means only wait until the task is successfully execve()'d. The |
| 5098 | * separate release agent task is forked by call_usermodehelper(), |
| 5099 | * then control in this thread returns here, without waiting for the |
| 5100 | * release agent task. We don't bother to wait because the caller of |
| 5101 | * this routine has no use for the exit status of the release agent |
| 5102 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5103 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5104 | static void cgroup_release_agent(struct work_struct *work) |
| 5105 | { |
| 5106 | BUG_ON(work != &release_agent_work); |
| 5107 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5108 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5109 | while (!list_empty(&release_list)) { |
| 5110 | char *argv[3], *envp[3]; |
| 5111 | int i; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5112 | char *pathbuf = NULL, *agentbuf = NULL, *path; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5113 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5114 | struct cgroup, |
| 5115 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5116 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5117 | raw_spin_unlock(&release_list_lock); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5118 | pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5119 | if (!pathbuf) |
| 5120 | goto continue_free; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5121 | path = cgroup_path(cgrp, pathbuf, PATH_MAX); |
| 5122 | if (!path) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5123 | goto continue_free; |
| 5124 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5125 | if (!agentbuf) |
| 5126 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5127 | |
| 5128 | i = 0; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5129 | argv[i++] = agentbuf; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5130 | argv[i++] = path; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5131 | argv[i] = NULL; |
| 5132 | |
| 5133 | i = 0; |
| 5134 | /* minimal command environment */ |
| 5135 | envp[i++] = "HOME=/"; |
| 5136 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5137 | envp[i] = NULL; |
| 5138 | |
| 5139 | /* Drop the lock while we invoke the usermode helper, |
| 5140 | * since the exec could involve hitting disk and hence |
| 5141 | * be a slow process */ |
| 5142 | mutex_unlock(&cgroup_mutex); |
| 5143 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5144 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5145 | continue_free: |
| 5146 | kfree(pathbuf); |
| 5147 | kfree(agentbuf); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5148 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5149 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5150 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5151 | mutex_unlock(&cgroup_mutex); |
| 5152 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5153 | |
| 5154 | static int __init cgroup_disable(char *str) |
| 5155 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5156 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5157 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5158 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5159 | |
| 5160 | while ((token = strsep(&str, ",")) != NULL) { |
| 5161 | if (!*token) |
| 5162 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5163 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5164 | for_each_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5165 | if (!strcmp(token, ss->name)) { |
| 5166 | ss->disabled = 1; |
| 5167 | printk(KERN_INFO "Disabling %s control group" |
| 5168 | " subsystem\n", ss->name); |
| 5169 | break; |
| 5170 | } |
| 5171 | } |
| 5172 | } |
| 5173 | return 1; |
| 5174 | } |
| 5175 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5176 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5177 | /** |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5178 | * css_tryget_online_from_dir - get corresponding css from a cgroup dentry |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5179 | * @dentry: directory dentry of interest |
| 5180 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5181 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5182 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 5183 | * to get the corresponding css and return it. If such css doesn't exist |
| 5184 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5185 | */ |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5186 | struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, |
| 5187 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5188 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5189 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
| 5190 | struct cgroup_subsys_state *css = NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5191 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5192 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5193 | /* is @dentry a cgroup dir? */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5194 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 5195 | kernfs_type(kn) != KERNFS_DIR) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5196 | return ERR_PTR(-EBADF); |
| 5197 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5198 | rcu_read_lock(); |
| 5199 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5200 | /* |
| 5201 | * This path doesn't originate from kernfs and @kn could already |
| 5202 | * have been or be removed at any point. @kn->priv is RCU |
| 5203 | * protected for this access. See destroy_locked() for details. |
| 5204 | */ |
| 5205 | cgrp = rcu_dereference(kn->priv); |
| 5206 | if (cgrp) |
| 5207 | css = cgroup_css(cgrp, ss); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5208 | |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5209 | if (!css || !css_tryget_online(css)) |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5210 | css = ERR_PTR(-ENOENT); |
| 5211 | |
| 5212 | rcu_read_unlock(); |
| 5213 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5214 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5215 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5216 | /** |
| 5217 | * css_from_id - lookup css by id |
| 5218 | * @id: the cgroup id |
| 5219 | * @ss: cgroup subsys to be looked into |
| 5220 | * |
| 5221 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5222 | * Should be called under rcu_read_lock(). |
| 5223 | */ |
| 5224 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5225 | { |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 5226 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5227 | return idr_find(&ss->css_idr, id); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5228 | } |
| 5229 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5230 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5231 | static struct cgroup_subsys_state * |
| 5232 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5233 | { |
| 5234 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5235 | |
| 5236 | if (!css) |
| 5237 | return ERR_PTR(-ENOMEM); |
| 5238 | |
| 5239 | return css; |
| 5240 | } |
| 5241 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5242 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5243 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5244 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5245 | } |
| 5246 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5247 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 5248 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5249 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5250 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5251 | } |
| 5252 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5253 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 5254 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5255 | { |
| 5256 | return (u64)(unsigned long)current->cgroups; |
| 5257 | } |
| 5258 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5259 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5260 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5261 | { |
| 5262 | u64 count; |
| 5263 | |
| 5264 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5265 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5266 | rcu_read_unlock(); |
| 5267 | return count; |
| 5268 | } |
| 5269 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5270 | 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] | 5271 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5272 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5273 | struct css_set *cset; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5274 | char *name_buf; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5275 | |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5276 | name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL); |
| 5277 | if (!name_buf) |
| 5278 | return -ENOMEM; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5279 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5280 | down_read(&css_set_rwsem); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5281 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5282 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5283 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5284 | struct cgroup *c = link->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5285 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 5286 | cgroup_name(c, name_buf, NAME_MAX + 1); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5287 | seq_printf(seq, "Root %d group %s\n", |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 5288 | c->root->hierarchy_id, name_buf); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5289 | } |
| 5290 | rcu_read_unlock(); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5291 | up_read(&css_set_rwsem); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5292 | kfree(name_buf); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5293 | return 0; |
| 5294 | } |
| 5295 | |
| 5296 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5297 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5298 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5299 | struct cgroup_subsys_state *css = seq_css(seq); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5300 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5301 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5302 | down_read(&css_set_rwsem); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5303 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5304 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5305 | struct task_struct *task; |
| 5306 | int count = 0; |
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 | seq_printf(seq, "css_set %p\n", cset); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 5309 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5310 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 5311 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 5312 | goto overflow; |
| 5313 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5314 | } |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 5315 | |
| 5316 | list_for_each_entry(task, &cset->mg_tasks, cg_list) { |
| 5317 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 5318 | goto overflow; |
| 5319 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
| 5320 | } |
| 5321 | continue; |
| 5322 | overflow: |
| 5323 | seq_puts(seq, " ...\n"); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5324 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5325 | up_read(&css_set_rwsem); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5326 | return 0; |
| 5327 | } |
| 5328 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5329 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5330 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5331 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5332 | } |
| 5333 | |
| 5334 | static struct cftype debug_files[] = { |
| 5335 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5336 | .name = "taskcount", |
| 5337 | .read_u64 = debug_taskcount_read, |
| 5338 | }, |
| 5339 | |
| 5340 | { |
| 5341 | .name = "current_css_set", |
| 5342 | .read_u64 = current_css_set_read, |
| 5343 | }, |
| 5344 | |
| 5345 | { |
| 5346 | .name = "current_css_set_refcount", |
| 5347 | .read_u64 = current_css_set_refcount_read, |
| 5348 | }, |
| 5349 | |
| 5350 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5351 | .name = "current_css_set_cg_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5352 | .seq_show = current_css_set_cg_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5353 | }, |
| 5354 | |
| 5355 | { |
| 5356 | .name = "cgroup_css_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5357 | .seq_show = cgroup_css_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5358 | }, |
| 5359 | |
| 5360 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5361 | .name = "releasable", |
| 5362 | .read_u64 = releasable_read, |
| 5363 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5364 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5365 | { } /* terminate */ |
| 5366 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5367 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5368 | struct cgroup_subsys debug_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5369 | .css_alloc = debug_css_alloc, |
| 5370 | .css_free = debug_css_free, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5371 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5372 | }; |
| 5373 | #endif /* CONFIG_CGROUP_DEBUG */ |