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 | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 74 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 75 | * hierarchy must be performed while holding it. |
| 76 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 77 | * css_set_rwsem protects task->cgroups pointer, the list of css_set |
| 78 | * objects, and the chain of tasks off each css_set. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 79 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 80 | * These locks are exported if CONFIG_PROVE_RCU so that accessors in |
| 81 | * cgroup.h can use them for lockdep annotations. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 82 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 83 | #ifdef CONFIG_PROVE_RCU |
| 84 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 85 | DECLARE_RWSEM(css_set_rwsem); |
| 86 | EXPORT_SYMBOL_GPL(cgroup_mutex); |
| 87 | EXPORT_SYMBOL_GPL(css_set_rwsem); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 88 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 89 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 90 | static DECLARE_RWSEM(css_set_rwsem); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 91 | #endif |
| 92 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 93 | /* |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 94 | * Protects cgroup_idr and css_idr so that IDs can be released without |
| 95 | * grabbing cgroup_mutex. |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 96 | */ |
| 97 | static DEFINE_SPINLOCK(cgroup_idr_lock); |
| 98 | |
| 99 | /* |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 100 | * Protects cgroup_subsys->release_agent_path. Modifying it also requires |
| 101 | * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock. |
| 102 | */ |
| 103 | static DEFINE_SPINLOCK(release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 104 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 105 | #define cgroup_assert_mutex_or_rcu_locked() \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 106 | rcu_lockdep_assert(rcu_read_lock_held() || \ |
| 107 | lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 108 | "cgroup_mutex or RCU read lock required"); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 109 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 110 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 111 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 112 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 113 | * destruction work items don't end up filling up max_active of system_wq |
| 114 | * which may lead to deadlock. |
| 115 | */ |
| 116 | static struct workqueue_struct *cgroup_destroy_wq; |
| 117 | |
| 118 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 119 | * pidlist destructions need to be flushed on cgroup destruction. Use a |
| 120 | * separate workqueue as flush domain. |
| 121 | */ |
| 122 | static struct workqueue_struct *cgroup_pidlist_destroy_wq; |
| 123 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 124 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 125 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 126 | static struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 127 | #include <linux/cgroup_subsys.h> |
| 128 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 129 | #undef SUBSYS |
| 130 | |
| 131 | /* array of cgroup subsystem names */ |
| 132 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 133 | static const char *cgroup_subsys_name[] = { |
| 134 | #include <linux/cgroup_subsys.h> |
| 135 | }; |
| 136 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 137 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 138 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 139 | * The default hierarchy, reserved for the subsystems that are otherwise |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 140 | * unattached - it never has more than a single cgroup, and all tasks are |
| 141 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 142 | */ |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 143 | struct cgroup_root cgrp_dfl_root; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 144 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 145 | /* |
| 146 | * The default hierarchy always exists but is hidden until mounted for the |
| 147 | * first time. This is for backward compatibility. |
| 148 | */ |
| 149 | static bool cgrp_dfl_root_visible; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 150 | |
| 151 | /* The list of hierarchy roots */ |
| 152 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 153 | static LIST_HEAD(cgroup_roots); |
| 154 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 155 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 156 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 157 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 158 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 159 | /* |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 160 | * Assign a monotonically increasing serial number to csses. It guarantees |
| 161 | * cgroups with bigger numbers are newer than those with smaller numbers. |
| 162 | * Also, as csses are always appended to the parent's ->children list, it |
| 163 | * guarantees that sibling csses are always sorted in the ascending serial |
| 164 | * number order on the list. Protected by cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 165 | */ |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 166 | static u64 css_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 167 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 168 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 169 | * check for fork/exit handlers to call. This avoids us having to do |
| 170 | * extra work in the fork/exit path if none of the subsystems need to |
| 171 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 172 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 173 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 174 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 175 | static struct cftype cgroup_base_files[]; |
| 176 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 177 | static void cgroup_put(struct cgroup *cgrp); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 178 | static bool cgroup_has_live_children(struct cgroup *cgrp); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 179 | static int rebind_subsystems(struct cgroup_root *dst_root, |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 180 | unsigned int ss_mask); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 181 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 182 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 183 | static void css_release(struct percpu_ref *ref); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 184 | static void kill_css(struct cgroup_subsys_state *css); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 185 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 186 | bool is_add); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 187 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 188 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 189 | /* IDR wrappers which synchronize using cgroup_idr_lock */ |
| 190 | static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end, |
| 191 | gfp_t gfp_mask) |
| 192 | { |
| 193 | int ret; |
| 194 | |
| 195 | idr_preload(gfp_mask); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 196 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 197 | ret = idr_alloc(idr, ptr, start, end, gfp_mask); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 198 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 199 | idr_preload_end(); |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id) |
| 204 | { |
| 205 | void *ret; |
| 206 | |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 207 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 208 | ret = idr_replace(idr, ptr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 209 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static void cgroup_idr_remove(struct idr *idr, int id) |
| 214 | { |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 215 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 216 | idr_remove(idr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 217 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 220 | static struct cgroup *cgroup_parent(struct cgroup *cgrp) |
| 221 | { |
| 222 | struct cgroup_subsys_state *parent_css = cgrp->self.parent; |
| 223 | |
| 224 | if (parent_css) |
| 225 | return container_of(parent_css, struct cgroup, self); |
| 226 | return NULL; |
| 227 | } |
| 228 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 229 | /** |
| 230 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 231 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 232 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 233 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 234 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 235 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 236 | * the caller is responsible for pinning the returned css if it wants to |
| 237 | * keep accessing it outside the said locks. This function may return |
| 238 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 239 | */ |
| 240 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 241 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 242 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 243 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 244 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 245 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 246 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 247 | return &cgrp->self; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 248 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 249 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 250 | /** |
| 251 | * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem |
| 252 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 253 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 254 | * |
| 255 | * Similar to cgroup_css() but returns the effctive css, which is defined |
| 256 | * as the matching css of the nearest ancestor including self which has @ss |
| 257 | * enabled. If @ss is associated with the hierarchy @cgrp is on, this |
| 258 | * function is guaranteed to return non-NULL css. |
| 259 | */ |
| 260 | static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp, |
| 261 | struct cgroup_subsys *ss) |
| 262 | { |
| 263 | lockdep_assert_held(&cgroup_mutex); |
| 264 | |
| 265 | if (!ss) |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 266 | return &cgrp->self; |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 267 | |
| 268 | if (!(cgrp->root->subsys_mask & (1 << ss->id))) |
| 269 | return NULL; |
| 270 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 271 | while (cgroup_parent(cgrp) && |
| 272 | !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id))) |
| 273 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 274 | |
| 275 | return cgroup_css(cgrp, ss); |
| 276 | } |
| 277 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 278 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 279 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 280 | { |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame^] | 281 | return !(cgrp->self.flags & CSS_ONLINE); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 284 | struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 285 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 286 | struct cgroup *cgrp = of->kn->parent->priv; |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 287 | struct cftype *cft = of_cft(of); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 288 | |
| 289 | /* |
| 290 | * This is open and unprotected implementation of cgroup_css(). |
| 291 | * seq_css() is only called from a kernfs file operation which has |
| 292 | * an active reference on the file. Because all the subsystem |
| 293 | * files are drained before a css is disassociated with a cgroup, |
| 294 | * the matching css from the cgroup's subsys table is guaranteed to |
| 295 | * be and stay valid until the enclosing operation is complete. |
| 296 | */ |
| 297 | if (cft->ss) |
| 298 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 299 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 300 | return &cgrp->self; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 301 | } |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 302 | EXPORT_SYMBOL_GPL(of_css); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 303 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 304 | /** |
| 305 | * cgroup_is_descendant - test ancestry |
| 306 | * @cgrp: the cgroup to be tested |
| 307 | * @ancestor: possible ancestor of @cgrp |
| 308 | * |
| 309 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 310 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 311 | * and @ancestor are accessible. |
| 312 | */ |
| 313 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 314 | { |
| 315 | while (cgrp) { |
| 316 | if (cgrp == ancestor) |
| 317 | return true; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 318 | cgrp = cgroup_parent(cgrp); |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 319 | } |
| 320 | return false; |
| 321 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 322 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 323 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 324 | { |
| 325 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 326 | (1 << CGRP_RELEASABLE) | |
| 327 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 328 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 331 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 332 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 333 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 336 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 337 | * for_each_css - iterate all css's of a cgroup |
| 338 | * @css: the iteration cursor |
| 339 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 340 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 341 | * |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 342 | * Should be called under cgroup_[tree_]mutex. |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 343 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 344 | #define for_each_css(css, ssid, cgrp) \ |
| 345 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 346 | if (!((css) = rcu_dereference_check( \ |
| 347 | (cgrp)->subsys[(ssid)], \ |
| 348 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 349 | else |
| 350 | |
| 351 | /** |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 352 | * for_each_e_css - iterate all effective css's of a cgroup |
| 353 | * @css: the iteration cursor |
| 354 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 355 | * @cgrp: the target cgroup to iterate css's of |
| 356 | * |
| 357 | * Should be called under cgroup_[tree_]mutex. |
| 358 | */ |
| 359 | #define for_each_e_css(css, ssid, cgrp) \ |
| 360 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 361 | if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \ |
| 362 | ; \ |
| 363 | else |
| 364 | |
| 365 | /** |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 366 | * for_each_subsys - iterate all enabled cgroup subsystems |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 367 | * @ss: the iteration cursor |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 368 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 369 | */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 370 | #define for_each_subsys(ss, ssid) \ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 371 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \ |
| 372 | (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 373 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 374 | /* iterate across the hierarchies */ |
| 375 | #define for_each_root(root) \ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 376 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 377 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 378 | /* iterate over child cgrps, lock should be held throughout iteration */ |
| 379 | #define cgroup_for_each_live_child(child, cgrp) \ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 380 | list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 381 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 382 | cgroup_is_dead(child); })) \ |
| 383 | ; \ |
| 384 | else |
| 385 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 386 | /* the list of cgroups eligible for automatic release. Protected by |
| 387 | * release_list_lock */ |
| 388 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 389 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 390 | static void cgroup_release_agent(struct work_struct *work); |
| 391 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 392 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 393 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 394 | /* |
| 395 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 396 | * belong to different cgroups on different hierarchies. In the other |
| 397 | * direction, a css_set is naturally associated with multiple cgroups. |
| 398 | * This M:N relationship is represented by the following link structure |
| 399 | * which exists for each association and allows traversing the associations |
| 400 | * from both sides. |
| 401 | */ |
| 402 | struct cgrp_cset_link { |
| 403 | /* the cgroup and css_set this link associates */ |
| 404 | struct cgroup *cgrp; |
| 405 | struct css_set *cset; |
| 406 | |
| 407 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 408 | struct list_head cset_link; |
| 409 | |
| 410 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 411 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 412 | }; |
| 413 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 414 | /* |
| 415 | * 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] | 416 | * hierarchies being mounted. It contains a pointer to the root state |
| 417 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 418 | * reference-counted, to improve performance when child cgroups |
| 419 | * haven't been created. |
| 420 | */ |
Tejun Heo | 5024ae2 | 2014-05-07 21:31:17 -0400 | [diff] [blame] | 421 | struct css_set init_css_set = { |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 422 | .refcount = ATOMIC_INIT(1), |
| 423 | .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), |
| 424 | .tasks = LIST_HEAD_INIT(init_css_set.tasks), |
| 425 | .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), |
| 426 | .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node), |
| 427 | .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), |
| 428 | }; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 429 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 430 | static int css_set_count = 1; /* 1 for init_css_set */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 431 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 432 | /** |
| 433 | * cgroup_update_populated - updated populated count of a cgroup |
| 434 | * @cgrp: the target cgroup |
| 435 | * @populated: inc or dec populated count |
| 436 | * |
| 437 | * @cgrp is either getting the first task (css_set) or losing the last. |
| 438 | * Update @cgrp->populated_cnt accordingly. The count is propagated |
| 439 | * towards root so that a given cgroup's populated_cnt is zero iff the |
| 440 | * cgroup and all its descendants are empty. |
| 441 | * |
| 442 | * @cgrp's interface file "cgroup.populated" is zero if |
| 443 | * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt |
| 444 | * changes from or to zero, userland is notified that the content of the |
| 445 | * interface file has changed. This can be used to detect when @cgrp and |
| 446 | * its descendants become populated or empty. |
| 447 | */ |
| 448 | static void cgroup_update_populated(struct cgroup *cgrp, bool populated) |
| 449 | { |
| 450 | lockdep_assert_held(&css_set_rwsem); |
| 451 | |
| 452 | do { |
| 453 | bool trigger; |
| 454 | |
| 455 | if (populated) |
| 456 | trigger = !cgrp->populated_cnt++; |
| 457 | else |
| 458 | trigger = !--cgrp->populated_cnt; |
| 459 | |
| 460 | if (!trigger) |
| 461 | break; |
| 462 | |
| 463 | if (cgrp->populated_kn) |
| 464 | kernfs_notify(cgrp->populated_kn); |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 465 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 466 | } while (cgrp); |
| 467 | } |
| 468 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 469 | /* |
| 470 | * hash table for cgroup groups. This improves the performance to find |
| 471 | * an existing css_set. This hash doesn't (currently) take into |
| 472 | * account cgroups in empty hierarchies. |
| 473 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 474 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 475 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 476 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 477 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 478 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 479 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 480 | struct cgroup_subsys *ss; |
| 481 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 482 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 483 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 484 | key += (unsigned long)css[i]; |
| 485 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 486 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 487 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 490 | static void put_css_set_locked(struct css_set *cset, bool taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 491 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 492 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 493 | struct cgroup_subsys *ss; |
| 494 | int ssid; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 495 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 496 | lockdep_assert_held(&css_set_rwsem); |
| 497 | |
| 498 | if (!atomic_dec_and_test(&cset->refcount)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 499 | return; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 500 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 501 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 502 | for_each_subsys(ss, ssid) |
| 503 | list_del(&cset->e_cset_node[ssid]); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 504 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 505 | css_set_count--; |
| 506 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 507 | 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] | 508 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 509 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 510 | list_del(&link->cset_link); |
| 511 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 512 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 513 | /* @cgrp can't go away while we're holding css_set_rwsem */ |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 514 | if (list_empty(&cgrp->cset_links)) { |
| 515 | cgroup_update_populated(cgrp, false); |
| 516 | if (notify_on_release(cgrp)) { |
| 517 | if (taskexit) |
| 518 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 519 | check_for_release(cgrp); |
| 520 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 521 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 522 | |
| 523 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 524 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 525 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 526 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 529 | static void put_css_set(struct css_set *cset, bool taskexit) |
| 530 | { |
| 531 | /* |
| 532 | * Ensure that the refcount doesn't hit zero while any readers |
| 533 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 534 | * rwlock |
| 535 | */ |
| 536 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
| 537 | return; |
| 538 | |
| 539 | down_write(&css_set_rwsem); |
| 540 | put_css_set_locked(cset, taskexit); |
| 541 | up_write(&css_set_rwsem); |
| 542 | } |
| 543 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 544 | /* |
| 545 | * refcounted get/put for css_set objects |
| 546 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 547 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 548 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 549 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 552 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 553 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 554 | * @cset: candidate css_set being tested |
| 555 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 556 | * @new_cgrp: cgroup that's being entered by the task |
| 557 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 558 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 559 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 560 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 561 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 562 | static bool compare_css_sets(struct css_set *cset, |
| 563 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 564 | struct cgroup *new_cgrp, |
| 565 | struct cgroup_subsys_state *template[]) |
| 566 | { |
| 567 | struct list_head *l1, *l2; |
| 568 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 569 | /* |
| 570 | * On the default hierarchy, there can be csets which are |
| 571 | * associated with the same set of cgroups but different csses. |
| 572 | * Let's first ensure that csses match. |
| 573 | */ |
| 574 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 575 | return false; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 576 | |
| 577 | /* |
| 578 | * Compare cgroup pointers in order to distinguish between |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 579 | * different cgroups in hierarchies. As different cgroups may |
| 580 | * share the same effective css, this comparison is always |
| 581 | * necessary. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 582 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 583 | l1 = &cset->cgrp_links; |
| 584 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 585 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 586 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 587 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 588 | |
| 589 | l1 = l1->next; |
| 590 | l2 = l2->next; |
| 591 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 592 | if (l1 == &cset->cgrp_links) { |
| 593 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 594 | break; |
| 595 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 596 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 597 | } |
| 598 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 599 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 600 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 601 | cgrp1 = link1->cgrp; |
| 602 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 603 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 604 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * If this hierarchy is the hierarchy of the cgroup |
| 608 | * that's changing, then we need to check that this |
| 609 | * css_set points to the new cgroup; if it's any other |
| 610 | * hierarchy, then this css_set should point to the |
| 611 | * same cgroup as the old css_set. |
| 612 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 613 | if (cgrp1->root == new_cgrp->root) { |
| 614 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 615 | return false; |
| 616 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 617 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 618 | return false; |
| 619 | } |
| 620 | } |
| 621 | return true; |
| 622 | } |
| 623 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 624 | /** |
| 625 | * find_existing_css_set - init css array and find the matching css_set |
| 626 | * @old_cset: the css_set that we're using before the cgroup transition |
| 627 | * @cgrp: the cgroup that we're moving into |
| 628 | * @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] | 629 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 630 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 631 | struct cgroup *cgrp, |
| 632 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 633 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 634 | struct cgroup_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 635 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 636 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 637 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 638 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 639 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 640 | /* |
| 641 | * Build the set of subsystem state objects that we want to see in the |
| 642 | * new css_set. while subsystems can change globally, the entries here |
| 643 | * won't change, so no need for locking. |
| 644 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 645 | for_each_subsys(ss, i) { |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 646 | if (root->subsys_mask & (1UL << i)) { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 647 | /* |
| 648 | * @ss is in this hierarchy, so we want the |
| 649 | * effective css from @cgrp. |
| 650 | */ |
| 651 | template[i] = cgroup_e_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 652 | } else { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 653 | /* |
| 654 | * @ss is not in this hierarchy, so we don't want |
| 655 | * to change the css. |
| 656 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 657 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 661 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 662 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 663 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 664 | continue; |
| 665 | |
| 666 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 667 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 668 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 669 | |
| 670 | /* No existing cgroup group matched */ |
| 671 | return NULL; |
| 672 | } |
| 673 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 674 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 675 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 676 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 677 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 678 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 679 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 680 | kfree(link); |
| 681 | } |
| 682 | } |
| 683 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 684 | /** |
| 685 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 686 | * @count: the number of links to allocate |
| 687 | * @tmp_links: list_head the allocated links are put on |
| 688 | * |
| 689 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 690 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 691 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 692 | 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] | 693 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 694 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 695 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 696 | |
| 697 | INIT_LIST_HEAD(tmp_links); |
| 698 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 699 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 700 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 701 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 702 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 703 | return -ENOMEM; |
| 704 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 705 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 706 | } |
| 707 | return 0; |
| 708 | } |
| 709 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 710 | /** |
| 711 | * 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] | 712 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 713 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 714 | * @cgrp: the destination cgroup |
| 715 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 716 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 717 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 718 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 719 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 720 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 721 | BUG_ON(list_empty(tmp_links)); |
Tejun Heo | 6803c00 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 722 | |
| 723 | if (cgroup_on_dfl(cgrp)) |
| 724 | cset->dfl_cgrp = cgrp; |
| 725 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 726 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 727 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 728 | link->cgrp = cgrp; |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 729 | |
| 730 | if (list_empty(&cgrp->cset_links)) |
| 731 | cgroup_update_populated(cgrp, true); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 732 | list_move(&link->cset_link, &cgrp->cset_links); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 733 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 734 | /* |
| 735 | * Always add links to the tail of the list so that the list |
| 736 | * is sorted by order of hierarchy creation |
| 737 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 738 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 741 | /** |
| 742 | * find_css_set - return a new css_set with one cgroup updated |
| 743 | * @old_cset: the baseline css_set |
| 744 | * @cgrp: the cgroup to be updated |
| 745 | * |
| 746 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 747 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 748 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 749 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 750 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 751 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 752 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 753 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 754 | struct list_head tmp_links; |
| 755 | struct cgrp_cset_link *link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 756 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 757 | unsigned long key; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 758 | int ssid; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 759 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 760 | lockdep_assert_held(&cgroup_mutex); |
| 761 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 762 | /* First see if we already have a cgroup group that matches |
| 763 | * the desired set */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 764 | down_read(&css_set_rwsem); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 765 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 766 | if (cset) |
| 767 | get_css_set(cset); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 768 | up_read(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 769 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 770 | if (cset) |
| 771 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 772 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 773 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 774 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 775 | return NULL; |
| 776 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 777 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 778 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 779 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 780 | return NULL; |
| 781 | } |
| 782 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 783 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 784 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 785 | INIT_LIST_HEAD(&cset->tasks); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 786 | INIT_LIST_HEAD(&cset->mg_tasks); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 787 | INIT_LIST_HEAD(&cset->mg_preload_node); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 788 | INIT_LIST_HEAD(&cset->mg_node); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 789 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 790 | |
| 791 | /* Copy the set of subsystem state objects generated in |
| 792 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 793 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 794 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 795 | down_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 796 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 797 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 798 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 799 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 800 | if (c->root == cgrp->root) |
| 801 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 802 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 803 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 804 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 805 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 806 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 807 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 808 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 809 | /* Add @cset to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 810 | key = css_set_hash(cset->subsys); |
| 811 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 812 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 813 | for_each_subsys(ss, ssid) |
| 814 | list_add_tail(&cset->e_cset_node[ssid], |
| 815 | &cset->subsys[ssid]->cgroup->e_csets[ssid]); |
| 816 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 817 | up_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 818 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 819 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 822 | 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] | 823 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 824 | struct cgroup *root_cgrp = kf_root->kn->priv; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 825 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 826 | return root_cgrp->root; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 827 | } |
| 828 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 829 | static int cgroup_init_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 830 | { |
| 831 | int id; |
| 832 | |
| 833 | lockdep_assert_held(&cgroup_mutex); |
| 834 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 835 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 836 | if (id < 0) |
| 837 | return id; |
| 838 | |
| 839 | root->hierarchy_id = id; |
| 840 | return 0; |
| 841 | } |
| 842 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 843 | static void cgroup_exit_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 844 | { |
| 845 | lockdep_assert_held(&cgroup_mutex); |
| 846 | |
| 847 | if (root->hierarchy_id) { |
| 848 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
| 849 | root->hierarchy_id = 0; |
| 850 | } |
| 851 | } |
| 852 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 853 | static void cgroup_free_root(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 854 | { |
| 855 | if (root) { |
| 856 | /* hierarhcy ID shoulid already have been released */ |
| 857 | WARN_ON_ONCE(root->hierarchy_id); |
| 858 | |
| 859 | idr_destroy(&root->cgroup_idr); |
| 860 | kfree(root); |
| 861 | } |
| 862 | } |
| 863 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 864 | static void cgroup_destroy_root(struct cgroup_root *root) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 865 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 866 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 867 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 868 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 869 | mutex_lock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 870 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 871 | BUG_ON(atomic_read(&root->nr_cgrps)); |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 872 | BUG_ON(!list_empty(&cgrp->self.children)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 873 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 874 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 875 | rebind_subsystems(&cgrp_dfl_root, root->subsys_mask); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 876 | |
| 877 | /* |
| 878 | * Release all the links from cset_links to this hierarchy's |
| 879 | * root cgroup |
| 880 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 881 | down_write(&css_set_rwsem); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 882 | |
| 883 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 884 | list_del(&link->cset_link); |
| 885 | list_del(&link->cgrp_link); |
| 886 | kfree(link); |
| 887 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 888 | up_write(&css_set_rwsem); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 889 | |
| 890 | if (!list_empty(&root->root_list)) { |
| 891 | list_del(&root->root_list); |
| 892 | cgroup_root_count--; |
| 893 | } |
| 894 | |
| 895 | cgroup_exit_root_id(root); |
| 896 | |
| 897 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 898 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 899 | kernfs_destroy_root(root->kf_root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 900 | cgroup_free_root(root); |
| 901 | } |
| 902 | |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 903 | /* look up cgroup associated with given css_set on the specified hierarchy */ |
| 904 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 905 | struct cgroup_root *root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 906 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 907 | struct cgroup *res = NULL; |
| 908 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 909 | lockdep_assert_held(&cgroup_mutex); |
| 910 | lockdep_assert_held(&css_set_rwsem); |
| 911 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 912 | if (cset == &init_css_set) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 913 | res = &root->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 914 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 915 | struct cgrp_cset_link *link; |
| 916 | |
| 917 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 918 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 919 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 920 | if (c->root == root) { |
| 921 | res = c; |
| 922 | break; |
| 923 | } |
| 924 | } |
| 925 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 926 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 927 | BUG_ON(!res); |
| 928 | return res; |
| 929 | } |
| 930 | |
| 931 | /* |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 932 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 933 | * called with cgroup_mutex and css_set_rwsem held. |
| 934 | */ |
| 935 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 936 | struct cgroup_root *root) |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 937 | { |
| 938 | /* |
| 939 | * No need to lock the task - since we hold cgroup_mutex the |
| 940 | * task can't change groups, so the only thing that can happen |
| 941 | * is that it exits and its css is set back to init_css_set. |
| 942 | */ |
| 943 | return cset_cgroup_from_root(task_css_set(task), root); |
| 944 | } |
| 945 | |
| 946 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 947 | * A task must hold cgroup_mutex to modify cgroups. |
| 948 | * |
| 949 | * Any task can increment and decrement the count field without lock. |
| 950 | * So in general, code holding cgroup_mutex can't rely on the count |
| 951 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 952 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 953 | * means that no tasks are currently attached, therefore there is no |
| 954 | * way a task attached to that cgroup can fork (the other way to |
| 955 | * increment the count). So code holding cgroup_mutex can safely |
| 956 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 957 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 958 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 959 | * needs that mutex. |
| 960 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 961 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 962 | * (usually) take cgroup_mutex. These are the two most performance |
| 963 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 964 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 965 | * 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] | 966 | * to the release agent with the name of the cgroup (path relative to |
| 967 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 968 | * |
| 969 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 970 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 971 | * 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] | 972 | * least one task in the system (init, pid == 1), therefore, root cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 973 | * 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] | 974 | * need a special hack to ensure that root cgroup cannot be deleted. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 975 | * |
| 976 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 977 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 978 | */ |
| 979 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 980 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 981 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 982 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 983 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 984 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 985 | char *buf) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 986 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 987 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 988 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) |
| 989 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s", |
| 990 | cft->ss->name, cft->name); |
| 991 | else |
| 992 | strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
| 993 | return buf; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 996 | /** |
| 997 | * cgroup_file_mode - deduce file mode of a control file |
| 998 | * @cft: the control file in question |
| 999 | * |
| 1000 | * returns cft->mode if ->mode is not 0 |
| 1001 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 1002 | * returns S_IRUGO if it has only a read handler |
| 1003 | * returns S_IWUSR if it has only a write hander |
| 1004 | */ |
| 1005 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1006 | { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1007 | umode_t mode = 0; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1008 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1009 | if (cft->mode) |
| 1010 | return cft->mode; |
| 1011 | |
| 1012 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 1013 | mode |= S_IRUGO; |
| 1014 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1015 | if (cft->write_u64 || cft->write_s64 || cft->write) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1016 | mode |= S_IWUSR; |
| 1017 | |
| 1018 | return mode; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1019 | } |
| 1020 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1021 | static void cgroup_get(struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1022 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1023 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1024 | css_get(&cgrp->self); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1027 | static void cgroup_put(struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1028 | { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1029 | css_put(&cgrp->self); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1032 | /** |
| 1033 | * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods |
| 1034 | * @kn: the kernfs_node being serviced |
| 1035 | * |
| 1036 | * This helper undoes cgroup_kn_lock_live() and should be invoked before |
| 1037 | * the method finishes if locking succeeded. Note that once this function |
| 1038 | * returns the cgroup returned by cgroup_kn_lock_live() may become |
| 1039 | * inaccessible any time. If the caller intends to continue to access the |
| 1040 | * cgroup, it should pin it before invoking this function. |
| 1041 | */ |
| 1042 | static void cgroup_kn_unlock(struct kernfs_node *kn) |
| 1043 | { |
| 1044 | struct cgroup *cgrp; |
| 1045 | |
| 1046 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1047 | cgrp = kn->priv; |
| 1048 | else |
| 1049 | cgrp = kn->parent->priv; |
| 1050 | |
| 1051 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1052 | |
| 1053 | kernfs_unbreak_active_protection(kn); |
| 1054 | cgroup_put(cgrp); |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * cgroup_kn_lock_live - locking helper for cgroup kernfs methods |
| 1059 | * @kn: the kernfs_node being serviced |
| 1060 | * |
| 1061 | * This helper is to be used by a cgroup kernfs method currently servicing |
| 1062 | * @kn. It breaks the active protection, performs cgroup locking and |
| 1063 | * verifies that the associated cgroup is alive. Returns the cgroup if |
| 1064 | * alive; otherwise, %NULL. A successful return should be undone by a |
| 1065 | * matching cgroup_kn_unlock() invocation. |
| 1066 | * |
| 1067 | * Any cgroup kernfs method implementation which requires locking the |
| 1068 | * associated cgroup should use this helper. It avoids nesting cgroup |
| 1069 | * locking under kernfs active protection and allows all kernfs operations |
| 1070 | * including self-removal. |
| 1071 | */ |
| 1072 | static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn) |
| 1073 | { |
| 1074 | struct cgroup *cgrp; |
| 1075 | |
| 1076 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1077 | cgrp = kn->priv; |
| 1078 | else |
| 1079 | cgrp = kn->parent->priv; |
| 1080 | |
| 1081 | /* |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1082 | * We're gonna grab cgroup_mutex which nests outside kernfs |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1083 | * active_ref. cgroup liveliness check alone provides enough |
| 1084 | * protection against removal. Ensure @cgrp stays accessible and |
| 1085 | * break the active_ref protection. |
| 1086 | */ |
| 1087 | cgroup_get(cgrp); |
| 1088 | kernfs_break_active_protection(kn); |
| 1089 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1090 | mutex_lock(&cgroup_mutex); |
| 1091 | |
| 1092 | if (!cgroup_is_dead(cgrp)) |
| 1093 | return cgrp; |
| 1094 | |
| 1095 | cgroup_kn_unlock(kn); |
| 1096 | return NULL; |
| 1097 | } |
| 1098 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 1099 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1100 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1101 | char name[CGROUP_FILE_NAME_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1102 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1103 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1104 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1107 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 1108 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 1109 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1110 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 1111 | */ |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1112 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned int subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1113 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1114 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1115 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1116 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1117 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 1118 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 1119 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1120 | if (!(subsys_mask & (1 << i))) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1121 | continue; |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 1122 | list_for_each_entry(cfts, &ss->cfts, node) |
| 1123 | cgroup_addrm_files(cgrp, cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1124 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1127 | 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] | 1128 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1129 | struct cgroup_subsys *ss; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1130 | int ssid, i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1131 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1132 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1133 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1134 | for_each_subsys(ss, ssid) { |
| 1135 | if (!(ss_mask & (1 << ssid))) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1136 | continue; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1137 | |
Tejun Heo | 7fd8c56 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1138 | /* if @ss has non-root csses attached to it, can't move */ |
| 1139 | if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss))) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1140 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1141 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1142 | /* can't move between two non-dummy roots either */ |
Tejun Heo | 7fd8c56 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1143 | if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1144 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1147 | ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask); |
| 1148 | if (ret) { |
| 1149 | if (dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1150 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1151 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1152 | /* |
| 1153 | * Rebinding back to the default root is not allowed to |
| 1154 | * fail. Using both default and non-default roots should |
| 1155 | * be rare. Moving subsystems back and forth even more so. |
| 1156 | * Just warn about it and continue. |
| 1157 | */ |
| 1158 | if (cgrp_dfl_root_visible) { |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1159 | pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n", |
Jianyu Zhan | a2a1f9e | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1160 | ret, ss_mask); |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1161 | pr_warn("you may retry by moving them to a different hierarchy and unbinding\n"); |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1162 | } |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1163 | } |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1164 | |
| 1165 | /* |
| 1166 | * Nothing can fail from this point on. Remove files for the |
| 1167 | * removed subsystems and rebind each subsystem. |
| 1168 | */ |
Tejun Heo | 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 | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1172 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1173 | for_each_subsys(ss, ssid) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1174 | struct cgroup_root *src_root; |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1175 | struct cgroup_subsys_state *css; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1176 | struct css_set *cset; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1177 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1178 | if (!(ss_mask & (1 << ssid))) |
| 1179 | continue; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1180 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1181 | src_root = ss->root; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1182 | css = cgroup_css(&src_root->cgrp, ss); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1183 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1184 | WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1185 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1186 | RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL); |
| 1187 | rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1188 | ss->root = dst_root; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1189 | css->cgroup = &dst_root->cgrp; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1190 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1191 | down_write(&css_set_rwsem); |
| 1192 | hash_for_each(css_set_table, i, cset, hlist) |
| 1193 | list_move_tail(&cset->e_cset_node[ss->id], |
| 1194 | &dst_root->cgrp.e_csets[ss->id]); |
| 1195 | up_write(&css_set_rwsem); |
| 1196 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1197 | src_root->subsys_mask &= ~(1 << ssid); |
| 1198 | src_root->cgrp.child_subsys_mask &= ~(1 << ssid); |
| 1199 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1200 | /* default hierarchy doesn't enable controllers by default */ |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1201 | dst_root->subsys_mask |= 1 << ssid; |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1202 | if (dst_root != &cgrp_dfl_root) |
| 1203 | dst_root->cgrp.child_subsys_mask |= 1 << ssid; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1204 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1205 | if (ss->bind) |
| 1206 | ss->bind(css); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1207 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1208 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1209 | kernfs_activate(dst_root->cgrp.kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1210 | return 0; |
| 1211 | } |
| 1212 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1213 | static int cgroup_show_options(struct seq_file *seq, |
| 1214 | struct kernfs_root *kf_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1215 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1216 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1217 | struct cgroup_subsys *ss; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1218 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1219 | |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1220 | for_each_subsys(ss, ssid) |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1221 | if (root->subsys_mask & (1 << ssid)) |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1222 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1223 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1224 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1225 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1226 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1227 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1228 | seq_puts(seq, ",xattr"); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1229 | |
| 1230 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1231 | if (strlen(root->release_agent_path)) |
| 1232 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1233 | spin_unlock(&release_agent_path_lock); |
| 1234 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1235 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1236 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1237 | if (strlen(root->name)) |
| 1238 | seq_printf(seq, ",name=%s", root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | struct cgroup_sb_opts { |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1243 | unsigned int subsys_mask; |
| 1244 | unsigned int flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1245 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1246 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1247 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1248 | /* User explicitly requested empty subsystem */ |
| 1249 | bool none; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1250 | }; |
| 1251 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1252 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1253 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1254 | char *token, *o = data; |
| 1255 | bool all_ss = false, one_ss = false; |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1256 | unsigned int mask = -1U; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1257 | struct cgroup_subsys *ss; |
| 1258 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1259 | |
| 1260 | #ifdef CONFIG_CPUSETS |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1261 | mask = ~(1U << cpuset_cgrp_id); |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1262 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1263 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1264 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1265 | |
| 1266 | while ((token = strsep(&o, ",")) != NULL) { |
| 1267 | if (!*token) |
| 1268 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1269 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1270 | /* Explicitly have no subsystems */ |
| 1271 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1272 | continue; |
| 1273 | } |
| 1274 | if (!strcmp(token, "all")) { |
| 1275 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1276 | if (one_ss) |
| 1277 | return -EINVAL; |
| 1278 | all_ss = true; |
| 1279 | continue; |
| 1280 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1281 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1282 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1283 | continue; |
| 1284 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1285 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1286 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1287 | continue; |
| 1288 | } |
| 1289 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1290 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1291 | continue; |
| 1292 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1293 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1294 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1295 | continue; |
| 1296 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1297 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1298 | /* Specifying two release agents is forbidden */ |
| 1299 | if (opts->release_agent) |
| 1300 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1301 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1302 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1303 | if (!opts->release_agent) |
| 1304 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1305 | continue; |
| 1306 | } |
| 1307 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1308 | const char *name = token + 5; |
| 1309 | /* Can't specify an empty name */ |
| 1310 | if (!strlen(name)) |
| 1311 | return -EINVAL; |
| 1312 | /* Must match [\w.-]+ */ |
| 1313 | for (i = 0; i < strlen(name); i++) { |
| 1314 | char c = name[i]; |
| 1315 | if (isalnum(c)) |
| 1316 | continue; |
| 1317 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1318 | continue; |
| 1319 | return -EINVAL; |
| 1320 | } |
| 1321 | /* Specifying two names is forbidden */ |
| 1322 | if (opts->name) |
| 1323 | return -EINVAL; |
| 1324 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1325 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1326 | GFP_KERNEL); |
| 1327 | if (!opts->name) |
| 1328 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1329 | |
| 1330 | continue; |
| 1331 | } |
| 1332 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1333 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1334 | if (strcmp(token, ss->name)) |
| 1335 | continue; |
| 1336 | if (ss->disabled) |
| 1337 | continue; |
| 1338 | |
| 1339 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1340 | if (all_ss) |
| 1341 | return -EINVAL; |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1342 | opts->subsys_mask |= (1 << i); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1343 | one_ss = true; |
| 1344 | |
| 1345 | break; |
| 1346 | } |
| 1347 | if (i == CGROUP_SUBSYS_COUNT) |
| 1348 | return -ENOENT; |
| 1349 | } |
| 1350 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1351 | /* Consistency checks */ |
| 1352 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1353 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1354 | 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] | 1355 | |
Tejun Heo | d3ba07c | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1356 | if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) || |
| 1357 | opts->cpuset_clone_children || opts->release_agent || |
| 1358 | opts->name) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1359 | 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] | 1360 | return -EINVAL; |
| 1361 | } |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1362 | } else { |
| 1363 | /* |
| 1364 | * If the 'all' option was specified select all the |
| 1365 | * subsystems, otherwise if 'none', 'name=' and a subsystem |
| 1366 | * name options were not specified, let's default to 'all' |
| 1367 | */ |
| 1368 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1369 | for_each_subsys(ss, i) |
| 1370 | if (!ss->disabled) |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1371 | opts->subsys_mask |= (1 << i); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1372 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1373 | /* |
| 1374 | * We either have to specify by name or by subsystems. (So |
| 1375 | * all empty hierarchies must have a name). |
| 1376 | */ |
| 1377 | if (!opts->subsys_mask && !opts->name) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1378 | return -EINVAL; |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1381 | /* |
| 1382 | * Option noprefix was introduced just for backward compatibility |
| 1383 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1384 | * the cpuset subsystem. |
| 1385 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1386 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1387 | return -EINVAL; |
| 1388 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1389 | |
| 1390 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1391 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1392 | return -EINVAL; |
| 1393 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1394 | return 0; |
| 1395 | } |
| 1396 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1397 | 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] | 1398 | { |
| 1399 | int ret = 0; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1400 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1401 | struct cgroup_sb_opts opts; |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1402 | unsigned int added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1403 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1404 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1405 | pr_err("sane_behavior: remount is not allowed\n"); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1406 | return -EINVAL; |
| 1407 | } |
| 1408 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1409 | mutex_lock(&cgroup_mutex); |
| 1410 | |
| 1411 | /* See what subsystems are wanted */ |
| 1412 | ret = parse_cgroupfs_options(data, &opts); |
| 1413 | if (ret) |
| 1414 | goto out_unlock; |
| 1415 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1416 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1417 | 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] | 1418 | task_tgid_nr(current), current->comm); |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1419 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1420 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1421 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1422 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1423 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1424 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1425 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1426 | 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] | 1427 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1428 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1429 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1430 | goto out_unlock; |
| 1431 | } |
| 1432 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1433 | /* remounting is not allowed for populated hierarchies */ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1434 | if (!list_empty(&root->cgrp.self.children)) { |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1435 | ret = -EBUSY; |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1436 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1437 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1438 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1439 | ret = rebind_subsystems(root, added_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1440 | if (ret) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1441 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1442 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1443 | rebind_subsystems(&cgrp_dfl_root, removed_mask); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1444 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1445 | if (opts.release_agent) { |
| 1446 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1447 | strcpy(root->release_agent_path, opts.release_agent); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1448 | spin_unlock(&release_agent_path_lock); |
| 1449 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1450 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1451 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1452 | kfree(opts.name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1453 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1454 | return ret; |
| 1455 | } |
| 1456 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1457 | /* |
| 1458 | * To reduce the fork() overhead for systems that are not actually using |
| 1459 | * their cgroups capability, we don't maintain the lists running through |
| 1460 | * each css_set to its tasks until we see the list actually used - in other |
| 1461 | * words after the first mount. |
| 1462 | */ |
| 1463 | static bool use_task_css_set_links __read_mostly; |
| 1464 | |
| 1465 | static void cgroup_enable_task_cg_lists(void) |
| 1466 | { |
| 1467 | struct task_struct *p, *g; |
| 1468 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1469 | down_write(&css_set_rwsem); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1470 | |
| 1471 | if (use_task_css_set_links) |
| 1472 | goto out_unlock; |
| 1473 | |
| 1474 | use_task_css_set_links = true; |
| 1475 | |
| 1476 | /* |
| 1477 | * We need tasklist_lock because RCU is not safe against |
| 1478 | * while_each_thread(). Besides, a forking task that has passed |
| 1479 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 1480 | * is not guaranteed to have its child immediately visible in the |
| 1481 | * tasklist if we walk through it with RCU. |
| 1482 | */ |
| 1483 | read_lock(&tasklist_lock); |
| 1484 | do_each_thread(g, p) { |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1485 | WARN_ON_ONCE(!list_empty(&p->cg_list) || |
| 1486 | task_css_set(p) != &init_css_set); |
| 1487 | |
| 1488 | /* |
| 1489 | * We should check if the process is exiting, otherwise |
| 1490 | * it will race with cgroup_exit() in that the list |
| 1491 | * entry won't be deleted though the process has exited. |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1492 | * Do it while holding siglock so that we don't end up |
| 1493 | * racing against cgroup_exit(). |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1494 | */ |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1495 | spin_lock_irq(&p->sighand->siglock); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1496 | if (!(p->flags & PF_EXITING)) { |
| 1497 | struct css_set *cset = task_css_set(p); |
| 1498 | |
| 1499 | list_add(&p->cg_list, &cset->tasks); |
| 1500 | get_css_set(cset); |
| 1501 | } |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1502 | spin_unlock_irq(&p->sighand->siglock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1503 | } while_each_thread(g, p); |
| 1504 | read_unlock(&tasklist_lock); |
| 1505 | out_unlock: |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1506 | up_write(&css_set_rwsem); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1507 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1508 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1509 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1510 | { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1511 | struct cgroup_subsys *ss; |
| 1512 | int ssid; |
| 1513 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1514 | INIT_LIST_HEAD(&cgrp->self.sibling); |
| 1515 | INIT_LIST_HEAD(&cgrp->self.children); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1516 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1517 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1518 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1519 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 1520 | cgrp->self.cgroup = cgrp; |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame^] | 1521 | cgrp->self.flags |= CSS_ONLINE; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1522 | |
| 1523 | for_each_subsys(ss, ssid) |
| 1524 | INIT_LIST_HEAD(&cgrp->e_csets[ssid]); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1525 | |
| 1526 | init_waitqueue_head(&cgrp->offline_waitq); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1527 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1528 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1529 | static void init_cgroup_root(struct cgroup_root *root, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1530 | struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1531 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1532 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1533 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1534 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1535 | atomic_set(&root->nr_cgrps, 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1536 | cgrp->root = root; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1537 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1538 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1539 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1540 | root->flags = opts->flags; |
| 1541 | if (opts->release_agent) |
| 1542 | strcpy(root->release_agent_path, opts->release_agent); |
| 1543 | if (opts->name) |
| 1544 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1545 | if (opts->cpuset_clone_children) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1546 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1547 | } |
| 1548 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1549 | 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] | 1550 | { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1551 | LIST_HEAD(tmp_links); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1552 | struct cgroup *root_cgrp = &root->cgrp; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1553 | struct css_set *cset; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1554 | int i, ret; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1555 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1556 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1557 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 1558 | 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] | 1559 | if (ret < 0) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1560 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1561 | root_cgrp->id = ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1562 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1563 | ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release); |
| 1564 | if (ret) |
| 1565 | goto out; |
| 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 | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1575 | goto cancel_ref; |
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 | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1579 | goto cancel_ref; |
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 | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1615 | BUG_ON(!list_empty(&root_cgrp->self.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 | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1627 | cancel_ref: |
| 1628 | percpu_ref_cancel_init(&root_cgrp->self.refcnt); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1629 | out: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1630 | free_cgrp_cset_links(&tmp_links); |
| 1631 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1634 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1635 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1636 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1637 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1638 | struct cgroup_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1639 | struct cgroup_sb_opts opts; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1640 | struct dentry *dentry; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1641 | int ret; |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 1642 | bool new_sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1643 | |
| 1644 | /* |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1645 | * The first time anyone tries to mount a cgroup, enable the list |
| 1646 | * 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] | 1647 | */ |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1648 | if (!use_task_css_set_links) |
| 1649 | cgroup_enable_task_cg_lists(); |
Li Zefan | e37a06f | 2014-04-17 13:53:08 +0800 | [diff] [blame] | 1650 | |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1651 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1652 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1653 | /* First find the desired set of subsystems */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1654 | ret = parse_cgroupfs_options(data, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1655 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1656 | goto out_unlock; |
Tejun Heo | a015edd | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 1657 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1658 | /* look for a matching existing root */ |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1659 | if (!opts.subsys_mask && !opts.none && !opts.name) { |
| 1660 | cgrp_dfl_root_visible = true; |
| 1661 | root = &cgrp_dfl_root; |
| 1662 | cgroup_get(&root->cgrp); |
| 1663 | ret = 0; |
| 1664 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1667 | for_each_root(root) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1668 | bool name_match = false; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1669 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1670 | if (root == &cgrp_dfl_root) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1671 | continue; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1672 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1673 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1674 | * If we asked for a name then it must match. Also, if |
| 1675 | * name matches but sybsys_mask doesn't, we should fail. |
| 1676 | * Remember whether name matched. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1677 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1678 | if (opts.name) { |
| 1679 | if (strcmp(opts.name, root->name)) |
| 1680 | continue; |
| 1681 | name_match = true; |
| 1682 | } |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1683 | |
| 1684 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1685 | * If we asked for subsystems (or explicitly for no |
| 1686 | * subsystems) then they must match. |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1687 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1688 | if ((opts.subsys_mask || opts.none) && |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1689 | (opts.subsys_mask != root->subsys_mask)) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1690 | if (!name_match) |
| 1691 | continue; |
| 1692 | ret = -EBUSY; |
| 1693 | goto out_unlock; |
| 1694 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1695 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1696 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1697 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1698 | pr_err("sane_behavior: new mount options should match the existing superblock\n"); |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1699 | ret = -EINVAL; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1700 | goto out_unlock; |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1701 | } else { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 1702 | pr_warn("new mount options do not match the existing superblock, will be ignored\n"); |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1703 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1704 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1705 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1706 | /* |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1707 | * A root's lifetime is governed by its root cgroup. |
| 1708 | * tryget_live failure indicate that the root is being |
| 1709 | * destroyed. Wait for destruction to complete so that the |
| 1710 | * subsystems are free. We can use wait_queue for the wait |
| 1711 | * but this path is super cold. Let's just sleep for a bit |
| 1712 | * and retry. |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1713 | */ |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1714 | if (!percpu_ref_tryget_live(&root->cgrp.self.refcnt)) { |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1715 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1716 | msleep(10); |
Tejun Heo | a015edd | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 1717 | ret = restart_syscall(); |
| 1718 | goto out_free; |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 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 | a015edd | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 1749 | out_free: |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1750 | kfree(opts.release_agent); |
| 1751 | kfree(opts.name); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1752 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1753 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1754 | return ERR_PTR(ret); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1755 | |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 1756 | dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb); |
| 1757 | if (IS_ERR(dentry) || !new_sb) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1758 | cgroup_put(&root->cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1759 | return dentry; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 1762 | static void cgroup_kill_sb(struct super_block *sb) |
| 1763 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1764 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1765 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1766 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1767 | /* |
| 1768 | * If @root doesn't have any mounts or children, start killing it. |
| 1769 | * This prevents new mounts by disabling percpu_ref_tryget_live(). |
| 1770 | * cgroup_mount() may wait for @root's release. |
| 1771 | */ |
| 1772 | if (cgroup_has_live_children(&root->cgrp)) |
| 1773 | cgroup_put(&root->cgrp); |
| 1774 | else |
| 1775 | percpu_ref_kill(&root->cgrp.self.refcnt); |
| 1776 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1777 | kernfs_kill_sb(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | static struct file_system_type cgroup_fs_type = { |
| 1781 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1782 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1783 | .kill_sb = cgroup_kill_sb, |
| 1784 | }; |
| 1785 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1786 | static struct kobject *cgroup_kobj; |
| 1787 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1788 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1789 | * 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] | 1790 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1791 | * @buf: the buffer to write the path into |
| 1792 | * @buflen: the length of the buffer |
| 1793 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1794 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1795 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1796 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1797 | * cgroup controller callbacks. |
| 1798 | * |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1799 | * Return value is the same as kernfs_path(). |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1800 | */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1801 | 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] | 1802 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1803 | struct cgroup_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1804 | struct cgroup *cgrp; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1805 | int hierarchy_id = 1; |
| 1806 | char *path = NULL; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1807 | |
| 1808 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1809 | down_read(&css_set_rwsem); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1810 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1811 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1812 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1813 | if (root) { |
| 1814 | cgrp = task_cgroup_from_root(task, root); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1815 | path = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1816 | } else { |
| 1817 | /* if no hierarchy exists, everyone is in "/" */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1818 | if (strlcpy(buf, "/", buflen) < buflen) |
| 1819 | path = buf; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1822 | up_read(&css_set_rwsem); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1823 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1824 | return path; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1825 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1826 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1827 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1828 | /* used to track tasks and other necessary states during migration */ |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1829 | struct cgroup_taskset { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1830 | /* the src and dst cset list running through cset->mg_node */ |
| 1831 | struct list_head src_csets; |
| 1832 | struct list_head dst_csets; |
| 1833 | |
| 1834 | /* |
| 1835 | * Fields for cgroup_taskset_*() iteration. |
| 1836 | * |
| 1837 | * Before migration is committed, the target migration tasks are on |
| 1838 | * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of |
| 1839 | * the csets on ->dst_csets. ->csets point to either ->src_csets |
| 1840 | * or ->dst_csets depending on whether migration is committed. |
| 1841 | * |
| 1842 | * ->cur_csets and ->cur_task point to the current task position |
| 1843 | * during iteration. |
| 1844 | */ |
| 1845 | struct list_head *csets; |
| 1846 | struct css_set *cur_cset; |
| 1847 | struct task_struct *cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1848 | }; |
| 1849 | |
| 1850 | /** |
| 1851 | * cgroup_taskset_first - reset taskset and return the first task |
| 1852 | * @tset: taskset of interest |
| 1853 | * |
| 1854 | * @tset iteration is initialized and the first task is returned. |
| 1855 | */ |
| 1856 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1857 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1858 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); |
| 1859 | tset->cur_task = NULL; |
| 1860 | |
| 1861 | return cgroup_taskset_next(tset); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1862 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1863 | |
| 1864 | /** |
| 1865 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1866 | * @tset: taskset of interest |
| 1867 | * |
| 1868 | * Return the next task in @tset. Iteration must have been initialized |
| 1869 | * with cgroup_taskset_first(). |
| 1870 | */ |
| 1871 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1872 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1873 | struct css_set *cset = tset->cur_cset; |
| 1874 | struct task_struct *task = tset->cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1875 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1876 | while (&cset->mg_node != tset->csets) { |
| 1877 | if (!task) |
| 1878 | task = list_first_entry(&cset->mg_tasks, |
| 1879 | struct task_struct, cg_list); |
| 1880 | else |
| 1881 | task = list_next_entry(task, cg_list); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1882 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1883 | if (&task->cg_list != &cset->mg_tasks) { |
| 1884 | tset->cur_cset = cset; |
| 1885 | tset->cur_task = task; |
| 1886 | return task; |
| 1887 | } |
| 1888 | |
| 1889 | cset = list_next_entry(cset, mg_node); |
| 1890 | task = NULL; |
| 1891 | } |
| 1892 | |
| 1893 | return NULL; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1894 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1895 | |
| 1896 | /** |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1897 | * cgroup_task_migrate - move a task from one cgroup to another. |
Fabian Frederick | 6010694 | 2014-05-05 20:08:13 +0200 | [diff] [blame] | 1898 | * @old_cgrp: the cgroup @tsk is being migrated from |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1899 | * @tsk: the task being migrated |
| 1900 | * @new_cset: the new css_set @tsk is being attached to |
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 | * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1903 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1904 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1905 | struct task_struct *tsk, |
| 1906 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1907 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1908 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1909 | |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1910 | lockdep_assert_held(&cgroup_mutex); |
| 1911 | lockdep_assert_held(&css_set_rwsem); |
| 1912 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1913 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1914 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1915 | * setting such that we can't race against cgroup_exit() changing the |
| 1916 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1917 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1918 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1919 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1920 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1921 | get_css_set(new_cset); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1922 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1923 | |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 1924 | /* |
| 1925 | * Use move_tail so that cgroup_taskset_first() still returns the |
| 1926 | * leader after migration. This works because cgroup_migrate() |
| 1927 | * ensures that the dst_cset of the leader is the first on the |
| 1928 | * tset's dst_csets list. |
| 1929 | */ |
| 1930 | list_move_tail(&tsk->cg_list, &new_cset->mg_tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1931 | |
| 1932 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1933 | * We just gained a reference on old_cset by taking it from the |
| 1934 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1935 | * we're safe to drop it here; it will be freed under RCU. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1936 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1937 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1938 | put_css_set_locked(old_cset, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1939 | } |
| 1940 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1941 | /** |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1942 | * cgroup_migrate_finish - cleanup after attach |
| 1943 | * @preloaded_csets: list of preloaded css_sets |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1944 | * |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1945 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See |
| 1946 | * those functions for details. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1947 | */ |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1948 | static void cgroup_migrate_finish(struct list_head *preloaded_csets) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1949 | { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1950 | struct css_set *cset, *tmp_cset; |
| 1951 | |
| 1952 | lockdep_assert_held(&cgroup_mutex); |
| 1953 | |
| 1954 | down_write(&css_set_rwsem); |
| 1955 | list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) { |
| 1956 | cset->mg_src_cgrp = NULL; |
| 1957 | cset->mg_dst_cset = NULL; |
| 1958 | list_del_init(&cset->mg_preload_node); |
| 1959 | put_css_set_locked(cset, false); |
| 1960 | } |
| 1961 | up_write(&css_set_rwsem); |
| 1962 | } |
| 1963 | |
| 1964 | /** |
| 1965 | * cgroup_migrate_add_src - add a migration source css_set |
| 1966 | * @src_cset: the source css_set to add |
| 1967 | * @dst_cgrp: the destination cgroup |
| 1968 | * @preloaded_csets: list of preloaded css_sets |
| 1969 | * |
| 1970 | * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin |
| 1971 | * @src_cset and add it to @preloaded_csets, which should later be cleaned |
| 1972 | * up by cgroup_migrate_finish(). |
| 1973 | * |
| 1974 | * This function may be called without holding threadgroup_lock even if the |
| 1975 | * target is a process. Threads may be created and destroyed but as long |
| 1976 | * as cgroup_mutex is not dropped, no new css_set can be put into play and |
| 1977 | * the preloaded css_sets are guaranteed to cover all migrations. |
| 1978 | */ |
| 1979 | static void cgroup_migrate_add_src(struct css_set *src_cset, |
| 1980 | struct cgroup *dst_cgrp, |
| 1981 | struct list_head *preloaded_csets) |
| 1982 | { |
| 1983 | struct cgroup *src_cgrp; |
| 1984 | |
| 1985 | lockdep_assert_held(&cgroup_mutex); |
| 1986 | lockdep_assert_held(&css_set_rwsem); |
| 1987 | |
| 1988 | src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); |
| 1989 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1990 | if (!list_empty(&src_cset->mg_preload_node)) |
| 1991 | return; |
| 1992 | |
| 1993 | WARN_ON(src_cset->mg_src_cgrp); |
| 1994 | WARN_ON(!list_empty(&src_cset->mg_tasks)); |
| 1995 | WARN_ON(!list_empty(&src_cset->mg_node)); |
| 1996 | |
| 1997 | src_cset->mg_src_cgrp = src_cgrp; |
| 1998 | get_css_set(src_cset); |
| 1999 | list_add(&src_cset->mg_preload_node, preloaded_csets); |
| 2000 | } |
| 2001 | |
| 2002 | /** |
| 2003 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2004 | * @dst_cgrp: the destination cgroup (may be %NULL) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2005 | * @preloaded_csets: list of preloaded source css_sets |
| 2006 | * |
| 2007 | * Tasks are about to be moved to @dst_cgrp and all the source css_sets |
| 2008 | * have been preloaded to @preloaded_csets. This function looks up and |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2009 | * pins all destination css_sets, links each to its source, and append them |
| 2010 | * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each |
| 2011 | * 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] | 2012 | * |
| 2013 | * This function must be called after cgroup_migrate_add_src() has been |
| 2014 | * called on each migration source css_set. After migration is performed |
| 2015 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on |
| 2016 | * @preloaded_csets. |
| 2017 | */ |
| 2018 | static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp, |
| 2019 | struct list_head *preloaded_csets) |
| 2020 | { |
| 2021 | LIST_HEAD(csets); |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2022 | struct css_set *src_cset, *tmp_cset; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2023 | |
| 2024 | lockdep_assert_held(&cgroup_mutex); |
| 2025 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2026 | /* |
| 2027 | * Except for the root, child_subsys_mask must be zero for a cgroup |
| 2028 | * with tasks so that child cgroups don't compete against tasks. |
| 2029 | */ |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 2030 | if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) && |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2031 | dst_cgrp->child_subsys_mask) |
| 2032 | return -EBUSY; |
| 2033 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2034 | /* 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] | 2035 | 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] | 2036 | struct css_set *dst_cset; |
| 2037 | |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2038 | dst_cset = find_css_set(src_cset, |
| 2039 | dst_cgrp ?: src_cset->dfl_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2040 | if (!dst_cset) |
| 2041 | goto err; |
| 2042 | |
| 2043 | 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] | 2044 | |
| 2045 | /* |
| 2046 | * If src cset equals dst, it's noop. Drop the src. |
| 2047 | * cgroup_migrate() will skip the cset too. Note that we |
| 2048 | * can't handle src == dst as some nodes are used by both. |
| 2049 | */ |
| 2050 | if (src_cset == dst_cset) { |
| 2051 | src_cset->mg_src_cgrp = NULL; |
| 2052 | list_del_init(&src_cset->mg_preload_node); |
| 2053 | put_css_set(src_cset, false); |
| 2054 | put_css_set(dst_cset, false); |
| 2055 | continue; |
| 2056 | } |
| 2057 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2058 | src_cset->mg_dst_cset = dst_cset; |
| 2059 | |
| 2060 | if (list_empty(&dst_cset->mg_preload_node)) |
| 2061 | list_add(&dst_cset->mg_preload_node, &csets); |
| 2062 | else |
| 2063 | put_css_set(dst_cset, false); |
| 2064 | } |
| 2065 | |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2066 | list_splice_tail(&csets, preloaded_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2067 | return 0; |
| 2068 | err: |
| 2069 | cgroup_migrate_finish(&csets); |
| 2070 | return -ENOMEM; |
| 2071 | } |
| 2072 | |
| 2073 | /** |
| 2074 | * cgroup_migrate - migrate a process or task to a cgroup |
| 2075 | * @cgrp: the destination cgroup |
| 2076 | * @leader: the leader of the process or the task to migrate |
| 2077 | * @threadgroup: whether @leader points to the whole process or a single task |
| 2078 | * |
| 2079 | * Migrate a process or task denoted by @leader to @cgrp. If migrating a |
| 2080 | * process, the caller must be holding threadgroup_lock of @leader. The |
| 2081 | * caller is also responsible for invoking cgroup_migrate_add_src() and |
| 2082 | * cgroup_migrate_prepare_dst() on the targets before invoking this |
| 2083 | * function and following up with cgroup_migrate_finish(). |
| 2084 | * |
| 2085 | * As long as a controller's ->can_attach() doesn't fail, this function is |
| 2086 | * guaranteed to succeed. This means that, excluding ->can_attach() |
| 2087 | * failure, when migrating multiple targets, the success or failure can be |
| 2088 | * decided for all targets by invoking group_migrate_prepare_dst() before |
| 2089 | * actually starting migrating. |
| 2090 | */ |
| 2091 | static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader, |
| 2092 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2093 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2094 | struct cgroup_taskset tset = { |
| 2095 | .src_csets = LIST_HEAD_INIT(tset.src_csets), |
| 2096 | .dst_csets = LIST_HEAD_INIT(tset.dst_csets), |
| 2097 | .csets = &tset.src_csets, |
| 2098 | }; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2099 | struct cgroup_subsys_state *css, *failed_css = NULL; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2100 | struct css_set *cset, *tmp_cset; |
| 2101 | struct task_struct *task, *tmp_task; |
| 2102 | int i, ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2103 | |
| 2104 | /* |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2105 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2106 | * already PF_EXITING could be freed from underneath us unless we |
| 2107 | * take an rcu_read_lock. |
| 2108 | */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2109 | down_write(&css_set_rwsem); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2110 | rcu_read_lock(); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2111 | task = leader; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2112 | do { |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2113 | /* @task either already exited or can't exit until the end */ |
| 2114 | if (task->flags & PF_EXITING) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2115 | goto next; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2116 | |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2117 | /* leave @task alone if post_fork() hasn't linked it yet */ |
| 2118 | if (list_empty(&task->cg_list)) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2119 | goto next; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2120 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2121 | cset = task_css_set(task); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2122 | if (!cset->mg_src_cgrp) |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2123 | goto next; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2124 | |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2125 | /* |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 2126 | * cgroup_taskset_first() must always return the leader. |
| 2127 | * Take care to avoid disturbing the ordering. |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2128 | */ |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 2129 | list_move_tail(&task->cg_list, &cset->mg_tasks); |
| 2130 | if (list_empty(&cset->mg_node)) |
| 2131 | list_add_tail(&cset->mg_node, &tset.src_csets); |
| 2132 | if (list_empty(&cset->mg_dst_cset->mg_node)) |
| 2133 | list_move_tail(&cset->mg_dst_cset->mg_node, |
| 2134 | &tset.dst_csets); |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 2135 | next: |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2136 | if (!threadgroup) |
| 2137 | break; |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2138 | } while_each_thread(leader, task); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2139 | rcu_read_unlock(); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2140 | up_write(&css_set_rwsem); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2141 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2142 | /* methods shouldn't be called if no task is actually migrating */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2143 | if (list_empty(&tset.src_csets)) |
| 2144 | return 0; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2145 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2146 | /* check that we can legitimately attach to the cgroup */ |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 2147 | for_each_e_css(css, i, cgrp) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2148 | if (css->ss->can_attach) { |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2149 | ret = css->ss->can_attach(css, &tset); |
| 2150 | if (ret) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2151 | failed_css = css; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2152 | goto out_cancel_attach; |
| 2153 | } |
| 2154 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | /* |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2158 | * Now that we're guaranteed success, proceed to move all tasks to |
| 2159 | * the new cgroup. There are no failure cases after here, so this |
| 2160 | * is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2161 | */ |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 2162 | down_write(&css_set_rwsem); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2163 | list_for_each_entry(cset, &tset.src_csets, mg_node) { |
| 2164 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) |
| 2165 | cgroup_task_migrate(cset->mg_src_cgrp, task, |
| 2166 | cset->mg_dst_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2167 | } |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 2168 | up_write(&css_set_rwsem); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2169 | |
| 2170 | /* |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2171 | * Migration is committed, all target tasks are now on dst_csets. |
| 2172 | * Nothing is sensitive to fork() after this point. Notify |
| 2173 | * controllers that migration is complete. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2174 | */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2175 | tset.csets = &tset.dst_csets; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2176 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 2177 | for_each_e_css(css, i, cgrp) |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 2178 | if (css->ss->attach) |
| 2179 | css->ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2180 | |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2181 | ret = 0; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2182 | goto out_release_tset; |
| 2183 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2184 | out_cancel_attach: |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 2185 | for_each_e_css(css, i, cgrp) { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2186 | if (css == failed_css) |
| 2187 | break; |
| 2188 | if (css->ss->cancel_attach) |
| 2189 | css->ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2190 | } |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2191 | out_release_tset: |
| 2192 | down_write(&css_set_rwsem); |
| 2193 | list_splice_init(&tset.dst_csets, &tset.src_csets); |
| 2194 | 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] | 2195 | list_splice_tail_init(&cset->mg_tasks, &cset->tasks); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2196 | list_del_init(&cset->mg_node); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2197 | } |
| 2198 | up_write(&css_set_rwsem); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2199 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2200 | } |
| 2201 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2202 | /** |
| 2203 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
| 2204 | * @dst_cgrp: the cgroup to attach to |
| 2205 | * @leader: the task or the leader of the threadgroup to be attached |
| 2206 | * @threadgroup: attach the whole threadgroup? |
| 2207 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2208 | * Call holding cgroup_mutex and threadgroup_lock of @leader. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2209 | */ |
| 2210 | static int cgroup_attach_task(struct cgroup *dst_cgrp, |
| 2211 | struct task_struct *leader, bool threadgroup) |
| 2212 | { |
| 2213 | LIST_HEAD(preloaded_csets); |
| 2214 | struct task_struct *task; |
| 2215 | int ret; |
| 2216 | |
| 2217 | /* look up all src csets */ |
| 2218 | down_read(&css_set_rwsem); |
| 2219 | rcu_read_lock(); |
| 2220 | task = leader; |
| 2221 | do { |
| 2222 | cgroup_migrate_add_src(task_css_set(task), dst_cgrp, |
| 2223 | &preloaded_csets); |
| 2224 | if (!threadgroup) |
| 2225 | break; |
| 2226 | } while_each_thread(leader, task); |
| 2227 | rcu_read_unlock(); |
| 2228 | up_read(&css_set_rwsem); |
| 2229 | |
| 2230 | /* prepare dst csets and commit */ |
| 2231 | ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets); |
| 2232 | if (!ret) |
| 2233 | ret = cgroup_migrate(dst_cgrp, leader, threadgroup); |
| 2234 | |
| 2235 | cgroup_migrate_finish(&preloaded_csets); |
| 2236 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | /* |
| 2240 | * 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] | 2241 | * 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] | 2242 | * cgroup_mutex and threadgroup. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2243 | */ |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2244 | static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf, |
| 2245 | size_t nbytes, loff_t off, bool threadgroup) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2246 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2247 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2248 | const struct cred *cred = current_cred(), *tcred; |
Tejun Heo | e76ecae | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 2249 | struct cgroup *cgrp; |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2250 | pid_t pid; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2251 | int ret; |
| 2252 | |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2253 | if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) |
| 2254 | return -EINVAL; |
| 2255 | |
Tejun Heo | e76ecae | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 2256 | cgrp = cgroup_kn_lock_live(of->kn); |
| 2257 | if (!cgrp) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2258 | return -ENODEV; |
| 2259 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2260 | retry_find_task: |
| 2261 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2262 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2263 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2264 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2265 | rcu_read_unlock(); |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2266 | ret = -ESRCH; |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2267 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2268 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2269 | /* |
| 2270 | * even if we're attaching all tasks in the thread group, we |
| 2271 | * only need to check permissions on one of them. |
| 2272 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2273 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2274 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2275 | !uid_eq(cred->euid, tcred->uid) && |
| 2276 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2277 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2278 | ret = -EACCES; |
| 2279 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2280 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2281 | } else |
| 2282 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2283 | |
| 2284 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2285 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2286 | |
| 2287 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2288 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2289 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2290 | * with no rt_runtime allocated. Just say no. |
| 2291 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2292 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2293 | ret = -EINVAL; |
| 2294 | rcu_read_unlock(); |
| 2295 | goto out_unlock_cgroup; |
| 2296 | } |
| 2297 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2298 | get_task_struct(tsk); |
| 2299 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2300 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2301 | threadgroup_lock(tsk); |
| 2302 | if (threadgroup) { |
| 2303 | if (!thread_group_leader(tsk)) { |
| 2304 | /* |
| 2305 | * a race with de_thread from another thread's exec() |
| 2306 | * may strip us of our leadership, if this happens, |
| 2307 | * there is no choice but to throw this task away and |
| 2308 | * try again; this is |
| 2309 | * "double-double-toil-and-trouble-check locking". |
| 2310 | */ |
| 2311 | threadgroup_unlock(tsk); |
| 2312 | put_task_struct(tsk); |
| 2313 | goto retry_find_task; |
| 2314 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2318 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2319 | threadgroup_unlock(tsk); |
| 2320 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2321 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2322 | out_unlock_cgroup: |
Tejun Heo | e76ecae | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 2323 | cgroup_kn_unlock(of->kn); |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2324 | return ret ?: nbytes; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2325 | } |
| 2326 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2327 | /** |
| 2328 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2329 | * @from: attach to all cgroups of a given task |
| 2330 | * @tsk: the task to be attached |
| 2331 | */ |
| 2332 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2333 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2334 | struct cgroup_root *root; |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2335 | int retval = 0; |
| 2336 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2337 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 2338 | for_each_root(root) { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2339 | struct cgroup *from_cgrp; |
| 2340 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2341 | if (root == &cgrp_dfl_root) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 2342 | continue; |
| 2343 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2344 | down_read(&css_set_rwsem); |
| 2345 | from_cgrp = task_cgroup_from_root(from, root); |
| 2346 | up_read(&css_set_rwsem); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2347 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2348 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2349 | if (retval) |
| 2350 | break; |
| 2351 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2352 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2353 | |
| 2354 | return retval; |
| 2355 | } |
| 2356 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2357 | |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2358 | static ssize_t cgroup_tasks_write(struct kernfs_open_file *of, |
| 2359 | char *buf, size_t nbytes, loff_t off) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2360 | { |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2361 | return __cgroup_procs_write(of, buf, nbytes, off, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2362 | } |
| 2363 | |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2364 | static ssize_t cgroup_procs_write(struct kernfs_open_file *of, |
| 2365 | char *buf, size_t nbytes, loff_t off) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2366 | { |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2367 | return __cgroup_procs_write(of, buf, nbytes, off, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2368 | } |
| 2369 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 2370 | static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of, |
| 2371 | char *buf, size_t nbytes, loff_t off) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2372 | { |
Tejun Heo | e76ecae | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 2373 | struct cgroup *cgrp; |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2374 | |
Tejun Heo | e76ecae | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 2375 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
| 2376 | |
| 2377 | cgrp = cgroup_kn_lock_live(of->kn); |
| 2378 | if (!cgrp) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2379 | return -ENODEV; |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2380 | spin_lock(&release_agent_path_lock); |
Tejun Heo | e76ecae | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 2381 | strlcpy(cgrp->root->release_agent_path, strstrip(buf), |
| 2382 | sizeof(cgrp->root->release_agent_path)); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2383 | spin_unlock(&release_agent_path_lock); |
Tejun Heo | e76ecae | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 2384 | cgroup_kn_unlock(of->kn); |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 2385 | return nbytes; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2386 | } |
| 2387 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2388 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2389 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2390 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2391 | |
Tejun Heo | 46cfeb0 | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2392 | spin_lock(&release_agent_path_lock); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2393 | seq_puts(seq, cgrp->root->release_agent_path); |
Tejun Heo | 46cfeb0 | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2394 | spin_unlock(&release_agent_path_lock); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2395 | seq_putc(seq, '\n'); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2396 | return 0; |
| 2397 | } |
| 2398 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2399 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2400 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2401 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2402 | |
| 2403 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2404 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2405 | } |
| 2406 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2407 | static void cgroup_print_ss_mask(struct seq_file *seq, unsigned int ss_mask) |
| 2408 | { |
| 2409 | struct cgroup_subsys *ss; |
| 2410 | bool printed = false; |
| 2411 | int ssid; |
| 2412 | |
| 2413 | for_each_subsys(ss, ssid) { |
| 2414 | if (ss_mask & (1 << ssid)) { |
| 2415 | if (printed) |
| 2416 | seq_putc(seq, ' '); |
| 2417 | seq_printf(seq, "%s", ss->name); |
| 2418 | printed = true; |
| 2419 | } |
| 2420 | } |
| 2421 | if (printed) |
| 2422 | seq_putc(seq, '\n'); |
| 2423 | } |
| 2424 | |
| 2425 | /* show controllers which are currently attached to the default hierarchy */ |
| 2426 | static int cgroup_root_controllers_show(struct seq_file *seq, void *v) |
| 2427 | { |
| 2428 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2429 | |
| 2430 | cgroup_print_ss_mask(seq, cgrp->root->subsys_mask); |
| 2431 | return 0; |
| 2432 | } |
| 2433 | |
| 2434 | /* show controllers which are enabled from the parent */ |
| 2435 | static int cgroup_controllers_show(struct seq_file *seq, void *v) |
| 2436 | { |
| 2437 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2438 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 2439 | cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->child_subsys_mask); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2440 | return 0; |
| 2441 | } |
| 2442 | |
| 2443 | /* show controllers which are enabled for a given cgroup's children */ |
| 2444 | static int cgroup_subtree_control_show(struct seq_file *seq, void *v) |
| 2445 | { |
| 2446 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2447 | |
| 2448 | cgroup_print_ss_mask(seq, cgrp->child_subsys_mask); |
| 2449 | return 0; |
| 2450 | } |
| 2451 | |
| 2452 | /** |
| 2453 | * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy |
| 2454 | * @cgrp: root of the subtree to update csses for |
| 2455 | * |
| 2456 | * @cgrp's child_subsys_mask has changed and its subtree's (self excluded) |
| 2457 | * css associations need to be updated accordingly. This function looks up |
| 2458 | * all css_sets which are attached to the subtree, creates the matching |
| 2459 | * updated css_sets and migrates the tasks to the new ones. |
| 2460 | */ |
| 2461 | static int cgroup_update_dfl_csses(struct cgroup *cgrp) |
| 2462 | { |
| 2463 | LIST_HEAD(preloaded_csets); |
| 2464 | struct cgroup_subsys_state *css; |
| 2465 | struct css_set *src_cset; |
| 2466 | int ret; |
| 2467 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2468 | lockdep_assert_held(&cgroup_mutex); |
| 2469 | |
| 2470 | /* look up all csses currently attached to @cgrp's subtree */ |
| 2471 | down_read(&css_set_rwsem); |
| 2472 | css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) { |
| 2473 | struct cgrp_cset_link *link; |
| 2474 | |
| 2475 | /* self is not affected by child_subsys_mask change */ |
| 2476 | if (css->cgroup == cgrp) |
| 2477 | continue; |
| 2478 | |
| 2479 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) |
| 2480 | cgroup_migrate_add_src(link->cset, cgrp, |
| 2481 | &preloaded_csets); |
| 2482 | } |
| 2483 | up_read(&css_set_rwsem); |
| 2484 | |
| 2485 | /* NULL dst indicates self on default hierarchy */ |
| 2486 | ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets); |
| 2487 | if (ret) |
| 2488 | goto out_finish; |
| 2489 | |
| 2490 | list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) { |
| 2491 | struct task_struct *last_task = NULL, *task; |
| 2492 | |
| 2493 | /* src_csets precede dst_csets, break on the first dst_cset */ |
| 2494 | if (!src_cset->mg_src_cgrp) |
| 2495 | break; |
| 2496 | |
| 2497 | /* |
| 2498 | * All tasks in src_cset need to be migrated to the |
| 2499 | * matching dst_cset. Empty it process by process. We |
| 2500 | * walk tasks but migrate processes. The leader might even |
| 2501 | * belong to a different cset but such src_cset would also |
| 2502 | * be among the target src_csets because the default |
| 2503 | * hierarchy enforces per-process membership. |
| 2504 | */ |
| 2505 | while (true) { |
| 2506 | down_read(&css_set_rwsem); |
| 2507 | task = list_first_entry_or_null(&src_cset->tasks, |
| 2508 | struct task_struct, cg_list); |
| 2509 | if (task) { |
| 2510 | task = task->group_leader; |
| 2511 | WARN_ON_ONCE(!task_css_set(task)->mg_src_cgrp); |
| 2512 | get_task_struct(task); |
| 2513 | } |
| 2514 | up_read(&css_set_rwsem); |
| 2515 | |
| 2516 | if (!task) |
| 2517 | break; |
| 2518 | |
| 2519 | /* guard against possible infinite loop */ |
| 2520 | if (WARN(last_task == task, |
| 2521 | "cgroup: update_dfl_csses failed to make progress, aborting in inconsistent state\n")) |
| 2522 | goto out_finish; |
| 2523 | last_task = task; |
| 2524 | |
| 2525 | threadgroup_lock(task); |
| 2526 | /* raced against de_thread() from another thread? */ |
| 2527 | if (!thread_group_leader(task)) { |
| 2528 | threadgroup_unlock(task); |
| 2529 | put_task_struct(task); |
| 2530 | continue; |
| 2531 | } |
| 2532 | |
| 2533 | ret = cgroup_migrate(src_cset->dfl_cgrp, task, true); |
| 2534 | |
| 2535 | threadgroup_unlock(task); |
| 2536 | put_task_struct(task); |
| 2537 | |
| 2538 | if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret)) |
| 2539 | goto out_finish; |
| 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | out_finish: |
| 2544 | cgroup_migrate_finish(&preloaded_csets); |
| 2545 | return ret; |
| 2546 | } |
| 2547 | |
| 2548 | /* change the enabled child controllers for a cgroup in the default hierarchy */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 2549 | static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, |
| 2550 | char *buf, size_t nbytes, |
| 2551 | loff_t off) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2552 | { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2553 | unsigned int enable = 0, disable = 0; |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 2554 | struct cgroup *cgrp, *child; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2555 | struct cgroup_subsys *ss; |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 2556 | char *tok; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2557 | int ssid, ret; |
| 2558 | |
| 2559 | /* |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2560 | * Parse input - space separated list of subsystem names prefixed |
| 2561 | * with either + or -. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2562 | */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 2563 | buf = strstrip(buf); |
| 2564 | while ((tok = strsep(&buf, " "))) { |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2565 | if (tok[0] == '\0') |
| 2566 | continue; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2567 | for_each_subsys(ss, ssid) { |
| 2568 | if (ss->disabled || strcmp(tok + 1, ss->name)) |
| 2569 | continue; |
| 2570 | |
| 2571 | if (*tok == '+') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2572 | enable |= 1 << ssid; |
| 2573 | disable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2574 | } else if (*tok == '-') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2575 | disable |= 1 << ssid; |
| 2576 | enable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2577 | } else { |
| 2578 | return -EINVAL; |
| 2579 | } |
| 2580 | break; |
| 2581 | } |
| 2582 | if (ssid == CGROUP_SUBSYS_COUNT) |
| 2583 | return -EINVAL; |
| 2584 | } |
| 2585 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 2586 | cgrp = cgroup_kn_lock_live(of->kn); |
| 2587 | if (!cgrp) |
| 2588 | return -ENODEV; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2589 | |
| 2590 | for_each_subsys(ss, ssid) { |
| 2591 | if (enable & (1 << ssid)) { |
| 2592 | if (cgrp->child_subsys_mask & (1 << ssid)) { |
| 2593 | enable &= ~(1 << ssid); |
| 2594 | continue; |
| 2595 | } |
| 2596 | |
| 2597 | /* |
| 2598 | * Because css offlining is asynchronous, userland |
| 2599 | * might try to re-enable the same controller while |
| 2600 | * the previous instance is still around. In such |
| 2601 | * cases, wait till it's gone using offline_waitq. |
| 2602 | */ |
| 2603 | cgroup_for_each_live_child(child, cgrp) { |
Tejun Heo | 0cee8b7 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2604 | DEFINE_WAIT(wait); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2605 | |
| 2606 | if (!cgroup_css(child, ss)) |
| 2607 | continue; |
| 2608 | |
Tejun Heo | 0cee8b7 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2609 | cgroup_get(child); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2610 | prepare_to_wait(&child->offline_waitq, &wait, |
| 2611 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 2612 | cgroup_kn_unlock(of->kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2613 | schedule(); |
| 2614 | finish_wait(&child->offline_waitq, &wait); |
Tejun Heo | 0cee8b7 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 2615 | cgroup_put(child); |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 2616 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 2617 | return restart_syscall(); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | /* unavailable or not enabled on the parent? */ |
| 2621 | if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) || |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 2622 | (cgroup_parent(cgrp) && |
| 2623 | !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ssid)))) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2624 | ret = -ENOENT; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 2625 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2626 | } |
| 2627 | } else if (disable & (1 << ssid)) { |
| 2628 | if (!(cgrp->child_subsys_mask & (1 << ssid))) { |
| 2629 | disable &= ~(1 << ssid); |
| 2630 | continue; |
| 2631 | } |
| 2632 | |
| 2633 | /* a child has it enabled? */ |
| 2634 | cgroup_for_each_live_child(child, cgrp) { |
| 2635 | if (child->child_subsys_mask & (1 << ssid)) { |
| 2636 | ret = -EBUSY; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 2637 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2638 | } |
| 2639 | } |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | if (!enable && !disable) { |
| 2644 | ret = 0; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 2645 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2646 | } |
| 2647 | |
| 2648 | /* |
| 2649 | * Except for the root, child_subsys_mask must be zero for a cgroup |
| 2650 | * with tasks so that child cgroups don't compete against tasks. |
| 2651 | */ |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 2652 | if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2653 | ret = -EBUSY; |
| 2654 | goto out_unlock; |
| 2655 | } |
| 2656 | |
| 2657 | /* |
| 2658 | * Create csses for enables and update child_subsys_mask. This |
| 2659 | * changes cgroup_e_css() results which in turn makes the |
| 2660 | * subsequent cgroup_update_dfl_csses() associate all tasks in the |
| 2661 | * subtree to the updated csses. |
| 2662 | */ |
| 2663 | for_each_subsys(ss, ssid) { |
| 2664 | if (!(enable & (1 << ssid))) |
| 2665 | continue; |
| 2666 | |
| 2667 | cgroup_for_each_live_child(child, cgrp) { |
| 2668 | ret = create_css(child, ss); |
| 2669 | if (ret) |
| 2670 | goto err_undo_css; |
| 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | cgrp->child_subsys_mask |= enable; |
| 2675 | cgrp->child_subsys_mask &= ~disable; |
| 2676 | |
| 2677 | ret = cgroup_update_dfl_csses(cgrp); |
| 2678 | if (ret) |
| 2679 | goto err_undo_css; |
| 2680 | |
| 2681 | /* all tasks are now migrated away from the old csses, kill them */ |
| 2682 | for_each_subsys(ss, ssid) { |
| 2683 | if (!(disable & (1 << ssid))) |
| 2684 | continue; |
| 2685 | |
| 2686 | cgroup_for_each_live_child(child, cgrp) |
| 2687 | kill_css(cgroup_css(child, ss)); |
| 2688 | } |
| 2689 | |
| 2690 | kernfs_activate(cgrp->kn); |
| 2691 | ret = 0; |
| 2692 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 2693 | cgroup_kn_unlock(of->kn); |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 2694 | return ret ?: nbytes; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2695 | |
| 2696 | err_undo_css: |
| 2697 | cgrp->child_subsys_mask &= ~enable; |
| 2698 | cgrp->child_subsys_mask |= disable; |
| 2699 | |
| 2700 | for_each_subsys(ss, ssid) { |
| 2701 | if (!(enable & (1 << ssid))) |
| 2702 | continue; |
| 2703 | |
| 2704 | cgroup_for_each_live_child(child, cgrp) { |
| 2705 | struct cgroup_subsys_state *css = cgroup_css(child, ss); |
| 2706 | if (css) |
| 2707 | kill_css(css); |
| 2708 | } |
| 2709 | } |
| 2710 | goto out_unlock; |
| 2711 | } |
| 2712 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 2713 | static int cgroup_populated_show(struct seq_file *seq, void *v) |
| 2714 | { |
| 2715 | seq_printf(seq, "%d\n", (bool)seq_css(seq)->cgroup->populated_cnt); |
| 2716 | return 0; |
| 2717 | } |
| 2718 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2719 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 2720 | size_t nbytes, loff_t off) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2721 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2722 | struct cgroup *cgrp = of->kn->parent->priv; |
| 2723 | struct cftype *cft = of->kn->priv; |
| 2724 | struct cgroup_subsys_state *css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2725 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2726 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 2727 | if (cft->write) |
| 2728 | return cft->write(of, buf, nbytes, off); |
| 2729 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2730 | /* |
| 2731 | * kernfs guarantees that a file isn't deleted with operations in |
| 2732 | * flight, which means that the matching css is and stays alive and |
| 2733 | * doesn't need to be pinned. The RCU locking is not necessary |
| 2734 | * either. It's just for the convenience of using cgroup_css(). |
| 2735 | */ |
| 2736 | rcu_read_lock(); |
| 2737 | css = cgroup_css(cgrp, cft->ss); |
| 2738 | rcu_read_unlock(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2739 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 2740 | if (cft->write_u64) { |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2741 | unsigned long long v; |
| 2742 | ret = kstrtoull(buf, 0, &v); |
| 2743 | if (!ret) |
| 2744 | ret = cft->write_u64(css, cft, v); |
| 2745 | } else if (cft->write_s64) { |
| 2746 | long long v; |
| 2747 | ret = kstrtoll(buf, 0, &v); |
| 2748 | if (!ret) |
| 2749 | ret = cft->write_s64(css, cft, v); |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2750 | } else { |
| 2751 | ret = -EINVAL; |
| 2752 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2753 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2754 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2755 | } |
| 2756 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2757 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2758 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2759 | return seq_cft(seq)->seq_start(seq, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 2763 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2764 | return seq_cft(seq)->seq_next(seq, v, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 2768 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2769 | seq_cft(seq)->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2773 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2774 | struct cftype *cft = seq_cft(m); |
| 2775 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2776 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2777 | if (cft->seq_show) |
| 2778 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2779 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2780 | if (cft->read_u64) |
| 2781 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 2782 | else if (cft->read_s64) |
| 2783 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 2784 | else |
| 2785 | return -EINVAL; |
| 2786 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2787 | } |
| 2788 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2789 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 2790 | .atomic_write_len = PAGE_SIZE, |
| 2791 | .write = cgroup_file_write, |
| 2792 | .seq_show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2793 | }; |
| 2794 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2795 | static struct kernfs_ops cgroup_kf_ops = { |
| 2796 | .atomic_write_len = PAGE_SIZE, |
| 2797 | .write = cgroup_file_write, |
| 2798 | .seq_start = cgroup_seqfile_start, |
| 2799 | .seq_next = cgroup_seqfile_next, |
| 2800 | .seq_stop = cgroup_seqfile_stop, |
| 2801 | .seq_show = cgroup_seqfile_show, |
| 2802 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2803 | |
| 2804 | /* |
| 2805 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2806 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2807 | static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent, |
| 2808 | const char *new_name_str) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2809 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2810 | struct cgroup *cgrp = kn->priv; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2811 | int ret; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2812 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2813 | if (kernfs_type(kn) != KERNFS_DIR) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2814 | return -ENOTDIR; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2815 | if (kn->parent != new_parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2816 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2817 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2818 | /* |
| 2819 | * This isn't a proper migration and its usefulness is very |
| 2820 | * limited. Disallow if sane_behavior. |
| 2821 | */ |
| 2822 | if (cgroup_sane_behavior(cgrp)) |
| 2823 | return -EPERM; |
| 2824 | |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 2825 | /* |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 2826 | * We're gonna grab cgroup_mutex which nests outside kernfs |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 2827 | * active_ref. kernfs_rename() doesn't require active_ref |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 2828 | * protection. Break them before grabbing cgroup_mutex. |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 2829 | */ |
| 2830 | kernfs_break_active_protection(new_parent); |
| 2831 | kernfs_break_active_protection(kn); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2832 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2833 | mutex_lock(&cgroup_mutex); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2834 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2835 | ret = kernfs_rename(kn, new_parent, new_name_str); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2836 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2837 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2838 | |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 2839 | kernfs_unbreak_active_protection(kn); |
| 2840 | kernfs_unbreak_active_protection(new_parent); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2841 | return ret; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2842 | } |
| 2843 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2844 | /* set uid and gid of cgroup dirs and files to that of the creator */ |
| 2845 | static int cgroup_kn_set_ugid(struct kernfs_node *kn) |
| 2846 | { |
| 2847 | struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, |
| 2848 | .ia_uid = current_fsuid(), |
| 2849 | .ia_gid = current_fsgid(), }; |
| 2850 | |
| 2851 | if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && |
| 2852 | gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) |
| 2853 | return 0; |
| 2854 | |
| 2855 | return kernfs_setattr(kn, &iattr); |
| 2856 | } |
| 2857 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2858 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2859 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2860 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2861 | struct kernfs_node *kn; |
| 2862 | struct lock_class_key *key = NULL; |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2863 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2864 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2865 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 2866 | key = &cft->lockdep_key; |
| 2867 | #endif |
| 2868 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
| 2869 | cgroup_file_mode(cft), 0, cft->kf_ops, cft, |
| 2870 | NULL, false, key); |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2871 | if (IS_ERR(kn)) |
| 2872 | return PTR_ERR(kn); |
| 2873 | |
| 2874 | ret = cgroup_kn_set_ugid(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2875 | if (ret) { |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2876 | kernfs_remove(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2877 | return ret; |
| 2878 | } |
| 2879 | |
Tejun Heo | b7fc5ad | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2880 | if (cft->seq_show == cgroup_populated_show) |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 2881 | cgrp->populated_kn = kn; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2882 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2883 | } |
| 2884 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2885 | /** |
| 2886 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2887 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2888 | * @cfts: array of cftypes to be added |
| 2889 | * @is_add: whether to add or remove |
| 2890 | * |
| 2891 | * 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] | 2892 | * For removals, this function never fails. If addition fails, this |
| 2893 | * function doesn't remove files already added. The caller is responsible |
| 2894 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2895 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2896 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2897 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2898 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2899 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2900 | int ret; |
| 2901 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 2902 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2903 | |
| 2904 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2905 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 8cbbf2c | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 2906 | if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) |
| 2907 | continue; |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2908 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2909 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 2910 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2911 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 2912 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2913 | continue; |
| 2914 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2915 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2916 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2917 | if (ret) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 2918 | pr_warn("%s: failed to add %s, err=%d\n", |
| 2919 | __func__, cft->name, ret); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2920 | return ret; |
| 2921 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2922 | } else { |
| 2923 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2924 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2925 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2926 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2927 | } |
| 2928 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2929 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2930 | { |
| 2931 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2932 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2933 | struct cgroup *root = &ss->root->cgrp; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2934 | struct cgroup_subsys_state *css; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2935 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2936 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 2937 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2938 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2939 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2940 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2941 | struct cgroup *cgrp = css->cgroup; |
| 2942 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2943 | if (cgroup_is_dead(cgrp)) |
| 2944 | continue; |
| 2945 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2946 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2947 | if (ret) |
| 2948 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2949 | } |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2950 | |
| 2951 | if (is_add && !ret) |
| 2952 | kernfs_activate(root->kn); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2953 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2954 | } |
| 2955 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2956 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 2957 | { |
| 2958 | struct cftype *cft; |
| 2959 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2960 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2961 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 2962 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 2963 | kfree(cft->kf_ops); |
| 2964 | cft->kf_ops = NULL; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2965 | cft->ss = NULL; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2966 | } |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2967 | } |
| 2968 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2969 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2970 | { |
| 2971 | struct cftype *cft; |
| 2972 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2973 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2974 | struct kernfs_ops *kf_ops; |
| 2975 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2976 | WARN_ON(cft->ss || cft->kf_ops); |
| 2977 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2978 | if (cft->seq_start) |
| 2979 | kf_ops = &cgroup_kf_ops; |
| 2980 | else |
| 2981 | kf_ops = &cgroup_kf_single_ops; |
| 2982 | |
| 2983 | /* |
| 2984 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 2985 | * make a copy of kf_ops to set its atomic_write_len. |
| 2986 | */ |
| 2987 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 2988 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 2989 | if (!kf_ops) { |
| 2990 | cgroup_exit_cftypes(cfts); |
| 2991 | return -ENOMEM; |
| 2992 | } |
| 2993 | kf_ops->atomic_write_len = cft->max_write_len; |
| 2994 | } |
| 2995 | |
| 2996 | cft->kf_ops = kf_ops; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2997 | cft->ss = ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | return 0; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3001 | } |
| 3002 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3003 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 3004 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3005 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3006 | |
| 3007 | if (!cfts || !cfts[0].ss) |
| 3008 | return -ENOENT; |
| 3009 | |
| 3010 | list_del(&cfts->node); |
| 3011 | cgroup_apply_cftypes(cfts, false); |
| 3012 | cgroup_exit_cftypes(cfts); |
| 3013 | return 0; |
| 3014 | } |
| 3015 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3016 | /** |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3017 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 3018 | * @cfts: zero-length name terminated array of cftypes |
| 3019 | * |
| 3020 | * Unregister @cfts. Files described by @cfts are removed from all |
| 3021 | * existing cgroups and all future cgroups won't have them either. This |
| 3022 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 3023 | * |
| 3024 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 3025 | * registered. |
| 3026 | */ |
| 3027 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 3028 | { |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3029 | int ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3030 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3031 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3032 | ret = cgroup_rm_cftypes_locked(cfts); |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3033 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3034 | return ret; |
| 3035 | } |
| 3036 | |
| 3037 | /** |
| 3038 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 3039 | * @ss: target cgroup subsystem |
| 3040 | * @cfts: zero-length name terminated array of cftypes |
| 3041 | * |
| 3042 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 3043 | * existing cgroups to which @ss is attached and all future cgroups will |
| 3044 | * have them too. This function can be called anytime whether @ss is |
| 3045 | * attached or not. |
| 3046 | * |
| 3047 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 3048 | * function currently returns 0 as long as @cfts registration is successful |
| 3049 | * even if some file creation attempts on existing cgroups fail. |
| 3050 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 3051 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3052 | { |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3053 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3054 | |
Li Zefan | dc5736e | 2014-02-17 10:41:50 +0800 | [diff] [blame] | 3055 | if (!cfts || cfts[0].name[0] == '\0') |
| 3056 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3057 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3058 | ret = cgroup_init_cftypes(ss, cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3059 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3060 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3061 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3062 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3063 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3064 | list_add_tail(&cfts->node, &ss->cfts); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3065 | ret = cgroup_apply_cftypes(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3066 | if (ret) |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3067 | cgroup_rm_cftypes_locked(cfts); |
| 3068 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3069 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3070 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3071 | } |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3072 | |
| 3073 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3074 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 3075 | * @cgrp: the cgroup in question |
| 3076 | * |
| 3077 | * Return the number of tasks in the cgroup. |
| 3078 | */ |
Tejun Heo | 07bc356 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3079 | static int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3080 | { |
| 3081 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3082 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3083 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3084 | down_read(&css_set_rwsem); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3085 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 3086 | count += atomic_read(&link->cset->refcount); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3087 | up_read(&css_set_rwsem); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3088 | return count; |
| 3089 | } |
| 3090 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3091 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3092 | * css_next_child - find the next child of a given css |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3093 | * @pos: the current position (%NULL to initiate traversal) |
| 3094 | * @parent: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3095 | * |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3096 | * This function returns the next child of @parent and should be called |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3097 | * under either cgroup_mutex or RCU read lock. The only requirement is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3098 | * that @parent and @pos are accessible. The next sibling is guaranteed to |
| 3099 | * be returned regardless of their states. |
| 3100 | * |
| 3101 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 3102 | * css which finished ->css_online() is guaranteed to be visible in the |
| 3103 | * future iterations and will stay visible until the last reference is put. |
| 3104 | * A css which hasn't finished ->css_online() or already finished |
| 3105 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 3106 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3107 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3108 | struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, |
| 3109 | struct cgroup_subsys_state *parent) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3110 | { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3111 | struct cgroup_subsys_state *next; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3112 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3113 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3114 | |
| 3115 | /* |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 3116 | * @pos could already have been unlinked from the sibling list. |
| 3117 | * Once a cgroup is removed, its ->sibling.next is no longer |
| 3118 | * updated when its next sibling changes. CSS_RELEASED is set when |
| 3119 | * @pos is taken off list, at which time its next pointer is valid, |
| 3120 | * and, as releases are serialized, the one pointed to by the next |
| 3121 | * pointer is guaranteed to not have started release yet. This |
| 3122 | * implies that if we observe !CSS_RELEASED on @pos in this RCU |
| 3123 | * critical section, the one pointed to by its next pointer is |
| 3124 | * guaranteed to not have finished its RCU grace period even if we |
| 3125 | * have dropped rcu_read_lock() inbetween iterations. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3126 | * |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 3127 | * If @pos has CSS_RELEASED set, its next pointer can't be |
| 3128 | * dereferenced; however, as each css is given a monotonically |
| 3129 | * increasing unique serial number and always appended to the |
| 3130 | * sibling list, the next one can be found by walking the parent's |
| 3131 | * children until the first css with higher serial number than |
| 3132 | * @pos's. While this path can be slower, it happens iff iteration |
| 3133 | * races against release and the race window is very small. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3134 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3135 | if (!pos) { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3136 | next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling); |
| 3137 | } else if (likely(!(pos->flags & CSS_RELEASED))) { |
| 3138 | next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3139 | } else { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3140 | list_for_each_entry_rcu(next, &parent->children, sibling) |
| 3141 | if (next->serial_nr > pos->serial_nr) |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3142 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3143 | } |
| 3144 | |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3145 | /* |
| 3146 | * @next, if not pointing to the head, can be dereferenced and is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3147 | * the next sibling. |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3148 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3149 | if (&next->sibling != &parent->children) |
| 3150 | return next; |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3151 | return NULL; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3152 | } |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3153 | |
| 3154 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3155 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3156 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3157 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3158 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3159 | * 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] | 3160 | * to visit for pre-order traversal of @root's descendants. @root is |
| 3161 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3162 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3163 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3164 | * doesn't require the whole traversal to be contained in a single critical |
| 3165 | * section. This function will return the correct next descendant as long |
| 3166 | * as both @pos and @root are accessible and @pos is a descendant of @root. |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3167 | * |
| 3168 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 3169 | * css which finished ->css_online() is guaranteed to be visible in the |
| 3170 | * future iterations and will stay visible until the last reference is put. |
| 3171 | * A css which hasn't finished ->css_online() or already finished |
| 3172 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 3173 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3174 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3175 | struct cgroup_subsys_state * |
| 3176 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 3177 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3178 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3179 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3180 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3181 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3182 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3183 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3184 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3185 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3186 | |
| 3187 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3188 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3189 | if (next) |
| 3190 | return next; |
| 3191 | |
| 3192 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3193 | while (pos != root) { |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3194 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3195 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3196 | return next; |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3197 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3198 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3199 | |
| 3200 | return NULL; |
| 3201 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3202 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3203 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3204 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 3205 | * @pos: css of interest |
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 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 3208 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3209 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3210 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3211 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3212 | * doesn't require the whole traversal to be contained in a single critical |
| 3213 | * section. This function will return the correct rightmost descendant as |
| 3214 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3215 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3216 | struct cgroup_subsys_state * |
| 3217 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
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 *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3220 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3221 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3222 | |
| 3223 | do { |
| 3224 | last = pos; |
| 3225 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3226 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3227 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3228 | pos = tmp; |
| 3229 | } while (pos); |
| 3230 | |
| 3231 | return last; |
| 3232 | } |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3233 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3234 | static struct cgroup_subsys_state * |
| 3235 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3236 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3237 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3238 | |
| 3239 | do { |
| 3240 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3241 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3242 | } while (pos); |
| 3243 | |
| 3244 | return last; |
| 3245 | } |
| 3246 | |
| 3247 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3248 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3249 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3250 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3251 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3252 | * 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] | 3253 | * to visit for post-order traversal of @root's descendants. @root is |
| 3254 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3255 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3256 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3257 | * doesn't require the whole traversal to be contained in a single critical |
| 3258 | * section. This function will return the correct next descendant as long |
| 3259 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 3260 | * @cgroup. |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3261 | * |
| 3262 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 3263 | * css which finished ->css_online() is guaranteed to be visible in the |
| 3264 | * future iterations and will stay visible until the last reference is put. |
| 3265 | * A css which hasn't finished ->css_online() or already finished |
| 3266 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 3267 | * responsibility to synchronize against on/offlining. |
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 * |
| 3270 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 3271 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3272 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3273 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3274 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3275 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3276 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 3277 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 3278 | if (!pos) |
| 3279 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3280 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3281 | /* if we visited @root, we're done */ |
| 3282 | if (pos == root) |
| 3283 | return NULL; |
| 3284 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3285 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3286 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3287 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3288 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3289 | |
| 3290 | /* no sibling left, visit parent */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3291 | return pos->parent; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3292 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3293 | |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 3294 | static bool cgroup_has_live_children(struct cgroup *cgrp) |
| 3295 | { |
| 3296 | struct cgroup *child; |
| 3297 | |
| 3298 | rcu_read_lock(); |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3299 | list_for_each_entry_rcu(child, &cgrp->self.children, self.sibling) { |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 3300 | if (!cgroup_is_dead(child)) { |
| 3301 | rcu_read_unlock(); |
| 3302 | return true; |
| 3303 | } |
| 3304 | } |
| 3305 | rcu_read_unlock(); |
| 3306 | return false; |
| 3307 | } |
| 3308 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3309 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3310 | * 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] | 3311 | * @it: the iterator to advance |
| 3312 | * |
| 3313 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3314 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3315 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3316 | { |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3317 | struct list_head *l = it->cset_pos; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3318 | struct cgrp_cset_link *link; |
| 3319 | struct css_set *cset; |
| 3320 | |
| 3321 | /* Advance to the next non-empty css_set */ |
| 3322 | do { |
| 3323 | l = l->next; |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3324 | if (l == it->cset_head) { |
| 3325 | it->cset_pos = NULL; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3326 | return; |
| 3327 | } |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3328 | |
| 3329 | if (it->ss) { |
| 3330 | cset = container_of(l, struct css_set, |
| 3331 | e_cset_node[it->ss->id]); |
| 3332 | } else { |
| 3333 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3334 | cset = link->cset; |
| 3335 | } |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3336 | } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks)); |
| 3337 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3338 | it->cset_pos = l; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3339 | |
| 3340 | if (!list_empty(&cset->tasks)) |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3341 | it->task_pos = cset->tasks.next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3342 | else |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3343 | it->task_pos = cset->mg_tasks.next; |
| 3344 | |
| 3345 | it->tasks_head = &cset->tasks; |
| 3346 | it->mg_tasks_head = &cset->mg_tasks; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3347 | } |
| 3348 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3349 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3350 | * css_task_iter_start - initiate task iteration |
| 3351 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3352 | * @it: the task iterator to use |
| 3353 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3354 | * Initiate iteration through the tasks of @css. The caller can call |
| 3355 | * css_task_iter_next() to walk through the tasks until the function |
| 3356 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 3357 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3358 | * |
| 3359 | * Note that this function acquires a lock which is released when the |
| 3360 | * iteration finishes. The caller can't sleep while iteration is in |
| 3361 | * progress. |
| 3362 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3363 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 3364 | struct css_task_iter *it) |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3365 | __acquires(css_set_rwsem) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3366 | { |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 3367 | /* no one should try to iterate before mounting cgroups */ |
| 3368 | WARN_ON_ONCE(!use_task_css_set_links); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3369 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3370 | down_read(&css_set_rwsem); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3371 | |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3372 | it->ss = css->ss; |
| 3373 | |
| 3374 | if (it->ss) |
| 3375 | it->cset_pos = &css->cgroup->e_csets[css->ss->id]; |
| 3376 | else |
| 3377 | it->cset_pos = &css->cgroup->cset_links; |
| 3378 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3379 | it->cset_head = it->cset_pos; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3380 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3381 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3382 | } |
| 3383 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3384 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3385 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3386 | * @it: the task iterator being iterated |
| 3387 | * |
| 3388 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3389 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 3390 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3391 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3392 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3393 | { |
| 3394 | struct task_struct *res; |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3395 | struct list_head *l = it->task_pos; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3396 | |
| 3397 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3398 | if (!it->cset_pos) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3399 | return NULL; |
| 3400 | res = list_entry(l, struct task_struct, cg_list); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3401 | |
| 3402 | /* |
| 3403 | * Advance iterator to find next entry. cset->tasks is consumed |
| 3404 | * first and then ->mg_tasks. After ->mg_tasks, we move onto the |
| 3405 | * next cset. |
| 3406 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3407 | l = l->next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3408 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3409 | if (l == it->tasks_head) |
| 3410 | l = it->mg_tasks_head->next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3411 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3412 | if (l == it->mg_tasks_head) |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3413 | css_advance_task_iter(it); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3414 | else |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3415 | it->task_pos = l; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 3416 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3417 | return res; |
| 3418 | } |
| 3419 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3420 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3421 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3422 | * @it: the task iterator to finish |
| 3423 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3424 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3425 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3426 | void css_task_iter_end(struct css_task_iter *it) |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3427 | __releases(css_set_rwsem) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3428 | { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3429 | up_read(&css_set_rwsem); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | /** |
| 3433 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3434 | * @to: cgroup to which the tasks will be moved |
| 3435 | * @from: cgroup in which the tasks currently reside |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3436 | * |
| 3437 | * Locking rules between cgroup_post_fork() and the migration path |
| 3438 | * guarantee that, if a task is forking while being migrated, the new child |
| 3439 | * is guaranteed to be either visible in the source cgroup after the |
| 3440 | * parent's migration is complete or put into the target cgroup. No task |
| 3441 | * can slip out of migration through forking. |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3442 | */ |
| 3443 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3444 | { |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3445 | LIST_HEAD(preloaded_csets); |
| 3446 | struct cgrp_cset_link *link; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3447 | struct css_task_iter it; |
| 3448 | struct task_struct *task; |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3449 | int ret; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3450 | |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3451 | mutex_lock(&cgroup_mutex); |
| 3452 | |
| 3453 | /* all tasks in @from are being moved, all csets are source */ |
| 3454 | down_read(&css_set_rwsem); |
| 3455 | list_for_each_entry(link, &from->cset_links, cset_link) |
| 3456 | cgroup_migrate_add_src(link->cset, to, &preloaded_csets); |
| 3457 | up_read(&css_set_rwsem); |
| 3458 | |
| 3459 | ret = cgroup_migrate_prepare_dst(to, &preloaded_csets); |
| 3460 | if (ret) |
| 3461 | goto out_err; |
| 3462 | |
| 3463 | /* |
| 3464 | * Migrate tasks one-by-one until @form is empty. This fails iff |
| 3465 | * ->can_attach() fails. |
| 3466 | */ |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3467 | do { |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 3468 | css_task_iter_start(&from->self, &it); |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3469 | task = css_task_iter_next(&it); |
| 3470 | if (task) |
| 3471 | get_task_struct(task); |
| 3472 | css_task_iter_end(&it); |
| 3473 | |
| 3474 | if (task) { |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3475 | ret = cgroup_migrate(to, task, false); |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3476 | put_task_struct(task); |
| 3477 | } |
| 3478 | } while (task && !ret); |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 3479 | out_err: |
| 3480 | cgroup_migrate_finish(&preloaded_csets); |
| 3481 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 3482 | return ret; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3483 | } |
| 3484 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3485 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3486 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3487 | * |
| 3488 | * Reading this file can return large amounts of data if a cgroup has |
| 3489 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3490 | * but we cannot guarantee that the information we produce is correct |
| 3491 | * unless we produce it entirely atomically. |
| 3492 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3493 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3494 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3495 | /* which pidlist file are we talking about? */ |
| 3496 | enum cgroup_filetype { |
| 3497 | CGROUP_FILE_PROCS, |
| 3498 | CGROUP_FILE_TASKS, |
| 3499 | }; |
| 3500 | |
| 3501 | /* |
| 3502 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3503 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3504 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3505 | * to the cgroup. |
| 3506 | */ |
| 3507 | struct cgroup_pidlist { |
| 3508 | /* |
| 3509 | * used to find which pidlist is wanted. doesn't change as long as |
| 3510 | * this particular list stays in the list. |
| 3511 | */ |
| 3512 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3513 | /* array of xids */ |
| 3514 | pid_t *list; |
| 3515 | /* how many elements the above list has */ |
| 3516 | int length; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3517 | /* each of these stored in a list by its cgroup */ |
| 3518 | struct list_head links; |
| 3519 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3520 | struct cgroup *owner; |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3521 | /* for delayed destruction */ |
| 3522 | struct delayed_work destroy_dwork; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3523 | }; |
| 3524 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3525 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3526 | * The following two functions "fix" the issue where there are more pids |
| 3527 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3528 | * TODO: replace with a kernel-wide solution to this problem |
| 3529 | */ |
| 3530 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3531 | static void *pidlist_allocate(int count) |
| 3532 | { |
| 3533 | if (PIDLIST_TOO_LARGE(count)) |
| 3534 | return vmalloc(count * sizeof(pid_t)); |
| 3535 | else |
| 3536 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3537 | } |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3538 | |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3539 | static void pidlist_free(void *p) |
| 3540 | { |
| 3541 | if (is_vmalloc_addr(p)) |
| 3542 | vfree(p); |
| 3543 | else |
| 3544 | kfree(p); |
| 3545 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3546 | |
| 3547 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3548 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 3549 | * should be left afterwards. |
| 3550 | */ |
| 3551 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 3552 | { |
| 3553 | struct cgroup_pidlist *l, *tmp_l; |
| 3554 | |
| 3555 | mutex_lock(&cgrp->pidlist_mutex); |
| 3556 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 3557 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 3558 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3559 | |
| 3560 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 3561 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 3562 | } |
| 3563 | |
| 3564 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 3565 | { |
| 3566 | struct delayed_work *dwork = to_delayed_work(work); |
| 3567 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 3568 | destroy_dwork); |
| 3569 | struct cgroup_pidlist *tofree = NULL; |
| 3570 | |
| 3571 | mutex_lock(&l->owner->pidlist_mutex); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3572 | |
| 3573 | /* |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3574 | * Destroy iff we didn't get queued again. The state won't change |
| 3575 | * as destroy_dwork can only be queued while locked. |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3576 | */ |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3577 | if (!delayed_work_pending(dwork)) { |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3578 | list_del(&l->links); |
| 3579 | pidlist_free(l->list); |
| 3580 | put_pid_ns(l->key.ns); |
| 3581 | tofree = l; |
| 3582 | } |
| 3583 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3584 | mutex_unlock(&l->owner->pidlist_mutex); |
| 3585 | kfree(tofree); |
| 3586 | } |
| 3587 | |
| 3588 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3589 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3590 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3591 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3592 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3593 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3594 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3595 | |
| 3596 | /* |
| 3597 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3598 | * edge cases first; no work needs to be done for either |
| 3599 | */ |
| 3600 | if (length == 0 || length == 1) |
| 3601 | return length; |
| 3602 | /* src and dest walk down the list; dest counts unique elements */ |
| 3603 | for (src = 1; src < length; src++) { |
| 3604 | /* find next unique element */ |
| 3605 | while (list[src] == list[src-1]) { |
| 3606 | src++; |
| 3607 | if (src == length) |
| 3608 | goto after; |
| 3609 | } |
| 3610 | /* dest always points to where the next unique element goes */ |
| 3611 | list[dest] = list[src]; |
| 3612 | dest++; |
| 3613 | } |
| 3614 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3615 | return dest; |
| 3616 | } |
| 3617 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3618 | /* |
| 3619 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 3620 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 3621 | * different per namespace, each namespace needs differently sorted list, |
| 3622 | * making it impossible to use, for example, single rbtree of member tasks |
| 3623 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 3624 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 3625 | * pidlists keyed by cgroup and namespace. |
| 3626 | * |
| 3627 | * All this extra complexity was caused by the original implementation |
| 3628 | * committing to an entirely unnecessary property. In the long term, we |
| 3629 | * want to do away with it. Explicitly scramble sort order if |
| 3630 | * sane_behavior so that no such expectation exists in the new interface. |
| 3631 | * |
| 3632 | * Scrambling is done by swapping every two consecutive bits, which is |
| 3633 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 3634 | */ |
| 3635 | static pid_t pid_fry(pid_t pid) |
| 3636 | { |
| 3637 | unsigned a = pid & 0x55555555; |
| 3638 | unsigned b = pid & 0xAAAAAAAA; |
| 3639 | |
| 3640 | return (a << 1) | (b >> 1); |
| 3641 | } |
| 3642 | |
| 3643 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 3644 | { |
| 3645 | if (cgroup_sane_behavior(cgrp)) |
| 3646 | return pid_fry(pid); |
| 3647 | else |
| 3648 | return pid; |
| 3649 | } |
| 3650 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3651 | static int cmppid(const void *a, const void *b) |
| 3652 | { |
| 3653 | return *(pid_t *)a - *(pid_t *)b; |
| 3654 | } |
| 3655 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3656 | static int fried_cmppid(const void *a, const void *b) |
| 3657 | { |
| 3658 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 3659 | } |
| 3660 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3661 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3662 | enum cgroup_filetype type) |
| 3663 | { |
| 3664 | struct cgroup_pidlist *l; |
| 3665 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3666 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3667 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3668 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3669 | |
| 3670 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 3671 | if (l->key.type == type && l->key.ns == ns) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3672 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3673 | return NULL; |
| 3674 | } |
| 3675 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3676 | /* |
| 3677 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3678 | * returns with the lock on that pidlist already held, and takes care |
| 3679 | * of the use count, or returns NULL with no locks held if we're out of |
| 3680 | * memory. |
| 3681 | */ |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3682 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 3683 | enum cgroup_filetype type) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3684 | { |
| 3685 | struct cgroup_pidlist *l; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3686 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3687 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3688 | |
| 3689 | l = cgroup_pidlist_find(cgrp, type); |
| 3690 | if (l) |
| 3691 | return l; |
| 3692 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3693 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3694 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3695 | if (!l) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3696 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3697 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3698 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3699 | l->key.type = type; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3700 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 3701 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3702 | l->owner = cgrp; |
| 3703 | list_add(&l->links, &cgrp->pidlists); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3704 | return l; |
| 3705 | } |
| 3706 | |
| 3707 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3708 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3709 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3710 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3711 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3712 | { |
| 3713 | pid_t *array; |
| 3714 | int length; |
| 3715 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3716 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3717 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3718 | struct cgroup_pidlist *l; |
| 3719 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3720 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3721 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3722 | /* |
| 3723 | * If cgroup gets more users after we read count, we won't have |
| 3724 | * enough space - tough. This race is indistinguishable to the |
| 3725 | * caller from the case that the additional cgroup users didn't |
| 3726 | * show up until sometime later on. |
| 3727 | */ |
| 3728 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3729 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3730 | if (!array) |
| 3731 | return -ENOMEM; |
| 3732 | /* now, populate the array */ |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 3733 | css_task_iter_start(&cgrp->self, &it); |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3734 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3735 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3736 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3737 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3738 | if (type == CGROUP_FILE_PROCS) |
| 3739 | pid = task_tgid_vnr(tsk); |
| 3740 | else |
| 3741 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3742 | if (pid > 0) /* make sure to only use valid results */ |
| 3743 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3744 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3745 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3746 | length = n; |
| 3747 | /* now sort & (if procs) strip out duplicates */ |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3748 | if (cgroup_sane_behavior(cgrp)) |
| 3749 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 3750 | else |
| 3751 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3752 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3753 | length = pidlist_uniq(array, length); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3754 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3755 | l = cgroup_pidlist_find_create(cgrp, type); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3756 | if (!l) { |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3757 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3758 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3759 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3760 | } |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3761 | |
| 3762 | /* store array, freeing old if necessary */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3763 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3764 | l->list = array; |
| 3765 | l->length = length; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3766 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3767 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3768 | } |
| 3769 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3770 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3771 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3772 | * @stats: cgroupstats to fill information into |
| 3773 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3774 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3775 | * |
| 3776 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3777 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3778 | */ |
| 3779 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3780 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3781 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3782 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3783 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3784 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3785 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3786 | /* it should be kernfs_node belonging to cgroupfs and is a directory */ |
| 3787 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 3788 | kernfs_type(kn) != KERNFS_DIR) |
| 3789 | return -EINVAL; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3790 | |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3791 | mutex_lock(&cgroup_mutex); |
| 3792 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3793 | /* |
| 3794 | * 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] | 3795 | * @kn->priv's validity. For this and css_tryget_online_from_dir(), |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3796 | * @kn->priv is RCU safe. Let's do the RCU dancing. |
| 3797 | */ |
| 3798 | rcu_read_lock(); |
| 3799 | cgrp = rcu_dereference(kn->priv); |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3800 | if (!cgrp || cgroup_is_dead(cgrp)) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3801 | rcu_read_unlock(); |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3802 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3803 | return -ENOENT; |
| 3804 | } |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3805 | rcu_read_unlock(); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3806 | |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 3807 | css_task_iter_start(&cgrp->self, &it); |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3808 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3809 | switch (tsk->state) { |
| 3810 | case TASK_RUNNING: |
| 3811 | stats->nr_running++; |
| 3812 | break; |
| 3813 | case TASK_INTERRUPTIBLE: |
| 3814 | stats->nr_sleeping++; |
| 3815 | break; |
| 3816 | case TASK_UNINTERRUPTIBLE: |
| 3817 | stats->nr_uninterruptible++; |
| 3818 | break; |
| 3819 | case TASK_STOPPED: |
| 3820 | stats->nr_stopped++; |
| 3821 | break; |
| 3822 | default: |
| 3823 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3824 | stats->nr_io_wait++; |
| 3825 | break; |
| 3826 | } |
| 3827 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3828 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3829 | |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3830 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3831 | return 0; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3832 | } |
| 3833 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3834 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3835 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3836 | * 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] | 3837 | * 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] | 3838 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3839 | */ |
| 3840 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3841 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3842 | { |
| 3843 | /* |
| 3844 | * Initially we receive a position value that corresponds to |
| 3845 | * one more than the last pid shown (or 0 on the first call or |
| 3846 | * after a seek to the start). Use a binary-search to find the |
| 3847 | * next pid to display, if any |
| 3848 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3849 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3850 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3851 | struct cgroup_pidlist *l; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3852 | enum cgroup_filetype type = seq_cft(s)->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3853 | int index = 0, pid = *pos; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3854 | int *iter, ret; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3855 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3856 | mutex_lock(&cgrp->pidlist_mutex); |
| 3857 | |
| 3858 | /* |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3859 | * !NULL @of->priv indicates that this isn't the first start() |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3860 | * after open. If the matching pidlist is around, we can use that. |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3861 | * 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] | 3862 | * could already have been destroyed. |
| 3863 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3864 | if (of->priv) |
| 3865 | of->priv = cgroup_pidlist_find(cgrp, type); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3866 | |
| 3867 | /* |
| 3868 | * Either this is the first start() after open or the matching |
| 3869 | * pidlist has been destroyed inbetween. Create a new one. |
| 3870 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3871 | if (!of->priv) { |
| 3872 | ret = pidlist_array_load(cgrp, type, |
| 3873 | (struct cgroup_pidlist **)&of->priv); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3874 | if (ret) |
| 3875 | return ERR_PTR(ret); |
| 3876 | } |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3877 | l = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3878 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3879 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3880 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3881 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3882 | while (index < end) { |
| 3883 | int mid = (index + end) / 2; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3884 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3885 | index = mid; |
| 3886 | break; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3887 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3888 | index = mid + 1; |
| 3889 | else |
| 3890 | end = mid; |
| 3891 | } |
| 3892 | } |
| 3893 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3894 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3895 | return NULL; |
| 3896 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3897 | iter = l->list + index; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3898 | *pos = cgroup_pid_fry(cgrp, *iter); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3899 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3900 | } |
| 3901 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3902 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3903 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3904 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3905 | struct cgroup_pidlist *l = of->priv; |
Tejun Heo | 6223685 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3906 | |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3907 | if (l) |
| 3908 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3909 | CGROUP_PIDLIST_DESTROY_DELAY); |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3910 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3911 | } |
| 3912 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3913 | 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] | 3914 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3915 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3916 | struct cgroup_pidlist *l = of->priv; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3917 | pid_t *p = v; |
| 3918 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3919 | /* |
| 3920 | * Advance to the next pid in the array. If this goes off the |
| 3921 | * end, we're done |
| 3922 | */ |
| 3923 | p++; |
| 3924 | if (p >= end) { |
| 3925 | return NULL; |
| 3926 | } else { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3927 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3928 | return p; |
| 3929 | } |
| 3930 | } |
| 3931 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3932 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3933 | { |
| 3934 | return seq_printf(s, "%d\n", *(int *)v); |
| 3935 | } |
| 3936 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3937 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3938 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3939 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3940 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3941 | } |
| 3942 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3943 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3944 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3945 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3946 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3947 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3948 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3949 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3950 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3951 | return 0; |
| 3952 | } |
| 3953 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3954 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 3955 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3956 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3957 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3958 | } |
| 3959 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3960 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 3961 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3962 | { |
| 3963 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3964 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3965 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3966 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3967 | return 0; |
| 3968 | } |
| 3969 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3970 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3971 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3972 | .name = "cgroup.procs", |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3973 | .seq_start = cgroup_pidlist_start, |
| 3974 | .seq_next = cgroup_pidlist_next, |
| 3975 | .seq_stop = cgroup_pidlist_stop, |
| 3976 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3977 | .private = CGROUP_FILE_PROCS, |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 3978 | .write = cgroup_procs_write, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3979 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3980 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3981 | { |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3982 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3983 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3984 | .read_u64 = cgroup_clone_children_read, |
| 3985 | .write_u64 = cgroup_clone_children_write, |
| 3986 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3987 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3988 | .name = "cgroup.sane_behavior", |
| 3989 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3990 | .seq_show = cgroup_sane_behavior_show, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3991 | }, |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3992 | { |
| 3993 | .name = "cgroup.controllers", |
| 3994 | .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_ONLY_ON_ROOT, |
| 3995 | .seq_show = cgroup_root_controllers_show, |
| 3996 | }, |
| 3997 | { |
| 3998 | .name = "cgroup.controllers", |
| 3999 | .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT, |
| 4000 | .seq_show = cgroup_controllers_show, |
| 4001 | }, |
| 4002 | { |
| 4003 | .name = "cgroup.subtree_control", |
| 4004 | .flags = CFTYPE_ONLY_ON_DFL, |
| 4005 | .seq_show = cgroup_subtree_control_show, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4006 | .write = cgroup_subtree_control_write, |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4007 | }, |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 4008 | { |
| 4009 | .name = "cgroup.populated", |
| 4010 | .flags = CFTYPE_ONLY_ON_DFL | CFTYPE_NOT_ON_ROOT, |
| 4011 | .seq_show = cgroup_populated_show, |
| 4012 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4013 | |
| 4014 | /* |
| 4015 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 4016 | * don't exist if sane_behavior. If you're depending on these, be |
| 4017 | * prepared to be burned. |
| 4018 | */ |
| 4019 | { |
| 4020 | .name = "tasks", |
| 4021 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4022 | .seq_start = cgroup_pidlist_start, |
| 4023 | .seq_next = cgroup_pidlist_next, |
| 4024 | .seq_stop = cgroup_pidlist_stop, |
| 4025 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4026 | .private = CGROUP_FILE_TASKS, |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 4027 | .write = cgroup_tasks_write, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4028 | .mode = S_IRUGO | S_IWUSR, |
| 4029 | }, |
| 4030 | { |
| 4031 | .name = "notify_on_release", |
| 4032 | .flags = CFTYPE_INSANE, |
| 4033 | .read_u64 = cgroup_read_notify_on_release, |
| 4034 | .write_u64 = cgroup_write_notify_on_release, |
| 4035 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4036 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4037 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4038 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4039 | .seq_show = cgroup_release_agent_show, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4040 | .write = cgroup_release_agent_write, |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 4041 | .max_write_len = PATH_MAX - 1, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4042 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4043 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4044 | }; |
| 4045 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4046 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4047 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4048 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4049 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4050 | * |
| 4051 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4052 | */ |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4053 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned int subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4054 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4055 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4056 | int i, ret = 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4057 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4058 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4059 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4060 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4061 | |
Tejun Heo | 69dfa00 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4062 | if (!(subsys_mask & (1 << i))) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4063 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4064 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4065 | list_for_each_entry(cfts, &ss->cfts, node) { |
| 4066 | ret = cgroup_addrm_files(cgrp, cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4067 | if (ret < 0) |
| 4068 | goto err; |
| 4069 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4070 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4071 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4072 | err: |
| 4073 | cgroup_clear_dir(cgrp, subsys_mask); |
| 4074 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4075 | } |
| 4076 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4077 | /* |
| 4078 | * css destruction is four-stage process. |
| 4079 | * |
| 4080 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 4081 | * Implemented in kill_css(). |
| 4082 | * |
| 4083 | * 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] | 4084 | * and thus css_tryget_online() is guaranteed to fail, the css can be |
| 4085 | * offlined by invoking offline_css(). After offlining, the base ref is |
| 4086 | * put. Implemented in css_killed_work_fn(). |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4087 | * |
| 4088 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 4089 | * accessors are inside RCU read sections. css_release() schedules the |
| 4090 | * RCU callback. |
| 4091 | * |
| 4092 | * 4. After the grace period, the css can be freed. Implemented in |
| 4093 | * css_free_work_fn(). |
| 4094 | * |
| 4095 | * It is actually hairier because both step 2 and 4 require process context |
| 4096 | * and thus involve punting to css->destroy_work adding two additional |
| 4097 | * steps to the already complex sequence. |
| 4098 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4099 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4100 | { |
| 4101 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4102 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4103 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4104 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4105 | if (css->ss) { |
| 4106 | /* css free path */ |
| 4107 | if (css->parent) |
| 4108 | css_put(css->parent); |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4109 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4110 | css->ss->css_free(css); |
| 4111 | cgroup_put(cgrp); |
| 4112 | } else { |
| 4113 | /* cgroup free path */ |
| 4114 | atomic_dec(&cgrp->root->nr_cgrps); |
| 4115 | cgroup_pidlist_destroy_all(cgrp); |
| 4116 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4117 | if (cgroup_parent(cgrp)) { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4118 | /* |
| 4119 | * We get a ref to the parent, and put the ref when |
| 4120 | * this cgroup is being freed, so it's guaranteed |
| 4121 | * that the parent won't be destroyed before its |
| 4122 | * children. |
| 4123 | */ |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4124 | cgroup_put(cgroup_parent(cgrp)); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4125 | kernfs_put(cgrp->kn); |
| 4126 | kfree(cgrp); |
| 4127 | } else { |
| 4128 | /* |
| 4129 | * This is root cgroup's refcnt reaching zero, |
| 4130 | * which indicates that the root should be |
| 4131 | * released. |
| 4132 | */ |
| 4133 | cgroup_destroy_root(cgrp->root); |
| 4134 | } |
| 4135 | } |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 4139 | { |
| 4140 | struct cgroup_subsys_state *css = |
| 4141 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 4142 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4143 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4144 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4145 | } |
| 4146 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4147 | static void css_release_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4148 | { |
| 4149 | struct cgroup_subsys_state *css = |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4150 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4151 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4152 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4153 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4154 | mutex_lock(&cgroup_mutex); |
| 4155 | |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4156 | css->flags |= CSS_RELEASED; |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4157 | list_del_rcu(&css->sibling); |
| 4158 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4159 | if (ss) { |
| 4160 | /* css release path */ |
| 4161 | cgroup_idr_remove(&ss->css_idr, css->id); |
| 4162 | } else { |
| 4163 | /* cgroup release path */ |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4164 | cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 4165 | cgrp->id = -1; |
| 4166 | } |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4167 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4168 | mutex_unlock(&cgroup_mutex); |
| 4169 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4170 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4171 | } |
| 4172 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4173 | static void css_release(struct percpu_ref *ref) |
| 4174 | { |
| 4175 | struct cgroup_subsys_state *css = |
| 4176 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4177 | |
| 4178 | INIT_WORK(&css->destroy_work, css_release_work_fn); |
| 4179 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
| 4180 | } |
| 4181 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4182 | static void init_and_link_css(struct cgroup_subsys_state *css, |
| 4183 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4184 | { |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4185 | lockdep_assert_held(&cgroup_mutex); |
| 4186 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4187 | cgroup_get(cgrp); |
| 4188 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4189 | memset(css, 0, sizeof(*css)); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4190 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4191 | css->ss = ss; |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4192 | INIT_LIST_HEAD(&css->sibling); |
| 4193 | INIT_LIST_HEAD(&css->children); |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4194 | css->serial_nr = css_serial_nr_next++; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4195 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4196 | if (cgroup_parent(cgrp)) { |
| 4197 | css->parent = cgroup_css(cgroup_parent(cgrp), ss); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4198 | css_get(css->parent); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4199 | } |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4200 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4201 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4202 | } |
| 4203 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4204 | /* 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] | 4205 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4206 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4207 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4208 | int ret = 0; |
| 4209 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4210 | lockdep_assert_held(&cgroup_mutex); |
| 4211 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4212 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4213 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4214 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4215 | css->flags |= CSS_ONLINE; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4216 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4217 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4218 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4219 | } |
| 4220 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4221 | /* 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] | 4222 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4223 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4224 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4225 | |
| 4226 | lockdep_assert_held(&cgroup_mutex); |
| 4227 | |
| 4228 | if (!(css->flags & CSS_ONLINE)) |
| 4229 | return; |
| 4230 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4231 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4232 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4233 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4234 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | e329780 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4235 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4236 | |
| 4237 | wake_up_all(&css->cgroup->offline_waitq); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4238 | } |
| 4239 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4240 | /** |
| 4241 | * create_css - create a cgroup_subsys_state |
| 4242 | * @cgrp: the cgroup new css will be associated with |
| 4243 | * @ss: the subsys of new css |
| 4244 | * |
| 4245 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
| 4246 | * css is online and installed in @cgrp with all interface files created. |
| 4247 | * Returns 0 on success, -errno on failure. |
| 4248 | */ |
| 4249 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss) |
| 4250 | { |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4251 | struct cgroup *parent = cgroup_parent(cgrp); |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4252 | struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4253 | struct cgroup_subsys_state *css; |
| 4254 | int err; |
| 4255 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4256 | lockdep_assert_held(&cgroup_mutex); |
| 4257 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4258 | css = ss->css_alloc(parent_css); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4259 | if (IS_ERR(css)) |
| 4260 | return PTR_ERR(css); |
| 4261 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4262 | init_and_link_css(css, ss, cgrp); |
Tejun Heo | a2bed82 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4263 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4264 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4265 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4266 | goto err_free_css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4267 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4268 | err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_NOWAIT); |
| 4269 | if (err < 0) |
| 4270 | goto err_free_percpu_ref; |
| 4271 | css->id = err; |
| 4272 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4273 | err = cgroup_populate_dir(cgrp, 1 << ss->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4274 | if (err) |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4275 | goto err_free_id; |
| 4276 | |
| 4277 | /* @css is ready to be brought online now, make it visible */ |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4278 | list_add_tail_rcu(&css->sibling, &parent_css->children); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4279 | cgroup_idr_replace(&ss->css_idr, css, css->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4280 | |
| 4281 | err = online_css(css); |
| 4282 | if (err) |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4283 | goto err_list_del; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4284 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4285 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4286 | cgroup_parent(parent)) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4287 | 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] | 4288 | current->comm, current->pid, ss->name); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4289 | if (!strcmp(ss->name, "memory")) |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4290 | 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] | 4291 | ss->warned_broken_hierarchy = true; |
| 4292 | } |
| 4293 | |
| 4294 | return 0; |
| 4295 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4296 | err_list_del: |
| 4297 | list_del_rcu(&css->sibling); |
Linus Torvalds | 32d01dc | 2014-04-03 13:05:42 -0700 | [diff] [blame] | 4298 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4299 | err_free_id: |
| 4300 | cgroup_idr_remove(&ss->css_idr, css->id); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4301 | err_free_percpu_ref: |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4302 | percpu_ref_cancel_init(&css->refcnt); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4303 | err_free_css: |
Tejun Heo | a2bed82 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4304 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4305 | return err; |
| 4306 | } |
| 4307 | |
Tejun Heo | b3bfd98 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4308 | static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, |
| 4309 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4310 | { |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4311 | struct cgroup *parent, *cgrp; |
| 4312 | struct cgroup_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4313 | struct cgroup_subsys *ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4314 | struct kernfs_node *kn; |
Tejun Heo | b3bfd98 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4315 | int ssid, ret; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4316 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4317 | parent = cgroup_kn_lock_live(parent_kn); |
| 4318 | if (!parent) |
| 4319 | return -ENODEV; |
| 4320 | root = parent->root; |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4321 | |
| 4322 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
| 4323 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4324 | if (!cgrp) { |
| 4325 | ret = -ENOMEM; |
| 4326 | goto out_unlock; |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4327 | } |
| 4328 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4329 | ret = percpu_ref_init(&cgrp->self.refcnt, css_release); |
| 4330 | if (ret) |
| 4331 | goto out_free_cgrp; |
| 4332 | |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4333 | /* |
| 4334 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4335 | * a half-baked cgroup. |
| 4336 | */ |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4337 | 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] | 4338 | if (cgrp->id < 0) { |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4339 | ret = -ENOMEM; |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4340 | goto out_cancel_ref; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4341 | } |
| 4342 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4343 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4344 | |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 4345 | cgrp->self.parent = &parent->self; |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4346 | cgrp->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4347 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4348 | if (notify_on_release(parent)) |
| 4349 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4350 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4351 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4352 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4353 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4354 | /* create the directory */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4355 | kn = kernfs_create_dir(parent->kn, name, mode, cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4356 | if (IS_ERR(kn)) { |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4357 | ret = PTR_ERR(kn); |
| 4358 | goto out_free_id; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4359 | } |
| 4360 | cgrp->kn = kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4361 | |
Tejun Heo | 6f30558 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4362 | /* |
| 4363 | * This extra ref will be put in cgroup_free_fn() and guarantees |
| 4364 | * that @cgrp->kn is always accessible. |
| 4365 | */ |
| 4366 | kernfs_get(kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4367 | |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4368 | cgrp->self.serial_nr = css_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4369 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4370 | /* allocation complete, commit to creation */ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4371 | list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4372 | atomic_inc(&root->nr_cgrps); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4373 | cgroup_get(parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4374 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4375 | /* |
| 4376 | * @cgrp is now fully operational. If something fails after this |
| 4377 | * point, it'll be released via the normal destruction path. |
| 4378 | */ |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4379 | cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4380 | |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4381 | ret = cgroup_kn_set_ugid(kn); |
| 4382 | if (ret) |
| 4383 | goto out_destroy; |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 4384 | |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4385 | ret = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
| 4386 | if (ret) |
| 4387 | goto out_destroy; |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4388 | |
Tejun Heo | 9d403e9 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4389 | /* let's create and online css's */ |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4390 | for_each_subsys(ss, ssid) { |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4391 | if (parent->child_subsys_mask & (1 << ssid)) { |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4392 | ret = create_css(cgrp, ss); |
| 4393 | if (ret) |
| 4394 | goto out_destroy; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4395 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4396 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4397 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4398 | /* |
| 4399 | * On the default hierarchy, a child doesn't automatically inherit |
| 4400 | * child_subsys_mask from the parent. Each is configured manually. |
| 4401 | */ |
| 4402 | if (!cgroup_on_dfl(cgrp)) |
| 4403 | cgrp->child_subsys_mask = parent->child_subsys_mask; |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4404 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4405 | kernfs_activate(kn); |
| 4406 | |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4407 | ret = 0; |
| 4408 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4409 | |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4410 | out_free_id: |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4411 | cgroup_idr_remove(&root->cgroup_idr, cgrp->id); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4412 | out_cancel_ref: |
| 4413 | percpu_ref_cancel_init(&cgrp->self.refcnt); |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4414 | out_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4415 | kfree(cgrp); |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4416 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4417 | cgroup_kn_unlock(parent_kn); |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4418 | return ret; |
| 4419 | |
| 4420 | out_destroy: |
| 4421 | cgroup_destroy_locked(cgrp); |
| 4422 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4423 | } |
| 4424 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4425 | /* |
| 4426 | * This is called when the refcnt of a css is confirmed to be killed. |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4427 | * css_tryget_online() is now guaranteed to fail. Tell the subsystem to |
| 4428 | * initate destruction and put the css ref from kill_css(). |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4429 | */ |
| 4430 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4431 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4432 | struct cgroup_subsys_state *css = |
| 4433 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4434 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4435 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4436 | offline_css(css); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4437 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4438 | |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4439 | css_put(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4440 | } |
| 4441 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4442 | /* css kill confirmation processing requires process context, bounce */ |
| 4443 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4444 | { |
| 4445 | struct cgroup_subsys_state *css = |
| 4446 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4447 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4448 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4449 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4450 | } |
| 4451 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4452 | /** |
| 4453 | * kill_css - destroy a css |
| 4454 | * @css: css to destroy |
| 4455 | * |
| 4456 | * This function initiates destruction of @css by removing cgroup interface |
| 4457 | * files and putting its base reference. ->css_offline() will be invoked |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4458 | * asynchronously once css_tryget_online() is guaranteed to fail and when |
| 4459 | * the reference count reaches zero, @css will be released. |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4460 | */ |
| 4461 | static void kill_css(struct cgroup_subsys_state *css) |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4462 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4463 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4464 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4465 | /* |
| 4466 | * This must happen before css is disassociated with its cgroup. |
| 4467 | * See seq_css() for details. |
| 4468 | */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4469 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4470 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4471 | /* |
| 4472 | * Killing would put the base ref, but we need to keep it alive |
| 4473 | * until after ->css_offline(). |
| 4474 | */ |
| 4475 | css_get(css); |
| 4476 | |
| 4477 | /* |
| 4478 | * cgroup core guarantees that, by the time ->css_offline() is |
| 4479 | * invoked, no new css reference will be given out via |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4480 | * css_tryget_online(). We can't simply call percpu_ref_kill() and |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4481 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 4482 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 4483 | * |
| 4484 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4485 | * css is confirmed to be seen as killed on all CPUs. |
| 4486 | */ |
| 4487 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4488 | } |
| 4489 | |
| 4490 | /** |
| 4491 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4492 | * @cgrp: cgroup to be destroyed |
| 4493 | * |
| 4494 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4495 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 4496 | * guarantee that css_tryget_online() won't succeed by the time |
| 4497 | * ->css_offline() is invoked. To satisfy all the requirements, |
| 4498 | * destruction is implemented in the following two steps. |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4499 | * |
| 4500 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4501 | * userland visible parts and start killing the percpu refcnts of |
| 4502 | * css's. Set up so that the next stage will be kicked off once all |
| 4503 | * the percpu refcnts are confirmed to be killed. |
| 4504 | * |
| 4505 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4506 | * rest of destruction. Once all cgroup references are gone, the |
| 4507 | * cgroup is RCU-freed. |
| 4508 | * |
| 4509 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4510 | * the userland is concerned and a new cgroup with the same name may be |
| 4511 | * created. As cgroup doesn't care about the names internally, this |
| 4512 | * doesn't cause any problem. |
| 4513 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4514 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4515 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4516 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4517 | struct cgroup_subsys_state *css; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4518 | bool empty; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4519 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4520 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4521 | lockdep_assert_held(&cgroup_mutex); |
| 4522 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4523 | /* |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4524 | * css_set_rwsem synchronizes access to ->cset_links and prevents |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4525 | * @cgrp from being removed while put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4526 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4527 | down_read(&css_set_rwsem); |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4528 | empty = list_empty(&cgrp->cset_links); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4529 | up_read(&css_set_rwsem); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4530 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4531 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4532 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4533 | /* |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4534 | * Make sure there's no live children. We can't test emptiness of |
| 4535 | * ->self.children as dead children linger on it while being |
| 4536 | * drained; otherwise, "rmdir parent/child parent" may fail. |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4537 | */ |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4538 | if (cgroup_has_live_children(cgrp)) |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 4539 | return -EBUSY; |
| 4540 | |
| 4541 | /* |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4542 | * Mark @cgrp dead. This prevents further task migration and child |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4543 | * creation by disabling cgroup_lock_live_group(). |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4544 | */ |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame^] | 4545 | cgrp->self.flags &= ~CSS_ONLINE; |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4546 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4547 | /* initiate massacre of all css's */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4548 | for_each_css(css, ssid, cgrp) |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4549 | kill_css(css); |
| 4550 | |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame^] | 4551 | /* CSS_ONLINE is clear, remove from ->release_list for the last time */ |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4552 | raw_spin_lock(&release_list_lock); |
| 4553 | if (!list_empty(&cgrp->release_list)) |
| 4554 | list_del_init(&cgrp->release_list); |
| 4555 | raw_spin_unlock(&release_list_lock); |
| 4556 | |
| 4557 | /* |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4558 | * Remove @cgrp directory along with the base files. @cgrp has an |
| 4559 | * extra ref on its kn. |
| 4560 | */ |
| 4561 | kernfs_remove(cgrp->kn); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4562 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4563 | set_bit(CGRP_RELEASABLE, &cgroup_parent(cgrp)->flags); |
| 4564 | check_for_release(cgroup_parent(cgrp)); |
Tejun Heo | 9e4173e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4565 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4566 | /* put the base reference */ |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4567 | percpu_ref_kill(&cgrp->self.refcnt); |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4568 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4569 | return 0; |
| 4570 | }; |
| 4571 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4572 | static int cgroup_rmdir(struct kernfs_node *kn) |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4573 | { |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4574 | struct cgroup *cgrp; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4575 | int ret = 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4576 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4577 | cgrp = cgroup_kn_lock_live(kn); |
| 4578 | if (!cgrp) |
| 4579 | return 0; |
| 4580 | cgroup_get(cgrp); /* for @kn->priv clearing */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4581 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4582 | ret = cgroup_destroy_locked(cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4583 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4584 | cgroup_kn_unlock(kn); |
Tejun Heo | cfc79d5 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4585 | |
| 4586 | /* |
| 4587 | * There are two control paths which try to determine cgroup from |
| 4588 | * dentry without going through kernfs - cgroupstats_build() and |
| 4589 | * css_tryget_online_from_dir(). Those are supported by RCU |
| 4590 | * protecting clearing of cgrp->kn->priv backpointer, which should |
| 4591 | * happen after all files under it have been removed. |
| 4592 | */ |
| 4593 | if (!ret) |
| 4594 | RCU_INIT_POINTER(*(void __rcu __force **)&kn->priv, NULL); |
| 4595 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4596 | cgroup_put(cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4597 | return ret; |
| 4598 | } |
| 4599 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4600 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
| 4601 | .remount_fs = cgroup_remount, |
| 4602 | .show_options = cgroup_show_options, |
| 4603 | .mkdir = cgroup_mkdir, |
| 4604 | .rmdir = cgroup_rmdir, |
| 4605 | .rename = cgroup_rename, |
| 4606 | }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4607 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4608 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4609 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4610 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4611 | |
| 4612 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4613 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4614 | mutex_lock(&cgroup_mutex); |
| 4615 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4616 | idr_init(&ss->css_idr); |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4617 | INIT_LIST_HEAD(&ss->cfts); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4618 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4619 | /* Create the root cgroup state for this subsystem */ |
| 4620 | ss->root = &cgrp_dfl_root; |
| 4621 | css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4622 | /* We don't handle early failures gracefully */ |
| 4623 | BUG_ON(IS_ERR(css)); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4624 | init_and_link_css(css, ss, &cgrp_dfl_root.cgrp); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 4625 | |
| 4626 | /* |
| 4627 | * Root csses are never destroyed and we can't initialize |
| 4628 | * percpu_ref during early init. Disable refcnting. |
| 4629 | */ |
| 4630 | css->flags |= CSS_NO_REF; |
| 4631 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4632 | if (early) { |
Tejun Heo | 9395a45 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4633 | /* allocation can't be done safely during early init */ |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4634 | css->id = 1; |
| 4635 | } else { |
| 4636 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL); |
| 4637 | BUG_ON(css->id < 0); |
| 4638 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4639 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4640 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4641 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4642 | * newly registered, all tasks and hence the |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4643 | * init_css_set is in the subsystem's root cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4644 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4645 | |
| 4646 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4647 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4648 | /* At system boot, before all subsystems have been |
| 4649 | * registered, no tasks have been forked, so we don't |
| 4650 | * need to invoke fork callbacks here. */ |
| 4651 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4652 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4653 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4654 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4655 | cgrp_dfl_root.subsys_mask |= 1 << ss->id; |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4656 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4657 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4661 | * cgroup_init_early - cgroup initialization at system boot |
| 4662 | * |
| 4663 | * Initialize cgroups at system boot, and initialize any |
| 4664 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4665 | */ |
| 4666 | int __init cgroup_init_early(void) |
| 4667 | { |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 4668 | static struct cgroup_sb_opts __initdata opts = |
| 4669 | { .flags = CGRP_ROOT_SANE_BEHAVIOR }; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4670 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4671 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4672 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4673 | init_cgroup_root(&cgrp_dfl_root, &opts); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 4674 | cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF; |
| 4675 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4676 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4677 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4678 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4679 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4680 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n", |
| 4681 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4682 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4683 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 4684 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4685 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4686 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4687 | ss->name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4688 | |
| 4689 | if (ss->early_init) |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4690 | cgroup_init_subsys(ss, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4691 | } |
| 4692 | return 0; |
| 4693 | } |
| 4694 | |
| 4695 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4696 | * cgroup_init - cgroup initialization |
| 4697 | * |
| 4698 | * Register cgroup filesystem and /proc file, and initialize |
| 4699 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4700 | */ |
| 4701 | int __init cgroup_init(void) |
| 4702 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4703 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4704 | unsigned long key; |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4705 | int ssid, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4706 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4707 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4708 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4709 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4710 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4711 | /* Add init_css_set to the hash table */ |
| 4712 | key = css_set_hash(init_css_set.subsys); |
| 4713 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4714 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4715 | BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4716 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4717 | mutex_unlock(&cgroup_mutex); |
| 4718 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4719 | for_each_subsys(ss, ssid) { |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4720 | if (ss->early_init) { |
| 4721 | struct cgroup_subsys_state *css = |
| 4722 | init_css_set.subsys[ss->id]; |
| 4723 | |
| 4724 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, |
| 4725 | GFP_KERNEL); |
| 4726 | BUG_ON(css->id < 0); |
| 4727 | } else { |
| 4728 | cgroup_init_subsys(ss, false); |
| 4729 | } |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4730 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4731 | list_add_tail(&init_css_set.e_cset_node[ssid], |
| 4732 | &cgrp_dfl_root.cgrp.e_csets[ssid]); |
| 4733 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4734 | /* |
| 4735 | * cftype registration needs kmalloc and can't be done |
| 4736 | * during early_init. Register base cftypes separately. |
| 4737 | */ |
| 4738 | if (ss->base_cftypes) |
| 4739 | WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes)); |
| 4740 | } |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4741 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4742 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4743 | if (!cgroup_kobj) |
| 4744 | return -ENOMEM; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4745 | |
| 4746 | err = register_filesystem(&cgroup_fs_type); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4747 | if (err < 0) { |
| 4748 | kobject_put(cgroup_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4749 | return err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4750 | } |
| 4751 | |
| 4752 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4753 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4754 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4755 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4756 | static int __init cgroup_wq_init(void) |
| 4757 | { |
| 4758 | /* |
| 4759 | * There isn't much point in executing destruction path in |
| 4760 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 4761 | * Use 1 for @max_active. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4762 | * |
| 4763 | * We would prefer to do this in cgroup_init() above, but that |
| 4764 | * is called before init_workqueues(): so leave this until after. |
| 4765 | */ |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 4766 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4767 | BUG_ON(!cgroup_destroy_wq); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 4768 | |
| 4769 | /* |
| 4770 | * Used to destroy pidlists and separate to serve as flush domain. |
| 4771 | * Cap @max_active to 1 too. |
| 4772 | */ |
| 4773 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 4774 | 0, 1); |
| 4775 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 4776 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4777 | return 0; |
| 4778 | } |
| 4779 | core_initcall(cgroup_wq_init); |
| 4780 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4781 | /* |
| 4782 | * proc_cgroup_show() |
| 4783 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4784 | * - Used for /proc/<pid>/cgroup. |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4785 | */ |
| 4786 | |
| 4787 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4788 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4789 | { |
| 4790 | struct pid *pid; |
| 4791 | struct task_struct *tsk; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4792 | char *buf, *path; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4793 | int retval; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4794 | struct cgroup_root *root; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4795 | |
| 4796 | retval = -ENOMEM; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4797 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4798 | if (!buf) |
| 4799 | goto out; |
| 4800 | |
| 4801 | retval = -ESRCH; |
| 4802 | pid = m->private; |
| 4803 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4804 | if (!tsk) |
| 4805 | goto out_free; |
| 4806 | |
| 4807 | retval = 0; |
| 4808 | |
| 4809 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4810 | down_read(&css_set_rwsem); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4811 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4812 | for_each_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4813 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4814 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4815 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4816 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 4817 | if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4818 | continue; |
| 4819 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4820 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4821 | for_each_subsys(ss, ssid) |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 4822 | if (root->subsys_mask & (1 << ssid)) |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4823 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4824 | if (strlen(root->name)) |
| 4825 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4826 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4827 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4828 | cgrp = task_cgroup_from_root(tsk, root); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4829 | path = cgroup_path(cgrp, buf, PATH_MAX); |
| 4830 | if (!path) { |
| 4831 | retval = -ENAMETOOLONG; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4832 | goto out_unlock; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4833 | } |
| 4834 | seq_puts(m, path); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4835 | seq_putc(m, '\n'); |
| 4836 | } |
| 4837 | |
| 4838 | out_unlock: |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4839 | up_read(&css_set_rwsem); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4840 | mutex_unlock(&cgroup_mutex); |
| 4841 | put_task_struct(tsk); |
| 4842 | out_free: |
| 4843 | kfree(buf); |
| 4844 | out: |
| 4845 | return retval; |
| 4846 | } |
| 4847 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4848 | /* Display information about each subsystem and each hierarchy */ |
| 4849 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4850 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4851 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4852 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4853 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4854 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4855 | /* |
| 4856 | * ideally we don't want subsystems moving around while we do this. |
| 4857 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4858 | * subsys/hierarchy state. |
| 4859 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4860 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4861 | |
| 4862 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4863 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4864 | ss->name, ss->root->hierarchy_id, |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4865 | atomic_read(&ss->root->nr_cgrps), !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4866 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4867 | mutex_unlock(&cgroup_mutex); |
| 4868 | return 0; |
| 4869 | } |
| 4870 | |
| 4871 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4872 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4873 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4874 | } |
| 4875 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4876 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4877 | .open = cgroupstats_open, |
| 4878 | .read = seq_read, |
| 4879 | .llseek = seq_lseek, |
| 4880 | .release = single_release, |
| 4881 | }; |
| 4882 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4883 | /** |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4884 | * cgroup_fork - initialize cgroup related fields during copy_process() |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4885 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4886 | * |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4887 | * A task is associated with the init_css_set until cgroup_post_fork() |
| 4888 | * attaches it to the parent's css_set. Empty cg_list indicates that |
| 4889 | * @child isn't holding reference to its css_set. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4890 | */ |
| 4891 | void cgroup_fork(struct task_struct *child) |
| 4892 | { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4893 | RCU_INIT_POINTER(child->cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4894 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4895 | } |
| 4896 | |
| 4897 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4898 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4899 | * @child: the task in question |
| 4900 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4901 | * Adds the task to the list running through its css_set if necessary and |
| 4902 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4903 | * 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] | 4904 | * 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] | 4905 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4906 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4907 | void cgroup_post_fork(struct task_struct *child) |
| 4908 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4909 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4910 | int i; |
| 4911 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4912 | /* |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4913 | * This may race against cgroup_enable_task_cg_links(). As that |
| 4914 | * function sets use_task_css_set_links before grabbing |
| 4915 | * tasklist_lock and we just went through tasklist_lock to add |
| 4916 | * @child, it's guaranteed that either we see the set |
| 4917 | * use_task_css_set_links or cgroup_enable_task_cg_lists() sees |
| 4918 | * @child during its iteration. |
| 4919 | * |
| 4920 | * If we won the race, @child is associated with %current's |
| 4921 | * css_set. Grabbing css_set_rwsem guarantees both that the |
| 4922 | * association is stable, and, on completion of the parent's |
| 4923 | * migration, @child is visible in the source of migration or |
| 4924 | * already in the destination cgroup. This guarantee is necessary |
| 4925 | * when implementing operations which need to migrate all tasks of |
| 4926 | * a cgroup to another. |
| 4927 | * |
| 4928 | * Note that if we lose to cgroup_enable_task_cg_links(), @child |
| 4929 | * will remain in init_css_set. This is safe because all tasks are |
| 4930 | * in the init_css_set before cg_links is enabled and there's no |
| 4931 | * operation which transfers all tasks out of init_css_set. |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4932 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4933 | if (use_task_css_set_links) { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4934 | struct css_set *cset; |
| 4935 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4936 | down_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4937 | cset = task_css_set(current); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4938 | if (list_empty(&child->cg_list)) { |
| 4939 | rcu_assign_pointer(child->cgroups, cset); |
| 4940 | list_add(&child->cg_list, &cset->tasks); |
| 4941 | get_css_set(cset); |
| 4942 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4943 | up_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4944 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4945 | |
| 4946 | /* |
| 4947 | * Call ss->fork(). This must happen after @child is linked on |
| 4948 | * css_set; otherwise, @child might change state between ->fork() |
| 4949 | * and addition to css_set. |
| 4950 | */ |
| 4951 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4952 | for_each_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4953 | if (ss->fork) |
| 4954 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4955 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4956 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4957 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4958 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4959 | * cgroup_exit - detach cgroup from exiting task |
| 4960 | * @tsk: pointer to task_struct of exiting process |
| 4961 | * |
| 4962 | * Description: Detach cgroup from @tsk and release it. |
| 4963 | * |
| 4964 | * Note that cgroups marked notify_on_release force every task in |
| 4965 | * them to take the global cgroup_mutex mutex when exiting. |
| 4966 | * This could impact scaling on very large systems. Be reluctant to |
| 4967 | * use notify_on_release cgroups where very high task exit scaling |
| 4968 | * is required on large systems. |
| 4969 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4970 | * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We |
| 4971 | * call cgroup_exit() while the task is still competent to handle |
| 4972 | * notify_on_release(), then leave the task attached to the root cgroup in |
| 4973 | * each hierarchy for the remainder of its exit. No need to bother with |
| 4974 | * 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] | 4975 | * with migration path - PF_EXITING is visible to migration path. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4976 | */ |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 4977 | void cgroup_exit(struct task_struct *tsk) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4978 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4979 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4980 | struct css_set *cset; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4981 | bool put_cset = false; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4982 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4983 | |
| 4984 | /* |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4985 | * Unlink from @tsk from its css_set. As migration path can't race |
| 4986 | * with us, we can check cg_list without grabbing css_set_rwsem. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4987 | */ |
| 4988 | if (!list_empty(&tsk->cg_list)) { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4989 | down_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4990 | list_del_init(&tsk->cg_list); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4991 | up_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4992 | put_cset = true; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4993 | } |
| 4994 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4995 | /* Reassign the task to the init_css_set. */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4996 | cset = task_css_set(tsk); |
| 4997 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4998 | |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 4999 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5000 | /* see cgroup_post_fork() for details */ |
| 5001 | for_each_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5002 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5003 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 5004 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5005 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5006 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5007 | } |
| 5008 | } |
| 5009 | } |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5010 | |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5011 | if (put_cset) |
| 5012 | put_css_set(cset, true); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5013 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5014 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5015 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5016 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5017 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 9e4173e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5018 | list_empty(&cgrp->cset_links) && !cgroup_has_live_children(cgrp)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5019 | /* |
| 5020 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5021 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5022 | * it now |
| 5023 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5024 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5025 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5026 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5027 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5028 | list_empty(&cgrp->release_list)) { |
| 5029 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5030 | need_schedule_work = 1; |
| 5031 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5032 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5033 | if (need_schedule_work) |
| 5034 | schedule_work(&release_agent_work); |
| 5035 | } |
| 5036 | } |
| 5037 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5038 | /* |
| 5039 | * Notify userspace when a cgroup is released, by running the |
| 5040 | * configured release agent with the name of the cgroup (path |
| 5041 | * relative to the root of cgroup file system) as the argument. |
| 5042 | * |
| 5043 | * Most likely, this user command will try to rmdir this cgroup. |
| 5044 | * |
| 5045 | * This races with the possibility that some other task will be |
| 5046 | * attached to this cgroup before it is removed, or that some other |
| 5047 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5048 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5049 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5050 | * to continue to serve a useful existence. Next time it's released, |
| 5051 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5052 | * |
| 5053 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5054 | * means only wait until the task is successfully execve()'d. The |
| 5055 | * separate release agent task is forked by call_usermodehelper(), |
| 5056 | * then control in this thread returns here, without waiting for the |
| 5057 | * release agent task. We don't bother to wait because the caller of |
| 5058 | * this routine has no use for the exit status of the release agent |
| 5059 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5060 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5061 | static void cgroup_release_agent(struct work_struct *work) |
| 5062 | { |
| 5063 | BUG_ON(work != &release_agent_work); |
| 5064 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5065 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5066 | while (!list_empty(&release_list)) { |
| 5067 | char *argv[3], *envp[3]; |
| 5068 | int i; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5069 | char *pathbuf = NULL, *agentbuf = NULL, *path; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5070 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5071 | struct cgroup, |
| 5072 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5073 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5074 | raw_spin_unlock(&release_list_lock); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5075 | pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5076 | if (!pathbuf) |
| 5077 | goto continue_free; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5078 | path = cgroup_path(cgrp, pathbuf, PATH_MAX); |
| 5079 | if (!path) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5080 | goto continue_free; |
| 5081 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5082 | if (!agentbuf) |
| 5083 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5084 | |
| 5085 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5086 | argv[i++] = agentbuf; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5087 | argv[i++] = path; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5088 | argv[i] = NULL; |
| 5089 | |
| 5090 | i = 0; |
| 5091 | /* minimal command environment */ |
| 5092 | envp[i++] = "HOME=/"; |
| 5093 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5094 | envp[i] = NULL; |
| 5095 | |
| 5096 | /* Drop the lock while we invoke the usermode helper, |
| 5097 | * since the exec could involve hitting disk and hence |
| 5098 | * be a slow process */ |
| 5099 | mutex_unlock(&cgroup_mutex); |
| 5100 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5101 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5102 | continue_free: |
| 5103 | kfree(pathbuf); |
| 5104 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5105 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5106 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5107 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5108 | mutex_unlock(&cgroup_mutex); |
| 5109 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5110 | |
| 5111 | static int __init cgroup_disable(char *str) |
| 5112 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5113 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5114 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5115 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5116 | |
| 5117 | while ((token = strsep(&str, ",")) != NULL) { |
| 5118 | if (!*token) |
| 5119 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5120 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5121 | for_each_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5122 | if (!strcmp(token, ss->name)) { |
| 5123 | ss->disabled = 1; |
| 5124 | printk(KERN_INFO "Disabling %s control group" |
| 5125 | " subsystem\n", ss->name); |
| 5126 | break; |
| 5127 | } |
| 5128 | } |
| 5129 | } |
| 5130 | return 1; |
| 5131 | } |
| 5132 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5133 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5134 | /** |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5135 | * css_tryget_online_from_dir - get corresponding css from a cgroup dentry |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5136 | * @dentry: directory dentry of interest |
| 5137 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5138 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5139 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 5140 | * to get the corresponding css and return it. If such css doesn't exist |
| 5141 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5142 | */ |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5143 | struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, |
| 5144 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5145 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5146 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
| 5147 | struct cgroup_subsys_state *css = NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5148 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5149 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5150 | /* is @dentry a cgroup dir? */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5151 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 5152 | kernfs_type(kn) != KERNFS_DIR) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5153 | return ERR_PTR(-EBADF); |
| 5154 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5155 | rcu_read_lock(); |
| 5156 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5157 | /* |
| 5158 | * This path doesn't originate from kernfs and @kn could already |
| 5159 | * have been or be removed at any point. @kn->priv is RCU |
Tejun Heo | cfc79d5 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5160 | * protected for this access. See cgroup_rmdir() for details. |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5161 | */ |
| 5162 | cgrp = rcu_dereference(kn->priv); |
| 5163 | if (cgrp) |
| 5164 | css = cgroup_css(cgrp, ss); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5165 | |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5166 | if (!css || !css_tryget_online(css)) |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5167 | css = ERR_PTR(-ENOENT); |
| 5168 | |
| 5169 | rcu_read_unlock(); |
| 5170 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5171 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5172 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5173 | /** |
| 5174 | * css_from_id - lookup css by id |
| 5175 | * @id: the cgroup id |
| 5176 | * @ss: cgroup subsys to be looked into |
| 5177 | * |
| 5178 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5179 | * Should be called under rcu_read_lock(). |
| 5180 | */ |
| 5181 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5182 | { |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 5183 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5184 | return idr_find(&ss->css_idr, id); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5185 | } |
| 5186 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5187 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5188 | static struct cgroup_subsys_state * |
| 5189 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5190 | { |
| 5191 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5192 | |
| 5193 | if (!css) |
| 5194 | return ERR_PTR(-ENOMEM); |
| 5195 | |
| 5196 | return css; |
| 5197 | } |
| 5198 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5199 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5200 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5201 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5202 | } |
| 5203 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5204 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 5205 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5206 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5207 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5208 | } |
| 5209 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5210 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 5211 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5212 | { |
| 5213 | return (u64)(unsigned long)current->cgroups; |
| 5214 | } |
| 5215 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5216 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5217 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5218 | { |
| 5219 | u64 count; |
| 5220 | |
| 5221 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5222 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5223 | rcu_read_unlock(); |
| 5224 | return count; |
| 5225 | } |
| 5226 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5227 | 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] | 5228 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5229 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5230 | struct css_set *cset; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5231 | char *name_buf; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5232 | |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5233 | name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL); |
| 5234 | if (!name_buf) |
| 5235 | return -ENOMEM; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5236 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5237 | down_read(&css_set_rwsem); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5238 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5239 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5240 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5241 | struct cgroup *c = link->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5242 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 5243 | cgroup_name(c, name_buf, NAME_MAX + 1); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5244 | seq_printf(seq, "Root %d group %s\n", |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 5245 | c->root->hierarchy_id, name_buf); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5246 | } |
| 5247 | rcu_read_unlock(); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5248 | up_read(&css_set_rwsem); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5249 | kfree(name_buf); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5250 | return 0; |
| 5251 | } |
| 5252 | |
| 5253 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5254 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5255 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5256 | struct cgroup_subsys_state *css = seq_css(seq); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5257 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5258 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5259 | down_read(&css_set_rwsem); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5260 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5261 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5262 | struct task_struct *task; |
| 5263 | int count = 0; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 5264 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5265 | seq_printf(seq, "css_set %p\n", cset); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 5266 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5267 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 5268 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 5269 | goto overflow; |
| 5270 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5271 | } |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 5272 | |
| 5273 | list_for_each_entry(task, &cset->mg_tasks, cg_list) { |
| 5274 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 5275 | goto overflow; |
| 5276 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
| 5277 | } |
| 5278 | continue; |
| 5279 | overflow: |
| 5280 | seq_puts(seq, " ...\n"); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5281 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 5282 | up_read(&css_set_rwsem); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5283 | return 0; |
| 5284 | } |
| 5285 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5286 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5287 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5288 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5289 | } |
| 5290 | |
| 5291 | static struct cftype debug_files[] = { |
| 5292 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5293 | .name = "taskcount", |
| 5294 | .read_u64 = debug_taskcount_read, |
| 5295 | }, |
| 5296 | |
| 5297 | { |
| 5298 | .name = "current_css_set", |
| 5299 | .read_u64 = current_css_set_read, |
| 5300 | }, |
| 5301 | |
| 5302 | { |
| 5303 | .name = "current_css_set_refcount", |
| 5304 | .read_u64 = current_css_set_refcount_read, |
| 5305 | }, |
| 5306 | |
| 5307 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5308 | .name = "current_css_set_cg_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5309 | .seq_show = current_css_set_cg_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5310 | }, |
| 5311 | |
| 5312 | { |
| 5313 | .name = "cgroup_css_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5314 | .seq_show = cgroup_css_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5315 | }, |
| 5316 | |
| 5317 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5318 | .name = "releasable", |
| 5319 | .read_u64 = releasable_read, |
| 5320 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5321 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5322 | { } /* terminate */ |
| 5323 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5324 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5325 | struct cgroup_subsys debug_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5326 | .css_alloc = debug_css_alloc, |
| 5327 | .css_free = debug_css_free, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5328 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5329 | }; |
| 5330 | #endif /* CONFIG_CGROUP_DEBUG */ |