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 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 31 | #include "cgroup-internal.h" |
| 32 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 33 | #include <linux/cred.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> |
Jianyu Zhan | c9482a5 | 2014-04-26 15:40:28 +0800 | [diff] [blame] | 37 | #include <linux/magic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 38 | #include <linux/mutex.h> |
| 39 | #include <linux/mount.h> |
| 40 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 41 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 42 | #include <linux/rcupdate.h> |
| 43 | #include <linux/sched.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 44 | #include <linux/sched/task.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 | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 47 | #include <linux/percpu-rwsem.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 48 | #include <linux/string.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 49 | #include <linux/hashtable.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 50 | #include <linux/idr.h> |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 51 | #include <linux/kthread.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 52 | #include <linux/atomic.h> |
Tejun Heo | e93ad19 | 2016-01-19 12:18:41 -0500 | [diff] [blame] | 53 | #include <linux/cpuset.h> |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 54 | #include <linux/proc_ns.h> |
| 55 | #include <linux/nsproxy.h> |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 56 | #include <linux/file.h> |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 57 | #include <linux/sched/cputime.h> |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 58 | #include <net/sock.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 59 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 60 | #define CREATE_TRACE_POINTS |
| 61 | #include <trace/events/cgroup.h> |
| 62 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 63 | #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ |
| 64 | MAX_CFTYPE_NAME + 2) |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 65 | /* let's not notify more than 100 times per second */ |
| 66 | #define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100) |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 67 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 68 | /* |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 69 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 70 | * hierarchy must be performed while holding it. |
| 71 | * |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 72 | * css_set_lock protects task->cgroups pointer, the list of css_set |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 73 | * objects, and the chain of tasks off each css_set. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 74 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 75 | * These locks are exported if CONFIG_PROVE_RCU so that accessors in |
| 76 | * cgroup.h can use them for lockdep annotations. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 77 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 78 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 79 | DEFINE_SPINLOCK(css_set_lock); |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 80 | |
| 81 | #ifdef CONFIG_PROVE_RCU |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 82 | EXPORT_SYMBOL_GPL(cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 83 | EXPORT_SYMBOL_GPL(css_set_lock); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 84 | #endif |
| 85 | |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame^] | 86 | DEFINE_SPINLOCK(trace_cgroup_path_lock); |
| 87 | char trace_cgroup_path[TRACE_CGROUP_PATH_LEN]; |
| 88 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 89 | /* |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 90 | * Protects cgroup_idr and css_idr so that IDs can be released without |
| 91 | * grabbing cgroup_mutex. |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 92 | */ |
| 93 | static DEFINE_SPINLOCK(cgroup_idr_lock); |
| 94 | |
| 95 | /* |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 96 | * Protects cgroup_file->kn for !self csses. It synchronizes notifications |
| 97 | * against file removal/re-creation across css hiding. |
| 98 | */ |
| 99 | static DEFINE_SPINLOCK(cgroup_file_kn_lock); |
| 100 | |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 101 | struct percpu_rw_semaphore cgroup_threadgroup_rwsem; |
| 102 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 103 | #define cgroup_assert_mutex_or_rcu_locked() \ |
Paul E. McKenney | f78f5b9 | 2015-06-18 15:50:02 -0700 | [diff] [blame] | 104 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ |
| 105 | !lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 106 | "cgroup_mutex or RCU read lock required"); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 107 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 108 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 109 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 110 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 111 | * destruction work items don't end up filling up max_active of system_wq |
| 112 | * which may lead to deadlock. |
| 113 | */ |
| 114 | static struct workqueue_struct *cgroup_destroy_wq; |
| 115 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 116 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 117 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 118 | struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 119 | #include <linux/cgroup_subsys.h> |
| 120 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 121 | #undef SUBSYS |
| 122 | |
| 123 | /* array of cgroup subsystem names */ |
| 124 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 125 | static const char *cgroup_subsys_name[] = { |
| 126 | #include <linux/cgroup_subsys.h> |
| 127 | }; |
| 128 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 129 | |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 130 | /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */ |
| 131 | #define SUBSYS(_x) \ |
| 132 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \ |
| 133 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \ |
| 134 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \ |
| 135 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key); |
| 136 | #include <linux/cgroup_subsys.h> |
| 137 | #undef SUBSYS |
| 138 | |
| 139 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key, |
| 140 | static struct static_key_true *cgroup_subsys_enabled_key[] = { |
| 141 | #include <linux/cgroup_subsys.h> |
| 142 | }; |
| 143 | #undef SUBSYS |
| 144 | |
| 145 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key, |
| 146 | static struct static_key_true *cgroup_subsys_on_dfl_key[] = { |
| 147 | #include <linux/cgroup_subsys.h> |
| 148 | }; |
| 149 | #undef SUBSYS |
| 150 | |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 151 | static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 152 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 153 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 154 | * The default hierarchy, reserved for the subsystems that are otherwise |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 155 | * unattached - it never has more than a single cgroup, and all tasks are |
| 156 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 157 | */ |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 158 | struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu }; |
Tejun Heo | d0ec423 | 2015-08-05 16:03:19 -0400 | [diff] [blame] | 159 | EXPORT_SYMBOL_GPL(cgrp_dfl_root); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 160 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 161 | /* |
| 162 | * The default hierarchy always exists but is hidden until mounted for the |
| 163 | * first time. This is for backward compatibility. |
| 164 | */ |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 165 | static bool cgrp_dfl_visible; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 166 | |
Tejun Heo | 5533e01 | 2014-05-14 19:33:07 -0400 | [diff] [blame] | 167 | /* some controllers are not supported in the default hierarchy */ |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 168 | static u16 cgrp_dfl_inhibit_ss_mask; |
Tejun Heo | 5533e01 | 2014-05-14 19:33:07 -0400 | [diff] [blame] | 169 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 170 | /* some controllers are implicitly enabled on the default hierarchy */ |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 171 | static u16 cgrp_dfl_implicit_ss_mask; |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 172 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 173 | /* some controllers can be threaded on the default hierarchy */ |
| 174 | static u16 cgrp_dfl_threaded_ss_mask; |
| 175 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 176 | /* The list of hierarchy roots */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 177 | LIST_HEAD(cgroup_roots); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 178 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 179 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 180 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 181 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 182 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 183 | /* |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 184 | * Assign a monotonically increasing serial number to csses. It guarantees |
| 185 | * cgroups with bigger numbers are newer than those with smaller numbers. |
| 186 | * Also, as csses are always appended to the parent's ->children list, it |
| 187 | * guarantees that sibling csses are always sorted in the ascending serial |
| 188 | * number order on the list. Protected by cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 189 | */ |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 190 | static u64 css_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 191 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 192 | /* |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 193 | * These bitmasks identify subsystems with specific features to avoid |
| 194 | * having to do iterative checks repeatedly. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 195 | */ |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 196 | static u16 have_fork_callback __read_mostly; |
| 197 | static u16 have_exit_callback __read_mostly; |
| 198 | static u16 have_free_callback __read_mostly; |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 199 | static u16 have_canfork_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 200 | |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 201 | /* cgroup namespace for init task */ |
| 202 | struct cgroup_namespace init_cgroup_ns = { |
Elena Reshetova | 387ad96 | 2017-02-20 12:19:00 +0200 | [diff] [blame] | 203 | .count = REFCOUNT_INIT(2), |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 204 | .user_ns = &init_user_ns, |
| 205 | .ns.ops = &cgroupns_operations, |
| 206 | .ns.inum = PROC_CGROUP_INIT_INO, |
| 207 | .root_cset = &init_css_set, |
| 208 | }; |
| 209 | |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 210 | static struct file_system_type cgroup2_fs_type; |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 211 | static struct cftype cgroup_base_files[]; |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 212 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 213 | static int cgroup_apply_control(struct cgroup *cgrp); |
| 214 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 215 | static void css_task_iter_advance(struct css_task_iter *it); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 216 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 217 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 218 | struct cgroup_subsys *ss); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 219 | static void css_release(struct percpu_ref *ref); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 220 | static void kill_css(struct cgroup_subsys_state *css); |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 221 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 222 | struct cgroup *cgrp, struct cftype cfts[], |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 223 | bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 224 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 225 | /** |
| 226 | * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID |
| 227 | * @ssid: subsys ID of interest |
| 228 | * |
| 229 | * cgroup_subsys_enabled() can only be used with literal subsys names which |
| 230 | * is fine for individual subsystems but unsuitable for cgroup core. This |
| 231 | * is slower static_key_enabled() based test indexed by @ssid. |
| 232 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 233 | bool cgroup_ssid_enabled(int ssid) |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 234 | { |
Arnd Bergmann | cfe02a8 | 2016-03-15 00:21:06 +0100 | [diff] [blame] | 235 | if (CGROUP_SUBSYS_COUNT == 0) |
| 236 | return false; |
| 237 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 238 | return static_key_enabled(cgroup_subsys_enabled_key[ssid]); |
| 239 | } |
| 240 | |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 241 | /** |
| 242 | * cgroup_on_dfl - test whether a cgroup is on the default hierarchy |
| 243 | * @cgrp: the cgroup of interest |
| 244 | * |
| 245 | * The default hierarchy is the v2 interface of cgroup and this function |
| 246 | * can be used to test whether a cgroup is on the default hierarchy for |
| 247 | * cases where a subsystem should behave differnetly depending on the |
| 248 | * interface version. |
| 249 | * |
| 250 | * The set of behaviors which change on the default hierarchy are still |
| 251 | * being determined and the mount option is prefixed with __DEVEL__. |
| 252 | * |
| 253 | * List of changed behaviors: |
| 254 | * |
| 255 | * - Mount options "noprefix", "xattr", "clone_children", "release_agent" |
| 256 | * and "name" are disallowed. |
| 257 | * |
| 258 | * - When mounting an existing superblock, mount options should match. |
| 259 | * |
| 260 | * - Remount is disallowed. |
| 261 | * |
| 262 | * - rename(2) is disallowed. |
| 263 | * |
| 264 | * - "tasks" is removed. Everything should be at process granularity. Use |
| 265 | * "cgroup.procs" instead. |
| 266 | * |
| 267 | * - "cgroup.procs" is not sorted. pids will be unique unless they got |
| 268 | * recycled inbetween reads. |
| 269 | * |
| 270 | * - "release_agent" and "notify_on_release" are removed. Replacement |
| 271 | * notification mechanism will be implemented. |
| 272 | * |
| 273 | * - "cgroup.clone_children" is removed. |
| 274 | * |
| 275 | * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup |
| 276 | * and its descendants contain no task; otherwise, 1. The file also |
| 277 | * generates kernfs notification which can be monitored through poll and |
| 278 | * [di]notify when the value of the file changes. |
| 279 | * |
| 280 | * - cpuset: tasks will be kept in empty cpusets when hotplug happens and |
| 281 | * take masks of ancestors with non-empty cpus/mems, instead of being |
| 282 | * moved to an ancestor. |
| 283 | * |
| 284 | * - cpuset: a task can be moved into an empty cpuset, and again it takes |
| 285 | * masks of ancestors. |
| 286 | * |
| 287 | * - memcg: use_hierarchy is on by default and the cgroup file for the flag |
| 288 | * is not created. |
| 289 | * |
| 290 | * - blkcg: blk-throttle becomes properly hierarchical. |
| 291 | * |
| 292 | * - debug: disallowed on the default hierarchy. |
| 293 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 294 | bool cgroup_on_dfl(const struct cgroup *cgrp) |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 295 | { |
| 296 | return cgrp->root == &cgrp_dfl_root; |
| 297 | } |
| 298 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 299 | /* IDR wrappers which synchronize using cgroup_idr_lock */ |
| 300 | static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end, |
| 301 | gfp_t gfp_mask) |
| 302 | { |
| 303 | int ret; |
| 304 | |
| 305 | idr_preload(gfp_mask); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 306 | spin_lock_bh(&cgroup_idr_lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 307 | ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 308 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 309 | idr_preload_end(); |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id) |
| 314 | { |
| 315 | void *ret; |
| 316 | |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 317 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 318 | ret = idr_replace(idr, ptr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 319 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | static void cgroup_idr_remove(struct idr *idr, int id) |
| 324 | { |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 325 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 326 | idr_remove(idr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 327 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 330 | static bool cgroup_has_tasks(struct cgroup *cgrp) |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 331 | { |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 332 | return cgrp->nr_populated_csets; |
| 333 | } |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 334 | |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 335 | bool cgroup_is_threaded(struct cgroup *cgrp) |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 336 | { |
| 337 | return cgrp->dom_cgrp != cgrp; |
| 338 | } |
| 339 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 340 | /* can @cgrp host both domain and threaded children? */ |
| 341 | static bool cgroup_is_mixable(struct cgroup *cgrp) |
| 342 | { |
| 343 | /* |
| 344 | * Root isn't under domain level resource control exempting it from |
| 345 | * the no-internal-process constraint, so it can serve as a thread |
| 346 | * root and a parent of resource domains at the same time. |
| 347 | */ |
| 348 | return !cgroup_parent(cgrp); |
| 349 | } |
| 350 | |
| 351 | /* can @cgrp become a thread root? should always be true for a thread root */ |
| 352 | static bool cgroup_can_be_thread_root(struct cgroup *cgrp) |
| 353 | { |
| 354 | /* mixables don't care */ |
| 355 | if (cgroup_is_mixable(cgrp)) |
| 356 | return true; |
| 357 | |
| 358 | /* domain roots can't be nested under threaded */ |
| 359 | if (cgroup_is_threaded(cgrp)) |
| 360 | return false; |
| 361 | |
| 362 | /* can only have either domain or threaded children */ |
| 363 | if (cgrp->nr_populated_domain_children) |
| 364 | return false; |
| 365 | |
| 366 | /* and no domain controllers can be enabled */ |
| 367 | if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) |
| 368 | return false; |
| 369 | |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | /* is @cgrp root of a threaded subtree? */ |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 374 | bool cgroup_is_thread_root(struct cgroup *cgrp) |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 375 | { |
| 376 | /* thread root should be a domain */ |
| 377 | if (cgroup_is_threaded(cgrp)) |
| 378 | return false; |
| 379 | |
| 380 | /* a domain w/ threaded children is a thread root */ |
| 381 | if (cgrp->nr_threaded_children) |
| 382 | return true; |
| 383 | |
| 384 | /* |
| 385 | * A domain which has tasks and explicit threaded controllers |
| 386 | * enabled is a thread root. |
| 387 | */ |
| 388 | if (cgroup_has_tasks(cgrp) && |
| 389 | (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask)) |
| 390 | return true; |
| 391 | |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | /* a domain which isn't connected to the root w/o brekage can't be used */ |
| 396 | static bool cgroup_is_valid_domain(struct cgroup *cgrp) |
| 397 | { |
| 398 | /* the cgroup itself can be a thread root */ |
| 399 | if (cgroup_is_threaded(cgrp)) |
| 400 | return false; |
| 401 | |
| 402 | /* but the ancestors can't be unless mixable */ |
| 403 | while ((cgrp = cgroup_parent(cgrp))) { |
| 404 | if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp)) |
| 405 | return false; |
| 406 | if (cgroup_is_threaded(cgrp)) |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | return true; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 411 | } |
| 412 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 413 | /* subsystems visibly enabled on a cgroup */ |
| 414 | static u16 cgroup_control(struct cgroup *cgrp) |
| 415 | { |
| 416 | struct cgroup *parent = cgroup_parent(cgrp); |
| 417 | u16 root_ss_mask = cgrp->root->subsys_mask; |
| 418 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 419 | if (parent) { |
| 420 | u16 ss_mask = parent->subtree_control; |
| 421 | |
| 422 | /* threaded cgroups can only have threaded controllers */ |
| 423 | if (cgroup_is_threaded(cgrp)) |
| 424 | ss_mask &= cgrp_dfl_threaded_ss_mask; |
| 425 | return ss_mask; |
| 426 | } |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 427 | |
| 428 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 429 | root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask | |
| 430 | cgrp_dfl_implicit_ss_mask); |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 431 | return root_ss_mask; |
| 432 | } |
| 433 | |
| 434 | /* subsystems enabled on a cgroup */ |
| 435 | static u16 cgroup_ss_mask(struct cgroup *cgrp) |
| 436 | { |
| 437 | struct cgroup *parent = cgroup_parent(cgrp); |
| 438 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 439 | if (parent) { |
| 440 | u16 ss_mask = parent->subtree_ss_mask; |
| 441 | |
| 442 | /* threaded cgroups can only have threaded controllers */ |
| 443 | if (cgroup_is_threaded(cgrp)) |
| 444 | ss_mask &= cgrp_dfl_threaded_ss_mask; |
| 445 | return ss_mask; |
| 446 | } |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 447 | |
| 448 | return cgrp->root->subsys_mask; |
| 449 | } |
| 450 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 451 | /** |
| 452 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 453 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 454 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 455 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 456 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 457 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 458 | * the caller is responsible for pinning the returned css if it wants to |
| 459 | * keep accessing it outside the said locks. This function may return |
| 460 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 461 | */ |
| 462 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 463 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 464 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 465 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 466 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 467 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 468 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 469 | return &cgrp->self; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 470 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 471 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 472 | /** |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 473 | * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem |
| 474 | * @cgrp: the cgroup of interest |
| 475 | * @ss: the subsystem of interest |
| 476 | * |
| 477 | * Find and get @cgrp's css assocaited with @ss. If the css doesn't exist |
| 478 | * or is offline, %NULL is returned. |
| 479 | */ |
| 480 | static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp, |
| 481 | struct cgroup_subsys *ss) |
| 482 | { |
| 483 | struct cgroup_subsys_state *css; |
| 484 | |
| 485 | rcu_read_lock(); |
| 486 | css = cgroup_css(cgrp, ss); |
| 487 | if (!css || !css_tryget_online(css)) |
| 488 | css = NULL; |
| 489 | rcu_read_unlock(); |
| 490 | |
| 491 | return css; |
| 492 | } |
| 493 | |
| 494 | /** |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 495 | * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem |
| 496 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 497 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 498 | * |
Chen Hanxiao | d0f702e | 2015-04-23 07:57:33 -0400 | [diff] [blame] | 499 | * Similar to cgroup_css() but returns the effective css, which is defined |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 500 | * as the matching css of the nearest ancestor including self which has @ss |
| 501 | * enabled. If @ss is associated with the hierarchy @cgrp is on, this |
| 502 | * function is guaranteed to return non-NULL css. |
| 503 | */ |
| 504 | static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp, |
| 505 | struct cgroup_subsys *ss) |
| 506 | { |
| 507 | lockdep_assert_held(&cgroup_mutex); |
| 508 | |
| 509 | if (!ss) |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 510 | return &cgrp->self; |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 511 | |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 512 | /* |
| 513 | * This function is used while updating css associations and thus |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 514 | * can't test the csses directly. Test ss_mask. |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 515 | */ |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 516 | while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) { |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 517 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 518 | if (!cgrp) |
| 519 | return NULL; |
| 520 | } |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 521 | |
| 522 | return cgroup_css(cgrp, ss); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 525 | /** |
| 526 | * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem |
| 527 | * @cgrp: the cgroup of interest |
| 528 | * @ss: the subsystem of interest |
| 529 | * |
| 530 | * Find and get the effective css of @cgrp for @ss. The effective css is |
| 531 | * defined as the matching css of the nearest ancestor including self which |
| 532 | * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, |
| 533 | * the root css is returned, so this function always returns a valid css. |
| 534 | * The returned css must be put using css_put(). |
| 535 | */ |
| 536 | struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp, |
| 537 | struct cgroup_subsys *ss) |
| 538 | { |
| 539 | struct cgroup_subsys_state *css; |
| 540 | |
| 541 | rcu_read_lock(); |
| 542 | |
| 543 | do { |
| 544 | css = cgroup_css(cgrp, ss); |
| 545 | |
| 546 | if (css && css_tryget_online(css)) |
| 547 | goto out_unlock; |
| 548 | cgrp = cgroup_parent(cgrp); |
| 549 | } while (cgrp); |
| 550 | |
| 551 | css = init_css_set.subsys[ss->id]; |
| 552 | css_get(css); |
| 553 | out_unlock: |
| 554 | rcu_read_unlock(); |
| 555 | return css; |
| 556 | } |
| 557 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 558 | static void cgroup_get_live(struct cgroup *cgrp) |
Tejun Heo | 052c3f3 | 2015-10-15 16:41:50 -0400 | [diff] [blame] | 559 | { |
| 560 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
| 561 | css_get(&cgrp->self); |
| 562 | } |
| 563 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 564 | struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 565 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 566 | struct cgroup *cgrp = of->kn->parent->priv; |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 567 | struct cftype *cft = of_cft(of); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 568 | |
| 569 | /* |
| 570 | * This is open and unprotected implementation of cgroup_css(). |
| 571 | * seq_css() is only called from a kernfs file operation which has |
| 572 | * an active reference on the file. Because all the subsystem |
| 573 | * files are drained before a css is disassociated with a cgroup, |
| 574 | * the matching css from the cgroup's subsys table is guaranteed to |
| 575 | * be and stay valid until the enclosing operation is complete. |
| 576 | */ |
| 577 | if (cft->ss) |
| 578 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 579 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 580 | return &cgrp->self; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 581 | } |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 582 | EXPORT_SYMBOL_GPL(of_css); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 583 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 584 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 585 | * for_each_css - iterate all css's of a cgroup |
| 586 | * @css: the iteration cursor |
| 587 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 588 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 589 | * |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 590 | * Should be called under cgroup_[tree_]mutex. |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 591 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 592 | #define for_each_css(css, ssid, cgrp) \ |
| 593 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 594 | if (!((css) = rcu_dereference_check( \ |
| 595 | (cgrp)->subsys[(ssid)], \ |
| 596 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 597 | else |
| 598 | |
| 599 | /** |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 600 | * for_each_e_css - iterate all effective css's of a cgroup |
| 601 | * @css: the iteration cursor |
| 602 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 603 | * @cgrp: the target cgroup to iterate css's of |
| 604 | * |
| 605 | * Should be called under cgroup_[tree_]mutex. |
| 606 | */ |
| 607 | #define for_each_e_css(css, ssid, cgrp) \ |
| 608 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 609 | if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \ |
| 610 | ; \ |
| 611 | else |
| 612 | |
| 613 | /** |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 614 | * do_each_subsys_mask - filter for_each_subsys with a bitmask |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 615 | * @ss: the iteration cursor |
| 616 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 617 | * @ss_mask: the bitmask |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 618 | * |
| 619 | * The block will only run for cases where the ssid-th bit (1 << ssid) of |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 620 | * @ss_mask is set. |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 621 | */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 622 | #define do_each_subsys_mask(ss, ssid, ss_mask) do { \ |
| 623 | unsigned long __ss_mask = (ss_mask); \ |
| 624 | if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \ |
Aleksa Sarai | 4a705c5 | 2015-06-09 21:32:07 +1000 | [diff] [blame] | 625 | (ssid) = 0; \ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 626 | break; \ |
| 627 | } \ |
| 628 | for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \ |
| 629 | (ss) = cgroup_subsys[ssid]; \ |
| 630 | { |
| 631 | |
| 632 | #define while_each_subsys_mask() \ |
| 633 | } \ |
| 634 | } \ |
| 635 | } while (false) |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 636 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 637 | /* iterate over child cgrps, lock should be held throughout iteration */ |
| 638 | #define cgroup_for_each_live_child(child, cgrp) \ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 639 | list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 640 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 641 | cgroup_is_dead(child); })) \ |
| 642 | ; \ |
| 643 | else |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 644 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 645 | /* walk live descendants in preorder */ |
| 646 | #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \ |
| 647 | css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \ |
| 648 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 649 | (dsct) = (d_css)->cgroup; \ |
| 650 | cgroup_is_dead(dsct); })) \ |
| 651 | ; \ |
| 652 | else |
| 653 | |
| 654 | /* walk live descendants in postorder */ |
| 655 | #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \ |
| 656 | css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \ |
| 657 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 658 | (dsct) = (d_css)->cgroup; \ |
| 659 | cgroup_is_dead(dsct); })) \ |
| 660 | ; \ |
| 661 | else |
| 662 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 663 | /* |
| 664 | * 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] | 665 | * hierarchies being mounted. It contains a pointer to the root state |
| 666 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 667 | * reference-counted, to improve performance when child cgroups |
| 668 | * haven't been created. |
| 669 | */ |
Tejun Heo | 5024ae2 | 2014-05-07 21:31:17 -0400 | [diff] [blame] | 670 | struct css_set init_css_set = { |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 671 | .refcount = REFCOUNT_INIT(1), |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 672 | .dom_cset = &init_css_set, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 673 | .tasks = LIST_HEAD_INIT(init_css_set.tasks), |
| 674 | .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 675 | .task_iters = LIST_HEAD_INIT(init_css_set.task_iters), |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 676 | .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets), |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 677 | .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 678 | .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node), |
| 679 | .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), |
Tejun Heo | 3868314 | 2017-09-25 13:50:20 -0700 | [diff] [blame] | 680 | |
| 681 | /* |
| 682 | * The following field is re-initialized when this cset gets linked |
| 683 | * in cgroup_init(). However, let's initialize the field |
| 684 | * statically too so that the default cgroup can be accessed safely |
| 685 | * early during boot. |
| 686 | */ |
| 687 | .dfl_cgrp = &cgrp_dfl_root.cgrp, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 688 | }; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 689 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 690 | static int css_set_count = 1; /* 1 for init_css_set */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 691 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 692 | static bool css_set_threaded(struct css_set *cset) |
| 693 | { |
| 694 | return cset->dom_cset != cset; |
| 695 | } |
| 696 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 697 | /** |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 698 | * css_set_populated - does a css_set contain any tasks? |
| 699 | * @cset: target css_set |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 700 | * |
| 701 | * css_set_populated() should be the same as !!cset->nr_tasks at steady |
| 702 | * state. However, css_set_populated() can be called while a task is being |
| 703 | * added to or removed from the linked list before the nr_tasks is |
| 704 | * properly updated. Hence, we can't just look at ->nr_tasks here. |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 705 | */ |
| 706 | static bool css_set_populated(struct css_set *cset) |
| 707 | { |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 708 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 709 | |
| 710 | return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks); |
| 711 | } |
| 712 | |
| 713 | /** |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 714 | * cgroup_update_populated - update the populated count of a cgroup |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 715 | * @cgrp: the target cgroup |
| 716 | * @populated: inc or dec populated count |
| 717 | * |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 718 | * One of the css_sets associated with @cgrp is either getting its first |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 719 | * task or losing the last. Update @cgrp->nr_populated_* accordingly. The |
| 720 | * count is propagated towards root so that a given cgroup's |
| 721 | * nr_populated_children is zero iff none of its descendants contain any |
| 722 | * tasks. |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 723 | * |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 724 | * @cgrp's interface file "cgroup.populated" is zero if both |
| 725 | * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and |
| 726 | * 1 otherwise. When the sum changes from or to zero, userland is notified |
| 727 | * that the content of the interface file has changed. This can be used to |
| 728 | * detect when @cgrp and its descendants become populated or empty. |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 729 | */ |
| 730 | static void cgroup_update_populated(struct cgroup *cgrp, bool populated) |
| 731 | { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 732 | struct cgroup *child = NULL; |
| 733 | int adj = populated ? 1 : -1; |
| 734 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 735 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 736 | |
| 737 | do { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 738 | bool was_populated = cgroup_is_populated(cgrp); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 739 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 740 | if (!child) { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 741 | cgrp->nr_populated_csets += adj; |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 742 | } else { |
| 743 | if (cgroup_is_threaded(child)) |
| 744 | cgrp->nr_populated_threaded_children += adj; |
| 745 | else |
| 746 | cgrp->nr_populated_domain_children += adj; |
| 747 | } |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 748 | |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 749 | if (was_populated == cgroup_is_populated(cgrp)) |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 750 | break; |
| 751 | |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 752 | cgroup1_check_for_release(cgrp); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 753 | cgroup_file_notify(&cgrp->events_file); |
| 754 | |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 755 | child = cgrp; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 756 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 757 | } while (cgrp); |
| 758 | } |
| 759 | |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 760 | /** |
| 761 | * css_set_update_populated - update populated state of a css_set |
| 762 | * @cset: target css_set |
| 763 | * @populated: whether @cset is populated or depopulated |
| 764 | * |
| 765 | * @cset is either getting the first task or losing the last. Update the |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 766 | * populated counters of all associated cgroups accordingly. |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 767 | */ |
| 768 | static void css_set_update_populated(struct css_set *cset, bool populated) |
| 769 | { |
| 770 | struct cgrp_cset_link *link; |
| 771 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 772 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 773 | |
| 774 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) |
| 775 | cgroup_update_populated(link->cgrp, populated); |
| 776 | } |
| 777 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 778 | /** |
| 779 | * css_set_move_task - move a task from one css_set to another |
| 780 | * @task: task being moved |
| 781 | * @from_cset: css_set @task currently belongs to (may be NULL) |
| 782 | * @to_cset: new css_set @task is being moved to (may be NULL) |
| 783 | * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks |
| 784 | * |
| 785 | * Move @task from @from_cset to @to_cset. If @task didn't belong to any |
| 786 | * css_set, @from_cset can be NULL. If @task is being disassociated |
| 787 | * instead of moved, @to_cset can be NULL. |
| 788 | * |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 789 | * This function automatically handles populated counter updates and |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 790 | * css_task_iter adjustments but the caller is responsible for managing |
| 791 | * @from_cset and @to_cset's reference counts. |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 792 | */ |
| 793 | static void css_set_move_task(struct task_struct *task, |
| 794 | struct css_set *from_cset, struct css_set *to_cset, |
| 795 | bool use_mg_tasks) |
| 796 | { |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 797 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 798 | |
Tejun Heo | 20b454a | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 799 | if (to_cset && !css_set_populated(to_cset)) |
| 800 | css_set_update_populated(to_cset, true); |
| 801 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 802 | if (from_cset) { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 803 | struct css_task_iter *it, *pos; |
| 804 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 805 | WARN_ON_ONCE(list_empty(&task->cg_list)); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 806 | |
| 807 | /* |
| 808 | * @task is leaving, advance task iterators which are |
| 809 | * pointing to it so that they can resume at the next |
| 810 | * position. Advancing an iterator might remove it from |
| 811 | * the list, use safe walk. See css_task_iter_advance*() |
| 812 | * for details. |
| 813 | */ |
| 814 | list_for_each_entry_safe(it, pos, &from_cset->task_iters, |
| 815 | iters_node) |
| 816 | if (it->task_pos == &task->cg_list) |
| 817 | css_task_iter_advance(it); |
| 818 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 819 | list_del_init(&task->cg_list); |
| 820 | if (!css_set_populated(from_cset)) |
| 821 | css_set_update_populated(from_cset, false); |
| 822 | } else { |
| 823 | WARN_ON_ONCE(!list_empty(&task->cg_list)); |
| 824 | } |
| 825 | |
| 826 | if (to_cset) { |
| 827 | /* |
| 828 | * We are synchronized through cgroup_threadgroup_rwsem |
| 829 | * against PF_EXITING setting such that we can't race |
| 830 | * against cgroup_exit() changing the css_set to |
| 831 | * init_css_set and dropping the old one. |
| 832 | */ |
| 833 | WARN_ON_ONCE(task->flags & PF_EXITING); |
| 834 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 835 | rcu_assign_pointer(task->cgroups, to_cset); |
| 836 | list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks : |
| 837 | &to_cset->tasks); |
| 838 | } |
| 839 | } |
| 840 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 841 | /* |
| 842 | * hash table for cgroup groups. This improves the performance to find |
| 843 | * an existing css_set. This hash doesn't (currently) take into |
| 844 | * account cgroups in empty hierarchies. |
| 845 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 846 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 847 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 848 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 849 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 850 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 851 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 852 | struct cgroup_subsys *ss; |
| 853 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 854 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 855 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 856 | key += (unsigned long)css[i]; |
| 857 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 858 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 859 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Tejun Heo | dcfe149 | 2016-12-27 14:49:09 -0500 | [diff] [blame] | 862 | void put_css_set_locked(struct css_set *cset) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 863 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 864 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 865 | struct cgroup_subsys *ss; |
| 866 | int ssid; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 867 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 868 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 869 | |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 870 | if (!refcount_dec_and_test(&cset->refcount)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 871 | return; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 872 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 873 | WARN_ON_ONCE(!list_empty(&cset->threaded_csets)); |
| 874 | |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 875 | /* This css_set is dead. unlink it and release cgroup and css refs */ |
| 876 | for_each_subsys(ss, ssid) { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 877 | list_del(&cset->e_cset_node[ssid]); |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 878 | css_put(cset->subsys[ssid]); |
| 879 | } |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 880 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 881 | css_set_count--; |
| 882 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 883 | list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 884 | list_del(&link->cset_link); |
| 885 | list_del(&link->cgrp_link); |
Tejun Heo | 2ceb231 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 886 | if (cgroup_parent(link->cgrp)) |
| 887 | cgroup_put(link->cgrp); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 888 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 889 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 890 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 891 | if (css_set_threaded(cset)) { |
| 892 | list_del(&cset->threaded_csets_node); |
| 893 | put_css_set_locked(cset->dom_cset); |
| 894 | } |
| 895 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 896 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 899 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 900 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 901 | * @cset: candidate css_set being tested |
| 902 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 903 | * @new_cgrp: cgroup that's being entered by the task |
| 904 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 905 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 906 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 907 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 908 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 909 | static bool compare_css_sets(struct css_set *cset, |
| 910 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 911 | struct cgroup *new_cgrp, |
| 912 | struct cgroup_subsys_state *template[]) |
| 913 | { |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 914 | struct cgroup *new_dfl_cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 915 | struct list_head *l1, *l2; |
| 916 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 917 | /* |
| 918 | * On the default hierarchy, there can be csets which are |
| 919 | * associated with the same set of cgroups but different csses. |
| 920 | * Let's first ensure that csses match. |
| 921 | */ |
| 922 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 923 | return false; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 924 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 925 | |
| 926 | /* @cset's domain should match the default cgroup's */ |
| 927 | if (cgroup_on_dfl(new_cgrp)) |
| 928 | new_dfl_cgrp = new_cgrp; |
| 929 | else |
| 930 | new_dfl_cgrp = old_cset->dfl_cgrp; |
| 931 | |
| 932 | if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp) |
| 933 | return false; |
| 934 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 935 | /* |
| 936 | * Compare cgroup pointers in order to distinguish between |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 937 | * different cgroups in hierarchies. As different cgroups may |
| 938 | * share the same effective css, this comparison is always |
| 939 | * necessary. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 940 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 941 | l1 = &cset->cgrp_links; |
| 942 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 943 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 944 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 945 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 946 | |
| 947 | l1 = l1->next; |
| 948 | l2 = l2->next; |
| 949 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 950 | if (l1 == &cset->cgrp_links) { |
| 951 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 952 | break; |
| 953 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 954 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 955 | } |
| 956 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 957 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 958 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 959 | cgrp1 = link1->cgrp; |
| 960 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 961 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 962 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 963 | |
| 964 | /* |
| 965 | * If this hierarchy is the hierarchy of the cgroup |
| 966 | * that's changing, then we need to check that this |
| 967 | * css_set points to the new cgroup; if it's any other |
| 968 | * hierarchy, then this css_set should point to the |
| 969 | * same cgroup as the old css_set. |
| 970 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 971 | if (cgrp1->root == new_cgrp->root) { |
| 972 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 973 | return false; |
| 974 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 975 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 976 | return false; |
| 977 | } |
| 978 | } |
| 979 | return true; |
| 980 | } |
| 981 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 982 | /** |
| 983 | * find_existing_css_set - init css array and find the matching css_set |
| 984 | * @old_cset: the css_set that we're using before the cgroup transition |
| 985 | * @cgrp: the cgroup that we're moving into |
| 986 | * @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] | 987 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 988 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 989 | struct cgroup *cgrp, |
| 990 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 991 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 992 | struct cgroup_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 993 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 994 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 995 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 996 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 997 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 998 | /* |
| 999 | * Build the set of subsystem state objects that we want to see in the |
| 1000 | * new css_set. while subsystems can change globally, the entries here |
| 1001 | * won't change, so no need for locking. |
| 1002 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1003 | for_each_subsys(ss, i) { |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1004 | if (root->subsys_mask & (1UL << i)) { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1005 | /* |
| 1006 | * @ss is in this hierarchy, so we want the |
| 1007 | * effective css from @cgrp. |
| 1008 | */ |
| 1009 | template[i] = cgroup_e_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1010 | } else { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1011 | /* |
| 1012 | * @ss is not in this hierarchy, so we don't want |
| 1013 | * to change the css. |
| 1014 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1015 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1019 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1020 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 1021 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1022 | continue; |
| 1023 | |
| 1024 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1025 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1026 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | /* No existing cgroup group matched */ |
| 1029 | return NULL; |
| 1030 | } |
| 1031 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1032 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1033 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1034 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1035 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1036 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 1037 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1038 | kfree(link); |
| 1039 | } |
| 1040 | } |
| 1041 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1042 | /** |
| 1043 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 1044 | * @count: the number of links to allocate |
| 1045 | * @tmp_links: list_head the allocated links are put on |
| 1046 | * |
| 1047 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 1048 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1049 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1050 | 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] | 1051 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1052 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1053 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1054 | |
| 1055 | INIT_LIST_HEAD(tmp_links); |
| 1056 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1057 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 1058 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1059 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1060 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1061 | return -ENOMEM; |
| 1062 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1063 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1064 | } |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1068 | /** |
| 1069 | * 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] | 1070 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1071 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1072 | * @cgrp: the destination cgroup |
| 1073 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1074 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 1075 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1076 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1077 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1078 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1079 | BUG_ON(list_empty(tmp_links)); |
Tejun Heo | 6803c00 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1080 | |
| 1081 | if (cgroup_on_dfl(cgrp)) |
| 1082 | cset->dfl_cgrp = cgrp; |
| 1083 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1084 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 1085 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1086 | link->cgrp = cgrp; |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 1087 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1088 | /* |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1089 | * Always add links to the tail of the lists so that the lists are |
| 1090 | * in choronological order. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1091 | */ |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1092 | list_move_tail(&link->cset_link, &cgrp->cset_links); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1093 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Tejun Heo | 2ceb231 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1094 | |
| 1095 | if (cgroup_parent(cgrp)) |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 1096 | cgroup_get_live(cgrp); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1099 | /** |
| 1100 | * find_css_set - return a new css_set with one cgroup updated |
| 1101 | * @old_cset: the baseline css_set |
| 1102 | * @cgrp: the cgroup to be updated |
| 1103 | * |
| 1104 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 1105 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1106 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1107 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 1108 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1109 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1110 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1111 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1112 | struct list_head tmp_links; |
| 1113 | struct cgrp_cset_link *link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1114 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1115 | unsigned long key; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1116 | int ssid; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1117 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1118 | lockdep_assert_held(&cgroup_mutex); |
| 1119 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1120 | /* First see if we already have a cgroup group that matches |
| 1121 | * the desired set */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1122 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1123 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 1124 | if (cset) |
| 1125 | get_css_set(cset); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1126 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1127 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1128 | if (cset) |
| 1129 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1130 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 1131 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1132 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1133 | return NULL; |
| 1134 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1135 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1136 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1137 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1138 | return NULL; |
| 1139 | } |
| 1140 | |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 1141 | refcount_set(&cset->refcount, 1); |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1142 | cset->dom_cset = cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1143 | INIT_LIST_HEAD(&cset->tasks); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1144 | INIT_LIST_HEAD(&cset->mg_tasks); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 1145 | INIT_LIST_HEAD(&cset->task_iters); |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1146 | INIT_LIST_HEAD(&cset->threaded_csets); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1147 | INIT_HLIST_NODE(&cset->hlist); |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 1148 | INIT_LIST_HEAD(&cset->cgrp_links); |
| 1149 | INIT_LIST_HEAD(&cset->mg_preload_node); |
| 1150 | INIT_LIST_HEAD(&cset->mg_node); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1151 | |
| 1152 | /* Copy the set of subsystem state objects generated in |
| 1153 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1154 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1155 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1156 | spin_lock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1157 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1158 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1159 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1160 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1161 | if (c->root == cgrp->root) |
| 1162 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1163 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1164 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1165 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1166 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1167 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1168 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1169 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1170 | /* Add @cset to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1171 | key = css_set_hash(cset->subsys); |
| 1172 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1173 | |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 1174 | for_each_subsys(ss, ssid) { |
| 1175 | struct cgroup_subsys_state *css = cset->subsys[ssid]; |
| 1176 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1177 | list_add_tail(&cset->e_cset_node[ssid], |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 1178 | &css->cgroup->e_csets[ssid]); |
| 1179 | css_get(css); |
| 1180 | } |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1181 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1182 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1183 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1184 | /* |
| 1185 | * If @cset should be threaded, look up the matching dom_cset and |
| 1186 | * link them up. We first fully initialize @cset then look for the |
| 1187 | * dom_cset. It's simpler this way and safe as @cset is guaranteed |
| 1188 | * to stay empty until we return. |
| 1189 | */ |
| 1190 | if (cgroup_is_threaded(cset->dfl_cgrp)) { |
| 1191 | struct css_set *dcset; |
| 1192 | |
| 1193 | dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp); |
| 1194 | if (!dcset) { |
| 1195 | put_css_set(cset); |
| 1196 | return NULL; |
| 1197 | } |
| 1198 | |
| 1199 | spin_lock_irq(&css_set_lock); |
| 1200 | cset->dom_cset = dcset; |
| 1201 | list_add_tail(&cset->threaded_csets_node, |
| 1202 | &dcset->threaded_csets); |
| 1203 | spin_unlock_irq(&css_set_lock); |
| 1204 | } |
| 1205 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1206 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1209 | struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1210 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1211 | struct cgroup *root_cgrp = kf_root->kn->priv; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1212 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1213 | return root_cgrp->root; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1214 | } |
| 1215 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1216 | static int cgroup_init_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1217 | { |
| 1218 | int id; |
| 1219 | |
| 1220 | lockdep_assert_held(&cgroup_mutex); |
| 1221 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1222 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1223 | if (id < 0) |
| 1224 | return id; |
| 1225 | |
| 1226 | root->hierarchy_id = id; |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1230 | static void cgroup_exit_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1231 | { |
| 1232 | lockdep_assert_held(&cgroup_mutex); |
| 1233 | |
Johannes Weiner | 8c8a550 | 2016-06-17 12:23:59 -0400 | [diff] [blame] | 1234 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1235 | } |
| 1236 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1237 | void cgroup_free_root(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1238 | { |
| 1239 | if (root) { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1240 | idr_destroy(&root->cgroup_idr); |
| 1241 | kfree(root); |
| 1242 | } |
| 1243 | } |
| 1244 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1245 | static void cgroup_destroy_root(struct cgroup_root *root) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1246 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1247 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1248 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1249 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 1250 | trace_cgroup_destroy_root(root); |
| 1251 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1252 | cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1253 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1254 | BUG_ON(atomic_read(&root->nr_cgrps)); |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1255 | BUG_ON(!list_empty(&cgrp->self.children)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1256 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1257 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1258 | WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1259 | |
| 1260 | /* |
| 1261 | * Release all the links from cset_links to this hierarchy's |
| 1262 | * root cgroup |
| 1263 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1264 | spin_lock_irq(&css_set_lock); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1265 | |
| 1266 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1267 | list_del(&link->cset_link); |
| 1268 | list_del(&link->cgrp_link); |
| 1269 | kfree(link); |
| 1270 | } |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1271 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1272 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1273 | |
| 1274 | if (!list_empty(&root->root_list)) { |
| 1275 | list_del(&root->root_list); |
| 1276 | cgroup_root_count--; |
| 1277 | } |
| 1278 | |
| 1279 | cgroup_exit_root_id(root); |
| 1280 | |
| 1281 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1282 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1283 | kernfs_destroy_root(root->kf_root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1284 | cgroup_free_root(root); |
| 1285 | } |
| 1286 | |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1287 | /* |
| 1288 | * look up cgroup associated with current task's cgroup namespace on the |
| 1289 | * specified hierarchy |
| 1290 | */ |
| 1291 | static struct cgroup * |
| 1292 | current_cgns_cgroup_from_root(struct cgroup_root *root) |
| 1293 | { |
| 1294 | struct cgroup *res = NULL; |
| 1295 | struct css_set *cset; |
| 1296 | |
| 1297 | lockdep_assert_held(&css_set_lock); |
| 1298 | |
| 1299 | rcu_read_lock(); |
| 1300 | |
| 1301 | cset = current->nsproxy->cgroup_ns->root_cset; |
| 1302 | if (cset == &init_css_set) { |
| 1303 | res = &root->cgrp; |
| 1304 | } else { |
| 1305 | struct cgrp_cset_link *link; |
| 1306 | |
| 1307 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
| 1308 | struct cgroup *c = link->cgrp; |
| 1309 | |
| 1310 | if (c->root == root) { |
| 1311 | res = c; |
| 1312 | break; |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | rcu_read_unlock(); |
| 1317 | |
| 1318 | BUG_ON(!res); |
| 1319 | return res; |
| 1320 | } |
| 1321 | |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1322 | /* look up cgroup associated with given css_set on the specified hierarchy */ |
| 1323 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1324 | struct cgroup_root *root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1325 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1326 | struct cgroup *res = NULL; |
| 1327 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1328 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1329 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1330 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1331 | if (cset == &init_css_set) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1332 | res = &root->cgrp; |
Tejun Heo | 13d82fb | 2017-08-02 15:39:38 -0700 | [diff] [blame] | 1333 | } else if (root == &cgrp_dfl_root) { |
| 1334 | res = cset->dfl_cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1335 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1336 | struct cgrp_cset_link *link; |
| 1337 | |
| 1338 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1339 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1340 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1341 | if (c->root == root) { |
| 1342 | res = c; |
| 1343 | break; |
| 1344 | } |
| 1345 | } |
| 1346 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1347 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1348 | BUG_ON(!res); |
| 1349 | return res; |
| 1350 | } |
| 1351 | |
| 1352 | /* |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1353 | * Return the cgroup for "task" from the given hierarchy. Must be |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1354 | * called with cgroup_mutex and css_set_lock held. |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1355 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1356 | struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 1357 | struct cgroup_root *root) |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1358 | { |
| 1359 | /* |
| 1360 | * No need to lock the task - since we hold cgroup_mutex the |
| 1361 | * task can't change groups, so the only thing that can happen |
| 1362 | * is that it exits and its css is set back to init_css_set. |
| 1363 | */ |
| 1364 | return cset_cgroup_from_root(task_css_set(task), root); |
| 1365 | } |
| 1366 | |
| 1367 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1368 | * A task must hold cgroup_mutex to modify cgroups. |
| 1369 | * |
| 1370 | * Any task can increment and decrement the count field without lock. |
| 1371 | * So in general, code holding cgroup_mutex can't rely on the count |
| 1372 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1373 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1374 | * means that no tasks are currently attached, therefore there is no |
| 1375 | * way a task attached to that cgroup can fork (the other way to |
| 1376 | * increment the count). So code holding cgroup_mutex can safely |
| 1377 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 1378 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 1379 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 1380 | * needs that mutex. |
| 1381 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1382 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 1383 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 1384 | * 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] | 1385 | * least one task in the system (init, pid == 1), therefore, root cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1386 | * 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] | 1387 | * need a special hack to ensure that root cgroup cannot be deleted. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1388 | * |
| 1389 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1390 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1391 | */ |
| 1392 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1393 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 1394 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1395 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 1396 | char *buf) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1397 | { |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 1398 | struct cgroup_subsys *ss = cft->ss; |
| 1399 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1400 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 1401 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) |
| 1402 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s", |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 1403 | cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name, |
| 1404 | cft->name); |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1405 | else |
Tejun Heo | 08a7767 | 2018-01-09 07:21:15 -0800 | [diff] [blame] | 1406 | strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1407 | return buf; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1410 | /** |
| 1411 | * cgroup_file_mode - deduce file mode of a control file |
| 1412 | * @cft: the control file in question |
| 1413 | * |
Tejun Heo | 7dbdb19 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1414 | * S_IRUGO for read, S_IWUSR for write. |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1415 | */ |
| 1416 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1417 | { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1418 | umode_t mode = 0; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1419 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1420 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 1421 | mode |= S_IRUGO; |
| 1422 | |
Tejun Heo | 7dbdb19 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1423 | if (cft->write_u64 || cft->write_s64 || cft->write) { |
| 1424 | if (cft->flags & CFTYPE_WORLD_WRITABLE) |
| 1425 | mode |= S_IWUGO; |
| 1426 | else |
| 1427 | mode |= S_IWUSR; |
| 1428 | } |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1429 | |
| 1430 | return mode; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1431 | } |
| 1432 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1433 | /** |
Tejun Heo | 8699b77 | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1434 | * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1435 | * @subtree_control: the new subtree_control mask to consider |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1436 | * @this_ss_mask: available subsystems |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1437 | * |
| 1438 | * On the default hierarchy, a subsystem may request other subsystems to be |
| 1439 | * enabled together through its ->depends_on mask. In such cases, more |
| 1440 | * subsystems than specified in "cgroup.subtree_control" may be enabled. |
| 1441 | * |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1442 | * This function calculates which subsystems need to be enabled if |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1443 | * @subtree_control is to be applied while restricted to @this_ss_mask. |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1444 | */ |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1445 | static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask) |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 1446 | { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 1447 | u16 cur_ss_mask = subtree_control; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1448 | struct cgroup_subsys *ss; |
| 1449 | int ssid; |
| 1450 | |
| 1451 | lockdep_assert_held(&cgroup_mutex); |
| 1452 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 1453 | cur_ss_mask |= cgrp_dfl_implicit_ss_mask; |
| 1454 | |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1455 | while (true) { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 1456 | u16 new_ss_mask = cur_ss_mask; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1457 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1458 | do_each_subsys_mask(ss, ssid, cur_ss_mask) { |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 1459 | new_ss_mask |= ss->depends_on; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1460 | } while_each_subsys_mask(); |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1461 | |
| 1462 | /* |
| 1463 | * Mask out subsystems which aren't available. This can |
| 1464 | * happen only if some depended-upon subsystems were bound |
| 1465 | * to non-default hierarchies. |
| 1466 | */ |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1467 | new_ss_mask &= this_ss_mask; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1468 | |
| 1469 | if (new_ss_mask == cur_ss_mask) |
| 1470 | break; |
| 1471 | cur_ss_mask = new_ss_mask; |
| 1472 | } |
| 1473 | |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1474 | return cur_ss_mask; |
| 1475 | } |
| 1476 | |
| 1477 | /** |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1478 | * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods |
| 1479 | * @kn: the kernfs_node being serviced |
| 1480 | * |
| 1481 | * This helper undoes cgroup_kn_lock_live() and should be invoked before |
| 1482 | * the method finishes if locking succeeded. Note that once this function |
| 1483 | * returns the cgroup returned by cgroup_kn_lock_live() may become |
| 1484 | * inaccessible any time. If the caller intends to continue to access the |
| 1485 | * cgroup, it should pin it before invoking this function. |
| 1486 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1487 | void cgroup_kn_unlock(struct kernfs_node *kn) |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1488 | { |
| 1489 | struct cgroup *cgrp; |
| 1490 | |
| 1491 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1492 | cgrp = kn->priv; |
| 1493 | else |
| 1494 | cgrp = kn->parent->priv; |
| 1495 | |
| 1496 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1497 | |
| 1498 | kernfs_unbreak_active_protection(kn); |
| 1499 | cgroup_put(cgrp); |
| 1500 | } |
| 1501 | |
| 1502 | /** |
| 1503 | * cgroup_kn_lock_live - locking helper for cgroup kernfs methods |
| 1504 | * @kn: the kernfs_node being serviced |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1505 | * @drain_offline: perform offline draining on the cgroup |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1506 | * |
| 1507 | * This helper is to be used by a cgroup kernfs method currently servicing |
| 1508 | * @kn. It breaks the active protection, performs cgroup locking and |
| 1509 | * verifies that the associated cgroup is alive. Returns the cgroup if |
| 1510 | * alive; otherwise, %NULL. A successful return should be undone by a |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1511 | * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the |
| 1512 | * cgroup is drained of offlining csses before return. |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1513 | * |
| 1514 | * Any cgroup kernfs method implementation which requires locking the |
| 1515 | * associated cgroup should use this helper. It avoids nesting cgroup |
| 1516 | * locking under kernfs active protection and allows all kernfs operations |
| 1517 | * including self-removal. |
| 1518 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1519 | struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline) |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1520 | { |
| 1521 | struct cgroup *cgrp; |
| 1522 | |
| 1523 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1524 | cgrp = kn->priv; |
| 1525 | else |
| 1526 | cgrp = kn->parent->priv; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1527 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1528 | /* |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1529 | * We're gonna grab cgroup_mutex which nests outside kernfs |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1530 | * active_ref. cgroup liveliness check alone provides enough |
| 1531 | * protection against removal. Ensure @cgrp stays accessible and |
| 1532 | * break the active_ref protection. |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1533 | */ |
Li Zefan | aa32362 | 2014-09-04 14:43:38 +0800 | [diff] [blame] | 1534 | if (!cgroup_tryget(cgrp)) |
| 1535 | return NULL; |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1536 | kernfs_break_active_protection(kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1537 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1538 | if (drain_offline) |
| 1539 | cgroup_lock_and_drain_offline(cgrp); |
| 1540 | else |
| 1541 | mutex_lock(&cgroup_mutex); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1542 | |
| 1543 | if (!cgroup_is_dead(cgrp)) |
| 1544 | return cgrp; |
| 1545 | |
| 1546 | cgroup_kn_unlock(kn); |
| 1547 | return NULL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 1550 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1551 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1552 | char name[CGROUP_FILE_NAME_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1553 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1554 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 1555 | |
| 1556 | if (cft->file_offset) { |
| 1557 | struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss); |
| 1558 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 1559 | |
| 1560 | spin_lock_irq(&cgroup_file_kn_lock); |
| 1561 | cfile->kn = NULL; |
| 1562 | spin_unlock_irq(&cgroup_file_kn_lock); |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1563 | |
| 1564 | del_timer_sync(&cfile->notify_timer); |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 1565 | } |
| 1566 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1567 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1570 | /** |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1571 | * css_clear_dir - remove subsys files in a cgroup directory |
| 1572 | * @css: taget css |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1573 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1574 | static void css_clear_dir(struct cgroup_subsys_state *css) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1575 | { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1576 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1577 | struct cftype *cfts; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1578 | |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1579 | if (!(css->flags & CSS_VISIBLE)) |
| 1580 | return; |
| 1581 | |
| 1582 | css->flags &= ~CSS_VISIBLE; |
| 1583 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1584 | if (!css->ss) { |
| 1585 | if (cgroup_on_dfl(cgrp)) |
| 1586 | cfts = cgroup_base_files; |
| 1587 | else |
| 1588 | cfts = cgroup1_base_files; |
| 1589 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1590 | cgroup_addrm_files(css, cgrp, cfts, false); |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1591 | } else { |
| 1592 | list_for_each_entry(cfts, &css->ss->cfts, node) |
| 1593 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1594 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1597 | /** |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1598 | * css_populate_dir - create subsys files in a cgroup directory |
| 1599 | * @css: target css |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1600 | * |
| 1601 | * On failure, no file is added. |
| 1602 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1603 | static int css_populate_dir(struct cgroup_subsys_state *css) |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1604 | { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1605 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1606 | struct cftype *cfts, *failed_cfts; |
| 1607 | int ret; |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1608 | |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1609 | if ((css->flags & CSS_VISIBLE) || !cgrp->kn) |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1610 | return 0; |
| 1611 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1612 | if (!css->ss) { |
| 1613 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1614 | cfts = cgroup_base_files; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1615 | else |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1616 | cfts = cgroup1_base_files; |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1617 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1618 | ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true); |
| 1619 | if (ret < 0) |
| 1620 | return ret; |
| 1621 | } else { |
| 1622 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1623 | ret = cgroup_addrm_files(css, cgrp, cfts, true); |
| 1624 | if (ret < 0) { |
| 1625 | failed_cfts = cfts; |
| 1626 | goto err; |
| 1627 | } |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1628 | } |
| 1629 | } |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1630 | |
| 1631 | css->flags |= CSS_VISIBLE; |
| 1632 | |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1633 | return 0; |
| 1634 | err: |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1635 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1636 | if (cfts == failed_cfts) |
| 1637 | break; |
| 1638 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1639 | } |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1640 | return ret; |
| 1641 | } |
| 1642 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1643 | int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1644 | { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1645 | struct cgroup *dcgrp = &dst_root->cgrp; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1646 | struct cgroup_subsys *ss; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1647 | int ssid, i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1648 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1649 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1650 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1651 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 1652 | /* |
| 1653 | * If @ss has non-root csses attached to it, can't move. |
| 1654 | * If @ss is an implicit controller, it is exempt from this |
| 1655 | * rule and can be stolen. |
| 1656 | */ |
| 1657 | if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) && |
| 1658 | !ss->implicit_on_dfl) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1659 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1660 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1661 | /* can't move between two non-dummy roots either */ |
Tejun Heo | 7fd8c56 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1662 | if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1663 | return -EBUSY; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1664 | } while_each_subsys_mask(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1665 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1666 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1667 | struct cgroup_root *src_root = ss->root; |
| 1668 | struct cgroup *scgrp = &src_root->cgrp; |
| 1669 | struct cgroup_subsys_state *css = cgroup_css(scgrp, ss); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1670 | struct css_set *cset; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1671 | |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1672 | WARN_ON(!css || cgroup_css(dcgrp, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1673 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1674 | /* disable from the source */ |
| 1675 | src_root->subsys_mask &= ~(1 << ssid); |
| 1676 | WARN_ON(cgroup_apply_control(scgrp)); |
| 1677 | cgroup_finalize_control(scgrp, 0); |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1678 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1679 | /* rebind */ |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1680 | RCU_INIT_POINTER(scgrp->subsys[ssid], NULL); |
| 1681 | rcu_assign_pointer(dcgrp->subsys[ssid], css); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1682 | ss->root = dst_root; |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1683 | css->cgroup = dcgrp; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1684 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1685 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1686 | hash_for_each(css_set_table, i, cset, hlist) |
| 1687 | list_move_tail(&cset->e_cset_node[ss->id], |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1688 | &dcgrp->e_csets[ss->id]); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1689 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1690 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1691 | /* default hierarchy doesn't enable controllers by default */ |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1692 | dst_root->subsys_mask |= 1 << ssid; |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1693 | if (dst_root == &cgrp_dfl_root) { |
| 1694 | static_branch_enable(cgroup_subsys_on_dfl_key[ssid]); |
| 1695 | } else { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1696 | dcgrp->subtree_control |= 1 << ssid; |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1697 | static_branch_disable(cgroup_subsys_on_dfl_key[ssid]); |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 1698 | } |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1699 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1700 | ret = cgroup_apply_control(dcgrp); |
| 1701 | if (ret) |
| 1702 | pr_warn("partial failure to rebind %s controller (err=%d)\n", |
| 1703 | ss->name, ret); |
| 1704 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1705 | if (ss->bind) |
| 1706 | ss->bind(css); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1707 | } while_each_subsys_mask(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1708 | |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1709 | kernfs_activate(dcgrp->kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1710 | return 0; |
| 1711 | } |
| 1712 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1713 | int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node, |
| 1714 | struct kernfs_root *kf_root) |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1715 | { |
Felipe Balbi | 09be4c8 | 2016-05-12 12:34:38 +0300 | [diff] [blame] | 1716 | int len = 0; |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1717 | char *buf = NULL; |
| 1718 | struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root); |
| 1719 | struct cgroup *ns_cgroup; |
| 1720 | |
| 1721 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 1722 | if (!buf) |
| 1723 | return -ENOMEM; |
| 1724 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1725 | spin_lock_irq(&css_set_lock); |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1726 | ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot); |
| 1727 | len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1728 | spin_unlock_irq(&css_set_lock); |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1729 | |
| 1730 | if (len >= PATH_MAX) |
| 1731 | len = -ERANGE; |
| 1732 | else if (len > 0) { |
| 1733 | seq_escape(sf, buf, " \t\n\\"); |
| 1734 | len = 0; |
| 1735 | } |
| 1736 | kfree(buf); |
| 1737 | return len; |
| 1738 | } |
| 1739 | |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1740 | static int parse_cgroup_root_flags(char *data, unsigned int *root_flags) |
| 1741 | { |
| 1742 | char *token; |
| 1743 | |
| 1744 | *root_flags = 0; |
| 1745 | |
| 1746 | if (!data) |
| 1747 | return 0; |
| 1748 | |
| 1749 | while ((token = strsep(&data, ",")) != NULL) { |
| 1750 | if (!strcmp(token, "nsdelegate")) { |
| 1751 | *root_flags |= CGRP_ROOT_NS_DELEGATE; |
| 1752 | continue; |
| 1753 | } |
| 1754 | |
| 1755 | pr_err("cgroup2: unknown option \"%s\"\n", token); |
| 1756 | return -EINVAL; |
| 1757 | } |
| 1758 | |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
| 1762 | static void apply_cgroup_root_flags(unsigned int root_flags) |
| 1763 | { |
| 1764 | if (current->nsproxy->cgroup_ns == &init_cgroup_ns) { |
| 1765 | if (root_flags & CGRP_ROOT_NS_DELEGATE) |
| 1766 | cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE; |
| 1767 | else |
| 1768 | cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE; |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root) |
| 1773 | { |
| 1774 | if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) |
| 1775 | seq_puts(seq, ",nsdelegate"); |
| 1776 | return 0; |
| 1777 | } |
| 1778 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1779 | 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] | 1780 | { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1781 | unsigned int root_flags; |
| 1782 | int ret; |
| 1783 | |
| 1784 | ret = parse_cgroup_root_flags(data, &root_flags); |
| 1785 | if (ret) |
| 1786 | return ret; |
| 1787 | |
| 1788 | apply_cgroup_root_flags(root_flags); |
| 1789 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1792 | /* |
| 1793 | * To reduce the fork() overhead for systems that are not actually using |
| 1794 | * their cgroups capability, we don't maintain the lists running through |
| 1795 | * each css_set to its tasks until we see the list actually used - in other |
| 1796 | * words after the first mount. |
| 1797 | */ |
| 1798 | static bool use_task_css_set_links __read_mostly; |
| 1799 | |
| 1800 | static void cgroup_enable_task_cg_lists(void) |
| 1801 | { |
| 1802 | struct task_struct *p, *g; |
| 1803 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1804 | /* |
| 1805 | * We need tasklist_lock because RCU is not safe against |
| 1806 | * while_each_thread(). Besides, a forking task that has passed |
| 1807 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 1808 | * is not guaranteed to have its child immediately visible in the |
| 1809 | * tasklist if we walk through it with RCU. |
| 1810 | */ |
| 1811 | read_lock(&tasklist_lock); |
Tejun Heo | d8742e2 | 2018-05-23 11:04:54 -0700 | [diff] [blame] | 1812 | spin_lock_irq(&css_set_lock); |
| 1813 | |
| 1814 | if (use_task_css_set_links) |
| 1815 | goto out_unlock; |
| 1816 | |
| 1817 | use_task_css_set_links = true; |
| 1818 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1819 | do_each_thread(g, p) { |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1820 | WARN_ON_ONCE(!list_empty(&p->cg_list) || |
| 1821 | task_css_set(p) != &init_css_set); |
| 1822 | |
| 1823 | /* |
| 1824 | * We should check if the process is exiting, otherwise |
| 1825 | * it will race with cgroup_exit() in that the list |
| 1826 | * entry won't be deleted though the process has exited. |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1827 | * Do it while holding siglock so that we don't end up |
| 1828 | * racing against cgroup_exit(). |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1829 | * |
| 1830 | * Interrupts were already disabled while acquiring |
| 1831 | * the css_set_lock, so we do not need to disable it |
| 1832 | * again when acquiring the sighand->siglock here. |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1833 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1834 | spin_lock(&p->sighand->siglock); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1835 | if (!(p->flags & PF_EXITING)) { |
| 1836 | struct css_set *cset = task_css_set(p); |
| 1837 | |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 1838 | if (!css_set_populated(cset)) |
| 1839 | css_set_update_populated(cset, true); |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1840 | list_add_tail(&p->cg_list, &cset->tasks); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1841 | get_css_set(cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 1842 | cset->nr_tasks++; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1843 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1844 | spin_unlock(&p->sighand->siglock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1845 | } while_each_thread(g, p); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1846 | out_unlock: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1847 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | d8742e2 | 2018-05-23 11:04:54 -0700 | [diff] [blame] | 1848 | read_unlock(&tasklist_lock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1849 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1850 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1851 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1852 | { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1853 | struct cgroup_subsys *ss; |
| 1854 | int ssid; |
| 1855 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1856 | INIT_LIST_HEAD(&cgrp->self.sibling); |
| 1857 | INIT_LIST_HEAD(&cgrp->self.children); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1858 | INIT_LIST_HEAD(&cgrp->cset_links); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1859 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1860 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 1861 | cgrp->self.cgroup = cgrp; |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 1862 | cgrp->self.flags |= CSS_ONLINE; |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1863 | cgrp->dom_cgrp = cgrp; |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 1864 | cgrp->max_descendants = INT_MAX; |
| 1865 | cgrp->max_depth = INT_MAX; |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 1866 | INIT_LIST_HEAD(&cgrp->rstat_css_list); |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1867 | prev_cputime_init(&cgrp->prev_cputime); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1868 | |
| 1869 | for_each_subsys(ss, ssid) |
| 1870 | INIT_LIST_HEAD(&cgrp->e_csets[ssid]); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1871 | |
| 1872 | init_waitqueue_head(&cgrp->offline_waitq); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1873 | INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1874 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1875 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1876 | void init_cgroup_root(struct cgroup_root *root, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1877 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1878 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1879 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1880 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1881 | atomic_set(&root->nr_cgrps, 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1882 | cgrp->root = root; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1883 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1884 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1885 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1886 | root->flags = opts->flags; |
| 1887 | if (opts->release_agent) |
Tejun Heo | 08a7767 | 2018-01-09 07:21:15 -0800 | [diff] [blame] | 1888 | strscpy(root->release_agent_path, opts->release_agent, PATH_MAX); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1889 | if (opts->name) |
Tejun Heo | 08a7767 | 2018-01-09 07:21:15 -0800 | [diff] [blame] | 1890 | strscpy(root->name, opts->name, MAX_CGROUP_ROOT_NAMELEN); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1891 | if (opts->cpuset_clone_children) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1892 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
Zefan Li | 9732adc | 2017-04-19 10:15:59 +0800 | [diff] [blame] | 1895 | int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask, int ref_flags) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1896 | { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1897 | LIST_HEAD(tmp_links); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1898 | struct cgroup *root_cgrp = &root->cgrp; |
Tejun Heo | fa06990 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 1899 | struct kernfs_syscall_ops *kf_sops; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1900 | struct css_set *cset; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1901 | int i, ret; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1902 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1903 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1904 | |
Vladimir Davydov | cf780b7 | 2015-08-03 15:32:26 +0300 | [diff] [blame] | 1905 | ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1906 | if (ret < 0) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1907 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1908 | root_cgrp->id = ret; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 1909 | root_cgrp->ancestor_ids[0] = ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1910 | |
Zefan Li | 9732adc | 2017-04-19 10:15:59 +0800 | [diff] [blame] | 1911 | ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, |
| 1912 | ref_flags, GFP_KERNEL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1913 | if (ret) |
| 1914 | goto out; |
| 1915 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1916 | /* |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1917 | * We're accessing css_set_count without locking css_set_lock here, |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1918 | * but that's OK - it can only be increased by someone holding |
Tejun Heo | 0431359 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1919 | * cgroup_lock, and that's us. Later rebinding may disable |
| 1920 | * controllers on the default hierarchy and thus create new csets, |
| 1921 | * which can't be more than the existing ones. Allocate 2x. |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1922 | */ |
Tejun Heo | 0431359 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1923 | ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1924 | if (ret) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1925 | goto cancel_ref; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1926 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1927 | ret = cgroup_init_root_id(root); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1928 | if (ret) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1929 | goto cancel_ref; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1930 | |
Tejun Heo | fa06990 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 1931 | kf_sops = root == &cgrp_dfl_root ? |
| 1932 | &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops; |
| 1933 | |
| 1934 | root->kf_root = kernfs_create_root(kf_sops, |
Shaohua Li | aa81882 | 2017-07-12 11:49:51 -0700 | [diff] [blame] | 1935 | KERNFS_ROOT_CREATE_DEACTIVATED | |
| 1936 | KERNFS_ROOT_SUPPORT_EXPORTOP, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1937 | root_cgrp); |
| 1938 | if (IS_ERR(root->kf_root)) { |
| 1939 | ret = PTR_ERR(root->kf_root); |
| 1940 | goto exit_root_id; |
| 1941 | } |
| 1942 | root_cgrp->kn = root->kf_root->kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1943 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1944 | ret = css_populate_dir(&root_cgrp->self); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1945 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1946 | goto destroy_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1947 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1948 | ret = rebind_subsystems(root, ss_mask); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1949 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1950 | goto destroy_root; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1951 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1952 | ret = cgroup_bpf_inherit(root_cgrp); |
| 1953 | WARN_ON_ONCE(ret); |
| 1954 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 1955 | trace_cgroup_setup_root(root); |
| 1956 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1957 | /* |
| 1958 | * There must be no failure case after here, since rebinding takes |
| 1959 | * care of subsystems' refcounts, which are explicitly dropped in |
| 1960 | * the failure exit path. |
| 1961 | */ |
| 1962 | list_add(&root->root_list, &cgroup_roots); |
| 1963 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1964 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1965 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1966 | * Link the root cgroup in this hierarchy into all the css_set |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1967 | * objects. |
| 1968 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1969 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 1970 | hash_for_each(css_set_table, i, cset, hlist) { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1971 | link_css_set(&tmp_links, cset, root_cgrp); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 1972 | if (css_set_populated(cset)) |
| 1973 | cgroup_update_populated(root_cgrp, true); |
| 1974 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1975 | spin_unlock_irq(&css_set_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1976 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1977 | BUG_ON(!list_empty(&root_cgrp->self.children)); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1978 | BUG_ON(atomic_read(&root->nr_cgrps) != 1); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1979 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1980 | kernfs_activate(root_cgrp->kn); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1981 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1982 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1983 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1984 | destroy_root: |
| 1985 | kernfs_destroy_root(root->kf_root); |
| 1986 | root->kf_root = NULL; |
| 1987 | exit_root_id: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1988 | cgroup_exit_root_id(root); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1989 | cancel_ref: |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 1990 | percpu_ref_exit(&root_cgrp->self.refcnt); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1991 | out: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1992 | free_cgrp_cset_links(&tmp_links); |
| 1993 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1996 | struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags, |
| 1997 | struct cgroup_root *root, unsigned long magic, |
| 1998 | struct cgroup_namespace *ns) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1999 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2000 | struct dentry *dentry; |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 2001 | bool new_sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 2002 | |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2003 | dentry = kernfs_mount(fs_type, flags, root->kf_root, magic, &new_sb); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2004 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 2005 | /* |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2006 | * In non-init cgroup namespace, instead of root cgroup's dentry, |
| 2007 | * we return the dentry corresponding to the cgroupns->root_cgrp. |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2008 | */ |
| 2009 | if (!IS_ERR(dentry) && ns != &init_cgroup_ns) { |
| 2010 | struct dentry *nsdentry; |
| 2011 | struct cgroup *cgrp; |
| 2012 | |
| 2013 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2014 | spin_lock_irq(&css_set_lock); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2015 | |
| 2016 | cgrp = cset_cgroup_from_root(ns->root_cset, root); |
| 2017 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2018 | spin_unlock_irq(&css_set_lock); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2019 | mutex_unlock(&cgroup_mutex); |
| 2020 | |
| 2021 | nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb); |
| 2022 | dput(dentry); |
| 2023 | dentry = nsdentry; |
| 2024 | } |
| 2025 | |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 2026 | if (IS_ERR(dentry) || !new_sb) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2027 | cgroup_put(&root->cgrp); |
Li Zefan | 3a32bd7 | 2014-06-30 11:50:59 +0800 | [diff] [blame] | 2028 | |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2029 | return dentry; |
| 2030 | } |
| 2031 | |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2032 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
| 2033 | int flags, const char *unused_dev_name, |
| 2034 | void *data) |
| 2035 | { |
| 2036 | struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; |
| 2037 | struct dentry *dentry; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2038 | int ret; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2039 | |
| 2040 | get_cgroup_ns(ns); |
| 2041 | |
| 2042 | /* Check if the caller has permission to mount. */ |
| 2043 | if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) { |
| 2044 | put_cgroup_ns(ns); |
| 2045 | return ERR_PTR(-EPERM); |
| 2046 | } |
| 2047 | |
Li Zefan | 3a32bd7 | 2014-06-30 11:50:59 +0800 | [diff] [blame] | 2048 | /* |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2049 | * The first time anyone tries to mount a cgroup, enable the list |
| 2050 | * linking each css_set to its tasks and fix up all existing tasks. |
Li Zefan | 3a32bd7 | 2014-06-30 11:50:59 +0800 | [diff] [blame] | 2051 | */ |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2052 | if (!use_task_css_set_links) |
| 2053 | cgroup_enable_task_cg_lists(); |
| 2054 | |
| 2055 | if (fs_type == &cgroup2_fs_type) { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2056 | unsigned int root_flags; |
| 2057 | |
| 2058 | ret = parse_cgroup_root_flags(data, &root_flags); |
| 2059 | if (ret) { |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2060 | put_cgroup_ns(ns); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2061 | return ERR_PTR(ret); |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2062 | } |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2063 | |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2064 | cgrp_dfl_visible = true; |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 2065 | cgroup_get_live(&cgrp_dfl_root.cgrp); |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2066 | |
| 2067 | dentry = cgroup_do_mount(&cgroup2_fs_type, flags, &cgrp_dfl_root, |
| 2068 | CGROUP2_SUPER_MAGIC, ns); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2069 | if (!IS_ERR(dentry)) |
| 2070 | apply_cgroup_root_flags(root_flags); |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2071 | } else { |
| 2072 | dentry = cgroup1_mount(&cgroup_fs_type, flags, data, |
| 2073 | CGROUP_SUPER_MAGIC, ns); |
Li Zefan | 3a32bd7 | 2014-06-30 11:50:59 +0800 | [diff] [blame] | 2074 | } |
| 2075 | |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2076 | put_cgroup_ns(ns); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2077 | return dentry; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2080 | static void cgroup_kill_sb(struct super_block *sb) |
| 2081 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2082 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2083 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2084 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2085 | /* |
| 2086 | * If @root doesn't have any mounts or children, start killing it. |
| 2087 | * This prevents new mounts by disabling percpu_ref_tryget_live(). |
| 2088 | * cgroup_mount() may wait for @root's release. |
Li Zefan | 1f779fb | 2014-06-04 16:48:15 +0800 | [diff] [blame] | 2089 | * |
| 2090 | * And don't kill the default root. |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2091 | */ |
Johannes Weiner | 3c606d3 | 2015-01-22 10:19:43 -0500 | [diff] [blame] | 2092 | if (!list_empty(&root->cgrp.self.children) || |
Li Zefan | 1f779fb | 2014-06-04 16:48:15 +0800 | [diff] [blame] | 2093 | root == &cgrp_dfl_root) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2094 | cgroup_put(&root->cgrp); |
| 2095 | else |
| 2096 | percpu_ref_kill(&root->cgrp.self.refcnt); |
| 2097 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2098 | kernfs_kill_sb(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2101 | struct file_system_type cgroup_fs_type = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2102 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 2103 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2104 | .kill_sb = cgroup_kill_sb, |
Serge Hallyn | 1c53753 | 2016-01-29 02:54:11 -0600 | [diff] [blame] | 2105 | .fs_flags = FS_USERNS_MOUNT, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2106 | }; |
| 2107 | |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 2108 | static struct file_system_type cgroup2_fs_type = { |
| 2109 | .name = "cgroup2", |
| 2110 | .mount = cgroup_mount, |
| 2111 | .kill_sb = cgroup_kill_sb, |
Serge Hallyn | 1c53753 | 2016-01-29 02:54:11 -0600 | [diff] [blame] | 2112 | .fs_flags = FS_USERNS_MOUNT, |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 2113 | }; |
| 2114 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2115 | int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen, |
| 2116 | struct cgroup_namespace *ns) |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2117 | { |
| 2118 | struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2119 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2120 | return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2121 | } |
| 2122 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2123 | int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen, |
| 2124 | struct cgroup_namespace *ns) |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2125 | { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2126 | int ret; |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2127 | |
| 2128 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2129 | spin_lock_irq(&css_set_lock); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2130 | |
| 2131 | ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns); |
| 2132 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2133 | spin_unlock_irq(&css_set_lock); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2134 | mutex_unlock(&cgroup_mutex); |
| 2135 | |
| 2136 | return ret; |
| 2137 | } |
| 2138 | EXPORT_SYMBOL_GPL(cgroup_path_ns); |
| 2139 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2140 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2141 | * 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] | 2142 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2143 | * @buf: the buffer to write the path into |
| 2144 | * @buflen: the length of the buffer |
| 2145 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2146 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 2147 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 2148 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 2149 | * cgroup controller callbacks. |
| 2150 | * |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2151 | * Return value is the same as kernfs_path(). |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2152 | */ |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2153 | int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2154 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2155 | struct cgroup_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2156 | struct cgroup *cgrp; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2157 | int hierarchy_id = 1; |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2158 | int ret; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2159 | |
| 2160 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2161 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2162 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2163 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 2164 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2165 | if (root) { |
| 2166 | cgrp = task_cgroup_from_root(task, root); |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2167 | ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2168 | } else { |
| 2169 | /* if no hierarchy exists, everyone is in "/" */ |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2170 | ret = strlcpy(buf, "/", buflen); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2171 | } |
| 2172 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2173 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2174 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2175 | return ret; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2176 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2177 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2178 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2179 | /** |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2180 | * cgroup_migrate_add_task - add a migration target task to a migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2181 | * @task: target task |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2182 | * @mgctx: target migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2183 | * |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2184 | * Add @task, which is a migration target, to @mgctx->tset. This function |
| 2185 | * becomes noop if @task doesn't need to be migrated. @task's css_set |
| 2186 | * should have been added as a migration source and @task->cg_list will be |
| 2187 | * moved from the css_set's tasks list to mg_tasks one. |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2188 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2189 | static void cgroup_migrate_add_task(struct task_struct *task, |
| 2190 | struct cgroup_mgctx *mgctx) |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2191 | { |
| 2192 | struct css_set *cset; |
| 2193 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 2194 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2195 | |
| 2196 | /* @task either already exited or can't exit until the end */ |
| 2197 | if (task->flags & PF_EXITING) |
| 2198 | return; |
| 2199 | |
| 2200 | /* leave @task alone if post_fork() hasn't linked it yet */ |
| 2201 | if (list_empty(&task->cg_list)) |
| 2202 | return; |
| 2203 | |
| 2204 | cset = task_css_set(task); |
| 2205 | if (!cset->mg_src_cgrp) |
| 2206 | return; |
| 2207 | |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2208 | mgctx->tset.nr_tasks++; |
| 2209 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2210 | list_move_tail(&task->cg_list, &cset->mg_tasks); |
| 2211 | if (list_empty(&cset->mg_node)) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2212 | list_add_tail(&cset->mg_node, |
| 2213 | &mgctx->tset.src_csets); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2214 | if (list_empty(&cset->mg_dst_cset->mg_node)) |
Tejun Heo | d8ebf51 | 2017-01-15 19:03:40 -0500 | [diff] [blame] | 2215 | list_add_tail(&cset->mg_dst_cset->mg_node, |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2216 | &mgctx->tset.dst_csets); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2217 | } |
| 2218 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2219 | /** |
| 2220 | * cgroup_taskset_first - reset taskset and return the first task |
| 2221 | * @tset: taskset of interest |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2222 | * @dst_cssp: output variable for the destination css |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2223 | * |
| 2224 | * @tset iteration is initialized and the first task is returned. |
| 2225 | */ |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2226 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset, |
| 2227 | struct cgroup_subsys_state **dst_cssp) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2228 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2229 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); |
| 2230 | tset->cur_task = NULL; |
| 2231 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2232 | return cgroup_taskset_next(tset, dst_cssp); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2233 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2234 | |
| 2235 | /** |
| 2236 | * cgroup_taskset_next - iterate to the next task in taskset |
| 2237 | * @tset: taskset of interest |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2238 | * @dst_cssp: output variable for the destination css |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2239 | * |
| 2240 | * Return the next task in @tset. Iteration must have been initialized |
| 2241 | * with cgroup_taskset_first(). |
| 2242 | */ |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2243 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset, |
| 2244 | struct cgroup_subsys_state **dst_cssp) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2245 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2246 | struct css_set *cset = tset->cur_cset; |
| 2247 | struct task_struct *task = tset->cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2248 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2249 | while (&cset->mg_node != tset->csets) { |
| 2250 | if (!task) |
| 2251 | task = list_first_entry(&cset->mg_tasks, |
| 2252 | struct task_struct, cg_list); |
| 2253 | else |
| 2254 | task = list_next_entry(task, cg_list); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2255 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2256 | if (&task->cg_list != &cset->mg_tasks) { |
| 2257 | tset->cur_cset = cset; |
| 2258 | tset->cur_task = task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2259 | |
| 2260 | /* |
| 2261 | * This function may be called both before and |
| 2262 | * after cgroup_taskset_migrate(). The two cases |
| 2263 | * can be distinguished by looking at whether @cset |
| 2264 | * has its ->mg_dst_cset set. |
| 2265 | */ |
| 2266 | if (cset->mg_dst_cset) |
| 2267 | *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid]; |
| 2268 | else |
| 2269 | *dst_cssp = cset->subsys[tset->ssid]; |
| 2270 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2271 | return task; |
| 2272 | } |
| 2273 | |
| 2274 | cset = list_next_entry(cset, mg_node); |
| 2275 | task = NULL; |
| 2276 | } |
| 2277 | |
| 2278 | return NULL; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2279 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2280 | |
| 2281 | /** |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2282 | * cgroup_taskset_migrate - migrate a taskset |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2283 | * @mgctx: migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2284 | * |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2285 | * Migrate tasks in @mgctx as setup by migration preparation functions. |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2286 | * This function fails iff one of the ->can_attach callbacks fails and |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2287 | * guarantees that either all or none of the tasks in @mgctx are migrated. |
| 2288 | * @mgctx is consumed regardless of success. |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2289 | */ |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2290 | static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx) |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2291 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2292 | struct cgroup_taskset *tset = &mgctx->tset; |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2293 | struct cgroup_subsys *ss; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2294 | struct task_struct *task, *tmp_task; |
| 2295 | struct css_set *cset, *tmp_cset; |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2296 | int ssid, failed_ssid, ret; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2297 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2298 | /* check that we can legitimately attach to the cgroup */ |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2299 | if (tset->nr_tasks) { |
| 2300 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2301 | if (ss->can_attach) { |
| 2302 | tset->ssid = ssid; |
| 2303 | ret = ss->can_attach(tset); |
| 2304 | if (ret) { |
| 2305 | failed_ssid = ssid; |
| 2306 | goto out_cancel_attach; |
| 2307 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2308 | } |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2309 | } while_each_subsys_mask(); |
| 2310 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2311 | |
| 2312 | /* |
| 2313 | * Now that we're guaranteed success, proceed to move all tasks to |
| 2314 | * the new cgroup. There are no failure cases after here, so this |
| 2315 | * is the commit point. |
| 2316 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2317 | spin_lock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2318 | list_for_each_entry(cset, &tset->src_csets, mg_node) { |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2319 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) { |
| 2320 | struct css_set *from_cset = task_css_set(task); |
| 2321 | struct css_set *to_cset = cset->mg_dst_cset; |
| 2322 | |
| 2323 | get_css_set(to_cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 2324 | to_cset->nr_tasks++; |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2325 | css_set_move_task(task, from_cset, to_cset, true); |
| 2326 | put_css_set_locked(from_cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 2327 | from_cset->nr_tasks--; |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2328 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2329 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2330 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2331 | |
| 2332 | /* |
| 2333 | * Migration is committed, all target tasks are now on dst_csets. |
| 2334 | * Nothing is sensitive to fork() after this point. Notify |
| 2335 | * controllers that migration is complete. |
| 2336 | */ |
| 2337 | tset->csets = &tset->dst_csets; |
| 2338 | |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2339 | if (tset->nr_tasks) { |
| 2340 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2341 | if (ss->attach) { |
| 2342 | tset->ssid = ssid; |
| 2343 | ss->attach(tset); |
| 2344 | } |
| 2345 | } while_each_subsys_mask(); |
| 2346 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2347 | |
| 2348 | ret = 0; |
| 2349 | goto out_release_tset; |
| 2350 | |
| 2351 | out_cancel_attach: |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2352 | if (tset->nr_tasks) { |
| 2353 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2354 | if (ssid == failed_ssid) |
| 2355 | break; |
| 2356 | if (ss->cancel_attach) { |
| 2357 | tset->ssid = ssid; |
| 2358 | ss->cancel_attach(tset); |
| 2359 | } |
| 2360 | } while_each_subsys_mask(); |
| 2361 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2362 | out_release_tset: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2363 | spin_lock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2364 | list_splice_init(&tset->dst_csets, &tset->src_csets); |
| 2365 | list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) { |
| 2366 | list_splice_tail_init(&cset->mg_tasks, &cset->tasks); |
| 2367 | list_del_init(&cset->mg_node); |
| 2368 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2369 | spin_unlock_irq(&css_set_lock); |
Waiman Long | c4fa6c4 | 2017-09-21 09:54:13 -0400 | [diff] [blame] | 2370 | |
| 2371 | /* |
| 2372 | * Re-initialize the cgroup_taskset structure in case it is reused |
| 2373 | * again in another cgroup_migrate_add_task()/cgroup_migrate_execute() |
| 2374 | * iteration. |
| 2375 | */ |
| 2376 | tset->nr_tasks = 0; |
| 2377 | tset->csets = &tset->src_csets; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2378 | return ret; |
| 2379 | } |
| 2380 | |
| 2381 | /** |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2382 | * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2383 | * @dst_cgrp: destination cgroup to test |
| 2384 | * |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2385 | * On the default hierarchy, except for the mixable, (possible) thread root |
| 2386 | * and threaded cgroups, subtree_control must be zero for migration |
| 2387 | * destination cgroups with tasks so that child cgroups don't compete |
| 2388 | * against tasks. |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2389 | */ |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2390 | int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp) |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2391 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2392 | /* v1 doesn't have any restriction */ |
| 2393 | if (!cgroup_on_dfl(dst_cgrp)) |
| 2394 | return 0; |
| 2395 | |
| 2396 | /* verify @dst_cgrp can host resources */ |
| 2397 | if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp)) |
| 2398 | return -EOPNOTSUPP; |
| 2399 | |
| 2400 | /* mixables don't care */ |
| 2401 | if (cgroup_is_mixable(dst_cgrp)) |
| 2402 | return 0; |
| 2403 | |
| 2404 | /* |
| 2405 | * If @dst_cgrp is already or can become a thread root or is |
| 2406 | * threaded, it doesn't matter. |
| 2407 | */ |
| 2408 | if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp)) |
| 2409 | return 0; |
| 2410 | |
| 2411 | /* apply no-internal-process constraint */ |
| 2412 | if (dst_cgrp->subtree_control) |
| 2413 | return -EBUSY; |
| 2414 | |
| 2415 | return 0; |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | /** |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2419 | * cgroup_migrate_finish - cleanup after attach |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2420 | * @mgctx: migration context |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2421 | * |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2422 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See |
| 2423 | * those functions for details. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2424 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2425 | void cgroup_migrate_finish(struct cgroup_mgctx *mgctx) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2426 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2427 | LIST_HEAD(preloaded); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2428 | struct css_set *cset, *tmp_cset; |
| 2429 | |
| 2430 | lockdep_assert_held(&cgroup_mutex); |
| 2431 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2432 | spin_lock_irq(&css_set_lock); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2433 | |
| 2434 | list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded); |
| 2435 | list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded); |
| 2436 | |
| 2437 | list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2438 | cset->mg_src_cgrp = NULL; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2439 | cset->mg_dst_cgrp = NULL; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2440 | cset->mg_dst_cset = NULL; |
| 2441 | list_del_init(&cset->mg_preload_node); |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2442 | put_css_set_locked(cset); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2443 | } |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2444 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2445 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | /** |
| 2449 | * cgroup_migrate_add_src - add a migration source css_set |
| 2450 | * @src_cset: the source css_set to add |
| 2451 | * @dst_cgrp: the destination cgroup |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2452 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2453 | * |
| 2454 | * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2455 | * @src_cset and add it to @mgctx->src_csets, which should later be cleaned |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2456 | * up by cgroup_migrate_finish(). |
| 2457 | * |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 2458 | * This function may be called without holding cgroup_threadgroup_rwsem |
| 2459 | * even if the target is a process. Threads may be created and destroyed |
| 2460 | * but as long as cgroup_mutex is not dropped, no new css_set can be put |
| 2461 | * into play and the preloaded css_sets are guaranteed to cover all |
| 2462 | * migrations. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2463 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2464 | void cgroup_migrate_add_src(struct css_set *src_cset, |
| 2465 | struct cgroup *dst_cgrp, |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2466 | struct cgroup_mgctx *mgctx) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2467 | { |
| 2468 | struct cgroup *src_cgrp; |
| 2469 | |
| 2470 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 2471 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2472 | |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 2473 | /* |
| 2474 | * If ->dead, @src_set is associated with one or more dead cgroups |
| 2475 | * and doesn't contain any migratable tasks. Ignore it early so |
| 2476 | * that the rest of migration path doesn't get confused by it. |
| 2477 | */ |
| 2478 | if (src_cset->dead) |
| 2479 | return; |
| 2480 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2481 | src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); |
| 2482 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2483 | if (!list_empty(&src_cset->mg_preload_node)) |
| 2484 | return; |
| 2485 | |
| 2486 | WARN_ON(src_cset->mg_src_cgrp); |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2487 | WARN_ON(src_cset->mg_dst_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2488 | WARN_ON(!list_empty(&src_cset->mg_tasks)); |
| 2489 | WARN_ON(!list_empty(&src_cset->mg_node)); |
| 2490 | |
| 2491 | src_cset->mg_src_cgrp = src_cgrp; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2492 | src_cset->mg_dst_cgrp = dst_cgrp; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2493 | get_css_set(src_cset); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2494 | list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | /** |
| 2498 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2499 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2500 | * |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2501 | * Tasks are about to be moved and all the source css_sets have been |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2502 | * preloaded to @mgctx->preloaded_src_csets. This function looks up and |
| 2503 | * pins all destination css_sets, links each to its source, and append them |
| 2504 | * to @mgctx->preloaded_dst_csets. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2505 | * |
| 2506 | * This function must be called after cgroup_migrate_add_src() has been |
| 2507 | * called on each migration source css_set. After migration is performed |
| 2508 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2509 | * @mgctx. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2510 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2511 | int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2512 | { |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2513 | struct css_set *src_cset, *tmp_cset; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2514 | |
| 2515 | lockdep_assert_held(&cgroup_mutex); |
| 2516 | |
| 2517 | /* look up the dst cset for each src cset and link it to src */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2518 | list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets, |
| 2519 | mg_preload_node) { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2520 | struct css_set *dst_cset; |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2521 | struct cgroup_subsys *ss; |
| 2522 | int ssid; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2523 | |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2524 | dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2525 | if (!dst_cset) |
| 2526 | goto err; |
| 2527 | |
| 2528 | 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] | 2529 | |
| 2530 | /* |
| 2531 | * If src cset equals dst, it's noop. Drop the src. |
| 2532 | * cgroup_migrate() will skip the cset too. Note that we |
| 2533 | * can't handle src == dst as some nodes are used by both. |
| 2534 | */ |
| 2535 | if (src_cset == dst_cset) { |
| 2536 | src_cset->mg_src_cgrp = NULL; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2537 | src_cset->mg_dst_cgrp = NULL; |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2538 | list_del_init(&src_cset->mg_preload_node); |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2539 | put_css_set(src_cset); |
| 2540 | put_css_set(dst_cset); |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2541 | continue; |
| 2542 | } |
| 2543 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2544 | src_cset->mg_dst_cset = dst_cset; |
| 2545 | |
| 2546 | if (list_empty(&dst_cset->mg_preload_node)) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2547 | list_add_tail(&dst_cset->mg_preload_node, |
| 2548 | &mgctx->preloaded_dst_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2549 | else |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2550 | put_css_set(dst_cset); |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2551 | |
| 2552 | for_each_subsys(ss, ssid) |
| 2553 | if (src_cset->subsys[ssid] != dst_cset->subsys[ssid]) |
| 2554 | mgctx->ss_mask |= 1 << ssid; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2555 | } |
| 2556 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2557 | return 0; |
| 2558 | err: |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2559 | cgroup_migrate_finish(mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2560 | return -ENOMEM; |
| 2561 | } |
| 2562 | |
| 2563 | /** |
| 2564 | * cgroup_migrate - migrate a process or task to a cgroup |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2565 | * @leader: the leader of the process or the task to migrate |
| 2566 | * @threadgroup: whether @leader points to the whole process or a single task |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2567 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2568 | * |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2569 | * Migrate a process or task denoted by @leader. If migrating a process, |
| 2570 | * the caller must be holding cgroup_threadgroup_rwsem. The caller is also |
| 2571 | * responsible for invoking cgroup_migrate_add_src() and |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2572 | * cgroup_migrate_prepare_dst() on the targets before invoking this |
| 2573 | * function and following up with cgroup_migrate_finish(). |
| 2574 | * |
| 2575 | * As long as a controller's ->can_attach() doesn't fail, this function is |
| 2576 | * guaranteed to succeed. This means that, excluding ->can_attach() |
| 2577 | * failure, when migrating multiple targets, the success or failure can be |
| 2578 | * decided for all targets by invoking group_migrate_prepare_dst() before |
| 2579 | * actually starting migrating. |
| 2580 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2581 | int cgroup_migrate(struct task_struct *leader, bool threadgroup, |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2582 | struct cgroup_mgctx *mgctx) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2583 | { |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2584 | struct task_struct *task; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2585 | |
| 2586 | /* |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2587 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2588 | * already PF_EXITING could be freed from underneath us unless we |
| 2589 | * take an rcu_read_lock. |
| 2590 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2591 | spin_lock_irq(&css_set_lock); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2592 | rcu_read_lock(); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2593 | task = leader; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2594 | do { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2595 | cgroup_migrate_add_task(task, mgctx); |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2596 | if (!threadgroup) |
| 2597 | break; |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2598 | } while_each_thread(leader, task); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2599 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2600 | spin_unlock_irq(&css_set_lock); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2601 | |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2602 | return cgroup_migrate_execute(mgctx); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2603 | } |
| 2604 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2605 | /** |
| 2606 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
| 2607 | * @dst_cgrp: the cgroup to attach to |
| 2608 | * @leader: the task or the leader of the threadgroup to be attached |
| 2609 | * @threadgroup: attach the whole threadgroup? |
| 2610 | * |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 2611 | * Call holding cgroup_mutex and cgroup_threadgroup_rwsem. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2612 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2613 | int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, |
| 2614 | bool threadgroup) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2615 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2616 | DEFINE_CGROUP_MGCTX(mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2617 | struct task_struct *task; |
| 2618 | int ret; |
| 2619 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2620 | ret = cgroup_migrate_vet_dst(dst_cgrp); |
| 2621 | if (ret) |
| 2622 | return ret; |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2623 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2624 | /* look up all src csets */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2625 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2626 | rcu_read_lock(); |
| 2627 | task = leader; |
| 2628 | do { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2629 | cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2630 | if (!threadgroup) |
| 2631 | break; |
| 2632 | } while_each_thread(leader, task); |
| 2633 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2634 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2635 | |
| 2636 | /* prepare dst csets and commit */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2637 | ret = cgroup_migrate_prepare_dst(&mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2638 | if (!ret) |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2639 | ret = cgroup_migrate(leader, threadgroup, &mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2640 | |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2641 | cgroup_migrate_finish(&mgctx); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2642 | |
| 2643 | if (!ret) |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame^] | 2644 | TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2645 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2646 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2647 | } |
| 2648 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2649 | struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup) |
| 2650 | __acquires(&cgroup_threadgroup_rwsem) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2651 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2652 | struct task_struct *tsk; |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2653 | pid_t pid; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2654 | |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2655 | if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2656 | return ERR_PTR(-EINVAL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2657 | |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2658 | percpu_down_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2659 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2660 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2661 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2662 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2663 | if (!tsk) { |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2664 | tsk = ERR_PTR(-ESRCH); |
| 2665 | goto out_unlock_threadgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2666 | } |
Tejun Heo | dedf22e | 2015-06-18 16:54:28 -0400 | [diff] [blame] | 2667 | } else { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2668 | tsk = current; |
Tejun Heo | dedf22e | 2015-06-18 16:54:28 -0400 | [diff] [blame] | 2669 | } |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2670 | |
| 2671 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2672 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2673 | |
| 2674 | /* |
Tejun Heo | 77f8879 | 2017-03-16 16:54:24 -0400 | [diff] [blame] | 2675 | * kthreads may acquire PF_NO_SETAFFINITY during initialization. |
| 2676 | * If userland migrates such a kthread to a non-root cgroup, it can |
| 2677 | * become trapped in a cpuset, or RT kthread may be born in a |
| 2678 | * cgroup with no rt_runtime allocated. Just say no. |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2679 | */ |
Tejun Heo | 77f8879 | 2017-03-16 16:54:24 -0400 | [diff] [blame] | 2680 | if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) { |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2681 | tsk = ERR_PTR(-EINVAL); |
| 2682 | goto out_unlock_threadgroup; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2683 | } |
| 2684 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2685 | get_task_struct(tsk); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2686 | goto out_unlock_rcu; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2687 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2688 | out_unlock_threadgroup: |
| 2689 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2690 | out_unlock_rcu: |
| 2691 | rcu_read_unlock(); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2692 | return tsk; |
| 2693 | } |
| 2694 | |
| 2695 | void cgroup_procs_write_finish(struct task_struct *task) |
| 2696 | __releases(&cgroup_threadgroup_rwsem) |
| 2697 | { |
| 2698 | struct cgroup_subsys *ss; |
| 2699 | int ssid; |
| 2700 | |
| 2701 | /* release reference from cgroup_procs_write_start() */ |
| 2702 | put_task_struct(task); |
| 2703 | |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2704 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | 5cf1cac | 2016-04-21 19:06:48 -0400 | [diff] [blame] | 2705 | for_each_subsys(ss, ssid) |
| 2706 | if (ss->post_attach) |
| 2707 | ss->post_attach(); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 2710 | static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2711 | { |
| 2712 | struct cgroup_subsys *ss; |
| 2713 | bool printed = false; |
| 2714 | int ssid; |
| 2715 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 2716 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 2717 | if (printed) |
| 2718 | seq_putc(seq, ' '); |
| 2719 | seq_printf(seq, "%s", ss->name); |
| 2720 | printed = true; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 2721 | } while_each_subsys_mask(); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2722 | if (printed) |
| 2723 | seq_putc(seq, '\n'); |
| 2724 | } |
| 2725 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2726 | /* show controllers which are enabled from the parent */ |
| 2727 | static int cgroup_controllers_show(struct seq_file *seq, void *v) |
| 2728 | { |
| 2729 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2730 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 2731 | cgroup_print_ss_mask(seq, cgroup_control(cgrp)); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2732 | return 0; |
| 2733 | } |
| 2734 | |
| 2735 | /* show controllers which are enabled for a given cgroup's children */ |
| 2736 | static int cgroup_subtree_control_show(struct seq_file *seq, void *v) |
| 2737 | { |
| 2738 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2739 | |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 2740 | cgroup_print_ss_mask(seq, cgrp->subtree_control); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2741 | return 0; |
| 2742 | } |
| 2743 | |
| 2744 | /** |
| 2745 | * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy |
| 2746 | * @cgrp: root of the subtree to update csses for |
| 2747 | * |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2748 | * @cgrp's control masks have changed and its subtree's css associations |
| 2749 | * need to be updated accordingly. This function looks up all css_sets |
| 2750 | * which are attached to the subtree, creates the matching updated css_sets |
| 2751 | * and migrates the tasks to the new ones. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2752 | */ |
| 2753 | static int cgroup_update_dfl_csses(struct cgroup *cgrp) |
| 2754 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2755 | DEFINE_CGROUP_MGCTX(mgctx); |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2756 | struct cgroup_subsys_state *d_css; |
| 2757 | struct cgroup *dsct; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2758 | struct css_set *src_cset; |
| 2759 | int ret; |
| 2760 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2761 | lockdep_assert_held(&cgroup_mutex); |
| 2762 | |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2763 | percpu_down_write(&cgroup_threadgroup_rwsem); |
| 2764 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2765 | /* look up all csses currently attached to @cgrp's subtree */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2766 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2767 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2768 | struct cgrp_cset_link *link; |
| 2769 | |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2770 | list_for_each_entry(link, &dsct->cset_links, cset_link) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2771 | cgroup_migrate_add_src(link->cset, dsct, &mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2772 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2773 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2774 | |
| 2775 | /* NULL dst indicates self on default hierarchy */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2776 | ret = cgroup_migrate_prepare_dst(&mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2777 | if (ret) |
| 2778 | goto out_finish; |
| 2779 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2780 | spin_lock_irq(&css_set_lock); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2781 | list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) { |
Tejun Heo | 1026507 | 2015-09-11 15:00:22 -0400 | [diff] [blame] | 2782 | struct task_struct *task, *ntask; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2783 | |
Tejun Heo | 1026507 | 2015-09-11 15:00:22 -0400 | [diff] [blame] | 2784 | /* all tasks in src_csets need to be migrated */ |
| 2785 | list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2786 | cgroup_migrate_add_task(task, &mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2787 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2788 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2789 | |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2790 | ret = cgroup_migrate_execute(&mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2791 | out_finish: |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2792 | cgroup_migrate_finish(&mgctx); |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2793 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2794 | return ret; |
| 2795 | } |
| 2796 | |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2797 | /** |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2798 | * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2799 | * @cgrp: root of the target subtree |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2800 | * |
| 2801 | * Because css offlining is asynchronous, userland may try to re-enable a |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2802 | * controller while the previous css is still around. This function grabs |
| 2803 | * cgroup_mutex and drains the previous css instances of @cgrp's subtree. |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2804 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2805 | void cgroup_lock_and_drain_offline(struct cgroup *cgrp) |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2806 | __acquires(&cgroup_mutex) |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2807 | { |
| 2808 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2809 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2810 | struct cgroup_subsys *ss; |
| 2811 | int ssid; |
| 2812 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2813 | restart: |
| 2814 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2815 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2816 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2817 | for_each_subsys(ss, ssid) { |
| 2818 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 2819 | DEFINE_WAIT(wait); |
| 2820 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2821 | if (!css || !percpu_ref_is_dying(&css->refcnt)) |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2822 | continue; |
| 2823 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 2824 | cgroup_get_live(dsct); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2825 | prepare_to_wait(&dsct->offline_waitq, &wait, |
| 2826 | TASK_UNINTERRUPTIBLE); |
| 2827 | |
| 2828 | mutex_unlock(&cgroup_mutex); |
| 2829 | schedule(); |
| 2830 | finish_wait(&dsct->offline_waitq, &wait); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2831 | |
| 2832 | cgroup_put(dsct); |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2833 | goto restart; |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2834 | } |
| 2835 | } |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2836 | } |
| 2837 | |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2838 | /** |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2839 | * cgroup_save_control - save control masks of a subtree |
| 2840 | * @cgrp: root of the target subtree |
| 2841 | * |
| 2842 | * Save ->subtree_control and ->subtree_ss_mask to the respective old_ |
| 2843 | * prefixed fields for @cgrp's subtree including @cgrp itself. |
| 2844 | */ |
| 2845 | static void cgroup_save_control(struct cgroup *cgrp) |
| 2846 | { |
| 2847 | struct cgroup *dsct; |
| 2848 | struct cgroup_subsys_state *d_css; |
| 2849 | |
| 2850 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 2851 | dsct->old_subtree_control = dsct->subtree_control; |
| 2852 | dsct->old_subtree_ss_mask = dsct->subtree_ss_mask; |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | /** |
| 2857 | * cgroup_propagate_control - refresh control masks of a subtree |
| 2858 | * @cgrp: root of the target subtree |
| 2859 | * |
| 2860 | * For @cgrp and its subtree, ensure ->subtree_ss_mask matches |
| 2861 | * ->subtree_control and propagate controller availability through the |
| 2862 | * subtree so that descendants don't have unavailable controllers enabled. |
| 2863 | */ |
| 2864 | static void cgroup_propagate_control(struct cgroup *cgrp) |
| 2865 | { |
| 2866 | struct cgroup *dsct; |
| 2867 | struct cgroup_subsys_state *d_css; |
| 2868 | |
| 2869 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 2870 | dsct->subtree_control &= cgroup_control(dsct); |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2871 | dsct->subtree_ss_mask = |
| 2872 | cgroup_calc_subtree_ss_mask(dsct->subtree_control, |
| 2873 | cgroup_ss_mask(dsct)); |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | /** |
| 2878 | * cgroup_restore_control - restore control masks of a subtree |
| 2879 | * @cgrp: root of the target subtree |
| 2880 | * |
| 2881 | * Restore ->subtree_control and ->subtree_ss_mask from the respective old_ |
| 2882 | * prefixed fields for @cgrp's subtree including @cgrp itself. |
| 2883 | */ |
| 2884 | static void cgroup_restore_control(struct cgroup *cgrp) |
| 2885 | { |
| 2886 | struct cgroup *dsct; |
| 2887 | struct cgroup_subsys_state *d_css; |
| 2888 | |
| 2889 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
| 2890 | dsct->subtree_control = dsct->old_subtree_control; |
| 2891 | dsct->subtree_ss_mask = dsct->old_subtree_ss_mask; |
| 2892 | } |
| 2893 | } |
| 2894 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2895 | static bool css_visible(struct cgroup_subsys_state *css) |
| 2896 | { |
| 2897 | struct cgroup_subsys *ss = css->ss; |
| 2898 | struct cgroup *cgrp = css->cgroup; |
| 2899 | |
| 2900 | if (cgroup_control(cgrp) & (1 << ss->id)) |
| 2901 | return true; |
| 2902 | if (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) |
| 2903 | return false; |
| 2904 | return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl; |
| 2905 | } |
| 2906 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2907 | /** |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2908 | * cgroup_apply_control_enable - enable or show csses according to control |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2909 | * @cgrp: root of the target subtree |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2910 | * |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2911 | * Walk @cgrp's subtree and create new csses or make the existing ones |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2912 | * visible. A css is created invisible if it's being implicitly enabled |
| 2913 | * through dependency. An invisible css is made visible when the userland |
| 2914 | * explicitly enables it. |
| 2915 | * |
| 2916 | * Returns 0 on success, -errno on failure. On failure, csses which have |
| 2917 | * been processed already aren't cleaned up. The caller is responsible for |
Masahiro Yamada | 8a1115f | 2017-03-09 16:16:31 -0800 | [diff] [blame] | 2918 | * cleaning up with cgroup_apply_control_disable(). |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2919 | */ |
| 2920 | static int cgroup_apply_control_enable(struct cgroup *cgrp) |
| 2921 | { |
| 2922 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2923 | struct cgroup_subsys_state *d_css; |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2924 | struct cgroup_subsys *ss; |
| 2925 | int ssid, ret; |
| 2926 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2927 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2928 | for_each_subsys(ss, ssid) { |
| 2929 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 2930 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2931 | WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); |
| 2932 | |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2933 | if (!(cgroup_ss_mask(dsct) & (1 << ss->id))) |
| 2934 | continue; |
| 2935 | |
| 2936 | if (!css) { |
| 2937 | css = css_create(dsct, ss); |
| 2938 | if (IS_ERR(css)) |
| 2939 | return PTR_ERR(css); |
| 2940 | } |
| 2941 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2942 | if (css_visible(css)) { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2943 | ret = css_populate_dir(css); |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2944 | if (ret) |
| 2945 | return ret; |
| 2946 | } |
| 2947 | } |
| 2948 | } |
| 2949 | |
| 2950 | return 0; |
| 2951 | } |
| 2952 | |
| 2953 | /** |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2954 | * cgroup_apply_control_disable - kill or hide csses according to control |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2955 | * @cgrp: root of the target subtree |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2956 | * |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2957 | * Walk @cgrp's subtree and kill and hide csses so that they match |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2958 | * cgroup_ss_mask() and cgroup_visible_mask(). |
| 2959 | * |
| 2960 | * A css is hidden when the userland requests it to be disabled while other |
| 2961 | * subsystems are still depending on it. The css must not actively control |
| 2962 | * resources and be in the vanilla state if it's made visible again later. |
| 2963 | * Controllers which may be depended upon should provide ->css_reset() for |
| 2964 | * this purpose. |
| 2965 | */ |
| 2966 | static void cgroup_apply_control_disable(struct cgroup *cgrp) |
| 2967 | { |
| 2968 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2969 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2970 | struct cgroup_subsys *ss; |
| 2971 | int ssid; |
| 2972 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2973 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2974 | for_each_subsys(ss, ssid) { |
| 2975 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 2976 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2977 | WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); |
| 2978 | |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2979 | if (!css) |
| 2980 | continue; |
| 2981 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2982 | if (css->parent && |
| 2983 | !(cgroup_ss_mask(dsct) & (1 << ss->id))) { |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2984 | kill_css(css); |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2985 | } else if (!css_visible(css)) { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2986 | css_clear_dir(css); |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2987 | if (ss->css_reset) |
| 2988 | ss->css_reset(css); |
| 2989 | } |
| 2990 | } |
| 2991 | } |
| 2992 | } |
| 2993 | |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2994 | /** |
| 2995 | * cgroup_apply_control - apply control mask updates to the subtree |
| 2996 | * @cgrp: root of the target subtree |
| 2997 | * |
| 2998 | * subsystems can be enabled and disabled in a subtree using the following |
| 2999 | * steps. |
| 3000 | * |
| 3001 | * 1. Call cgroup_save_control() to stash the current state. |
| 3002 | * 2. Update ->subtree_control masks in the subtree as desired. |
| 3003 | * 3. Call cgroup_apply_control() to apply the changes. |
| 3004 | * 4. Optionally perform other related operations. |
| 3005 | * 5. Call cgroup_finalize_control() to finish up. |
| 3006 | * |
| 3007 | * This function implements step 3 and propagates the mask changes |
| 3008 | * throughout @cgrp's subtree, updates csses accordingly and perform |
| 3009 | * process migrations. |
| 3010 | */ |
| 3011 | static int cgroup_apply_control(struct cgroup *cgrp) |
| 3012 | { |
| 3013 | int ret; |
| 3014 | |
| 3015 | cgroup_propagate_control(cgrp); |
| 3016 | |
| 3017 | ret = cgroup_apply_control_enable(cgrp); |
| 3018 | if (ret) |
| 3019 | return ret; |
| 3020 | |
| 3021 | /* |
| 3022 | * At this point, cgroup_e_css() results reflect the new csses |
| 3023 | * making the following cgroup_update_dfl_csses() properly update |
| 3024 | * css associations of all tasks in the subtree. |
| 3025 | */ |
| 3026 | ret = cgroup_update_dfl_csses(cgrp); |
| 3027 | if (ret) |
| 3028 | return ret; |
| 3029 | |
| 3030 | return 0; |
| 3031 | } |
| 3032 | |
| 3033 | /** |
| 3034 | * cgroup_finalize_control - finalize control mask update |
| 3035 | * @cgrp: root of the target subtree |
| 3036 | * @ret: the result of the update |
| 3037 | * |
| 3038 | * Finalize control mask update. See cgroup_apply_control() for more info. |
| 3039 | */ |
| 3040 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret) |
| 3041 | { |
| 3042 | if (ret) { |
| 3043 | cgroup_restore_control(cgrp); |
| 3044 | cgroup_propagate_control(cgrp); |
| 3045 | } |
| 3046 | |
| 3047 | cgroup_apply_control_disable(cgrp); |
| 3048 | } |
| 3049 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3050 | static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable) |
| 3051 | { |
| 3052 | u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask; |
| 3053 | |
| 3054 | /* if nothing is getting enabled, nothing to worry about */ |
| 3055 | if (!enable) |
| 3056 | return 0; |
| 3057 | |
| 3058 | /* can @cgrp host any resources? */ |
| 3059 | if (!cgroup_is_valid_domain(cgrp->dom_cgrp)) |
| 3060 | return -EOPNOTSUPP; |
| 3061 | |
| 3062 | /* mixables don't care */ |
| 3063 | if (cgroup_is_mixable(cgrp)) |
| 3064 | return 0; |
| 3065 | |
| 3066 | if (domain_enable) { |
| 3067 | /* can't enable domain controllers inside a thread subtree */ |
| 3068 | if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp)) |
| 3069 | return -EOPNOTSUPP; |
| 3070 | } else { |
| 3071 | /* |
| 3072 | * Threaded controllers can handle internal competitions |
| 3073 | * and are always allowed inside a (prospective) thread |
| 3074 | * subtree. |
| 3075 | */ |
| 3076 | if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp)) |
| 3077 | return 0; |
| 3078 | } |
| 3079 | |
| 3080 | /* |
| 3081 | * Controllers can't be enabled for a cgroup with tasks to avoid |
| 3082 | * child cgroups competing against tasks. |
| 3083 | */ |
| 3084 | if (cgroup_has_tasks(cgrp)) |
| 3085 | return -EBUSY; |
| 3086 | |
| 3087 | return 0; |
| 3088 | } |
| 3089 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3090 | /* change the enabled child controllers for a cgroup in the default hierarchy */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3091 | static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, |
| 3092 | char *buf, size_t nbytes, |
| 3093 | loff_t off) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3094 | { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 3095 | u16 enable = 0, disable = 0; |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3096 | struct cgroup *cgrp, *child; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3097 | struct cgroup_subsys *ss; |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3098 | char *tok; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3099 | int ssid, ret; |
| 3100 | |
| 3101 | /* |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 3102 | * Parse input - space separated list of subsystem names prefixed |
| 3103 | * with either + or -. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3104 | */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3105 | buf = strstrip(buf); |
| 3106 | while ((tok = strsep(&buf, " "))) { |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 3107 | if (tok[0] == '\0') |
| 3108 | continue; |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 3109 | do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) { |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 3110 | if (!cgroup_ssid_enabled(ssid) || |
| 3111 | strcmp(tok + 1, ss->name)) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3112 | continue; |
| 3113 | |
| 3114 | if (*tok == '+') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 3115 | enable |= 1 << ssid; |
| 3116 | disable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3117 | } else if (*tok == '-') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 3118 | disable |= 1 << ssid; |
| 3119 | enable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3120 | } else { |
| 3121 | return -EINVAL; |
| 3122 | } |
| 3123 | break; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 3124 | } while_each_subsys_mask(); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3125 | if (ssid == CGROUP_SUBSYS_COUNT) |
| 3126 | return -EINVAL; |
| 3127 | } |
| 3128 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3129 | cgrp = cgroup_kn_lock_live(of->kn, true); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3130 | if (!cgrp) |
| 3131 | return -ENODEV; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3132 | |
| 3133 | for_each_subsys(ss, ssid) { |
| 3134 | if (enable & (1 << ssid)) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3135 | if (cgrp->subtree_control & (1 << ssid)) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3136 | enable &= ~(1 << ssid); |
| 3137 | continue; |
| 3138 | } |
| 3139 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 3140 | if (!(cgroup_control(cgrp) & (1 << ssid))) { |
Tejun Heo | c29adf2 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3141 | ret = -ENOENT; |
| 3142 | goto out_unlock; |
| 3143 | } |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3144 | } else if (disable & (1 << ssid)) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3145 | if (!(cgrp->subtree_control & (1 << ssid))) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3146 | disable &= ~(1 << ssid); |
| 3147 | continue; |
| 3148 | } |
| 3149 | |
| 3150 | /* a child has it enabled? */ |
| 3151 | cgroup_for_each_live_child(child, cgrp) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3152 | if (child->subtree_control & (1 << ssid)) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3153 | ret = -EBUSY; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3154 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3155 | } |
| 3156 | } |
| 3157 | } |
| 3158 | } |
| 3159 | |
| 3160 | if (!enable && !disable) { |
| 3161 | ret = 0; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3162 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3163 | } |
| 3164 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3165 | ret = cgroup_vet_subtree_control_enable(cgrp, enable); |
| 3166 | if (ret) |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 3167 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3168 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3169 | /* save and update control masks and prepare csses */ |
| 3170 | cgroup_save_control(cgrp); |
Tejun Heo | c29adf2 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3171 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3172 | cgrp->subtree_control |= enable; |
| 3173 | cgrp->subtree_control &= ~disable; |
Tejun Heo | f63070d | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 3174 | |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3175 | ret = cgroup_apply_control(cgrp); |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3176 | cgroup_finalize_control(cgrp, ret); |
Tejun Heo | 3c74541 | 2017-07-23 08:14:15 -0400 | [diff] [blame] | 3177 | if (ret) |
| 3178 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3179 | |
| 3180 | kernfs_activate(cgrp->kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3181 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3182 | cgroup_kn_unlock(of->kn); |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3183 | return ret ?: nbytes; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3184 | } |
| 3185 | |
Tejun Heo | c705a00 | 2017-07-25 13:20:18 -0400 | [diff] [blame] | 3186 | /** |
| 3187 | * cgroup_enable_threaded - make @cgrp threaded |
| 3188 | * @cgrp: the target cgroup |
| 3189 | * |
| 3190 | * Called when "threaded" is written to the cgroup.type interface file and |
| 3191 | * tries to make @cgrp threaded and join the parent's resource domain. |
| 3192 | * This function is never called on the root cgroup as cgroup.type doesn't |
| 3193 | * exist on it. |
| 3194 | */ |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3195 | static int cgroup_enable_threaded(struct cgroup *cgrp) |
| 3196 | { |
| 3197 | struct cgroup *parent = cgroup_parent(cgrp); |
| 3198 | struct cgroup *dom_cgrp = parent->dom_cgrp; |
| 3199 | int ret; |
| 3200 | |
| 3201 | lockdep_assert_held(&cgroup_mutex); |
| 3202 | |
| 3203 | /* noop if already threaded */ |
| 3204 | if (cgroup_is_threaded(cgrp)) |
| 3205 | return 0; |
| 3206 | |
Tejun Heo | d1897c9 | 2018-02-21 11:39:22 -0800 | [diff] [blame] | 3207 | /* |
| 3208 | * If @cgroup is populated or has domain controllers enabled, it |
| 3209 | * can't be switched. While the below cgroup_can_be_thread_root() |
| 3210 | * test can catch the same conditions, that's only when @parent is |
| 3211 | * not mixable, so let's check it explicitly. |
| 3212 | */ |
| 3213 | if (cgroup_is_populated(cgrp) || |
| 3214 | cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) |
| 3215 | return -EOPNOTSUPP; |
| 3216 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3217 | /* we're joining the parent's domain, ensure its validity */ |
| 3218 | if (!cgroup_is_valid_domain(dom_cgrp) || |
| 3219 | !cgroup_can_be_thread_root(dom_cgrp)) |
| 3220 | return -EOPNOTSUPP; |
| 3221 | |
| 3222 | /* |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3223 | * The following shouldn't cause actual migrations and should |
| 3224 | * always succeed. |
| 3225 | */ |
| 3226 | cgroup_save_control(cgrp); |
| 3227 | |
| 3228 | cgrp->dom_cgrp = dom_cgrp; |
| 3229 | ret = cgroup_apply_control(cgrp); |
| 3230 | if (!ret) |
| 3231 | parent->nr_threaded_children++; |
| 3232 | else |
| 3233 | cgrp->dom_cgrp = cgrp; |
| 3234 | |
| 3235 | cgroup_finalize_control(cgrp, ret); |
| 3236 | return ret; |
| 3237 | } |
| 3238 | |
| 3239 | static int cgroup_type_show(struct seq_file *seq, void *v) |
| 3240 | { |
| 3241 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3242 | |
| 3243 | if (cgroup_is_threaded(cgrp)) |
| 3244 | seq_puts(seq, "threaded\n"); |
| 3245 | else if (!cgroup_is_valid_domain(cgrp)) |
| 3246 | seq_puts(seq, "domain invalid\n"); |
| 3247 | else if (cgroup_is_thread_root(cgrp)) |
| 3248 | seq_puts(seq, "domain threaded\n"); |
| 3249 | else |
| 3250 | seq_puts(seq, "domain\n"); |
| 3251 | |
| 3252 | return 0; |
| 3253 | } |
| 3254 | |
| 3255 | static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf, |
| 3256 | size_t nbytes, loff_t off) |
| 3257 | { |
| 3258 | struct cgroup *cgrp; |
| 3259 | int ret; |
| 3260 | |
| 3261 | /* only switching to threaded mode is supported */ |
| 3262 | if (strcmp(strstrip(buf), "threaded")) |
| 3263 | return -EINVAL; |
| 3264 | |
| 3265 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3266 | if (!cgrp) |
| 3267 | return -ENOENT; |
| 3268 | |
| 3269 | /* threaded can only be enabled */ |
| 3270 | ret = cgroup_enable_threaded(cgrp); |
| 3271 | |
| 3272 | cgroup_kn_unlock(of->kn); |
| 3273 | return ret ?: nbytes; |
| 3274 | } |
| 3275 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3276 | static int cgroup_max_descendants_show(struct seq_file *seq, void *v) |
| 3277 | { |
| 3278 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3279 | int descendants = READ_ONCE(cgrp->max_descendants); |
| 3280 | |
| 3281 | if (descendants == INT_MAX) |
| 3282 | seq_puts(seq, "max\n"); |
| 3283 | else |
| 3284 | seq_printf(seq, "%d\n", descendants); |
| 3285 | |
| 3286 | return 0; |
| 3287 | } |
| 3288 | |
| 3289 | static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of, |
| 3290 | char *buf, size_t nbytes, loff_t off) |
| 3291 | { |
| 3292 | struct cgroup *cgrp; |
| 3293 | int descendants; |
| 3294 | ssize_t ret; |
| 3295 | |
| 3296 | buf = strstrip(buf); |
| 3297 | if (!strcmp(buf, "max")) { |
| 3298 | descendants = INT_MAX; |
| 3299 | } else { |
| 3300 | ret = kstrtoint(buf, 0, &descendants); |
| 3301 | if (ret) |
| 3302 | return ret; |
| 3303 | } |
| 3304 | |
Dan Carpenter | 696b98f | 2017-08-09 13:25:21 +0300 | [diff] [blame] | 3305 | if (descendants < 0) |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3306 | return -ERANGE; |
| 3307 | |
| 3308 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3309 | if (!cgrp) |
| 3310 | return -ENOENT; |
| 3311 | |
| 3312 | cgrp->max_descendants = descendants; |
| 3313 | |
| 3314 | cgroup_kn_unlock(of->kn); |
| 3315 | |
| 3316 | return nbytes; |
| 3317 | } |
| 3318 | |
| 3319 | static int cgroup_max_depth_show(struct seq_file *seq, void *v) |
| 3320 | { |
| 3321 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3322 | int depth = READ_ONCE(cgrp->max_depth); |
| 3323 | |
| 3324 | if (depth == INT_MAX) |
| 3325 | seq_puts(seq, "max\n"); |
| 3326 | else |
| 3327 | seq_printf(seq, "%d\n", depth); |
| 3328 | |
| 3329 | return 0; |
| 3330 | } |
| 3331 | |
| 3332 | static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of, |
| 3333 | char *buf, size_t nbytes, loff_t off) |
| 3334 | { |
| 3335 | struct cgroup *cgrp; |
| 3336 | ssize_t ret; |
| 3337 | int depth; |
| 3338 | |
| 3339 | buf = strstrip(buf); |
| 3340 | if (!strcmp(buf, "max")) { |
| 3341 | depth = INT_MAX; |
| 3342 | } else { |
| 3343 | ret = kstrtoint(buf, 0, &depth); |
| 3344 | if (ret) |
| 3345 | return ret; |
| 3346 | } |
| 3347 | |
Dan Carpenter | 696b98f | 2017-08-09 13:25:21 +0300 | [diff] [blame] | 3348 | if (depth < 0) |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3349 | return -ERANGE; |
| 3350 | |
| 3351 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3352 | if (!cgrp) |
| 3353 | return -ENOENT; |
| 3354 | |
| 3355 | cgrp->max_depth = depth; |
| 3356 | |
| 3357 | cgroup_kn_unlock(of->kn); |
| 3358 | |
| 3359 | return nbytes; |
| 3360 | } |
| 3361 | |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 3362 | static int cgroup_events_show(struct seq_file *seq, void *v) |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3363 | { |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 3364 | seq_printf(seq, "populated %d\n", |
Tejun Heo | 27bd4db | 2015-10-15 16:41:50 -0400 | [diff] [blame] | 3365 | cgroup_is_populated(seq_css(seq)->cgroup)); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3366 | return 0; |
| 3367 | } |
| 3368 | |
Tejun Heo | 3e48930 | 2017-08-11 05:49:01 -0700 | [diff] [blame] | 3369 | static int cgroup_stat_show(struct seq_file *seq, void *v) |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 3370 | { |
| 3371 | struct cgroup *cgroup = seq_css(seq)->cgroup; |
| 3372 | |
| 3373 | seq_printf(seq, "nr_descendants %d\n", |
| 3374 | cgroup->nr_descendants); |
| 3375 | seq_printf(seq, "nr_dying_descendants %d\n", |
| 3376 | cgroup->nr_dying_descendants); |
| 3377 | |
| 3378 | return 0; |
| 3379 | } |
| 3380 | |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3381 | static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq, |
| 3382 | struct cgroup *cgrp, int ssid) |
| 3383 | { |
| 3384 | struct cgroup_subsys *ss = cgroup_subsys[ssid]; |
| 3385 | struct cgroup_subsys_state *css; |
| 3386 | int ret; |
| 3387 | |
| 3388 | if (!ss->css_extra_stat_show) |
| 3389 | return 0; |
| 3390 | |
| 3391 | css = cgroup_tryget_css(cgrp, ss); |
| 3392 | if (!css) |
| 3393 | return 0; |
| 3394 | |
| 3395 | ret = ss->css_extra_stat_show(seq, css); |
| 3396 | css_put(css); |
| 3397 | return ret; |
| 3398 | } |
| 3399 | |
| 3400 | static int cpu_stat_show(struct seq_file *seq, void *v) |
| 3401 | { |
Tejun Heo | c3ba132 | 2017-10-30 08:13:14 -0700 | [diff] [blame] | 3402 | struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3403 | int ret = 0; |
| 3404 | |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3405 | cgroup_base_stat_cputime_show(seq); |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3406 | #ifdef CONFIG_CGROUP_SCHED |
| 3407 | ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id); |
| 3408 | #endif |
| 3409 | return ret; |
| 3410 | } |
| 3411 | |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3412 | static int cgroup_file_open(struct kernfs_open_file *of) |
| 3413 | { |
| 3414 | struct cftype *cft = of->kn->priv; |
| 3415 | |
| 3416 | if (cft->open) |
| 3417 | return cft->open(of); |
| 3418 | return 0; |
| 3419 | } |
| 3420 | |
| 3421 | static void cgroup_file_release(struct kernfs_open_file *of) |
| 3422 | { |
| 3423 | struct cftype *cft = of->kn->priv; |
| 3424 | |
| 3425 | if (cft->release) |
| 3426 | cft->release(of); |
| 3427 | } |
| 3428 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3429 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 3430 | size_t nbytes, loff_t off) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3431 | { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 3432 | struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3433 | struct cgroup *cgrp = of->kn->parent->priv; |
| 3434 | struct cftype *cft = of->kn->priv; |
| 3435 | struct cgroup_subsys_state *css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3436 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3437 | |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 3438 | /* |
| 3439 | * If namespaces are delegation boundaries, disallow writes to |
| 3440 | * files in an non-init namespace root from inside the namespace |
| 3441 | * except for the files explicitly marked delegatable - |
| 3442 | * cgroup.procs and cgroup.subtree_control. |
| 3443 | */ |
| 3444 | if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) && |
| 3445 | !(cft->flags & CFTYPE_NS_DELEGATABLE) && |
| 3446 | ns != &init_cgroup_ns && ns->root_cset->dfl_cgrp == cgrp) |
| 3447 | return -EPERM; |
| 3448 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3449 | if (cft->write) |
| 3450 | return cft->write(of, buf, nbytes, off); |
| 3451 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3452 | /* |
| 3453 | * kernfs guarantees that a file isn't deleted with operations in |
| 3454 | * flight, which means that the matching css is and stays alive and |
| 3455 | * doesn't need to be pinned. The RCU locking is not necessary |
| 3456 | * either. It's just for the convenience of using cgroup_css(). |
| 3457 | */ |
| 3458 | rcu_read_lock(); |
| 3459 | css = cgroup_css(cgrp, cft->ss); |
| 3460 | rcu_read_unlock(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3461 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3462 | if (cft->write_u64) { |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3463 | unsigned long long v; |
| 3464 | ret = kstrtoull(buf, 0, &v); |
| 3465 | if (!ret) |
| 3466 | ret = cft->write_u64(css, cft, v); |
| 3467 | } else if (cft->write_s64) { |
| 3468 | long long v; |
| 3469 | ret = kstrtoll(buf, 0, &v); |
| 3470 | if (!ret) |
| 3471 | ret = cft->write_s64(css, cft, v); |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3472 | } else { |
| 3473 | ret = -EINVAL; |
| 3474 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3475 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3476 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3477 | } |
| 3478 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3479 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3480 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3481 | return seq_cft(seq)->seq_start(seq, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 3485 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3486 | return seq_cft(seq)->seq_next(seq, v, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 3490 | { |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3491 | if (seq_cft(seq)->seq_stop) |
| 3492 | seq_cft(seq)->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 3496 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3497 | struct cftype *cft = seq_cft(m); |
| 3498 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 3499 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3500 | if (cft->seq_show) |
| 3501 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3502 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3503 | if (cft->read_u64) |
| 3504 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 3505 | else if (cft->read_s64) |
| 3506 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 3507 | else |
| 3508 | return -EINVAL; |
| 3509 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3510 | } |
| 3511 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3512 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 3513 | .atomic_write_len = PAGE_SIZE, |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3514 | .open = cgroup_file_open, |
| 3515 | .release = cgroup_file_release, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3516 | .write = cgroup_file_write, |
| 3517 | .seq_show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3518 | }; |
| 3519 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3520 | static struct kernfs_ops cgroup_kf_ops = { |
| 3521 | .atomic_write_len = PAGE_SIZE, |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3522 | .open = cgroup_file_open, |
| 3523 | .release = cgroup_file_release, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3524 | .write = cgroup_file_write, |
| 3525 | .seq_start = cgroup_seqfile_start, |
| 3526 | .seq_next = cgroup_seqfile_next, |
| 3527 | .seq_stop = cgroup_seqfile_stop, |
| 3528 | .seq_show = cgroup_seqfile_show, |
| 3529 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3530 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3531 | /* set uid and gid of cgroup dirs and files to that of the creator */ |
| 3532 | static int cgroup_kn_set_ugid(struct kernfs_node *kn) |
| 3533 | { |
| 3534 | struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, |
| 3535 | .ia_uid = current_fsuid(), |
| 3536 | .ia_gid = current_fsgid(), }; |
| 3537 | |
| 3538 | if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && |
| 3539 | gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) |
| 3540 | return 0; |
| 3541 | |
| 3542 | return kernfs_setattr(kn, &iattr); |
| 3543 | } |
| 3544 | |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3545 | static void cgroup_file_notify_timer(struct timer_list *timer) |
| 3546 | { |
| 3547 | cgroup_file_notify(container_of(timer, struct cgroup_file, |
| 3548 | notify_timer)); |
| 3549 | } |
| 3550 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3551 | static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, |
| 3552 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3553 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3554 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3555 | struct kernfs_node *kn; |
| 3556 | struct lock_class_key *key = NULL; |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3557 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3558 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3559 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 3560 | key = &cft->lockdep_key; |
| 3561 | #endif |
| 3562 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
| 3563 | cgroup_file_mode(cft), 0, cft->kf_ops, cft, |
Tejun Heo | dfeb0750 | 2015-02-13 14:36:31 -0800 | [diff] [blame] | 3564 | NULL, key); |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3565 | if (IS_ERR(kn)) |
| 3566 | return PTR_ERR(kn); |
| 3567 | |
| 3568 | ret = cgroup_kn_set_ugid(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3569 | if (ret) { |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3570 | kernfs_remove(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3571 | return ret; |
| 3572 | } |
| 3573 | |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3574 | if (cft->file_offset) { |
| 3575 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 3576 | |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3577 | timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0); |
| 3578 | |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3579 | spin_lock_irq(&cgroup_file_kn_lock); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3580 | cfile->kn = kn; |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3581 | spin_unlock_irq(&cgroup_file_kn_lock); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3582 | } |
| 3583 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3584 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3585 | } |
| 3586 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3587 | /** |
| 3588 | * cgroup_addrm_files - add or remove files to a cgroup directory |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3589 | * @css: the target css |
| 3590 | * @cgrp: the target cgroup (usually css->cgroup) |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3591 | * @cfts: array of cftypes to be added |
| 3592 | * @is_add: whether to add or remove |
| 3593 | * |
| 3594 | * Depending on @is_add, add or remove files defined by @cfts on @cgrp. |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3595 | * For removals, this function never fails. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3596 | */ |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3597 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 3598 | struct cgroup *cgrp, struct cftype cfts[], |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3599 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3600 | { |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3601 | struct cftype *cft, *cft_end = NULL; |
Tejun Heo | b598dde | 2016-02-22 22:25:45 -0500 | [diff] [blame] | 3602 | int ret = 0; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3603 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3604 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3605 | |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3606 | restart: |
| 3607 | for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3608 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3609 | if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) |
Tejun Heo | 8cbbf2c | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 3610 | continue; |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3611 | if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp)) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3612 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3613 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3614 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3615 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3616 | continue; |
| 3617 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 3618 | if (is_add) { |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3619 | ret = cgroup_add_file(css, cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3620 | if (ret) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 3621 | pr_warn("%s: failed to add %s, err=%d\n", |
| 3622 | __func__, cft->name, ret); |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3623 | cft_end = cft; |
| 3624 | is_add = false; |
| 3625 | goto restart; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3626 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 3627 | } else { |
| 3628 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3629 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3630 | } |
Tejun Heo | b598dde | 2016-02-22 22:25:45 -0500 | [diff] [blame] | 3631 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3634 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3635 | { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3636 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 3637 | struct cgroup *root = &ss->root->cgrp; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3638 | struct cgroup_subsys_state *css; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3639 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3640 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3641 | lockdep_assert_held(&cgroup_mutex); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3642 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3643 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3644 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3645 | struct cgroup *cgrp = css->cgroup; |
| 3646 | |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 3647 | if (!(css->flags & CSS_VISIBLE)) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3648 | continue; |
| 3649 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3650 | ret = cgroup_addrm_files(css, cgrp, cfts, is_add); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3651 | if (ret) |
| 3652 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3653 | } |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3654 | |
| 3655 | if (is_add && !ret) |
| 3656 | kernfs_activate(root->kn); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3657 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3658 | } |
| 3659 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3660 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 3661 | { |
| 3662 | struct cftype *cft; |
| 3663 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3664 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 3665 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 3666 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 3667 | kfree(cft->kf_ops); |
| 3668 | cft->kf_ops = NULL; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3669 | cft->ss = NULL; |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3670 | |
| 3671 | /* revert flags set by cgroup core while adding @cfts */ |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3672 | cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3673 | } |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3674 | } |
| 3675 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3676 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3677 | { |
| 3678 | struct cftype *cft; |
| 3679 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3680 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 3681 | struct kernfs_ops *kf_ops; |
| 3682 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3683 | WARN_ON(cft->ss || cft->kf_ops); |
| 3684 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3685 | if (cft->seq_start) |
| 3686 | kf_ops = &cgroup_kf_ops; |
| 3687 | else |
| 3688 | kf_ops = &cgroup_kf_single_ops; |
| 3689 | |
| 3690 | /* |
| 3691 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 3692 | * make a copy of kf_ops to set its atomic_write_len. |
| 3693 | */ |
| 3694 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 3695 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 3696 | if (!kf_ops) { |
| 3697 | cgroup_exit_cftypes(cfts); |
| 3698 | return -ENOMEM; |
| 3699 | } |
| 3700 | kf_ops->atomic_write_len = cft->max_write_len; |
| 3701 | } |
| 3702 | |
| 3703 | cft->kf_ops = kf_ops; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3704 | cft->ss = ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | return 0; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3708 | } |
| 3709 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3710 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 3711 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3712 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3713 | |
| 3714 | if (!cfts || !cfts[0].ss) |
| 3715 | return -ENOENT; |
| 3716 | |
| 3717 | list_del(&cfts->node); |
| 3718 | cgroup_apply_cftypes(cfts, false); |
| 3719 | cgroup_exit_cftypes(cfts); |
| 3720 | return 0; |
| 3721 | } |
| 3722 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3723 | /** |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3724 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 3725 | * @cfts: zero-length name terminated array of cftypes |
| 3726 | * |
| 3727 | * Unregister @cfts. Files described by @cfts are removed from all |
| 3728 | * existing cgroups and all future cgroups won't have them either. This |
| 3729 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 3730 | * |
| 3731 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 3732 | * registered. |
| 3733 | */ |
| 3734 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 3735 | { |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3736 | int ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3737 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3738 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3739 | ret = cgroup_rm_cftypes_locked(cfts); |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3740 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3741 | return ret; |
| 3742 | } |
| 3743 | |
| 3744 | /** |
| 3745 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 3746 | * @ss: target cgroup subsystem |
| 3747 | * @cfts: zero-length name terminated array of cftypes |
| 3748 | * |
| 3749 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 3750 | * existing cgroups to which @ss is attached and all future cgroups will |
| 3751 | * have them too. This function can be called anytime whether @ss is |
| 3752 | * attached or not. |
| 3753 | * |
| 3754 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 3755 | * function currently returns 0 as long as @cfts registration is successful |
| 3756 | * even if some file creation attempts on existing cgroups fail. |
| 3757 | */ |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 3758 | static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3759 | { |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3760 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3761 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 3762 | if (!cgroup_ssid_enabled(ss->id)) |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 3763 | return 0; |
| 3764 | |
Li Zefan | dc5736e | 2014-02-17 10:41:50 +0800 | [diff] [blame] | 3765 | if (!cfts || cfts[0].name[0] == '\0') |
| 3766 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3767 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3768 | ret = cgroup_init_cftypes(ss, cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3769 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3770 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3771 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3772 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3773 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3774 | list_add_tail(&cfts->node, &ss->cfts); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3775 | ret = cgroup_apply_cftypes(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3776 | if (ret) |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3777 | cgroup_rm_cftypes_locked(cfts); |
| 3778 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3779 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3780 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3781 | } |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3782 | |
| 3783 | /** |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3784 | * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy |
| 3785 | * @ss: target cgroup subsystem |
| 3786 | * @cfts: zero-length name terminated array of cftypes |
| 3787 | * |
| 3788 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 3789 | * the default hierarchy. |
| 3790 | */ |
| 3791 | int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 3792 | { |
| 3793 | struct cftype *cft; |
| 3794 | |
| 3795 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3796 | cft->flags |= __CFTYPE_ONLY_ON_DFL; |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3797 | return cgroup_add_cftypes(ss, cfts); |
| 3798 | } |
| 3799 | |
| 3800 | /** |
| 3801 | * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies |
| 3802 | * @ss: target cgroup subsystem |
| 3803 | * @cfts: zero-length name terminated array of cftypes |
| 3804 | * |
| 3805 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 3806 | * the legacy hierarchies. |
| 3807 | */ |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 3808 | int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 3809 | { |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3810 | struct cftype *cft; |
| 3811 | |
Tejun Heo | e4b7037 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 3812 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
| 3813 | cft->flags |= __CFTYPE_NOT_ON_DFL; |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 3814 | return cgroup_add_cftypes(ss, cfts); |
| 3815 | } |
| 3816 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3817 | /** |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3818 | * cgroup_file_notify - generate a file modified event for a cgroup_file |
| 3819 | * @cfile: target cgroup_file |
| 3820 | * |
| 3821 | * @cfile must have been obtained by setting cftype->file_offset. |
| 3822 | */ |
| 3823 | void cgroup_file_notify(struct cgroup_file *cfile) |
| 3824 | { |
| 3825 | unsigned long flags; |
| 3826 | |
| 3827 | spin_lock_irqsave(&cgroup_file_kn_lock, flags); |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3828 | if (cfile->kn) { |
| 3829 | unsigned long last = cfile->notified_at; |
| 3830 | unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV; |
| 3831 | |
| 3832 | if (time_in_range(jiffies, last, next)) { |
| 3833 | timer_reduce(&cfile->notify_timer, next); |
| 3834 | } else { |
| 3835 | kernfs_notify(cfile->kn); |
| 3836 | cfile->notified_at = jiffies; |
| 3837 | } |
| 3838 | } |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3839 | spin_unlock_irqrestore(&cgroup_file_kn_lock, flags); |
| 3840 | } |
| 3841 | |
| 3842 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3843 | * css_next_child - find the next child of a given css |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3844 | * @pos: the current position (%NULL to initiate traversal) |
| 3845 | * @parent: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3846 | * |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3847 | * This function returns the next child of @parent and should be called |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3848 | * under either cgroup_mutex or RCU read lock. The only requirement is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3849 | * that @parent and @pos are accessible. The next sibling is guaranteed to |
| 3850 | * be returned regardless of their states. |
| 3851 | * |
| 3852 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 3853 | * css which finished ->css_online() is guaranteed to be visible in the |
| 3854 | * future iterations and will stay visible until the last reference is put. |
| 3855 | * A css which hasn't finished ->css_online() or already finished |
| 3856 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 3857 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3858 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3859 | struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, |
| 3860 | struct cgroup_subsys_state *parent) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3861 | { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3862 | struct cgroup_subsys_state *next; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3863 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3864 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3865 | |
| 3866 | /* |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 3867 | * @pos could already have been unlinked from the sibling list. |
| 3868 | * Once a cgroup is removed, its ->sibling.next is no longer |
| 3869 | * updated when its next sibling changes. CSS_RELEASED is set when |
| 3870 | * @pos is taken off list, at which time its next pointer is valid, |
| 3871 | * and, as releases are serialized, the one pointed to by the next |
| 3872 | * pointer is guaranteed to not have started release yet. This |
| 3873 | * implies that if we observe !CSS_RELEASED on @pos in this RCU |
| 3874 | * critical section, the one pointed to by its next pointer is |
| 3875 | * guaranteed to not have finished its RCU grace period even if we |
| 3876 | * have dropped rcu_read_lock() inbetween iterations. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3877 | * |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 3878 | * If @pos has CSS_RELEASED set, its next pointer can't be |
| 3879 | * dereferenced; however, as each css is given a monotonically |
| 3880 | * increasing unique serial number and always appended to the |
| 3881 | * sibling list, the next one can be found by walking the parent's |
| 3882 | * children until the first css with higher serial number than |
| 3883 | * @pos's. While this path can be slower, it happens iff iteration |
| 3884 | * races against release and the race window is very small. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3885 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3886 | if (!pos) { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3887 | next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling); |
| 3888 | } else if (likely(!(pos->flags & CSS_RELEASED))) { |
| 3889 | next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3890 | } else { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3891 | list_for_each_entry_rcu(next, &parent->children, sibling) |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3892 | if (next->serial_nr > pos->serial_nr) |
| 3893 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3894 | } |
| 3895 | |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3896 | /* |
| 3897 | * @next, if not pointing to the head, can be dereferenced and is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3898 | * the next sibling. |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3899 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3900 | if (&next->sibling != &parent->children) |
| 3901 | return next; |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3902 | return NULL; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3903 | } |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3904 | |
| 3905 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3906 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3907 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3908 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3909 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3910 | * 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] | 3911 | * to visit for pre-order traversal of @root's descendants. @root is |
| 3912 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3913 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3914 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3915 | * doesn't require the whole traversal to be contained in a single critical |
| 3916 | * section. This function will return the correct next descendant as long |
| 3917 | * 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] | 3918 | * |
| 3919 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 3920 | * css which finished ->css_online() is guaranteed to be visible in the |
| 3921 | * future iterations and will stay visible until the last reference is put. |
| 3922 | * A css which hasn't finished ->css_online() or already finished |
| 3923 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 3924 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3925 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3926 | struct cgroup_subsys_state * |
| 3927 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 3928 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3929 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3930 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3931 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3932 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3933 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3934 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3935 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3936 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3937 | |
| 3938 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3939 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3940 | if (next) |
| 3941 | return next; |
| 3942 | |
| 3943 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3944 | while (pos != root) { |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3945 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3946 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3947 | return next; |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3948 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3949 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3950 | |
| 3951 | return NULL; |
| 3952 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3953 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3954 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3955 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 3956 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3957 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3958 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 3959 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3960 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3961 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3962 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3963 | * doesn't require the whole traversal to be contained in a single critical |
| 3964 | * section. This function will return the correct rightmost descendant as |
| 3965 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3966 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3967 | struct cgroup_subsys_state * |
| 3968 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3969 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3970 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3971 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3972 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3973 | |
| 3974 | do { |
| 3975 | last = pos; |
| 3976 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3977 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3978 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3979 | pos = tmp; |
| 3980 | } while (pos); |
| 3981 | |
| 3982 | return last; |
| 3983 | } |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3984 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3985 | static struct cgroup_subsys_state * |
| 3986 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3987 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3988 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3989 | |
| 3990 | do { |
| 3991 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3992 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3993 | } while (pos); |
| 3994 | |
| 3995 | return last; |
| 3996 | } |
| 3997 | |
| 3998 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3999 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4000 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4001 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4002 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4003 | * 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] | 4004 | * to visit for post-order traversal of @root's descendants. @root is |
| 4005 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4006 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4007 | * While this function requires cgroup_mutex or RCU read locking, it |
| 4008 | * doesn't require the whole traversal to be contained in a single critical |
| 4009 | * section. This function will return the correct next descendant as long |
| 4010 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 4011 | * @cgroup. |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4012 | * |
| 4013 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 4014 | * css which finished ->css_online() is guaranteed to be visible in the |
| 4015 | * future iterations and will stay visible until the last reference is put. |
| 4016 | * A css which hasn't finished ->css_online() or already finished |
| 4017 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 4018 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4019 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4020 | struct cgroup_subsys_state * |
| 4021 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 4022 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4023 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4024 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4025 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4026 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4027 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 4028 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 4029 | if (!pos) |
| 4030 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4031 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4032 | /* if we visited @root, we're done */ |
| 4033 | if (pos == root) |
| 4034 | return NULL; |
| 4035 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4036 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4037 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4038 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4039 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4040 | |
| 4041 | /* no sibling left, visit parent */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4042 | return pos->parent; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4043 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4044 | |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4045 | /** |
| 4046 | * css_has_online_children - does a css have online children |
| 4047 | * @css: the target css |
| 4048 | * |
| 4049 | * Returns %true if @css has any online children; otherwise, %false. This |
| 4050 | * function can be called from any context but the caller is responsible |
| 4051 | * for synchronizing against on/offlining as necessary. |
| 4052 | */ |
| 4053 | bool css_has_online_children(struct cgroup_subsys_state *css) |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4054 | { |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4055 | struct cgroup_subsys_state *child; |
| 4056 | bool ret = false; |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4057 | |
| 4058 | rcu_read_lock(); |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4059 | css_for_each_child(child, css) { |
Li Zefan | 99bae5f | 2014-06-12 14:31:31 +0800 | [diff] [blame] | 4060 | if (child->flags & CSS_ONLINE) { |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4061 | ret = true; |
| 4062 | break; |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4063 | } |
| 4064 | } |
| 4065 | rcu_read_unlock(); |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4066 | return ret; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 4067 | } |
| 4068 | |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4069 | static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it) |
| 4070 | { |
| 4071 | struct list_head *l; |
| 4072 | struct cgrp_cset_link *link; |
| 4073 | struct css_set *cset; |
| 4074 | |
| 4075 | lockdep_assert_held(&css_set_lock); |
| 4076 | |
| 4077 | /* find the next threaded cset */ |
| 4078 | if (it->tcset_pos) { |
| 4079 | l = it->tcset_pos->next; |
| 4080 | |
| 4081 | if (l != it->tcset_head) { |
| 4082 | it->tcset_pos = l; |
| 4083 | return container_of(l, struct css_set, |
| 4084 | threaded_csets_node); |
| 4085 | } |
| 4086 | |
| 4087 | it->tcset_pos = NULL; |
| 4088 | } |
| 4089 | |
| 4090 | /* find the next cset */ |
| 4091 | l = it->cset_pos; |
| 4092 | l = l->next; |
| 4093 | if (l == it->cset_head) { |
| 4094 | it->cset_pos = NULL; |
| 4095 | return NULL; |
| 4096 | } |
| 4097 | |
| 4098 | if (it->ss) { |
| 4099 | cset = container_of(l, struct css_set, e_cset_node[it->ss->id]); |
| 4100 | } else { |
| 4101 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 4102 | cset = link->cset; |
| 4103 | } |
| 4104 | |
| 4105 | it->cset_pos = l; |
| 4106 | |
| 4107 | /* initialize threaded css_set walking */ |
| 4108 | if (it->flags & CSS_TASK_ITER_THREADED) { |
| 4109 | if (it->cur_dcset) |
| 4110 | put_css_set_locked(it->cur_dcset); |
| 4111 | it->cur_dcset = cset; |
| 4112 | get_css_set(cset); |
| 4113 | |
| 4114 | it->tcset_head = &cset->threaded_csets; |
| 4115 | it->tcset_pos = &cset->threaded_csets; |
| 4116 | } |
| 4117 | |
| 4118 | return cset; |
| 4119 | } |
| 4120 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4121 | /** |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4122 | * css_task_iter_advance_css_set - advance a task itererator to the next css_set |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4123 | * @it: the iterator to advance |
| 4124 | * |
| 4125 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4126 | */ |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4127 | static void css_task_iter_advance_css_set(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4128 | { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4129 | struct css_set *cset; |
| 4130 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 4131 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4132 | |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4133 | /* Advance to the next non-empty css_set */ |
| 4134 | do { |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4135 | cset = css_task_iter_next_css_set(it); |
| 4136 | if (!cset) { |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4137 | it->task_pos = NULL; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4138 | return; |
| 4139 | } |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 4140 | } while (!css_set_populated(cset)); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4141 | |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4142 | if (!list_empty(&cset->tasks)) |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4143 | it->task_pos = cset->tasks.next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4144 | else |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4145 | it->task_pos = cset->mg_tasks.next; |
| 4146 | |
| 4147 | it->tasks_head = &cset->tasks; |
| 4148 | it->mg_tasks_head = &cset->mg_tasks; |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4149 | |
| 4150 | /* |
| 4151 | * We don't keep css_sets locked across iteration steps and thus |
| 4152 | * need to take steps to ensure that iteration can be resumed after |
| 4153 | * the lock is re-acquired. Iteration is performed at two levels - |
| 4154 | * css_sets and tasks in them. |
| 4155 | * |
| 4156 | * Once created, a css_set never leaves its cgroup lists, so a |
| 4157 | * pinned css_set is guaranteed to stay put and we can resume |
| 4158 | * iteration afterwards. |
| 4159 | * |
| 4160 | * Tasks may leave @cset across iteration steps. This is resolved |
| 4161 | * by registering each iterator with the css_set currently being |
| 4162 | * walked and making css_set_move_task() advance iterators whose |
| 4163 | * next task is leaving. |
| 4164 | */ |
| 4165 | if (it->cur_cset) { |
| 4166 | list_del(&it->iters_node); |
| 4167 | put_css_set_locked(it->cur_cset); |
| 4168 | } |
| 4169 | get_css_set(cset); |
| 4170 | it->cur_cset = cset; |
| 4171 | list_add(&it->iters_node, &cset->task_iters); |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4172 | } |
| 4173 | |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4174 | static void css_task_iter_advance(struct css_task_iter *it) |
| 4175 | { |
Tejun Heo | 74d0833 | 2017-12-20 07:09:19 -0800 | [diff] [blame] | 4176 | struct list_head *next; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4177 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 4178 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4179 | repeat: |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4180 | /* |
| 4181 | * Advance iterator to find next entry. cset->tasks is consumed |
| 4182 | * first and then ->mg_tasks. After ->mg_tasks, we move onto the |
| 4183 | * next cset. |
| 4184 | */ |
Tejun Heo | 74d0833 | 2017-12-20 07:09:19 -0800 | [diff] [blame] | 4185 | next = it->task_pos->next; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4186 | |
Tejun Heo | 74d0833 | 2017-12-20 07:09:19 -0800 | [diff] [blame] | 4187 | if (next == it->tasks_head) |
| 4188 | next = it->mg_tasks_head->next; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4189 | |
Tejun Heo | 74d0833 | 2017-12-20 07:09:19 -0800 | [diff] [blame] | 4190 | if (next == it->mg_tasks_head) |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4191 | css_task_iter_advance_css_set(it); |
| 4192 | else |
Tejun Heo | 74d0833 | 2017-12-20 07:09:19 -0800 | [diff] [blame] | 4193 | it->task_pos = next; |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4194 | |
| 4195 | /* if PROCS, skip over tasks which aren't group leaders */ |
| 4196 | if ((it->flags & CSS_TASK_ITER_PROCS) && it->task_pos && |
| 4197 | !thread_group_leader(list_entry(it->task_pos, struct task_struct, |
| 4198 | cg_list))) |
| 4199 | goto repeat; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4200 | } |
| 4201 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4202 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4203 | * css_task_iter_start - initiate task iteration |
| 4204 | * @css: the css to walk tasks of |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4205 | * @flags: CSS_TASK_ITER_* flags |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4206 | * @it: the task iterator to use |
| 4207 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4208 | * Initiate iteration through the tasks of @css. The caller can call |
| 4209 | * css_task_iter_next() to walk through the tasks until the function |
| 4210 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 4211 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4212 | */ |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4213 | void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags, |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4214 | struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4215 | { |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 4216 | /* no one should try to iterate before mounting cgroups */ |
| 4217 | WARN_ON_ONCE(!use_task_css_set_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4218 | |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4219 | memset(it, 0, sizeof(*it)); |
| 4220 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4221 | spin_lock_irq(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4222 | |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4223 | it->ss = css->ss; |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4224 | it->flags = flags; |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4225 | |
| 4226 | if (it->ss) |
| 4227 | it->cset_pos = &css->cgroup->e_csets[css->ss->id]; |
| 4228 | else |
| 4229 | it->cset_pos = &css->cgroup->cset_links; |
| 4230 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4231 | it->cset_head = it->cset_pos; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4232 | |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4233 | css_task_iter_advance_css_set(it); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4234 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4235 | spin_unlock_irq(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4236 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4237 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4238 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4239 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4240 | * @it: the task iterator being iterated |
| 4241 | * |
| 4242 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4243 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 4244 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4245 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4246 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4247 | { |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4248 | if (it->cur_task) { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4249 | put_task_struct(it->cur_task); |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4250 | it->cur_task = NULL; |
| 4251 | } |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4252 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4253 | spin_lock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4254 | |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4255 | if (it->task_pos) { |
| 4256 | it->cur_task = list_entry(it->task_pos, struct task_struct, |
| 4257 | cg_list); |
| 4258 | get_task_struct(it->cur_task); |
| 4259 | css_task_iter_advance(it); |
| 4260 | } |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4261 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4262 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4263 | |
| 4264 | return it->cur_task; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4265 | } |
| 4266 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4267 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4268 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4269 | * @it: the task iterator to finish |
| 4270 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4271 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4272 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4273 | void css_task_iter_end(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4274 | { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4275 | if (it->cur_cset) { |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4276 | spin_lock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4277 | list_del(&it->iters_node); |
| 4278 | put_css_set_locked(it->cur_cset); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4279 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4280 | } |
| 4281 | |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4282 | if (it->cur_dcset) |
| 4283 | put_css_set(it->cur_dcset); |
| 4284 | |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4285 | if (it->cur_task) |
| 4286 | put_task_struct(it->cur_task); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4289 | static void cgroup_procs_release(struct kernfs_open_file *of) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4290 | { |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4291 | if (of->priv) { |
| 4292 | css_task_iter_end(of->priv); |
| 4293 | kfree(of->priv); |
| 4294 | } |
| 4295 | } |
| 4296 | |
| 4297 | static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos) |
| 4298 | { |
| 4299 | struct kernfs_open_file *of = s->private; |
| 4300 | struct css_task_iter *it = of->priv; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 4301 | |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4302 | return css_task_iter_next(it); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4303 | } |
| 4304 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4305 | static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos, |
| 4306 | unsigned int iter_flags) |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 4307 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4308 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4309 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4310 | struct css_task_iter *it = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4311 | |
| 4312 | /* |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4313 | * When a seq_file is seeked, it's always traversed sequentially |
| 4314 | * from position 0, so we can simply keep iterating on !0 *pos. |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4315 | */ |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4316 | if (!it) { |
| 4317 | if (WARN_ON_ONCE((*pos)++)) |
| 4318 | return ERR_PTR(-EINVAL); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4319 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4320 | it = kzalloc(sizeof(*it), GFP_KERNEL); |
| 4321 | if (!it) |
| 4322 | return ERR_PTR(-ENOMEM); |
| 4323 | of->priv = it; |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4324 | css_task_iter_start(&cgrp->self, iter_flags, it); |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4325 | } else if (!(*pos)++) { |
| 4326 | css_task_iter_end(it); |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4327 | css_task_iter_start(&cgrp->self, iter_flags, it); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4328 | } |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4329 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4330 | return cgroup_procs_next(s, NULL, NULL); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4331 | } |
| 4332 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4333 | static void *cgroup_procs_start(struct seq_file *s, loff_t *pos) |
| 4334 | { |
| 4335 | struct cgroup *cgrp = seq_css(s)->cgroup; |
| 4336 | |
| 4337 | /* |
| 4338 | * All processes of a threaded subtree belong to the domain cgroup |
| 4339 | * of the subtree. Only threads can be distributed across the |
| 4340 | * subtree. Reject reads on cgroup.procs in the subtree proper. |
| 4341 | * They're always empty anyway. |
| 4342 | */ |
| 4343 | if (cgroup_is_threaded(cgrp)) |
| 4344 | return ERR_PTR(-EOPNOTSUPP); |
| 4345 | |
| 4346 | return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS | |
| 4347 | CSS_TASK_ITER_THREADED); |
| 4348 | } |
| 4349 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4350 | static int cgroup_procs_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4351 | { |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4352 | seq_printf(s, "%d\n", task_pid_vnr(v)); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4353 | return 0; |
| 4354 | } |
| 4355 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4356 | static int cgroup_procs_write_permission(struct cgroup *src_cgrp, |
| 4357 | struct cgroup *dst_cgrp, |
| 4358 | struct super_block *sb) |
| 4359 | { |
| 4360 | struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; |
| 4361 | struct cgroup *com_cgrp = src_cgrp; |
| 4362 | struct inode *inode; |
| 4363 | int ret; |
| 4364 | |
| 4365 | lockdep_assert_held(&cgroup_mutex); |
| 4366 | |
| 4367 | /* find the common ancestor */ |
| 4368 | while (!cgroup_is_descendant(dst_cgrp, com_cgrp)) |
| 4369 | com_cgrp = cgroup_parent(com_cgrp); |
| 4370 | |
| 4371 | /* %current should be authorized to migrate to the common ancestor */ |
| 4372 | inode = kernfs_get_inode(sb, com_cgrp->procs_file.kn); |
| 4373 | if (!inode) |
| 4374 | return -ENOMEM; |
| 4375 | |
| 4376 | ret = inode_permission(inode, MAY_WRITE); |
| 4377 | iput(inode); |
| 4378 | if (ret) |
| 4379 | return ret; |
| 4380 | |
| 4381 | /* |
| 4382 | * If namespaces are delegation boundaries, %current must be able |
| 4383 | * to see both source and destination cgroups from its namespace. |
| 4384 | */ |
| 4385 | if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) && |
| 4386 | (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) || |
| 4387 | !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp))) |
| 4388 | return -ENOENT; |
| 4389 | |
| 4390 | return 0; |
| 4391 | } |
| 4392 | |
| 4393 | static ssize_t cgroup_procs_write(struct kernfs_open_file *of, |
| 4394 | char *buf, size_t nbytes, loff_t off) |
| 4395 | { |
| 4396 | struct cgroup *src_cgrp, *dst_cgrp; |
| 4397 | struct task_struct *task; |
| 4398 | ssize_t ret; |
| 4399 | |
| 4400 | dst_cgrp = cgroup_kn_lock_live(of->kn, false); |
| 4401 | if (!dst_cgrp) |
| 4402 | return -ENODEV; |
| 4403 | |
| 4404 | task = cgroup_procs_write_start(buf, true); |
| 4405 | ret = PTR_ERR_OR_ZERO(task); |
| 4406 | if (ret) |
| 4407 | goto out_unlock; |
| 4408 | |
| 4409 | /* find the source cgroup */ |
| 4410 | spin_lock_irq(&css_set_lock); |
| 4411 | src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); |
| 4412 | spin_unlock_irq(&css_set_lock); |
| 4413 | |
| 4414 | ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, |
| 4415 | of->file->f_path.dentry->d_sb); |
| 4416 | if (ret) |
| 4417 | goto out_finish; |
| 4418 | |
| 4419 | ret = cgroup_attach_task(dst_cgrp, task, true); |
| 4420 | |
| 4421 | out_finish: |
| 4422 | cgroup_procs_write_finish(task); |
| 4423 | out_unlock: |
| 4424 | cgroup_kn_unlock(of->kn); |
| 4425 | |
| 4426 | return ret ?: nbytes; |
| 4427 | } |
| 4428 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4429 | static void *cgroup_threads_start(struct seq_file *s, loff_t *pos) |
| 4430 | { |
| 4431 | return __cgroup_procs_start(s, pos, 0); |
| 4432 | } |
| 4433 | |
| 4434 | static ssize_t cgroup_threads_write(struct kernfs_open_file *of, |
| 4435 | char *buf, size_t nbytes, loff_t off) |
| 4436 | { |
| 4437 | struct cgroup *src_cgrp, *dst_cgrp; |
| 4438 | struct task_struct *task; |
| 4439 | ssize_t ret; |
| 4440 | |
| 4441 | buf = strstrip(buf); |
| 4442 | |
| 4443 | dst_cgrp = cgroup_kn_lock_live(of->kn, false); |
| 4444 | if (!dst_cgrp) |
| 4445 | return -ENODEV; |
| 4446 | |
| 4447 | task = cgroup_procs_write_start(buf, false); |
| 4448 | ret = PTR_ERR_OR_ZERO(task); |
| 4449 | if (ret) |
| 4450 | goto out_unlock; |
| 4451 | |
| 4452 | /* find the source cgroup */ |
| 4453 | spin_lock_irq(&css_set_lock); |
| 4454 | src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); |
| 4455 | spin_unlock_irq(&css_set_lock); |
| 4456 | |
| 4457 | /* thread migrations follow the cgroup.procs delegation rule */ |
| 4458 | ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, |
| 4459 | of->file->f_path.dentry->d_sb); |
| 4460 | if (ret) |
| 4461 | goto out_finish; |
| 4462 | |
| 4463 | /* and must be contained in the same domain */ |
| 4464 | ret = -EOPNOTSUPP; |
| 4465 | if (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp) |
| 4466 | goto out_finish; |
| 4467 | |
| 4468 | ret = cgroup_attach_task(dst_cgrp, task, false); |
| 4469 | |
| 4470 | out_finish: |
| 4471 | cgroup_procs_write_finish(task); |
| 4472 | out_unlock: |
| 4473 | cgroup_kn_unlock(of->kn); |
| 4474 | |
| 4475 | return ret ?: nbytes; |
| 4476 | } |
| 4477 | |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4478 | /* cgroup core interface files for the default hierarchy */ |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 4479 | static struct cftype cgroup_base_files[] = { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4480 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4481 | .name = "cgroup.type", |
| 4482 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4483 | .seq_show = cgroup_type_show, |
| 4484 | .write = cgroup_type_write, |
| 4485 | }, |
| 4486 | { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4487 | .name = "cgroup.procs", |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 4488 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4489 | .file_offset = offsetof(struct cgroup, procs_file), |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4490 | .release = cgroup_procs_release, |
| 4491 | .seq_start = cgroup_procs_start, |
| 4492 | .seq_next = cgroup_procs_next, |
| 4493 | .seq_show = cgroup_procs_show, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4494 | .write = cgroup_procs_write, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4495 | }, |
| 4496 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4497 | .name = "cgroup.threads", |
Roman Gushchin | 4f58424 | 2018-01-10 04:35:12 -0800 | [diff] [blame] | 4498 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4499 | .release = cgroup_procs_release, |
| 4500 | .seq_start = cgroup_threads_start, |
| 4501 | .seq_next = cgroup_procs_next, |
| 4502 | .seq_show = cgroup_procs_show, |
| 4503 | .write = cgroup_threads_write, |
| 4504 | }, |
| 4505 | { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4506 | .name = "cgroup.controllers", |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4507 | .seq_show = cgroup_controllers_show, |
| 4508 | }, |
| 4509 | { |
| 4510 | .name = "cgroup.subtree_control", |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 4511 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4512 | .seq_show = cgroup_subtree_control_show, |
| 4513 | .write = cgroup_subtree_control_write, |
| 4514 | }, |
| 4515 | { |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 4516 | .name = "cgroup.events", |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4517 | .flags = CFTYPE_NOT_ON_ROOT, |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4518 | .file_offset = offsetof(struct cgroup, events_file), |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 4519 | .seq_show = cgroup_events_show, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4520 | }, |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 4521 | { |
| 4522 | .name = "cgroup.max.descendants", |
| 4523 | .seq_show = cgroup_max_descendants_show, |
| 4524 | .write = cgroup_max_descendants_write, |
| 4525 | }, |
| 4526 | { |
| 4527 | .name = "cgroup.max.depth", |
| 4528 | .seq_show = cgroup_max_depth_show, |
| 4529 | .write = cgroup_max_depth_write, |
| 4530 | }, |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 4531 | { |
| 4532 | .name = "cgroup.stat", |
Tejun Heo | 3e48930 | 2017-08-11 05:49:01 -0700 | [diff] [blame] | 4533 | .seq_show = cgroup_stat_show, |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 4534 | }, |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 4535 | { |
| 4536 | .name = "cpu.stat", |
| 4537 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4538 | .seq_show = cpu_stat_show, |
| 4539 | }, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4540 | { } /* terminate */ |
| 4541 | }; |
| 4542 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4543 | /* |
| 4544 | * css destruction is four-stage process. |
| 4545 | * |
| 4546 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 4547 | * Implemented in kill_css(). |
| 4548 | * |
| 4549 | * 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] | 4550 | * and thus css_tryget_online() is guaranteed to fail, the css can be |
| 4551 | * offlined by invoking offline_css(). After offlining, the base ref is |
| 4552 | * put. Implemented in css_killed_work_fn(). |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4553 | * |
| 4554 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 4555 | * accessors are inside RCU read sections. css_release() schedules the |
| 4556 | * RCU callback. |
| 4557 | * |
| 4558 | * 4. After the grace period, the css can be freed. Implemented in |
| 4559 | * css_free_work_fn(). |
| 4560 | * |
| 4561 | * It is actually hairier because both step 2 and 4 require process context |
| 4562 | * and thus involve punting to css->destroy_work adding two additional |
| 4563 | * steps to the already complex sequence. |
| 4564 | */ |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4565 | static void css_free_rwork_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4566 | { |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4567 | struct cgroup_subsys_state *css = container_of(to_rcu_work(work), |
| 4568 | struct cgroup_subsys_state, destroy_rwork); |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4569 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4570 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4571 | |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 4572 | percpu_ref_exit(&css->refcnt); |
| 4573 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4574 | if (ss) { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4575 | /* css free path */ |
Tejun Heo | 8bb5ef7 | 2016-01-21 15:32:15 -0500 | [diff] [blame] | 4576 | struct cgroup_subsys_state *parent = css->parent; |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4577 | int id = css->id; |
| 4578 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4579 | ss->css_free(css); |
| 4580 | cgroup_idr_remove(&ss->css_idr, id); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4581 | cgroup_put(cgrp); |
Tejun Heo | 8bb5ef7 | 2016-01-21 15:32:15 -0500 | [diff] [blame] | 4582 | |
| 4583 | if (parent) |
| 4584 | css_put(parent); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4585 | } else { |
| 4586 | /* cgroup free path */ |
| 4587 | atomic_dec(&cgrp->root->nr_cgrps); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 4588 | cgroup1_pidlist_destroy_all(cgrp); |
Zefan Li | 971ff49 | 2014-09-18 16:06:19 +0800 | [diff] [blame] | 4589 | cancel_work_sync(&cgrp->release_agent_work); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4590 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4591 | if (cgroup_parent(cgrp)) { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4592 | /* |
| 4593 | * We get a ref to the parent, and put the ref when |
| 4594 | * this cgroup is being freed, so it's guaranteed |
| 4595 | * that the parent won't be destroyed before its |
| 4596 | * children. |
| 4597 | */ |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4598 | cgroup_put(cgroup_parent(cgrp)); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4599 | kernfs_put(cgrp->kn); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4600 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4601 | cgroup_rstat_exit(cgrp); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4602 | kfree(cgrp); |
| 4603 | } else { |
| 4604 | /* |
| 4605 | * This is root cgroup's refcnt reaching zero, |
| 4606 | * which indicates that the root should be |
| 4607 | * released. |
| 4608 | */ |
| 4609 | cgroup_destroy_root(cgrp->root); |
| 4610 | } |
| 4611 | } |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4612 | } |
| 4613 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4614 | static void css_release_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4615 | { |
| 4616 | struct cgroup_subsys_state *css = |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4617 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4618 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4619 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4620 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4621 | mutex_lock(&cgroup_mutex); |
| 4622 | |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4623 | css->flags |= CSS_RELEASED; |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4624 | list_del_rcu(&css->sibling); |
| 4625 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4626 | if (ss) { |
| 4627 | /* css release path */ |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 4628 | if (!list_empty(&css->rstat_css_node)) { |
| 4629 | cgroup_rstat_flush(cgrp); |
| 4630 | list_del_rcu(&css->rstat_css_node); |
| 4631 | } |
| 4632 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4633 | cgroup_idr_replace(&ss->css_idr, NULL, css->id); |
Tejun Heo | 7d172cc | 2014-11-18 02:49:51 -0500 | [diff] [blame] | 4634 | if (ss->css_released) |
| 4635 | ss->css_released(css); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4636 | } else { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4637 | struct cgroup *tcgrp; |
| 4638 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4639 | /* cgroup release path */ |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame^] | 4640 | TRACE_CGROUP_PATH(release, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 4641 | |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4642 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4643 | cgroup_rstat_flush(cgrp); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4644 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4645 | for (tcgrp = cgroup_parent(cgrp); tcgrp; |
| 4646 | tcgrp = cgroup_parent(tcgrp)) |
| 4647 | tcgrp->nr_dying_descendants--; |
| 4648 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4649 | cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 4650 | cgrp->id = -1; |
Li Zefan | a418948 | 2014-09-04 14:43:07 +0800 | [diff] [blame] | 4651 | |
| 4652 | /* |
| 4653 | * There are two control paths which try to determine |
| 4654 | * cgroup from dentry without going through kernfs - |
| 4655 | * cgroupstats_build() and css_tryget_online_from_dir(). |
| 4656 | * Those are supported by RCU protecting clearing of |
| 4657 | * cgrp->kn->priv backpointer. |
| 4658 | */ |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4659 | if (cgrp->kn) |
| 4660 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, |
| 4661 | NULL); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 4662 | |
| 4663 | cgroup_bpf_put(cgrp); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4664 | } |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4665 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4666 | mutex_unlock(&cgroup_mutex); |
| 4667 | |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4668 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); |
| 4669 | queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4670 | } |
| 4671 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4672 | static void css_release(struct percpu_ref *ref) |
| 4673 | { |
| 4674 | struct cgroup_subsys_state *css = |
| 4675 | container_of(ref, struct cgroup_subsys_state, refcnt); |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4676 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4677 | INIT_WORK(&css->destroy_work, css_release_work_fn); |
| 4678 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4679 | } |
| 4680 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4681 | static void init_and_link_css(struct cgroup_subsys_state *css, |
| 4682 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4683 | { |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4684 | lockdep_assert_held(&cgroup_mutex); |
| 4685 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 4686 | cgroup_get_live(cgrp); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4687 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4688 | memset(css, 0, sizeof(*css)); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4689 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4690 | css->ss = ss; |
Tejun Heo | 8fa3b8d | 2016-05-26 15:42:13 -0400 | [diff] [blame] | 4691 | css->id = -1; |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4692 | INIT_LIST_HEAD(&css->sibling); |
| 4693 | INIT_LIST_HEAD(&css->children); |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 4694 | INIT_LIST_HEAD(&css->rstat_css_node); |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4695 | css->serial_nr = css_serial_nr_next++; |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 4696 | atomic_set(&css->online_cnt, 0); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4697 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4698 | if (cgroup_parent(cgrp)) { |
| 4699 | css->parent = cgroup_css(cgroup_parent(cgrp), ss); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4700 | css_get(css->parent); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4701 | } |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4702 | |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 4703 | if (cgroup_on_dfl(cgrp) && ss->css_rstat_flush) |
| 4704 | list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list); |
| 4705 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4706 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4707 | } |
| 4708 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4709 | /* 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] | 4710 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4711 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4712 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4713 | int ret = 0; |
| 4714 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4715 | lockdep_assert_held(&cgroup_mutex); |
| 4716 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4717 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4718 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4719 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4720 | css->flags |= CSS_ONLINE; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4721 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 4722 | |
| 4723 | atomic_inc(&css->online_cnt); |
| 4724 | if (css->parent) |
| 4725 | atomic_inc(&css->parent->online_cnt); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4726 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4727 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4728 | } |
| 4729 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4730 | /* 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] | 4731 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4732 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4733 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4734 | |
| 4735 | lockdep_assert_held(&cgroup_mutex); |
| 4736 | |
| 4737 | if (!(css->flags & CSS_ONLINE)) |
| 4738 | return; |
| 4739 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4740 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4741 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4742 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4743 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | e329780 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4744 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4745 | |
| 4746 | wake_up_all(&css->cgroup->offline_waitq); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4747 | } |
| 4748 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4749 | /** |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4750 | * css_create - create a cgroup_subsys_state |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4751 | * @cgrp: the cgroup new css will be associated with |
| 4752 | * @ss: the subsys of new css |
| 4753 | * |
| 4754 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4755 | * css is online and installed in @cgrp. This function doesn't create the |
| 4756 | * interface files. Returns 0 on success, -errno on failure. |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4757 | */ |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4758 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 4759 | struct cgroup_subsys *ss) |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4760 | { |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4761 | struct cgroup *parent = cgroup_parent(cgrp); |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4762 | struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4763 | struct cgroup_subsys_state *css; |
| 4764 | int err; |
| 4765 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4766 | lockdep_assert_held(&cgroup_mutex); |
| 4767 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4768 | css = ss->css_alloc(parent_css); |
Tejun Heo | e7e15b8 | 2016-06-21 13:06:24 -0400 | [diff] [blame] | 4769 | if (!css) |
| 4770 | css = ERR_PTR(-ENOMEM); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4771 | if (IS_ERR(css)) |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4772 | return css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4773 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4774 | init_and_link_css(css, ss, cgrp); |
Tejun Heo | a2bed82 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4775 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 4776 | err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4777 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4778 | goto err_free_css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4779 | |
Vladimir Davydov | cf780b7 | 2015-08-03 15:32:26 +0300 | [diff] [blame] | 4780 | err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4781 | if (err < 0) |
Wenwei Tao | b00c52d | 2016-05-13 22:59:20 +0800 | [diff] [blame] | 4782 | goto err_free_css; |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4783 | css->id = err; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4784 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4785 | /* @css is ready to be brought online now, make it visible */ |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4786 | list_add_tail_rcu(&css->sibling, &parent_css->children); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4787 | cgroup_idr_replace(&ss->css_idr, css, css->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4788 | |
| 4789 | err = online_css(css); |
| 4790 | if (err) |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4791 | goto err_list_del; |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4792 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4793 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4794 | cgroup_parent(parent)) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4795 | 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] | 4796 | current->comm, current->pid, ss->name); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4797 | if (!strcmp(ss->name, "memory")) |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4798 | 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] | 4799 | ss->warned_broken_hierarchy = true; |
| 4800 | } |
| 4801 | |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4802 | return css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4803 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4804 | err_list_del: |
| 4805 | list_del_rcu(&css->sibling); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4806 | err_free_css: |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 4807 | list_del_rcu(&css->rstat_css_node); |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4808 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); |
| 4809 | queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4810 | return ERR_PTR(err); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4811 | } |
| 4812 | |
Tejun Heo | 07cd129 | 2017-01-26 16:47:28 -0500 | [diff] [blame] | 4813 | /* |
| 4814 | * The returned cgroup is fully initialized including its control mask, but |
| 4815 | * it isn't associated with its kernfs_node and doesn't have the control |
| 4816 | * mask applied. |
| 4817 | */ |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4818 | static struct cgroup *cgroup_create(struct cgroup *parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4819 | { |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4820 | struct cgroup_root *root = parent->root; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4821 | struct cgroup *cgrp, *tcgrp; |
| 4822 | int level = parent->level + 1; |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4823 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4824 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4825 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Kees Cook | acafe7e | 2018-05-08 13:45:50 -0700 | [diff] [blame] | 4826 | cgrp = kzalloc(struct_size(cgrp, ancestor_ids, (level + 1)), |
| 4827 | GFP_KERNEL); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4828 | if (!cgrp) |
| 4829 | return ERR_PTR(-ENOMEM); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4830 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 4831 | ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4832 | if (ret) |
| 4833 | goto out_free_cgrp; |
| 4834 | |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4835 | if (cgroup_on_dfl(parent)) { |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4836 | ret = cgroup_rstat_init(cgrp); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4837 | if (ret) |
| 4838 | goto out_cancel_ref; |
| 4839 | } |
| 4840 | |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4841 | /* |
| 4842 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4843 | * a half-baked cgroup. |
| 4844 | */ |
Vladimir Davydov | cf780b7 | 2015-08-03 15:32:26 +0300 | [diff] [blame] | 4845 | cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4846 | if (cgrp->id < 0) { |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4847 | ret = -ENOMEM; |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4848 | goto out_stat_exit; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4849 | } |
| 4850 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4851 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4852 | |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 4853 | cgrp->self.parent = &parent->self; |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4854 | cgrp->root = root; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 4855 | cgrp->level = level; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 4856 | ret = cgroup_bpf_inherit(cgrp); |
| 4857 | if (ret) |
| 4858 | goto out_idr_free; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 4859 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4860 | for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) { |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 4861 | cgrp->ancestor_ids[tcgrp->level] = tcgrp->id; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4862 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4863 | if (tcgrp != cgrp) |
| 4864 | tcgrp->nr_descendants++; |
| 4865 | } |
| 4866 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4867 | if (notify_on_release(parent)) |
| 4868 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4869 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4870 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4871 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4872 | |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4873 | cgrp->self.serial_nr = css_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4874 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4875 | /* allocation complete, commit to creation */ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4876 | list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4877 | atomic_inc(&root->nr_cgrps); |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 4878 | cgroup_get_live(parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4879 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4880 | /* |
| 4881 | * @cgrp is now fully operational. If something fails after this |
| 4882 | * point, it'll be released via the normal destruction path. |
| 4883 | */ |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4884 | cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4885 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4886 | /* |
| 4887 | * On the default hierarchy, a child doesn't automatically inherit |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 4888 | * subtree_control from the parent. Each is configured manually. |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4889 | */ |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4890 | if (!cgroup_on_dfl(cgrp)) |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4891 | cgrp->subtree_control = cgroup_control(cgrp); |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4892 | |
| 4893 | cgroup_propagate_control(cgrp); |
| 4894 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4895 | return cgrp; |
| 4896 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 4897 | out_idr_free: |
| 4898 | cgroup_idr_remove(&root->cgroup_idr, cgrp->id); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4899 | out_stat_exit: |
| 4900 | if (cgroup_on_dfl(parent)) |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4901 | cgroup_rstat_exit(cgrp); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4902 | out_cancel_ref: |
| 4903 | percpu_ref_exit(&cgrp->self.refcnt); |
| 4904 | out_free_cgrp: |
| 4905 | kfree(cgrp); |
| 4906 | return ERR_PTR(ret); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4907 | } |
| 4908 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 4909 | static bool cgroup_check_hierarchy_limits(struct cgroup *parent) |
| 4910 | { |
| 4911 | struct cgroup *cgroup; |
| 4912 | int ret = false; |
| 4913 | int level = 1; |
| 4914 | |
| 4915 | lockdep_assert_held(&cgroup_mutex); |
| 4916 | |
| 4917 | for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) { |
| 4918 | if (cgroup->nr_descendants >= cgroup->max_descendants) |
| 4919 | goto fail; |
| 4920 | |
| 4921 | if (level > cgroup->max_depth) |
| 4922 | goto fail; |
| 4923 | |
| 4924 | level++; |
| 4925 | } |
| 4926 | |
| 4927 | ret = true; |
| 4928 | fail: |
| 4929 | return ret; |
| 4930 | } |
| 4931 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 4932 | int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode) |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4933 | { |
| 4934 | struct cgroup *parent, *cgrp; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4935 | struct kernfs_node *kn; |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4936 | int ret; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4937 | |
| 4938 | /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */ |
| 4939 | if (strchr(name, '\n')) |
| 4940 | return -EINVAL; |
| 4941 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4942 | parent = cgroup_kn_lock_live(parent_kn, false); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4943 | if (!parent) |
| 4944 | return -ENODEV; |
| 4945 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 4946 | if (!cgroup_check_hierarchy_limits(parent)) { |
| 4947 | ret = -EAGAIN; |
| 4948 | goto out_unlock; |
| 4949 | } |
| 4950 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4951 | cgrp = cgroup_create(parent); |
| 4952 | if (IS_ERR(cgrp)) { |
| 4953 | ret = PTR_ERR(cgrp); |
| 4954 | goto out_unlock; |
| 4955 | } |
| 4956 | |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4957 | /* create the directory */ |
| 4958 | kn = kernfs_create_dir(parent->kn, name, mode, cgrp); |
| 4959 | if (IS_ERR(kn)) { |
| 4960 | ret = PTR_ERR(kn); |
| 4961 | goto out_destroy; |
| 4962 | } |
| 4963 | cgrp->kn = kn; |
| 4964 | |
| 4965 | /* |
| 4966 | * This extra ref will be put in cgroup_free_fn() and guarantees |
| 4967 | * that @cgrp->kn is always accessible. |
| 4968 | */ |
| 4969 | kernfs_get(kn); |
| 4970 | |
| 4971 | ret = cgroup_kn_set_ugid(kn); |
| 4972 | if (ret) |
| 4973 | goto out_destroy; |
| 4974 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 4975 | ret = css_populate_dir(&cgrp->self); |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4976 | if (ret) |
| 4977 | goto out_destroy; |
| 4978 | |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4979 | ret = cgroup_apply_control_enable(cgrp); |
| 4980 | if (ret) |
| 4981 | goto out_destroy; |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4982 | |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame^] | 4983 | TRACE_CGROUP_PATH(mkdir, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 4984 | |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4985 | /* let's create and online css's */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4986 | kernfs_activate(kn); |
| 4987 | |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4988 | ret = 0; |
| 4989 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4990 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4991 | out_destroy: |
| 4992 | cgroup_destroy_locked(cgrp); |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4993 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4994 | cgroup_kn_unlock(parent_kn); |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 4995 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4996 | } |
| 4997 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4998 | /* |
| 4999 | * 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] | 5000 | * css_tryget_online() is now guaranteed to fail. Tell the subsystem to |
| 5001 | * initate destruction and put the css ref from kill_css(). |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5002 | */ |
| 5003 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5004 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5005 | struct cgroup_subsys_state *css = |
| 5006 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5007 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5008 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5009 | |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5010 | do { |
| 5011 | offline_css(css); |
| 5012 | css_put(css); |
| 5013 | /* @css can't go away while we're holding cgroup_mutex */ |
| 5014 | css = css->parent; |
| 5015 | } while (css && atomic_dec_and_test(&css->online_cnt)); |
| 5016 | |
| 5017 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5018 | } |
| 5019 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5020 | /* css kill confirmation processing requires process context, bounce */ |
| 5021 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5022 | { |
| 5023 | struct cgroup_subsys_state *css = |
| 5024 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 5025 | |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5026 | if (atomic_dec_and_test(&css->online_cnt)) { |
| 5027 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
| 5028 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
| 5029 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5030 | } |
| 5031 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 5032 | /** |
| 5033 | * kill_css - destroy a css |
| 5034 | * @css: css to destroy |
| 5035 | * |
| 5036 | * This function initiates destruction of @css by removing cgroup interface |
| 5037 | * files and putting its base reference. ->css_offline() will be invoked |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5038 | * asynchronously once css_tryget_online() is guaranteed to fail and when |
| 5039 | * the reference count reaches zero, @css will be released. |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 5040 | */ |
| 5041 | static void kill_css(struct cgroup_subsys_state *css) |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5042 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 5043 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5044 | |
Waiman Long | 33c35aa | 2017-05-15 09:34:06 -0400 | [diff] [blame] | 5045 | if (css->flags & CSS_DYING) |
| 5046 | return; |
| 5047 | |
| 5048 | css->flags |= CSS_DYING; |
| 5049 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5050 | /* |
| 5051 | * This must happen before css is disassociated with its cgroup. |
| 5052 | * See seq_css() for details. |
| 5053 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 5054 | css_clear_dir(css); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5055 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5056 | /* |
| 5057 | * Killing would put the base ref, but we need to keep it alive |
| 5058 | * until after ->css_offline(). |
| 5059 | */ |
| 5060 | css_get(css); |
| 5061 | |
| 5062 | /* |
| 5063 | * cgroup core guarantees that, by the time ->css_offline() is |
| 5064 | * invoked, no new css reference will be given out via |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5065 | * css_tryget_online(). We can't simply call percpu_ref_kill() and |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5066 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 5067 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 5068 | * |
| 5069 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 5070 | * css is confirmed to be seen as killed on all CPUs. |
| 5071 | */ |
| 5072 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5073 | } |
| 5074 | |
| 5075 | /** |
| 5076 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 5077 | * @cgrp: cgroup to be destroyed |
| 5078 | * |
| 5079 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 5080 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5081 | * guarantee that css_tryget_online() won't succeed by the time |
| 5082 | * ->css_offline() is invoked. To satisfy all the requirements, |
| 5083 | * destruction is implemented in the following two steps. |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5084 | * |
| 5085 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 5086 | * userland visible parts and start killing the percpu refcnts of |
| 5087 | * css's. Set up so that the next stage will be kicked off once all |
| 5088 | * the percpu refcnts are confirmed to be killed. |
| 5089 | * |
| 5090 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 5091 | * rest of destruction. Once all cgroup references are gone, the |
| 5092 | * cgroup is RCU-freed. |
| 5093 | * |
| 5094 | * This function implements s1. After this step, @cgrp is gone as far as |
| 5095 | * the userland is concerned and a new cgroup with the same name may be |
| 5096 | * created. As cgroup doesn't care about the names internally, this |
| 5097 | * doesn't cause any problem. |
| 5098 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5099 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 5100 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5101 | { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5102 | struct cgroup *tcgrp, *parent = cgroup_parent(cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5103 | struct cgroup_subsys_state *css; |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5104 | struct cgrp_cset_link *link; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5105 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5106 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5107 | lockdep_assert_held(&cgroup_mutex); |
| 5108 | |
Tejun Heo | 91486f6 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 5109 | /* |
| 5110 | * Only migration can raise populated from zero and we're already |
| 5111 | * holding cgroup_mutex. |
| 5112 | */ |
| 5113 | if (cgroup_is_populated(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5114 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5115 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5116 | /* |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5117 | * Make sure there's no live children. We can't test emptiness of |
| 5118 | * ->self.children as dead children linger on it while being |
| 5119 | * drained; otherwise, "rmdir parent/child parent" may fail. |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 5120 | */ |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 5121 | if (css_has_online_children(&cgrp->self)) |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 5122 | return -EBUSY; |
| 5123 | |
| 5124 | /* |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5125 | * Mark @cgrp and the associated csets dead. The former prevents |
| 5126 | * further task migration and child creation by disabling |
| 5127 | * cgroup_lock_live_group(). The latter makes the csets ignored by |
| 5128 | * the migration path. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5129 | */ |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 5130 | cgrp->self.flags &= ~CSS_ONLINE; |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5131 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5132 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5133 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 5134 | link->cset->dead = true; |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5135 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5136 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5137 | /* initiate massacre of all css's */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5138 | for_each_css(css, ssid, cgrp) |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5139 | kill_css(css); |
| 5140 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 5141 | /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */ |
| 5142 | css_clear_dir(&cgrp->self); |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 5143 | kernfs_remove(cgrp->kn); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5144 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 5145 | if (parent && cgroup_is_threaded(cgrp)) |
| 5146 | parent->nr_threaded_children--; |
| 5147 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5148 | for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) { |
| 5149 | tcgrp->nr_descendants--; |
| 5150 | tcgrp->nr_dying_descendants++; |
| 5151 | } |
| 5152 | |
Roman Gushchin | 5a621e6 | 2017-08-02 17:55:32 +0100 | [diff] [blame] | 5153 | cgroup1_check_for_release(parent); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5154 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5155 | /* put the base reference */ |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5156 | percpu_ref_kill(&cgrp->self.refcnt); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5157 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 5158 | return 0; |
| 5159 | }; |
| 5160 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5161 | int cgroup_rmdir(struct kernfs_node *kn) |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5162 | { |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5163 | struct cgroup *cgrp; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5164 | int ret = 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5165 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5166 | cgrp = cgroup_kn_lock_live(kn, false); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5167 | if (!cgrp) |
| 5168 | return 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5169 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5170 | ret = cgroup_destroy_locked(cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5171 | if (!ret) |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame^] | 5172 | TRACE_CGROUP_PATH(rmdir, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5173 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5174 | cgroup_kn_unlock(kn); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5175 | return ret; |
| 5176 | } |
| 5177 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5178 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 5179 | .show_options = cgroup_show_options, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5180 | .remount_fs = cgroup_remount, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5181 | .mkdir = cgroup_mkdir, |
| 5182 | .rmdir = cgroup_rmdir, |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 5183 | .show_path = cgroup_show_path, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5184 | }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5185 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5186 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5187 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5188 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 5189 | |
Tejun Heo | a5ae989 | 2015-12-29 14:53:56 -0500 | [diff] [blame] | 5190 | pr_debug("Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5191 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5192 | mutex_lock(&cgroup_mutex); |
| 5193 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5194 | idr_init(&ss->css_idr); |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 5195 | INIT_LIST_HEAD(&ss->cfts); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5196 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5197 | /* Create the root cgroup state for this subsystem */ |
| 5198 | ss->root = &cgrp_dfl_root; |
| 5199 | css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5200 | /* We don't handle early failures gracefully */ |
| 5201 | BUG_ON(IS_ERR(css)); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5202 | init_and_link_css(css, ss, &cgrp_dfl_root.cgrp); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 5203 | |
| 5204 | /* |
| 5205 | * Root csses are never destroyed and we can't initialize |
| 5206 | * percpu_ref during early init. Disable refcnting. |
| 5207 | */ |
| 5208 | css->flags |= CSS_NO_REF; |
| 5209 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5210 | if (early) { |
Tejun Heo | 9395a45 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5211 | /* allocation can't be done safely during early init */ |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5212 | css->id = 1; |
| 5213 | } else { |
| 5214 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL); |
| 5215 | BUG_ON(css->id < 0); |
| 5216 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5217 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5218 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5219 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5220 | * newly registered, all tasks and hence the |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5221 | * init_css_set is in the subsystem's root cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5222 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5223 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 5224 | have_fork_callback |= (bool)ss->fork << ss->id; |
| 5225 | have_exit_callback |= (bool)ss->exit << ss->id; |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5226 | have_free_callback |= (bool)ss->free << ss->id; |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5227 | have_canfork_callback |= (bool)ss->can_fork << ss->id; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5228 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5229 | /* At system boot, before all subsystems have been |
| 5230 | * registered, no tasks have been forked, so we don't |
| 5231 | * need to invoke fork callbacks here. */ |
| 5232 | BUG_ON(!list_empty(&init_task.tasks)); |
| 5233 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5234 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 5235 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5236 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5240 | * cgroup_init_early - cgroup initialization at system boot |
| 5241 | * |
| 5242 | * Initialize cgroups at system boot, and initialize any |
| 5243 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5244 | */ |
| 5245 | int __init cgroup_init_early(void) |
| 5246 | { |
Tejun Heo | 7b9a6ba | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 5247 | static struct cgroup_sb_opts __initdata opts; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5248 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5249 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5250 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5251 | init_cgroup_root(&cgrp_dfl_root, &opts); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 5252 | cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF; |
| 5253 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5254 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5255 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5256 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5257 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Xiubo Li | 63253ad | 2016-02-26 13:07:38 +0800 | [diff] [blame] | 5258 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n", |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5259 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5260 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5261 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 5262 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5263 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5264 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5265 | ss->name = cgroup_subsys_name[i]; |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5266 | if (!ss->legacy_name) |
| 5267 | ss->legacy_name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5268 | |
| 5269 | if (ss->early_init) |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5270 | cgroup_init_subsys(ss, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5271 | } |
| 5272 | return 0; |
| 5273 | } |
| 5274 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 5275 | static u16 cgroup_disable_mask __initdata; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5276 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5277 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5278 | * cgroup_init - cgroup initialization |
| 5279 | * |
| 5280 | * Register cgroup filesystem and /proc file, and initialize |
| 5281 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5282 | */ |
| 5283 | int __init cgroup_init(void) |
| 5284 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5285 | struct cgroup_subsys *ss; |
Tejun Heo | 035f4f5 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 5286 | int ssid; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5287 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 5288 | BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16); |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 5289 | BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem)); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5290 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); |
| 5291 | BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5292 | |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 5293 | cgroup_rstat_boot(); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5294 | |
Peter Zijlstra | 3942a9b | 2016-08-11 18:54:13 +0200 | [diff] [blame] | 5295 | /* |
| 5296 | * The latency of the synchronize_sched() is too high for cgroups, |
| 5297 | * avoid it at the cost of forcing all readers into the slow path. |
| 5298 | */ |
| 5299 | rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss); |
| 5300 | |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 5301 | get_user_ns(init_cgroup_ns.user_ns); |
| 5302 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5303 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5304 | |
Tejun Heo | 2378d8b | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 5305 | /* |
| 5306 | * Add init_css_set to the hash table so that dfl_root can link to |
| 5307 | * it during init. |
| 5308 | */ |
| 5309 | hash_add(css_set_table, &init_css_set.hlist, |
| 5310 | css_set_hash(init_css_set.subsys)); |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5311 | |
Zefan Li | 9732adc | 2017-04-19 10:15:59 +0800 | [diff] [blame] | 5312 | BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0, 0)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5313 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5314 | mutex_unlock(&cgroup_mutex); |
| 5315 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5316 | for_each_subsys(ss, ssid) { |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5317 | if (ss->early_init) { |
| 5318 | struct cgroup_subsys_state *css = |
| 5319 | init_css_set.subsys[ss->id]; |
| 5320 | |
| 5321 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, |
| 5322 | GFP_KERNEL); |
| 5323 | BUG_ON(css->id < 0); |
| 5324 | } else { |
| 5325 | cgroup_init_subsys(ss, false); |
| 5326 | } |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5327 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 5328 | list_add_tail(&init_css_set.e_cset_node[ssid], |
| 5329 | &cgrp_dfl_root.cgrp.e_csets[ssid]); |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5330 | |
| 5331 | /* |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 5332 | * Setting dfl_root subsys_mask needs to consider the |
| 5333 | * disabled flag and cftype registration needs kmalloc, |
| 5334 | * both of which aren't available during early_init. |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5335 | */ |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5336 | if (cgroup_disable_mask & (1 << ssid)) { |
| 5337 | static_branch_disable(cgroup_subsys_enabled_key[ssid]); |
| 5338 | printk(KERN_INFO "Disabling %s control group subsystem\n", |
| 5339 | ss->name); |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5340 | continue; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5341 | } |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5342 | |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5343 | if (cgroup1_ssid_disabled(ssid)) |
Johannes Weiner | 223ffb2 | 2016-02-11 13:34:49 -0500 | [diff] [blame] | 5344 | printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n", |
| 5345 | ss->name); |
| 5346 | |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5347 | cgrp_dfl_root.subsys_mask |= 1 << ss->id; |
| 5348 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5349 | /* implicit controllers must be threaded too */ |
| 5350 | WARN_ON(ss->implicit_on_dfl && !ss->threaded); |
| 5351 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 5352 | if (ss->implicit_on_dfl) |
| 5353 | cgrp_dfl_implicit_ss_mask |= 1 << ss->id; |
| 5354 | else if (!ss->dfl_cftypes) |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 5355 | cgrp_dfl_inhibit_ss_mask |= 1 << ss->id; |
Tejun Heo | 5de4fa1 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5356 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5357 | if (ss->threaded) |
| 5358 | cgrp_dfl_threaded_ss_mask |= 1 << ss->id; |
| 5359 | |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5360 | if (ss->dfl_cftypes == ss->legacy_cftypes) { |
| 5361 | WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes)); |
| 5362 | } else { |
| 5363 | WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes)); |
| 5364 | WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes)); |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 5365 | } |
Vladimir Davydov | 295458e | 2015-02-19 17:34:46 +0300 | [diff] [blame] | 5366 | |
| 5367 | if (ss->bind) |
| 5368 | ss->bind(init_css_set.subsys[ssid]); |
Tejun Heo | 7af608e | 2017-07-18 17:57:46 -0400 | [diff] [blame] | 5369 | |
| 5370 | mutex_lock(&cgroup_mutex); |
| 5371 | css_populate_dir(init_css_set.subsys[ssid]); |
| 5372 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5373 | } |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5374 | |
Tejun Heo | 2378d8b | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 5375 | /* init_css_set.subsys[] has been updated, re-hash */ |
| 5376 | hash_del(&init_css_set.hlist); |
| 5377 | hash_add(css_set_table, &init_css_set.hlist, |
| 5378 | css_set_hash(init_css_set.subsys)); |
| 5379 | |
Tejun Heo | 035f4f5 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 5380 | WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup")); |
| 5381 | WARN_ON(register_filesystem(&cgroup_fs_type)); |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 5382 | WARN_ON(register_filesystem(&cgroup2_fs_type)); |
Christoph Hellwig | 3f3942a | 2018-05-15 15:57:23 +0200 | [diff] [blame] | 5383 | WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show)); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5384 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5385 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5386 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5387 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5388 | static int __init cgroup_wq_init(void) |
| 5389 | { |
| 5390 | /* |
| 5391 | * There isn't much point in executing destruction path in |
| 5392 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 5393 | * Use 1 for @max_active. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5394 | * |
| 5395 | * We would prefer to do this in cgroup_init() above, but that |
| 5396 | * is called before init_workqueues(): so leave this until after. |
| 5397 | */ |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 5398 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5399 | BUG_ON(!cgroup_destroy_wq); |
| 5400 | return 0; |
| 5401 | } |
| 5402 | core_initcall(cgroup_wq_init); |
| 5403 | |
Shaohua Li | 69fd5c3 | 2017-07-12 11:49:55 -0700 | [diff] [blame] | 5404 | void cgroup_path_from_kernfs_id(const union kernfs_node_id *id, |
| 5405 | char *buf, size_t buflen) |
| 5406 | { |
| 5407 | struct kernfs_node *kn; |
| 5408 | |
| 5409 | kn = kernfs_get_node_by_id(cgrp_dfl_root.kf_root, id); |
| 5410 | if (!kn) |
| 5411 | return; |
| 5412 | kernfs_path(kn, buf, buflen); |
| 5413 | kernfs_put(kn); |
| 5414 | } |
| 5415 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5416 | /* |
| 5417 | * proc_cgroup_show() |
| 5418 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 5419 | * - Used for /proc/<pid>/cgroup. |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5420 | */ |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 5421 | int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, |
| 5422 | struct pid *pid, struct task_struct *tsk) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5423 | { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5424 | char *buf; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5425 | int retval; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5426 | struct cgroup_root *root; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5427 | |
| 5428 | retval = -ENOMEM; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5429 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5430 | if (!buf) |
| 5431 | goto out; |
| 5432 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5433 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5434 | spin_lock_irq(&css_set_lock); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5435 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5436 | for_each_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5437 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5438 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 5439 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5440 | |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 5441 | if (root == &cgrp_dfl_root && !cgrp_dfl_visible) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5442 | continue; |
| 5443 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5444 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | d98817d | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5445 | if (root != &cgrp_dfl_root) |
| 5446 | for_each_subsys(ss, ssid) |
| 5447 | if (root->subsys_mask & (1 << ssid)) |
| 5448 | seq_printf(m, "%s%s", count++ ? "," : "", |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5449 | ss->legacy_name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 5450 | if (strlen(root->name)) |
| 5451 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 5452 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5453 | seq_putc(m, ':'); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5454 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5455 | cgrp = task_cgroup_from_root(tsk, root); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5456 | |
| 5457 | /* |
| 5458 | * On traditional hierarchies, all zombie tasks show up as |
| 5459 | * belonging to the root cgroup. On the default hierarchy, |
| 5460 | * while a zombie doesn't show up in "cgroup.procs" and |
| 5461 | * thus can't be migrated, its /proc/PID/cgroup keeps |
| 5462 | * reporting the cgroup it belonged to before exiting. If |
| 5463 | * the cgroup is removed before the zombie is reaped, |
| 5464 | * " (deleted)" is appended to the cgroup path. |
| 5465 | */ |
| 5466 | if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5467 | retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX, |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 5468 | current->nsproxy->cgroup_ns); |
Tejun Heo | e022300 | 2016-09-29 15:49:40 +0200 | [diff] [blame] | 5469 | if (retval >= PATH_MAX) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5470 | retval = -ENAMETOOLONG; |
Tejun Heo | e022300 | 2016-09-29 15:49:40 +0200 | [diff] [blame] | 5471 | if (retval < 0) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5472 | goto out_unlock; |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5473 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5474 | seq_puts(m, buf); |
| 5475 | } else { |
| 5476 | seq_puts(m, "/"); |
| 5477 | } |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5478 | |
| 5479 | if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp)) |
| 5480 | seq_puts(m, " (deleted)\n"); |
| 5481 | else |
| 5482 | seq_putc(m, '\n'); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5483 | } |
| 5484 | |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 5485 | retval = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5486 | out_unlock: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5487 | spin_unlock_irq(&css_set_lock); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5488 | mutex_unlock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5489 | kfree(buf); |
| 5490 | out: |
| 5491 | return retval; |
| 5492 | } |
| 5493 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5494 | /** |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5495 | * cgroup_fork - initialize cgroup related fields during copy_process() |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5496 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5497 | * |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5498 | * A task is associated with the init_css_set until cgroup_post_fork() |
| 5499 | * attaches it to the parent's css_set. Empty cg_list indicates that |
| 5500 | * @child isn't holding reference to its css_set. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5501 | */ |
| 5502 | void cgroup_fork(struct task_struct *child) |
| 5503 | { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5504 | RCU_INIT_POINTER(child->cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5505 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | /** |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5509 | * cgroup_can_fork - called on a new task before the process is exposed |
| 5510 | * @child: the task in question. |
| 5511 | * |
| 5512 | * This calls the subsystem can_fork() callbacks. If the can_fork() callback |
| 5513 | * returns an error, the fork aborts with that error code. This allows for |
| 5514 | * a cgroup subsystem to conditionally allow or deny new forks. |
| 5515 | */ |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5516 | int cgroup_can_fork(struct task_struct *child) |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5517 | { |
| 5518 | struct cgroup_subsys *ss; |
| 5519 | int i, j, ret; |
| 5520 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5521 | do_each_subsys_mask(ss, i, have_canfork_callback) { |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5522 | ret = ss->can_fork(child); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5523 | if (ret) |
| 5524 | goto out_revert; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5525 | } while_each_subsys_mask(); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5526 | |
| 5527 | return 0; |
| 5528 | |
| 5529 | out_revert: |
| 5530 | for_each_subsys(ss, j) { |
| 5531 | if (j >= i) |
| 5532 | break; |
| 5533 | if (ss->cancel_fork) |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5534 | ss->cancel_fork(child); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | return ret; |
| 5538 | } |
| 5539 | |
| 5540 | /** |
| 5541 | * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork() |
| 5542 | * @child: the task in question |
| 5543 | * |
| 5544 | * This calls the cancel_fork() callbacks if a fork failed *after* |
| 5545 | * cgroup_can_fork() succeded. |
| 5546 | */ |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5547 | void cgroup_cancel_fork(struct task_struct *child) |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5548 | { |
| 5549 | struct cgroup_subsys *ss; |
| 5550 | int i; |
| 5551 | |
| 5552 | for_each_subsys(ss, i) |
| 5553 | if (ss->cancel_fork) |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5554 | ss->cancel_fork(child); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5555 | } |
| 5556 | |
| 5557 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5558 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5559 | * @child: the task in question |
| 5560 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5561 | * Adds the task to the list running through its css_set if necessary and |
| 5562 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5563 | * 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] | 5564 | * 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] | 5565 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5566 | */ |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5567 | void cgroup_post_fork(struct task_struct *child) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5568 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5569 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5570 | int i; |
| 5571 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5572 | /* |
Dongsheng Yang | 251f8c0 | 2014-08-25 19:27:52 +0800 | [diff] [blame] | 5573 | * This may race against cgroup_enable_task_cg_lists(). As that |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5574 | * function sets use_task_css_set_links before grabbing |
| 5575 | * tasklist_lock and we just went through tasklist_lock to add |
| 5576 | * @child, it's guaranteed that either we see the set |
| 5577 | * use_task_css_set_links or cgroup_enable_task_cg_lists() sees |
| 5578 | * @child during its iteration. |
| 5579 | * |
| 5580 | * If we won the race, @child is associated with %current's |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5581 | * css_set. Grabbing css_set_lock guarantees both that the |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5582 | * association is stable, and, on completion of the parent's |
| 5583 | * migration, @child is visible in the source of migration or |
| 5584 | * already in the destination cgroup. This guarantee is necessary |
| 5585 | * when implementing operations which need to migrate all tasks of |
| 5586 | * a cgroup to another. |
| 5587 | * |
Dongsheng Yang | 251f8c0 | 2014-08-25 19:27:52 +0800 | [diff] [blame] | 5588 | * Note that if we lose to cgroup_enable_task_cg_lists(), @child |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5589 | * will remain in init_css_set. This is safe because all tasks are |
| 5590 | * in the init_css_set before cg_links is enabled and there's no |
| 5591 | * operation which transfers all tasks out of init_css_set. |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5592 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5593 | if (use_task_css_set_links) { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5594 | struct css_set *cset; |
| 5595 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5596 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5597 | cset = task_css_set(current); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5598 | if (list_empty(&child->cg_list)) { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5599 | get_css_set(cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 5600 | cset->nr_tasks++; |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 5601 | css_set_move_task(child, NULL, cset, false); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5602 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5603 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5604 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5605 | |
| 5606 | /* |
| 5607 | * Call ss->fork(). This must happen after @child is linked on |
| 5608 | * css_set; otherwise, @child might change state between ->fork() |
| 5609 | * and addition to css_set. |
| 5610 | */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5611 | do_each_subsys_mask(ss, i, have_fork_callback) { |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5612 | ss->fork(child); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5613 | } while_each_subsys_mask(); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5614 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5615 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5616 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5617 | * cgroup_exit - detach cgroup from exiting task |
| 5618 | * @tsk: pointer to task_struct of exiting process |
| 5619 | * |
| 5620 | * Description: Detach cgroup from @tsk and release it. |
| 5621 | * |
| 5622 | * Note that cgroups marked notify_on_release force every task in |
| 5623 | * them to take the global cgroup_mutex mutex when exiting. |
| 5624 | * This could impact scaling on very large systems. Be reluctant to |
| 5625 | * use notify_on_release cgroups where very high task exit scaling |
| 5626 | * is required on large systems. |
| 5627 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5628 | * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We |
| 5629 | * call cgroup_exit() while the task is still competent to handle |
| 5630 | * notify_on_release(), then leave the task attached to the root cgroup in |
| 5631 | * each hierarchy for the remainder of its exit. No need to bother with |
| 5632 | * 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] | 5633 | * with migration path - PF_EXITING is visible to migration path. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5634 | */ |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 5635 | void cgroup_exit(struct task_struct *tsk) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5636 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5637 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5638 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5639 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5640 | |
| 5641 | /* |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5642 | * Unlink from @tsk from its css_set. As migration path can't race |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 5643 | * with us, we can check css_set and cg_list without synchronization. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5644 | */ |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 5645 | cset = task_css_set(tsk); |
| 5646 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5647 | if (!list_empty(&tsk->cg_list)) { |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5648 | spin_lock_irq(&css_set_lock); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 5649 | css_set_move_task(tsk, cset, NULL, false); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 5650 | cset->nr_tasks--; |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5651 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5652 | } else { |
| 5653 | get_css_set(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5654 | } |
| 5655 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 5656 | /* see cgroup_post_fork() for details */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5657 | do_each_subsys_mask(ss, i, have_exit_callback) { |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5658 | ss->exit(tsk); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5659 | } while_each_subsys_mask(); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5660 | } |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5661 | |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5662 | void cgroup_free(struct task_struct *task) |
| 5663 | { |
| 5664 | struct css_set *cset = task_css_set(task); |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5665 | struct cgroup_subsys *ss; |
| 5666 | int ssid; |
| 5667 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5668 | do_each_subsys_mask(ss, ssid, have_free_callback) { |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5669 | ss->free(task); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5670 | } while_each_subsys_mask(); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5671 | |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5672 | put_css_set(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5673 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5674 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5675 | static int __init cgroup_disable(char *str) |
| 5676 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5677 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5678 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5679 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5680 | |
| 5681 | while ((token = strsep(&str, ",")) != NULL) { |
| 5682 | if (!*token) |
| 5683 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5684 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5685 | for_each_subsys(ss, i) { |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5686 | if (strcmp(token, ss->name) && |
| 5687 | strcmp(token, ss->legacy_name)) |
| 5688 | continue; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5689 | cgroup_disable_mask |= 1 << i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5690 | } |
| 5691 | } |
| 5692 | return 1; |
| 5693 | } |
| 5694 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5695 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5696 | /** |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5697 | * css_tryget_online_from_dir - get corresponding css from a cgroup dentry |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5698 | * @dentry: directory dentry of interest |
| 5699 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5700 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5701 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 5702 | * to get the corresponding css and return it. If such css doesn't exist |
| 5703 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5704 | */ |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5705 | struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, |
| 5706 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5707 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5708 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
Tejun Heo | f17fc25 | 2016-02-23 10:00:51 -0500 | [diff] [blame] | 5709 | struct file_system_type *s_type = dentry->d_sb->s_type; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5710 | struct cgroup_subsys_state *css = NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5711 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5712 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5713 | /* is @dentry a cgroup dir? */ |
Tejun Heo | f17fc25 | 2016-02-23 10:00:51 -0500 | [diff] [blame] | 5714 | if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) || |
| 5715 | !kn || kernfs_type(kn) != KERNFS_DIR) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5716 | return ERR_PTR(-EBADF); |
| 5717 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5718 | rcu_read_lock(); |
| 5719 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5720 | /* |
| 5721 | * This path doesn't originate from kernfs and @kn could already |
| 5722 | * have been or be removed at any point. @kn->priv is RCU |
Li Zefan | a418948 | 2014-09-04 14:43:07 +0800 | [diff] [blame] | 5723 | * protected for this access. See css_release_work_fn() for details. |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5724 | */ |
Tejun Heo | e0aed7c | 2016-12-27 14:49:09 -0500 | [diff] [blame] | 5725 | cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5726 | if (cgrp) |
| 5727 | css = cgroup_css(cgrp, ss); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5728 | |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5729 | if (!css || !css_tryget_online(css)) |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5730 | css = ERR_PTR(-ENOENT); |
| 5731 | |
| 5732 | rcu_read_unlock(); |
| 5733 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5734 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5735 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5736 | /** |
| 5737 | * css_from_id - lookup css by id |
| 5738 | * @id: the cgroup id |
| 5739 | * @ss: cgroup subsys to be looked into |
| 5740 | * |
| 5741 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5742 | * Should be called under rcu_read_lock(). |
| 5743 | */ |
| 5744 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5745 | { |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 5746 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Johannes Weiner | d6ccc55 | 2016-06-17 12:24:27 -0400 | [diff] [blame] | 5747 | return idr_find(&ss->css_idr, id); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5748 | } |
| 5749 | |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 5750 | /** |
| 5751 | * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path |
| 5752 | * @path: path on the default hierarchy |
| 5753 | * |
| 5754 | * Find the cgroup at @path on the default hierarchy, increment its |
| 5755 | * reference count and return it. Returns pointer to the found cgroup on |
| 5756 | * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR) |
| 5757 | * if @path points to a non-directory. |
| 5758 | */ |
| 5759 | struct cgroup *cgroup_get_from_path(const char *path) |
| 5760 | { |
| 5761 | struct kernfs_node *kn; |
| 5762 | struct cgroup *cgrp; |
| 5763 | |
| 5764 | mutex_lock(&cgroup_mutex); |
| 5765 | |
| 5766 | kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path); |
| 5767 | if (kn) { |
| 5768 | if (kernfs_type(kn) == KERNFS_DIR) { |
| 5769 | cgrp = kn->priv; |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 5770 | cgroup_get_live(cgrp); |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 5771 | } else { |
| 5772 | cgrp = ERR_PTR(-ENOTDIR); |
| 5773 | } |
| 5774 | kernfs_put(kn); |
| 5775 | } else { |
| 5776 | cgrp = ERR_PTR(-ENOENT); |
| 5777 | } |
| 5778 | |
| 5779 | mutex_unlock(&cgroup_mutex); |
| 5780 | return cgrp; |
| 5781 | } |
| 5782 | EXPORT_SYMBOL_GPL(cgroup_get_from_path); |
| 5783 | |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 5784 | /** |
| 5785 | * cgroup_get_from_fd - get a cgroup pointer from a fd |
| 5786 | * @fd: fd obtained by open(cgroup2_dir) |
| 5787 | * |
| 5788 | * Find the cgroup from a fd which should be obtained |
| 5789 | * by opening a cgroup directory. Returns a pointer to the |
| 5790 | * cgroup on success. ERR_PTR is returned if the cgroup |
| 5791 | * cannot be found. |
| 5792 | */ |
| 5793 | struct cgroup *cgroup_get_from_fd(int fd) |
| 5794 | { |
| 5795 | struct cgroup_subsys_state *css; |
| 5796 | struct cgroup *cgrp; |
| 5797 | struct file *f; |
| 5798 | |
| 5799 | f = fget_raw(fd); |
| 5800 | if (!f) |
| 5801 | return ERR_PTR(-EBADF); |
| 5802 | |
| 5803 | css = css_tryget_online_from_dir(f->f_path.dentry, NULL); |
| 5804 | fput(f); |
| 5805 | if (IS_ERR(css)) |
| 5806 | return ERR_CAST(css); |
| 5807 | |
| 5808 | cgrp = css->cgroup; |
| 5809 | if (!cgroup_on_dfl(cgrp)) { |
| 5810 | cgroup_put(cgrp); |
| 5811 | return ERR_PTR(-EBADF); |
| 5812 | } |
| 5813 | |
| 5814 | return cgrp; |
| 5815 | } |
| 5816 | EXPORT_SYMBOL_GPL(cgroup_get_from_fd); |
| 5817 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 5818 | /* |
| 5819 | * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data |
| 5820 | * definition in cgroup-defs.h. |
| 5821 | */ |
| 5822 | #ifdef CONFIG_SOCK_CGROUP_DATA |
| 5823 | |
| 5824 | #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID) |
| 5825 | |
Tejun Heo | 3fa4cc9 | 2015-12-14 11:24:06 -0500 | [diff] [blame] | 5826 | DEFINE_SPINLOCK(cgroup_sk_update_lock); |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 5827 | static bool cgroup_sk_alloc_disabled __read_mostly; |
| 5828 | |
| 5829 | void cgroup_sk_alloc_disable(void) |
| 5830 | { |
| 5831 | if (cgroup_sk_alloc_disabled) |
| 5832 | return; |
| 5833 | pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n"); |
| 5834 | cgroup_sk_alloc_disabled = true; |
| 5835 | } |
| 5836 | |
| 5837 | #else |
| 5838 | |
| 5839 | #define cgroup_sk_alloc_disabled false |
| 5840 | |
| 5841 | #endif |
| 5842 | |
| 5843 | void cgroup_sk_alloc(struct sock_cgroup_data *skcd) |
| 5844 | { |
| 5845 | if (cgroup_sk_alloc_disabled) |
| 5846 | return; |
| 5847 | |
Johannes Weiner | d979a39 | 2016-09-19 14:44:38 -0700 | [diff] [blame] | 5848 | /* Socket clone path */ |
| 5849 | if (skcd->val) { |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 5850 | /* |
| 5851 | * We might be cloning a socket which is left in an empty |
| 5852 | * cgroup and the cgroup might have already been rmdir'd. |
| 5853 | * Don't use cgroup_get_live(). |
| 5854 | */ |
Johannes Weiner | d979a39 | 2016-09-19 14:44:38 -0700 | [diff] [blame] | 5855 | cgroup_get(sock_cgroup_ptr(skcd)); |
| 5856 | return; |
| 5857 | } |
| 5858 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 5859 | rcu_read_lock(); |
| 5860 | |
| 5861 | while (true) { |
| 5862 | struct css_set *cset; |
| 5863 | |
| 5864 | cset = task_css_set(current); |
| 5865 | if (likely(cgroup_tryget(cset->dfl_cgrp))) { |
| 5866 | skcd->val = (unsigned long)cset->dfl_cgrp; |
| 5867 | break; |
| 5868 | } |
| 5869 | cpu_relax(); |
| 5870 | } |
| 5871 | |
| 5872 | rcu_read_unlock(); |
| 5873 | } |
| 5874 | |
| 5875 | void cgroup_sk_free(struct sock_cgroup_data *skcd) |
| 5876 | { |
| 5877 | cgroup_put(sock_cgroup_ptr(skcd)); |
| 5878 | } |
| 5879 | |
| 5880 | #endif /* CONFIG_SOCK_CGROUP_DATA */ |
| 5881 | |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5882 | #ifdef CONFIG_CGROUP_BPF |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 5883 | int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 5884 | enum bpf_attach_type type, u32 flags) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5885 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 5886 | int ret; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5887 | |
| 5888 | mutex_lock(&cgroup_mutex); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 5889 | ret = __cgroup_bpf_attach(cgrp, prog, type, flags); |
| 5890 | mutex_unlock(&cgroup_mutex); |
| 5891 | return ret; |
| 5892 | } |
| 5893 | int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 5894 | enum bpf_attach_type type, u32 flags) |
| 5895 | { |
| 5896 | int ret; |
| 5897 | |
| 5898 | mutex_lock(&cgroup_mutex); |
| 5899 | ret = __cgroup_bpf_detach(cgrp, prog, type, flags); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5900 | mutex_unlock(&cgroup_mutex); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 5901 | return ret; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5902 | } |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 5903 | int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr, |
| 5904 | union bpf_attr __user *uattr) |
| 5905 | { |
| 5906 | int ret; |
| 5907 | |
| 5908 | mutex_lock(&cgroup_mutex); |
| 5909 | ret = __cgroup_bpf_query(cgrp, attr, uattr); |
| 5910 | mutex_unlock(&cgroup_mutex); |
| 5911 | return ret; |
| 5912 | } |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5913 | #endif /* CONFIG_CGROUP_BPF */ |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 5914 | |
| 5915 | #ifdef CONFIG_SYSFS |
| 5916 | static ssize_t show_delegatable_files(struct cftype *files, char *buf, |
| 5917 | ssize_t size, const char *prefix) |
| 5918 | { |
| 5919 | struct cftype *cft; |
| 5920 | ssize_t ret = 0; |
| 5921 | |
| 5922 | for (cft = files; cft && cft->name[0] != '\0'; cft++) { |
| 5923 | if (!(cft->flags & CFTYPE_NS_DELEGATABLE)) |
| 5924 | continue; |
| 5925 | |
| 5926 | if (prefix) |
| 5927 | ret += snprintf(buf + ret, size - ret, "%s.", prefix); |
| 5928 | |
| 5929 | ret += snprintf(buf + ret, size - ret, "%s\n", cft->name); |
| 5930 | |
| 5931 | if (unlikely(ret >= size)) { |
| 5932 | WARN_ON(1); |
| 5933 | break; |
| 5934 | } |
| 5935 | } |
| 5936 | |
| 5937 | return ret; |
| 5938 | } |
| 5939 | |
| 5940 | static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 5941 | char *buf) |
| 5942 | { |
| 5943 | struct cgroup_subsys *ss; |
| 5944 | int ssid; |
| 5945 | ssize_t ret = 0; |
| 5946 | |
| 5947 | ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret, |
| 5948 | NULL); |
| 5949 | |
| 5950 | for_each_subsys(ss, ssid) |
| 5951 | ret += show_delegatable_files(ss->dfl_cftypes, buf + ret, |
| 5952 | PAGE_SIZE - ret, |
| 5953 | cgroup_subsys_name[ssid]); |
| 5954 | |
| 5955 | return ret; |
| 5956 | } |
| 5957 | static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate); |
| 5958 | |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 5959 | static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 5960 | char *buf) |
| 5961 | { |
| 5962 | return snprintf(buf, PAGE_SIZE, "nsdelegate\n"); |
| 5963 | } |
| 5964 | static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features); |
| 5965 | |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 5966 | static struct attribute *cgroup_sysfs_attrs[] = { |
| 5967 | &cgroup_delegate_attr.attr, |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 5968 | &cgroup_features_attr.attr, |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 5969 | NULL, |
| 5970 | }; |
| 5971 | |
| 5972 | static const struct attribute_group cgroup_sysfs_attr_group = { |
| 5973 | .attrs = cgroup_sysfs_attrs, |
| 5974 | .name = "cgroup", |
| 5975 | }; |
| 5976 | |
| 5977 | static int __init cgroup_sysfs_init(void) |
| 5978 | { |
| 5979 | return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group); |
| 5980 | } |
| 5981 | subsys_initcall(cgroup_sysfs_init); |
| 5982 | #endif /* CONFIG_SYSFS */ |