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> |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 57 | #include <linux/fs_parser.h> |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 58 | #include <linux/sched/cputime.h> |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 59 | #include <linux/psi.h> |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 60 | #include <net/sock.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 61 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 62 | #define CREATE_TRACE_POINTS |
| 63 | #include <trace/events/cgroup.h> |
| 64 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 65 | #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ |
| 66 | MAX_CFTYPE_NAME + 2) |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 67 | /* let's not notify more than 100 times per second */ |
| 68 | #define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100) |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 69 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 70 | /* |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 71 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 72 | * hierarchy must be performed while holding it. |
| 73 | * |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 74 | * css_set_lock protects task->cgroups pointer, the list of css_set |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 75 | * objects, and the chain of tasks off each css_set. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 76 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 77 | * These locks are exported if CONFIG_PROVE_RCU so that accessors in |
| 78 | * cgroup.h can use them for lockdep annotations. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 79 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 80 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 81 | DEFINE_SPINLOCK(css_set_lock); |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 82 | |
| 83 | #ifdef CONFIG_PROVE_RCU |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 84 | EXPORT_SYMBOL_GPL(cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 85 | EXPORT_SYMBOL_GPL(css_set_lock); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 86 | #endif |
| 87 | |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 88 | DEFINE_SPINLOCK(trace_cgroup_path_lock); |
| 89 | char trace_cgroup_path[TRACE_CGROUP_PATH_LEN]; |
Waiman Long | 5cf8114 | 2018-11-08 10:08:46 -0500 | [diff] [blame] | 90 | bool cgroup_debug __read_mostly; |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 91 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 92 | /* |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 93 | * Protects cgroup_idr and css_idr so that IDs can be released without |
| 94 | * grabbing cgroup_mutex. |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 95 | */ |
| 96 | static DEFINE_SPINLOCK(cgroup_idr_lock); |
| 97 | |
| 98 | /* |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 99 | * Protects cgroup_file->kn for !self csses. It synchronizes notifications |
| 100 | * against file removal/re-creation across css hiding. |
| 101 | */ |
| 102 | static DEFINE_SPINLOCK(cgroup_file_kn_lock); |
| 103 | |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 104 | struct percpu_rw_semaphore cgroup_threadgroup_rwsem; |
| 105 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 106 | #define cgroup_assert_mutex_or_rcu_locked() \ |
Paul E. McKenney | f78f5b9 | 2015-06-18 15:50:02 -0700 | [diff] [blame] | 107 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ |
| 108 | !lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 109 | "cgroup_mutex or RCU read lock required"); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 110 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 111 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 112 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 113 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 114 | * destruction work items don't end up filling up max_active of system_wq |
| 115 | * which may lead to deadlock. |
| 116 | */ |
| 117 | static struct workqueue_struct *cgroup_destroy_wq; |
| 118 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 119 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 120 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 121 | struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 122 | #include <linux/cgroup_subsys.h> |
| 123 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 124 | #undef SUBSYS |
| 125 | |
| 126 | /* array of cgroup subsystem names */ |
| 127 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 128 | static const char *cgroup_subsys_name[] = { |
| 129 | #include <linux/cgroup_subsys.h> |
| 130 | }; |
| 131 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 132 | |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 133 | /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */ |
| 134 | #define SUBSYS(_x) \ |
| 135 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \ |
| 136 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \ |
| 137 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \ |
| 138 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key); |
| 139 | #include <linux/cgroup_subsys.h> |
| 140 | #undef SUBSYS |
| 141 | |
| 142 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key, |
| 143 | static struct static_key_true *cgroup_subsys_enabled_key[] = { |
| 144 | #include <linux/cgroup_subsys.h> |
| 145 | }; |
| 146 | #undef SUBSYS |
| 147 | |
| 148 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key, |
| 149 | static struct static_key_true *cgroup_subsys_on_dfl_key[] = { |
| 150 | #include <linux/cgroup_subsys.h> |
| 151 | }; |
| 152 | #undef SUBSYS |
| 153 | |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 154 | 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] | 155 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 156 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 157 | * The default hierarchy, reserved for the subsystems that are otherwise |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 158 | * unattached - it never has more than a single cgroup, and all tasks are |
| 159 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 160 | */ |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 161 | 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] | 162 | EXPORT_SYMBOL_GPL(cgrp_dfl_root); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 163 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 164 | /* |
| 165 | * The default hierarchy always exists but is hidden until mounted for the |
| 166 | * first time. This is for backward compatibility. |
| 167 | */ |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 168 | static bool cgrp_dfl_visible; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 169 | |
Tejun Heo | 5533e01 | 2014-05-14 19:33:07 -0400 | [diff] [blame] | 170 | /* some controllers are not supported in the default hierarchy */ |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 171 | static u16 cgrp_dfl_inhibit_ss_mask; |
Tejun Heo | 5533e01 | 2014-05-14 19:33:07 -0400 | [diff] [blame] | 172 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 173 | /* some controllers are implicitly enabled on the default hierarchy */ |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 174 | static u16 cgrp_dfl_implicit_ss_mask; |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 175 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 176 | /* some controllers can be threaded on the default hierarchy */ |
| 177 | static u16 cgrp_dfl_threaded_ss_mask; |
| 178 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 179 | /* The list of hierarchy roots */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 180 | LIST_HEAD(cgroup_roots); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 181 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 182 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 183 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 184 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 185 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 186 | /* |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 187 | * Assign a monotonically increasing serial number to csses. It guarantees |
| 188 | * cgroups with bigger numbers are newer than those with smaller numbers. |
| 189 | * Also, as csses are always appended to the parent's ->children list, it |
| 190 | * guarantees that sibling csses are always sorted in the ascending serial |
| 191 | * number order on the list. Protected by cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 192 | */ |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 193 | static u64 css_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 194 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 195 | /* |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 196 | * These bitmasks identify subsystems with specific features to avoid |
| 197 | * having to do iterative checks repeatedly. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 198 | */ |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 199 | static u16 have_fork_callback __read_mostly; |
| 200 | static u16 have_exit_callback __read_mostly; |
| 201 | static u16 have_free_callback __read_mostly; |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 202 | static u16 have_canfork_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 203 | |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 204 | /* cgroup namespace for init task */ |
| 205 | struct cgroup_namespace init_cgroup_ns = { |
Elena Reshetova | 387ad96 | 2017-02-20 12:19:00 +0200 | [diff] [blame] | 206 | .count = REFCOUNT_INIT(2), |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 207 | .user_ns = &init_user_ns, |
| 208 | .ns.ops = &cgroupns_operations, |
| 209 | .ns.inum = PROC_CGROUP_INIT_INO, |
| 210 | .root_cset = &init_css_set, |
| 211 | }; |
| 212 | |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 213 | static struct file_system_type cgroup2_fs_type; |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 214 | static struct cftype cgroup_base_files[]; |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 215 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 216 | static int cgroup_apply_control(struct cgroup *cgrp); |
| 217 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 218 | static void css_task_iter_advance(struct css_task_iter *it); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 219 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 220 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 221 | struct cgroup_subsys *ss); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 222 | static void css_release(struct percpu_ref *ref); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 223 | static void kill_css(struct cgroup_subsys_state *css); |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 224 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 225 | struct cgroup *cgrp, struct cftype cfts[], |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 226 | bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 227 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 228 | /** |
| 229 | * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID |
| 230 | * @ssid: subsys ID of interest |
| 231 | * |
| 232 | * cgroup_subsys_enabled() can only be used with literal subsys names which |
| 233 | * is fine for individual subsystems but unsuitable for cgroup core. This |
| 234 | * is slower static_key_enabled() based test indexed by @ssid. |
| 235 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 236 | bool cgroup_ssid_enabled(int ssid) |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 237 | { |
Arnd Bergmann | cfe02a8 | 2016-03-15 00:21:06 +0100 | [diff] [blame] | 238 | if (CGROUP_SUBSYS_COUNT == 0) |
| 239 | return false; |
| 240 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 241 | return static_key_enabled(cgroup_subsys_enabled_key[ssid]); |
| 242 | } |
| 243 | |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 244 | /** |
| 245 | * cgroup_on_dfl - test whether a cgroup is on the default hierarchy |
| 246 | * @cgrp: the cgroup of interest |
| 247 | * |
| 248 | * The default hierarchy is the v2 interface of cgroup and this function |
| 249 | * can be used to test whether a cgroup is on the default hierarchy for |
| 250 | * cases where a subsystem should behave differnetly depending on the |
| 251 | * interface version. |
| 252 | * |
| 253 | * The set of behaviors which change on the default hierarchy are still |
| 254 | * being determined and the mount option is prefixed with __DEVEL__. |
| 255 | * |
| 256 | * List of changed behaviors: |
| 257 | * |
| 258 | * - Mount options "noprefix", "xattr", "clone_children", "release_agent" |
| 259 | * and "name" are disallowed. |
| 260 | * |
| 261 | * - When mounting an existing superblock, mount options should match. |
| 262 | * |
| 263 | * - Remount is disallowed. |
| 264 | * |
| 265 | * - rename(2) is disallowed. |
| 266 | * |
| 267 | * - "tasks" is removed. Everything should be at process granularity. Use |
| 268 | * "cgroup.procs" instead. |
| 269 | * |
| 270 | * - "cgroup.procs" is not sorted. pids will be unique unless they got |
| 271 | * recycled inbetween reads. |
| 272 | * |
| 273 | * - "release_agent" and "notify_on_release" are removed. Replacement |
| 274 | * notification mechanism will be implemented. |
| 275 | * |
| 276 | * - "cgroup.clone_children" is removed. |
| 277 | * |
| 278 | * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup |
| 279 | * and its descendants contain no task; otherwise, 1. The file also |
| 280 | * generates kernfs notification which can be monitored through poll and |
| 281 | * [di]notify when the value of the file changes. |
| 282 | * |
| 283 | * - cpuset: tasks will be kept in empty cpusets when hotplug happens and |
| 284 | * take masks of ancestors with non-empty cpus/mems, instead of being |
| 285 | * moved to an ancestor. |
| 286 | * |
| 287 | * - cpuset: a task can be moved into an empty cpuset, and again it takes |
| 288 | * masks of ancestors. |
| 289 | * |
| 290 | * - memcg: use_hierarchy is on by default and the cgroup file for the flag |
| 291 | * is not created. |
| 292 | * |
| 293 | * - blkcg: blk-throttle becomes properly hierarchical. |
| 294 | * |
| 295 | * - debug: disallowed on the default hierarchy. |
| 296 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 297 | bool cgroup_on_dfl(const struct cgroup *cgrp) |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 298 | { |
| 299 | return cgrp->root == &cgrp_dfl_root; |
| 300 | } |
| 301 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 302 | /* IDR wrappers which synchronize using cgroup_idr_lock */ |
| 303 | static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end, |
| 304 | gfp_t gfp_mask) |
| 305 | { |
| 306 | int ret; |
| 307 | |
| 308 | idr_preload(gfp_mask); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 309 | spin_lock_bh(&cgroup_idr_lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 310 | ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 311 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 312 | idr_preload_end(); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id) |
| 317 | { |
| 318 | void *ret; |
| 319 | |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 320 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 321 | ret = idr_replace(idr, ptr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 322 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | static void cgroup_idr_remove(struct idr *idr, int id) |
| 327 | { |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 328 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 329 | idr_remove(idr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 330 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 333 | static bool cgroup_has_tasks(struct cgroup *cgrp) |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 334 | { |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 335 | return cgrp->nr_populated_csets; |
| 336 | } |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 337 | |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 338 | bool cgroup_is_threaded(struct cgroup *cgrp) |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 339 | { |
| 340 | return cgrp->dom_cgrp != cgrp; |
| 341 | } |
| 342 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 343 | /* can @cgrp host both domain and threaded children? */ |
| 344 | static bool cgroup_is_mixable(struct cgroup *cgrp) |
| 345 | { |
| 346 | /* |
| 347 | * Root isn't under domain level resource control exempting it from |
| 348 | * the no-internal-process constraint, so it can serve as a thread |
| 349 | * root and a parent of resource domains at the same time. |
| 350 | */ |
| 351 | return !cgroup_parent(cgrp); |
| 352 | } |
| 353 | |
| 354 | /* can @cgrp become a thread root? should always be true for a thread root */ |
| 355 | static bool cgroup_can_be_thread_root(struct cgroup *cgrp) |
| 356 | { |
| 357 | /* mixables don't care */ |
| 358 | if (cgroup_is_mixable(cgrp)) |
| 359 | return true; |
| 360 | |
| 361 | /* domain roots can't be nested under threaded */ |
| 362 | if (cgroup_is_threaded(cgrp)) |
| 363 | return false; |
| 364 | |
| 365 | /* can only have either domain or threaded children */ |
| 366 | if (cgrp->nr_populated_domain_children) |
| 367 | return false; |
| 368 | |
| 369 | /* and no domain controllers can be enabled */ |
| 370 | if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) |
| 371 | return false; |
| 372 | |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | /* is @cgrp root of a threaded subtree? */ |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 377 | bool cgroup_is_thread_root(struct cgroup *cgrp) |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 378 | { |
| 379 | /* thread root should be a domain */ |
| 380 | if (cgroup_is_threaded(cgrp)) |
| 381 | return false; |
| 382 | |
| 383 | /* a domain w/ threaded children is a thread root */ |
| 384 | if (cgrp->nr_threaded_children) |
| 385 | return true; |
| 386 | |
| 387 | /* |
| 388 | * A domain which has tasks and explicit threaded controllers |
| 389 | * enabled is a thread root. |
| 390 | */ |
| 391 | if (cgroup_has_tasks(cgrp) && |
| 392 | (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask)) |
| 393 | return true; |
| 394 | |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | /* a domain which isn't connected to the root w/o brekage can't be used */ |
| 399 | static bool cgroup_is_valid_domain(struct cgroup *cgrp) |
| 400 | { |
| 401 | /* the cgroup itself can be a thread root */ |
| 402 | if (cgroup_is_threaded(cgrp)) |
| 403 | return false; |
| 404 | |
| 405 | /* but the ancestors can't be unless mixable */ |
| 406 | while ((cgrp = cgroup_parent(cgrp))) { |
| 407 | if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp)) |
| 408 | return false; |
| 409 | if (cgroup_is_threaded(cgrp)) |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | return true; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 416 | /* subsystems visibly enabled on a cgroup */ |
| 417 | static u16 cgroup_control(struct cgroup *cgrp) |
| 418 | { |
| 419 | struct cgroup *parent = cgroup_parent(cgrp); |
| 420 | u16 root_ss_mask = cgrp->root->subsys_mask; |
| 421 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 422 | if (parent) { |
| 423 | u16 ss_mask = parent->subtree_control; |
| 424 | |
| 425 | /* threaded cgroups can only have threaded controllers */ |
| 426 | if (cgroup_is_threaded(cgrp)) |
| 427 | ss_mask &= cgrp_dfl_threaded_ss_mask; |
| 428 | return ss_mask; |
| 429 | } |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 430 | |
| 431 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 432 | root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask | |
| 433 | cgrp_dfl_implicit_ss_mask); |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 434 | return root_ss_mask; |
| 435 | } |
| 436 | |
| 437 | /* subsystems enabled on a cgroup */ |
| 438 | static u16 cgroup_ss_mask(struct cgroup *cgrp) |
| 439 | { |
| 440 | struct cgroup *parent = cgroup_parent(cgrp); |
| 441 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 442 | if (parent) { |
| 443 | u16 ss_mask = parent->subtree_ss_mask; |
| 444 | |
| 445 | /* threaded cgroups can only have threaded controllers */ |
| 446 | if (cgroup_is_threaded(cgrp)) |
| 447 | ss_mask &= cgrp_dfl_threaded_ss_mask; |
| 448 | return ss_mask; |
| 449 | } |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 450 | |
| 451 | return cgrp->root->subsys_mask; |
| 452 | } |
| 453 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 454 | /** |
| 455 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 456 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 457 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 458 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 459 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 460 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 461 | * the caller is responsible for pinning the returned css if it wants to |
| 462 | * keep accessing it outside the said locks. This function may return |
| 463 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 464 | */ |
| 465 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 466 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 467 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 468 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 469 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 470 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 471 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 472 | return &cgrp->self; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 473 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 474 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 475 | /** |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 476 | * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem |
| 477 | * @cgrp: the cgroup of interest |
| 478 | * @ss: the subsystem of interest |
| 479 | * |
| 480 | * Find and get @cgrp's css assocaited with @ss. If the css doesn't exist |
| 481 | * or is offline, %NULL is returned. |
| 482 | */ |
| 483 | static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp, |
| 484 | struct cgroup_subsys *ss) |
| 485 | { |
| 486 | struct cgroup_subsys_state *css; |
| 487 | |
| 488 | rcu_read_lock(); |
| 489 | css = cgroup_css(cgrp, ss); |
| 490 | if (!css || !css_tryget_online(css)) |
| 491 | css = NULL; |
| 492 | rcu_read_unlock(); |
| 493 | |
| 494 | return css; |
| 495 | } |
| 496 | |
| 497 | /** |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 498 | * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 499 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 500 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 501 | * |
Chen Hanxiao | d0f702e | 2015-04-23 07:57:33 -0400 | [diff] [blame] | 502 | * Similar to cgroup_css() but returns the effective css, which is defined |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 503 | * as the matching css of the nearest ancestor including self which has @ss |
| 504 | * enabled. If @ss is associated with the hierarchy @cgrp is on, this |
| 505 | * function is guaranteed to return non-NULL css. |
| 506 | */ |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 507 | static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp, |
| 508 | struct cgroup_subsys *ss) |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 509 | { |
| 510 | lockdep_assert_held(&cgroup_mutex); |
| 511 | |
| 512 | if (!ss) |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 513 | return &cgrp->self; |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 514 | |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 515 | /* |
| 516 | * This function is used while updating css associations and thus |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 517 | * can't test the csses directly. Test ss_mask. |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 518 | */ |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 519 | while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) { |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 520 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 521 | if (!cgrp) |
| 522 | return NULL; |
| 523 | } |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 524 | |
| 525 | return cgroup_css(cgrp, ss); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 528 | /** |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 529 | * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem |
| 530 | * @cgrp: the cgroup of interest |
| 531 | * @ss: the subsystem of interest |
| 532 | * |
| 533 | * Find and get the effective css of @cgrp for @ss. The effective css is |
| 534 | * defined as the matching css of the nearest ancestor including self which |
| 535 | * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, |
| 536 | * the root css is returned, so this function always returns a valid css. |
| 537 | * |
| 538 | * The returned css is not guaranteed to be online, and therefore it is the |
| 539 | * callers responsiblity to tryget a reference for it. |
| 540 | */ |
| 541 | struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp, |
| 542 | struct cgroup_subsys *ss) |
| 543 | { |
| 544 | struct cgroup_subsys_state *css; |
| 545 | |
| 546 | do { |
| 547 | css = cgroup_css(cgrp, ss); |
| 548 | |
| 549 | if (css) |
| 550 | return css; |
| 551 | cgrp = cgroup_parent(cgrp); |
| 552 | } while (cgrp); |
| 553 | |
| 554 | return init_css_set.subsys[ss->id]; |
| 555 | } |
| 556 | |
| 557 | /** |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 558 | * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem |
| 559 | * @cgrp: the cgroup of interest |
| 560 | * @ss: the subsystem of interest |
| 561 | * |
| 562 | * Find and get the effective css of @cgrp for @ss. The effective css is |
| 563 | * defined as the matching css of the nearest ancestor including self which |
| 564 | * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, |
| 565 | * the root css is returned, so this function always returns a valid css. |
| 566 | * The returned css must be put using css_put(). |
| 567 | */ |
| 568 | struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp, |
| 569 | struct cgroup_subsys *ss) |
| 570 | { |
| 571 | struct cgroup_subsys_state *css; |
| 572 | |
| 573 | rcu_read_lock(); |
| 574 | |
| 575 | do { |
| 576 | css = cgroup_css(cgrp, ss); |
| 577 | |
| 578 | if (css && css_tryget_online(css)) |
| 579 | goto out_unlock; |
| 580 | cgrp = cgroup_parent(cgrp); |
| 581 | } while (cgrp); |
| 582 | |
| 583 | css = init_css_set.subsys[ss->id]; |
| 584 | css_get(css); |
| 585 | out_unlock: |
| 586 | rcu_read_unlock(); |
| 587 | return css; |
| 588 | } |
| 589 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 590 | static void cgroup_get_live(struct cgroup *cgrp) |
Tejun Heo | 052c3f3 | 2015-10-15 16:41:50 -0400 | [diff] [blame] | 591 | { |
| 592 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
| 593 | css_get(&cgrp->self); |
| 594 | } |
| 595 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 596 | struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 597 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 598 | struct cgroup *cgrp = of->kn->parent->priv; |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 599 | struct cftype *cft = of_cft(of); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 600 | |
| 601 | /* |
| 602 | * This is open and unprotected implementation of cgroup_css(). |
| 603 | * seq_css() is only called from a kernfs file operation which has |
| 604 | * an active reference on the file. Because all the subsystem |
| 605 | * files are drained before a css is disassociated with a cgroup, |
| 606 | * the matching css from the cgroup's subsys table is guaranteed to |
| 607 | * be and stay valid until the enclosing operation is complete. |
| 608 | */ |
| 609 | if (cft->ss) |
| 610 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 611 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 612 | return &cgrp->self; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 613 | } |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 614 | EXPORT_SYMBOL_GPL(of_css); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 615 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 616 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 617 | * for_each_css - iterate all css's of a cgroup |
| 618 | * @css: the iteration cursor |
| 619 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 620 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 621 | * |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 622 | * Should be called under cgroup_[tree_]mutex. |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 623 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 624 | #define for_each_css(css, ssid, cgrp) \ |
| 625 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 626 | if (!((css) = rcu_dereference_check( \ |
| 627 | (cgrp)->subsys[(ssid)], \ |
| 628 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 629 | else |
| 630 | |
| 631 | /** |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 632 | * for_each_e_css - iterate all effective css's of a cgroup |
| 633 | * @css: the iteration cursor |
| 634 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 635 | * @cgrp: the target cgroup to iterate css's of |
| 636 | * |
| 637 | * Should be called under cgroup_[tree_]mutex. |
| 638 | */ |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 639 | #define for_each_e_css(css, ssid, cgrp) \ |
| 640 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 641 | if (!((css) = cgroup_e_css_by_mask(cgrp, \ |
| 642 | cgroup_subsys[(ssid)]))) \ |
| 643 | ; \ |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 644 | else |
| 645 | |
| 646 | /** |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 647 | * do_each_subsys_mask - filter for_each_subsys with a bitmask |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 648 | * @ss: the iteration cursor |
| 649 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 650 | * @ss_mask: the bitmask |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 651 | * |
| 652 | * 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] | 653 | * @ss_mask is set. |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 654 | */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 655 | #define do_each_subsys_mask(ss, ssid, ss_mask) do { \ |
| 656 | unsigned long __ss_mask = (ss_mask); \ |
| 657 | if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \ |
Aleksa Sarai | 4a705c5 | 2015-06-09 21:32:07 +1000 | [diff] [blame] | 658 | (ssid) = 0; \ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 659 | break; \ |
| 660 | } \ |
| 661 | for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \ |
| 662 | (ss) = cgroup_subsys[ssid]; \ |
| 663 | { |
| 664 | |
| 665 | #define while_each_subsys_mask() \ |
| 666 | } \ |
| 667 | } \ |
| 668 | } while (false) |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 669 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 670 | /* iterate over child cgrps, lock should be held throughout iteration */ |
| 671 | #define cgroup_for_each_live_child(child, cgrp) \ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 672 | list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 673 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 674 | cgroup_is_dead(child); })) \ |
| 675 | ; \ |
| 676 | else |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 677 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 678 | /* walk live descendants in preorder */ |
| 679 | #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \ |
| 680 | css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \ |
| 681 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 682 | (dsct) = (d_css)->cgroup; \ |
| 683 | cgroup_is_dead(dsct); })) \ |
| 684 | ; \ |
| 685 | else |
| 686 | |
| 687 | /* walk live descendants in postorder */ |
| 688 | #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \ |
| 689 | css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \ |
| 690 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 691 | (dsct) = (d_css)->cgroup; \ |
| 692 | cgroup_is_dead(dsct); })) \ |
| 693 | ; \ |
| 694 | else |
| 695 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 696 | /* |
| 697 | * 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] | 698 | * hierarchies being mounted. It contains a pointer to the root state |
| 699 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 700 | * reference-counted, to improve performance when child cgroups |
| 701 | * haven't been created. |
| 702 | */ |
Tejun Heo | 5024ae2 | 2014-05-07 21:31:17 -0400 | [diff] [blame] | 703 | struct css_set init_css_set = { |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 704 | .refcount = REFCOUNT_INIT(1), |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 705 | .dom_cset = &init_css_set, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 706 | .tasks = LIST_HEAD_INIT(init_css_set.tasks), |
| 707 | .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 708 | .task_iters = LIST_HEAD_INIT(init_css_set.task_iters), |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 709 | .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets), |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 710 | .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 711 | .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node), |
| 712 | .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), |
Tejun Heo | 3868314 | 2017-09-25 13:50:20 -0700 | [diff] [blame] | 713 | |
| 714 | /* |
| 715 | * The following field is re-initialized when this cset gets linked |
| 716 | * in cgroup_init(). However, let's initialize the field |
| 717 | * statically too so that the default cgroup can be accessed safely |
| 718 | * early during boot. |
| 719 | */ |
| 720 | .dfl_cgrp = &cgrp_dfl_root.cgrp, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 721 | }; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 722 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 723 | static int css_set_count = 1; /* 1 for init_css_set */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 724 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 725 | static bool css_set_threaded(struct css_set *cset) |
| 726 | { |
| 727 | return cset->dom_cset != cset; |
| 728 | } |
| 729 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 730 | /** |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 731 | * css_set_populated - does a css_set contain any tasks? |
| 732 | * @cset: target css_set |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 733 | * |
| 734 | * css_set_populated() should be the same as !!cset->nr_tasks at steady |
| 735 | * state. However, css_set_populated() can be called while a task is being |
| 736 | * added to or removed from the linked list before the nr_tasks is |
| 737 | * properly updated. Hence, we can't just look at ->nr_tasks here. |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 738 | */ |
| 739 | static bool css_set_populated(struct css_set *cset) |
| 740 | { |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 741 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 742 | |
| 743 | return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks); |
| 744 | } |
| 745 | |
| 746 | /** |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 747 | * cgroup_update_populated - update the populated count of a cgroup |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 748 | * @cgrp: the target cgroup |
| 749 | * @populated: inc or dec populated count |
| 750 | * |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 751 | * 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] | 752 | * task or losing the last. Update @cgrp->nr_populated_* accordingly. The |
| 753 | * count is propagated towards root so that a given cgroup's |
| 754 | * nr_populated_children is zero iff none of its descendants contain any |
| 755 | * tasks. |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 756 | * |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 757 | * @cgrp's interface file "cgroup.populated" is zero if both |
| 758 | * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and |
| 759 | * 1 otherwise. When the sum changes from or to zero, userland is notified |
| 760 | * that the content of the interface file has changed. This can be used to |
| 761 | * detect when @cgrp and its descendants become populated or empty. |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 762 | */ |
| 763 | static void cgroup_update_populated(struct cgroup *cgrp, bool populated) |
| 764 | { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 765 | struct cgroup *child = NULL; |
| 766 | int adj = populated ? 1 : -1; |
| 767 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 768 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 769 | |
| 770 | do { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 771 | bool was_populated = cgroup_is_populated(cgrp); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 772 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 773 | if (!child) { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 774 | cgrp->nr_populated_csets += adj; |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 775 | } else { |
| 776 | if (cgroup_is_threaded(child)) |
| 777 | cgrp->nr_populated_threaded_children += adj; |
| 778 | else |
| 779 | cgrp->nr_populated_domain_children += adj; |
| 780 | } |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 781 | |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 782 | if (was_populated == cgroup_is_populated(cgrp)) |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 783 | break; |
| 784 | |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 785 | cgroup1_check_for_release(cgrp); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 786 | cgroup_file_notify(&cgrp->events_file); |
| 787 | |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 788 | child = cgrp; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 789 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 790 | } while (cgrp); |
| 791 | } |
| 792 | |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 793 | /** |
| 794 | * css_set_update_populated - update populated state of a css_set |
| 795 | * @cset: target css_set |
| 796 | * @populated: whether @cset is populated or depopulated |
| 797 | * |
| 798 | * @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] | 799 | * populated counters of all associated cgroups accordingly. |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 800 | */ |
| 801 | static void css_set_update_populated(struct css_set *cset, bool populated) |
| 802 | { |
| 803 | struct cgrp_cset_link *link; |
| 804 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 805 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 806 | |
| 807 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) |
| 808 | cgroup_update_populated(link->cgrp, populated); |
| 809 | } |
| 810 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 811 | /** |
| 812 | * css_set_move_task - move a task from one css_set to another |
| 813 | * @task: task being moved |
| 814 | * @from_cset: css_set @task currently belongs to (may be NULL) |
| 815 | * @to_cset: new css_set @task is being moved to (may be NULL) |
| 816 | * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks |
| 817 | * |
| 818 | * Move @task from @from_cset to @to_cset. If @task didn't belong to any |
| 819 | * css_set, @from_cset can be NULL. If @task is being disassociated |
| 820 | * instead of moved, @to_cset can be NULL. |
| 821 | * |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 822 | * This function automatically handles populated counter updates and |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 823 | * css_task_iter adjustments but the caller is responsible for managing |
| 824 | * @from_cset and @to_cset's reference counts. |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 825 | */ |
| 826 | static void css_set_move_task(struct task_struct *task, |
| 827 | struct css_set *from_cset, struct css_set *to_cset, |
| 828 | bool use_mg_tasks) |
| 829 | { |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 830 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 831 | |
Tejun Heo | 20b454a | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 832 | if (to_cset && !css_set_populated(to_cset)) |
| 833 | css_set_update_populated(to_cset, true); |
| 834 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 835 | if (from_cset) { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 836 | struct css_task_iter *it, *pos; |
| 837 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 838 | WARN_ON_ONCE(list_empty(&task->cg_list)); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 839 | |
| 840 | /* |
| 841 | * @task is leaving, advance task iterators which are |
| 842 | * pointing to it so that they can resume at the next |
| 843 | * position. Advancing an iterator might remove it from |
| 844 | * the list, use safe walk. See css_task_iter_advance*() |
| 845 | * for details. |
| 846 | */ |
| 847 | list_for_each_entry_safe(it, pos, &from_cset->task_iters, |
| 848 | iters_node) |
| 849 | if (it->task_pos == &task->cg_list) |
| 850 | css_task_iter_advance(it); |
| 851 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 852 | list_del_init(&task->cg_list); |
| 853 | if (!css_set_populated(from_cset)) |
| 854 | css_set_update_populated(from_cset, false); |
| 855 | } else { |
| 856 | WARN_ON_ONCE(!list_empty(&task->cg_list)); |
| 857 | } |
| 858 | |
| 859 | if (to_cset) { |
| 860 | /* |
| 861 | * We are synchronized through cgroup_threadgroup_rwsem |
| 862 | * against PF_EXITING setting such that we can't race |
| 863 | * against cgroup_exit() changing the css_set to |
| 864 | * init_css_set and dropping the old one. |
| 865 | */ |
| 866 | WARN_ON_ONCE(task->flags & PF_EXITING); |
| 867 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 868 | cgroup_move_task(task, to_cset); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 869 | list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks : |
| 870 | &to_cset->tasks); |
| 871 | } |
| 872 | } |
| 873 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 874 | /* |
| 875 | * hash table for cgroup groups. This improves the performance to find |
| 876 | * an existing css_set. This hash doesn't (currently) take into |
| 877 | * account cgroups in empty hierarchies. |
| 878 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 879 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 880 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 881 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 882 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 883 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 884 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 885 | struct cgroup_subsys *ss; |
| 886 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 887 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 888 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 889 | key += (unsigned long)css[i]; |
| 890 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 891 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 892 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Tejun Heo | dcfe149 | 2016-12-27 14:49:09 -0500 | [diff] [blame] | 895 | void put_css_set_locked(struct css_set *cset) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 896 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 897 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 898 | struct cgroup_subsys *ss; |
| 899 | int ssid; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 900 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 901 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 902 | |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 903 | if (!refcount_dec_and_test(&cset->refcount)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 904 | return; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 905 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 906 | WARN_ON_ONCE(!list_empty(&cset->threaded_csets)); |
| 907 | |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 908 | /* This css_set is dead. unlink it and release cgroup and css refs */ |
| 909 | for_each_subsys(ss, ssid) { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 910 | list_del(&cset->e_cset_node[ssid]); |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 911 | css_put(cset->subsys[ssid]); |
| 912 | } |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 913 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 914 | css_set_count--; |
| 915 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 916 | 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] | 917 | list_del(&link->cset_link); |
| 918 | list_del(&link->cgrp_link); |
Tejun Heo | 2ceb231 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 919 | if (cgroup_parent(link->cgrp)) |
| 920 | cgroup_put(link->cgrp); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 921 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 922 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 923 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 924 | if (css_set_threaded(cset)) { |
| 925 | list_del(&cset->threaded_csets_node); |
| 926 | put_css_set_locked(cset->dom_cset); |
| 927 | } |
| 928 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 929 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 932 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 933 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 934 | * @cset: candidate css_set being tested |
| 935 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 936 | * @new_cgrp: cgroup that's being entered by the task |
| 937 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 938 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 939 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 940 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 941 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 942 | static bool compare_css_sets(struct css_set *cset, |
| 943 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 944 | struct cgroup *new_cgrp, |
| 945 | struct cgroup_subsys_state *template[]) |
| 946 | { |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 947 | struct cgroup *new_dfl_cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 948 | struct list_head *l1, *l2; |
| 949 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 950 | /* |
| 951 | * On the default hierarchy, there can be csets which are |
| 952 | * associated with the same set of cgroups but different csses. |
| 953 | * Let's first ensure that csses match. |
| 954 | */ |
| 955 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 956 | return false; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 957 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 958 | |
| 959 | /* @cset's domain should match the default cgroup's */ |
| 960 | if (cgroup_on_dfl(new_cgrp)) |
| 961 | new_dfl_cgrp = new_cgrp; |
| 962 | else |
| 963 | new_dfl_cgrp = old_cset->dfl_cgrp; |
| 964 | |
| 965 | if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp) |
| 966 | return false; |
| 967 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 968 | /* |
| 969 | * Compare cgroup pointers in order to distinguish between |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 970 | * different cgroups in hierarchies. As different cgroups may |
| 971 | * share the same effective css, this comparison is always |
| 972 | * necessary. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 973 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 974 | l1 = &cset->cgrp_links; |
| 975 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 976 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 977 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 978 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 979 | |
| 980 | l1 = l1->next; |
| 981 | l2 = l2->next; |
| 982 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 983 | if (l1 == &cset->cgrp_links) { |
| 984 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 985 | break; |
| 986 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 987 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 988 | } |
| 989 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 990 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 991 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 992 | cgrp1 = link1->cgrp; |
| 993 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 994 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 995 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 996 | |
| 997 | /* |
| 998 | * If this hierarchy is the hierarchy of the cgroup |
| 999 | * that's changing, then we need to check that this |
| 1000 | * css_set points to the new cgroup; if it's any other |
| 1001 | * hierarchy, then this css_set should point to the |
| 1002 | * same cgroup as the old css_set. |
| 1003 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1004 | if (cgrp1->root == new_cgrp->root) { |
| 1005 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1006 | return false; |
| 1007 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1008 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1009 | return false; |
| 1010 | } |
| 1011 | } |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1015 | /** |
| 1016 | * find_existing_css_set - init css array and find the matching css_set |
| 1017 | * @old_cset: the css_set that we're using before the cgroup transition |
| 1018 | * @cgrp: the cgroup that we're moving into |
| 1019 | * @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] | 1020 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1021 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 1022 | struct cgroup *cgrp, |
| 1023 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1024 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1025 | struct cgroup_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1026 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1027 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1028 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1029 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1030 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1031 | /* |
| 1032 | * Build the set of subsystem state objects that we want to see in the |
| 1033 | * new css_set. while subsystems can change globally, the entries here |
| 1034 | * won't change, so no need for locking. |
| 1035 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1036 | for_each_subsys(ss, i) { |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1037 | if (root->subsys_mask & (1UL << i)) { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1038 | /* |
| 1039 | * @ss is in this hierarchy, so we want the |
| 1040 | * effective css from @cgrp. |
| 1041 | */ |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 1042 | template[i] = cgroup_e_css_by_mask(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1043 | } else { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1044 | /* |
| 1045 | * @ss is not in this hierarchy, so we don't want |
| 1046 | * to change the css. |
| 1047 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1048 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1052 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1053 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 1054 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1055 | continue; |
| 1056 | |
| 1057 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1058 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1059 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1060 | |
| 1061 | /* No existing cgroup group matched */ |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1065 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1066 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1067 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1068 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1069 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 1070 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1071 | kfree(link); |
| 1072 | } |
| 1073 | } |
| 1074 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1075 | /** |
| 1076 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 1077 | * @count: the number of links to allocate |
| 1078 | * @tmp_links: list_head the allocated links are put on |
| 1079 | * |
| 1080 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 1081 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1082 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1083 | 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] | 1084 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1085 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1086 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1087 | |
| 1088 | INIT_LIST_HEAD(tmp_links); |
| 1089 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1090 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 1091 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1092 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1093 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1094 | return -ENOMEM; |
| 1095 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1096 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1097 | } |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1101 | /** |
| 1102 | * 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] | 1103 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1104 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1105 | * @cgrp: the destination cgroup |
| 1106 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1107 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 1108 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1109 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1110 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1111 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1112 | BUG_ON(list_empty(tmp_links)); |
Tejun Heo | 6803c00 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1113 | |
| 1114 | if (cgroup_on_dfl(cgrp)) |
| 1115 | cset->dfl_cgrp = cgrp; |
| 1116 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1117 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 1118 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1119 | link->cgrp = cgrp; |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 1120 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1121 | /* |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1122 | * Always add links to the tail of the lists so that the lists are |
| 1123 | * in choronological order. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1124 | */ |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1125 | list_move_tail(&link->cset_link, &cgrp->cset_links); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1126 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Tejun Heo | 2ceb231 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1127 | |
| 1128 | if (cgroup_parent(cgrp)) |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 1129 | cgroup_get_live(cgrp); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1132 | /** |
| 1133 | * find_css_set - return a new css_set with one cgroup updated |
| 1134 | * @old_cset: the baseline css_set |
| 1135 | * @cgrp: the cgroup to be updated |
| 1136 | * |
| 1137 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 1138 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1139 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1140 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 1141 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1142 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1143 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1144 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1145 | struct list_head tmp_links; |
| 1146 | struct cgrp_cset_link *link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1147 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1148 | unsigned long key; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1149 | int ssid; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1150 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1151 | lockdep_assert_held(&cgroup_mutex); |
| 1152 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1153 | /* First see if we already have a cgroup group that matches |
| 1154 | * the desired set */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1155 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1156 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 1157 | if (cset) |
| 1158 | get_css_set(cset); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1159 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1160 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1161 | if (cset) |
| 1162 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1163 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 1164 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1165 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1166 | return NULL; |
| 1167 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1168 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1169 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1170 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1171 | return NULL; |
| 1172 | } |
| 1173 | |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 1174 | refcount_set(&cset->refcount, 1); |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1175 | cset->dom_cset = cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1176 | INIT_LIST_HEAD(&cset->tasks); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1177 | INIT_LIST_HEAD(&cset->mg_tasks); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 1178 | INIT_LIST_HEAD(&cset->task_iters); |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1179 | INIT_LIST_HEAD(&cset->threaded_csets); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1180 | INIT_HLIST_NODE(&cset->hlist); |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 1181 | INIT_LIST_HEAD(&cset->cgrp_links); |
| 1182 | INIT_LIST_HEAD(&cset->mg_preload_node); |
| 1183 | INIT_LIST_HEAD(&cset->mg_node); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1184 | |
| 1185 | /* Copy the set of subsystem state objects generated in |
| 1186 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1187 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1188 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1189 | spin_lock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1190 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1191 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1192 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1193 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1194 | if (c->root == cgrp->root) |
| 1195 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1196 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1197 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1198 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1199 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1200 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1201 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1202 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1203 | /* Add @cset to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1204 | key = css_set_hash(cset->subsys); |
| 1205 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1206 | |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 1207 | for_each_subsys(ss, ssid) { |
| 1208 | struct cgroup_subsys_state *css = cset->subsys[ssid]; |
| 1209 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1210 | list_add_tail(&cset->e_cset_node[ssid], |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 1211 | &css->cgroup->e_csets[ssid]); |
| 1212 | css_get(css); |
| 1213 | } |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1214 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1215 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1216 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1217 | /* |
| 1218 | * If @cset should be threaded, look up the matching dom_cset and |
| 1219 | * link them up. We first fully initialize @cset then look for the |
| 1220 | * dom_cset. It's simpler this way and safe as @cset is guaranteed |
| 1221 | * to stay empty until we return. |
| 1222 | */ |
| 1223 | if (cgroup_is_threaded(cset->dfl_cgrp)) { |
| 1224 | struct css_set *dcset; |
| 1225 | |
| 1226 | dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp); |
| 1227 | if (!dcset) { |
| 1228 | put_css_set(cset); |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | |
| 1232 | spin_lock_irq(&css_set_lock); |
| 1233 | cset->dom_cset = dcset; |
| 1234 | list_add_tail(&cset->threaded_csets_node, |
| 1235 | &dcset->threaded_csets); |
| 1236 | spin_unlock_irq(&css_set_lock); |
| 1237 | } |
| 1238 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1239 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1242 | struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1243 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1244 | struct cgroup *root_cgrp = kf_root->kn->priv; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1245 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1246 | return root_cgrp->root; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1247 | } |
| 1248 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1249 | static int cgroup_init_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1250 | { |
| 1251 | int id; |
| 1252 | |
| 1253 | lockdep_assert_held(&cgroup_mutex); |
| 1254 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1255 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1256 | if (id < 0) |
| 1257 | return id; |
| 1258 | |
| 1259 | root->hierarchy_id = id; |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1263 | static void cgroup_exit_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1264 | { |
| 1265 | lockdep_assert_held(&cgroup_mutex); |
| 1266 | |
Johannes Weiner | 8c8a550 | 2016-06-17 12:23:59 -0400 | [diff] [blame] | 1267 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1268 | } |
| 1269 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1270 | void cgroup_free_root(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1271 | { |
| 1272 | if (root) { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1273 | idr_destroy(&root->cgroup_idr); |
| 1274 | kfree(root); |
| 1275 | } |
| 1276 | } |
| 1277 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1278 | static void cgroup_destroy_root(struct cgroup_root *root) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1279 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1280 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1281 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1282 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 1283 | trace_cgroup_destroy_root(root); |
| 1284 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1285 | cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1286 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1287 | BUG_ON(atomic_read(&root->nr_cgrps)); |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1288 | BUG_ON(!list_empty(&cgrp->self.children)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1289 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1290 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1291 | WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1292 | |
| 1293 | /* |
| 1294 | * Release all the links from cset_links to this hierarchy's |
| 1295 | * root cgroup |
| 1296 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1297 | spin_lock_irq(&css_set_lock); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1298 | |
| 1299 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1300 | list_del(&link->cset_link); |
| 1301 | list_del(&link->cgrp_link); |
| 1302 | kfree(link); |
| 1303 | } |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1304 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1305 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1306 | |
| 1307 | if (!list_empty(&root->root_list)) { |
| 1308 | list_del(&root->root_list); |
| 1309 | cgroup_root_count--; |
| 1310 | } |
| 1311 | |
| 1312 | cgroup_exit_root_id(root); |
| 1313 | |
| 1314 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1315 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1316 | kernfs_destroy_root(root->kf_root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1317 | cgroup_free_root(root); |
| 1318 | } |
| 1319 | |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1320 | /* |
| 1321 | * look up cgroup associated with current task's cgroup namespace on the |
| 1322 | * specified hierarchy |
| 1323 | */ |
| 1324 | static struct cgroup * |
| 1325 | current_cgns_cgroup_from_root(struct cgroup_root *root) |
| 1326 | { |
| 1327 | struct cgroup *res = NULL; |
| 1328 | struct css_set *cset; |
| 1329 | |
| 1330 | lockdep_assert_held(&css_set_lock); |
| 1331 | |
| 1332 | rcu_read_lock(); |
| 1333 | |
| 1334 | cset = current->nsproxy->cgroup_ns->root_cset; |
| 1335 | if (cset == &init_css_set) { |
| 1336 | res = &root->cgrp; |
| 1337 | } else { |
| 1338 | struct cgrp_cset_link *link; |
| 1339 | |
| 1340 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
| 1341 | struct cgroup *c = link->cgrp; |
| 1342 | |
| 1343 | if (c->root == root) { |
| 1344 | res = c; |
| 1345 | break; |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | rcu_read_unlock(); |
| 1350 | |
| 1351 | BUG_ON(!res); |
| 1352 | return res; |
| 1353 | } |
| 1354 | |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1355 | /* look up cgroup associated with given css_set on the specified hierarchy */ |
| 1356 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1357 | struct cgroup_root *root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1358 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1359 | struct cgroup *res = NULL; |
| 1360 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1361 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1362 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1363 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1364 | if (cset == &init_css_set) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1365 | res = &root->cgrp; |
Tejun Heo | 13d82fb | 2017-08-02 15:39:38 -0700 | [diff] [blame] | 1366 | } else if (root == &cgrp_dfl_root) { |
| 1367 | res = cset->dfl_cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1368 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1369 | struct cgrp_cset_link *link; |
| 1370 | |
| 1371 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1372 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1373 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1374 | if (c->root == root) { |
| 1375 | res = c; |
| 1376 | break; |
| 1377 | } |
| 1378 | } |
| 1379 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1380 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1381 | BUG_ON(!res); |
| 1382 | return res; |
| 1383 | } |
| 1384 | |
| 1385 | /* |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1386 | * Return the cgroup for "task" from the given hierarchy. Must be |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1387 | * called with cgroup_mutex and css_set_lock held. |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1388 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1389 | struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 1390 | struct cgroup_root *root) |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1391 | { |
| 1392 | /* |
| 1393 | * No need to lock the task - since we hold cgroup_mutex the |
| 1394 | * task can't change groups, so the only thing that can happen |
| 1395 | * is that it exits and its css is set back to init_css_set. |
| 1396 | */ |
| 1397 | return cset_cgroup_from_root(task_css_set(task), root); |
| 1398 | } |
| 1399 | |
| 1400 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1401 | * A task must hold cgroup_mutex to modify cgroups. |
| 1402 | * |
| 1403 | * Any task can increment and decrement the count field without lock. |
| 1404 | * So in general, code holding cgroup_mutex can't rely on the count |
| 1405 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1406 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1407 | * means that no tasks are currently attached, therefore there is no |
| 1408 | * way a task attached to that cgroup can fork (the other way to |
| 1409 | * increment the count). So code holding cgroup_mutex can safely |
| 1410 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 1411 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 1412 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 1413 | * needs that mutex. |
| 1414 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1415 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 1416 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 1417 | * 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] | 1418 | * least one task in the system (init, pid == 1), therefore, root cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1419 | * 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] | 1420 | * need a special hack to ensure that root cgroup cannot be deleted. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1421 | * |
| 1422 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1423 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1424 | */ |
| 1425 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1426 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 1427 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1428 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 1429 | char *buf) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1430 | { |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 1431 | struct cgroup_subsys *ss = cft->ss; |
| 1432 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1433 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
Tejun Heo | c1bbd93 | 2018-11-13 12:06:41 -0800 | [diff] [blame] | 1434 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
| 1435 | const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : ""; |
| 1436 | |
| 1437 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s", |
| 1438 | dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name, |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 1439 | cft->name); |
Tejun Heo | c1bbd93 | 2018-11-13 12:06:41 -0800 | [diff] [blame] | 1440 | } else { |
Tejun Heo | 08a7767 | 2018-01-09 07:21:15 -0800 | [diff] [blame] | 1441 | strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
Tejun Heo | c1bbd93 | 2018-11-13 12:06:41 -0800 | [diff] [blame] | 1442 | } |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1443 | return buf; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1446 | /** |
| 1447 | * cgroup_file_mode - deduce file mode of a control file |
| 1448 | * @cft: the control file in question |
| 1449 | * |
Tejun Heo | 7dbdb19 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1450 | * S_IRUGO for read, S_IWUSR for write. |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1451 | */ |
| 1452 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1453 | { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1454 | umode_t mode = 0; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1455 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1456 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 1457 | mode |= S_IRUGO; |
| 1458 | |
Tejun Heo | 7dbdb19 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1459 | if (cft->write_u64 || cft->write_s64 || cft->write) { |
| 1460 | if (cft->flags & CFTYPE_WORLD_WRITABLE) |
| 1461 | mode |= S_IWUGO; |
| 1462 | else |
| 1463 | mode |= S_IWUSR; |
| 1464 | } |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1465 | |
| 1466 | return mode; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1467 | } |
| 1468 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1469 | /** |
Tejun Heo | 8699b77 | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1470 | * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1471 | * @subtree_control: the new subtree_control mask to consider |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1472 | * @this_ss_mask: available subsystems |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1473 | * |
| 1474 | * On the default hierarchy, a subsystem may request other subsystems to be |
| 1475 | * enabled together through its ->depends_on mask. In such cases, more |
| 1476 | * subsystems than specified in "cgroup.subtree_control" may be enabled. |
| 1477 | * |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1478 | * This function calculates which subsystems need to be enabled if |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1479 | * @subtree_control is to be applied while restricted to @this_ss_mask. |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1480 | */ |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1481 | 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] | 1482 | { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 1483 | u16 cur_ss_mask = subtree_control; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1484 | struct cgroup_subsys *ss; |
| 1485 | int ssid; |
| 1486 | |
| 1487 | lockdep_assert_held(&cgroup_mutex); |
| 1488 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 1489 | cur_ss_mask |= cgrp_dfl_implicit_ss_mask; |
| 1490 | |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1491 | while (true) { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 1492 | u16 new_ss_mask = cur_ss_mask; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1493 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1494 | do_each_subsys_mask(ss, ssid, cur_ss_mask) { |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 1495 | new_ss_mask |= ss->depends_on; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1496 | } while_each_subsys_mask(); |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1497 | |
| 1498 | /* |
| 1499 | * Mask out subsystems which aren't available. This can |
| 1500 | * happen only if some depended-upon subsystems were bound |
| 1501 | * to non-default hierarchies. |
| 1502 | */ |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1503 | new_ss_mask &= this_ss_mask; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1504 | |
| 1505 | if (new_ss_mask == cur_ss_mask) |
| 1506 | break; |
| 1507 | cur_ss_mask = new_ss_mask; |
| 1508 | } |
| 1509 | |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1510 | return cur_ss_mask; |
| 1511 | } |
| 1512 | |
| 1513 | /** |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1514 | * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods |
| 1515 | * @kn: the kernfs_node being serviced |
| 1516 | * |
| 1517 | * This helper undoes cgroup_kn_lock_live() and should be invoked before |
| 1518 | * the method finishes if locking succeeded. Note that once this function |
| 1519 | * returns the cgroup returned by cgroup_kn_lock_live() may become |
| 1520 | * inaccessible any time. If the caller intends to continue to access the |
| 1521 | * cgroup, it should pin it before invoking this function. |
| 1522 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1523 | void cgroup_kn_unlock(struct kernfs_node *kn) |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1524 | { |
| 1525 | struct cgroup *cgrp; |
| 1526 | |
| 1527 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1528 | cgrp = kn->priv; |
| 1529 | else |
| 1530 | cgrp = kn->parent->priv; |
| 1531 | |
| 1532 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1533 | |
| 1534 | kernfs_unbreak_active_protection(kn); |
| 1535 | cgroup_put(cgrp); |
| 1536 | } |
| 1537 | |
| 1538 | /** |
| 1539 | * cgroup_kn_lock_live - locking helper for cgroup kernfs methods |
| 1540 | * @kn: the kernfs_node being serviced |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1541 | * @drain_offline: perform offline draining on the cgroup |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1542 | * |
| 1543 | * This helper is to be used by a cgroup kernfs method currently servicing |
| 1544 | * @kn. It breaks the active protection, performs cgroup locking and |
| 1545 | * verifies that the associated cgroup is alive. Returns the cgroup if |
| 1546 | * alive; otherwise, %NULL. A successful return should be undone by a |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1547 | * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the |
| 1548 | * cgroup is drained of offlining csses before return. |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1549 | * |
| 1550 | * Any cgroup kernfs method implementation which requires locking the |
| 1551 | * associated cgroup should use this helper. It avoids nesting cgroup |
| 1552 | * locking under kernfs active protection and allows all kernfs operations |
| 1553 | * including self-removal. |
| 1554 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1555 | 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] | 1556 | { |
| 1557 | struct cgroup *cgrp; |
| 1558 | |
| 1559 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1560 | cgrp = kn->priv; |
| 1561 | else |
| 1562 | cgrp = kn->parent->priv; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1563 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1564 | /* |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1565 | * We're gonna grab cgroup_mutex which nests outside kernfs |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1566 | * active_ref. cgroup liveliness check alone provides enough |
| 1567 | * protection against removal. Ensure @cgrp stays accessible and |
| 1568 | * break the active_ref protection. |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1569 | */ |
Li Zefan | aa32362 | 2014-09-04 14:43:38 +0800 | [diff] [blame] | 1570 | if (!cgroup_tryget(cgrp)) |
| 1571 | return NULL; |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1572 | kernfs_break_active_protection(kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1573 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1574 | if (drain_offline) |
| 1575 | cgroup_lock_and_drain_offline(cgrp); |
| 1576 | else |
| 1577 | mutex_lock(&cgroup_mutex); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1578 | |
| 1579 | if (!cgroup_is_dead(cgrp)) |
| 1580 | return cgrp; |
| 1581 | |
| 1582 | cgroup_kn_unlock(kn); |
| 1583 | return NULL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 1586 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1587 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1588 | char name[CGROUP_FILE_NAME_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1589 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1590 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 1591 | |
| 1592 | if (cft->file_offset) { |
| 1593 | struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss); |
| 1594 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 1595 | |
| 1596 | spin_lock_irq(&cgroup_file_kn_lock); |
| 1597 | cfile->kn = NULL; |
| 1598 | spin_unlock_irq(&cgroup_file_kn_lock); |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1599 | |
| 1600 | del_timer_sync(&cfile->notify_timer); |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 1601 | } |
| 1602 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1603 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1606 | /** |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1607 | * css_clear_dir - remove subsys files in a cgroup directory |
| 1608 | * @css: taget css |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1609 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1610 | static void css_clear_dir(struct cgroup_subsys_state *css) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1611 | { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1612 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1613 | struct cftype *cfts; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1614 | |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1615 | if (!(css->flags & CSS_VISIBLE)) |
| 1616 | return; |
| 1617 | |
| 1618 | css->flags &= ~CSS_VISIBLE; |
| 1619 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1620 | if (!css->ss) { |
| 1621 | if (cgroup_on_dfl(cgrp)) |
| 1622 | cfts = cgroup_base_files; |
| 1623 | else |
| 1624 | cfts = cgroup1_base_files; |
| 1625 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1626 | cgroup_addrm_files(css, cgrp, cfts, false); |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1627 | } else { |
| 1628 | list_for_each_entry(cfts, &css->ss->cfts, node) |
| 1629 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1630 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1633 | /** |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1634 | * css_populate_dir - create subsys files in a cgroup directory |
| 1635 | * @css: target css |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1636 | * |
| 1637 | * On failure, no file is added. |
| 1638 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1639 | static int css_populate_dir(struct cgroup_subsys_state *css) |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1640 | { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1641 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1642 | struct cftype *cfts, *failed_cfts; |
| 1643 | int ret; |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1644 | |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1645 | if ((css->flags & CSS_VISIBLE) || !cgrp->kn) |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1646 | return 0; |
| 1647 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1648 | if (!css->ss) { |
| 1649 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1650 | cfts = cgroup_base_files; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1651 | else |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1652 | cfts = cgroup1_base_files; |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1653 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1654 | ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true); |
| 1655 | if (ret < 0) |
| 1656 | return ret; |
| 1657 | } else { |
| 1658 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1659 | ret = cgroup_addrm_files(css, cgrp, cfts, true); |
| 1660 | if (ret < 0) { |
| 1661 | failed_cfts = cfts; |
| 1662 | goto err; |
| 1663 | } |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1664 | } |
| 1665 | } |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1666 | |
| 1667 | css->flags |= CSS_VISIBLE; |
| 1668 | |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1669 | return 0; |
| 1670 | err: |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1671 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1672 | if (cfts == failed_cfts) |
| 1673 | break; |
| 1674 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1675 | } |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1676 | return ret; |
| 1677 | } |
| 1678 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1679 | int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1680 | { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1681 | struct cgroup *dcgrp = &dst_root->cgrp; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1682 | struct cgroup_subsys *ss; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1683 | int ssid, i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1684 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1685 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1686 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1687 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 1688 | /* |
| 1689 | * If @ss has non-root csses attached to it, can't move. |
| 1690 | * If @ss is an implicit controller, it is exempt from this |
| 1691 | * rule and can be stolen. |
| 1692 | */ |
| 1693 | if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) && |
| 1694 | !ss->implicit_on_dfl) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1695 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1696 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1697 | /* can't move between two non-dummy roots either */ |
Tejun Heo | 7fd8c56 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1698 | if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1699 | return -EBUSY; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1700 | } while_each_subsys_mask(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1701 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1702 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1703 | struct cgroup_root *src_root = ss->root; |
| 1704 | struct cgroup *scgrp = &src_root->cgrp; |
| 1705 | struct cgroup_subsys_state *css = cgroup_css(scgrp, ss); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1706 | struct css_set *cset; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1707 | |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1708 | WARN_ON(!css || cgroup_css(dcgrp, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1709 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1710 | /* disable from the source */ |
| 1711 | src_root->subsys_mask &= ~(1 << ssid); |
| 1712 | WARN_ON(cgroup_apply_control(scgrp)); |
| 1713 | cgroup_finalize_control(scgrp, 0); |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1714 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1715 | /* rebind */ |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1716 | RCU_INIT_POINTER(scgrp->subsys[ssid], NULL); |
| 1717 | rcu_assign_pointer(dcgrp->subsys[ssid], css); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1718 | ss->root = dst_root; |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1719 | css->cgroup = dcgrp; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1720 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1721 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1722 | hash_for_each(css_set_table, i, cset, hlist) |
| 1723 | list_move_tail(&cset->e_cset_node[ss->id], |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1724 | &dcgrp->e_csets[ss->id]); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1725 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1726 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1727 | /* default hierarchy doesn't enable controllers by default */ |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1728 | dst_root->subsys_mask |= 1 << ssid; |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1729 | if (dst_root == &cgrp_dfl_root) { |
| 1730 | static_branch_enable(cgroup_subsys_on_dfl_key[ssid]); |
| 1731 | } else { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1732 | dcgrp->subtree_control |= 1 << ssid; |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1733 | static_branch_disable(cgroup_subsys_on_dfl_key[ssid]); |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 1734 | } |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1735 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1736 | ret = cgroup_apply_control(dcgrp); |
| 1737 | if (ret) |
| 1738 | pr_warn("partial failure to rebind %s controller (err=%d)\n", |
| 1739 | ss->name, ret); |
| 1740 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1741 | if (ss->bind) |
| 1742 | ss->bind(css); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1743 | } while_each_subsys_mask(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1744 | |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1745 | kernfs_activate(dcgrp->kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1746 | return 0; |
| 1747 | } |
| 1748 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1749 | int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node, |
| 1750 | struct kernfs_root *kf_root) |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1751 | { |
Felipe Balbi | 09be4c8 | 2016-05-12 12:34:38 +0300 | [diff] [blame] | 1752 | int len = 0; |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1753 | char *buf = NULL; |
| 1754 | struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root); |
| 1755 | struct cgroup *ns_cgroup; |
| 1756 | |
| 1757 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 1758 | if (!buf) |
| 1759 | return -ENOMEM; |
| 1760 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1761 | spin_lock_irq(&css_set_lock); |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1762 | ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot); |
| 1763 | 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] | 1764 | spin_unlock_irq(&css_set_lock); |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1765 | |
| 1766 | if (len >= PATH_MAX) |
| 1767 | len = -ERANGE; |
| 1768 | else if (len > 0) { |
| 1769 | seq_escape(sf, buf, " \t\n\\"); |
| 1770 | len = 0; |
| 1771 | } |
| 1772 | kfree(buf); |
| 1773 | return len; |
| 1774 | } |
| 1775 | |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1776 | enum cgroup2_param { |
| 1777 | Opt_nsdelegate, |
| 1778 | nr__cgroup2_params |
| 1779 | }; |
| 1780 | |
| 1781 | static const struct fs_parameter_spec cgroup2_param_specs[] = { |
| 1782 | fsparam_flag ("nsdelegate", Opt_nsdelegate), |
| 1783 | {} |
| 1784 | }; |
| 1785 | |
| 1786 | static const struct fs_parameter_description cgroup2_fs_parameters = { |
| 1787 | .name = "cgroup2", |
| 1788 | .specs = cgroup2_param_specs, |
| 1789 | }; |
| 1790 | |
| 1791 | static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param) |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1792 | { |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1793 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
| 1794 | struct fs_parse_result result; |
| 1795 | int opt; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1796 | |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1797 | opt = fs_parse(fc, &cgroup2_fs_parameters, param, &result); |
| 1798 | if (opt < 0) |
| 1799 | return opt; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1800 | |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1801 | switch (opt) { |
| 1802 | case Opt_nsdelegate: |
| 1803 | ctx->flags |= CGRP_ROOT_NS_DELEGATE; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1804 | return 0; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1805 | } |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 1806 | return -EINVAL; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | static void apply_cgroup_root_flags(unsigned int root_flags) |
| 1810 | { |
| 1811 | if (current->nsproxy->cgroup_ns == &init_cgroup_ns) { |
| 1812 | if (root_flags & CGRP_ROOT_NS_DELEGATE) |
| 1813 | cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE; |
| 1814 | else |
| 1815 | cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE; |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root) |
| 1820 | { |
| 1821 | if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) |
| 1822 | seq_puts(seq, ",nsdelegate"); |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 1826 | static int cgroup_reconfigure(struct fs_context *fc) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1827 | { |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 1828 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1829 | |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 1830 | apply_cgroup_root_flags(ctx->flags); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1831 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1834 | /* |
| 1835 | * To reduce the fork() overhead for systems that are not actually using |
| 1836 | * their cgroups capability, we don't maintain the lists running through |
| 1837 | * each css_set to its tasks until we see the list actually used - in other |
| 1838 | * words after the first mount. |
| 1839 | */ |
| 1840 | static bool use_task_css_set_links __read_mostly; |
| 1841 | |
| 1842 | static void cgroup_enable_task_cg_lists(void) |
| 1843 | { |
| 1844 | struct task_struct *p, *g; |
| 1845 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1846 | /* |
| 1847 | * We need tasklist_lock because RCU is not safe against |
| 1848 | * while_each_thread(). Besides, a forking task that has passed |
| 1849 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 1850 | * is not guaranteed to have its child immediately visible in the |
| 1851 | * tasklist if we walk through it with RCU. |
| 1852 | */ |
| 1853 | read_lock(&tasklist_lock); |
Tejun Heo | d8742e2 | 2018-05-23 11:04:54 -0700 | [diff] [blame] | 1854 | spin_lock_irq(&css_set_lock); |
| 1855 | |
| 1856 | if (use_task_css_set_links) |
| 1857 | goto out_unlock; |
| 1858 | |
| 1859 | use_task_css_set_links = true; |
| 1860 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1861 | do_each_thread(g, p) { |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1862 | WARN_ON_ONCE(!list_empty(&p->cg_list) || |
| 1863 | task_css_set(p) != &init_css_set); |
| 1864 | |
| 1865 | /* |
| 1866 | * We should check if the process is exiting, otherwise |
| 1867 | * it will race with cgroup_exit() in that the list |
| 1868 | * entry won't be deleted though the process has exited. |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1869 | * Do it while holding siglock so that we don't end up |
| 1870 | * racing against cgroup_exit(). |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1871 | * |
| 1872 | * Interrupts were already disabled while acquiring |
| 1873 | * the css_set_lock, so we do not need to disable it |
| 1874 | * again when acquiring the sighand->siglock here. |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1875 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1876 | spin_lock(&p->sighand->siglock); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1877 | if (!(p->flags & PF_EXITING)) { |
| 1878 | struct css_set *cset = task_css_set(p); |
| 1879 | |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 1880 | if (!css_set_populated(cset)) |
| 1881 | css_set_update_populated(cset, true); |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1882 | list_add_tail(&p->cg_list, &cset->tasks); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1883 | get_css_set(cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 1884 | cset->nr_tasks++; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1885 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1886 | spin_unlock(&p->sighand->siglock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1887 | } while_each_thread(g, p); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1888 | out_unlock: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1889 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | d8742e2 | 2018-05-23 11:04:54 -0700 | [diff] [blame] | 1890 | read_unlock(&tasklist_lock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1891 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1892 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1893 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1894 | { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1895 | struct cgroup_subsys *ss; |
| 1896 | int ssid; |
| 1897 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1898 | INIT_LIST_HEAD(&cgrp->self.sibling); |
| 1899 | INIT_LIST_HEAD(&cgrp->self.children); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1900 | INIT_LIST_HEAD(&cgrp->cset_links); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1901 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1902 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 1903 | cgrp->self.cgroup = cgrp; |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 1904 | cgrp->self.flags |= CSS_ONLINE; |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1905 | cgrp->dom_cgrp = cgrp; |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 1906 | cgrp->max_descendants = INT_MAX; |
| 1907 | cgrp->max_depth = INT_MAX; |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 1908 | INIT_LIST_HEAD(&cgrp->rstat_css_list); |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1909 | prev_cputime_init(&cgrp->prev_cputime); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1910 | |
| 1911 | for_each_subsys(ss, ssid) |
| 1912 | INIT_LIST_HEAD(&cgrp->e_csets[ssid]); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1913 | |
| 1914 | init_waitqueue_head(&cgrp->offline_waitq); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1915 | INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1916 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1917 | |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 1918 | void init_cgroup_root(struct cgroup_fs_context *ctx) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1919 | { |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 1920 | struct cgroup_root *root = ctx->root; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1921 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1922 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1923 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1924 | atomic_set(&root->nr_cgrps, 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1925 | cgrp->root = root; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1926 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1927 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1928 | |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 1929 | root->flags = ctx->flags; |
| 1930 | if (ctx->release_agent) |
| 1931 | strscpy(root->release_agent_path, ctx->release_agent, PATH_MAX); |
| 1932 | if (ctx->name) |
| 1933 | strscpy(root->name, ctx->name, MAX_CGROUP_ROOT_NAMELEN); |
| 1934 | if (ctx->cpuset_clone_children) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1935 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1936 | } |
| 1937 | |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 1938 | int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1939 | { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1940 | LIST_HEAD(tmp_links); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1941 | struct cgroup *root_cgrp = &root->cgrp; |
Tejun Heo | fa06990 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 1942 | struct kernfs_syscall_ops *kf_sops; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1943 | struct css_set *cset; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1944 | int i, ret; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1945 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1946 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1947 | |
Vladimir Davydov | cf780b7 | 2015-08-03 15:32:26 +0300 | [diff] [blame] | 1948 | 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] | 1949 | if (ret < 0) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1950 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1951 | root_cgrp->id = ret; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 1952 | root_cgrp->ancestor_ids[0] = ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1953 | |
Zefan Li | 9732adc | 2017-04-19 10:15:59 +0800 | [diff] [blame] | 1954 | ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 1955 | 0, GFP_KERNEL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1956 | if (ret) |
| 1957 | goto out; |
| 1958 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1959 | /* |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1960 | * We're accessing css_set_count without locking css_set_lock here, |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1961 | * but that's OK - it can only be increased by someone holding |
Tejun Heo | 0431359 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1962 | * cgroup_lock, and that's us. Later rebinding may disable |
| 1963 | * controllers on the default hierarchy and thus create new csets, |
| 1964 | * which can't be more than the existing ones. Allocate 2x. |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1965 | */ |
Tejun Heo | 0431359 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1966 | ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1967 | if (ret) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1968 | goto cancel_ref; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1969 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1970 | ret = cgroup_init_root_id(root); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1971 | if (ret) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1972 | goto cancel_ref; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1973 | |
Tejun Heo | fa06990 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 1974 | kf_sops = root == &cgrp_dfl_root ? |
| 1975 | &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops; |
| 1976 | |
| 1977 | root->kf_root = kernfs_create_root(kf_sops, |
Shaohua Li | aa81882 | 2017-07-12 11:49:51 -0700 | [diff] [blame] | 1978 | KERNFS_ROOT_CREATE_DEACTIVATED | |
| 1979 | KERNFS_ROOT_SUPPORT_EXPORTOP, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1980 | root_cgrp); |
| 1981 | if (IS_ERR(root->kf_root)) { |
| 1982 | ret = PTR_ERR(root->kf_root); |
| 1983 | goto exit_root_id; |
| 1984 | } |
| 1985 | root_cgrp->kn = root->kf_root->kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1986 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1987 | ret = css_populate_dir(&root_cgrp->self); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1988 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1989 | goto destroy_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1990 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1991 | ret = rebind_subsystems(root, ss_mask); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1992 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1993 | goto destroy_root; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1994 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1995 | ret = cgroup_bpf_inherit(root_cgrp); |
| 1996 | WARN_ON_ONCE(ret); |
| 1997 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 1998 | trace_cgroup_setup_root(root); |
| 1999 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2000 | /* |
| 2001 | * There must be no failure case after here, since rebinding takes |
| 2002 | * care of subsystems' refcounts, which are explicitly dropped in |
| 2003 | * the failure exit path. |
| 2004 | */ |
| 2005 | list_add(&root->root_list, &cgroup_roots); |
| 2006 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2007 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2008 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2009 | * Link the root cgroup in this hierarchy into all the css_set |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2010 | * objects. |
| 2011 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2012 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 2013 | hash_for_each(css_set_table, i, cset, hlist) { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2014 | link_css_set(&tmp_links, cset, root_cgrp); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 2015 | if (css_set_populated(cset)) |
| 2016 | cgroup_update_populated(root_cgrp, true); |
| 2017 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2018 | spin_unlock_irq(&css_set_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2019 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 2020 | BUG_ON(!list_empty(&root_cgrp->self.children)); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2021 | BUG_ON(atomic_read(&root->nr_cgrps) != 1); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2022 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2023 | kernfs_activate(root_cgrp->kn); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2024 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2025 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2026 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2027 | destroy_root: |
| 2028 | kernfs_destroy_root(root->kf_root); |
| 2029 | root->kf_root = NULL; |
| 2030 | exit_root_id: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2031 | cgroup_exit_root_id(root); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2032 | cancel_ref: |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 2033 | percpu_ref_exit(&root_cgrp->self.refcnt); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2034 | out: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2035 | free_cgrp_cset_links(&tmp_links); |
| 2036 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2039 | int cgroup_do_mount(struct fs_context *fc, unsigned long magic, |
| 2040 | struct cgroup_namespace *ns) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2041 | { |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2042 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
Al Viro | 399504e | 2019-01-06 11:41:29 -0500 | [diff] [blame] | 2043 | bool new_sb = false; |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2044 | int ret = 0; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 2045 | |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2046 | fc->root = kernfs_mount(fc->fs_type, fc->sb_flags, ctx->root->kf_root, |
| 2047 | magic, &new_sb); |
| 2048 | if (IS_ERR(fc->root)) |
| 2049 | ret = PTR_ERR(fc->root); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2050 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 2051 | /* |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2052 | * In non-init cgroup namespace, instead of root cgroup's dentry, |
| 2053 | * we return the dentry corresponding to the cgroupns->root_cgrp. |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2054 | */ |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2055 | if (!ret && ns != &init_cgroup_ns) { |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2056 | struct dentry *nsdentry; |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2057 | struct super_block *sb = fc->root->d_sb; |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2058 | struct cgroup *cgrp; |
| 2059 | |
| 2060 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2061 | spin_lock_irq(&css_set_lock); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2062 | |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2063 | cgrp = cset_cgroup_from_root(ns->root_cset, ctx->root); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2064 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2065 | spin_unlock_irq(&css_set_lock); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2066 | mutex_unlock(&cgroup_mutex); |
| 2067 | |
Al Viro | 399504e | 2019-01-06 11:41:29 -0500 | [diff] [blame] | 2068 | nsdentry = kernfs_node_dentry(cgrp->kn, sb); |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2069 | dput(fc->root); |
| 2070 | fc->root = nsdentry; |
| 2071 | if (IS_ERR(nsdentry)) { |
| 2072 | ret = PTR_ERR(nsdentry); |
Al Viro | 399504e | 2019-01-06 11:41:29 -0500 | [diff] [blame] | 2073 | deactivate_locked_super(sb); |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2074 | } |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2075 | } |
| 2076 | |
Al Viro | 399504e | 2019-01-06 11:41:29 -0500 | [diff] [blame] | 2077 | if (!new_sb) |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2078 | cgroup_put(&ctx->root->cgrp); |
Li Zefan | 3a32bd7 | 2014-06-30 11:50:59 +0800 | [diff] [blame] | 2079 | |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2080 | return ret; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2081 | } |
| 2082 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2083 | /* |
| 2084 | * Destroy a cgroup filesystem context. |
| 2085 | */ |
| 2086 | static void cgroup_fs_context_free(struct fs_context *fc) |
| 2087 | { |
| 2088 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
| 2089 | |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 2090 | kfree(ctx->name); |
| 2091 | kfree(ctx->release_agent); |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2092 | kfree(ctx); |
| 2093 | } |
| 2094 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2095 | static int cgroup_get_tree(struct fs_context *fc) |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2096 | { |
| 2097 | struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2098 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2099 | int ret; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2100 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2101 | /* Check if the caller has permission to mount. */ |
| 2102 | if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) |
| 2103 | return -EPERM; |
| 2104 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2105 | cgrp_dfl_visible = true; |
| 2106 | cgroup_get_live(&cgrp_dfl_root.cgrp); |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 2107 | ctx->root = &cgrp_dfl_root; |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2108 | |
Al Viro | 71d883c | 2019-01-17 02:44:07 -0500 | [diff] [blame^] | 2109 | ret = cgroup_do_mount(fc, CGROUP2_SUPER_MAGIC, ns); |
| 2110 | if (!ret) |
| 2111 | apply_cgroup_root_flags(ctx->flags); |
| 2112 | return ret; |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2113 | } |
| 2114 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2115 | static const struct fs_context_operations cgroup_fs_context_ops = { |
| 2116 | .free = cgroup_fs_context_free, |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 2117 | .parse_param = cgroup2_parse_param, |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2118 | .get_tree = cgroup_get_tree, |
| 2119 | .reconfigure = cgroup_reconfigure, |
| 2120 | }; |
| 2121 | |
| 2122 | static const struct fs_context_operations cgroup1_fs_context_ops = { |
| 2123 | .free = cgroup_fs_context_free, |
Al Viro | 8d2451f | 2019-01-17 00:15:11 -0500 | [diff] [blame] | 2124 | .parse_param = cgroup1_parse_param, |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2125 | .get_tree = cgroup1_get_tree, |
| 2126 | .reconfigure = cgroup1_reconfigure, |
| 2127 | }; |
| 2128 | |
| 2129 | /* |
| 2130 | * Initialise the cgroup filesystem creation/reconfiguration context. |
| 2131 | */ |
| 2132 | static int cgroup_init_fs_context(struct fs_context *fc) |
| 2133 | { |
| 2134 | struct cgroup_fs_context *ctx; |
| 2135 | |
| 2136 | ctx = kzalloc(sizeof(struct cgroup_fs_context), GFP_KERNEL); |
| 2137 | if (!ctx) |
| 2138 | return -ENOMEM; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2139 | |
Li Zefan | 3a32bd7 | 2014-06-30 11:50:59 +0800 | [diff] [blame] | 2140 | /* |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2141 | * The first time anyone tries to mount a cgroup, enable the list |
| 2142 | * 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] | 2143 | */ |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2144 | if (!use_task_css_set_links) |
| 2145 | cgroup_enable_task_cg_lists(); |
| 2146 | |
Al Viro | 9012962 | 2019-01-05 00:38:03 -0500 | [diff] [blame] | 2147 | fc->fs_private = ctx; |
| 2148 | if (fc->fs_type == &cgroup2_fs_type) |
| 2149 | fc->ops = &cgroup_fs_context_ops; |
| 2150 | else |
| 2151 | fc->ops = &cgroup1_fs_context_ops; |
| 2152 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2155 | static void cgroup_kill_sb(struct super_block *sb) |
| 2156 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2157 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2158 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2159 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2160 | /* |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 2161 | * If @root doesn't have any children, start killing it. |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2162 | * This prevents new mounts by disabling percpu_ref_tryget_live(). |
| 2163 | * cgroup_mount() may wait for @root's release. |
Li Zefan | 1f779fb | 2014-06-04 16:48:15 +0800 | [diff] [blame] | 2164 | * |
| 2165 | * And don't kill the default root. |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2166 | */ |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 2167 | if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root && |
| 2168 | !percpu_ref_is_dying(&root->cgrp.self.refcnt)) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2169 | percpu_ref_kill(&root->cgrp.self.refcnt); |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 2170 | cgroup_put(&root->cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2171 | kernfs_kill_sb(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2174 | struct file_system_type cgroup_fs_type = { |
Al Viro | 8d2451f | 2019-01-17 00:15:11 -0500 | [diff] [blame] | 2175 | .name = "cgroup", |
| 2176 | .init_fs_context = cgroup_init_fs_context, |
| 2177 | .parameters = &cgroup1_fs_parameters, |
| 2178 | .kill_sb = cgroup_kill_sb, |
| 2179 | .fs_flags = FS_USERNS_MOUNT, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2180 | }; |
| 2181 | |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 2182 | static struct file_system_type cgroup2_fs_type = { |
Al Viro | e34a98d | 2019-01-17 00:22:58 -0500 | [diff] [blame] | 2183 | .name = "cgroup2", |
| 2184 | .init_fs_context = cgroup_init_fs_context, |
| 2185 | .parameters = &cgroup2_fs_parameters, |
| 2186 | .kill_sb = cgroup_kill_sb, |
| 2187 | .fs_flags = FS_USERNS_MOUNT, |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 2188 | }; |
| 2189 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2190 | int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen, |
| 2191 | struct cgroup_namespace *ns) |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2192 | { |
| 2193 | struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2194 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2195 | return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2196 | } |
| 2197 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2198 | int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen, |
| 2199 | struct cgroup_namespace *ns) |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2200 | { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2201 | int ret; |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2202 | |
| 2203 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2204 | spin_lock_irq(&css_set_lock); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2205 | |
| 2206 | ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns); |
| 2207 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2208 | spin_unlock_irq(&css_set_lock); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2209 | mutex_unlock(&cgroup_mutex); |
| 2210 | |
| 2211 | return ret; |
| 2212 | } |
| 2213 | EXPORT_SYMBOL_GPL(cgroup_path_ns); |
| 2214 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2215 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2216 | * 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] | 2217 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2218 | * @buf: the buffer to write the path into |
| 2219 | * @buflen: the length of the buffer |
| 2220 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2221 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 2222 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 2223 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 2224 | * cgroup controller callbacks. |
| 2225 | * |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2226 | * Return value is the same as kernfs_path(). |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2227 | */ |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2228 | 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] | 2229 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2230 | struct cgroup_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2231 | struct cgroup *cgrp; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2232 | int hierarchy_id = 1; |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2233 | int ret; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2234 | |
| 2235 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2236 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2237 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2238 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 2239 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2240 | if (root) { |
| 2241 | cgrp = task_cgroup_from_root(task, root); |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2242 | ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2243 | } else { |
| 2244 | /* if no hierarchy exists, everyone is in "/" */ |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2245 | ret = strlcpy(buf, "/", buflen); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2248 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2249 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2250 | return ret; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2251 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2252 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2253 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2254 | /** |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2255 | * 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] | 2256 | * @task: target task |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2257 | * @mgctx: target migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2258 | * |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2259 | * Add @task, which is a migration target, to @mgctx->tset. This function |
| 2260 | * becomes noop if @task doesn't need to be migrated. @task's css_set |
| 2261 | * should have been added as a migration source and @task->cg_list will be |
| 2262 | * moved from the css_set's tasks list to mg_tasks one. |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2263 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2264 | static void cgroup_migrate_add_task(struct task_struct *task, |
| 2265 | struct cgroup_mgctx *mgctx) |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2266 | { |
| 2267 | struct css_set *cset; |
| 2268 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 2269 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2270 | |
| 2271 | /* @task either already exited or can't exit until the end */ |
| 2272 | if (task->flags & PF_EXITING) |
| 2273 | return; |
| 2274 | |
| 2275 | /* leave @task alone if post_fork() hasn't linked it yet */ |
| 2276 | if (list_empty(&task->cg_list)) |
| 2277 | return; |
| 2278 | |
| 2279 | cset = task_css_set(task); |
| 2280 | if (!cset->mg_src_cgrp) |
| 2281 | return; |
| 2282 | |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2283 | mgctx->tset.nr_tasks++; |
| 2284 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2285 | list_move_tail(&task->cg_list, &cset->mg_tasks); |
| 2286 | if (list_empty(&cset->mg_node)) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2287 | list_add_tail(&cset->mg_node, |
| 2288 | &mgctx->tset.src_csets); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2289 | if (list_empty(&cset->mg_dst_cset->mg_node)) |
Tejun Heo | d8ebf51 | 2017-01-15 19:03:40 -0500 | [diff] [blame] | 2290 | list_add_tail(&cset->mg_dst_cset->mg_node, |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2291 | &mgctx->tset.dst_csets); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2292 | } |
| 2293 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2294 | /** |
| 2295 | * cgroup_taskset_first - reset taskset and return the first task |
| 2296 | * @tset: taskset of interest |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2297 | * @dst_cssp: output variable for the destination css |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2298 | * |
| 2299 | * @tset iteration is initialized and the first task is returned. |
| 2300 | */ |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2301 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset, |
| 2302 | struct cgroup_subsys_state **dst_cssp) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2303 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2304 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); |
| 2305 | tset->cur_task = NULL; |
| 2306 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2307 | return cgroup_taskset_next(tset, dst_cssp); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2308 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2309 | |
| 2310 | /** |
| 2311 | * cgroup_taskset_next - iterate to the next task in taskset |
| 2312 | * @tset: taskset of interest |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2313 | * @dst_cssp: output variable for the destination css |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2314 | * |
| 2315 | * Return the next task in @tset. Iteration must have been initialized |
| 2316 | * with cgroup_taskset_first(). |
| 2317 | */ |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2318 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset, |
| 2319 | struct cgroup_subsys_state **dst_cssp) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2320 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2321 | struct css_set *cset = tset->cur_cset; |
| 2322 | struct task_struct *task = tset->cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2323 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2324 | while (&cset->mg_node != tset->csets) { |
| 2325 | if (!task) |
| 2326 | task = list_first_entry(&cset->mg_tasks, |
| 2327 | struct task_struct, cg_list); |
| 2328 | else |
| 2329 | task = list_next_entry(task, cg_list); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2330 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2331 | if (&task->cg_list != &cset->mg_tasks) { |
| 2332 | tset->cur_cset = cset; |
| 2333 | tset->cur_task = task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2334 | |
| 2335 | /* |
| 2336 | * This function may be called both before and |
| 2337 | * after cgroup_taskset_migrate(). The two cases |
| 2338 | * can be distinguished by looking at whether @cset |
| 2339 | * has its ->mg_dst_cset set. |
| 2340 | */ |
| 2341 | if (cset->mg_dst_cset) |
| 2342 | *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid]; |
| 2343 | else |
| 2344 | *dst_cssp = cset->subsys[tset->ssid]; |
| 2345 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2346 | return task; |
| 2347 | } |
| 2348 | |
| 2349 | cset = list_next_entry(cset, mg_node); |
| 2350 | task = NULL; |
| 2351 | } |
| 2352 | |
| 2353 | return NULL; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2354 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2355 | |
| 2356 | /** |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2357 | * cgroup_taskset_migrate - migrate a taskset |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2358 | * @mgctx: migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2359 | * |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2360 | * Migrate tasks in @mgctx as setup by migration preparation functions. |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2361 | * This function fails iff one of the ->can_attach callbacks fails and |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2362 | * guarantees that either all or none of the tasks in @mgctx are migrated. |
| 2363 | * @mgctx is consumed regardless of success. |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2364 | */ |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2365 | static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx) |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2366 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2367 | struct cgroup_taskset *tset = &mgctx->tset; |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2368 | struct cgroup_subsys *ss; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2369 | struct task_struct *task, *tmp_task; |
| 2370 | struct css_set *cset, *tmp_cset; |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2371 | int ssid, failed_ssid, ret; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2372 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2373 | /* check that we can legitimately attach to the cgroup */ |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2374 | if (tset->nr_tasks) { |
| 2375 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2376 | if (ss->can_attach) { |
| 2377 | tset->ssid = ssid; |
| 2378 | ret = ss->can_attach(tset); |
| 2379 | if (ret) { |
| 2380 | failed_ssid = ssid; |
| 2381 | goto out_cancel_attach; |
| 2382 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2383 | } |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2384 | } while_each_subsys_mask(); |
| 2385 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2386 | |
| 2387 | /* |
| 2388 | * Now that we're guaranteed success, proceed to move all tasks to |
| 2389 | * the new cgroup. There are no failure cases after here, so this |
| 2390 | * is the commit point. |
| 2391 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2392 | spin_lock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2393 | list_for_each_entry(cset, &tset->src_csets, mg_node) { |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2394 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) { |
| 2395 | struct css_set *from_cset = task_css_set(task); |
| 2396 | struct css_set *to_cset = cset->mg_dst_cset; |
| 2397 | |
| 2398 | get_css_set(to_cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 2399 | to_cset->nr_tasks++; |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2400 | css_set_move_task(task, from_cset, to_cset, true); |
| 2401 | put_css_set_locked(from_cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 2402 | from_cset->nr_tasks--; |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2403 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2404 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2405 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2406 | |
| 2407 | /* |
| 2408 | * Migration is committed, all target tasks are now on dst_csets. |
| 2409 | * Nothing is sensitive to fork() after this point. Notify |
| 2410 | * controllers that migration is complete. |
| 2411 | */ |
| 2412 | tset->csets = &tset->dst_csets; |
| 2413 | |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2414 | if (tset->nr_tasks) { |
| 2415 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2416 | if (ss->attach) { |
| 2417 | tset->ssid = ssid; |
| 2418 | ss->attach(tset); |
| 2419 | } |
| 2420 | } while_each_subsys_mask(); |
| 2421 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2422 | |
| 2423 | ret = 0; |
| 2424 | goto out_release_tset; |
| 2425 | |
| 2426 | out_cancel_attach: |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2427 | if (tset->nr_tasks) { |
| 2428 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2429 | if (ssid == failed_ssid) |
| 2430 | break; |
| 2431 | if (ss->cancel_attach) { |
| 2432 | tset->ssid = ssid; |
| 2433 | ss->cancel_attach(tset); |
| 2434 | } |
| 2435 | } while_each_subsys_mask(); |
| 2436 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2437 | out_release_tset: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2438 | spin_lock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2439 | list_splice_init(&tset->dst_csets, &tset->src_csets); |
| 2440 | list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) { |
| 2441 | list_splice_tail_init(&cset->mg_tasks, &cset->tasks); |
| 2442 | list_del_init(&cset->mg_node); |
| 2443 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2444 | spin_unlock_irq(&css_set_lock); |
Waiman Long | c4fa6c4 | 2017-09-21 09:54:13 -0400 | [diff] [blame] | 2445 | |
| 2446 | /* |
| 2447 | * Re-initialize the cgroup_taskset structure in case it is reused |
| 2448 | * again in another cgroup_migrate_add_task()/cgroup_migrate_execute() |
| 2449 | * iteration. |
| 2450 | */ |
| 2451 | tset->nr_tasks = 0; |
| 2452 | tset->csets = &tset->src_csets; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2453 | return ret; |
| 2454 | } |
| 2455 | |
| 2456 | /** |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2457 | * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2458 | * @dst_cgrp: destination cgroup to test |
| 2459 | * |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2460 | * On the default hierarchy, except for the mixable, (possible) thread root |
| 2461 | * and threaded cgroups, subtree_control must be zero for migration |
| 2462 | * destination cgroups with tasks so that child cgroups don't compete |
| 2463 | * against tasks. |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2464 | */ |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2465 | int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp) |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2466 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2467 | /* v1 doesn't have any restriction */ |
| 2468 | if (!cgroup_on_dfl(dst_cgrp)) |
| 2469 | return 0; |
| 2470 | |
| 2471 | /* verify @dst_cgrp can host resources */ |
| 2472 | if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp)) |
| 2473 | return -EOPNOTSUPP; |
| 2474 | |
| 2475 | /* mixables don't care */ |
| 2476 | if (cgroup_is_mixable(dst_cgrp)) |
| 2477 | return 0; |
| 2478 | |
| 2479 | /* |
| 2480 | * If @dst_cgrp is already or can become a thread root or is |
| 2481 | * threaded, it doesn't matter. |
| 2482 | */ |
| 2483 | if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp)) |
| 2484 | return 0; |
| 2485 | |
| 2486 | /* apply no-internal-process constraint */ |
| 2487 | if (dst_cgrp->subtree_control) |
| 2488 | return -EBUSY; |
| 2489 | |
| 2490 | return 0; |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | /** |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2494 | * cgroup_migrate_finish - cleanup after attach |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2495 | * @mgctx: migration context |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2496 | * |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2497 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See |
| 2498 | * those functions for details. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2499 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2500 | void cgroup_migrate_finish(struct cgroup_mgctx *mgctx) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2501 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2502 | LIST_HEAD(preloaded); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2503 | struct css_set *cset, *tmp_cset; |
| 2504 | |
| 2505 | lockdep_assert_held(&cgroup_mutex); |
| 2506 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2507 | spin_lock_irq(&css_set_lock); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2508 | |
| 2509 | list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded); |
| 2510 | list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded); |
| 2511 | |
| 2512 | list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2513 | cset->mg_src_cgrp = NULL; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2514 | cset->mg_dst_cgrp = NULL; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2515 | cset->mg_dst_cset = NULL; |
| 2516 | list_del_init(&cset->mg_preload_node); |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2517 | put_css_set_locked(cset); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2518 | } |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2519 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2520 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | /** |
| 2524 | * cgroup_migrate_add_src - add a migration source css_set |
| 2525 | * @src_cset: the source css_set to add |
| 2526 | * @dst_cgrp: the destination cgroup |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2527 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2528 | * |
| 2529 | * 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] | 2530 | * @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] | 2531 | * up by cgroup_migrate_finish(). |
| 2532 | * |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 2533 | * This function may be called without holding cgroup_threadgroup_rwsem |
| 2534 | * even if the target is a process. Threads may be created and destroyed |
| 2535 | * but as long as cgroup_mutex is not dropped, no new css_set can be put |
| 2536 | * into play and the preloaded css_sets are guaranteed to cover all |
| 2537 | * migrations. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2538 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2539 | void cgroup_migrate_add_src(struct css_set *src_cset, |
| 2540 | struct cgroup *dst_cgrp, |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2541 | struct cgroup_mgctx *mgctx) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2542 | { |
| 2543 | struct cgroup *src_cgrp; |
| 2544 | |
| 2545 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 2546 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2547 | |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 2548 | /* |
| 2549 | * If ->dead, @src_set is associated with one or more dead cgroups |
| 2550 | * and doesn't contain any migratable tasks. Ignore it early so |
| 2551 | * that the rest of migration path doesn't get confused by it. |
| 2552 | */ |
| 2553 | if (src_cset->dead) |
| 2554 | return; |
| 2555 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2556 | src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); |
| 2557 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2558 | if (!list_empty(&src_cset->mg_preload_node)) |
| 2559 | return; |
| 2560 | |
| 2561 | WARN_ON(src_cset->mg_src_cgrp); |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2562 | WARN_ON(src_cset->mg_dst_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2563 | WARN_ON(!list_empty(&src_cset->mg_tasks)); |
| 2564 | WARN_ON(!list_empty(&src_cset->mg_node)); |
| 2565 | |
| 2566 | src_cset->mg_src_cgrp = src_cgrp; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2567 | src_cset->mg_dst_cgrp = dst_cgrp; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2568 | get_css_set(src_cset); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2569 | list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | /** |
| 2573 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2574 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2575 | * |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2576 | * 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] | 2577 | * preloaded to @mgctx->preloaded_src_csets. This function looks up and |
| 2578 | * pins all destination css_sets, links each to its source, and append them |
| 2579 | * to @mgctx->preloaded_dst_csets. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2580 | * |
| 2581 | * This function must be called after cgroup_migrate_add_src() has been |
| 2582 | * called on each migration source css_set. After migration is performed |
| 2583 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2584 | * @mgctx. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2585 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2586 | int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2587 | { |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2588 | struct css_set *src_cset, *tmp_cset; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2589 | |
| 2590 | lockdep_assert_held(&cgroup_mutex); |
| 2591 | |
| 2592 | /* 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] | 2593 | list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets, |
| 2594 | mg_preload_node) { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2595 | struct css_set *dst_cset; |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2596 | struct cgroup_subsys *ss; |
| 2597 | int ssid; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2598 | |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2599 | dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2600 | if (!dst_cset) |
| 2601 | goto err; |
| 2602 | |
| 2603 | 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] | 2604 | |
| 2605 | /* |
| 2606 | * If src cset equals dst, it's noop. Drop the src. |
| 2607 | * cgroup_migrate() will skip the cset too. Note that we |
| 2608 | * can't handle src == dst as some nodes are used by both. |
| 2609 | */ |
| 2610 | if (src_cset == dst_cset) { |
| 2611 | src_cset->mg_src_cgrp = NULL; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2612 | src_cset->mg_dst_cgrp = NULL; |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2613 | list_del_init(&src_cset->mg_preload_node); |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2614 | put_css_set(src_cset); |
| 2615 | put_css_set(dst_cset); |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2616 | continue; |
| 2617 | } |
| 2618 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2619 | src_cset->mg_dst_cset = dst_cset; |
| 2620 | |
| 2621 | if (list_empty(&dst_cset->mg_preload_node)) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2622 | list_add_tail(&dst_cset->mg_preload_node, |
| 2623 | &mgctx->preloaded_dst_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2624 | else |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2625 | put_css_set(dst_cset); |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2626 | |
| 2627 | for_each_subsys(ss, ssid) |
| 2628 | if (src_cset->subsys[ssid] != dst_cset->subsys[ssid]) |
| 2629 | mgctx->ss_mask |= 1 << ssid; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2630 | } |
| 2631 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2632 | return 0; |
| 2633 | err: |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2634 | cgroup_migrate_finish(mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2635 | return -ENOMEM; |
| 2636 | } |
| 2637 | |
| 2638 | /** |
| 2639 | * cgroup_migrate - migrate a process or task to a cgroup |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2640 | * @leader: the leader of the process or the task to migrate |
| 2641 | * @threadgroup: whether @leader points to the whole process or a single task |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2642 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2643 | * |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2644 | * Migrate a process or task denoted by @leader. If migrating a process, |
| 2645 | * the caller must be holding cgroup_threadgroup_rwsem. The caller is also |
| 2646 | * responsible for invoking cgroup_migrate_add_src() and |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2647 | * cgroup_migrate_prepare_dst() on the targets before invoking this |
| 2648 | * function and following up with cgroup_migrate_finish(). |
| 2649 | * |
| 2650 | * As long as a controller's ->can_attach() doesn't fail, this function is |
| 2651 | * guaranteed to succeed. This means that, excluding ->can_attach() |
| 2652 | * failure, when migrating multiple targets, the success or failure can be |
| 2653 | * decided for all targets by invoking group_migrate_prepare_dst() before |
| 2654 | * actually starting migrating. |
| 2655 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2656 | int cgroup_migrate(struct task_struct *leader, bool threadgroup, |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2657 | struct cgroup_mgctx *mgctx) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2658 | { |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2659 | struct task_struct *task; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2660 | |
| 2661 | /* |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2662 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2663 | * already PF_EXITING could be freed from underneath us unless we |
| 2664 | * take an rcu_read_lock. |
| 2665 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2666 | spin_lock_irq(&css_set_lock); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2667 | rcu_read_lock(); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2668 | task = leader; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2669 | do { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2670 | cgroup_migrate_add_task(task, mgctx); |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2671 | if (!threadgroup) |
| 2672 | break; |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2673 | } while_each_thread(leader, task); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2674 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2675 | spin_unlock_irq(&css_set_lock); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2676 | |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2677 | return cgroup_migrate_execute(mgctx); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2678 | } |
| 2679 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2680 | /** |
| 2681 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
| 2682 | * @dst_cgrp: the cgroup to attach to |
| 2683 | * @leader: the task or the leader of the threadgroup to be attached |
| 2684 | * @threadgroup: attach the whole threadgroup? |
| 2685 | * |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 2686 | * Call holding cgroup_mutex and cgroup_threadgroup_rwsem. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2687 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2688 | int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, |
| 2689 | bool threadgroup) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2690 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2691 | DEFINE_CGROUP_MGCTX(mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2692 | struct task_struct *task; |
| 2693 | int ret; |
| 2694 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2695 | ret = cgroup_migrate_vet_dst(dst_cgrp); |
| 2696 | if (ret) |
| 2697 | return ret; |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2698 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2699 | /* look up all src csets */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2700 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2701 | rcu_read_lock(); |
| 2702 | task = leader; |
| 2703 | do { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2704 | cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2705 | if (!threadgroup) |
| 2706 | break; |
| 2707 | } while_each_thread(leader, task); |
| 2708 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2709 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2710 | |
| 2711 | /* prepare dst csets and commit */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2712 | ret = cgroup_migrate_prepare_dst(&mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2713 | if (!ret) |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2714 | ret = cgroup_migrate(leader, threadgroup, &mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2715 | |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2716 | cgroup_migrate_finish(&mgctx); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2717 | |
| 2718 | if (!ret) |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 2719 | TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2720 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2721 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2722 | } |
| 2723 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2724 | struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup) |
| 2725 | __acquires(&cgroup_threadgroup_rwsem) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2726 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2727 | struct task_struct *tsk; |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2728 | pid_t pid; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2729 | |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2730 | if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2731 | return ERR_PTR(-EINVAL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2732 | |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2733 | percpu_down_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2734 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2735 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2736 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2737 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2738 | if (!tsk) { |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2739 | tsk = ERR_PTR(-ESRCH); |
| 2740 | goto out_unlock_threadgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2741 | } |
Tejun Heo | dedf22e | 2015-06-18 16:54:28 -0400 | [diff] [blame] | 2742 | } else { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2743 | tsk = current; |
Tejun Heo | dedf22e | 2015-06-18 16:54:28 -0400 | [diff] [blame] | 2744 | } |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2745 | |
| 2746 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2747 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2748 | |
| 2749 | /* |
Tejun Heo | 77f8879 | 2017-03-16 16:54:24 -0400 | [diff] [blame] | 2750 | * kthreads may acquire PF_NO_SETAFFINITY during initialization. |
| 2751 | * If userland migrates such a kthread to a non-root cgroup, it can |
| 2752 | * become trapped in a cpuset, or RT kthread may be born in a |
| 2753 | * cgroup with no rt_runtime allocated. Just say no. |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2754 | */ |
Tejun Heo | 77f8879 | 2017-03-16 16:54:24 -0400 | [diff] [blame] | 2755 | if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) { |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2756 | tsk = ERR_PTR(-EINVAL); |
| 2757 | goto out_unlock_threadgroup; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2758 | } |
| 2759 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2760 | get_task_struct(tsk); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2761 | goto out_unlock_rcu; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2762 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2763 | out_unlock_threadgroup: |
| 2764 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2765 | out_unlock_rcu: |
| 2766 | rcu_read_unlock(); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2767 | return tsk; |
| 2768 | } |
| 2769 | |
| 2770 | void cgroup_procs_write_finish(struct task_struct *task) |
| 2771 | __releases(&cgroup_threadgroup_rwsem) |
| 2772 | { |
| 2773 | struct cgroup_subsys *ss; |
| 2774 | int ssid; |
| 2775 | |
| 2776 | /* release reference from cgroup_procs_write_start() */ |
| 2777 | put_task_struct(task); |
| 2778 | |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2779 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | 5cf1cac | 2016-04-21 19:06:48 -0400 | [diff] [blame] | 2780 | for_each_subsys(ss, ssid) |
| 2781 | if (ss->post_attach) |
| 2782 | ss->post_attach(); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 2785 | 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] | 2786 | { |
| 2787 | struct cgroup_subsys *ss; |
| 2788 | bool printed = false; |
| 2789 | int ssid; |
| 2790 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 2791 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 2792 | if (printed) |
| 2793 | seq_putc(seq, ' '); |
| 2794 | seq_printf(seq, "%s", ss->name); |
| 2795 | printed = true; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 2796 | } while_each_subsys_mask(); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2797 | if (printed) |
| 2798 | seq_putc(seq, '\n'); |
| 2799 | } |
| 2800 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2801 | /* show controllers which are enabled from the parent */ |
| 2802 | static int cgroup_controllers_show(struct seq_file *seq, void *v) |
| 2803 | { |
| 2804 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2805 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 2806 | cgroup_print_ss_mask(seq, cgroup_control(cgrp)); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2807 | return 0; |
| 2808 | } |
| 2809 | |
| 2810 | /* show controllers which are enabled for a given cgroup's children */ |
| 2811 | static int cgroup_subtree_control_show(struct seq_file *seq, void *v) |
| 2812 | { |
| 2813 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2814 | |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 2815 | cgroup_print_ss_mask(seq, cgrp->subtree_control); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2816 | return 0; |
| 2817 | } |
| 2818 | |
| 2819 | /** |
| 2820 | * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy |
| 2821 | * @cgrp: root of the subtree to update csses for |
| 2822 | * |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2823 | * @cgrp's control masks have changed and its subtree's css associations |
| 2824 | * need to be updated accordingly. This function looks up all css_sets |
| 2825 | * which are attached to the subtree, creates the matching updated css_sets |
| 2826 | * and migrates the tasks to the new ones. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2827 | */ |
| 2828 | static int cgroup_update_dfl_csses(struct cgroup *cgrp) |
| 2829 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2830 | DEFINE_CGROUP_MGCTX(mgctx); |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2831 | struct cgroup_subsys_state *d_css; |
| 2832 | struct cgroup *dsct; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2833 | struct css_set *src_cset; |
| 2834 | int ret; |
| 2835 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2836 | lockdep_assert_held(&cgroup_mutex); |
| 2837 | |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2838 | percpu_down_write(&cgroup_threadgroup_rwsem); |
| 2839 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2840 | /* look up all csses currently attached to @cgrp's subtree */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2841 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2842 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2843 | struct cgrp_cset_link *link; |
| 2844 | |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2845 | list_for_each_entry(link, &dsct->cset_links, cset_link) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2846 | cgroup_migrate_add_src(link->cset, dsct, &mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2847 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2848 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2849 | |
| 2850 | /* NULL dst indicates self on default hierarchy */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2851 | ret = cgroup_migrate_prepare_dst(&mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2852 | if (ret) |
| 2853 | goto out_finish; |
| 2854 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2855 | spin_lock_irq(&css_set_lock); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2856 | 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] | 2857 | struct task_struct *task, *ntask; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2858 | |
Tejun Heo | 1026507 | 2015-09-11 15:00:22 -0400 | [diff] [blame] | 2859 | /* all tasks in src_csets need to be migrated */ |
| 2860 | list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2861 | cgroup_migrate_add_task(task, &mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2862 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2863 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2864 | |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2865 | ret = cgroup_migrate_execute(&mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2866 | out_finish: |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2867 | cgroup_migrate_finish(&mgctx); |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2868 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2869 | return ret; |
| 2870 | } |
| 2871 | |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2872 | /** |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2873 | * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2874 | * @cgrp: root of the target subtree |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2875 | * |
| 2876 | * Because css offlining is asynchronous, userland may try to re-enable a |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2877 | * controller while the previous css is still around. This function grabs |
| 2878 | * cgroup_mutex and drains the previous css instances of @cgrp's subtree. |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2879 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2880 | void cgroup_lock_and_drain_offline(struct cgroup *cgrp) |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2881 | __acquires(&cgroup_mutex) |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2882 | { |
| 2883 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2884 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2885 | struct cgroup_subsys *ss; |
| 2886 | int ssid; |
| 2887 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2888 | restart: |
| 2889 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2890 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2891 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2892 | for_each_subsys(ss, ssid) { |
| 2893 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 2894 | DEFINE_WAIT(wait); |
| 2895 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2896 | if (!css || !percpu_ref_is_dying(&css->refcnt)) |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2897 | continue; |
| 2898 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 2899 | cgroup_get_live(dsct); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2900 | prepare_to_wait(&dsct->offline_waitq, &wait, |
| 2901 | TASK_UNINTERRUPTIBLE); |
| 2902 | |
| 2903 | mutex_unlock(&cgroup_mutex); |
| 2904 | schedule(); |
| 2905 | finish_wait(&dsct->offline_waitq, &wait); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2906 | |
| 2907 | cgroup_put(dsct); |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2908 | goto restart; |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2909 | } |
| 2910 | } |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2911 | } |
| 2912 | |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2913 | /** |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 2914 | * cgroup_save_control - save control masks and dom_cgrp of a subtree |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2915 | * @cgrp: root of the target subtree |
| 2916 | * |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 2917 | * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the |
| 2918 | * respective old_ prefixed fields for @cgrp's subtree including @cgrp |
| 2919 | * itself. |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2920 | */ |
| 2921 | static void cgroup_save_control(struct cgroup *cgrp) |
| 2922 | { |
| 2923 | struct cgroup *dsct; |
| 2924 | struct cgroup_subsys_state *d_css; |
| 2925 | |
| 2926 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 2927 | dsct->old_subtree_control = dsct->subtree_control; |
| 2928 | dsct->old_subtree_ss_mask = dsct->subtree_ss_mask; |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 2929 | dsct->old_dom_cgrp = dsct->dom_cgrp; |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | /** |
| 2934 | * cgroup_propagate_control - refresh control masks of a subtree |
| 2935 | * @cgrp: root of the target subtree |
| 2936 | * |
| 2937 | * For @cgrp and its subtree, ensure ->subtree_ss_mask matches |
| 2938 | * ->subtree_control and propagate controller availability through the |
| 2939 | * subtree so that descendants don't have unavailable controllers enabled. |
| 2940 | */ |
| 2941 | static void cgroup_propagate_control(struct cgroup *cgrp) |
| 2942 | { |
| 2943 | struct cgroup *dsct; |
| 2944 | struct cgroup_subsys_state *d_css; |
| 2945 | |
| 2946 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 2947 | dsct->subtree_control &= cgroup_control(dsct); |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2948 | dsct->subtree_ss_mask = |
| 2949 | cgroup_calc_subtree_ss_mask(dsct->subtree_control, |
| 2950 | cgroup_ss_mask(dsct)); |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2951 | } |
| 2952 | } |
| 2953 | |
| 2954 | /** |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 2955 | * cgroup_restore_control - restore control masks and dom_cgrp of a subtree |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2956 | * @cgrp: root of the target subtree |
| 2957 | * |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 2958 | * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the |
| 2959 | * respective old_ prefixed fields for @cgrp's subtree including @cgrp |
| 2960 | * itself. |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2961 | */ |
| 2962 | static void cgroup_restore_control(struct cgroup *cgrp) |
| 2963 | { |
| 2964 | struct cgroup *dsct; |
| 2965 | struct cgroup_subsys_state *d_css; |
| 2966 | |
| 2967 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
| 2968 | dsct->subtree_control = dsct->old_subtree_control; |
| 2969 | dsct->subtree_ss_mask = dsct->old_subtree_ss_mask; |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 2970 | dsct->dom_cgrp = dsct->old_dom_cgrp; |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2971 | } |
| 2972 | } |
| 2973 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2974 | static bool css_visible(struct cgroup_subsys_state *css) |
| 2975 | { |
| 2976 | struct cgroup_subsys *ss = css->ss; |
| 2977 | struct cgroup *cgrp = css->cgroup; |
| 2978 | |
| 2979 | if (cgroup_control(cgrp) & (1 << ss->id)) |
| 2980 | return true; |
| 2981 | if (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) |
| 2982 | return false; |
| 2983 | return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl; |
| 2984 | } |
| 2985 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2986 | /** |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2987 | * cgroup_apply_control_enable - enable or show csses according to control |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2988 | * @cgrp: root of the target subtree |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2989 | * |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2990 | * 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] | 2991 | * visible. A css is created invisible if it's being implicitly enabled |
| 2992 | * through dependency. An invisible css is made visible when the userland |
| 2993 | * explicitly enables it. |
| 2994 | * |
| 2995 | * Returns 0 on success, -errno on failure. On failure, csses which have |
| 2996 | * been processed already aren't cleaned up. The caller is responsible for |
Masahiro Yamada | 8a1115f | 2017-03-09 16:16:31 -0800 | [diff] [blame] | 2997 | * cleaning up with cgroup_apply_control_disable(). |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2998 | */ |
| 2999 | static int cgroup_apply_control_enable(struct cgroup *cgrp) |
| 3000 | { |
| 3001 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3002 | struct cgroup_subsys_state *d_css; |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3003 | struct cgroup_subsys *ss; |
| 3004 | int ssid, ret; |
| 3005 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3006 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3007 | for_each_subsys(ss, ssid) { |
| 3008 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 3009 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3010 | WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); |
| 3011 | |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3012 | if (!(cgroup_ss_mask(dsct) & (1 << ss->id))) |
| 3013 | continue; |
| 3014 | |
| 3015 | if (!css) { |
| 3016 | css = css_create(dsct, ss); |
| 3017 | if (IS_ERR(css)) |
| 3018 | return PTR_ERR(css); |
| 3019 | } |
| 3020 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 3021 | if (css_visible(css)) { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 3022 | ret = css_populate_dir(css); |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3023 | if (ret) |
| 3024 | return ret; |
| 3025 | } |
| 3026 | } |
| 3027 | } |
| 3028 | |
| 3029 | return 0; |
| 3030 | } |
| 3031 | |
| 3032 | /** |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3033 | * cgroup_apply_control_disable - kill or hide csses according to control |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3034 | * @cgrp: root of the target subtree |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3035 | * |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3036 | * 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] | 3037 | * cgroup_ss_mask() and cgroup_visible_mask(). |
| 3038 | * |
| 3039 | * A css is hidden when the userland requests it to be disabled while other |
| 3040 | * subsystems are still depending on it. The css must not actively control |
| 3041 | * resources and be in the vanilla state if it's made visible again later. |
| 3042 | * Controllers which may be depended upon should provide ->css_reset() for |
| 3043 | * this purpose. |
| 3044 | */ |
| 3045 | static void cgroup_apply_control_disable(struct cgroup *cgrp) |
| 3046 | { |
| 3047 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3048 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3049 | struct cgroup_subsys *ss; |
| 3050 | int ssid; |
| 3051 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3052 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3053 | for_each_subsys(ss, ssid) { |
| 3054 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 3055 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3056 | WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); |
| 3057 | |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3058 | if (!css) |
| 3059 | continue; |
| 3060 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 3061 | if (css->parent && |
| 3062 | !(cgroup_ss_mask(dsct) & (1 << ss->id))) { |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3063 | kill_css(css); |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 3064 | } else if (!css_visible(css)) { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 3065 | css_clear_dir(css); |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3066 | if (ss->css_reset) |
| 3067 | ss->css_reset(css); |
| 3068 | } |
| 3069 | } |
| 3070 | } |
| 3071 | } |
| 3072 | |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3073 | /** |
| 3074 | * cgroup_apply_control - apply control mask updates to the subtree |
| 3075 | * @cgrp: root of the target subtree |
| 3076 | * |
| 3077 | * subsystems can be enabled and disabled in a subtree using the following |
| 3078 | * steps. |
| 3079 | * |
| 3080 | * 1. Call cgroup_save_control() to stash the current state. |
| 3081 | * 2. Update ->subtree_control masks in the subtree as desired. |
| 3082 | * 3. Call cgroup_apply_control() to apply the changes. |
| 3083 | * 4. Optionally perform other related operations. |
| 3084 | * 5. Call cgroup_finalize_control() to finish up. |
| 3085 | * |
| 3086 | * This function implements step 3 and propagates the mask changes |
| 3087 | * throughout @cgrp's subtree, updates csses accordingly and perform |
| 3088 | * process migrations. |
| 3089 | */ |
| 3090 | static int cgroup_apply_control(struct cgroup *cgrp) |
| 3091 | { |
| 3092 | int ret; |
| 3093 | |
| 3094 | cgroup_propagate_control(cgrp); |
| 3095 | |
| 3096 | ret = cgroup_apply_control_enable(cgrp); |
| 3097 | if (ret) |
| 3098 | return ret; |
| 3099 | |
| 3100 | /* |
Dennis Zhou | fc5a828 | 2018-12-05 12:10:36 -0500 | [diff] [blame] | 3101 | * At this point, cgroup_e_css_by_mask() results reflect the new csses |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3102 | * making the following cgroup_update_dfl_csses() properly update |
| 3103 | * css associations of all tasks in the subtree. |
| 3104 | */ |
| 3105 | ret = cgroup_update_dfl_csses(cgrp); |
| 3106 | if (ret) |
| 3107 | return ret; |
| 3108 | |
| 3109 | return 0; |
| 3110 | } |
| 3111 | |
| 3112 | /** |
| 3113 | * cgroup_finalize_control - finalize control mask update |
| 3114 | * @cgrp: root of the target subtree |
| 3115 | * @ret: the result of the update |
| 3116 | * |
| 3117 | * Finalize control mask update. See cgroup_apply_control() for more info. |
| 3118 | */ |
| 3119 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret) |
| 3120 | { |
| 3121 | if (ret) { |
| 3122 | cgroup_restore_control(cgrp); |
| 3123 | cgroup_propagate_control(cgrp); |
| 3124 | } |
| 3125 | |
| 3126 | cgroup_apply_control_disable(cgrp); |
| 3127 | } |
| 3128 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3129 | static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable) |
| 3130 | { |
| 3131 | u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask; |
| 3132 | |
| 3133 | /* if nothing is getting enabled, nothing to worry about */ |
| 3134 | if (!enable) |
| 3135 | return 0; |
| 3136 | |
| 3137 | /* can @cgrp host any resources? */ |
| 3138 | if (!cgroup_is_valid_domain(cgrp->dom_cgrp)) |
| 3139 | return -EOPNOTSUPP; |
| 3140 | |
| 3141 | /* mixables don't care */ |
| 3142 | if (cgroup_is_mixable(cgrp)) |
| 3143 | return 0; |
| 3144 | |
| 3145 | if (domain_enable) { |
| 3146 | /* can't enable domain controllers inside a thread subtree */ |
| 3147 | if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp)) |
| 3148 | return -EOPNOTSUPP; |
| 3149 | } else { |
| 3150 | /* |
| 3151 | * Threaded controllers can handle internal competitions |
| 3152 | * and are always allowed inside a (prospective) thread |
| 3153 | * subtree. |
| 3154 | */ |
| 3155 | if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp)) |
| 3156 | return 0; |
| 3157 | } |
| 3158 | |
| 3159 | /* |
| 3160 | * Controllers can't be enabled for a cgroup with tasks to avoid |
| 3161 | * child cgroups competing against tasks. |
| 3162 | */ |
| 3163 | if (cgroup_has_tasks(cgrp)) |
| 3164 | return -EBUSY; |
| 3165 | |
| 3166 | return 0; |
| 3167 | } |
| 3168 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3169 | /* change the enabled child controllers for a cgroup in the default hierarchy */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3170 | static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, |
| 3171 | char *buf, size_t nbytes, |
| 3172 | loff_t off) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3173 | { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 3174 | u16 enable = 0, disable = 0; |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3175 | struct cgroup *cgrp, *child; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3176 | struct cgroup_subsys *ss; |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3177 | char *tok; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3178 | int ssid, ret; |
| 3179 | |
| 3180 | /* |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 3181 | * Parse input - space separated list of subsystem names prefixed |
| 3182 | * with either + or -. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3183 | */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3184 | buf = strstrip(buf); |
| 3185 | while ((tok = strsep(&buf, " "))) { |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 3186 | if (tok[0] == '\0') |
| 3187 | continue; |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 3188 | do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) { |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 3189 | if (!cgroup_ssid_enabled(ssid) || |
| 3190 | strcmp(tok + 1, ss->name)) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3191 | continue; |
| 3192 | |
| 3193 | if (*tok == '+') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 3194 | enable |= 1 << ssid; |
| 3195 | disable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3196 | } else if (*tok == '-') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 3197 | disable |= 1 << ssid; |
| 3198 | enable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3199 | } else { |
| 3200 | return -EINVAL; |
| 3201 | } |
| 3202 | break; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 3203 | } while_each_subsys_mask(); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3204 | if (ssid == CGROUP_SUBSYS_COUNT) |
| 3205 | return -EINVAL; |
| 3206 | } |
| 3207 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3208 | cgrp = cgroup_kn_lock_live(of->kn, true); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3209 | if (!cgrp) |
| 3210 | return -ENODEV; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3211 | |
| 3212 | for_each_subsys(ss, ssid) { |
| 3213 | if (enable & (1 << ssid)) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3214 | if (cgrp->subtree_control & (1 << ssid)) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3215 | enable &= ~(1 << ssid); |
| 3216 | continue; |
| 3217 | } |
| 3218 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 3219 | if (!(cgroup_control(cgrp) & (1 << ssid))) { |
Tejun Heo | c29adf2 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3220 | ret = -ENOENT; |
| 3221 | goto out_unlock; |
| 3222 | } |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3223 | } else if (disable & (1 << ssid)) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3224 | if (!(cgrp->subtree_control & (1 << ssid))) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3225 | disable &= ~(1 << ssid); |
| 3226 | continue; |
| 3227 | } |
| 3228 | |
| 3229 | /* a child has it enabled? */ |
| 3230 | cgroup_for_each_live_child(child, cgrp) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3231 | if (child->subtree_control & (1 << ssid)) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3232 | ret = -EBUSY; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3233 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3234 | } |
| 3235 | } |
| 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | if (!enable && !disable) { |
| 3240 | ret = 0; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3241 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3242 | } |
| 3243 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3244 | ret = cgroup_vet_subtree_control_enable(cgrp, enable); |
| 3245 | if (ret) |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 3246 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3247 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3248 | /* save and update control masks and prepare csses */ |
| 3249 | cgroup_save_control(cgrp); |
Tejun Heo | c29adf2 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3250 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3251 | cgrp->subtree_control |= enable; |
| 3252 | cgrp->subtree_control &= ~disable; |
Tejun Heo | f63070d | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 3253 | |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3254 | ret = cgroup_apply_control(cgrp); |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3255 | cgroup_finalize_control(cgrp, ret); |
Tejun Heo | 3c74541 | 2017-07-23 08:14:15 -0400 | [diff] [blame] | 3256 | if (ret) |
| 3257 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3258 | |
| 3259 | kernfs_activate(cgrp->kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3260 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3261 | cgroup_kn_unlock(of->kn); |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3262 | return ret ?: nbytes; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3263 | } |
| 3264 | |
Tejun Heo | c705a00 | 2017-07-25 13:20:18 -0400 | [diff] [blame] | 3265 | /** |
| 3266 | * cgroup_enable_threaded - make @cgrp threaded |
| 3267 | * @cgrp: the target cgroup |
| 3268 | * |
| 3269 | * Called when "threaded" is written to the cgroup.type interface file and |
| 3270 | * tries to make @cgrp threaded and join the parent's resource domain. |
| 3271 | * This function is never called on the root cgroup as cgroup.type doesn't |
| 3272 | * exist on it. |
| 3273 | */ |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3274 | static int cgroup_enable_threaded(struct cgroup *cgrp) |
| 3275 | { |
| 3276 | struct cgroup *parent = cgroup_parent(cgrp); |
| 3277 | struct cgroup *dom_cgrp = parent->dom_cgrp; |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3278 | struct cgroup *dsct; |
| 3279 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3280 | int ret; |
| 3281 | |
| 3282 | lockdep_assert_held(&cgroup_mutex); |
| 3283 | |
| 3284 | /* noop if already threaded */ |
| 3285 | if (cgroup_is_threaded(cgrp)) |
| 3286 | return 0; |
| 3287 | |
Tejun Heo | d1897c9 | 2018-02-21 11:39:22 -0800 | [diff] [blame] | 3288 | /* |
| 3289 | * If @cgroup is populated or has domain controllers enabled, it |
| 3290 | * can't be switched. While the below cgroup_can_be_thread_root() |
| 3291 | * test can catch the same conditions, that's only when @parent is |
| 3292 | * not mixable, so let's check it explicitly. |
| 3293 | */ |
| 3294 | if (cgroup_is_populated(cgrp) || |
| 3295 | cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) |
| 3296 | return -EOPNOTSUPP; |
| 3297 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3298 | /* we're joining the parent's domain, ensure its validity */ |
| 3299 | if (!cgroup_is_valid_domain(dom_cgrp) || |
| 3300 | !cgroup_can_be_thread_root(dom_cgrp)) |
| 3301 | return -EOPNOTSUPP; |
| 3302 | |
| 3303 | /* |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3304 | * The following shouldn't cause actual migrations and should |
| 3305 | * always succeed. |
| 3306 | */ |
| 3307 | cgroup_save_control(cgrp); |
| 3308 | |
Tejun Heo | 479adb8 | 2018-10-04 13:28:08 -0700 | [diff] [blame] | 3309 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) |
| 3310 | if (dsct == cgrp || cgroup_is_threaded(dsct)) |
| 3311 | dsct->dom_cgrp = dom_cgrp; |
| 3312 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3313 | ret = cgroup_apply_control(cgrp); |
| 3314 | if (!ret) |
| 3315 | parent->nr_threaded_children++; |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3316 | |
| 3317 | cgroup_finalize_control(cgrp, ret); |
| 3318 | return ret; |
| 3319 | } |
| 3320 | |
| 3321 | static int cgroup_type_show(struct seq_file *seq, void *v) |
| 3322 | { |
| 3323 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3324 | |
| 3325 | if (cgroup_is_threaded(cgrp)) |
| 3326 | seq_puts(seq, "threaded\n"); |
| 3327 | else if (!cgroup_is_valid_domain(cgrp)) |
| 3328 | seq_puts(seq, "domain invalid\n"); |
| 3329 | else if (cgroup_is_thread_root(cgrp)) |
| 3330 | seq_puts(seq, "domain threaded\n"); |
| 3331 | else |
| 3332 | seq_puts(seq, "domain\n"); |
| 3333 | |
| 3334 | return 0; |
| 3335 | } |
| 3336 | |
| 3337 | static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf, |
| 3338 | size_t nbytes, loff_t off) |
| 3339 | { |
| 3340 | struct cgroup *cgrp; |
| 3341 | int ret; |
| 3342 | |
| 3343 | /* only switching to threaded mode is supported */ |
| 3344 | if (strcmp(strstrip(buf), "threaded")) |
| 3345 | return -EINVAL; |
| 3346 | |
| 3347 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3348 | if (!cgrp) |
| 3349 | return -ENOENT; |
| 3350 | |
| 3351 | /* threaded can only be enabled */ |
| 3352 | ret = cgroup_enable_threaded(cgrp); |
| 3353 | |
| 3354 | cgroup_kn_unlock(of->kn); |
| 3355 | return ret ?: nbytes; |
| 3356 | } |
| 3357 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3358 | static int cgroup_max_descendants_show(struct seq_file *seq, void *v) |
| 3359 | { |
| 3360 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3361 | int descendants = READ_ONCE(cgrp->max_descendants); |
| 3362 | |
| 3363 | if (descendants == INT_MAX) |
| 3364 | seq_puts(seq, "max\n"); |
| 3365 | else |
| 3366 | seq_printf(seq, "%d\n", descendants); |
| 3367 | |
| 3368 | return 0; |
| 3369 | } |
| 3370 | |
| 3371 | static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of, |
| 3372 | char *buf, size_t nbytes, loff_t off) |
| 3373 | { |
| 3374 | struct cgroup *cgrp; |
| 3375 | int descendants; |
| 3376 | ssize_t ret; |
| 3377 | |
| 3378 | buf = strstrip(buf); |
| 3379 | if (!strcmp(buf, "max")) { |
| 3380 | descendants = INT_MAX; |
| 3381 | } else { |
| 3382 | ret = kstrtoint(buf, 0, &descendants); |
| 3383 | if (ret) |
| 3384 | return ret; |
| 3385 | } |
| 3386 | |
Dan Carpenter | 696b98f | 2017-08-09 13:25:21 +0300 | [diff] [blame] | 3387 | if (descendants < 0) |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3388 | return -ERANGE; |
| 3389 | |
| 3390 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3391 | if (!cgrp) |
| 3392 | return -ENOENT; |
| 3393 | |
| 3394 | cgrp->max_descendants = descendants; |
| 3395 | |
| 3396 | cgroup_kn_unlock(of->kn); |
| 3397 | |
| 3398 | return nbytes; |
| 3399 | } |
| 3400 | |
| 3401 | static int cgroup_max_depth_show(struct seq_file *seq, void *v) |
| 3402 | { |
| 3403 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3404 | int depth = READ_ONCE(cgrp->max_depth); |
| 3405 | |
| 3406 | if (depth == INT_MAX) |
| 3407 | seq_puts(seq, "max\n"); |
| 3408 | else |
| 3409 | seq_printf(seq, "%d\n", depth); |
| 3410 | |
| 3411 | return 0; |
| 3412 | } |
| 3413 | |
| 3414 | static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of, |
| 3415 | char *buf, size_t nbytes, loff_t off) |
| 3416 | { |
| 3417 | struct cgroup *cgrp; |
| 3418 | ssize_t ret; |
| 3419 | int depth; |
| 3420 | |
| 3421 | buf = strstrip(buf); |
| 3422 | if (!strcmp(buf, "max")) { |
| 3423 | depth = INT_MAX; |
| 3424 | } else { |
| 3425 | ret = kstrtoint(buf, 0, &depth); |
| 3426 | if (ret) |
| 3427 | return ret; |
| 3428 | } |
| 3429 | |
Dan Carpenter | 696b98f | 2017-08-09 13:25:21 +0300 | [diff] [blame] | 3430 | if (depth < 0) |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3431 | return -ERANGE; |
| 3432 | |
| 3433 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3434 | if (!cgrp) |
| 3435 | return -ENOENT; |
| 3436 | |
| 3437 | cgrp->max_depth = depth; |
| 3438 | |
| 3439 | cgroup_kn_unlock(of->kn); |
| 3440 | |
| 3441 | return nbytes; |
| 3442 | } |
| 3443 | |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 3444 | static int cgroup_events_show(struct seq_file *seq, void *v) |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3445 | { |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 3446 | seq_printf(seq, "populated %d\n", |
Tejun Heo | 27bd4db | 2015-10-15 16:41:50 -0400 | [diff] [blame] | 3447 | cgroup_is_populated(seq_css(seq)->cgroup)); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3448 | return 0; |
| 3449 | } |
| 3450 | |
Tejun Heo | 3e48930 | 2017-08-11 05:49:01 -0700 | [diff] [blame] | 3451 | static int cgroup_stat_show(struct seq_file *seq, void *v) |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 3452 | { |
| 3453 | struct cgroup *cgroup = seq_css(seq)->cgroup; |
| 3454 | |
| 3455 | seq_printf(seq, "nr_descendants %d\n", |
| 3456 | cgroup->nr_descendants); |
| 3457 | seq_printf(seq, "nr_dying_descendants %d\n", |
| 3458 | cgroup->nr_dying_descendants); |
| 3459 | |
| 3460 | return 0; |
| 3461 | } |
| 3462 | |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3463 | static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq, |
| 3464 | struct cgroup *cgrp, int ssid) |
| 3465 | { |
| 3466 | struct cgroup_subsys *ss = cgroup_subsys[ssid]; |
| 3467 | struct cgroup_subsys_state *css; |
| 3468 | int ret; |
| 3469 | |
| 3470 | if (!ss->css_extra_stat_show) |
| 3471 | return 0; |
| 3472 | |
| 3473 | css = cgroup_tryget_css(cgrp, ss); |
| 3474 | if (!css) |
| 3475 | return 0; |
| 3476 | |
| 3477 | ret = ss->css_extra_stat_show(seq, css); |
| 3478 | css_put(css); |
| 3479 | return ret; |
| 3480 | } |
| 3481 | |
| 3482 | static int cpu_stat_show(struct seq_file *seq, void *v) |
| 3483 | { |
Tejun Heo | c3ba132 | 2017-10-30 08:13:14 -0700 | [diff] [blame] | 3484 | struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3485 | int ret = 0; |
| 3486 | |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3487 | cgroup_base_stat_cputime_show(seq); |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3488 | #ifdef CONFIG_CGROUP_SCHED |
| 3489 | ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id); |
| 3490 | #endif |
| 3491 | return ret; |
| 3492 | } |
| 3493 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 3494 | #ifdef CONFIG_PSI |
| 3495 | static int cgroup_io_pressure_show(struct seq_file *seq, void *v) |
| 3496 | { |
| 3497 | return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_IO); |
| 3498 | } |
| 3499 | static int cgroup_memory_pressure_show(struct seq_file *seq, void *v) |
| 3500 | { |
| 3501 | return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_MEM); |
| 3502 | } |
| 3503 | static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v) |
| 3504 | { |
| 3505 | return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_CPU); |
| 3506 | } |
| 3507 | #endif |
| 3508 | |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3509 | static int cgroup_file_open(struct kernfs_open_file *of) |
| 3510 | { |
| 3511 | struct cftype *cft = of->kn->priv; |
| 3512 | |
| 3513 | if (cft->open) |
| 3514 | return cft->open(of); |
| 3515 | return 0; |
| 3516 | } |
| 3517 | |
| 3518 | static void cgroup_file_release(struct kernfs_open_file *of) |
| 3519 | { |
| 3520 | struct cftype *cft = of->kn->priv; |
| 3521 | |
| 3522 | if (cft->release) |
| 3523 | cft->release(of); |
| 3524 | } |
| 3525 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3526 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 3527 | size_t nbytes, loff_t off) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3528 | { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 3529 | struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3530 | struct cgroup *cgrp = of->kn->parent->priv; |
| 3531 | struct cftype *cft = of->kn->priv; |
| 3532 | struct cgroup_subsys_state *css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3533 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3534 | |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 3535 | /* |
| 3536 | * If namespaces are delegation boundaries, disallow writes to |
| 3537 | * files in an non-init namespace root from inside the namespace |
| 3538 | * except for the files explicitly marked delegatable - |
| 3539 | * cgroup.procs and cgroup.subtree_control. |
| 3540 | */ |
| 3541 | if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) && |
| 3542 | !(cft->flags & CFTYPE_NS_DELEGATABLE) && |
| 3543 | ns != &init_cgroup_ns && ns->root_cset->dfl_cgrp == cgrp) |
| 3544 | return -EPERM; |
| 3545 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3546 | if (cft->write) |
| 3547 | return cft->write(of, buf, nbytes, off); |
| 3548 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3549 | /* |
| 3550 | * kernfs guarantees that a file isn't deleted with operations in |
| 3551 | * flight, which means that the matching css is and stays alive and |
| 3552 | * doesn't need to be pinned. The RCU locking is not necessary |
| 3553 | * either. It's just for the convenience of using cgroup_css(). |
| 3554 | */ |
| 3555 | rcu_read_lock(); |
| 3556 | css = cgroup_css(cgrp, cft->ss); |
| 3557 | rcu_read_unlock(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3558 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3559 | if (cft->write_u64) { |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3560 | unsigned long long v; |
| 3561 | ret = kstrtoull(buf, 0, &v); |
| 3562 | if (!ret) |
| 3563 | ret = cft->write_u64(css, cft, v); |
| 3564 | } else if (cft->write_s64) { |
| 3565 | long long v; |
| 3566 | ret = kstrtoll(buf, 0, &v); |
| 3567 | if (!ret) |
| 3568 | ret = cft->write_s64(css, cft, v); |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3569 | } else { |
| 3570 | ret = -EINVAL; |
| 3571 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3572 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3573 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3574 | } |
| 3575 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3576 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3577 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3578 | return seq_cft(seq)->seq_start(seq, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 3582 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3583 | return seq_cft(seq)->seq_next(seq, v, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 3587 | { |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3588 | if (seq_cft(seq)->seq_stop) |
| 3589 | seq_cft(seq)->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3590 | } |
| 3591 | |
| 3592 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 3593 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3594 | struct cftype *cft = seq_cft(m); |
| 3595 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 3596 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3597 | if (cft->seq_show) |
| 3598 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3599 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3600 | if (cft->read_u64) |
| 3601 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 3602 | else if (cft->read_s64) |
| 3603 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 3604 | else |
| 3605 | return -EINVAL; |
| 3606 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3607 | } |
| 3608 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3609 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 3610 | .atomic_write_len = PAGE_SIZE, |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3611 | .open = cgroup_file_open, |
| 3612 | .release = cgroup_file_release, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3613 | .write = cgroup_file_write, |
| 3614 | .seq_show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3615 | }; |
| 3616 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3617 | static struct kernfs_ops cgroup_kf_ops = { |
| 3618 | .atomic_write_len = PAGE_SIZE, |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3619 | .open = cgroup_file_open, |
| 3620 | .release = cgroup_file_release, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3621 | .write = cgroup_file_write, |
| 3622 | .seq_start = cgroup_seqfile_start, |
| 3623 | .seq_next = cgroup_seqfile_next, |
| 3624 | .seq_stop = cgroup_seqfile_stop, |
| 3625 | .seq_show = cgroup_seqfile_show, |
| 3626 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3627 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3628 | /* set uid and gid of cgroup dirs and files to that of the creator */ |
| 3629 | static int cgroup_kn_set_ugid(struct kernfs_node *kn) |
| 3630 | { |
| 3631 | struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, |
| 3632 | .ia_uid = current_fsuid(), |
| 3633 | .ia_gid = current_fsgid(), }; |
| 3634 | |
| 3635 | if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && |
| 3636 | gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) |
| 3637 | return 0; |
| 3638 | |
| 3639 | return kernfs_setattr(kn, &iattr); |
| 3640 | } |
| 3641 | |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3642 | static void cgroup_file_notify_timer(struct timer_list *timer) |
| 3643 | { |
| 3644 | cgroup_file_notify(container_of(timer, struct cgroup_file, |
| 3645 | notify_timer)); |
| 3646 | } |
| 3647 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3648 | static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, |
| 3649 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3650 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3651 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3652 | struct kernfs_node *kn; |
| 3653 | struct lock_class_key *key = NULL; |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3654 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3655 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3656 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 3657 | key = &cft->lockdep_key; |
| 3658 | #endif |
| 3659 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
Dmitry Torokhov | 488dee9 | 2018-07-20 21:56:47 +0000 | [diff] [blame] | 3660 | cgroup_file_mode(cft), |
| 3661 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, |
| 3662 | 0, cft->kf_ops, cft, |
Tejun Heo | dfeb0750 | 2015-02-13 14:36:31 -0800 | [diff] [blame] | 3663 | NULL, key); |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3664 | if (IS_ERR(kn)) |
| 3665 | return PTR_ERR(kn); |
| 3666 | |
| 3667 | ret = cgroup_kn_set_ugid(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3668 | if (ret) { |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3669 | kernfs_remove(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3670 | return ret; |
| 3671 | } |
| 3672 | |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3673 | if (cft->file_offset) { |
| 3674 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 3675 | |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3676 | timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0); |
| 3677 | |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3678 | spin_lock_irq(&cgroup_file_kn_lock); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3679 | cfile->kn = kn; |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3680 | spin_unlock_irq(&cgroup_file_kn_lock); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3681 | } |
| 3682 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3683 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3684 | } |
| 3685 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3686 | /** |
| 3687 | * cgroup_addrm_files - add or remove files to a cgroup directory |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3688 | * @css: the target css |
| 3689 | * @cgrp: the target cgroup (usually css->cgroup) |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3690 | * @cfts: array of cftypes to be added |
| 3691 | * @is_add: whether to add or remove |
| 3692 | * |
| 3693 | * 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] | 3694 | * For removals, this function never fails. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3695 | */ |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3696 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 3697 | struct cgroup *cgrp, struct cftype cfts[], |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3698 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3699 | { |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3700 | struct cftype *cft, *cft_end = NULL; |
Tejun Heo | b598dde | 2016-02-22 22:25:45 -0500 | [diff] [blame] | 3701 | int ret = 0; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3702 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3703 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3704 | |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3705 | restart: |
| 3706 | for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3707 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3708 | if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) |
Tejun Heo | 8cbbf2c | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 3709 | continue; |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3710 | if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp)) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3711 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3712 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3713 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3714 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3715 | continue; |
Waiman Long | 5cf8114 | 2018-11-08 10:08:46 -0500 | [diff] [blame] | 3716 | if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug) |
| 3717 | continue; |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 3718 | if (is_add) { |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3719 | ret = cgroup_add_file(css, cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3720 | if (ret) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 3721 | pr_warn("%s: failed to add %s, err=%d\n", |
| 3722 | __func__, cft->name, ret); |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3723 | cft_end = cft; |
| 3724 | is_add = false; |
| 3725 | goto restart; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3726 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 3727 | } else { |
| 3728 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3729 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3730 | } |
Tejun Heo | b598dde | 2016-02-22 22:25:45 -0500 | [diff] [blame] | 3731 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3732 | } |
| 3733 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3734 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3735 | { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3736 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 3737 | struct cgroup *root = &ss->root->cgrp; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3738 | struct cgroup_subsys_state *css; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3739 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3740 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3741 | lockdep_assert_held(&cgroup_mutex); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3742 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3743 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3744 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3745 | struct cgroup *cgrp = css->cgroup; |
| 3746 | |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 3747 | if (!(css->flags & CSS_VISIBLE)) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3748 | continue; |
| 3749 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3750 | ret = cgroup_addrm_files(css, cgrp, cfts, is_add); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3751 | if (ret) |
| 3752 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3753 | } |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3754 | |
| 3755 | if (is_add && !ret) |
| 3756 | kernfs_activate(root->kn); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3757 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3758 | } |
| 3759 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3760 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 3761 | { |
| 3762 | struct cftype *cft; |
| 3763 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3764 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 3765 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 3766 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 3767 | kfree(cft->kf_ops); |
| 3768 | cft->kf_ops = NULL; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3769 | cft->ss = NULL; |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3770 | |
| 3771 | /* revert flags set by cgroup core while adding @cfts */ |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3772 | cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3773 | } |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3774 | } |
| 3775 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3776 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3777 | { |
| 3778 | struct cftype *cft; |
| 3779 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3780 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 3781 | struct kernfs_ops *kf_ops; |
| 3782 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3783 | WARN_ON(cft->ss || cft->kf_ops); |
| 3784 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3785 | if (cft->seq_start) |
| 3786 | kf_ops = &cgroup_kf_ops; |
| 3787 | else |
| 3788 | kf_ops = &cgroup_kf_single_ops; |
| 3789 | |
| 3790 | /* |
| 3791 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 3792 | * make a copy of kf_ops to set its atomic_write_len. |
| 3793 | */ |
| 3794 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 3795 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 3796 | if (!kf_ops) { |
| 3797 | cgroup_exit_cftypes(cfts); |
| 3798 | return -ENOMEM; |
| 3799 | } |
| 3800 | kf_ops->atomic_write_len = cft->max_write_len; |
| 3801 | } |
| 3802 | |
| 3803 | cft->kf_ops = kf_ops; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3804 | cft->ss = ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | return 0; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3808 | } |
| 3809 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3810 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 3811 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3812 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3813 | |
| 3814 | if (!cfts || !cfts[0].ss) |
| 3815 | return -ENOENT; |
| 3816 | |
| 3817 | list_del(&cfts->node); |
| 3818 | cgroup_apply_cftypes(cfts, false); |
| 3819 | cgroup_exit_cftypes(cfts); |
| 3820 | return 0; |
| 3821 | } |
| 3822 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3823 | /** |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3824 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 3825 | * @cfts: zero-length name terminated array of cftypes |
| 3826 | * |
| 3827 | * Unregister @cfts. Files described by @cfts are removed from all |
| 3828 | * existing cgroups and all future cgroups won't have them either. This |
| 3829 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 3830 | * |
| 3831 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 3832 | * registered. |
| 3833 | */ |
| 3834 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 3835 | { |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3836 | int ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3837 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3838 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3839 | ret = cgroup_rm_cftypes_locked(cfts); |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3840 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3841 | return ret; |
| 3842 | } |
| 3843 | |
| 3844 | /** |
| 3845 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 3846 | * @ss: target cgroup subsystem |
| 3847 | * @cfts: zero-length name terminated array of cftypes |
| 3848 | * |
| 3849 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 3850 | * existing cgroups to which @ss is attached and all future cgroups will |
| 3851 | * have them too. This function can be called anytime whether @ss is |
| 3852 | * attached or not. |
| 3853 | * |
| 3854 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 3855 | * function currently returns 0 as long as @cfts registration is successful |
| 3856 | * even if some file creation attempts on existing cgroups fail. |
| 3857 | */ |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 3858 | static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3859 | { |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3860 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3861 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 3862 | if (!cgroup_ssid_enabled(ss->id)) |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 3863 | return 0; |
| 3864 | |
Li Zefan | dc5736e | 2014-02-17 10:41:50 +0800 | [diff] [blame] | 3865 | if (!cfts || cfts[0].name[0] == '\0') |
| 3866 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3867 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3868 | ret = cgroup_init_cftypes(ss, cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3869 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3870 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3871 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3872 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3873 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3874 | list_add_tail(&cfts->node, &ss->cfts); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3875 | ret = cgroup_apply_cftypes(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3876 | if (ret) |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3877 | cgroup_rm_cftypes_locked(cfts); |
| 3878 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3879 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3880 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3881 | } |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3882 | |
| 3883 | /** |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3884 | * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy |
| 3885 | * @ss: target cgroup subsystem |
| 3886 | * @cfts: zero-length name terminated array of cftypes |
| 3887 | * |
| 3888 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 3889 | * the default hierarchy. |
| 3890 | */ |
| 3891 | int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 3892 | { |
| 3893 | struct cftype *cft; |
| 3894 | |
| 3895 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3896 | cft->flags |= __CFTYPE_ONLY_ON_DFL; |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3897 | return cgroup_add_cftypes(ss, cfts); |
| 3898 | } |
| 3899 | |
| 3900 | /** |
| 3901 | * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies |
| 3902 | * @ss: target cgroup subsystem |
| 3903 | * @cfts: zero-length name terminated array of cftypes |
| 3904 | * |
| 3905 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 3906 | * the legacy hierarchies. |
| 3907 | */ |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 3908 | int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 3909 | { |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3910 | struct cftype *cft; |
| 3911 | |
Tejun Heo | e4b7037 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 3912 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
| 3913 | cft->flags |= __CFTYPE_NOT_ON_DFL; |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 3914 | return cgroup_add_cftypes(ss, cfts); |
| 3915 | } |
| 3916 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3917 | /** |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3918 | * cgroup_file_notify - generate a file modified event for a cgroup_file |
| 3919 | * @cfile: target cgroup_file |
| 3920 | * |
| 3921 | * @cfile must have been obtained by setting cftype->file_offset. |
| 3922 | */ |
| 3923 | void cgroup_file_notify(struct cgroup_file *cfile) |
| 3924 | { |
| 3925 | unsigned long flags; |
| 3926 | |
| 3927 | spin_lock_irqsave(&cgroup_file_kn_lock, flags); |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3928 | if (cfile->kn) { |
| 3929 | unsigned long last = cfile->notified_at; |
| 3930 | unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV; |
| 3931 | |
| 3932 | if (time_in_range(jiffies, last, next)) { |
| 3933 | timer_reduce(&cfile->notify_timer, next); |
| 3934 | } else { |
| 3935 | kernfs_notify(cfile->kn); |
| 3936 | cfile->notified_at = jiffies; |
| 3937 | } |
| 3938 | } |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3939 | spin_unlock_irqrestore(&cgroup_file_kn_lock, flags); |
| 3940 | } |
| 3941 | |
| 3942 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3943 | * css_next_child - find the next child of a given css |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3944 | * @pos: the current position (%NULL to initiate traversal) |
| 3945 | * @parent: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3946 | * |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3947 | * This function returns the next child of @parent and should be called |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3948 | * under either cgroup_mutex or RCU read lock. The only requirement is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3949 | * that @parent and @pos are accessible. The next sibling is guaranteed to |
| 3950 | * be returned regardless of their states. |
| 3951 | * |
| 3952 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 3953 | * css which finished ->css_online() is guaranteed to be visible in the |
| 3954 | * future iterations and will stay visible until the last reference is put. |
| 3955 | * A css which hasn't finished ->css_online() or already finished |
| 3956 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 3957 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3958 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3959 | struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, |
| 3960 | struct cgroup_subsys_state *parent) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3961 | { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3962 | struct cgroup_subsys_state *next; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3963 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3964 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3965 | |
| 3966 | /* |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 3967 | * @pos could already have been unlinked from the sibling list. |
| 3968 | * Once a cgroup is removed, its ->sibling.next is no longer |
| 3969 | * updated when its next sibling changes. CSS_RELEASED is set when |
| 3970 | * @pos is taken off list, at which time its next pointer is valid, |
| 3971 | * and, as releases are serialized, the one pointed to by the next |
| 3972 | * pointer is guaranteed to not have started release yet. This |
| 3973 | * implies that if we observe !CSS_RELEASED on @pos in this RCU |
| 3974 | * critical section, the one pointed to by its next pointer is |
| 3975 | * guaranteed to not have finished its RCU grace period even if we |
| 3976 | * have dropped rcu_read_lock() inbetween iterations. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3977 | * |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 3978 | * If @pos has CSS_RELEASED set, its next pointer can't be |
| 3979 | * dereferenced; however, as each css is given a monotonically |
| 3980 | * increasing unique serial number and always appended to the |
| 3981 | * sibling list, the next one can be found by walking the parent's |
| 3982 | * children until the first css with higher serial number than |
| 3983 | * @pos's. While this path can be slower, it happens iff iteration |
| 3984 | * races against release and the race window is very small. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3985 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3986 | if (!pos) { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3987 | next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling); |
| 3988 | } else if (likely(!(pos->flags & CSS_RELEASED))) { |
| 3989 | next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3990 | } else { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3991 | list_for_each_entry_rcu(next, &parent->children, sibling) |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3992 | if (next->serial_nr > pos->serial_nr) |
| 3993 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3994 | } |
| 3995 | |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3996 | /* |
| 3997 | * @next, if not pointing to the head, can be dereferenced and is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3998 | * the next sibling. |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3999 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4000 | if (&next->sibling != &parent->children) |
| 4001 | return next; |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4002 | return NULL; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4003 | } |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4004 | |
| 4005 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4006 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4007 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4008 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4009 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4010 | * 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] | 4011 | * to visit for pre-order traversal of @root's descendants. @root is |
| 4012 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4013 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4014 | * While this function requires cgroup_mutex or RCU read locking, it |
| 4015 | * doesn't require the whole traversal to be contained in a single critical |
| 4016 | * section. This function will return the correct next descendant as long |
| 4017 | * 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] | 4018 | * |
| 4019 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 4020 | * css which finished ->css_online() is guaranteed to be visible in the |
| 4021 | * future iterations and will stay visible until the last reference is put. |
| 4022 | * A css which hasn't finished ->css_online() or already finished |
| 4023 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 4024 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4025 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4026 | struct cgroup_subsys_state * |
| 4027 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 4028 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4029 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4030 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4031 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4032 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4033 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4034 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 4035 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4036 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4037 | |
| 4038 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4039 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4040 | if (next) |
| 4041 | return next; |
| 4042 | |
| 4043 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4044 | while (pos != root) { |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4045 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4046 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4047 | return next; |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4048 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 4049 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4050 | |
| 4051 | return NULL; |
| 4052 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4053 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4054 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4055 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 4056 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4057 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4058 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 4059 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4060 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4061 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4062 | * While this function requires cgroup_mutex or RCU read locking, it |
| 4063 | * doesn't require the whole traversal to be contained in a single critical |
| 4064 | * section. This function will return the correct rightmost descendant as |
| 4065 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4066 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4067 | struct cgroup_subsys_state * |
| 4068 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4069 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4070 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4071 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4072 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4073 | |
| 4074 | do { |
| 4075 | last = pos; |
| 4076 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 4077 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4078 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4079 | pos = tmp; |
| 4080 | } while (pos); |
| 4081 | |
| 4082 | return last; |
| 4083 | } |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 4084 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4085 | static struct cgroup_subsys_state * |
| 4086 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4087 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4088 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4089 | |
| 4090 | do { |
| 4091 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4092 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4093 | } while (pos); |
| 4094 | |
| 4095 | return last; |
| 4096 | } |
| 4097 | |
| 4098 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4099 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4100 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4101 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4102 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4103 | * 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] | 4104 | * to visit for post-order traversal of @root's descendants. @root is |
| 4105 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4106 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4107 | * While this function requires cgroup_mutex or RCU read locking, it |
| 4108 | * doesn't require the whole traversal to be contained in a single critical |
| 4109 | * section. This function will return the correct next descendant as long |
| 4110 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 4111 | * @cgroup. |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4112 | * |
| 4113 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 4114 | * css which finished ->css_online() is guaranteed to be visible in the |
| 4115 | * future iterations and will stay visible until the last reference is put. |
| 4116 | * A css which hasn't finished ->css_online() or already finished |
| 4117 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 4118 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4119 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4120 | struct cgroup_subsys_state * |
| 4121 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 4122 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4123 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4124 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4125 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4126 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4127 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 4128 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 4129 | if (!pos) |
| 4130 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4131 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4132 | /* if we visited @root, we're done */ |
| 4133 | if (pos == root) |
| 4134 | return NULL; |
| 4135 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4136 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4137 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4138 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4139 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4140 | |
| 4141 | /* no sibling left, visit parent */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4142 | return pos->parent; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4143 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4144 | |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4145 | /** |
| 4146 | * css_has_online_children - does a css have online children |
| 4147 | * @css: the target css |
| 4148 | * |
| 4149 | * Returns %true if @css has any online children; otherwise, %false. This |
| 4150 | * function can be called from any context but the caller is responsible |
| 4151 | * for synchronizing against on/offlining as necessary. |
| 4152 | */ |
| 4153 | bool css_has_online_children(struct cgroup_subsys_state *css) |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4154 | { |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4155 | struct cgroup_subsys_state *child; |
| 4156 | bool ret = false; |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4157 | |
| 4158 | rcu_read_lock(); |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4159 | css_for_each_child(child, css) { |
Li Zefan | 99bae5f | 2014-06-12 14:31:31 +0800 | [diff] [blame] | 4160 | if (child->flags & CSS_ONLINE) { |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4161 | ret = true; |
| 4162 | break; |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4163 | } |
| 4164 | } |
| 4165 | rcu_read_unlock(); |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4166 | return ret; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 4167 | } |
| 4168 | |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4169 | static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it) |
| 4170 | { |
| 4171 | struct list_head *l; |
| 4172 | struct cgrp_cset_link *link; |
| 4173 | struct css_set *cset; |
| 4174 | |
| 4175 | lockdep_assert_held(&css_set_lock); |
| 4176 | |
| 4177 | /* find the next threaded cset */ |
| 4178 | if (it->tcset_pos) { |
| 4179 | l = it->tcset_pos->next; |
| 4180 | |
| 4181 | if (l != it->tcset_head) { |
| 4182 | it->tcset_pos = l; |
| 4183 | return container_of(l, struct css_set, |
| 4184 | threaded_csets_node); |
| 4185 | } |
| 4186 | |
| 4187 | it->tcset_pos = NULL; |
| 4188 | } |
| 4189 | |
| 4190 | /* find the next cset */ |
| 4191 | l = it->cset_pos; |
| 4192 | l = l->next; |
| 4193 | if (l == it->cset_head) { |
| 4194 | it->cset_pos = NULL; |
| 4195 | return NULL; |
| 4196 | } |
| 4197 | |
| 4198 | if (it->ss) { |
| 4199 | cset = container_of(l, struct css_set, e_cset_node[it->ss->id]); |
| 4200 | } else { |
| 4201 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 4202 | cset = link->cset; |
| 4203 | } |
| 4204 | |
| 4205 | it->cset_pos = l; |
| 4206 | |
| 4207 | /* initialize threaded css_set walking */ |
| 4208 | if (it->flags & CSS_TASK_ITER_THREADED) { |
| 4209 | if (it->cur_dcset) |
| 4210 | put_css_set_locked(it->cur_dcset); |
| 4211 | it->cur_dcset = cset; |
| 4212 | get_css_set(cset); |
| 4213 | |
| 4214 | it->tcset_head = &cset->threaded_csets; |
| 4215 | it->tcset_pos = &cset->threaded_csets; |
| 4216 | } |
| 4217 | |
| 4218 | return cset; |
| 4219 | } |
| 4220 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4221 | /** |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4222 | * 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] | 4223 | * @it: the iterator to advance |
| 4224 | * |
| 4225 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4226 | */ |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4227 | 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] | 4228 | { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4229 | struct css_set *cset; |
| 4230 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 4231 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4232 | |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4233 | /* Advance to the next non-empty css_set */ |
| 4234 | do { |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4235 | cset = css_task_iter_next_css_set(it); |
| 4236 | if (!cset) { |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4237 | it->task_pos = NULL; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4238 | return; |
| 4239 | } |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 4240 | } while (!css_set_populated(cset)); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4241 | |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4242 | if (!list_empty(&cset->tasks)) |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4243 | it->task_pos = cset->tasks.next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4244 | else |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4245 | it->task_pos = cset->mg_tasks.next; |
| 4246 | |
| 4247 | it->tasks_head = &cset->tasks; |
| 4248 | it->mg_tasks_head = &cset->mg_tasks; |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4249 | |
| 4250 | /* |
| 4251 | * We don't keep css_sets locked across iteration steps and thus |
| 4252 | * need to take steps to ensure that iteration can be resumed after |
| 4253 | * the lock is re-acquired. Iteration is performed at two levels - |
| 4254 | * css_sets and tasks in them. |
| 4255 | * |
| 4256 | * Once created, a css_set never leaves its cgroup lists, so a |
| 4257 | * pinned css_set is guaranteed to stay put and we can resume |
| 4258 | * iteration afterwards. |
| 4259 | * |
| 4260 | * Tasks may leave @cset across iteration steps. This is resolved |
| 4261 | * by registering each iterator with the css_set currently being |
| 4262 | * walked and making css_set_move_task() advance iterators whose |
| 4263 | * next task is leaving. |
| 4264 | */ |
| 4265 | if (it->cur_cset) { |
| 4266 | list_del(&it->iters_node); |
| 4267 | put_css_set_locked(it->cur_cset); |
| 4268 | } |
| 4269 | get_css_set(cset); |
| 4270 | it->cur_cset = cset; |
| 4271 | list_add(&it->iters_node, &cset->task_iters); |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4272 | } |
| 4273 | |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4274 | static void css_task_iter_advance(struct css_task_iter *it) |
| 4275 | { |
Tejun Heo | 74d0833 | 2017-12-20 07:09:19 -0800 | [diff] [blame] | 4276 | struct list_head *next; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4277 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 4278 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4279 | repeat: |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4280 | if (it->task_pos) { |
| 4281 | /* |
| 4282 | * Advance iterator to find next entry. cset->tasks is |
| 4283 | * consumed first and then ->mg_tasks. After ->mg_tasks, |
| 4284 | * we move onto the next cset. |
| 4285 | */ |
| 4286 | next = it->task_pos->next; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4287 | |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4288 | if (next == it->tasks_head) |
| 4289 | next = it->mg_tasks_head->next; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4290 | |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4291 | if (next == it->mg_tasks_head) |
| 4292 | css_task_iter_advance_css_set(it); |
| 4293 | else |
| 4294 | it->task_pos = next; |
| 4295 | } else { |
| 4296 | /* called from start, proceed to the first cset */ |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4297 | css_task_iter_advance_css_set(it); |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4298 | } |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4299 | |
| 4300 | /* if PROCS, skip over tasks which aren't group leaders */ |
| 4301 | if ((it->flags & CSS_TASK_ITER_PROCS) && it->task_pos && |
| 4302 | !thread_group_leader(list_entry(it->task_pos, struct task_struct, |
| 4303 | cg_list))) |
| 4304 | goto repeat; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4305 | } |
| 4306 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4307 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4308 | * css_task_iter_start - initiate task iteration |
| 4309 | * @css: the css to walk tasks of |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4310 | * @flags: CSS_TASK_ITER_* flags |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4311 | * @it: the task iterator to use |
| 4312 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4313 | * Initiate iteration through the tasks of @css. The caller can call |
| 4314 | * css_task_iter_next() to walk through the tasks until the function |
| 4315 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 4316 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4317 | */ |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4318 | 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] | 4319 | struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4320 | { |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 4321 | /* no one should try to iterate before mounting cgroups */ |
| 4322 | WARN_ON_ONCE(!use_task_css_set_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4323 | |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4324 | memset(it, 0, sizeof(*it)); |
| 4325 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4326 | spin_lock_irq(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4327 | |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4328 | it->ss = css->ss; |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4329 | it->flags = flags; |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4330 | |
| 4331 | if (it->ss) |
| 4332 | it->cset_pos = &css->cgroup->e_csets[css->ss->id]; |
| 4333 | else |
| 4334 | it->cset_pos = &css->cgroup->cset_links; |
| 4335 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4336 | it->cset_head = it->cset_pos; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4337 | |
Tejun Heo | e9d81a1 | 2018-11-08 12:15:15 -0800 | [diff] [blame] | 4338 | css_task_iter_advance(it); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4339 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4340 | spin_unlock_irq(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4341 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4342 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4343 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4344 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4345 | * @it: the task iterator being iterated |
| 4346 | * |
| 4347 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4348 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 4349 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4350 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4351 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4352 | { |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4353 | if (it->cur_task) { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4354 | put_task_struct(it->cur_task); |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4355 | it->cur_task = NULL; |
| 4356 | } |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4357 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4358 | spin_lock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4359 | |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4360 | if (it->task_pos) { |
| 4361 | it->cur_task = list_entry(it->task_pos, struct task_struct, |
| 4362 | cg_list); |
| 4363 | get_task_struct(it->cur_task); |
| 4364 | css_task_iter_advance(it); |
| 4365 | } |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4366 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4367 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4368 | |
| 4369 | return it->cur_task; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4370 | } |
| 4371 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4372 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4373 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4374 | * @it: the task iterator to finish |
| 4375 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4376 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4377 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4378 | void css_task_iter_end(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4379 | { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4380 | if (it->cur_cset) { |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4381 | spin_lock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4382 | list_del(&it->iters_node); |
| 4383 | put_css_set_locked(it->cur_cset); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4384 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4385 | } |
| 4386 | |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4387 | if (it->cur_dcset) |
| 4388 | put_css_set(it->cur_dcset); |
| 4389 | |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4390 | if (it->cur_task) |
| 4391 | put_task_struct(it->cur_task); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4392 | } |
| 4393 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4394 | static void cgroup_procs_release(struct kernfs_open_file *of) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4395 | { |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4396 | if (of->priv) { |
| 4397 | css_task_iter_end(of->priv); |
| 4398 | kfree(of->priv); |
| 4399 | } |
| 4400 | } |
| 4401 | |
| 4402 | static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos) |
| 4403 | { |
| 4404 | struct kernfs_open_file *of = s->private; |
| 4405 | struct css_task_iter *it = of->priv; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 4406 | |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4407 | return css_task_iter_next(it); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4408 | } |
| 4409 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4410 | static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos, |
| 4411 | unsigned int iter_flags) |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 4412 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4413 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4414 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4415 | struct css_task_iter *it = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4416 | |
| 4417 | /* |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4418 | * When a seq_file is seeked, it's always traversed sequentially |
| 4419 | * from position 0, so we can simply keep iterating on !0 *pos. |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4420 | */ |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4421 | if (!it) { |
| 4422 | if (WARN_ON_ONCE((*pos)++)) |
| 4423 | return ERR_PTR(-EINVAL); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4424 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4425 | it = kzalloc(sizeof(*it), GFP_KERNEL); |
| 4426 | if (!it) |
| 4427 | return ERR_PTR(-ENOMEM); |
| 4428 | of->priv = it; |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4429 | css_task_iter_start(&cgrp->self, iter_flags, it); |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4430 | } else if (!(*pos)++) { |
| 4431 | css_task_iter_end(it); |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4432 | css_task_iter_start(&cgrp->self, iter_flags, it); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4433 | } |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4434 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4435 | return cgroup_procs_next(s, NULL, NULL); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4436 | } |
| 4437 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4438 | static void *cgroup_procs_start(struct seq_file *s, loff_t *pos) |
| 4439 | { |
| 4440 | struct cgroup *cgrp = seq_css(s)->cgroup; |
| 4441 | |
| 4442 | /* |
| 4443 | * All processes of a threaded subtree belong to the domain cgroup |
| 4444 | * of the subtree. Only threads can be distributed across the |
| 4445 | * subtree. Reject reads on cgroup.procs in the subtree proper. |
| 4446 | * They're always empty anyway. |
| 4447 | */ |
| 4448 | if (cgroup_is_threaded(cgrp)) |
| 4449 | return ERR_PTR(-EOPNOTSUPP); |
| 4450 | |
| 4451 | return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS | |
| 4452 | CSS_TASK_ITER_THREADED); |
| 4453 | } |
| 4454 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4455 | static int cgroup_procs_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4456 | { |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4457 | seq_printf(s, "%d\n", task_pid_vnr(v)); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4458 | return 0; |
| 4459 | } |
| 4460 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4461 | static int cgroup_procs_write_permission(struct cgroup *src_cgrp, |
| 4462 | struct cgroup *dst_cgrp, |
| 4463 | struct super_block *sb) |
| 4464 | { |
| 4465 | struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; |
| 4466 | struct cgroup *com_cgrp = src_cgrp; |
| 4467 | struct inode *inode; |
| 4468 | int ret; |
| 4469 | |
| 4470 | lockdep_assert_held(&cgroup_mutex); |
| 4471 | |
| 4472 | /* find the common ancestor */ |
| 4473 | while (!cgroup_is_descendant(dst_cgrp, com_cgrp)) |
| 4474 | com_cgrp = cgroup_parent(com_cgrp); |
| 4475 | |
| 4476 | /* %current should be authorized to migrate to the common ancestor */ |
| 4477 | inode = kernfs_get_inode(sb, com_cgrp->procs_file.kn); |
| 4478 | if (!inode) |
| 4479 | return -ENOMEM; |
| 4480 | |
| 4481 | ret = inode_permission(inode, MAY_WRITE); |
| 4482 | iput(inode); |
| 4483 | if (ret) |
| 4484 | return ret; |
| 4485 | |
| 4486 | /* |
| 4487 | * If namespaces are delegation boundaries, %current must be able |
| 4488 | * to see both source and destination cgroups from its namespace. |
| 4489 | */ |
| 4490 | if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) && |
| 4491 | (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) || |
| 4492 | !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp))) |
| 4493 | return -ENOENT; |
| 4494 | |
| 4495 | return 0; |
| 4496 | } |
| 4497 | |
| 4498 | static ssize_t cgroup_procs_write(struct kernfs_open_file *of, |
| 4499 | char *buf, size_t nbytes, loff_t off) |
| 4500 | { |
| 4501 | struct cgroup *src_cgrp, *dst_cgrp; |
| 4502 | struct task_struct *task; |
| 4503 | ssize_t ret; |
| 4504 | |
| 4505 | dst_cgrp = cgroup_kn_lock_live(of->kn, false); |
| 4506 | if (!dst_cgrp) |
| 4507 | return -ENODEV; |
| 4508 | |
| 4509 | task = cgroup_procs_write_start(buf, true); |
| 4510 | ret = PTR_ERR_OR_ZERO(task); |
| 4511 | if (ret) |
| 4512 | goto out_unlock; |
| 4513 | |
| 4514 | /* find the source cgroup */ |
| 4515 | spin_lock_irq(&css_set_lock); |
| 4516 | src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); |
| 4517 | spin_unlock_irq(&css_set_lock); |
| 4518 | |
| 4519 | ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, |
| 4520 | of->file->f_path.dentry->d_sb); |
| 4521 | if (ret) |
| 4522 | goto out_finish; |
| 4523 | |
| 4524 | ret = cgroup_attach_task(dst_cgrp, task, true); |
| 4525 | |
| 4526 | out_finish: |
| 4527 | cgroup_procs_write_finish(task); |
| 4528 | out_unlock: |
| 4529 | cgroup_kn_unlock(of->kn); |
| 4530 | |
| 4531 | return ret ?: nbytes; |
| 4532 | } |
| 4533 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4534 | static void *cgroup_threads_start(struct seq_file *s, loff_t *pos) |
| 4535 | { |
| 4536 | return __cgroup_procs_start(s, pos, 0); |
| 4537 | } |
| 4538 | |
| 4539 | static ssize_t cgroup_threads_write(struct kernfs_open_file *of, |
| 4540 | char *buf, size_t nbytes, loff_t off) |
| 4541 | { |
| 4542 | struct cgroup *src_cgrp, *dst_cgrp; |
| 4543 | struct task_struct *task; |
| 4544 | ssize_t ret; |
| 4545 | |
| 4546 | buf = strstrip(buf); |
| 4547 | |
| 4548 | dst_cgrp = cgroup_kn_lock_live(of->kn, false); |
| 4549 | if (!dst_cgrp) |
| 4550 | return -ENODEV; |
| 4551 | |
| 4552 | task = cgroup_procs_write_start(buf, false); |
| 4553 | ret = PTR_ERR_OR_ZERO(task); |
| 4554 | if (ret) |
| 4555 | goto out_unlock; |
| 4556 | |
| 4557 | /* find the source cgroup */ |
| 4558 | spin_lock_irq(&css_set_lock); |
| 4559 | src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); |
| 4560 | spin_unlock_irq(&css_set_lock); |
| 4561 | |
| 4562 | /* thread migrations follow the cgroup.procs delegation rule */ |
| 4563 | ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, |
| 4564 | of->file->f_path.dentry->d_sb); |
| 4565 | if (ret) |
| 4566 | goto out_finish; |
| 4567 | |
| 4568 | /* and must be contained in the same domain */ |
| 4569 | ret = -EOPNOTSUPP; |
| 4570 | if (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp) |
| 4571 | goto out_finish; |
| 4572 | |
| 4573 | ret = cgroup_attach_task(dst_cgrp, task, false); |
| 4574 | |
| 4575 | out_finish: |
| 4576 | cgroup_procs_write_finish(task); |
| 4577 | out_unlock: |
| 4578 | cgroup_kn_unlock(of->kn); |
| 4579 | |
| 4580 | return ret ?: nbytes; |
| 4581 | } |
| 4582 | |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4583 | /* cgroup core interface files for the default hierarchy */ |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 4584 | static struct cftype cgroup_base_files[] = { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4585 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4586 | .name = "cgroup.type", |
| 4587 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4588 | .seq_show = cgroup_type_show, |
| 4589 | .write = cgroup_type_write, |
| 4590 | }, |
| 4591 | { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4592 | .name = "cgroup.procs", |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 4593 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4594 | .file_offset = offsetof(struct cgroup, procs_file), |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4595 | .release = cgroup_procs_release, |
| 4596 | .seq_start = cgroup_procs_start, |
| 4597 | .seq_next = cgroup_procs_next, |
| 4598 | .seq_show = cgroup_procs_show, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4599 | .write = cgroup_procs_write, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4600 | }, |
| 4601 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4602 | .name = "cgroup.threads", |
Roman Gushchin | 4f58424 | 2018-01-10 04:35:12 -0800 | [diff] [blame] | 4603 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4604 | .release = cgroup_procs_release, |
| 4605 | .seq_start = cgroup_threads_start, |
| 4606 | .seq_next = cgroup_procs_next, |
| 4607 | .seq_show = cgroup_procs_show, |
| 4608 | .write = cgroup_threads_write, |
| 4609 | }, |
| 4610 | { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4611 | .name = "cgroup.controllers", |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4612 | .seq_show = cgroup_controllers_show, |
| 4613 | }, |
| 4614 | { |
| 4615 | .name = "cgroup.subtree_control", |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 4616 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4617 | .seq_show = cgroup_subtree_control_show, |
| 4618 | .write = cgroup_subtree_control_write, |
| 4619 | }, |
| 4620 | { |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 4621 | .name = "cgroup.events", |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4622 | .flags = CFTYPE_NOT_ON_ROOT, |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4623 | .file_offset = offsetof(struct cgroup, events_file), |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 4624 | .seq_show = cgroup_events_show, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4625 | }, |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 4626 | { |
| 4627 | .name = "cgroup.max.descendants", |
| 4628 | .seq_show = cgroup_max_descendants_show, |
| 4629 | .write = cgroup_max_descendants_write, |
| 4630 | }, |
| 4631 | { |
| 4632 | .name = "cgroup.max.depth", |
| 4633 | .seq_show = cgroup_max_depth_show, |
| 4634 | .write = cgroup_max_depth_write, |
| 4635 | }, |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 4636 | { |
| 4637 | .name = "cgroup.stat", |
Tejun Heo | 3e48930 | 2017-08-11 05:49:01 -0700 | [diff] [blame] | 4638 | .seq_show = cgroup_stat_show, |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 4639 | }, |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 4640 | { |
| 4641 | .name = "cpu.stat", |
| 4642 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4643 | .seq_show = cpu_stat_show, |
| 4644 | }, |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 4645 | #ifdef CONFIG_PSI |
| 4646 | { |
| 4647 | .name = "io.pressure", |
| 4648 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4649 | .seq_show = cgroup_io_pressure_show, |
| 4650 | }, |
| 4651 | { |
| 4652 | .name = "memory.pressure", |
| 4653 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4654 | .seq_show = cgroup_memory_pressure_show, |
| 4655 | }, |
| 4656 | { |
| 4657 | .name = "cpu.pressure", |
| 4658 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4659 | .seq_show = cgroup_cpu_pressure_show, |
| 4660 | }, |
| 4661 | #endif |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4662 | { } /* terminate */ |
| 4663 | }; |
| 4664 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4665 | /* |
| 4666 | * css destruction is four-stage process. |
| 4667 | * |
| 4668 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 4669 | * Implemented in kill_css(). |
| 4670 | * |
| 4671 | * 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] | 4672 | * and thus css_tryget_online() is guaranteed to fail, the css can be |
| 4673 | * offlined by invoking offline_css(). After offlining, the base ref is |
| 4674 | * put. Implemented in css_killed_work_fn(). |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4675 | * |
| 4676 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 4677 | * accessors are inside RCU read sections. css_release() schedules the |
| 4678 | * RCU callback. |
| 4679 | * |
| 4680 | * 4. After the grace period, the css can be freed. Implemented in |
| 4681 | * css_free_work_fn(). |
| 4682 | * |
| 4683 | * It is actually hairier because both step 2 and 4 require process context |
| 4684 | * and thus involve punting to css->destroy_work adding two additional |
| 4685 | * steps to the already complex sequence. |
| 4686 | */ |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4687 | static void css_free_rwork_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4688 | { |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4689 | struct cgroup_subsys_state *css = container_of(to_rcu_work(work), |
| 4690 | struct cgroup_subsys_state, destroy_rwork); |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4691 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4692 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4693 | |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 4694 | percpu_ref_exit(&css->refcnt); |
| 4695 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4696 | if (ss) { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4697 | /* css free path */ |
Tejun Heo | 8bb5ef7 | 2016-01-21 15:32:15 -0500 | [diff] [blame] | 4698 | struct cgroup_subsys_state *parent = css->parent; |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4699 | int id = css->id; |
| 4700 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4701 | ss->css_free(css); |
| 4702 | cgroup_idr_remove(&ss->css_idr, id); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4703 | cgroup_put(cgrp); |
Tejun Heo | 8bb5ef7 | 2016-01-21 15:32:15 -0500 | [diff] [blame] | 4704 | |
| 4705 | if (parent) |
| 4706 | css_put(parent); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4707 | } else { |
| 4708 | /* cgroup free path */ |
| 4709 | atomic_dec(&cgrp->root->nr_cgrps); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 4710 | cgroup1_pidlist_destroy_all(cgrp); |
Zefan Li | 971ff49 | 2014-09-18 16:06:19 +0800 | [diff] [blame] | 4711 | cancel_work_sync(&cgrp->release_agent_work); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4712 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4713 | if (cgroup_parent(cgrp)) { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4714 | /* |
| 4715 | * We get a ref to the parent, and put the ref when |
| 4716 | * this cgroup is being freed, so it's guaranteed |
| 4717 | * that the parent won't be destroyed before its |
| 4718 | * children. |
| 4719 | */ |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4720 | cgroup_put(cgroup_parent(cgrp)); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4721 | kernfs_put(cgrp->kn); |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 4722 | psi_cgroup_free(cgrp); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4723 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4724 | cgroup_rstat_exit(cgrp); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4725 | kfree(cgrp); |
| 4726 | } else { |
| 4727 | /* |
| 4728 | * This is root cgroup's refcnt reaching zero, |
| 4729 | * which indicates that the root should be |
| 4730 | * released. |
| 4731 | */ |
| 4732 | cgroup_destroy_root(cgrp->root); |
| 4733 | } |
| 4734 | } |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4735 | } |
| 4736 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4737 | static void css_release_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4738 | { |
| 4739 | struct cgroup_subsys_state *css = |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4740 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4741 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4742 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4743 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4744 | mutex_lock(&cgroup_mutex); |
| 4745 | |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4746 | css->flags |= CSS_RELEASED; |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4747 | list_del_rcu(&css->sibling); |
| 4748 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4749 | if (ss) { |
| 4750 | /* css release path */ |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 4751 | if (!list_empty(&css->rstat_css_node)) { |
| 4752 | cgroup_rstat_flush(cgrp); |
| 4753 | list_del_rcu(&css->rstat_css_node); |
| 4754 | } |
| 4755 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4756 | cgroup_idr_replace(&ss->css_idr, NULL, css->id); |
Tejun Heo | 7d172cc | 2014-11-18 02:49:51 -0500 | [diff] [blame] | 4757 | if (ss->css_released) |
| 4758 | ss->css_released(css); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4759 | } else { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4760 | struct cgroup *tcgrp; |
| 4761 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4762 | /* cgroup release path */ |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 4763 | TRACE_CGROUP_PATH(release, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 4764 | |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4765 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4766 | cgroup_rstat_flush(cgrp); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4767 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4768 | for (tcgrp = cgroup_parent(cgrp); tcgrp; |
| 4769 | tcgrp = cgroup_parent(tcgrp)) |
| 4770 | tcgrp->nr_dying_descendants--; |
| 4771 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4772 | cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 4773 | cgrp->id = -1; |
Li Zefan | a418948 | 2014-09-04 14:43:07 +0800 | [diff] [blame] | 4774 | |
| 4775 | /* |
| 4776 | * There are two control paths which try to determine |
| 4777 | * cgroup from dentry without going through kernfs - |
| 4778 | * cgroupstats_build() and css_tryget_online_from_dir(). |
| 4779 | * Those are supported by RCU protecting clearing of |
| 4780 | * cgrp->kn->priv backpointer. |
| 4781 | */ |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4782 | if (cgrp->kn) |
| 4783 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, |
| 4784 | NULL); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 4785 | |
| 4786 | cgroup_bpf_put(cgrp); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4787 | } |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4788 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4789 | mutex_unlock(&cgroup_mutex); |
| 4790 | |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4791 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); |
| 4792 | queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4793 | } |
| 4794 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4795 | static void css_release(struct percpu_ref *ref) |
| 4796 | { |
| 4797 | struct cgroup_subsys_state *css = |
| 4798 | container_of(ref, struct cgroup_subsys_state, refcnt); |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4799 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4800 | INIT_WORK(&css->destroy_work, css_release_work_fn); |
| 4801 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4802 | } |
| 4803 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4804 | static void init_and_link_css(struct cgroup_subsys_state *css, |
| 4805 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4806 | { |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4807 | lockdep_assert_held(&cgroup_mutex); |
| 4808 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 4809 | cgroup_get_live(cgrp); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4810 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4811 | memset(css, 0, sizeof(*css)); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4812 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4813 | css->ss = ss; |
Tejun Heo | 8fa3b8d | 2016-05-26 15:42:13 -0400 | [diff] [blame] | 4814 | css->id = -1; |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4815 | INIT_LIST_HEAD(&css->sibling); |
| 4816 | INIT_LIST_HEAD(&css->children); |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 4817 | INIT_LIST_HEAD(&css->rstat_css_node); |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4818 | css->serial_nr = css_serial_nr_next++; |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 4819 | atomic_set(&css->online_cnt, 0); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4820 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4821 | if (cgroup_parent(cgrp)) { |
| 4822 | css->parent = cgroup_css(cgroup_parent(cgrp), ss); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4823 | css_get(css->parent); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4824 | } |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4825 | |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 4826 | if (cgroup_on_dfl(cgrp) && ss->css_rstat_flush) |
| 4827 | list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list); |
| 4828 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4829 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4830 | } |
| 4831 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4832 | /* 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] | 4833 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4834 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4835 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4836 | int ret = 0; |
| 4837 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4838 | lockdep_assert_held(&cgroup_mutex); |
| 4839 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4840 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4841 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4842 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4843 | css->flags |= CSS_ONLINE; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4844 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 4845 | |
| 4846 | atomic_inc(&css->online_cnt); |
| 4847 | if (css->parent) |
| 4848 | atomic_inc(&css->parent->online_cnt); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4849 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4850 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4851 | } |
| 4852 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4853 | /* 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] | 4854 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4855 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4856 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4857 | |
| 4858 | lockdep_assert_held(&cgroup_mutex); |
| 4859 | |
| 4860 | if (!(css->flags & CSS_ONLINE)) |
| 4861 | return; |
| 4862 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4863 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4864 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4865 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4866 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | e329780 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4867 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4868 | |
| 4869 | wake_up_all(&css->cgroup->offline_waitq); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4870 | } |
| 4871 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4872 | /** |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4873 | * css_create - create a cgroup_subsys_state |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4874 | * @cgrp: the cgroup new css will be associated with |
| 4875 | * @ss: the subsys of new css |
| 4876 | * |
| 4877 | * 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] | 4878 | * css is online and installed in @cgrp. This function doesn't create the |
| 4879 | * interface files. Returns 0 on success, -errno on failure. |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4880 | */ |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4881 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 4882 | struct cgroup_subsys *ss) |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4883 | { |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4884 | struct cgroup *parent = cgroup_parent(cgrp); |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4885 | struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4886 | struct cgroup_subsys_state *css; |
| 4887 | int err; |
| 4888 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4889 | lockdep_assert_held(&cgroup_mutex); |
| 4890 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4891 | css = ss->css_alloc(parent_css); |
Tejun Heo | e7e15b8 | 2016-06-21 13:06:24 -0400 | [diff] [blame] | 4892 | if (!css) |
| 4893 | css = ERR_PTR(-ENOMEM); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4894 | if (IS_ERR(css)) |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4895 | return css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4896 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4897 | init_and_link_css(css, ss, cgrp); |
Tejun Heo | a2bed82 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4898 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 4899 | err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4900 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4901 | goto err_free_css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4902 | |
Vladimir Davydov | cf780b7 | 2015-08-03 15:32:26 +0300 | [diff] [blame] | 4903 | err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4904 | if (err < 0) |
Wenwei Tao | b00c52d | 2016-05-13 22:59:20 +0800 | [diff] [blame] | 4905 | goto err_free_css; |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4906 | css->id = err; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4907 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4908 | /* @css is ready to be brought online now, make it visible */ |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4909 | list_add_tail_rcu(&css->sibling, &parent_css->children); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4910 | cgroup_idr_replace(&ss->css_idr, css, css->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4911 | |
| 4912 | err = online_css(css); |
| 4913 | if (err) |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4914 | goto err_list_del; |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4915 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4916 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4917 | cgroup_parent(parent)) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4918 | 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] | 4919 | current->comm, current->pid, ss->name); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4920 | if (!strcmp(ss->name, "memory")) |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4921 | 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] | 4922 | ss->warned_broken_hierarchy = true; |
| 4923 | } |
| 4924 | |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4925 | return css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4926 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4927 | err_list_del: |
| 4928 | list_del_rcu(&css->sibling); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4929 | err_free_css: |
Tejun Heo | 8f53470 | 2018-04-26 14:29:05 -0700 | [diff] [blame] | 4930 | list_del_rcu(&css->rstat_css_node); |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4931 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); |
| 4932 | queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4933 | return ERR_PTR(err); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4934 | } |
| 4935 | |
Tejun Heo | 07cd129 | 2017-01-26 16:47:28 -0500 | [diff] [blame] | 4936 | /* |
| 4937 | * The returned cgroup is fully initialized including its control mask, but |
| 4938 | * it isn't associated with its kernfs_node and doesn't have the control |
| 4939 | * mask applied. |
| 4940 | */ |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4941 | static struct cgroup *cgroup_create(struct cgroup *parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4942 | { |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4943 | struct cgroup_root *root = parent->root; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4944 | struct cgroup *cgrp, *tcgrp; |
| 4945 | int level = parent->level + 1; |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4946 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4947 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4948 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Kees Cook | acafe7e | 2018-05-08 13:45:50 -0700 | [diff] [blame] | 4949 | cgrp = kzalloc(struct_size(cgrp, ancestor_ids, (level + 1)), |
| 4950 | GFP_KERNEL); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4951 | if (!cgrp) |
| 4952 | return ERR_PTR(-ENOMEM); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4953 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 4954 | ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4955 | if (ret) |
| 4956 | goto out_free_cgrp; |
| 4957 | |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4958 | if (cgroup_on_dfl(parent)) { |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4959 | ret = cgroup_rstat_init(cgrp); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4960 | if (ret) |
| 4961 | goto out_cancel_ref; |
| 4962 | } |
| 4963 | |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4964 | /* |
| 4965 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4966 | * a half-baked cgroup. |
| 4967 | */ |
Vladimir Davydov | cf780b7 | 2015-08-03 15:32:26 +0300 | [diff] [blame] | 4968 | 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] | 4969 | if (cgrp->id < 0) { |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4970 | ret = -ENOMEM; |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4971 | goto out_stat_exit; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4972 | } |
| 4973 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4974 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4975 | |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 4976 | cgrp->self.parent = &parent->self; |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4977 | cgrp->root = root; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 4978 | cgrp->level = level; |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 4979 | |
| 4980 | ret = psi_cgroup_alloc(cgrp); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 4981 | if (ret) |
| 4982 | goto out_idr_free; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 4983 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 4984 | ret = cgroup_bpf_inherit(cgrp); |
| 4985 | if (ret) |
| 4986 | goto out_psi_free; |
| 4987 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4988 | for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) { |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 4989 | cgrp->ancestor_ids[tcgrp->level] = tcgrp->id; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4990 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4991 | if (tcgrp != cgrp) |
| 4992 | tcgrp->nr_descendants++; |
| 4993 | } |
| 4994 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4995 | if (notify_on_release(parent)) |
| 4996 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4997 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4998 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4999 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 5000 | |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 5001 | cgrp->self.serial_nr = css_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 5002 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5003 | /* allocation complete, commit to creation */ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5004 | list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5005 | atomic_inc(&root->nr_cgrps); |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 5006 | cgroup_get_live(parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 5007 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5008 | /* |
| 5009 | * @cgrp is now fully operational. If something fails after this |
| 5010 | * point, it'll be released via the normal destruction path. |
| 5011 | */ |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 5012 | cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 5013 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 5014 | /* |
| 5015 | * On the default hierarchy, a child doesn't automatically inherit |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 5016 | * subtree_control from the parent. Each is configured manually. |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 5017 | */ |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5018 | if (!cgroup_on_dfl(cgrp)) |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5019 | cgrp->subtree_control = cgroup_control(cgrp); |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5020 | |
| 5021 | cgroup_propagate_control(cgrp); |
| 5022 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5023 | return cgrp; |
| 5024 | |
Johannes Weiner | 2ce7135 | 2018-10-26 15:06:31 -0700 | [diff] [blame] | 5025 | out_psi_free: |
| 5026 | psi_cgroup_free(cgrp); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 5027 | out_idr_free: |
| 5028 | cgroup_idr_remove(&root->cgroup_idr, cgrp->id); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5029 | out_stat_exit: |
| 5030 | if (cgroup_on_dfl(parent)) |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 5031 | cgroup_rstat_exit(cgrp); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5032 | out_cancel_ref: |
| 5033 | percpu_ref_exit(&cgrp->self.refcnt); |
| 5034 | out_free_cgrp: |
| 5035 | kfree(cgrp); |
| 5036 | return ERR_PTR(ret); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5037 | } |
| 5038 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 5039 | static bool cgroup_check_hierarchy_limits(struct cgroup *parent) |
| 5040 | { |
| 5041 | struct cgroup *cgroup; |
| 5042 | int ret = false; |
| 5043 | int level = 1; |
| 5044 | |
| 5045 | lockdep_assert_held(&cgroup_mutex); |
| 5046 | |
| 5047 | for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) { |
| 5048 | if (cgroup->nr_descendants >= cgroup->max_descendants) |
| 5049 | goto fail; |
| 5050 | |
| 5051 | if (level > cgroup->max_depth) |
| 5052 | goto fail; |
| 5053 | |
| 5054 | level++; |
| 5055 | } |
| 5056 | |
| 5057 | ret = true; |
| 5058 | fail: |
| 5059 | return ret; |
| 5060 | } |
| 5061 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5062 | 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] | 5063 | { |
| 5064 | struct cgroup *parent, *cgrp; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5065 | struct kernfs_node *kn; |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5066 | int ret; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5067 | |
| 5068 | /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */ |
| 5069 | if (strchr(name, '\n')) |
| 5070 | return -EINVAL; |
| 5071 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5072 | parent = cgroup_kn_lock_live(parent_kn, false); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5073 | if (!parent) |
| 5074 | return -ENODEV; |
| 5075 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 5076 | if (!cgroup_check_hierarchy_limits(parent)) { |
| 5077 | ret = -EAGAIN; |
| 5078 | goto out_unlock; |
| 5079 | } |
| 5080 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5081 | cgrp = cgroup_create(parent); |
| 5082 | if (IS_ERR(cgrp)) { |
| 5083 | ret = PTR_ERR(cgrp); |
| 5084 | goto out_unlock; |
| 5085 | } |
| 5086 | |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5087 | /* create the directory */ |
| 5088 | kn = kernfs_create_dir(parent->kn, name, mode, cgrp); |
| 5089 | if (IS_ERR(kn)) { |
| 5090 | ret = PTR_ERR(kn); |
| 5091 | goto out_destroy; |
| 5092 | } |
| 5093 | cgrp->kn = kn; |
| 5094 | |
| 5095 | /* |
| 5096 | * This extra ref will be put in cgroup_free_fn() and guarantees |
| 5097 | * that @cgrp->kn is always accessible. |
| 5098 | */ |
| 5099 | kernfs_get(kn); |
| 5100 | |
| 5101 | ret = cgroup_kn_set_ugid(kn); |
| 5102 | if (ret) |
| 5103 | goto out_destroy; |
| 5104 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 5105 | ret = css_populate_dir(&cgrp->self); |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5106 | if (ret) |
| 5107 | goto out_destroy; |
| 5108 | |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5109 | ret = cgroup_apply_control_enable(cgrp); |
| 5110 | if (ret) |
| 5111 | goto out_destroy; |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5112 | |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 5113 | TRACE_CGROUP_PATH(mkdir, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5114 | |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5115 | /* let's create and online css's */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5116 | kernfs_activate(kn); |
| 5117 | |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5118 | ret = 0; |
| 5119 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5120 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 5121 | out_destroy: |
| 5122 | cgroup_destroy_locked(cgrp); |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5123 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5124 | cgroup_kn_unlock(parent_kn); |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 5125 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5126 | } |
| 5127 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5128 | /* |
| 5129 | * 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] | 5130 | * css_tryget_online() is now guaranteed to fail. Tell the subsystem to |
| 5131 | * initate destruction and put the css ref from kill_css(). |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5132 | */ |
| 5133 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5134 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5135 | struct cgroup_subsys_state *css = |
| 5136 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5137 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5138 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5139 | |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5140 | do { |
| 5141 | offline_css(css); |
| 5142 | css_put(css); |
| 5143 | /* @css can't go away while we're holding cgroup_mutex */ |
| 5144 | css = css->parent; |
| 5145 | } while (css && atomic_dec_and_test(&css->online_cnt)); |
| 5146 | |
| 5147 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5148 | } |
| 5149 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5150 | /* css kill confirmation processing requires process context, bounce */ |
| 5151 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5152 | { |
| 5153 | struct cgroup_subsys_state *css = |
| 5154 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 5155 | |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5156 | if (atomic_dec_and_test(&css->online_cnt)) { |
| 5157 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
| 5158 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
| 5159 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5160 | } |
| 5161 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 5162 | /** |
| 5163 | * kill_css - destroy a css |
| 5164 | * @css: css to destroy |
| 5165 | * |
| 5166 | * This function initiates destruction of @css by removing cgroup interface |
| 5167 | * files and putting its base reference. ->css_offline() will be invoked |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5168 | * asynchronously once css_tryget_online() is guaranteed to fail and when |
| 5169 | * the reference count reaches zero, @css will be released. |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 5170 | */ |
| 5171 | static void kill_css(struct cgroup_subsys_state *css) |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5172 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 5173 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5174 | |
Waiman Long | 33c35aa | 2017-05-15 09:34:06 -0400 | [diff] [blame] | 5175 | if (css->flags & CSS_DYING) |
| 5176 | return; |
| 5177 | |
| 5178 | css->flags |= CSS_DYING; |
| 5179 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5180 | /* |
| 5181 | * This must happen before css is disassociated with its cgroup. |
| 5182 | * See seq_css() for details. |
| 5183 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 5184 | css_clear_dir(css); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5185 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5186 | /* |
| 5187 | * Killing would put the base ref, but we need to keep it alive |
| 5188 | * until after ->css_offline(). |
| 5189 | */ |
| 5190 | css_get(css); |
| 5191 | |
| 5192 | /* |
| 5193 | * cgroup core guarantees that, by the time ->css_offline() is |
| 5194 | * invoked, no new css reference will be given out via |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5195 | * css_tryget_online(). We can't simply call percpu_ref_kill() and |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5196 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 5197 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 5198 | * |
| 5199 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 5200 | * css is confirmed to be seen as killed on all CPUs. |
| 5201 | */ |
| 5202 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | /** |
| 5206 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 5207 | * @cgrp: cgroup to be destroyed |
| 5208 | * |
| 5209 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 5210 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5211 | * guarantee that css_tryget_online() won't succeed by the time |
| 5212 | * ->css_offline() is invoked. To satisfy all the requirements, |
| 5213 | * destruction is implemented in the following two steps. |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5214 | * |
| 5215 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 5216 | * userland visible parts and start killing the percpu refcnts of |
| 5217 | * css's. Set up so that the next stage will be kicked off once all |
| 5218 | * the percpu refcnts are confirmed to be killed. |
| 5219 | * |
| 5220 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 5221 | * rest of destruction. Once all cgroup references are gone, the |
| 5222 | * cgroup is RCU-freed. |
| 5223 | * |
| 5224 | * This function implements s1. After this step, @cgrp is gone as far as |
| 5225 | * the userland is concerned and a new cgroup with the same name may be |
| 5226 | * created. As cgroup doesn't care about the names internally, this |
| 5227 | * doesn't cause any problem. |
| 5228 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5229 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 5230 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5231 | { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5232 | struct cgroup *tcgrp, *parent = cgroup_parent(cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5233 | struct cgroup_subsys_state *css; |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5234 | struct cgrp_cset_link *link; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5235 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5236 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5237 | lockdep_assert_held(&cgroup_mutex); |
| 5238 | |
Tejun Heo | 91486f6 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 5239 | /* |
| 5240 | * Only migration can raise populated from zero and we're already |
| 5241 | * holding cgroup_mutex. |
| 5242 | */ |
| 5243 | if (cgroup_is_populated(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5244 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5245 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5246 | /* |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5247 | * Make sure there's no live children. We can't test emptiness of |
| 5248 | * ->self.children as dead children linger on it while being |
| 5249 | * drained; otherwise, "rmdir parent/child parent" may fail. |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 5250 | */ |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 5251 | if (css_has_online_children(&cgrp->self)) |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 5252 | return -EBUSY; |
| 5253 | |
| 5254 | /* |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5255 | * Mark @cgrp and the associated csets dead. The former prevents |
| 5256 | * further task migration and child creation by disabling |
| 5257 | * cgroup_lock_live_group(). The latter makes the csets ignored by |
| 5258 | * the migration path. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5259 | */ |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 5260 | cgrp->self.flags &= ~CSS_ONLINE; |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5261 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5262 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5263 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 5264 | link->cset->dead = true; |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5265 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5266 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5267 | /* initiate massacre of all css's */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5268 | for_each_css(css, ssid, cgrp) |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5269 | kill_css(css); |
| 5270 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 5271 | /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */ |
| 5272 | css_clear_dir(&cgrp->self); |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 5273 | kernfs_remove(cgrp->kn); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5274 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 5275 | if (parent && cgroup_is_threaded(cgrp)) |
| 5276 | parent->nr_threaded_children--; |
| 5277 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5278 | for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) { |
| 5279 | tcgrp->nr_descendants--; |
| 5280 | tcgrp->nr_dying_descendants++; |
| 5281 | } |
| 5282 | |
Roman Gushchin | 5a621e6 | 2017-08-02 17:55:32 +0100 | [diff] [blame] | 5283 | cgroup1_check_for_release(parent); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5284 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5285 | /* put the base reference */ |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5286 | percpu_ref_kill(&cgrp->self.refcnt); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5287 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 5288 | return 0; |
| 5289 | }; |
| 5290 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5291 | int cgroup_rmdir(struct kernfs_node *kn) |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5292 | { |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5293 | struct cgroup *cgrp; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5294 | int ret = 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5295 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5296 | cgrp = cgroup_kn_lock_live(kn, false); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5297 | if (!cgrp) |
| 5298 | return 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5299 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5300 | ret = cgroup_destroy_locked(cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5301 | if (!ret) |
Steven Rostedt (VMware) | e4f8d81 | 2018-07-09 17:48:54 -0400 | [diff] [blame] | 5302 | TRACE_CGROUP_PATH(rmdir, cgrp); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5303 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5304 | cgroup_kn_unlock(kn); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5305 | return ret; |
| 5306 | } |
| 5307 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5308 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 5309 | .show_options = cgroup_show_options, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5310 | .mkdir = cgroup_mkdir, |
| 5311 | .rmdir = cgroup_rmdir, |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 5312 | .show_path = cgroup_show_path, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5313 | }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5314 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5315 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5316 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5317 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 5318 | |
Tejun Heo | a5ae989 | 2015-12-29 14:53:56 -0500 | [diff] [blame] | 5319 | pr_debug("Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5320 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5321 | mutex_lock(&cgroup_mutex); |
| 5322 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5323 | idr_init(&ss->css_idr); |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 5324 | INIT_LIST_HEAD(&ss->cfts); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5325 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5326 | /* Create the root cgroup state for this subsystem */ |
| 5327 | ss->root = &cgrp_dfl_root; |
| 5328 | css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5329 | /* We don't handle early failures gracefully */ |
| 5330 | BUG_ON(IS_ERR(css)); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5331 | init_and_link_css(css, ss, &cgrp_dfl_root.cgrp); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 5332 | |
| 5333 | /* |
| 5334 | * Root csses are never destroyed and we can't initialize |
| 5335 | * percpu_ref during early init. Disable refcnting. |
| 5336 | */ |
| 5337 | css->flags |= CSS_NO_REF; |
| 5338 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5339 | if (early) { |
Tejun Heo | 9395a45 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5340 | /* allocation can't be done safely during early init */ |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5341 | css->id = 1; |
| 5342 | } else { |
| 5343 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL); |
| 5344 | BUG_ON(css->id < 0); |
| 5345 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5346 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5347 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5348 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5349 | * newly registered, all tasks and hence the |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5350 | * init_css_set is in the subsystem's root cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5351 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5352 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 5353 | have_fork_callback |= (bool)ss->fork << ss->id; |
| 5354 | have_exit_callback |= (bool)ss->exit << ss->id; |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5355 | have_free_callback |= (bool)ss->free << ss->id; |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5356 | have_canfork_callback |= (bool)ss->can_fork << ss->id; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5357 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5358 | /* At system boot, before all subsystems have been |
| 5359 | * registered, no tasks have been forked, so we don't |
| 5360 | * need to invoke fork callbacks here. */ |
| 5361 | BUG_ON(!list_empty(&init_task.tasks)); |
| 5362 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5363 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 5364 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5365 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5366 | } |
| 5367 | |
| 5368 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5369 | * cgroup_init_early - cgroup initialization at system boot |
| 5370 | * |
| 5371 | * Initialize cgroups at system boot, and initialize any |
| 5372 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5373 | */ |
| 5374 | int __init cgroup_init_early(void) |
| 5375 | { |
Al Viro | f5dfb53 | 2019-01-16 23:42:38 -0500 | [diff] [blame] | 5376 | static struct cgroup_fs_context __initdata ctx; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5377 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5378 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5379 | |
Al Viro | cf6299b1 | 2019-01-17 02:25:51 -0500 | [diff] [blame] | 5380 | ctx.root = &cgrp_dfl_root; |
| 5381 | init_cgroup_root(&ctx); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 5382 | cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF; |
| 5383 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5384 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5385 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5386 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5387 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Xiubo Li | 63253ad | 2016-02-26 13:07:38 +0800 | [diff] [blame] | 5388 | "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] | 5389 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5390 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5391 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 5392 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5393 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5394 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5395 | ss->name = cgroup_subsys_name[i]; |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5396 | if (!ss->legacy_name) |
| 5397 | ss->legacy_name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5398 | |
| 5399 | if (ss->early_init) |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5400 | cgroup_init_subsys(ss, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5401 | } |
| 5402 | return 0; |
| 5403 | } |
| 5404 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 5405 | static u16 cgroup_disable_mask __initdata; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5406 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5407 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5408 | * cgroup_init - cgroup initialization |
| 5409 | * |
| 5410 | * Register cgroup filesystem and /proc file, and initialize |
| 5411 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5412 | */ |
| 5413 | int __init cgroup_init(void) |
| 5414 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5415 | struct cgroup_subsys *ss; |
Tejun Heo | 035f4f5 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 5416 | int ssid; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5417 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 5418 | BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16); |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 5419 | BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem)); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5420 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); |
| 5421 | BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5422 | |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 5423 | cgroup_rstat_boot(); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5424 | |
Peter Zijlstra | 3942a9b | 2016-08-11 18:54:13 +0200 | [diff] [blame] | 5425 | /* |
Paul E. McKenney | 2af3024 | 2018-11-07 14:11:40 -0800 | [diff] [blame] | 5426 | * The latency of the synchronize_rcu() is too high for cgroups, |
Peter Zijlstra | 3942a9b | 2016-08-11 18:54:13 +0200 | [diff] [blame] | 5427 | * avoid it at the cost of forcing all readers into the slow path. |
| 5428 | */ |
| 5429 | rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss); |
| 5430 | |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 5431 | get_user_ns(init_cgroup_ns.user_ns); |
| 5432 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5433 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5434 | |
Tejun Heo | 2378d8b | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 5435 | /* |
| 5436 | * Add init_css_set to the hash table so that dfl_root can link to |
| 5437 | * it during init. |
| 5438 | */ |
| 5439 | hash_add(css_set_table, &init_css_set.hlist, |
| 5440 | css_set_hash(init_css_set.subsys)); |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5441 | |
Al Viro | 35ac118 | 2019-01-12 00:20:54 -0500 | [diff] [blame] | 5442 | BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5443 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5444 | mutex_unlock(&cgroup_mutex); |
| 5445 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5446 | for_each_subsys(ss, ssid) { |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5447 | if (ss->early_init) { |
| 5448 | struct cgroup_subsys_state *css = |
| 5449 | init_css_set.subsys[ss->id]; |
| 5450 | |
| 5451 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, |
| 5452 | GFP_KERNEL); |
| 5453 | BUG_ON(css->id < 0); |
| 5454 | } else { |
| 5455 | cgroup_init_subsys(ss, false); |
| 5456 | } |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5457 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 5458 | list_add_tail(&init_css_set.e_cset_node[ssid], |
| 5459 | &cgrp_dfl_root.cgrp.e_csets[ssid]); |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5460 | |
| 5461 | /* |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 5462 | * Setting dfl_root subsys_mask needs to consider the |
| 5463 | * disabled flag and cftype registration needs kmalloc, |
| 5464 | * both of which aren't available during early_init. |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5465 | */ |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5466 | if (cgroup_disable_mask & (1 << ssid)) { |
| 5467 | static_branch_disable(cgroup_subsys_enabled_key[ssid]); |
| 5468 | printk(KERN_INFO "Disabling %s control group subsystem\n", |
| 5469 | ss->name); |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5470 | continue; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5471 | } |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5472 | |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5473 | if (cgroup1_ssid_disabled(ssid)) |
Johannes Weiner | 223ffb2 | 2016-02-11 13:34:49 -0500 | [diff] [blame] | 5474 | printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n", |
| 5475 | ss->name); |
| 5476 | |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5477 | cgrp_dfl_root.subsys_mask |= 1 << ss->id; |
| 5478 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5479 | /* implicit controllers must be threaded too */ |
| 5480 | WARN_ON(ss->implicit_on_dfl && !ss->threaded); |
| 5481 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 5482 | if (ss->implicit_on_dfl) |
| 5483 | cgrp_dfl_implicit_ss_mask |= 1 << ss->id; |
| 5484 | else if (!ss->dfl_cftypes) |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 5485 | cgrp_dfl_inhibit_ss_mask |= 1 << ss->id; |
Tejun Heo | 5de4fa1 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5486 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5487 | if (ss->threaded) |
| 5488 | cgrp_dfl_threaded_ss_mask |= 1 << ss->id; |
| 5489 | |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5490 | if (ss->dfl_cftypes == ss->legacy_cftypes) { |
| 5491 | WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes)); |
| 5492 | } else { |
| 5493 | WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes)); |
| 5494 | WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes)); |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 5495 | } |
Vladimir Davydov | 295458e | 2015-02-19 17:34:46 +0300 | [diff] [blame] | 5496 | |
| 5497 | if (ss->bind) |
| 5498 | ss->bind(init_css_set.subsys[ssid]); |
Tejun Heo | 7af608e | 2017-07-18 17:57:46 -0400 | [diff] [blame] | 5499 | |
| 5500 | mutex_lock(&cgroup_mutex); |
| 5501 | css_populate_dir(init_css_set.subsys[ssid]); |
| 5502 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5503 | } |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5504 | |
Tejun Heo | 2378d8b | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 5505 | /* init_css_set.subsys[] has been updated, re-hash */ |
| 5506 | hash_del(&init_css_set.hlist); |
| 5507 | hash_add(css_set_table, &init_css_set.hlist, |
| 5508 | css_set_hash(init_css_set.subsys)); |
| 5509 | |
Tejun Heo | 035f4f5 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 5510 | WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup")); |
| 5511 | WARN_ON(register_filesystem(&cgroup_fs_type)); |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 5512 | WARN_ON(register_filesystem(&cgroup2_fs_type)); |
Christoph Hellwig | 3f3942a | 2018-05-15 15:57:23 +0200 | [diff] [blame] | 5513 | WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show)); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5514 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5515 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5516 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5517 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5518 | static int __init cgroup_wq_init(void) |
| 5519 | { |
| 5520 | /* |
| 5521 | * There isn't much point in executing destruction path in |
| 5522 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 5523 | * Use 1 for @max_active. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5524 | * |
| 5525 | * We would prefer to do this in cgroup_init() above, but that |
| 5526 | * is called before init_workqueues(): so leave this until after. |
| 5527 | */ |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 5528 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5529 | BUG_ON(!cgroup_destroy_wq); |
| 5530 | return 0; |
| 5531 | } |
| 5532 | core_initcall(cgroup_wq_init); |
| 5533 | |
Shaohua Li | 69fd5c3 | 2017-07-12 11:49:55 -0700 | [diff] [blame] | 5534 | void cgroup_path_from_kernfs_id(const union kernfs_node_id *id, |
| 5535 | char *buf, size_t buflen) |
| 5536 | { |
| 5537 | struct kernfs_node *kn; |
| 5538 | |
| 5539 | kn = kernfs_get_node_by_id(cgrp_dfl_root.kf_root, id); |
| 5540 | if (!kn) |
| 5541 | return; |
| 5542 | kernfs_path(kn, buf, buflen); |
| 5543 | kernfs_put(kn); |
| 5544 | } |
| 5545 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5546 | /* |
| 5547 | * proc_cgroup_show() |
| 5548 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 5549 | * - Used for /proc/<pid>/cgroup. |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5550 | */ |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 5551 | int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, |
| 5552 | struct pid *pid, struct task_struct *tsk) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5553 | { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5554 | char *buf; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5555 | int retval; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5556 | struct cgroup_root *root; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5557 | |
| 5558 | retval = -ENOMEM; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5559 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5560 | if (!buf) |
| 5561 | goto out; |
| 5562 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5563 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5564 | spin_lock_irq(&css_set_lock); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5565 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5566 | for_each_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5567 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5568 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 5569 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5570 | |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 5571 | if (root == &cgrp_dfl_root && !cgrp_dfl_visible) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5572 | continue; |
| 5573 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5574 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | d98817d | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5575 | if (root != &cgrp_dfl_root) |
| 5576 | for_each_subsys(ss, ssid) |
| 5577 | if (root->subsys_mask & (1 << ssid)) |
| 5578 | seq_printf(m, "%s%s", count++ ? "," : "", |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5579 | ss->legacy_name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 5580 | if (strlen(root->name)) |
| 5581 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 5582 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5583 | seq_putc(m, ':'); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5584 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5585 | cgrp = task_cgroup_from_root(tsk, root); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5586 | |
| 5587 | /* |
| 5588 | * On traditional hierarchies, all zombie tasks show up as |
| 5589 | * belonging to the root cgroup. On the default hierarchy, |
| 5590 | * while a zombie doesn't show up in "cgroup.procs" and |
| 5591 | * thus can't be migrated, its /proc/PID/cgroup keeps |
| 5592 | * reporting the cgroup it belonged to before exiting. If |
| 5593 | * the cgroup is removed before the zombie is reaped, |
| 5594 | * " (deleted)" is appended to the cgroup path. |
| 5595 | */ |
| 5596 | if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5597 | retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX, |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 5598 | current->nsproxy->cgroup_ns); |
Tejun Heo | e022300 | 2016-09-29 15:49:40 +0200 | [diff] [blame] | 5599 | if (retval >= PATH_MAX) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5600 | retval = -ENAMETOOLONG; |
Tejun Heo | e022300 | 2016-09-29 15:49:40 +0200 | [diff] [blame] | 5601 | if (retval < 0) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5602 | goto out_unlock; |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5603 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5604 | seq_puts(m, buf); |
| 5605 | } else { |
| 5606 | seq_puts(m, "/"); |
| 5607 | } |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5608 | |
| 5609 | if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp)) |
| 5610 | seq_puts(m, " (deleted)\n"); |
| 5611 | else |
| 5612 | seq_putc(m, '\n'); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5613 | } |
| 5614 | |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 5615 | retval = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5616 | out_unlock: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5617 | spin_unlock_irq(&css_set_lock); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5618 | mutex_unlock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5619 | kfree(buf); |
| 5620 | out: |
| 5621 | return retval; |
| 5622 | } |
| 5623 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5624 | /** |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5625 | * cgroup_fork - initialize cgroup related fields during copy_process() |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5626 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5627 | * |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5628 | * A task is associated with the init_css_set until cgroup_post_fork() |
| 5629 | * attaches it to the parent's css_set. Empty cg_list indicates that |
| 5630 | * @child isn't holding reference to its css_set. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5631 | */ |
| 5632 | void cgroup_fork(struct task_struct *child) |
| 5633 | { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5634 | RCU_INIT_POINTER(child->cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5635 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5636 | } |
| 5637 | |
| 5638 | /** |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5639 | * cgroup_can_fork - called on a new task before the process is exposed |
| 5640 | * @child: the task in question. |
| 5641 | * |
| 5642 | * This calls the subsystem can_fork() callbacks. If the can_fork() callback |
| 5643 | * returns an error, the fork aborts with that error code. This allows for |
| 5644 | * a cgroup subsystem to conditionally allow or deny new forks. |
| 5645 | */ |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5646 | int cgroup_can_fork(struct task_struct *child) |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5647 | { |
| 5648 | struct cgroup_subsys *ss; |
| 5649 | int i, j, ret; |
| 5650 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5651 | do_each_subsys_mask(ss, i, have_canfork_callback) { |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5652 | ret = ss->can_fork(child); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5653 | if (ret) |
| 5654 | goto out_revert; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5655 | } while_each_subsys_mask(); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5656 | |
| 5657 | return 0; |
| 5658 | |
| 5659 | out_revert: |
| 5660 | for_each_subsys(ss, j) { |
| 5661 | if (j >= i) |
| 5662 | break; |
| 5663 | if (ss->cancel_fork) |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5664 | ss->cancel_fork(child); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5665 | } |
| 5666 | |
| 5667 | return ret; |
| 5668 | } |
| 5669 | |
| 5670 | /** |
| 5671 | * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork() |
| 5672 | * @child: the task in question |
| 5673 | * |
| 5674 | * This calls the cancel_fork() callbacks if a fork failed *after* |
| 5675 | * cgroup_can_fork() succeded. |
| 5676 | */ |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5677 | void cgroup_cancel_fork(struct task_struct *child) |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5678 | { |
| 5679 | struct cgroup_subsys *ss; |
| 5680 | int i; |
| 5681 | |
| 5682 | for_each_subsys(ss, i) |
| 5683 | if (ss->cancel_fork) |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5684 | ss->cancel_fork(child); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5688 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5689 | * @child: the task in question |
| 5690 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5691 | * Adds the task to the list running through its css_set if necessary and |
| 5692 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5693 | * 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] | 5694 | * 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] | 5695 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5696 | */ |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5697 | void cgroup_post_fork(struct task_struct *child) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5698 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5699 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5700 | int i; |
| 5701 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5702 | /* |
Dongsheng Yang | 251f8c0 | 2014-08-25 19:27:52 +0800 | [diff] [blame] | 5703 | * This may race against cgroup_enable_task_cg_lists(). As that |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5704 | * function sets use_task_css_set_links before grabbing |
| 5705 | * tasklist_lock and we just went through tasklist_lock to add |
| 5706 | * @child, it's guaranteed that either we see the set |
| 5707 | * use_task_css_set_links or cgroup_enable_task_cg_lists() sees |
| 5708 | * @child during its iteration. |
| 5709 | * |
| 5710 | * If we won the race, @child is associated with %current's |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5711 | * css_set. Grabbing css_set_lock guarantees both that the |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5712 | * association is stable, and, on completion of the parent's |
| 5713 | * migration, @child is visible in the source of migration or |
| 5714 | * already in the destination cgroup. This guarantee is necessary |
| 5715 | * when implementing operations which need to migrate all tasks of |
| 5716 | * a cgroup to another. |
| 5717 | * |
Dongsheng Yang | 251f8c0 | 2014-08-25 19:27:52 +0800 | [diff] [blame] | 5718 | * Note that if we lose to cgroup_enable_task_cg_lists(), @child |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5719 | * will remain in init_css_set. This is safe because all tasks are |
| 5720 | * in the init_css_set before cg_links is enabled and there's no |
| 5721 | * operation which transfers all tasks out of init_css_set. |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5722 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5723 | if (use_task_css_set_links) { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5724 | struct css_set *cset; |
| 5725 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5726 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5727 | cset = task_css_set(current); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5728 | if (list_empty(&child->cg_list)) { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5729 | get_css_set(cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 5730 | cset->nr_tasks++; |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 5731 | css_set_move_task(child, NULL, cset, false); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5732 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5733 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5734 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5735 | |
| 5736 | /* |
| 5737 | * Call ss->fork(). This must happen after @child is linked on |
| 5738 | * css_set; otherwise, @child might change state between ->fork() |
| 5739 | * and addition to css_set. |
| 5740 | */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5741 | do_each_subsys_mask(ss, i, have_fork_callback) { |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5742 | ss->fork(child); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5743 | } while_each_subsys_mask(); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5744 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5745 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5746 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5747 | * cgroup_exit - detach cgroup from exiting task |
| 5748 | * @tsk: pointer to task_struct of exiting process |
| 5749 | * |
| 5750 | * Description: Detach cgroup from @tsk and release it. |
| 5751 | * |
| 5752 | * Note that cgroups marked notify_on_release force every task in |
| 5753 | * them to take the global cgroup_mutex mutex when exiting. |
| 5754 | * This could impact scaling on very large systems. Be reluctant to |
| 5755 | * use notify_on_release cgroups where very high task exit scaling |
| 5756 | * is required on large systems. |
| 5757 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5758 | * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We |
| 5759 | * call cgroup_exit() while the task is still competent to handle |
| 5760 | * notify_on_release(), then leave the task attached to the root cgroup in |
| 5761 | * each hierarchy for the remainder of its exit. No need to bother with |
| 5762 | * 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] | 5763 | * with migration path - PF_EXITING is visible to migration path. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5764 | */ |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 5765 | void cgroup_exit(struct task_struct *tsk) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5766 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5767 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5768 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5769 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5770 | |
| 5771 | /* |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5772 | * 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] | 5773 | * with us, we can check css_set and cg_list without synchronization. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5774 | */ |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 5775 | cset = task_css_set(tsk); |
| 5776 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5777 | if (!list_empty(&tsk->cg_list)) { |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5778 | spin_lock_irq(&css_set_lock); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 5779 | css_set_move_task(tsk, cset, NULL, false); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 5780 | cset->nr_tasks--; |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5781 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5782 | } else { |
| 5783 | get_css_set(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5784 | } |
| 5785 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 5786 | /* see cgroup_post_fork() for details */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5787 | do_each_subsys_mask(ss, i, have_exit_callback) { |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5788 | ss->exit(tsk); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5789 | } while_each_subsys_mask(); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5790 | } |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5791 | |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5792 | void cgroup_free(struct task_struct *task) |
| 5793 | { |
| 5794 | struct css_set *cset = task_css_set(task); |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5795 | struct cgroup_subsys *ss; |
| 5796 | int ssid; |
| 5797 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5798 | do_each_subsys_mask(ss, ssid, have_free_callback) { |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5799 | ss->free(task); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5800 | } while_each_subsys_mask(); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5801 | |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5802 | put_css_set(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5803 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5804 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5805 | static int __init cgroup_disable(char *str) |
| 5806 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5807 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5808 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5809 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5810 | |
| 5811 | while ((token = strsep(&str, ",")) != NULL) { |
| 5812 | if (!*token) |
| 5813 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5814 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5815 | for_each_subsys(ss, i) { |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5816 | if (strcmp(token, ss->name) && |
| 5817 | strcmp(token, ss->legacy_name)) |
| 5818 | continue; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5819 | cgroup_disable_mask |= 1 << i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5820 | } |
| 5821 | } |
| 5822 | return 1; |
| 5823 | } |
| 5824 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5825 | |
Waiman Long | 5cf8114 | 2018-11-08 10:08:46 -0500 | [diff] [blame] | 5826 | void __init __weak enable_debug_cgroup(void) { } |
| 5827 | |
| 5828 | static int __init enable_cgroup_debug(char *str) |
| 5829 | { |
| 5830 | cgroup_debug = true; |
| 5831 | enable_debug_cgroup(); |
| 5832 | return 1; |
| 5833 | } |
| 5834 | __setup("cgroup_debug", enable_cgroup_debug); |
| 5835 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5836 | /** |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5837 | * css_tryget_online_from_dir - get corresponding css from a cgroup dentry |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5838 | * @dentry: directory dentry of interest |
| 5839 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5840 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5841 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 5842 | * to get the corresponding css and return it. If such css doesn't exist |
| 5843 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5844 | */ |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5845 | struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, |
| 5846 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5847 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5848 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
Tejun Heo | f17fc25 | 2016-02-23 10:00:51 -0500 | [diff] [blame] | 5849 | struct file_system_type *s_type = dentry->d_sb->s_type; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5850 | struct cgroup_subsys_state *css = NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5851 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5852 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5853 | /* is @dentry a cgroup dir? */ |
Tejun Heo | f17fc25 | 2016-02-23 10:00:51 -0500 | [diff] [blame] | 5854 | if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) || |
| 5855 | !kn || kernfs_type(kn) != KERNFS_DIR) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5856 | return ERR_PTR(-EBADF); |
| 5857 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5858 | rcu_read_lock(); |
| 5859 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5860 | /* |
| 5861 | * This path doesn't originate from kernfs and @kn could already |
| 5862 | * have been or be removed at any point. @kn->priv is RCU |
Li Zefan | a418948 | 2014-09-04 14:43:07 +0800 | [diff] [blame] | 5863 | * protected for this access. See css_release_work_fn() for details. |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5864 | */ |
Tejun Heo | e0aed7c | 2016-12-27 14:49:09 -0500 | [diff] [blame] | 5865 | cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5866 | if (cgrp) |
| 5867 | css = cgroup_css(cgrp, ss); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5868 | |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5869 | if (!css || !css_tryget_online(css)) |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5870 | css = ERR_PTR(-ENOENT); |
| 5871 | |
| 5872 | rcu_read_unlock(); |
| 5873 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5874 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5875 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5876 | /** |
| 5877 | * css_from_id - lookup css by id |
| 5878 | * @id: the cgroup id |
| 5879 | * @ss: cgroup subsys to be looked into |
| 5880 | * |
| 5881 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5882 | * Should be called under rcu_read_lock(). |
| 5883 | */ |
| 5884 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5885 | { |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 5886 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Johannes Weiner | d6ccc55 | 2016-06-17 12:24:27 -0400 | [diff] [blame] | 5887 | return idr_find(&ss->css_idr, id); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5888 | } |
| 5889 | |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 5890 | /** |
| 5891 | * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path |
| 5892 | * @path: path on the default hierarchy |
| 5893 | * |
| 5894 | * Find the cgroup at @path on the default hierarchy, increment its |
| 5895 | * reference count and return it. Returns pointer to the found cgroup on |
| 5896 | * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR) |
| 5897 | * if @path points to a non-directory. |
| 5898 | */ |
| 5899 | struct cgroup *cgroup_get_from_path(const char *path) |
| 5900 | { |
| 5901 | struct kernfs_node *kn; |
| 5902 | struct cgroup *cgrp; |
| 5903 | |
| 5904 | mutex_lock(&cgroup_mutex); |
| 5905 | |
| 5906 | kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path); |
| 5907 | if (kn) { |
| 5908 | if (kernfs_type(kn) == KERNFS_DIR) { |
| 5909 | cgrp = kn->priv; |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 5910 | cgroup_get_live(cgrp); |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 5911 | } else { |
| 5912 | cgrp = ERR_PTR(-ENOTDIR); |
| 5913 | } |
| 5914 | kernfs_put(kn); |
| 5915 | } else { |
| 5916 | cgrp = ERR_PTR(-ENOENT); |
| 5917 | } |
| 5918 | |
| 5919 | mutex_unlock(&cgroup_mutex); |
| 5920 | return cgrp; |
| 5921 | } |
| 5922 | EXPORT_SYMBOL_GPL(cgroup_get_from_path); |
| 5923 | |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 5924 | /** |
| 5925 | * cgroup_get_from_fd - get a cgroup pointer from a fd |
| 5926 | * @fd: fd obtained by open(cgroup2_dir) |
| 5927 | * |
| 5928 | * Find the cgroup from a fd which should be obtained |
| 5929 | * by opening a cgroup directory. Returns a pointer to the |
| 5930 | * cgroup on success. ERR_PTR is returned if the cgroup |
| 5931 | * cannot be found. |
| 5932 | */ |
| 5933 | struct cgroup *cgroup_get_from_fd(int fd) |
| 5934 | { |
| 5935 | struct cgroup_subsys_state *css; |
| 5936 | struct cgroup *cgrp; |
| 5937 | struct file *f; |
| 5938 | |
| 5939 | f = fget_raw(fd); |
| 5940 | if (!f) |
| 5941 | return ERR_PTR(-EBADF); |
| 5942 | |
| 5943 | css = css_tryget_online_from_dir(f->f_path.dentry, NULL); |
| 5944 | fput(f); |
| 5945 | if (IS_ERR(css)) |
| 5946 | return ERR_CAST(css); |
| 5947 | |
| 5948 | cgrp = css->cgroup; |
| 5949 | if (!cgroup_on_dfl(cgrp)) { |
| 5950 | cgroup_put(cgrp); |
| 5951 | return ERR_PTR(-EBADF); |
| 5952 | } |
| 5953 | |
| 5954 | return cgrp; |
| 5955 | } |
| 5956 | EXPORT_SYMBOL_GPL(cgroup_get_from_fd); |
| 5957 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 5958 | /* |
| 5959 | * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data |
| 5960 | * definition in cgroup-defs.h. |
| 5961 | */ |
| 5962 | #ifdef CONFIG_SOCK_CGROUP_DATA |
| 5963 | |
| 5964 | #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID) |
| 5965 | |
Tejun Heo | 3fa4cc9 | 2015-12-14 11:24:06 -0500 | [diff] [blame] | 5966 | DEFINE_SPINLOCK(cgroup_sk_update_lock); |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 5967 | static bool cgroup_sk_alloc_disabled __read_mostly; |
| 5968 | |
| 5969 | void cgroup_sk_alloc_disable(void) |
| 5970 | { |
| 5971 | if (cgroup_sk_alloc_disabled) |
| 5972 | return; |
| 5973 | pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n"); |
| 5974 | cgroup_sk_alloc_disabled = true; |
| 5975 | } |
| 5976 | |
| 5977 | #else |
| 5978 | |
| 5979 | #define cgroup_sk_alloc_disabled false |
| 5980 | |
| 5981 | #endif |
| 5982 | |
| 5983 | void cgroup_sk_alloc(struct sock_cgroup_data *skcd) |
| 5984 | { |
| 5985 | if (cgroup_sk_alloc_disabled) |
| 5986 | return; |
| 5987 | |
Johannes Weiner | d979a39 | 2016-09-19 14:44:38 -0700 | [diff] [blame] | 5988 | /* Socket clone path */ |
| 5989 | if (skcd->val) { |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 5990 | /* |
| 5991 | * We might be cloning a socket which is left in an empty |
| 5992 | * cgroup and the cgroup might have already been rmdir'd. |
| 5993 | * Don't use cgroup_get_live(). |
| 5994 | */ |
Johannes Weiner | d979a39 | 2016-09-19 14:44:38 -0700 | [diff] [blame] | 5995 | cgroup_get(sock_cgroup_ptr(skcd)); |
| 5996 | return; |
| 5997 | } |
| 5998 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 5999 | rcu_read_lock(); |
| 6000 | |
| 6001 | while (true) { |
| 6002 | struct css_set *cset; |
| 6003 | |
| 6004 | cset = task_css_set(current); |
| 6005 | if (likely(cgroup_tryget(cset->dfl_cgrp))) { |
| 6006 | skcd->val = (unsigned long)cset->dfl_cgrp; |
| 6007 | break; |
| 6008 | } |
| 6009 | cpu_relax(); |
| 6010 | } |
| 6011 | |
| 6012 | rcu_read_unlock(); |
| 6013 | } |
| 6014 | |
| 6015 | void cgroup_sk_free(struct sock_cgroup_data *skcd) |
| 6016 | { |
| 6017 | cgroup_put(sock_cgroup_ptr(skcd)); |
| 6018 | } |
| 6019 | |
| 6020 | #endif /* CONFIG_SOCK_CGROUP_DATA */ |
| 6021 | |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6022 | #ifdef CONFIG_CGROUP_BPF |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 6023 | int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 6024 | enum bpf_attach_type type, u32 flags) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6025 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 6026 | int ret; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6027 | |
| 6028 | mutex_lock(&cgroup_mutex); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 6029 | ret = __cgroup_bpf_attach(cgrp, prog, type, flags); |
| 6030 | mutex_unlock(&cgroup_mutex); |
| 6031 | return ret; |
| 6032 | } |
| 6033 | int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 6034 | enum bpf_attach_type type, u32 flags) |
| 6035 | { |
| 6036 | int ret; |
| 6037 | |
| 6038 | mutex_lock(&cgroup_mutex); |
| 6039 | ret = __cgroup_bpf_detach(cgrp, prog, type, flags); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6040 | mutex_unlock(&cgroup_mutex); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 6041 | return ret; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6042 | } |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 6043 | int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr, |
| 6044 | union bpf_attr __user *uattr) |
| 6045 | { |
| 6046 | int ret; |
| 6047 | |
| 6048 | mutex_lock(&cgroup_mutex); |
| 6049 | ret = __cgroup_bpf_query(cgrp, attr, uattr); |
| 6050 | mutex_unlock(&cgroup_mutex); |
| 6051 | return ret; |
| 6052 | } |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6053 | #endif /* CONFIG_CGROUP_BPF */ |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6054 | |
| 6055 | #ifdef CONFIG_SYSFS |
| 6056 | static ssize_t show_delegatable_files(struct cftype *files, char *buf, |
| 6057 | ssize_t size, const char *prefix) |
| 6058 | { |
| 6059 | struct cftype *cft; |
| 6060 | ssize_t ret = 0; |
| 6061 | |
| 6062 | for (cft = files; cft && cft->name[0] != '\0'; cft++) { |
| 6063 | if (!(cft->flags & CFTYPE_NS_DELEGATABLE)) |
| 6064 | continue; |
| 6065 | |
| 6066 | if (prefix) |
| 6067 | ret += snprintf(buf + ret, size - ret, "%s.", prefix); |
| 6068 | |
| 6069 | ret += snprintf(buf + ret, size - ret, "%s\n", cft->name); |
| 6070 | |
Yangtao Li | 4d9ebbe | 2018-11-03 22:27:41 -0400 | [diff] [blame] | 6071 | if (WARN_ON(ret >= size)) |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6072 | break; |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | return ret; |
| 6076 | } |
| 6077 | |
| 6078 | static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 6079 | char *buf) |
| 6080 | { |
| 6081 | struct cgroup_subsys *ss; |
| 6082 | int ssid; |
| 6083 | ssize_t ret = 0; |
| 6084 | |
| 6085 | ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret, |
| 6086 | NULL); |
| 6087 | |
| 6088 | for_each_subsys(ss, ssid) |
| 6089 | ret += show_delegatable_files(ss->dfl_cftypes, buf + ret, |
| 6090 | PAGE_SIZE - ret, |
| 6091 | cgroup_subsys_name[ssid]); |
| 6092 | |
| 6093 | return ret; |
| 6094 | } |
| 6095 | static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate); |
| 6096 | |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 6097 | static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 6098 | char *buf) |
| 6099 | { |
| 6100 | return snprintf(buf, PAGE_SIZE, "nsdelegate\n"); |
| 6101 | } |
| 6102 | static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features); |
| 6103 | |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6104 | static struct attribute *cgroup_sysfs_attrs[] = { |
| 6105 | &cgroup_delegate_attr.attr, |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 6106 | &cgroup_features_attr.attr, |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 6107 | NULL, |
| 6108 | }; |
| 6109 | |
| 6110 | static const struct attribute_group cgroup_sysfs_attr_group = { |
| 6111 | .attrs = cgroup_sysfs_attrs, |
| 6112 | .name = "cgroup", |
| 6113 | }; |
| 6114 | |
| 6115 | static int __init cgroup_sysfs_init(void) |
| 6116 | { |
| 6117 | return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group); |
| 6118 | } |
| 6119 | subsys_initcall(cgroup_sysfs_init); |
| 6120 | #endif /* CONFIG_SYSFS */ |