Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2 | * Generic process-grouping system. |
| 3 | * |
| 4 | * Based originally on the cpuset system, extracted by Paul Menage |
| 5 | * Copyright (C) 2006 Google, Inc |
| 6 | * |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 7 | * Notifications support |
| 8 | * Copyright (C) 2009 Nokia Corporation |
| 9 | * Author: Kirill A. Shutemov |
| 10 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 11 | * Copyright notices from the original cpuset code: |
| 12 | * -------------------------------------------------- |
| 13 | * Copyright (C) 2003 BULL SA. |
| 14 | * Copyright (C) 2004-2006 Silicon Graphics, Inc. |
| 15 | * |
| 16 | * Portions derived from Patrick Mochel's sysfs code. |
| 17 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
| 18 | * |
| 19 | * 2003-10-10 Written by Simon Derr. |
| 20 | * 2003-10-22 Updates by Stephen Hemminger. |
| 21 | * 2004 May-July Rework by Paul Jackson. |
| 22 | * --------------------------------------------------- |
| 23 | * |
| 24 | * This file is subject to the terms and conditions of the GNU General Public |
| 25 | * License. See the file COPYING in the main directory of the Linux |
| 26 | * distribution for more details. |
| 27 | */ |
| 28 | |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 30 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 31 | #include "cgroup-internal.h" |
| 32 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 33 | #include <linux/cred.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 34 | #include <linux/errno.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 35 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 36 | #include <linux/kernel.h> |
Jianyu Zhan | c9482a5 | 2014-04-26 15:40:28 +0800 | [diff] [blame] | 37 | #include <linux/magic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 38 | #include <linux/mutex.h> |
| 39 | #include <linux/mount.h> |
| 40 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 41 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 42 | #include <linux/rcupdate.h> |
| 43 | #include <linux/sched.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 44 | #include <linux/sched/task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 45 | #include <linux/slab.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 46 | #include <linux/spinlock.h> |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 47 | #include <linux/percpu-rwsem.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 48 | #include <linux/string.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 49 | #include <linux/hashtable.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 50 | #include <linux/idr.h> |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 51 | #include <linux/kthread.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 52 | #include <linux/atomic.h> |
Tejun Heo | e93ad19 | 2016-01-19 12:18:41 -0500 | [diff] [blame] | 53 | #include <linux/cpuset.h> |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 54 | #include <linux/proc_ns.h> |
| 55 | #include <linux/nsproxy.h> |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 56 | #include <linux/file.h> |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 57 | #include <linux/sched/cputime.h> |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 58 | #include <net/sock.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 59 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 60 | #define CREATE_TRACE_POINTS |
| 61 | #include <trace/events/cgroup.h> |
| 62 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 63 | #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ |
| 64 | MAX_CFTYPE_NAME + 2) |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 65 | /* let's not notify more than 100 times per second */ |
| 66 | #define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100) |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 67 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 68 | /* |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 69 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 70 | * hierarchy must be performed while holding it. |
| 71 | * |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 72 | * css_set_lock protects task->cgroups pointer, the list of css_set |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 73 | * objects, and the chain of tasks off each css_set. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 74 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 75 | * These locks are exported if CONFIG_PROVE_RCU so that accessors in |
| 76 | * cgroup.h can use them for lockdep annotations. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 77 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 78 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 79 | DEFINE_SPINLOCK(css_set_lock); |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 80 | |
| 81 | #ifdef CONFIG_PROVE_RCU |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 82 | EXPORT_SYMBOL_GPL(cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 83 | EXPORT_SYMBOL_GPL(css_set_lock); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 84 | #endif |
| 85 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 86 | /* |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 87 | * Protects cgroup_idr and css_idr so that IDs can be released without |
| 88 | * grabbing cgroup_mutex. |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 89 | */ |
| 90 | static DEFINE_SPINLOCK(cgroup_idr_lock); |
| 91 | |
| 92 | /* |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 93 | * Protects cgroup_file->kn for !self csses. It synchronizes notifications |
| 94 | * against file removal/re-creation across css hiding. |
| 95 | */ |
| 96 | static DEFINE_SPINLOCK(cgroup_file_kn_lock); |
| 97 | |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 98 | struct percpu_rw_semaphore cgroup_threadgroup_rwsem; |
| 99 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 100 | #define cgroup_assert_mutex_or_rcu_locked() \ |
Paul E. McKenney | f78f5b9 | 2015-06-18 15:50:02 -0700 | [diff] [blame] | 101 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ |
| 102 | !lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 103 | "cgroup_mutex or RCU read lock required"); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 104 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 105 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 106 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 107 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 108 | * destruction work items don't end up filling up max_active of system_wq |
| 109 | * which may lead to deadlock. |
| 110 | */ |
| 111 | static struct workqueue_struct *cgroup_destroy_wq; |
| 112 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 113 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 114 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 115 | struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 116 | #include <linux/cgroup_subsys.h> |
| 117 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 118 | #undef SUBSYS |
| 119 | |
| 120 | /* array of cgroup subsystem names */ |
| 121 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 122 | static const char *cgroup_subsys_name[] = { |
| 123 | #include <linux/cgroup_subsys.h> |
| 124 | }; |
| 125 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 126 | |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 127 | /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */ |
| 128 | #define SUBSYS(_x) \ |
| 129 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \ |
| 130 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \ |
| 131 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \ |
| 132 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key); |
| 133 | #include <linux/cgroup_subsys.h> |
| 134 | #undef SUBSYS |
| 135 | |
| 136 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key, |
| 137 | static struct static_key_true *cgroup_subsys_enabled_key[] = { |
| 138 | #include <linux/cgroup_subsys.h> |
| 139 | }; |
| 140 | #undef SUBSYS |
| 141 | |
| 142 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key, |
| 143 | static struct static_key_true *cgroup_subsys_on_dfl_key[] = { |
| 144 | #include <linux/cgroup_subsys.h> |
| 145 | }; |
| 146 | #undef SUBSYS |
| 147 | |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 148 | 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] | 149 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 150 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 151 | * The default hierarchy, reserved for the subsystems that are otherwise |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 152 | * unattached - it never has more than a single cgroup, and all tasks are |
| 153 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 154 | */ |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 155 | 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] | 156 | EXPORT_SYMBOL_GPL(cgrp_dfl_root); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 157 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 158 | /* |
| 159 | * The default hierarchy always exists but is hidden until mounted for the |
| 160 | * first time. This is for backward compatibility. |
| 161 | */ |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 162 | static bool cgrp_dfl_visible; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 163 | |
Tejun Heo | 5533e01 | 2014-05-14 19:33:07 -0400 | [diff] [blame] | 164 | /* some controllers are not supported in the default hierarchy */ |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 165 | static u16 cgrp_dfl_inhibit_ss_mask; |
Tejun Heo | 5533e01 | 2014-05-14 19:33:07 -0400 | [diff] [blame] | 166 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 167 | /* some controllers are implicitly enabled on the default hierarchy */ |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 168 | static u16 cgrp_dfl_implicit_ss_mask; |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 169 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 170 | /* some controllers can be threaded on the default hierarchy */ |
| 171 | static u16 cgrp_dfl_threaded_ss_mask; |
| 172 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 173 | /* The list of hierarchy roots */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 174 | LIST_HEAD(cgroup_roots); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 175 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 176 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 177 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 178 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 179 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 180 | /* |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 181 | * Assign a monotonically increasing serial number to csses. It guarantees |
| 182 | * cgroups with bigger numbers are newer than those with smaller numbers. |
| 183 | * Also, as csses are always appended to the parent's ->children list, it |
| 184 | * guarantees that sibling csses are always sorted in the ascending serial |
| 185 | * number order on the list. Protected by cgroup_mutex. |
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 | static u64 css_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 188 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 189 | /* |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 190 | * These bitmasks identify subsystems with specific features to avoid |
| 191 | * having to do iterative checks repeatedly. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 192 | */ |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 193 | static u16 have_fork_callback __read_mostly; |
| 194 | static u16 have_exit_callback __read_mostly; |
| 195 | static u16 have_free_callback __read_mostly; |
Tejun Heo | b807421 | 2017-01-20 12:06:08 -0500 | [diff] [blame] | 196 | static u16 have_canfork_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 197 | |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 198 | /* cgroup namespace for init task */ |
| 199 | struct cgroup_namespace init_cgroup_ns = { |
Elena Reshetova | 387ad96 | 2017-02-20 12:19:00 +0200 | [diff] [blame] | 200 | .count = REFCOUNT_INIT(2), |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 201 | .user_ns = &init_user_ns, |
| 202 | .ns.ops = &cgroupns_operations, |
| 203 | .ns.inum = PROC_CGROUP_INIT_INO, |
| 204 | .root_cset = &init_css_set, |
| 205 | }; |
| 206 | |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 207 | static struct file_system_type cgroup2_fs_type; |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 208 | static struct cftype cgroup_base_files[]; |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 209 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 210 | static int cgroup_apply_control(struct cgroup *cgrp); |
| 211 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 212 | static void css_task_iter_advance(struct css_task_iter *it); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 213 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 214 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 215 | struct cgroup_subsys *ss); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 216 | static void css_release(struct percpu_ref *ref); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 217 | static void kill_css(struct cgroup_subsys_state *css); |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 218 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 219 | struct cgroup *cgrp, struct cftype cfts[], |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 220 | bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 221 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 222 | /** |
| 223 | * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID |
| 224 | * @ssid: subsys ID of interest |
| 225 | * |
| 226 | * cgroup_subsys_enabled() can only be used with literal subsys names which |
| 227 | * is fine for individual subsystems but unsuitable for cgroup core. This |
| 228 | * is slower static_key_enabled() based test indexed by @ssid. |
| 229 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 230 | bool cgroup_ssid_enabled(int ssid) |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 231 | { |
Arnd Bergmann | cfe02a8 | 2016-03-15 00:21:06 +0100 | [diff] [blame] | 232 | if (CGROUP_SUBSYS_COUNT == 0) |
| 233 | return false; |
| 234 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 235 | return static_key_enabled(cgroup_subsys_enabled_key[ssid]); |
| 236 | } |
| 237 | |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 238 | /** |
| 239 | * cgroup_on_dfl - test whether a cgroup is on the default hierarchy |
| 240 | * @cgrp: the cgroup of interest |
| 241 | * |
| 242 | * The default hierarchy is the v2 interface of cgroup and this function |
| 243 | * can be used to test whether a cgroup is on the default hierarchy for |
| 244 | * cases where a subsystem should behave differnetly depending on the |
| 245 | * interface version. |
| 246 | * |
| 247 | * The set of behaviors which change on the default hierarchy are still |
| 248 | * being determined and the mount option is prefixed with __DEVEL__. |
| 249 | * |
| 250 | * List of changed behaviors: |
| 251 | * |
| 252 | * - Mount options "noprefix", "xattr", "clone_children", "release_agent" |
| 253 | * and "name" are disallowed. |
| 254 | * |
| 255 | * - When mounting an existing superblock, mount options should match. |
| 256 | * |
| 257 | * - Remount is disallowed. |
| 258 | * |
| 259 | * - rename(2) is disallowed. |
| 260 | * |
| 261 | * - "tasks" is removed. Everything should be at process granularity. Use |
| 262 | * "cgroup.procs" instead. |
| 263 | * |
| 264 | * - "cgroup.procs" is not sorted. pids will be unique unless they got |
| 265 | * recycled inbetween reads. |
| 266 | * |
| 267 | * - "release_agent" and "notify_on_release" are removed. Replacement |
| 268 | * notification mechanism will be implemented. |
| 269 | * |
| 270 | * - "cgroup.clone_children" is removed. |
| 271 | * |
| 272 | * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup |
| 273 | * and its descendants contain no task; otherwise, 1. The file also |
| 274 | * generates kernfs notification which can be monitored through poll and |
| 275 | * [di]notify when the value of the file changes. |
| 276 | * |
| 277 | * - cpuset: tasks will be kept in empty cpusets when hotplug happens and |
| 278 | * take masks of ancestors with non-empty cpus/mems, instead of being |
| 279 | * moved to an ancestor. |
| 280 | * |
| 281 | * - cpuset: a task can be moved into an empty cpuset, and again it takes |
| 282 | * masks of ancestors. |
| 283 | * |
| 284 | * - memcg: use_hierarchy is on by default and the cgroup file for the flag |
| 285 | * is not created. |
| 286 | * |
| 287 | * - blkcg: blk-throttle becomes properly hierarchical. |
| 288 | * |
| 289 | * - debug: disallowed on the default hierarchy. |
| 290 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 291 | bool cgroup_on_dfl(const struct cgroup *cgrp) |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 292 | { |
| 293 | return cgrp->root == &cgrp_dfl_root; |
| 294 | } |
| 295 | |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 296 | /* IDR wrappers which synchronize using cgroup_idr_lock */ |
| 297 | static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end, |
| 298 | gfp_t gfp_mask) |
| 299 | { |
| 300 | int ret; |
| 301 | |
| 302 | idr_preload(gfp_mask); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 303 | spin_lock_bh(&cgroup_idr_lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 304 | ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 305 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 306 | idr_preload_end(); |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id) |
| 311 | { |
| 312 | void *ret; |
| 313 | |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 314 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 315 | ret = idr_replace(idr, ptr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 316 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | static void cgroup_idr_remove(struct idr *idr, int id) |
| 321 | { |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 322 | spin_lock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 323 | idr_remove(idr, id); |
Tejun Heo | 54504e9 | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 324 | spin_unlock_bh(&cgroup_idr_lock); |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 325 | } |
| 326 | |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 327 | static bool cgroup_has_tasks(struct cgroup *cgrp) |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 328 | { |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 329 | return cgrp->nr_populated_csets; |
| 330 | } |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 331 | |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 332 | bool cgroup_is_threaded(struct cgroup *cgrp) |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 333 | { |
| 334 | return cgrp->dom_cgrp != cgrp; |
| 335 | } |
| 336 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 337 | /* can @cgrp host both domain and threaded children? */ |
| 338 | static bool cgroup_is_mixable(struct cgroup *cgrp) |
| 339 | { |
| 340 | /* |
| 341 | * Root isn't under domain level resource control exempting it from |
| 342 | * the no-internal-process constraint, so it can serve as a thread |
| 343 | * root and a parent of resource domains at the same time. |
| 344 | */ |
| 345 | return !cgroup_parent(cgrp); |
| 346 | } |
| 347 | |
| 348 | /* can @cgrp become a thread root? should always be true for a thread root */ |
| 349 | static bool cgroup_can_be_thread_root(struct cgroup *cgrp) |
| 350 | { |
| 351 | /* mixables don't care */ |
| 352 | if (cgroup_is_mixable(cgrp)) |
| 353 | return true; |
| 354 | |
| 355 | /* domain roots can't be nested under threaded */ |
| 356 | if (cgroup_is_threaded(cgrp)) |
| 357 | return false; |
| 358 | |
| 359 | /* can only have either domain or threaded children */ |
| 360 | if (cgrp->nr_populated_domain_children) |
| 361 | return false; |
| 362 | |
| 363 | /* and no domain controllers can be enabled */ |
| 364 | if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) |
| 365 | return false; |
| 366 | |
| 367 | return true; |
| 368 | } |
| 369 | |
| 370 | /* is @cgrp root of a threaded subtree? */ |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 371 | bool cgroup_is_thread_root(struct cgroup *cgrp) |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 372 | { |
| 373 | /* thread root should be a domain */ |
| 374 | if (cgroup_is_threaded(cgrp)) |
| 375 | return false; |
| 376 | |
| 377 | /* a domain w/ threaded children is a thread root */ |
| 378 | if (cgrp->nr_threaded_children) |
| 379 | return true; |
| 380 | |
| 381 | /* |
| 382 | * A domain which has tasks and explicit threaded controllers |
| 383 | * enabled is a thread root. |
| 384 | */ |
| 385 | if (cgroup_has_tasks(cgrp) && |
| 386 | (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask)) |
| 387 | return true; |
| 388 | |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | /* a domain which isn't connected to the root w/o brekage can't be used */ |
| 393 | static bool cgroup_is_valid_domain(struct cgroup *cgrp) |
| 394 | { |
| 395 | /* the cgroup itself can be a thread root */ |
| 396 | if (cgroup_is_threaded(cgrp)) |
| 397 | return false; |
| 398 | |
| 399 | /* but the ancestors can't be unless mixable */ |
| 400 | while ((cgrp = cgroup_parent(cgrp))) { |
| 401 | if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp)) |
| 402 | return false; |
| 403 | if (cgroup_is_threaded(cgrp)) |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | return true; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 408 | } |
| 409 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 410 | /* subsystems visibly enabled on a cgroup */ |
| 411 | static u16 cgroup_control(struct cgroup *cgrp) |
| 412 | { |
| 413 | struct cgroup *parent = cgroup_parent(cgrp); |
| 414 | u16 root_ss_mask = cgrp->root->subsys_mask; |
| 415 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 416 | if (parent) { |
| 417 | u16 ss_mask = parent->subtree_control; |
| 418 | |
| 419 | /* threaded cgroups can only have threaded controllers */ |
| 420 | if (cgroup_is_threaded(cgrp)) |
| 421 | ss_mask &= cgrp_dfl_threaded_ss_mask; |
| 422 | return ss_mask; |
| 423 | } |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 424 | |
| 425 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 426 | root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask | |
| 427 | cgrp_dfl_implicit_ss_mask); |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 428 | return root_ss_mask; |
| 429 | } |
| 430 | |
| 431 | /* subsystems enabled on a cgroup */ |
| 432 | static u16 cgroup_ss_mask(struct cgroup *cgrp) |
| 433 | { |
| 434 | struct cgroup *parent = cgroup_parent(cgrp); |
| 435 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 436 | if (parent) { |
| 437 | u16 ss_mask = parent->subtree_ss_mask; |
| 438 | |
| 439 | /* threaded cgroups can only have threaded controllers */ |
| 440 | if (cgroup_is_threaded(cgrp)) |
| 441 | ss_mask &= cgrp_dfl_threaded_ss_mask; |
| 442 | return ss_mask; |
| 443 | } |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 444 | |
| 445 | return cgrp->root->subsys_mask; |
| 446 | } |
| 447 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 448 | /** |
| 449 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 450 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 451 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 452 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 453 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 454 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 455 | * the caller is responsible for pinning the returned css if it wants to |
| 456 | * keep accessing it outside the said locks. This function may return |
| 457 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 458 | */ |
| 459 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 460 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 461 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 462 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 463 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 464 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 465 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 466 | return &cgrp->self; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 467 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 468 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 469 | /** |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 470 | * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem |
| 471 | * @cgrp: the cgroup of interest |
| 472 | * @ss: the subsystem of interest |
| 473 | * |
| 474 | * Find and get @cgrp's css assocaited with @ss. If the css doesn't exist |
| 475 | * or is offline, %NULL is returned. |
| 476 | */ |
| 477 | static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp, |
| 478 | struct cgroup_subsys *ss) |
| 479 | { |
| 480 | struct cgroup_subsys_state *css; |
| 481 | |
| 482 | rcu_read_lock(); |
| 483 | css = cgroup_css(cgrp, ss); |
| 484 | if (!css || !css_tryget_online(css)) |
| 485 | css = NULL; |
| 486 | rcu_read_unlock(); |
| 487 | |
| 488 | return css; |
| 489 | } |
| 490 | |
| 491 | /** |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 492 | * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem |
| 493 | * @cgrp: the cgroup of interest |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 494 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 495 | * |
Chen Hanxiao | d0f702e | 2015-04-23 07:57:33 -0400 | [diff] [blame] | 496 | * Similar to cgroup_css() but returns the effective css, which is defined |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 497 | * as the matching css of the nearest ancestor including self which has @ss |
| 498 | * enabled. If @ss is associated with the hierarchy @cgrp is on, this |
| 499 | * function is guaranteed to return non-NULL css. |
| 500 | */ |
| 501 | static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp, |
| 502 | struct cgroup_subsys *ss) |
| 503 | { |
| 504 | lockdep_assert_held(&cgroup_mutex); |
| 505 | |
| 506 | if (!ss) |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 507 | return &cgrp->self; |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 508 | |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 509 | /* |
| 510 | * This function is used while updating css associations and thus |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 511 | * can't test the csses directly. Test ss_mask. |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 512 | */ |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 513 | while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) { |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 514 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 515 | if (!cgrp) |
| 516 | return NULL; |
| 517 | } |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 518 | |
| 519 | return cgroup_css(cgrp, ss); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Tejun Heo | eeecbd1 | 2014-11-18 02:49:52 -0500 | [diff] [blame] | 522 | /** |
| 523 | * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem |
| 524 | * @cgrp: the cgroup of interest |
| 525 | * @ss: the subsystem of interest |
| 526 | * |
| 527 | * Find and get the effective css of @cgrp for @ss. The effective css is |
| 528 | * defined as the matching css of the nearest ancestor including self which |
| 529 | * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, |
| 530 | * the root css is returned, so this function always returns a valid css. |
| 531 | * The returned css must be put using css_put(). |
| 532 | */ |
| 533 | struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp, |
| 534 | struct cgroup_subsys *ss) |
| 535 | { |
| 536 | struct cgroup_subsys_state *css; |
| 537 | |
| 538 | rcu_read_lock(); |
| 539 | |
| 540 | do { |
| 541 | css = cgroup_css(cgrp, ss); |
| 542 | |
| 543 | if (css && css_tryget_online(css)) |
| 544 | goto out_unlock; |
| 545 | cgrp = cgroup_parent(cgrp); |
| 546 | } while (cgrp); |
| 547 | |
| 548 | css = init_css_set.subsys[ss->id]; |
| 549 | css_get(css); |
| 550 | out_unlock: |
| 551 | rcu_read_unlock(); |
| 552 | return css; |
| 553 | } |
| 554 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 555 | static void cgroup_get_live(struct cgroup *cgrp) |
Tejun Heo | 052c3f3 | 2015-10-15 16:41:50 -0400 | [diff] [blame] | 556 | { |
| 557 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
| 558 | css_get(&cgrp->self); |
| 559 | } |
| 560 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 561 | struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 562 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 563 | struct cgroup *cgrp = of->kn->parent->priv; |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 564 | struct cftype *cft = of_cft(of); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 565 | |
| 566 | /* |
| 567 | * This is open and unprotected implementation of cgroup_css(). |
| 568 | * seq_css() is only called from a kernfs file operation which has |
| 569 | * an active reference on the file. Because all the subsystem |
| 570 | * files are drained before a css is disassociated with a cgroup, |
| 571 | * the matching css from the cgroup's subsys table is guaranteed to |
| 572 | * be and stay valid until the enclosing operation is complete. |
| 573 | */ |
| 574 | if (cft->ss) |
| 575 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 576 | else |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 577 | return &cgrp->self; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 578 | } |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 579 | EXPORT_SYMBOL_GPL(of_css); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 580 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 581 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 582 | * for_each_css - iterate all css's of a cgroup |
| 583 | * @css: the iteration cursor |
| 584 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 585 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 586 | * |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 587 | * Should be called under cgroup_[tree_]mutex. |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 588 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 589 | #define for_each_css(css, ssid, cgrp) \ |
| 590 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 591 | if (!((css) = rcu_dereference_check( \ |
| 592 | (cgrp)->subsys[(ssid)], \ |
| 593 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 594 | else |
| 595 | |
| 596 | /** |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 597 | * for_each_e_css - iterate all effective css's of a cgroup |
| 598 | * @css: the iteration cursor |
| 599 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 600 | * @cgrp: the target cgroup to iterate css's of |
| 601 | * |
| 602 | * Should be called under cgroup_[tree_]mutex. |
| 603 | */ |
| 604 | #define for_each_e_css(css, ssid, cgrp) \ |
| 605 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 606 | if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \ |
| 607 | ; \ |
| 608 | else |
| 609 | |
| 610 | /** |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 611 | * do_each_subsys_mask - filter for_each_subsys with a bitmask |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 612 | * @ss: the iteration cursor |
| 613 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 614 | * @ss_mask: the bitmask |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 615 | * |
| 616 | * 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] | 617 | * @ss_mask is set. |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 618 | */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 619 | #define do_each_subsys_mask(ss, ssid, ss_mask) do { \ |
| 620 | unsigned long __ss_mask = (ss_mask); \ |
| 621 | if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \ |
Aleksa Sarai | 4a705c5 | 2015-06-09 21:32:07 +1000 | [diff] [blame] | 622 | (ssid) = 0; \ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 623 | break; \ |
| 624 | } \ |
| 625 | for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \ |
| 626 | (ss) = cgroup_subsys[ssid]; \ |
| 627 | { |
| 628 | |
| 629 | #define while_each_subsys_mask() \ |
| 630 | } \ |
| 631 | } \ |
| 632 | } while (false) |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 633 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 634 | /* iterate over child cgrps, lock should be held throughout iteration */ |
| 635 | #define cgroup_for_each_live_child(child, cgrp) \ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 636 | list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \ |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 637 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 638 | cgroup_is_dead(child); })) \ |
| 639 | ; \ |
| 640 | else |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 641 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 642 | /* walk live descendants in preorder */ |
| 643 | #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \ |
| 644 | css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \ |
| 645 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 646 | (dsct) = (d_css)->cgroup; \ |
| 647 | cgroup_is_dead(dsct); })) \ |
| 648 | ; \ |
| 649 | else |
| 650 | |
| 651 | /* walk live descendants in postorder */ |
| 652 | #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \ |
| 653 | css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \ |
| 654 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 655 | (dsct) = (d_css)->cgroup; \ |
| 656 | cgroup_is_dead(dsct); })) \ |
| 657 | ; \ |
| 658 | else |
| 659 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 660 | /* |
| 661 | * 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] | 662 | * hierarchies being mounted. It contains a pointer to the root state |
| 663 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 664 | * reference-counted, to improve performance when child cgroups |
| 665 | * haven't been created. |
| 666 | */ |
Tejun Heo | 5024ae2 | 2014-05-07 21:31:17 -0400 | [diff] [blame] | 667 | struct css_set init_css_set = { |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 668 | .refcount = REFCOUNT_INIT(1), |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 669 | .dom_cset = &init_css_set, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 670 | .tasks = LIST_HEAD_INIT(init_css_set.tasks), |
| 671 | .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 672 | .task_iters = LIST_HEAD_INIT(init_css_set.task_iters), |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 673 | .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets), |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 674 | .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 675 | .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node), |
| 676 | .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), |
Tejun Heo | 3868314 | 2017-09-25 13:50:20 -0700 | [diff] [blame] | 677 | |
| 678 | /* |
| 679 | * The following field is re-initialized when this cset gets linked |
| 680 | * in cgroup_init(). However, let's initialize the field |
| 681 | * statically too so that the default cgroup can be accessed safely |
| 682 | * early during boot. |
| 683 | */ |
| 684 | .dfl_cgrp = &cgrp_dfl_root.cgrp, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 685 | }; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 686 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 687 | static int css_set_count = 1; /* 1 for init_css_set */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 688 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 689 | static bool css_set_threaded(struct css_set *cset) |
| 690 | { |
| 691 | return cset->dom_cset != cset; |
| 692 | } |
| 693 | |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 694 | /** |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 695 | * css_set_populated - does a css_set contain any tasks? |
| 696 | * @cset: target css_set |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 697 | * |
| 698 | * css_set_populated() should be the same as !!cset->nr_tasks at steady |
| 699 | * state. However, css_set_populated() can be called while a task is being |
| 700 | * added to or removed from the linked list before the nr_tasks is |
| 701 | * properly updated. Hence, we can't just look at ->nr_tasks here. |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 702 | */ |
| 703 | static bool css_set_populated(struct css_set *cset) |
| 704 | { |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 705 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 706 | |
| 707 | return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks); |
| 708 | } |
| 709 | |
| 710 | /** |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 711 | * cgroup_update_populated - update the populated count of a cgroup |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 712 | * @cgrp: the target cgroup |
| 713 | * @populated: inc or dec populated count |
| 714 | * |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 715 | * 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] | 716 | * task or losing the last. Update @cgrp->nr_populated_* accordingly. The |
| 717 | * count is propagated towards root so that a given cgroup's |
| 718 | * nr_populated_children is zero iff none of its descendants contain any |
| 719 | * tasks. |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 720 | * |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 721 | * @cgrp's interface file "cgroup.populated" is zero if both |
| 722 | * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and |
| 723 | * 1 otherwise. When the sum changes from or to zero, userland is notified |
| 724 | * that the content of the interface file has changed. This can be used to |
| 725 | * detect when @cgrp and its descendants become populated or empty. |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 726 | */ |
| 727 | static void cgroup_update_populated(struct cgroup *cgrp, bool populated) |
| 728 | { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 729 | struct cgroup *child = NULL; |
| 730 | int adj = populated ? 1 : -1; |
| 731 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 732 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 733 | |
| 734 | do { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 735 | bool was_populated = cgroup_is_populated(cgrp); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 736 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 737 | if (!child) { |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 738 | cgrp->nr_populated_csets += adj; |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 739 | } else { |
| 740 | if (cgroup_is_threaded(child)) |
| 741 | cgrp->nr_populated_threaded_children += adj; |
| 742 | else |
| 743 | cgrp->nr_populated_domain_children += adj; |
| 744 | } |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 745 | |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 746 | if (was_populated == cgroup_is_populated(cgrp)) |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 747 | break; |
| 748 | |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 749 | cgroup1_check_for_release(cgrp); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 750 | cgroup_file_notify(&cgrp->events_file); |
| 751 | |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 752 | child = cgrp; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 753 | cgrp = cgroup_parent(cgrp); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 754 | } while (cgrp); |
| 755 | } |
| 756 | |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 757 | /** |
| 758 | * css_set_update_populated - update populated state of a css_set |
| 759 | * @cset: target css_set |
| 760 | * @populated: whether @cset is populated or depopulated |
| 761 | * |
| 762 | * @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] | 763 | * populated counters of all associated cgroups accordingly. |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 764 | */ |
| 765 | static void css_set_update_populated(struct css_set *cset, bool populated) |
| 766 | { |
| 767 | struct cgrp_cset_link *link; |
| 768 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 769 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 770 | |
| 771 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) |
| 772 | cgroup_update_populated(link->cgrp, populated); |
| 773 | } |
| 774 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 775 | /** |
| 776 | * css_set_move_task - move a task from one css_set to another |
| 777 | * @task: task being moved |
| 778 | * @from_cset: css_set @task currently belongs to (may be NULL) |
| 779 | * @to_cset: new css_set @task is being moved to (may be NULL) |
| 780 | * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks |
| 781 | * |
| 782 | * Move @task from @from_cset to @to_cset. If @task didn't belong to any |
| 783 | * css_set, @from_cset can be NULL. If @task is being disassociated |
| 784 | * instead of moved, @to_cset can be NULL. |
| 785 | * |
Tejun Heo | 788b950 | 2017-07-16 21:43:33 -0400 | [diff] [blame] | 786 | * This function automatically handles populated counter updates and |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 787 | * css_task_iter adjustments but the caller is responsible for managing |
| 788 | * @from_cset and @to_cset's reference counts. |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 789 | */ |
| 790 | static void css_set_move_task(struct task_struct *task, |
| 791 | struct css_set *from_cset, struct css_set *to_cset, |
| 792 | bool use_mg_tasks) |
| 793 | { |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 794 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 795 | |
Tejun Heo | 20b454a | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 796 | if (to_cset && !css_set_populated(to_cset)) |
| 797 | css_set_update_populated(to_cset, true); |
| 798 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 799 | if (from_cset) { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 800 | struct css_task_iter *it, *pos; |
| 801 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 802 | WARN_ON_ONCE(list_empty(&task->cg_list)); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 803 | |
| 804 | /* |
| 805 | * @task is leaving, advance task iterators which are |
| 806 | * pointing to it so that they can resume at the next |
| 807 | * position. Advancing an iterator might remove it from |
| 808 | * the list, use safe walk. See css_task_iter_advance*() |
| 809 | * for details. |
| 810 | */ |
| 811 | list_for_each_entry_safe(it, pos, &from_cset->task_iters, |
| 812 | iters_node) |
| 813 | if (it->task_pos == &task->cg_list) |
| 814 | css_task_iter_advance(it); |
| 815 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 816 | list_del_init(&task->cg_list); |
| 817 | if (!css_set_populated(from_cset)) |
| 818 | css_set_update_populated(from_cset, false); |
| 819 | } else { |
| 820 | WARN_ON_ONCE(!list_empty(&task->cg_list)); |
| 821 | } |
| 822 | |
| 823 | if (to_cset) { |
| 824 | /* |
| 825 | * We are synchronized through cgroup_threadgroup_rwsem |
| 826 | * against PF_EXITING setting such that we can't race |
| 827 | * against cgroup_exit() changing the css_set to |
| 828 | * init_css_set and dropping the old one. |
| 829 | */ |
| 830 | WARN_ON_ONCE(task->flags & PF_EXITING); |
| 831 | |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 832 | rcu_assign_pointer(task->cgroups, to_cset); |
| 833 | list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks : |
| 834 | &to_cset->tasks); |
| 835 | } |
| 836 | } |
| 837 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 838 | /* |
| 839 | * hash table for cgroup groups. This improves the performance to find |
| 840 | * an existing css_set. This hash doesn't (currently) take into |
| 841 | * account cgroups in empty hierarchies. |
| 842 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 843 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 844 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 845 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 846 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 847 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 848 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 849 | struct cgroup_subsys *ss; |
| 850 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 851 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 852 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 853 | key += (unsigned long)css[i]; |
| 854 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 855 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 856 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Tejun Heo | dcfe149 | 2016-12-27 14:49:09 -0500 | [diff] [blame] | 859 | void put_css_set_locked(struct css_set *cset) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 860 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 861 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 862 | struct cgroup_subsys *ss; |
| 863 | int ssid; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 864 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 865 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 866 | |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 867 | if (!refcount_dec_and_test(&cset->refcount)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 868 | return; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 869 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 870 | WARN_ON_ONCE(!list_empty(&cset->threaded_csets)); |
| 871 | |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 872 | /* This css_set is dead. unlink it and release cgroup and css refs */ |
| 873 | for_each_subsys(ss, ssid) { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 874 | list_del(&cset->e_cset_node[ssid]); |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 875 | css_put(cset->subsys[ssid]); |
| 876 | } |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 877 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 878 | css_set_count--; |
| 879 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 880 | 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] | 881 | list_del(&link->cset_link); |
| 882 | list_del(&link->cgrp_link); |
Tejun Heo | 2ceb231 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 883 | if (cgroup_parent(link->cgrp)) |
| 884 | cgroup_put(link->cgrp); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 885 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 886 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 887 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 888 | if (css_set_threaded(cset)) { |
| 889 | list_del(&cset->threaded_csets_node); |
| 890 | put_css_set_locked(cset->dom_cset); |
| 891 | } |
| 892 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 893 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 896 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 897 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 898 | * @cset: candidate css_set being tested |
| 899 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 900 | * @new_cgrp: cgroup that's being entered by the task |
| 901 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 902 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 903 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 904 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 905 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 906 | static bool compare_css_sets(struct css_set *cset, |
| 907 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 908 | struct cgroup *new_cgrp, |
| 909 | struct cgroup_subsys_state *template[]) |
| 910 | { |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 911 | struct cgroup *new_dfl_cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 912 | struct list_head *l1, *l2; |
| 913 | |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 914 | /* |
| 915 | * On the default hierarchy, there can be csets which are |
| 916 | * associated with the same set of cgroups but different csses. |
| 917 | * Let's first ensure that csses match. |
| 918 | */ |
| 919 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 920 | return false; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 921 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 922 | |
| 923 | /* @cset's domain should match the default cgroup's */ |
| 924 | if (cgroup_on_dfl(new_cgrp)) |
| 925 | new_dfl_cgrp = new_cgrp; |
| 926 | else |
| 927 | new_dfl_cgrp = old_cset->dfl_cgrp; |
| 928 | |
| 929 | if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp) |
| 930 | return false; |
| 931 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 932 | /* |
| 933 | * Compare cgroup pointers in order to distinguish between |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 934 | * different cgroups in hierarchies. As different cgroups may |
| 935 | * share the same effective css, this comparison is always |
| 936 | * necessary. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 937 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 938 | l1 = &cset->cgrp_links; |
| 939 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 940 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 941 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 942 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 943 | |
| 944 | l1 = l1->next; |
| 945 | l2 = l2->next; |
| 946 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 947 | if (l1 == &cset->cgrp_links) { |
| 948 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 949 | break; |
| 950 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 951 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 952 | } |
| 953 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 954 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 955 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 956 | cgrp1 = link1->cgrp; |
| 957 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 958 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 959 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 960 | |
| 961 | /* |
| 962 | * If this hierarchy is the hierarchy of the cgroup |
| 963 | * that's changing, then we need to check that this |
| 964 | * css_set points to the new cgroup; if it's any other |
| 965 | * hierarchy, then this css_set should point to the |
| 966 | * same cgroup as the old css_set. |
| 967 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 968 | if (cgrp1->root == new_cgrp->root) { |
| 969 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 970 | return false; |
| 971 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 972 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 973 | return false; |
| 974 | } |
| 975 | } |
| 976 | return true; |
| 977 | } |
| 978 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 979 | /** |
| 980 | * find_existing_css_set - init css array and find the matching css_set |
| 981 | * @old_cset: the css_set that we're using before the cgroup transition |
| 982 | * @cgrp: the cgroup that we're moving into |
| 983 | * @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] | 984 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 985 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 986 | struct cgroup *cgrp, |
| 987 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 988 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 989 | struct cgroup_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 990 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 991 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 992 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 993 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 994 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 995 | /* |
| 996 | * Build the set of subsystem state objects that we want to see in the |
| 997 | * new css_set. while subsystems can change globally, the entries here |
| 998 | * won't change, so no need for locking. |
| 999 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1000 | for_each_subsys(ss, i) { |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1001 | if (root->subsys_mask & (1UL << i)) { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1002 | /* |
| 1003 | * @ss is in this hierarchy, so we want the |
| 1004 | * effective css from @cgrp. |
| 1005 | */ |
| 1006 | template[i] = cgroup_e_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1007 | } else { |
Tejun Heo | aec3dfc | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1008 | /* |
| 1009 | * @ss is not in this hierarchy, so we don't want |
| 1010 | * to change the css. |
| 1011 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1012 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1016 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1017 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 1018 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1019 | continue; |
| 1020 | |
| 1021 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1022 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1023 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1024 | |
| 1025 | /* No existing cgroup group matched */ |
| 1026 | return NULL; |
| 1027 | } |
| 1028 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1029 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1030 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1031 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1032 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1033 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 1034 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1035 | kfree(link); |
| 1036 | } |
| 1037 | } |
| 1038 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1039 | /** |
| 1040 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 1041 | * @count: the number of links to allocate |
| 1042 | * @tmp_links: list_head the allocated links are put on |
| 1043 | * |
| 1044 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 1045 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1046 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1047 | 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] | 1048 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1049 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1050 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1051 | |
| 1052 | INIT_LIST_HEAD(tmp_links); |
| 1053 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1054 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 1055 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1056 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1057 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1058 | return -ENOMEM; |
| 1059 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1060 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 1061 | } |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1065 | /** |
| 1066 | * 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] | 1067 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1068 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1069 | * @cgrp: the destination cgroup |
| 1070 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1071 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 1072 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1073 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1074 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1075 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1076 | BUG_ON(list_empty(tmp_links)); |
Tejun Heo | 6803c00 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1077 | |
| 1078 | if (cgroup_on_dfl(cgrp)) |
| 1079 | cset->dfl_cgrp = cgrp; |
| 1080 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1081 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 1082 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1083 | link->cgrp = cgrp; |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 1084 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1085 | /* |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1086 | * Always add links to the tail of the lists so that the lists are |
| 1087 | * in choronological order. |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1088 | */ |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1089 | list_move_tail(&link->cset_link, &cgrp->cset_links); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1090 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Tejun Heo | 2ceb231 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1091 | |
| 1092 | if (cgroup_parent(cgrp)) |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 1093 | cgroup_get_live(cgrp); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1096 | /** |
| 1097 | * find_css_set - return a new css_set with one cgroup updated |
| 1098 | * @old_cset: the baseline css_set |
| 1099 | * @cgrp: the cgroup to be updated |
| 1100 | * |
| 1101 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 1102 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1103 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1104 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 1105 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1106 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1107 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1108 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1109 | struct list_head tmp_links; |
| 1110 | struct cgrp_cset_link *link; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1111 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1112 | unsigned long key; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1113 | int ssid; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1114 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1115 | lockdep_assert_held(&cgroup_mutex); |
| 1116 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1117 | /* First see if we already have a cgroup group that matches |
| 1118 | * the desired set */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1119 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1120 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 1121 | if (cset) |
| 1122 | get_css_set(cset); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1123 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1124 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1125 | if (cset) |
| 1126 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1127 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 1128 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1129 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1130 | return NULL; |
| 1131 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1132 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1133 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1134 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1135 | return NULL; |
| 1136 | } |
| 1137 | |
Elena Reshetova | 4b9502e6 | 2017-03-08 10:00:40 +0200 | [diff] [blame] | 1138 | refcount_set(&cset->refcount, 1); |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1139 | cset->dom_cset = cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1140 | INIT_LIST_HEAD(&cset->tasks); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1141 | INIT_LIST_HEAD(&cset->mg_tasks); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 1142 | INIT_LIST_HEAD(&cset->task_iters); |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1143 | INIT_LIST_HEAD(&cset->threaded_csets); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1144 | INIT_HLIST_NODE(&cset->hlist); |
Tejun Heo | 5f617ebb | 2016-12-27 14:49:05 -0500 | [diff] [blame] | 1145 | INIT_LIST_HEAD(&cset->cgrp_links); |
| 1146 | INIT_LIST_HEAD(&cset->mg_preload_node); |
| 1147 | INIT_LIST_HEAD(&cset->mg_node); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1148 | |
| 1149 | /* Copy the set of subsystem state objects generated in |
| 1150 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1151 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1152 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1153 | spin_lock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1154 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1155 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1156 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1157 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1158 | if (c->root == cgrp->root) |
| 1159 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1160 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1161 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1162 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1163 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1164 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1165 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1166 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1167 | /* Add @cset to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1168 | key = css_set_hash(cset->subsys); |
| 1169 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 1170 | |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 1171 | for_each_subsys(ss, ssid) { |
| 1172 | struct cgroup_subsys_state *css = cset->subsys[ssid]; |
| 1173 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1174 | list_add_tail(&cset->e_cset_node[ssid], |
Tejun Heo | 53254f9 | 2015-11-23 14:55:41 -0500 | [diff] [blame] | 1175 | &css->cgroup->e_csets[ssid]); |
| 1176 | css_get(css); |
| 1177 | } |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1178 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1179 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1180 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1181 | /* |
| 1182 | * If @cset should be threaded, look up the matching dom_cset and |
| 1183 | * link them up. We first fully initialize @cset then look for the |
| 1184 | * dom_cset. It's simpler this way and safe as @cset is guaranteed |
| 1185 | * to stay empty until we return. |
| 1186 | */ |
| 1187 | if (cgroup_is_threaded(cset->dfl_cgrp)) { |
| 1188 | struct css_set *dcset; |
| 1189 | |
| 1190 | dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp); |
| 1191 | if (!dcset) { |
| 1192 | put_css_set(cset); |
| 1193 | return NULL; |
| 1194 | } |
| 1195 | |
| 1196 | spin_lock_irq(&css_set_lock); |
| 1197 | cset->dom_cset = dcset; |
| 1198 | list_add_tail(&cset->threaded_csets_node, |
| 1199 | &dcset->threaded_csets); |
| 1200 | spin_unlock_irq(&css_set_lock); |
| 1201 | } |
| 1202 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1203 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1206 | struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1207 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1208 | struct cgroup *root_cgrp = kf_root->kn->priv; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1209 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1210 | return root_cgrp->root; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1211 | } |
| 1212 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1213 | static int cgroup_init_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1214 | { |
| 1215 | int id; |
| 1216 | |
| 1217 | lockdep_assert_held(&cgroup_mutex); |
| 1218 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1219 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1220 | if (id < 0) |
| 1221 | return id; |
| 1222 | |
| 1223 | root->hierarchy_id = id; |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1227 | static void cgroup_exit_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1228 | { |
| 1229 | lockdep_assert_held(&cgroup_mutex); |
| 1230 | |
Johannes Weiner | 8c8a550 | 2016-06-17 12:23:59 -0400 | [diff] [blame] | 1231 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1232 | } |
| 1233 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1234 | void cgroup_free_root(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1235 | { |
| 1236 | if (root) { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1237 | idr_destroy(&root->cgroup_idr); |
| 1238 | kfree(root); |
| 1239 | } |
| 1240 | } |
| 1241 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1242 | static void cgroup_destroy_root(struct cgroup_root *root) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1243 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1244 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1245 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1246 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 1247 | trace_cgroup_destroy_root(root); |
| 1248 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1249 | cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1250 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1251 | BUG_ON(atomic_read(&root->nr_cgrps)); |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1252 | BUG_ON(!list_empty(&cgrp->self.children)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1253 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1254 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1255 | WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1256 | |
| 1257 | /* |
| 1258 | * Release all the links from cset_links to this hierarchy's |
| 1259 | * root cgroup |
| 1260 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1261 | spin_lock_irq(&css_set_lock); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1262 | |
| 1263 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1264 | list_del(&link->cset_link); |
| 1265 | list_del(&link->cgrp_link); |
| 1266 | kfree(link); |
| 1267 | } |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1268 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1269 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1270 | |
| 1271 | if (!list_empty(&root->root_list)) { |
| 1272 | list_del(&root->root_list); |
| 1273 | cgroup_root_count--; |
| 1274 | } |
| 1275 | |
| 1276 | cgroup_exit_root_id(root); |
| 1277 | |
| 1278 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1279 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1280 | kernfs_destroy_root(root->kf_root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1281 | cgroup_free_root(root); |
| 1282 | } |
| 1283 | |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1284 | /* |
| 1285 | * look up cgroup associated with current task's cgroup namespace on the |
| 1286 | * specified hierarchy |
| 1287 | */ |
| 1288 | static struct cgroup * |
| 1289 | current_cgns_cgroup_from_root(struct cgroup_root *root) |
| 1290 | { |
| 1291 | struct cgroup *res = NULL; |
| 1292 | struct css_set *cset; |
| 1293 | |
| 1294 | lockdep_assert_held(&css_set_lock); |
| 1295 | |
| 1296 | rcu_read_lock(); |
| 1297 | |
| 1298 | cset = current->nsproxy->cgroup_ns->root_cset; |
| 1299 | if (cset == &init_css_set) { |
| 1300 | res = &root->cgrp; |
| 1301 | } else { |
| 1302 | struct cgrp_cset_link *link; |
| 1303 | |
| 1304 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
| 1305 | struct cgroup *c = link->cgrp; |
| 1306 | |
| 1307 | if (c->root == root) { |
| 1308 | res = c; |
| 1309 | break; |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | rcu_read_unlock(); |
| 1314 | |
| 1315 | BUG_ON(!res); |
| 1316 | return res; |
| 1317 | } |
| 1318 | |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1319 | /* look up cgroup associated with given css_set on the specified hierarchy */ |
| 1320 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1321 | struct cgroup_root *root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1322 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1323 | struct cgroup *res = NULL; |
| 1324 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1325 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1326 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1327 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1328 | if (cset == &init_css_set) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1329 | res = &root->cgrp; |
Tejun Heo | 13d82fb | 2017-08-02 15:39:38 -0700 | [diff] [blame] | 1330 | } else if (root == &cgrp_dfl_root) { |
| 1331 | res = cset->dfl_cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1332 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1333 | struct cgrp_cset_link *link; |
| 1334 | |
| 1335 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1336 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1337 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1338 | if (c->root == root) { |
| 1339 | res = c; |
| 1340 | break; |
| 1341 | } |
| 1342 | } |
| 1343 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1344 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1345 | BUG_ON(!res); |
| 1346 | return res; |
| 1347 | } |
| 1348 | |
| 1349 | /* |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1350 | * Return the cgroup for "task" from the given hierarchy. Must be |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1351 | * called with cgroup_mutex and css_set_lock held. |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1352 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1353 | struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 1354 | struct cgroup_root *root) |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 1355 | { |
| 1356 | /* |
| 1357 | * No need to lock the task - since we hold cgroup_mutex the |
| 1358 | * task can't change groups, so the only thing that can happen |
| 1359 | * is that it exits and its css is set back to init_css_set. |
| 1360 | */ |
| 1361 | return cset_cgroup_from_root(task_css_set(task), root); |
| 1362 | } |
| 1363 | |
| 1364 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1365 | * A task must hold cgroup_mutex to modify cgroups. |
| 1366 | * |
| 1367 | * Any task can increment and decrement the count field without lock. |
| 1368 | * So in general, code holding cgroup_mutex can't rely on the count |
| 1369 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1370 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1371 | * means that no tasks are currently attached, therefore there is no |
| 1372 | * way a task attached to that cgroup can fork (the other way to |
| 1373 | * increment the count). So code holding cgroup_mutex can safely |
| 1374 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 1375 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 1376 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 1377 | * needs that mutex. |
| 1378 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1379 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 1380 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 1381 | * 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] | 1382 | * least one task in the system (init, pid == 1), therefore, root cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1383 | * 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] | 1384 | * need a special hack to ensure that root cgroup cannot be deleted. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1385 | * |
| 1386 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1387 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1388 | */ |
| 1389 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1390 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 1391 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1392 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 1393 | char *buf) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1394 | { |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 1395 | struct cgroup_subsys *ss = cft->ss; |
| 1396 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1397 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 1398 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) |
| 1399 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s", |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 1400 | cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name, |
| 1401 | cft->name); |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1402 | else |
Tejun Heo | 08a7767 | 2018-01-09 07:21:15 -0800 | [diff] [blame] | 1403 | strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1404 | return buf; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1407 | /** |
| 1408 | * cgroup_file_mode - deduce file mode of a control file |
| 1409 | * @cft: the control file in question |
| 1410 | * |
Tejun Heo | 7dbdb19 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1411 | * S_IRUGO for read, S_IWUSR for write. |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1412 | */ |
| 1413 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1414 | { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1415 | umode_t mode = 0; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1416 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1417 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 1418 | mode |= S_IRUGO; |
| 1419 | |
Tejun Heo | 7dbdb19 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1420 | if (cft->write_u64 || cft->write_s64 || cft->write) { |
| 1421 | if (cft->flags & CFTYPE_WORLD_WRITABLE) |
| 1422 | mode |= S_IWUGO; |
| 1423 | else |
| 1424 | mode |= S_IWUSR; |
| 1425 | } |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1426 | |
| 1427 | return mode; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1428 | } |
| 1429 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1430 | /** |
Tejun Heo | 8699b77 | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1431 | * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1432 | * @subtree_control: the new subtree_control mask to consider |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1433 | * @this_ss_mask: available subsystems |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1434 | * |
| 1435 | * On the default hierarchy, a subsystem may request other subsystems to be |
| 1436 | * enabled together through its ->depends_on mask. In such cases, more |
| 1437 | * subsystems than specified in "cgroup.subtree_control" may be enabled. |
| 1438 | * |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1439 | * This function calculates which subsystems need to be enabled if |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1440 | * @subtree_control is to be applied while restricted to @this_ss_mask. |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1441 | */ |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1442 | 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] | 1443 | { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 1444 | u16 cur_ss_mask = subtree_control; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1445 | struct cgroup_subsys *ss; |
| 1446 | int ssid; |
| 1447 | |
| 1448 | lockdep_assert_held(&cgroup_mutex); |
| 1449 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 1450 | cur_ss_mask |= cgrp_dfl_implicit_ss_mask; |
| 1451 | |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1452 | while (true) { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 1453 | u16 new_ss_mask = cur_ss_mask; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1454 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1455 | do_each_subsys_mask(ss, ssid, cur_ss_mask) { |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 1456 | new_ss_mask |= ss->depends_on; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1457 | } while_each_subsys_mask(); |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1458 | |
| 1459 | /* |
| 1460 | * Mask out subsystems which aren't available. This can |
| 1461 | * happen only if some depended-upon subsystems were bound |
| 1462 | * to non-default hierarchies. |
| 1463 | */ |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1464 | new_ss_mask &= this_ss_mask; |
Tejun Heo | af0ba67 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1465 | |
| 1466 | if (new_ss_mask == cur_ss_mask) |
| 1467 | break; |
| 1468 | cur_ss_mask = new_ss_mask; |
| 1469 | } |
| 1470 | |
Tejun Heo | 0f060de | 2014-11-18 02:49:50 -0500 | [diff] [blame] | 1471 | return cur_ss_mask; |
| 1472 | } |
| 1473 | |
| 1474 | /** |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1475 | * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods |
| 1476 | * @kn: the kernfs_node being serviced |
| 1477 | * |
| 1478 | * This helper undoes cgroup_kn_lock_live() and should be invoked before |
| 1479 | * the method finishes if locking succeeded. Note that once this function |
| 1480 | * returns the cgroup returned by cgroup_kn_lock_live() may become |
| 1481 | * inaccessible any time. If the caller intends to continue to access the |
| 1482 | * cgroup, it should pin it before invoking this function. |
| 1483 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1484 | void cgroup_kn_unlock(struct kernfs_node *kn) |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1485 | { |
| 1486 | struct cgroup *cgrp; |
| 1487 | |
| 1488 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1489 | cgrp = kn->priv; |
| 1490 | else |
| 1491 | cgrp = kn->parent->priv; |
| 1492 | |
| 1493 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1494 | |
| 1495 | kernfs_unbreak_active_protection(kn); |
| 1496 | cgroup_put(cgrp); |
| 1497 | } |
| 1498 | |
| 1499 | /** |
| 1500 | * cgroup_kn_lock_live - locking helper for cgroup kernfs methods |
| 1501 | * @kn: the kernfs_node being serviced |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1502 | * @drain_offline: perform offline draining on the cgroup |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1503 | * |
| 1504 | * This helper is to be used by a cgroup kernfs method currently servicing |
| 1505 | * @kn. It breaks the active protection, performs cgroup locking and |
| 1506 | * verifies that the associated cgroup is alive. Returns the cgroup if |
| 1507 | * alive; otherwise, %NULL. A successful return should be undone by a |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1508 | * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the |
| 1509 | * cgroup is drained of offlining csses before return. |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1510 | * |
| 1511 | * Any cgroup kernfs method implementation which requires locking the |
| 1512 | * associated cgroup should use this helper. It avoids nesting cgroup |
| 1513 | * locking under kernfs active protection and allows all kernfs operations |
| 1514 | * including self-removal. |
| 1515 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1516 | 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] | 1517 | { |
| 1518 | struct cgroup *cgrp; |
| 1519 | |
| 1520 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1521 | cgrp = kn->priv; |
| 1522 | else |
| 1523 | cgrp = kn->parent->priv; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1524 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1525 | /* |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1526 | * We're gonna grab cgroup_mutex which nests outside kernfs |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1527 | * active_ref. cgroup liveliness check alone provides enough |
| 1528 | * protection against removal. Ensure @cgrp stays accessible and |
| 1529 | * break the active_ref protection. |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1530 | */ |
Li Zefan | aa32362 | 2014-09-04 14:43:38 +0800 | [diff] [blame] | 1531 | if (!cgroup_tryget(cgrp)) |
| 1532 | return NULL; |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1533 | kernfs_break_active_protection(kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1534 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1535 | if (drain_offline) |
| 1536 | cgroup_lock_and_drain_offline(cgrp); |
| 1537 | else |
| 1538 | mutex_lock(&cgroup_mutex); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 1539 | |
| 1540 | if (!cgroup_is_dead(cgrp)) |
| 1541 | return cgrp; |
| 1542 | |
| 1543 | cgroup_kn_unlock(kn); |
| 1544 | return NULL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 1547 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1548 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1549 | char name[CGROUP_FILE_NAME_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1550 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 1551 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 1552 | |
| 1553 | if (cft->file_offset) { |
| 1554 | struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss); |
| 1555 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 1556 | |
| 1557 | spin_lock_irq(&cgroup_file_kn_lock); |
| 1558 | cfile->kn = NULL; |
| 1559 | spin_unlock_irq(&cgroup_file_kn_lock); |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1560 | |
| 1561 | del_timer_sync(&cfile->notify_timer); |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 1562 | } |
| 1563 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1564 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1567 | /** |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1568 | * css_clear_dir - remove subsys files in a cgroup directory |
| 1569 | * @css: taget css |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1570 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1571 | static void css_clear_dir(struct cgroup_subsys_state *css) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1572 | { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1573 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1574 | struct cftype *cfts; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1575 | |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1576 | if (!(css->flags & CSS_VISIBLE)) |
| 1577 | return; |
| 1578 | |
| 1579 | css->flags &= ~CSS_VISIBLE; |
| 1580 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1581 | if (!css->ss) { |
| 1582 | if (cgroup_on_dfl(cgrp)) |
| 1583 | cfts = cgroup_base_files; |
| 1584 | else |
| 1585 | cfts = cgroup1_base_files; |
| 1586 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1587 | cgroup_addrm_files(css, cgrp, cfts, false); |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1588 | } else { |
| 1589 | list_for_each_entry(cfts, &css->ss->cfts, node) |
| 1590 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1591 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1594 | /** |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1595 | * css_populate_dir - create subsys files in a cgroup directory |
| 1596 | * @css: target css |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1597 | * |
| 1598 | * On failure, no file is added. |
| 1599 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1600 | static int css_populate_dir(struct cgroup_subsys_state *css) |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1601 | { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1602 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1603 | struct cftype *cfts, *failed_cfts; |
| 1604 | int ret; |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1605 | |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 1606 | if ((css->flags & CSS_VISIBLE) || !cgrp->kn) |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1607 | return 0; |
| 1608 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1609 | if (!css->ss) { |
| 1610 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1611 | cfts = cgroup_base_files; |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1612 | else |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1613 | cfts = cgroup1_base_files; |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1614 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1615 | ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true); |
| 1616 | if (ret < 0) |
| 1617 | return ret; |
| 1618 | } else { |
| 1619 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1620 | ret = cgroup_addrm_files(css, cgrp, cfts, true); |
| 1621 | if (ret < 0) { |
| 1622 | failed_cfts = cfts; |
| 1623 | goto err; |
| 1624 | } |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1625 | } |
| 1626 | } |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 1627 | |
| 1628 | css->flags |= CSS_VISIBLE; |
| 1629 | |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1630 | return 0; |
| 1631 | err: |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1632 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1633 | if (cfts == failed_cfts) |
| 1634 | break; |
| 1635 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1636 | } |
Tejun Heo | ccdca21 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1637 | return ret; |
| 1638 | } |
| 1639 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 1640 | int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1641 | { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1642 | struct cgroup *dcgrp = &dst_root->cgrp; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1643 | struct cgroup_subsys *ss; |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1644 | int ssid, i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1645 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1646 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1647 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1648 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 1649 | /* |
| 1650 | * If @ss has non-root csses attached to it, can't move. |
| 1651 | * If @ss is an implicit controller, it is exempt from this |
| 1652 | * rule and can be stolen. |
| 1653 | */ |
| 1654 | if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) && |
| 1655 | !ss->implicit_on_dfl) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1656 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1657 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1658 | /* can't move between two non-dummy roots either */ |
Tejun Heo | 7fd8c56 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1659 | if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1660 | return -EBUSY; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1661 | } while_each_subsys_mask(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1662 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1663 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1664 | struct cgroup_root *src_root = ss->root; |
| 1665 | struct cgroup *scgrp = &src_root->cgrp; |
| 1666 | struct cgroup_subsys_state *css = cgroup_css(scgrp, ss); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1667 | struct css_set *cset; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1668 | |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1669 | WARN_ON(!css || cgroup_css(dcgrp, ss)); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1670 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1671 | /* disable from the source */ |
| 1672 | src_root->subsys_mask &= ~(1 << ssid); |
| 1673 | WARN_ON(cgroup_apply_control(scgrp)); |
| 1674 | cgroup_finalize_control(scgrp, 0); |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1675 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1676 | /* rebind */ |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1677 | RCU_INIT_POINTER(scgrp->subsys[ssid], NULL); |
| 1678 | rcu_assign_pointer(dcgrp->subsys[ssid], css); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1679 | ss->root = dst_root; |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1680 | css->cgroup = dcgrp; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1681 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1682 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1683 | hash_for_each(css_set_table, i, cset, hlist) |
| 1684 | list_move_tail(&cset->e_cset_node[ss->id], |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1685 | &dcgrp->e_csets[ss->id]); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1686 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1687 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1688 | /* default hierarchy doesn't enable controllers by default */ |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 1689 | dst_root->subsys_mask |= 1 << ssid; |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1690 | if (dst_root == &cgrp_dfl_root) { |
| 1691 | static_branch_enable(cgroup_subsys_on_dfl_key[ssid]); |
| 1692 | } else { |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1693 | dcgrp->subtree_control |= 1 << ssid; |
Tejun Heo | 49d1dc4 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1694 | static_branch_disable(cgroup_subsys_on_dfl_key[ssid]); |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 1695 | } |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1696 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1697 | ret = cgroup_apply_control(dcgrp); |
| 1698 | if (ret) |
| 1699 | pr_warn("partial failure to rebind %s controller (err=%d)\n", |
| 1700 | ss->name, ret); |
| 1701 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1702 | if (ss->bind) |
| 1703 | ss->bind(css); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 1704 | } while_each_subsys_mask(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1705 | |
Tejun Heo | 1ada483 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 1706 | kernfs_activate(dcgrp->kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1707 | return 0; |
| 1708 | } |
| 1709 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1710 | int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node, |
| 1711 | struct kernfs_root *kf_root) |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1712 | { |
Felipe Balbi | 09be4c8 | 2016-05-12 12:34:38 +0300 | [diff] [blame] | 1713 | int len = 0; |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1714 | char *buf = NULL; |
| 1715 | struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root); |
| 1716 | struct cgroup *ns_cgroup; |
| 1717 | |
| 1718 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 1719 | if (!buf) |
| 1720 | return -ENOMEM; |
| 1721 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1722 | spin_lock_irq(&css_set_lock); |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1723 | ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot); |
| 1724 | 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] | 1725 | spin_unlock_irq(&css_set_lock); |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 1726 | |
| 1727 | if (len >= PATH_MAX) |
| 1728 | len = -ERANGE; |
| 1729 | else if (len > 0) { |
| 1730 | seq_escape(sf, buf, " \t\n\\"); |
| 1731 | len = 0; |
| 1732 | } |
| 1733 | kfree(buf); |
| 1734 | return len; |
| 1735 | } |
| 1736 | |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1737 | static int parse_cgroup_root_flags(char *data, unsigned int *root_flags) |
| 1738 | { |
| 1739 | char *token; |
| 1740 | |
| 1741 | *root_flags = 0; |
| 1742 | |
| 1743 | if (!data) |
| 1744 | return 0; |
| 1745 | |
| 1746 | while ((token = strsep(&data, ",")) != NULL) { |
| 1747 | if (!strcmp(token, "nsdelegate")) { |
| 1748 | *root_flags |= CGRP_ROOT_NS_DELEGATE; |
| 1749 | continue; |
| 1750 | } |
| 1751 | |
| 1752 | pr_err("cgroup2: unknown option \"%s\"\n", token); |
| 1753 | return -EINVAL; |
| 1754 | } |
| 1755 | |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | static void apply_cgroup_root_flags(unsigned int root_flags) |
| 1760 | { |
| 1761 | if (current->nsproxy->cgroup_ns == &init_cgroup_ns) { |
| 1762 | if (root_flags & CGRP_ROOT_NS_DELEGATE) |
| 1763 | cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE; |
| 1764 | else |
| 1765 | cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE; |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root) |
| 1770 | { |
| 1771 | if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) |
| 1772 | seq_puts(seq, ",nsdelegate"); |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1776 | static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1777 | { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 1778 | unsigned int root_flags; |
| 1779 | int ret; |
| 1780 | |
| 1781 | ret = parse_cgroup_root_flags(data, &root_flags); |
| 1782 | if (ret) |
| 1783 | return ret; |
| 1784 | |
| 1785 | apply_cgroup_root_flags(root_flags); |
| 1786 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1789 | /* |
| 1790 | * To reduce the fork() overhead for systems that are not actually using |
| 1791 | * their cgroups capability, we don't maintain the lists running through |
| 1792 | * each css_set to its tasks until we see the list actually used - in other |
| 1793 | * words after the first mount. |
| 1794 | */ |
| 1795 | static bool use_task_css_set_links __read_mostly; |
| 1796 | |
| 1797 | static void cgroup_enable_task_cg_lists(void) |
| 1798 | { |
| 1799 | struct task_struct *p, *g; |
| 1800 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1801 | spin_lock_irq(&css_set_lock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1802 | |
| 1803 | if (use_task_css_set_links) |
| 1804 | goto out_unlock; |
| 1805 | |
| 1806 | use_task_css_set_links = true; |
| 1807 | |
| 1808 | /* |
| 1809 | * We need tasklist_lock because RCU is not safe against |
| 1810 | * while_each_thread(). Besides, a forking task that has passed |
| 1811 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 1812 | * is not guaranteed to have its child immediately visible in the |
| 1813 | * tasklist if we walk through it with RCU. |
| 1814 | */ |
| 1815 | read_lock(&tasklist_lock); |
| 1816 | do_each_thread(g, p) { |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1817 | WARN_ON_ONCE(!list_empty(&p->cg_list) || |
| 1818 | task_css_set(p) != &init_css_set); |
| 1819 | |
| 1820 | /* |
| 1821 | * We should check if the process is exiting, otherwise |
| 1822 | * it will race with cgroup_exit() in that the list |
| 1823 | * entry won't be deleted though the process has exited. |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1824 | * Do it while holding siglock so that we don't end up |
| 1825 | * racing against cgroup_exit(). |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1826 | * |
| 1827 | * Interrupts were already disabled while acquiring |
| 1828 | * the css_set_lock, so we do not need to disable it |
| 1829 | * again when acquiring the sighand->siglock here. |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1830 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1831 | spin_lock(&p->sighand->siglock); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1832 | if (!(p->flags & PF_EXITING)) { |
| 1833 | struct css_set *cset = task_css_set(p); |
| 1834 | |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 1835 | if (!css_set_populated(cset)) |
| 1836 | css_set_update_populated(cset, true); |
Tejun Heo | 389b9c1 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 1837 | list_add_tail(&p->cg_list, &cset->tasks); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1838 | get_css_set(cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 1839 | cset->nr_tasks++; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1840 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1841 | spin_unlock(&p->sighand->siglock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1842 | } while_each_thread(g, p); |
| 1843 | read_unlock(&tasklist_lock); |
| 1844 | out_unlock: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1845 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1846 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1847 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1848 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1849 | { |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1850 | struct cgroup_subsys *ss; |
| 1851 | int ssid; |
| 1852 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1853 | INIT_LIST_HEAD(&cgrp->self.sibling); |
| 1854 | INIT_LIST_HEAD(&cgrp->self.children); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1855 | INIT_LIST_HEAD(&cgrp->cset_links); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1856 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1857 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 1858 | cgrp->self.cgroup = cgrp; |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 1859 | cgrp->self.flags |= CSS_ONLINE; |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 1860 | cgrp->dom_cgrp = cgrp; |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 1861 | cgrp->max_descendants = INT_MAX; |
| 1862 | cgrp->max_depth = INT_MAX; |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 1863 | prev_cputime_init(&cgrp->prev_cputime); |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 1864 | |
| 1865 | for_each_subsys(ss, ssid) |
| 1866 | INIT_LIST_HEAD(&cgrp->e_csets[ssid]); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 1867 | |
| 1868 | init_waitqueue_head(&cgrp->offline_waitq); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1869 | INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1870 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1871 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1872 | void init_cgroup_root(struct cgroup_root *root, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1873 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1874 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1875 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1876 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1877 | atomic_set(&root->nr_cgrps, 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1878 | cgrp->root = root; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1879 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1880 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1881 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1882 | root->flags = opts->flags; |
| 1883 | if (opts->release_agent) |
Tejun Heo | 08a7767 | 2018-01-09 07:21:15 -0800 | [diff] [blame] | 1884 | strscpy(root->release_agent_path, opts->release_agent, PATH_MAX); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1885 | if (opts->name) |
Tejun Heo | 08a7767 | 2018-01-09 07:21:15 -0800 | [diff] [blame] | 1886 | strscpy(root->name, opts->name, MAX_CGROUP_ROOT_NAMELEN); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1887 | if (opts->cpuset_clone_children) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1888 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
Zefan Li | 9732adc | 2017-04-19 10:15:59 +0800 | [diff] [blame] | 1891 | int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask, int ref_flags) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1892 | { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1893 | LIST_HEAD(tmp_links); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1894 | struct cgroup *root_cgrp = &root->cgrp; |
Tejun Heo | fa06990 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 1895 | struct kernfs_syscall_ops *kf_sops; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1896 | struct css_set *cset; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1897 | int i, ret; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1898 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1899 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1900 | |
Vladimir Davydov | cf780b7 | 2015-08-03 15:32:26 +0300 | [diff] [blame] | 1901 | 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] | 1902 | if (ret < 0) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1903 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1904 | root_cgrp->id = ret; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 1905 | root_cgrp->ancestor_ids[0] = ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1906 | |
Zefan Li | 9732adc | 2017-04-19 10:15:59 +0800 | [diff] [blame] | 1907 | ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, |
| 1908 | ref_flags, GFP_KERNEL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1909 | if (ret) |
| 1910 | goto out; |
| 1911 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1912 | /* |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 1913 | * We're accessing css_set_count without locking css_set_lock here, |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1914 | * but that's OK - it can only be increased by someone holding |
Tejun Heo | 0431359 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1915 | * cgroup_lock, and that's us. Later rebinding may disable |
| 1916 | * controllers on the default hierarchy and thus create new csets, |
| 1917 | * which can't be more than the existing ones. Allocate 2x. |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1918 | */ |
Tejun Heo | 0431359 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1919 | ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1920 | if (ret) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1921 | goto cancel_ref; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1922 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1923 | ret = cgroup_init_root_id(root); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1924 | if (ret) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1925 | goto cancel_ref; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1926 | |
Tejun Heo | fa06990 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 1927 | kf_sops = root == &cgrp_dfl_root ? |
| 1928 | &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops; |
| 1929 | |
| 1930 | root->kf_root = kernfs_create_root(kf_sops, |
Shaohua Li | aa81882 | 2017-07-12 11:49:51 -0700 | [diff] [blame] | 1931 | KERNFS_ROOT_CREATE_DEACTIVATED | |
| 1932 | KERNFS_ROOT_SUPPORT_EXPORTOP, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1933 | root_cgrp); |
| 1934 | if (IS_ERR(root->kf_root)) { |
| 1935 | ret = PTR_ERR(root->kf_root); |
| 1936 | goto exit_root_id; |
| 1937 | } |
| 1938 | root_cgrp->kn = root->kf_root->kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1939 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 1940 | ret = css_populate_dir(&root_cgrp->self); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1941 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1942 | goto destroy_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1943 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1944 | ret = rebind_subsystems(root, ss_mask); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1945 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1946 | goto destroy_root; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1947 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1948 | ret = cgroup_bpf_inherit(root_cgrp); |
| 1949 | WARN_ON_ONCE(ret); |
| 1950 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 1951 | trace_cgroup_setup_root(root); |
| 1952 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1953 | /* |
| 1954 | * There must be no failure case after here, since rebinding takes |
| 1955 | * care of subsystems' refcounts, which are explicitly dropped in |
| 1956 | * the failure exit path. |
| 1957 | */ |
| 1958 | list_add(&root->root_list, &cgroup_roots); |
| 1959 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1960 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1961 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1962 | * Link the root cgroup in this hierarchy into all the css_set |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1963 | * objects. |
| 1964 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1965 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 1966 | hash_for_each(css_set_table, i, cset, hlist) { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1967 | link_css_set(&tmp_links, cset, root_cgrp); |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 1968 | if (css_set_populated(cset)) |
| 1969 | cgroup_update_populated(root_cgrp, true); |
| 1970 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 1971 | spin_unlock_irq(&css_set_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1972 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 1973 | BUG_ON(!list_empty(&root_cgrp->self.children)); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1974 | BUG_ON(atomic_read(&root->nr_cgrps) != 1); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1975 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1976 | kernfs_activate(root_cgrp->kn); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1977 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1978 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1979 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1980 | destroy_root: |
| 1981 | kernfs_destroy_root(root->kf_root); |
| 1982 | root->kf_root = NULL; |
| 1983 | exit_root_id: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1984 | cgroup_exit_root_id(root); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 1985 | cancel_ref: |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 1986 | percpu_ref_exit(&root_cgrp->self.refcnt); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1987 | out: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1988 | free_cgrp_cset_links(&tmp_links); |
| 1989 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 1992 | struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags, |
| 1993 | struct cgroup_root *root, unsigned long magic, |
| 1994 | struct cgroup_namespace *ns) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1995 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1996 | struct dentry *dentry; |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 1997 | bool new_sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1998 | |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 1999 | dentry = kernfs_mount(fs_type, flags, root->kf_root, magic, &new_sb); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2000 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 2001 | /* |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2002 | * In non-init cgroup namespace, instead of root cgroup's dentry, |
| 2003 | * we return the dentry corresponding to the cgroupns->root_cgrp. |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2004 | */ |
| 2005 | if (!IS_ERR(dentry) && ns != &init_cgroup_ns) { |
| 2006 | struct dentry *nsdentry; |
| 2007 | struct cgroup *cgrp; |
| 2008 | |
| 2009 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2010 | spin_lock_irq(&css_set_lock); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2011 | |
| 2012 | cgrp = cset_cgroup_from_root(ns->root_cset, root); |
| 2013 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2014 | spin_unlock_irq(&css_set_lock); |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2015 | mutex_unlock(&cgroup_mutex); |
| 2016 | |
| 2017 | nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb); |
| 2018 | dput(dentry); |
| 2019 | dentry = nsdentry; |
| 2020 | } |
| 2021 | |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 2022 | if (IS_ERR(dentry) || !new_sb) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2023 | cgroup_put(&root->cgrp); |
Li Zefan | 3a32bd7 | 2014-06-30 11:50:59 +0800 | [diff] [blame] | 2024 | |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2025 | return dentry; |
| 2026 | } |
| 2027 | |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2028 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
| 2029 | int flags, const char *unused_dev_name, |
| 2030 | void *data) |
| 2031 | { |
| 2032 | struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; |
| 2033 | struct dentry *dentry; |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2034 | int ret; |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2035 | |
| 2036 | get_cgroup_ns(ns); |
| 2037 | |
| 2038 | /* Check if the caller has permission to mount. */ |
| 2039 | if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) { |
| 2040 | put_cgroup_ns(ns); |
| 2041 | return ERR_PTR(-EPERM); |
| 2042 | } |
| 2043 | |
Li Zefan | 3a32bd7 | 2014-06-30 11:50:59 +0800 | [diff] [blame] | 2044 | /* |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2045 | * The first time anyone tries to mount a cgroup, enable the list |
| 2046 | * 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] | 2047 | */ |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2048 | if (!use_task_css_set_links) |
| 2049 | cgroup_enable_task_cg_lists(); |
| 2050 | |
| 2051 | if (fs_type == &cgroup2_fs_type) { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2052 | unsigned int root_flags; |
| 2053 | |
| 2054 | ret = parse_cgroup_root_flags(data, &root_flags); |
| 2055 | if (ret) { |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2056 | put_cgroup_ns(ns); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2057 | return ERR_PTR(ret); |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2058 | } |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2059 | |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2060 | cgrp_dfl_visible = true; |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 2061 | cgroup_get_live(&cgrp_dfl_root.cgrp); |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2062 | |
| 2063 | dentry = cgroup_do_mount(&cgroup2_fs_type, flags, &cgrp_dfl_root, |
| 2064 | CGROUP2_SUPER_MAGIC, ns); |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 2065 | if (!IS_ERR(dentry)) |
| 2066 | apply_cgroup_root_flags(root_flags); |
Tejun Heo | 633feee3 | 2016-12-27 14:49:07 -0500 | [diff] [blame] | 2067 | } else { |
| 2068 | dentry = cgroup1_mount(&cgroup_fs_type, flags, data, |
| 2069 | CGROUP_SUPER_MAGIC, ns); |
Li Zefan | 3a32bd7 | 2014-06-30 11:50:59 +0800 | [diff] [blame] | 2070 | } |
| 2071 | |
Serge Hallyn | ed82571 | 2016-01-29 02:54:09 -0600 | [diff] [blame] | 2072 | put_cgroup_ns(ns); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2073 | return dentry; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2074 | } |
| 2075 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2076 | static void cgroup_kill_sb(struct super_block *sb) |
| 2077 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2078 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2079 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2080 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2081 | /* |
| 2082 | * If @root doesn't have any mounts or children, start killing it. |
| 2083 | * This prevents new mounts by disabling percpu_ref_tryget_live(). |
| 2084 | * cgroup_mount() may wait for @root's release. |
Li Zefan | 1f779fb | 2014-06-04 16:48:15 +0800 | [diff] [blame] | 2085 | * |
| 2086 | * And don't kill the default root. |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2087 | */ |
Johannes Weiner | 3c606d3 | 2015-01-22 10:19:43 -0500 | [diff] [blame] | 2088 | if (!list_empty(&root->cgrp.self.children) || |
Li Zefan | 1f779fb | 2014-06-04 16:48:15 +0800 | [diff] [blame] | 2089 | root == &cgrp_dfl_root) |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 2090 | cgroup_put(&root->cgrp); |
| 2091 | else |
| 2092 | percpu_ref_kill(&root->cgrp.self.refcnt); |
| 2093 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2094 | kernfs_kill_sb(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2097 | struct file_system_type cgroup_fs_type = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2098 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 2099 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2100 | .kill_sb = cgroup_kill_sb, |
Serge Hallyn | 1c53753 | 2016-01-29 02:54:11 -0600 | [diff] [blame] | 2101 | .fs_flags = FS_USERNS_MOUNT, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2102 | }; |
| 2103 | |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 2104 | static struct file_system_type cgroup2_fs_type = { |
| 2105 | .name = "cgroup2", |
| 2106 | .mount = cgroup_mount, |
| 2107 | .kill_sb = cgroup_kill_sb, |
Serge Hallyn | 1c53753 | 2016-01-29 02:54:11 -0600 | [diff] [blame] | 2108 | .fs_flags = FS_USERNS_MOUNT, |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 2109 | }; |
| 2110 | |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2111 | int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen, |
| 2112 | struct cgroup_namespace *ns) |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2113 | { |
| 2114 | struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2115 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2116 | return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2117 | } |
| 2118 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2119 | int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen, |
| 2120 | struct cgroup_namespace *ns) |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2121 | { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2122 | int ret; |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2123 | |
| 2124 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2125 | spin_lock_irq(&css_set_lock); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2126 | |
| 2127 | ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns); |
| 2128 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2129 | spin_unlock_irq(&css_set_lock); |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 2130 | mutex_unlock(&cgroup_mutex); |
| 2131 | |
| 2132 | return ret; |
| 2133 | } |
| 2134 | EXPORT_SYMBOL_GPL(cgroup_path_ns); |
| 2135 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2136 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2137 | * 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] | 2138 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2139 | * @buf: the buffer to write the path into |
| 2140 | * @buflen: the length of the buffer |
| 2141 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2142 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 2143 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 2144 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 2145 | * cgroup controller callbacks. |
| 2146 | * |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2147 | * Return value is the same as kernfs_path(). |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2148 | */ |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2149 | 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] | 2150 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2151 | struct cgroup_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2152 | struct cgroup *cgrp; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 2153 | int hierarchy_id = 1; |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2154 | int ret; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2155 | |
| 2156 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2157 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2158 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2159 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 2160 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2161 | if (root) { |
| 2162 | cgrp = task_cgroup_from_root(task, root); |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2163 | ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2164 | } else { |
| 2165 | /* if no hierarchy exists, everyone is in "/" */ |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2166 | ret = strlcpy(buf, "/", buflen); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2167 | } |
| 2168 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2169 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2170 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2171 | return ret; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2172 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 2173 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 2174 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2175 | /** |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2176 | * 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] | 2177 | * @task: target task |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2178 | * @mgctx: target migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2179 | * |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2180 | * Add @task, which is a migration target, to @mgctx->tset. This function |
| 2181 | * becomes noop if @task doesn't need to be migrated. @task's css_set |
| 2182 | * should have been added as a migration source and @task->cg_list will be |
| 2183 | * moved from the css_set's tasks list to mg_tasks one. |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2184 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2185 | static void cgroup_migrate_add_task(struct task_struct *task, |
| 2186 | struct cgroup_mgctx *mgctx) |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2187 | { |
| 2188 | struct css_set *cset; |
| 2189 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 2190 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2191 | |
| 2192 | /* @task either already exited or can't exit until the end */ |
| 2193 | if (task->flags & PF_EXITING) |
| 2194 | return; |
| 2195 | |
| 2196 | /* leave @task alone if post_fork() hasn't linked it yet */ |
| 2197 | if (list_empty(&task->cg_list)) |
| 2198 | return; |
| 2199 | |
| 2200 | cset = task_css_set(task); |
| 2201 | if (!cset->mg_src_cgrp) |
| 2202 | return; |
| 2203 | |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2204 | mgctx->tset.nr_tasks++; |
| 2205 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2206 | list_move_tail(&task->cg_list, &cset->mg_tasks); |
| 2207 | if (list_empty(&cset->mg_node)) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2208 | list_add_tail(&cset->mg_node, |
| 2209 | &mgctx->tset.src_csets); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2210 | if (list_empty(&cset->mg_dst_cset->mg_node)) |
Tejun Heo | d8ebf51 | 2017-01-15 19:03:40 -0500 | [diff] [blame] | 2211 | list_add_tail(&cset->mg_dst_cset->mg_node, |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2212 | &mgctx->tset.dst_csets); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2213 | } |
| 2214 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2215 | /** |
| 2216 | * cgroup_taskset_first - reset taskset and return the first task |
| 2217 | * @tset: taskset of interest |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2218 | * @dst_cssp: output variable for the destination css |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2219 | * |
| 2220 | * @tset iteration is initialized and the first task is returned. |
| 2221 | */ |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2222 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset, |
| 2223 | struct cgroup_subsys_state **dst_cssp) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2224 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2225 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); |
| 2226 | tset->cur_task = NULL; |
| 2227 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2228 | return cgroup_taskset_next(tset, dst_cssp); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2229 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2230 | |
| 2231 | /** |
| 2232 | * cgroup_taskset_next - iterate to the next task in taskset |
| 2233 | * @tset: taskset of interest |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2234 | * @dst_cssp: output variable for the destination css |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2235 | * |
| 2236 | * Return the next task in @tset. Iteration must have been initialized |
| 2237 | * with cgroup_taskset_first(). |
| 2238 | */ |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2239 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset, |
| 2240 | struct cgroup_subsys_state **dst_cssp) |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2241 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2242 | struct css_set *cset = tset->cur_cset; |
| 2243 | struct task_struct *task = tset->cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2244 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2245 | while (&cset->mg_node != tset->csets) { |
| 2246 | if (!task) |
| 2247 | task = list_first_entry(&cset->mg_tasks, |
| 2248 | struct task_struct, cg_list); |
| 2249 | else |
| 2250 | task = list_next_entry(task, cg_list); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2251 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2252 | if (&task->cg_list != &cset->mg_tasks) { |
| 2253 | tset->cur_cset = cset; |
| 2254 | tset->cur_task = task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 2255 | |
| 2256 | /* |
| 2257 | * This function may be called both before and |
| 2258 | * after cgroup_taskset_migrate(). The two cases |
| 2259 | * can be distinguished by looking at whether @cset |
| 2260 | * has its ->mg_dst_cset set. |
| 2261 | */ |
| 2262 | if (cset->mg_dst_cset) |
| 2263 | *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid]; |
| 2264 | else |
| 2265 | *dst_cssp = cset->subsys[tset->ssid]; |
| 2266 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2267 | return task; |
| 2268 | } |
| 2269 | |
| 2270 | cset = list_next_entry(cset, mg_node); |
| 2271 | task = NULL; |
| 2272 | } |
| 2273 | |
| 2274 | return NULL; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2275 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2276 | |
| 2277 | /** |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2278 | * cgroup_taskset_migrate - migrate a taskset |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2279 | * @mgctx: migration context |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2280 | * |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2281 | * Migrate tasks in @mgctx as setup by migration preparation functions. |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2282 | * This function fails iff one of the ->can_attach callbacks fails and |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2283 | * guarantees that either all or none of the tasks in @mgctx are migrated. |
| 2284 | * @mgctx is consumed regardless of success. |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2285 | */ |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2286 | static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx) |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2287 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2288 | struct cgroup_taskset *tset = &mgctx->tset; |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2289 | struct cgroup_subsys *ss; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2290 | struct task_struct *task, *tmp_task; |
| 2291 | struct css_set *cset, *tmp_cset; |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2292 | int ssid, failed_ssid, ret; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2293 | |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2294 | /* check that we can legitimately attach to the cgroup */ |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2295 | if (tset->nr_tasks) { |
| 2296 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2297 | if (ss->can_attach) { |
| 2298 | tset->ssid = ssid; |
| 2299 | ret = ss->can_attach(tset); |
| 2300 | if (ret) { |
| 2301 | failed_ssid = ssid; |
| 2302 | goto out_cancel_attach; |
| 2303 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2304 | } |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2305 | } while_each_subsys_mask(); |
| 2306 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2307 | |
| 2308 | /* |
| 2309 | * Now that we're guaranteed success, proceed to move all tasks to |
| 2310 | * the new cgroup. There are no failure cases after here, so this |
| 2311 | * is the commit point. |
| 2312 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2313 | spin_lock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2314 | list_for_each_entry(cset, &tset->src_csets, mg_node) { |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2315 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) { |
| 2316 | struct css_set *from_cset = task_css_set(task); |
| 2317 | struct css_set *to_cset = cset->mg_dst_cset; |
| 2318 | |
| 2319 | get_css_set(to_cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 2320 | to_cset->nr_tasks++; |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2321 | css_set_move_task(task, from_cset, to_cset, true); |
| 2322 | put_css_set_locked(from_cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 2323 | from_cset->nr_tasks--; |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 2324 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2325 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2326 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2327 | |
| 2328 | /* |
| 2329 | * Migration is committed, all target tasks are now on dst_csets. |
| 2330 | * Nothing is sensitive to fork() after this point. Notify |
| 2331 | * controllers that migration is complete. |
| 2332 | */ |
| 2333 | tset->csets = &tset->dst_csets; |
| 2334 | |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2335 | if (tset->nr_tasks) { |
| 2336 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2337 | if (ss->attach) { |
| 2338 | tset->ssid = ssid; |
| 2339 | ss->attach(tset); |
| 2340 | } |
| 2341 | } while_each_subsys_mask(); |
| 2342 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2343 | |
| 2344 | ret = 0; |
| 2345 | goto out_release_tset; |
| 2346 | |
| 2347 | out_cancel_attach: |
Tejun Heo | 6104672 | 2017-07-08 07:17:02 -0400 | [diff] [blame] | 2348 | if (tset->nr_tasks) { |
| 2349 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { |
| 2350 | if (ssid == failed_ssid) |
| 2351 | break; |
| 2352 | if (ss->cancel_attach) { |
| 2353 | tset->ssid = ssid; |
| 2354 | ss->cancel_attach(tset); |
| 2355 | } |
| 2356 | } while_each_subsys_mask(); |
| 2357 | } |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2358 | out_release_tset: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2359 | spin_lock_irq(&css_set_lock); |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2360 | list_splice_init(&tset->dst_csets, &tset->src_csets); |
| 2361 | list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) { |
| 2362 | list_splice_tail_init(&cset->mg_tasks, &cset->tasks); |
| 2363 | list_del_init(&cset->mg_node); |
| 2364 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2365 | spin_unlock_irq(&css_set_lock); |
Waiman Long | c4fa6c4 | 2017-09-21 09:54:13 -0400 | [diff] [blame] | 2366 | |
| 2367 | /* |
| 2368 | * Re-initialize the cgroup_taskset structure in case it is reused |
| 2369 | * again in another cgroup_migrate_add_task()/cgroup_migrate_execute() |
| 2370 | * iteration. |
| 2371 | */ |
| 2372 | tset->nr_tasks = 0; |
| 2373 | tset->csets = &tset->src_csets; |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2374 | return ret; |
| 2375 | } |
| 2376 | |
| 2377 | /** |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2378 | * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2379 | * @dst_cgrp: destination cgroup to test |
| 2380 | * |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2381 | * On the default hierarchy, except for the mixable, (possible) thread root |
| 2382 | * and threaded cgroups, subtree_control must be zero for migration |
| 2383 | * destination cgroups with tasks so that child cgroups don't compete |
| 2384 | * against tasks. |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2385 | */ |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2386 | int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp) |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2387 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2388 | /* v1 doesn't have any restriction */ |
| 2389 | if (!cgroup_on_dfl(dst_cgrp)) |
| 2390 | return 0; |
| 2391 | |
| 2392 | /* verify @dst_cgrp can host resources */ |
| 2393 | if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp)) |
| 2394 | return -EOPNOTSUPP; |
| 2395 | |
| 2396 | /* mixables don't care */ |
| 2397 | if (cgroup_is_mixable(dst_cgrp)) |
| 2398 | return 0; |
| 2399 | |
| 2400 | /* |
| 2401 | * If @dst_cgrp is already or can become a thread root or is |
| 2402 | * threaded, it doesn't matter. |
| 2403 | */ |
| 2404 | if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp)) |
| 2405 | return 0; |
| 2406 | |
| 2407 | /* apply no-internal-process constraint */ |
| 2408 | if (dst_cgrp->subtree_control) |
| 2409 | return -EBUSY; |
| 2410 | |
| 2411 | return 0; |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | /** |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2415 | * cgroup_migrate_finish - cleanup after attach |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2416 | * @mgctx: migration context |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2417 | * |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2418 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See |
| 2419 | * those functions for details. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2420 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2421 | void cgroup_migrate_finish(struct cgroup_mgctx *mgctx) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2422 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2423 | LIST_HEAD(preloaded); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2424 | struct css_set *cset, *tmp_cset; |
| 2425 | |
| 2426 | lockdep_assert_held(&cgroup_mutex); |
| 2427 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2428 | spin_lock_irq(&css_set_lock); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2429 | |
| 2430 | list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded); |
| 2431 | list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded); |
| 2432 | |
| 2433 | list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2434 | cset->mg_src_cgrp = NULL; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2435 | cset->mg_dst_cgrp = NULL; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2436 | cset->mg_dst_cset = NULL; |
| 2437 | list_del_init(&cset->mg_preload_node); |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2438 | put_css_set_locked(cset); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2439 | } |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2440 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2441 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | /** |
| 2445 | * cgroup_migrate_add_src - add a migration source css_set |
| 2446 | * @src_cset: the source css_set to add |
| 2447 | * @dst_cgrp: the destination cgroup |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2448 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2449 | * |
| 2450 | * 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] | 2451 | * @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] | 2452 | * up by cgroup_migrate_finish(). |
| 2453 | * |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 2454 | * This function may be called without holding cgroup_threadgroup_rwsem |
| 2455 | * even if the target is a process. Threads may be created and destroyed |
| 2456 | * but as long as cgroup_mutex is not dropped, no new css_set can be put |
| 2457 | * into play and the preloaded css_sets are guaranteed to cover all |
| 2458 | * migrations. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2459 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2460 | void cgroup_migrate_add_src(struct css_set *src_cset, |
| 2461 | struct cgroup *dst_cgrp, |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2462 | struct cgroup_mgctx *mgctx) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2463 | { |
| 2464 | struct cgroup *src_cgrp; |
| 2465 | |
| 2466 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 2467 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2468 | |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 2469 | /* |
| 2470 | * If ->dead, @src_set is associated with one or more dead cgroups |
| 2471 | * and doesn't contain any migratable tasks. Ignore it early so |
| 2472 | * that the rest of migration path doesn't get confused by it. |
| 2473 | */ |
| 2474 | if (src_cset->dead) |
| 2475 | return; |
| 2476 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2477 | src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); |
| 2478 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2479 | if (!list_empty(&src_cset->mg_preload_node)) |
| 2480 | return; |
| 2481 | |
| 2482 | WARN_ON(src_cset->mg_src_cgrp); |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2483 | WARN_ON(src_cset->mg_dst_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2484 | WARN_ON(!list_empty(&src_cset->mg_tasks)); |
| 2485 | WARN_ON(!list_empty(&src_cset->mg_node)); |
| 2486 | |
| 2487 | src_cset->mg_src_cgrp = src_cgrp; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2488 | src_cset->mg_dst_cgrp = dst_cgrp; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2489 | get_css_set(src_cset); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2490 | list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | /** |
| 2494 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2495 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2496 | * |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2497 | * 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] | 2498 | * preloaded to @mgctx->preloaded_src_csets. This function looks up and |
| 2499 | * pins all destination css_sets, links each to its source, and append them |
| 2500 | * to @mgctx->preloaded_dst_csets. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2501 | * |
| 2502 | * This function must be called after cgroup_migrate_add_src() has been |
| 2503 | * called on each migration source css_set. After migration is performed |
| 2504 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2505 | * @mgctx. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2506 | */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2507 | int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2508 | { |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2509 | struct css_set *src_cset, *tmp_cset; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2510 | |
| 2511 | lockdep_assert_held(&cgroup_mutex); |
| 2512 | |
| 2513 | /* 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] | 2514 | list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets, |
| 2515 | mg_preload_node) { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2516 | struct css_set *dst_cset; |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2517 | struct cgroup_subsys *ss; |
| 2518 | int ssid; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2519 | |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2520 | dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2521 | if (!dst_cset) |
| 2522 | goto err; |
| 2523 | |
| 2524 | 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] | 2525 | |
| 2526 | /* |
| 2527 | * If src cset equals dst, it's noop. Drop the src. |
| 2528 | * cgroup_migrate() will skip the cset too. Note that we |
| 2529 | * can't handle src == dst as some nodes are used by both. |
| 2530 | */ |
| 2531 | if (src_cset == dst_cset) { |
| 2532 | src_cset->mg_src_cgrp = NULL; |
Tejun Heo | e485798 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2533 | src_cset->mg_dst_cgrp = NULL; |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2534 | list_del_init(&src_cset->mg_preload_node); |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2535 | put_css_set(src_cset); |
| 2536 | put_css_set(dst_cset); |
Tejun Heo | f817de9 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2537 | continue; |
| 2538 | } |
| 2539 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2540 | src_cset->mg_dst_cset = dst_cset; |
| 2541 | |
| 2542 | if (list_empty(&dst_cset->mg_preload_node)) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2543 | list_add_tail(&dst_cset->mg_preload_node, |
| 2544 | &mgctx->preloaded_dst_csets); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2545 | else |
Zefan Li | a25eb52 | 2014-09-19 16:51:00 +0800 | [diff] [blame] | 2546 | put_css_set(dst_cset); |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2547 | |
| 2548 | for_each_subsys(ss, ssid) |
| 2549 | if (src_cset->subsys[ssid] != dst_cset->subsys[ssid]) |
| 2550 | mgctx->ss_mask |= 1 << ssid; |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2551 | } |
| 2552 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2553 | return 0; |
| 2554 | err: |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2555 | cgroup_migrate_finish(mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2556 | return -ENOMEM; |
| 2557 | } |
| 2558 | |
| 2559 | /** |
| 2560 | * cgroup_migrate - migrate a process or task to a cgroup |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2561 | * @leader: the leader of the process or the task to migrate |
| 2562 | * @threadgroup: whether @leader points to the whole process or a single task |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2563 | * @mgctx: migration context |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2564 | * |
Tejun Heo | 37ff9f8 | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2565 | * Migrate a process or task denoted by @leader. If migrating a process, |
| 2566 | * the caller must be holding cgroup_threadgroup_rwsem. The caller is also |
| 2567 | * responsible for invoking cgroup_migrate_add_src() and |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2568 | * cgroup_migrate_prepare_dst() on the targets before invoking this |
| 2569 | * function and following up with cgroup_migrate_finish(). |
| 2570 | * |
| 2571 | * As long as a controller's ->can_attach() doesn't fail, this function is |
| 2572 | * guaranteed to succeed. This means that, excluding ->can_attach() |
| 2573 | * failure, when migrating multiple targets, the success or failure can be |
| 2574 | * decided for all targets by invoking group_migrate_prepare_dst() before |
| 2575 | * actually starting migrating. |
| 2576 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2577 | int cgroup_migrate(struct task_struct *leader, bool threadgroup, |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2578 | struct cgroup_mgctx *mgctx) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2579 | { |
Tejun Heo | adaae5d | 2015-09-11 15:00:21 -0400 | [diff] [blame] | 2580 | struct task_struct *task; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2581 | |
| 2582 | /* |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2583 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2584 | * already PF_EXITING could be freed from underneath us unless we |
| 2585 | * take an rcu_read_lock. |
| 2586 | */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2587 | spin_lock_irq(&css_set_lock); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2588 | rcu_read_lock(); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2589 | task = leader; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2590 | do { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2591 | cgroup_migrate_add_task(task, mgctx); |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2592 | if (!threadgroup) |
| 2593 | break; |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2594 | } while_each_thread(leader, task); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2595 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2596 | spin_unlock_irq(&css_set_lock); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2597 | |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2598 | return cgroup_migrate_execute(mgctx); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2599 | } |
| 2600 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2601 | /** |
| 2602 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
| 2603 | * @dst_cgrp: the cgroup to attach to |
| 2604 | * @leader: the task or the leader of the threadgroup to be attached |
| 2605 | * @threadgroup: attach the whole threadgroup? |
| 2606 | * |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 2607 | * Call holding cgroup_mutex and cgroup_threadgroup_rwsem. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2608 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2609 | int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, |
| 2610 | bool threadgroup) |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2611 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2612 | DEFINE_CGROUP_MGCTX(mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2613 | struct task_struct *task; |
| 2614 | int ret; |
| 2615 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 2616 | ret = cgroup_migrate_vet_dst(dst_cgrp); |
| 2617 | if (ret) |
| 2618 | return ret; |
Tejun Heo | 6c694c8 | 2016-03-08 11:51:25 -0500 | [diff] [blame] | 2619 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2620 | /* look up all src csets */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2621 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2622 | rcu_read_lock(); |
| 2623 | task = leader; |
| 2624 | do { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2625 | cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2626 | if (!threadgroup) |
| 2627 | break; |
| 2628 | } while_each_thread(leader, task); |
| 2629 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2630 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2631 | |
| 2632 | /* prepare dst csets and commit */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2633 | ret = cgroup_migrate_prepare_dst(&mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2634 | if (!ret) |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2635 | ret = cgroup_migrate(leader, threadgroup, &mgctx); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2636 | |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2637 | cgroup_migrate_finish(&mgctx); |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 2638 | |
| 2639 | if (!ret) |
| 2640 | trace_cgroup_attach_task(dst_cgrp, leader, threadgroup); |
| 2641 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2642 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2643 | } |
| 2644 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2645 | struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup) |
| 2646 | __acquires(&cgroup_threadgroup_rwsem) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2647 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2648 | struct task_struct *tsk; |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2649 | pid_t pid; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2650 | |
Tejun Heo | acbef75 | 2014-05-13 12:16:22 -0400 | [diff] [blame] | 2651 | if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2652 | return ERR_PTR(-EINVAL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2653 | |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2654 | percpu_down_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2655 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2656 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2657 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2658 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2659 | if (!tsk) { |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2660 | tsk = ERR_PTR(-ESRCH); |
| 2661 | goto out_unlock_threadgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2662 | } |
Tejun Heo | dedf22e | 2015-06-18 16:54:28 -0400 | [diff] [blame] | 2663 | } else { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2664 | tsk = current; |
Tejun Heo | dedf22e | 2015-06-18 16:54:28 -0400 | [diff] [blame] | 2665 | } |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2666 | |
| 2667 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2668 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2669 | |
| 2670 | /* |
Tejun Heo | 77f8879 | 2017-03-16 16:54:24 -0400 | [diff] [blame] | 2671 | * kthreads may acquire PF_NO_SETAFFINITY during initialization. |
| 2672 | * If userland migrates such a kthread to a non-root cgroup, it can |
| 2673 | * become trapped in a cpuset, or RT kthread may be born in a |
| 2674 | * cgroup with no rt_runtime allocated. Just say no. |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2675 | */ |
Tejun Heo | 77f8879 | 2017-03-16 16:54:24 -0400 | [diff] [blame] | 2676 | if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) { |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2677 | tsk = ERR_PTR(-EINVAL); |
| 2678 | goto out_unlock_threadgroup; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2679 | } |
| 2680 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2681 | get_task_struct(tsk); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2682 | goto out_unlock_rcu; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2683 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2684 | out_unlock_threadgroup: |
| 2685 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2686 | out_unlock_rcu: |
| 2687 | rcu_read_unlock(); |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 2688 | return tsk; |
| 2689 | } |
| 2690 | |
| 2691 | void cgroup_procs_write_finish(struct task_struct *task) |
| 2692 | __releases(&cgroup_threadgroup_rwsem) |
| 2693 | { |
| 2694 | struct cgroup_subsys *ss; |
| 2695 | int ssid; |
| 2696 | |
| 2697 | /* release reference from cgroup_procs_write_start() */ |
| 2698 | put_task_struct(task); |
| 2699 | |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2700 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | 5cf1cac | 2016-04-21 19:06:48 -0400 | [diff] [blame] | 2701 | for_each_subsys(ss, ssid) |
| 2702 | if (ss->post_attach) |
| 2703 | ss->post_attach(); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2704 | } |
| 2705 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 2706 | 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] | 2707 | { |
| 2708 | struct cgroup_subsys *ss; |
| 2709 | bool printed = false; |
| 2710 | int ssid; |
| 2711 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 2712 | do_each_subsys_mask(ss, ssid, ss_mask) { |
Aleksa Sarai | a966a4e | 2015-06-06 10:02:15 +1000 | [diff] [blame] | 2713 | if (printed) |
| 2714 | seq_putc(seq, ' '); |
| 2715 | seq_printf(seq, "%s", ss->name); |
| 2716 | printed = true; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 2717 | } while_each_subsys_mask(); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2718 | if (printed) |
| 2719 | seq_putc(seq, '\n'); |
| 2720 | } |
| 2721 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2722 | /* show controllers which are enabled from the parent */ |
| 2723 | static int cgroup_controllers_show(struct seq_file *seq, void *v) |
| 2724 | { |
| 2725 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2726 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 2727 | cgroup_print_ss_mask(seq, cgroup_control(cgrp)); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2728 | return 0; |
| 2729 | } |
| 2730 | |
| 2731 | /* show controllers which are enabled for a given cgroup's children */ |
| 2732 | static int cgroup_subtree_control_show(struct seq_file *seq, void *v) |
| 2733 | { |
| 2734 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2735 | |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 2736 | cgroup_print_ss_mask(seq, cgrp->subtree_control); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2737 | return 0; |
| 2738 | } |
| 2739 | |
| 2740 | /** |
| 2741 | * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy |
| 2742 | * @cgrp: root of the subtree to update csses for |
| 2743 | * |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2744 | * @cgrp's control masks have changed and its subtree's css associations |
| 2745 | * need to be updated accordingly. This function looks up all css_sets |
| 2746 | * which are attached to the subtree, creates the matching updated css_sets |
| 2747 | * and migrates the tasks to the new ones. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2748 | */ |
| 2749 | static int cgroup_update_dfl_csses(struct cgroup *cgrp) |
| 2750 | { |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2751 | DEFINE_CGROUP_MGCTX(mgctx); |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2752 | struct cgroup_subsys_state *d_css; |
| 2753 | struct cgroup *dsct; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2754 | struct css_set *src_cset; |
| 2755 | int ret; |
| 2756 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2757 | lockdep_assert_held(&cgroup_mutex); |
| 2758 | |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2759 | percpu_down_write(&cgroup_threadgroup_rwsem); |
| 2760 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2761 | /* look up all csses currently attached to @cgrp's subtree */ |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2762 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2763 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2764 | struct cgrp_cset_link *link; |
| 2765 | |
Tejun Heo | 5496260 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2766 | list_for_each_entry(link, &dsct->cset_links, cset_link) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2767 | cgroup_migrate_add_src(link->cset, dsct, &mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2768 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2769 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2770 | |
| 2771 | /* NULL dst indicates self on default hierarchy */ |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2772 | ret = cgroup_migrate_prepare_dst(&mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2773 | if (ret) |
| 2774 | goto out_finish; |
| 2775 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2776 | spin_lock_irq(&css_set_lock); |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2777 | 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] | 2778 | struct task_struct *task, *ntask; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2779 | |
Tejun Heo | 1026507 | 2015-09-11 15:00:22 -0400 | [diff] [blame] | 2780 | /* all tasks in src_csets need to be migrated */ |
| 2781 | list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list) |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2782 | cgroup_migrate_add_task(task, &mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2783 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 2784 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2785 | |
Tejun Heo | bfc2cf6 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2786 | ret = cgroup_migrate_execute(&mgctx); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2787 | out_finish: |
Tejun Heo | e595cd7 | 2017-01-15 19:03:41 -0500 | [diff] [blame] | 2788 | cgroup_migrate_finish(&mgctx); |
Tejun Heo | 3014dde | 2015-09-16 13:03:02 -0400 | [diff] [blame] | 2789 | percpu_up_write(&cgroup_threadgroup_rwsem); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 2790 | return ret; |
| 2791 | } |
| 2792 | |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2793 | /** |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2794 | * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2795 | * @cgrp: root of the target subtree |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2796 | * |
| 2797 | * Because css offlining is asynchronous, userland may try to re-enable a |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2798 | * controller while the previous css is still around. This function grabs |
| 2799 | * cgroup_mutex and drains the previous css instances of @cgrp's subtree. |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2800 | */ |
Tejun Heo | 0a268db | 2016-12-27 14:49:06 -0500 | [diff] [blame] | 2801 | void cgroup_lock_and_drain_offline(struct cgroup *cgrp) |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2802 | __acquires(&cgroup_mutex) |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2803 | { |
| 2804 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2805 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2806 | struct cgroup_subsys *ss; |
| 2807 | int ssid; |
| 2808 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2809 | restart: |
| 2810 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2811 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2812 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2813 | for_each_subsys(ss, ssid) { |
| 2814 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 2815 | DEFINE_WAIT(wait); |
| 2816 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2817 | if (!css || !percpu_ref_is_dying(&css->refcnt)) |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2818 | continue; |
| 2819 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 2820 | cgroup_get_live(dsct); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2821 | prepare_to_wait(&dsct->offline_waitq, &wait, |
| 2822 | TASK_UNINTERRUPTIBLE); |
| 2823 | |
| 2824 | mutex_unlock(&cgroup_mutex); |
| 2825 | schedule(); |
| 2826 | finish_wait(&dsct->offline_waitq, &wait); |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2827 | |
| 2828 | cgroup_put(dsct); |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2829 | goto restart; |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2830 | } |
| 2831 | } |
Tejun Heo | 1b9b96a | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2832 | } |
| 2833 | |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2834 | /** |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2835 | * cgroup_save_control - save control masks of a subtree |
| 2836 | * @cgrp: root of the target subtree |
| 2837 | * |
| 2838 | * Save ->subtree_control and ->subtree_ss_mask to the respective old_ |
| 2839 | * prefixed fields for @cgrp's subtree including @cgrp itself. |
| 2840 | */ |
| 2841 | static void cgroup_save_control(struct cgroup *cgrp) |
| 2842 | { |
| 2843 | struct cgroup *dsct; |
| 2844 | struct cgroup_subsys_state *d_css; |
| 2845 | |
| 2846 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 2847 | dsct->old_subtree_control = dsct->subtree_control; |
| 2848 | dsct->old_subtree_ss_mask = dsct->subtree_ss_mask; |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | /** |
| 2853 | * cgroup_propagate_control - refresh control masks of a subtree |
| 2854 | * @cgrp: root of the target subtree |
| 2855 | * |
| 2856 | * For @cgrp and its subtree, ensure ->subtree_ss_mask matches |
| 2857 | * ->subtree_control and propagate controller availability through the |
| 2858 | * subtree so that descendants don't have unavailable controllers enabled. |
| 2859 | */ |
| 2860 | static void cgroup_propagate_control(struct cgroup *cgrp) |
| 2861 | { |
| 2862 | struct cgroup *dsct; |
| 2863 | struct cgroup_subsys_state *d_css; |
| 2864 | |
| 2865 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 2866 | dsct->subtree_control &= cgroup_control(dsct); |
Tejun Heo | 5ced251 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2867 | dsct->subtree_ss_mask = |
| 2868 | cgroup_calc_subtree_ss_mask(dsct->subtree_control, |
| 2869 | cgroup_ss_mask(dsct)); |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2870 | } |
| 2871 | } |
| 2872 | |
| 2873 | /** |
| 2874 | * cgroup_restore_control - restore control masks of a subtree |
| 2875 | * @cgrp: root of the target subtree |
| 2876 | * |
| 2877 | * Restore ->subtree_control and ->subtree_ss_mask from the respective old_ |
| 2878 | * prefixed fields for @cgrp's subtree including @cgrp itself. |
| 2879 | */ |
| 2880 | static void cgroup_restore_control(struct cgroup *cgrp) |
| 2881 | { |
| 2882 | struct cgroup *dsct; |
| 2883 | struct cgroup_subsys_state *d_css; |
| 2884 | |
| 2885 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
| 2886 | dsct->subtree_control = dsct->old_subtree_control; |
| 2887 | dsct->subtree_ss_mask = dsct->old_subtree_ss_mask; |
| 2888 | } |
| 2889 | } |
| 2890 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2891 | static bool css_visible(struct cgroup_subsys_state *css) |
| 2892 | { |
| 2893 | struct cgroup_subsys *ss = css->ss; |
| 2894 | struct cgroup *cgrp = css->cgroup; |
| 2895 | |
| 2896 | if (cgroup_control(cgrp) & (1 << ss->id)) |
| 2897 | return true; |
| 2898 | if (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) |
| 2899 | return false; |
| 2900 | return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl; |
| 2901 | } |
| 2902 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2903 | /** |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2904 | * cgroup_apply_control_enable - enable or show csses according to control |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2905 | * @cgrp: root of the target subtree |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2906 | * |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2907 | * 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] | 2908 | * visible. A css is created invisible if it's being implicitly enabled |
| 2909 | * through dependency. An invisible css is made visible when the userland |
| 2910 | * explicitly enables it. |
| 2911 | * |
| 2912 | * Returns 0 on success, -errno on failure. On failure, csses which have |
| 2913 | * been processed already aren't cleaned up. The caller is responsible for |
Masahiro Yamada | 8a1115f | 2017-03-09 16:16:31 -0800 | [diff] [blame] | 2914 | * cleaning up with cgroup_apply_control_disable(). |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2915 | */ |
| 2916 | static int cgroup_apply_control_enable(struct cgroup *cgrp) |
| 2917 | { |
| 2918 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2919 | struct cgroup_subsys_state *d_css; |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2920 | struct cgroup_subsys *ss; |
| 2921 | int ssid, ret; |
| 2922 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2923 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2924 | for_each_subsys(ss, ssid) { |
| 2925 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 2926 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2927 | WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); |
| 2928 | |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2929 | if (!(cgroup_ss_mask(dsct) & (1 << ss->id))) |
| 2930 | continue; |
| 2931 | |
| 2932 | if (!css) { |
| 2933 | css = css_create(dsct, ss); |
| 2934 | if (IS_ERR(css)) |
| 2935 | return PTR_ERR(css); |
| 2936 | } |
| 2937 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2938 | if (css_visible(css)) { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2939 | ret = css_populate_dir(css); |
Tejun Heo | bdb53bd | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2940 | if (ret) |
| 2941 | return ret; |
| 2942 | } |
| 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | return 0; |
| 2947 | } |
| 2948 | |
| 2949 | /** |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2950 | * cgroup_apply_control_disable - kill or hide csses according to control |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2951 | * @cgrp: root of the target subtree |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2952 | * |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2953 | * 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] | 2954 | * cgroup_ss_mask() and cgroup_visible_mask(). |
| 2955 | * |
| 2956 | * A css is hidden when the userland requests it to be disabled while other |
| 2957 | * subsystems are still depending on it. The css must not actively control |
| 2958 | * resources and be in the vanilla state if it's made visible again later. |
| 2959 | * Controllers which may be depended upon should provide ->css_reset() for |
| 2960 | * this purpose. |
| 2961 | */ |
| 2962 | static void cgroup_apply_control_disable(struct cgroup *cgrp) |
| 2963 | { |
| 2964 | struct cgroup *dsct; |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2965 | struct cgroup_subsys_state *d_css; |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2966 | struct cgroup_subsys *ss; |
| 2967 | int ssid; |
| 2968 | |
Tejun Heo | ce3f1d9 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2969 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2970 | for_each_subsys(ss, ssid) { |
| 2971 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 2972 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2973 | WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); |
| 2974 | |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2975 | if (!css) |
| 2976 | continue; |
| 2977 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2978 | if (css->parent && |
| 2979 | !(cgroup_ss_mask(dsct) & (1 << ss->id))) { |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2980 | kill_css(css); |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 2981 | } else if (!css_visible(css)) { |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 2982 | css_clear_dir(css); |
Tejun Heo | 12b3bb6 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 2983 | if (ss->css_reset) |
| 2984 | ss->css_reset(css); |
| 2985 | } |
| 2986 | } |
| 2987 | } |
| 2988 | } |
| 2989 | |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 2990 | /** |
| 2991 | * cgroup_apply_control - apply control mask updates to the subtree |
| 2992 | * @cgrp: root of the target subtree |
| 2993 | * |
| 2994 | * subsystems can be enabled and disabled in a subtree using the following |
| 2995 | * steps. |
| 2996 | * |
| 2997 | * 1. Call cgroup_save_control() to stash the current state. |
| 2998 | * 2. Update ->subtree_control masks in the subtree as desired. |
| 2999 | * 3. Call cgroup_apply_control() to apply the changes. |
| 3000 | * 4. Optionally perform other related operations. |
| 3001 | * 5. Call cgroup_finalize_control() to finish up. |
| 3002 | * |
| 3003 | * This function implements step 3 and propagates the mask changes |
| 3004 | * throughout @cgrp's subtree, updates csses accordingly and perform |
| 3005 | * process migrations. |
| 3006 | */ |
| 3007 | static int cgroup_apply_control(struct cgroup *cgrp) |
| 3008 | { |
| 3009 | int ret; |
| 3010 | |
| 3011 | cgroup_propagate_control(cgrp); |
| 3012 | |
| 3013 | ret = cgroup_apply_control_enable(cgrp); |
| 3014 | if (ret) |
| 3015 | return ret; |
| 3016 | |
| 3017 | /* |
| 3018 | * At this point, cgroup_e_css() results reflect the new csses |
| 3019 | * making the following cgroup_update_dfl_csses() properly update |
| 3020 | * css associations of all tasks in the subtree. |
| 3021 | */ |
| 3022 | ret = cgroup_update_dfl_csses(cgrp); |
| 3023 | if (ret) |
| 3024 | return ret; |
| 3025 | |
| 3026 | return 0; |
| 3027 | } |
| 3028 | |
| 3029 | /** |
| 3030 | * cgroup_finalize_control - finalize control mask update |
| 3031 | * @cgrp: root of the target subtree |
| 3032 | * @ret: the result of the update |
| 3033 | * |
| 3034 | * Finalize control mask update. See cgroup_apply_control() for more info. |
| 3035 | */ |
| 3036 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret) |
| 3037 | { |
| 3038 | if (ret) { |
| 3039 | cgroup_restore_control(cgrp); |
| 3040 | cgroup_propagate_control(cgrp); |
| 3041 | } |
| 3042 | |
| 3043 | cgroup_apply_control_disable(cgrp); |
| 3044 | } |
| 3045 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3046 | static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable) |
| 3047 | { |
| 3048 | u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask; |
| 3049 | |
| 3050 | /* if nothing is getting enabled, nothing to worry about */ |
| 3051 | if (!enable) |
| 3052 | return 0; |
| 3053 | |
| 3054 | /* can @cgrp host any resources? */ |
| 3055 | if (!cgroup_is_valid_domain(cgrp->dom_cgrp)) |
| 3056 | return -EOPNOTSUPP; |
| 3057 | |
| 3058 | /* mixables don't care */ |
| 3059 | if (cgroup_is_mixable(cgrp)) |
| 3060 | return 0; |
| 3061 | |
| 3062 | if (domain_enable) { |
| 3063 | /* can't enable domain controllers inside a thread subtree */ |
| 3064 | if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp)) |
| 3065 | return -EOPNOTSUPP; |
| 3066 | } else { |
| 3067 | /* |
| 3068 | * Threaded controllers can handle internal competitions |
| 3069 | * and are always allowed inside a (prospective) thread |
| 3070 | * subtree. |
| 3071 | */ |
| 3072 | if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp)) |
| 3073 | return 0; |
| 3074 | } |
| 3075 | |
| 3076 | /* |
| 3077 | * Controllers can't be enabled for a cgroup with tasks to avoid |
| 3078 | * child cgroups competing against tasks. |
| 3079 | */ |
| 3080 | if (cgroup_has_tasks(cgrp)) |
| 3081 | return -EBUSY; |
| 3082 | |
| 3083 | return 0; |
| 3084 | } |
| 3085 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3086 | /* change the enabled child controllers for a cgroup in the default hierarchy */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3087 | static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, |
| 3088 | char *buf, size_t nbytes, |
| 3089 | loff_t off) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3090 | { |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 3091 | u16 enable = 0, disable = 0; |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3092 | struct cgroup *cgrp, *child; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3093 | struct cgroup_subsys *ss; |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3094 | char *tok; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3095 | int ssid, ret; |
| 3096 | |
| 3097 | /* |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 3098 | * Parse input - space separated list of subsystem names prefixed |
| 3099 | * with either + or -. |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3100 | */ |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3101 | buf = strstrip(buf); |
| 3102 | while ((tok = strsep(&buf, " "))) { |
Tejun Heo | d37167a | 2014-05-13 12:10:59 -0400 | [diff] [blame] | 3103 | if (tok[0] == '\0') |
| 3104 | continue; |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 3105 | do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) { |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 3106 | if (!cgroup_ssid_enabled(ssid) || |
| 3107 | strcmp(tok + 1, ss->name)) |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3108 | continue; |
| 3109 | |
| 3110 | if (*tok == '+') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 3111 | enable |= 1 << ssid; |
| 3112 | disable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3113 | } else if (*tok == '-') { |
Tejun Heo | 7d331fa | 2014-05-13 12:11:00 -0400 | [diff] [blame] | 3114 | disable |= 1 << ssid; |
| 3115 | enable &= ~(1 << ssid); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3116 | } else { |
| 3117 | return -EINVAL; |
| 3118 | } |
| 3119 | break; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 3120 | } while_each_subsys_mask(); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3121 | if (ssid == CGROUP_SUBSYS_COUNT) |
| 3122 | return -EINVAL; |
| 3123 | } |
| 3124 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3125 | cgrp = cgroup_kn_lock_live(of->kn, true); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3126 | if (!cgrp) |
| 3127 | return -ENODEV; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3128 | |
| 3129 | for_each_subsys(ss, ssid) { |
| 3130 | if (enable & (1 << ssid)) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3131 | if (cgrp->subtree_control & (1 << ssid)) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3132 | enable &= ~(1 << ssid); |
| 3133 | continue; |
| 3134 | } |
| 3135 | |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 3136 | if (!(cgroup_control(cgrp) & (1 << ssid))) { |
Tejun Heo | c29adf2 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3137 | ret = -ENOENT; |
| 3138 | goto out_unlock; |
| 3139 | } |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3140 | } else if (disable & (1 << ssid)) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3141 | if (!(cgrp->subtree_control & (1 << ssid))) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3142 | disable &= ~(1 << ssid); |
| 3143 | continue; |
| 3144 | } |
| 3145 | |
| 3146 | /* a child has it enabled? */ |
| 3147 | cgroup_for_each_live_child(child, cgrp) { |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3148 | if (child->subtree_control & (1 << ssid)) { |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3149 | ret = -EBUSY; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3150 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3151 | } |
| 3152 | } |
| 3153 | } |
| 3154 | } |
| 3155 | |
| 3156 | if (!enable && !disable) { |
| 3157 | ret = 0; |
Tejun Heo | ddab2b6 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3158 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3159 | } |
| 3160 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3161 | ret = cgroup_vet_subtree_control_enable(cgrp, enable); |
| 3162 | if (ret) |
Tejun Heo | 27f2675 | 2017-07-16 21:44:18 -0400 | [diff] [blame] | 3163 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3164 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3165 | /* save and update control masks and prepare csses */ |
| 3166 | cgroup_save_control(cgrp); |
Tejun Heo | c29adf2 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 3167 | |
Tejun Heo | 15a27c3 | 2016-03-03 09:57:59 -0500 | [diff] [blame] | 3168 | cgrp->subtree_control |= enable; |
| 3169 | cgrp->subtree_control &= ~disable; |
Tejun Heo | f63070d | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 3170 | |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3171 | ret = cgroup_apply_control(cgrp); |
Tejun Heo | f7b2814 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 3172 | cgroup_finalize_control(cgrp, ret); |
Tejun Heo | 3c74541 | 2017-07-23 08:14:15 -0400 | [diff] [blame] | 3173 | if (ret) |
| 3174 | goto out_unlock; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3175 | |
| 3176 | kernfs_activate(cgrp->kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3177 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 3178 | cgroup_kn_unlock(of->kn); |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3179 | return ret ?: nbytes; |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3180 | } |
| 3181 | |
Tejun Heo | c705a00 | 2017-07-25 13:20:18 -0400 | [diff] [blame] | 3182 | /** |
| 3183 | * cgroup_enable_threaded - make @cgrp threaded |
| 3184 | * @cgrp: the target cgroup |
| 3185 | * |
| 3186 | * Called when "threaded" is written to the cgroup.type interface file and |
| 3187 | * tries to make @cgrp threaded and join the parent's resource domain. |
| 3188 | * This function is never called on the root cgroup as cgroup.type doesn't |
| 3189 | * exist on it. |
| 3190 | */ |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3191 | static int cgroup_enable_threaded(struct cgroup *cgrp) |
| 3192 | { |
| 3193 | struct cgroup *parent = cgroup_parent(cgrp); |
| 3194 | struct cgroup *dom_cgrp = parent->dom_cgrp; |
| 3195 | int ret; |
| 3196 | |
| 3197 | lockdep_assert_held(&cgroup_mutex); |
| 3198 | |
| 3199 | /* noop if already threaded */ |
| 3200 | if (cgroup_is_threaded(cgrp)) |
| 3201 | return 0; |
| 3202 | |
Tejun Heo | d1897c9 | 2018-02-21 11:39:22 -0800 | [diff] [blame] | 3203 | /* |
| 3204 | * If @cgroup is populated or has domain controllers enabled, it |
| 3205 | * can't be switched. While the below cgroup_can_be_thread_root() |
| 3206 | * test can catch the same conditions, that's only when @parent is |
| 3207 | * not mixable, so let's check it explicitly. |
| 3208 | */ |
| 3209 | if (cgroup_is_populated(cgrp) || |
| 3210 | cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) |
| 3211 | return -EOPNOTSUPP; |
| 3212 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3213 | /* we're joining the parent's domain, ensure its validity */ |
| 3214 | if (!cgroup_is_valid_domain(dom_cgrp) || |
| 3215 | !cgroup_can_be_thread_root(dom_cgrp)) |
| 3216 | return -EOPNOTSUPP; |
| 3217 | |
| 3218 | /* |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 3219 | * The following shouldn't cause actual migrations and should |
| 3220 | * always succeed. |
| 3221 | */ |
| 3222 | cgroup_save_control(cgrp); |
| 3223 | |
| 3224 | cgrp->dom_cgrp = dom_cgrp; |
| 3225 | ret = cgroup_apply_control(cgrp); |
| 3226 | if (!ret) |
| 3227 | parent->nr_threaded_children++; |
| 3228 | else |
| 3229 | cgrp->dom_cgrp = cgrp; |
| 3230 | |
| 3231 | cgroup_finalize_control(cgrp, ret); |
| 3232 | return ret; |
| 3233 | } |
| 3234 | |
| 3235 | static int cgroup_type_show(struct seq_file *seq, void *v) |
| 3236 | { |
| 3237 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3238 | |
| 3239 | if (cgroup_is_threaded(cgrp)) |
| 3240 | seq_puts(seq, "threaded\n"); |
| 3241 | else if (!cgroup_is_valid_domain(cgrp)) |
| 3242 | seq_puts(seq, "domain invalid\n"); |
| 3243 | else if (cgroup_is_thread_root(cgrp)) |
| 3244 | seq_puts(seq, "domain threaded\n"); |
| 3245 | else |
| 3246 | seq_puts(seq, "domain\n"); |
| 3247 | |
| 3248 | return 0; |
| 3249 | } |
| 3250 | |
| 3251 | static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf, |
| 3252 | size_t nbytes, loff_t off) |
| 3253 | { |
| 3254 | struct cgroup *cgrp; |
| 3255 | int ret; |
| 3256 | |
| 3257 | /* only switching to threaded mode is supported */ |
| 3258 | if (strcmp(strstrip(buf), "threaded")) |
| 3259 | return -EINVAL; |
| 3260 | |
| 3261 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3262 | if (!cgrp) |
| 3263 | return -ENOENT; |
| 3264 | |
| 3265 | /* threaded can only be enabled */ |
| 3266 | ret = cgroup_enable_threaded(cgrp); |
| 3267 | |
| 3268 | cgroup_kn_unlock(of->kn); |
| 3269 | return ret ?: nbytes; |
| 3270 | } |
| 3271 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3272 | static int cgroup_max_descendants_show(struct seq_file *seq, void *v) |
| 3273 | { |
| 3274 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3275 | int descendants = READ_ONCE(cgrp->max_descendants); |
| 3276 | |
| 3277 | if (descendants == INT_MAX) |
| 3278 | seq_puts(seq, "max\n"); |
| 3279 | else |
| 3280 | seq_printf(seq, "%d\n", descendants); |
| 3281 | |
| 3282 | return 0; |
| 3283 | } |
| 3284 | |
| 3285 | static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of, |
| 3286 | char *buf, size_t nbytes, loff_t off) |
| 3287 | { |
| 3288 | struct cgroup *cgrp; |
| 3289 | int descendants; |
| 3290 | ssize_t ret; |
| 3291 | |
| 3292 | buf = strstrip(buf); |
| 3293 | if (!strcmp(buf, "max")) { |
| 3294 | descendants = INT_MAX; |
| 3295 | } else { |
| 3296 | ret = kstrtoint(buf, 0, &descendants); |
| 3297 | if (ret) |
| 3298 | return ret; |
| 3299 | } |
| 3300 | |
Dan Carpenter | 696b98f | 2017-08-09 13:25:21 +0300 | [diff] [blame] | 3301 | if (descendants < 0) |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3302 | return -ERANGE; |
| 3303 | |
| 3304 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3305 | if (!cgrp) |
| 3306 | return -ENOENT; |
| 3307 | |
| 3308 | cgrp->max_descendants = descendants; |
| 3309 | |
| 3310 | cgroup_kn_unlock(of->kn); |
| 3311 | |
| 3312 | return nbytes; |
| 3313 | } |
| 3314 | |
| 3315 | static int cgroup_max_depth_show(struct seq_file *seq, void *v) |
| 3316 | { |
| 3317 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 3318 | int depth = READ_ONCE(cgrp->max_depth); |
| 3319 | |
| 3320 | if (depth == INT_MAX) |
| 3321 | seq_puts(seq, "max\n"); |
| 3322 | else |
| 3323 | seq_printf(seq, "%d\n", depth); |
| 3324 | |
| 3325 | return 0; |
| 3326 | } |
| 3327 | |
| 3328 | static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of, |
| 3329 | char *buf, size_t nbytes, loff_t off) |
| 3330 | { |
| 3331 | struct cgroup *cgrp; |
| 3332 | ssize_t ret; |
| 3333 | int depth; |
| 3334 | |
| 3335 | buf = strstrip(buf); |
| 3336 | if (!strcmp(buf, "max")) { |
| 3337 | depth = INT_MAX; |
| 3338 | } else { |
| 3339 | ret = kstrtoint(buf, 0, &depth); |
| 3340 | if (ret) |
| 3341 | return ret; |
| 3342 | } |
| 3343 | |
Dan Carpenter | 696b98f | 2017-08-09 13:25:21 +0300 | [diff] [blame] | 3344 | if (depth < 0) |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 3345 | return -ERANGE; |
| 3346 | |
| 3347 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 3348 | if (!cgrp) |
| 3349 | return -ENOENT; |
| 3350 | |
| 3351 | cgrp->max_depth = depth; |
| 3352 | |
| 3353 | cgroup_kn_unlock(of->kn); |
| 3354 | |
| 3355 | return nbytes; |
| 3356 | } |
| 3357 | |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 3358 | static int cgroup_events_show(struct seq_file *seq, void *v) |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3359 | { |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 3360 | seq_printf(seq, "populated %d\n", |
Tejun Heo | 27bd4db | 2015-10-15 16:41:50 -0400 | [diff] [blame] | 3361 | cgroup_is_populated(seq_css(seq)->cgroup)); |
Tejun Heo | 842b597 | 2014-04-25 18:28:02 -0400 | [diff] [blame] | 3362 | return 0; |
| 3363 | } |
| 3364 | |
Tejun Heo | 3e48930 | 2017-08-11 05:49:01 -0700 | [diff] [blame] | 3365 | static int cgroup_stat_show(struct seq_file *seq, void *v) |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 3366 | { |
| 3367 | struct cgroup *cgroup = seq_css(seq)->cgroup; |
| 3368 | |
| 3369 | seq_printf(seq, "nr_descendants %d\n", |
| 3370 | cgroup->nr_descendants); |
| 3371 | seq_printf(seq, "nr_dying_descendants %d\n", |
| 3372 | cgroup->nr_dying_descendants); |
| 3373 | |
| 3374 | return 0; |
| 3375 | } |
| 3376 | |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3377 | static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq, |
| 3378 | struct cgroup *cgrp, int ssid) |
| 3379 | { |
| 3380 | struct cgroup_subsys *ss = cgroup_subsys[ssid]; |
| 3381 | struct cgroup_subsys_state *css; |
| 3382 | int ret; |
| 3383 | |
| 3384 | if (!ss->css_extra_stat_show) |
| 3385 | return 0; |
| 3386 | |
| 3387 | css = cgroup_tryget_css(cgrp, ss); |
| 3388 | if (!css) |
| 3389 | return 0; |
| 3390 | |
| 3391 | ret = ss->css_extra_stat_show(seq, css); |
| 3392 | css_put(css); |
| 3393 | return ret; |
| 3394 | } |
| 3395 | |
| 3396 | static int cpu_stat_show(struct seq_file *seq, void *v) |
| 3397 | { |
Tejun Heo | c3ba132 | 2017-10-30 08:13:14 -0700 | [diff] [blame] | 3398 | struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3399 | int ret = 0; |
| 3400 | |
Tejun Heo | d4ff749 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3401 | cgroup_base_stat_cputime_show(seq); |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 3402 | #ifdef CONFIG_CGROUP_SCHED |
| 3403 | ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id); |
| 3404 | #endif |
| 3405 | return ret; |
| 3406 | } |
| 3407 | |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3408 | static int cgroup_file_open(struct kernfs_open_file *of) |
| 3409 | { |
| 3410 | struct cftype *cft = of->kn->priv; |
| 3411 | |
| 3412 | if (cft->open) |
| 3413 | return cft->open(of); |
| 3414 | return 0; |
| 3415 | } |
| 3416 | |
| 3417 | static void cgroup_file_release(struct kernfs_open_file *of) |
| 3418 | { |
| 3419 | struct cftype *cft = of->kn->priv; |
| 3420 | |
| 3421 | if (cft->release) |
| 3422 | cft->release(of); |
| 3423 | } |
| 3424 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3425 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 3426 | size_t nbytes, loff_t off) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3427 | { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 3428 | struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3429 | struct cgroup *cgrp = of->kn->parent->priv; |
| 3430 | struct cftype *cft = of->kn->priv; |
| 3431 | struct cgroup_subsys_state *css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3432 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3433 | |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 3434 | /* |
| 3435 | * If namespaces are delegation boundaries, disallow writes to |
| 3436 | * files in an non-init namespace root from inside the namespace |
| 3437 | * except for the files explicitly marked delegatable - |
| 3438 | * cgroup.procs and cgroup.subtree_control. |
| 3439 | */ |
| 3440 | if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) && |
| 3441 | !(cft->flags & CFTYPE_NS_DELEGATABLE) && |
| 3442 | ns != &init_cgroup_ns && ns->root_cset->dfl_cgrp == cgrp) |
| 3443 | return -EPERM; |
| 3444 | |
Tejun Heo | b416864 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3445 | if (cft->write) |
| 3446 | return cft->write(of, buf, nbytes, off); |
| 3447 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3448 | /* |
| 3449 | * kernfs guarantees that a file isn't deleted with operations in |
| 3450 | * flight, which means that the matching css is and stays alive and |
| 3451 | * doesn't need to be pinned. The RCU locking is not necessary |
| 3452 | * either. It's just for the convenience of using cgroup_css(). |
| 3453 | */ |
| 3454 | rcu_read_lock(); |
| 3455 | css = cgroup_css(cgrp, cft->ss); |
| 3456 | rcu_read_unlock(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3457 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3458 | if (cft->write_u64) { |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3459 | unsigned long long v; |
| 3460 | ret = kstrtoull(buf, 0, &v); |
| 3461 | if (!ret) |
| 3462 | ret = cft->write_u64(css, cft, v); |
| 3463 | } else if (cft->write_s64) { |
| 3464 | long long v; |
| 3465 | ret = kstrtoll(buf, 0, &v); |
| 3466 | if (!ret) |
| 3467 | ret = cft->write_s64(css, cft, v); |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3468 | } else { |
| 3469 | ret = -EINVAL; |
| 3470 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3471 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 3472 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3473 | } |
| 3474 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3475 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3476 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3477 | return seq_cft(seq)->seq_start(seq, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 3481 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3482 | return seq_cft(seq)->seq_next(seq, v, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3483 | } |
| 3484 | |
| 3485 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 3486 | { |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3487 | if (seq_cft(seq)->seq_stop) |
| 3488 | seq_cft(seq)->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 3492 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3493 | struct cftype *cft = seq_cft(m); |
| 3494 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 3495 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3496 | if (cft->seq_show) |
| 3497 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3498 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3499 | if (cft->read_u64) |
| 3500 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 3501 | else if (cft->read_s64) |
| 3502 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 3503 | else |
| 3504 | return -EINVAL; |
| 3505 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3506 | } |
| 3507 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3508 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 3509 | .atomic_write_len = PAGE_SIZE, |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3510 | .open = cgroup_file_open, |
| 3511 | .release = cgroup_file_release, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3512 | .write = cgroup_file_write, |
| 3513 | .seq_show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 3514 | }; |
| 3515 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3516 | static struct kernfs_ops cgroup_kf_ops = { |
| 3517 | .atomic_write_len = PAGE_SIZE, |
Tejun Heo | e90cbeb | 2016-12-27 14:49:03 -0500 | [diff] [blame] | 3518 | .open = cgroup_file_open, |
| 3519 | .release = cgroup_file_release, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3520 | .write = cgroup_file_write, |
| 3521 | .seq_start = cgroup_seqfile_start, |
| 3522 | .seq_next = cgroup_seqfile_next, |
| 3523 | .seq_stop = cgroup_seqfile_stop, |
| 3524 | .seq_show = cgroup_seqfile_show, |
| 3525 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3526 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3527 | /* set uid and gid of cgroup dirs and files to that of the creator */ |
| 3528 | static int cgroup_kn_set_ugid(struct kernfs_node *kn) |
| 3529 | { |
| 3530 | struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, |
| 3531 | .ia_uid = current_fsuid(), |
| 3532 | .ia_gid = current_fsgid(), }; |
| 3533 | |
| 3534 | if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && |
| 3535 | gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) |
| 3536 | return 0; |
| 3537 | |
| 3538 | return kernfs_setattr(kn, &iattr); |
| 3539 | } |
| 3540 | |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3541 | static void cgroup_file_notify_timer(struct timer_list *timer) |
| 3542 | { |
| 3543 | cgroup_file_notify(container_of(timer, struct cgroup_file, |
| 3544 | notify_timer)); |
| 3545 | } |
| 3546 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3547 | static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, |
| 3548 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3549 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3550 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3551 | struct kernfs_node *kn; |
| 3552 | struct lock_class_key *key = NULL; |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3553 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3554 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3555 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 3556 | key = &cft->lockdep_key; |
| 3557 | #endif |
| 3558 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
| 3559 | cgroup_file_mode(cft), 0, cft->kf_ops, cft, |
Tejun Heo | dfeb0750 | 2015-02-13 14:36:31 -0800 | [diff] [blame] | 3560 | NULL, key); |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3561 | if (IS_ERR(kn)) |
| 3562 | return PTR_ERR(kn); |
| 3563 | |
| 3564 | ret = cgroup_kn_set_ugid(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3565 | if (ret) { |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3566 | kernfs_remove(kn); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3567 | return ret; |
| 3568 | } |
| 3569 | |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3570 | if (cft->file_offset) { |
| 3571 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 3572 | |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3573 | timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0); |
| 3574 | |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3575 | spin_lock_irq(&cgroup_file_kn_lock); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3576 | cfile->kn = kn; |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3577 | spin_unlock_irq(&cgroup_file_kn_lock); |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3578 | } |
| 3579 | |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 3580 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3581 | } |
| 3582 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3583 | /** |
| 3584 | * cgroup_addrm_files - add or remove files to a cgroup directory |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3585 | * @css: the target css |
| 3586 | * @cgrp: the target cgroup (usually css->cgroup) |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3587 | * @cfts: array of cftypes to be added |
| 3588 | * @is_add: whether to add or remove |
| 3589 | * |
| 3590 | * 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] | 3591 | * For removals, this function never fails. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3592 | */ |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3593 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 3594 | struct cgroup *cgrp, struct cftype cfts[], |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3595 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3596 | { |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3597 | struct cftype *cft, *cft_end = NULL; |
Tejun Heo | b598dde | 2016-02-22 22:25:45 -0500 | [diff] [blame] | 3598 | int ret = 0; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3599 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3600 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3601 | |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3602 | restart: |
| 3603 | for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3604 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3605 | if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) |
Tejun Heo | 8cbbf2c | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 3606 | continue; |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3607 | if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp)) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3608 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3609 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3610 | continue; |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3611 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp)) |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 3612 | continue; |
| 3613 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 3614 | if (is_add) { |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3615 | ret = cgroup_add_file(css, cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3616 | if (ret) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 3617 | pr_warn("%s: failed to add %s, err=%d\n", |
| 3618 | __func__, cft->name, ret); |
Tejun Heo | 6732ed8 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3619 | cft_end = cft; |
| 3620 | is_add = false; |
| 3621 | goto restart; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 3622 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 3623 | } else { |
| 3624 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3625 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3626 | } |
Tejun Heo | b598dde | 2016-02-22 22:25:45 -0500 | [diff] [blame] | 3627 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3628 | } |
| 3629 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3630 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3631 | { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3632 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 3633 | struct cgroup *root = &ss->root->cgrp; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3634 | struct cgroup_subsys_state *css; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3635 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3636 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3637 | lockdep_assert_held(&cgroup_mutex); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3638 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3639 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3640 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3641 | struct cgroup *cgrp = css->cgroup; |
| 3642 | |
Tejun Heo | 88cb04b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 3643 | if (!(css->flags & CSS_VISIBLE)) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 3644 | continue; |
| 3645 | |
Tejun Heo | 4df8dc9 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 3646 | ret = cgroup_addrm_files(css, cgrp, cfts, is_add); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3647 | if (ret) |
| 3648 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3649 | } |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3650 | |
| 3651 | if (is_add && !ret) |
| 3652 | kernfs_activate(root->kn); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3653 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3654 | } |
| 3655 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3656 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 3657 | { |
| 3658 | struct cftype *cft; |
| 3659 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3660 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 3661 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 3662 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 3663 | kfree(cft->kf_ops); |
| 3664 | cft->kf_ops = NULL; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3665 | cft->ss = NULL; |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3666 | |
| 3667 | /* revert flags set by cgroup core while adding @cfts */ |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3668 | cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3669 | } |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3670 | } |
| 3671 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3672 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3673 | { |
| 3674 | struct cftype *cft; |
| 3675 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3676 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 3677 | struct kernfs_ops *kf_ops; |
| 3678 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3679 | WARN_ON(cft->ss || cft->kf_ops); |
| 3680 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3681 | if (cft->seq_start) |
| 3682 | kf_ops = &cgroup_kf_ops; |
| 3683 | else |
| 3684 | kf_ops = &cgroup_kf_single_ops; |
| 3685 | |
| 3686 | /* |
| 3687 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 3688 | * make a copy of kf_ops to set its atomic_write_len. |
| 3689 | */ |
| 3690 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 3691 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 3692 | if (!kf_ops) { |
| 3693 | cgroup_exit_cftypes(cfts); |
| 3694 | return -ENOMEM; |
| 3695 | } |
| 3696 | kf_ops->atomic_write_len = cft->max_write_len; |
| 3697 | } |
| 3698 | |
| 3699 | cft->kf_ops = kf_ops; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3700 | cft->ss = ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | return 0; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3704 | } |
| 3705 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3706 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 3707 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3708 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3709 | |
| 3710 | if (!cfts || !cfts[0].ss) |
| 3711 | return -ENOENT; |
| 3712 | |
| 3713 | list_del(&cfts->node); |
| 3714 | cgroup_apply_cftypes(cfts, false); |
| 3715 | cgroup_exit_cftypes(cfts); |
| 3716 | return 0; |
| 3717 | } |
| 3718 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3719 | /** |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3720 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 3721 | * @cfts: zero-length name terminated array of cftypes |
| 3722 | * |
| 3723 | * Unregister @cfts. Files described by @cfts are removed from all |
| 3724 | * existing cgroups and all future cgroups won't have them either. This |
| 3725 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 3726 | * |
| 3727 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 3728 | * registered. |
| 3729 | */ |
| 3730 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 3731 | { |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3732 | int ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3733 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3734 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3735 | ret = cgroup_rm_cftypes_locked(cfts); |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3736 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3737 | return ret; |
| 3738 | } |
| 3739 | |
| 3740 | /** |
| 3741 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 3742 | * @ss: target cgroup subsystem |
| 3743 | * @cfts: zero-length name terminated array of cftypes |
| 3744 | * |
| 3745 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 3746 | * existing cgroups to which @ss is attached and all future cgroups will |
| 3747 | * have them too. This function can be called anytime whether @ss is |
| 3748 | * attached or not. |
| 3749 | * |
| 3750 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 3751 | * function currently returns 0 as long as @cfts registration is successful |
| 3752 | * even if some file creation attempts on existing cgroups fail. |
| 3753 | */ |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 3754 | static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3755 | { |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3756 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3757 | |
Tejun Heo | fc5ed1e | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 3758 | if (!cgroup_ssid_enabled(ss->id)) |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 3759 | return 0; |
| 3760 | |
Li Zefan | dc5736e | 2014-02-17 10:41:50 +0800 | [diff] [blame] | 3761 | if (!cfts || cfts[0].name[0] == '\0') |
| 3762 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3763 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3764 | ret = cgroup_init_cftypes(ss, cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3765 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3766 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3767 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3768 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3769 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3770 | list_add_tail(&cfts->node, &ss->cfts); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3771 | ret = cgroup_apply_cftypes(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3772 | if (ret) |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 3773 | cgroup_rm_cftypes_locked(cfts); |
| 3774 | |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3775 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3776 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3777 | } |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3778 | |
| 3779 | /** |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3780 | * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy |
| 3781 | * @ss: target cgroup subsystem |
| 3782 | * @cfts: zero-length name terminated array of cftypes |
| 3783 | * |
| 3784 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 3785 | * the default hierarchy. |
| 3786 | */ |
| 3787 | int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 3788 | { |
| 3789 | struct cftype *cft; |
| 3790 | |
| 3791 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
Tejun Heo | 05ebb6e | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3792 | cft->flags |= __CFTYPE_ONLY_ON_DFL; |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3793 | return cgroup_add_cftypes(ss, cfts); |
| 3794 | } |
| 3795 | |
| 3796 | /** |
| 3797 | * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies |
| 3798 | * @ss: target cgroup subsystem |
| 3799 | * @cfts: zero-length name terminated array of cftypes |
| 3800 | * |
| 3801 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 3802 | * the legacy hierarchies. |
| 3803 | */ |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 3804 | int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 3805 | { |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 3806 | struct cftype *cft; |
| 3807 | |
Tejun Heo | e4b7037 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 3808 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
| 3809 | cft->flags |= __CFTYPE_NOT_ON_DFL; |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 3810 | return cgroup_add_cftypes(ss, cfts); |
| 3811 | } |
| 3812 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3813 | /** |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3814 | * cgroup_file_notify - generate a file modified event for a cgroup_file |
| 3815 | * @cfile: target cgroup_file |
| 3816 | * |
| 3817 | * @cfile must have been obtained by setting cftype->file_offset. |
| 3818 | */ |
| 3819 | void cgroup_file_notify(struct cgroup_file *cfile) |
| 3820 | { |
| 3821 | unsigned long flags; |
| 3822 | |
| 3823 | spin_lock_irqsave(&cgroup_file_kn_lock, flags); |
Tejun Heo | b12e358 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 3824 | if (cfile->kn) { |
| 3825 | unsigned long last = cfile->notified_at; |
| 3826 | unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV; |
| 3827 | |
| 3828 | if (time_in_range(jiffies, last, next)) { |
| 3829 | timer_reduce(&cfile->notify_timer, next); |
| 3830 | } else { |
| 3831 | kernfs_notify(cfile->kn); |
| 3832 | cfile->notified_at = jiffies; |
| 3833 | } |
| 3834 | } |
Tejun Heo | 34c0625 | 2015-11-05 00:12:24 -0500 | [diff] [blame] | 3835 | spin_unlock_irqrestore(&cgroup_file_kn_lock, flags); |
| 3836 | } |
| 3837 | |
| 3838 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3839 | * css_next_child - find the next child of a given css |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3840 | * @pos: the current position (%NULL to initiate traversal) |
| 3841 | * @parent: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3842 | * |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3843 | * This function returns the next child of @parent and should be called |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3844 | * under either cgroup_mutex or RCU read lock. The only requirement is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3845 | * that @parent and @pos are accessible. The next sibling is guaranteed to |
| 3846 | * be returned regardless of their states. |
| 3847 | * |
| 3848 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 3849 | * css which finished ->css_online() is guaranteed to be visible in the |
| 3850 | * future iterations and will stay visible until the last reference is put. |
| 3851 | * A css which hasn't finished ->css_online() or already finished |
| 3852 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 3853 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3854 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3855 | struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, |
| 3856 | struct cgroup_subsys_state *parent) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3857 | { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3858 | struct cgroup_subsys_state *next; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3859 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3860 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3861 | |
| 3862 | /* |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 3863 | * @pos could already have been unlinked from the sibling list. |
| 3864 | * Once a cgroup is removed, its ->sibling.next is no longer |
| 3865 | * updated when its next sibling changes. CSS_RELEASED is set when |
| 3866 | * @pos is taken off list, at which time its next pointer is valid, |
| 3867 | * and, as releases are serialized, the one pointed to by the next |
| 3868 | * pointer is guaranteed to not have started release yet. This |
| 3869 | * implies that if we observe !CSS_RELEASED on @pos in this RCU |
| 3870 | * critical section, the one pointed to by its next pointer is |
| 3871 | * guaranteed to not have finished its RCU grace period even if we |
| 3872 | * have dropped rcu_read_lock() inbetween iterations. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3873 | * |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 3874 | * If @pos has CSS_RELEASED set, its next pointer can't be |
| 3875 | * dereferenced; however, as each css is given a monotonically |
| 3876 | * increasing unique serial number and always appended to the |
| 3877 | * sibling list, the next one can be found by walking the parent's |
| 3878 | * children until the first css with higher serial number than |
| 3879 | * @pos's. While this path can be slower, it happens iff iteration |
| 3880 | * races against release and the race window is very small. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3881 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3882 | if (!pos) { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3883 | next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling); |
| 3884 | } else if (likely(!(pos->flags & CSS_RELEASED))) { |
| 3885 | next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3886 | } else { |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3887 | list_for_each_entry_rcu(next, &parent->children, sibling) |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3888 | if (next->serial_nr > pos->serial_nr) |
| 3889 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3890 | } |
| 3891 | |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3892 | /* |
| 3893 | * @next, if not pointing to the head, can be dereferenced and is |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3894 | * the next sibling. |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3895 | */ |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 3896 | if (&next->sibling != &parent->children) |
| 3897 | return next; |
Tejun Heo | 3b281af | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 3898 | return NULL; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3899 | } |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3900 | |
| 3901 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3902 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3903 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3904 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3905 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3906 | * 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] | 3907 | * to visit for pre-order traversal of @root's descendants. @root is |
| 3908 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3909 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3910 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3911 | * doesn't require the whole traversal to be contained in a single critical |
| 3912 | * section. This function will return the correct next descendant as long |
| 3913 | * 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] | 3914 | * |
| 3915 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 3916 | * css which finished ->css_online() is guaranteed to be visible in the |
| 3917 | * future iterations and will stay visible until the last reference is put. |
| 3918 | * A css which hasn't finished ->css_online() or already finished |
| 3919 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 3920 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3921 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3922 | struct cgroup_subsys_state * |
| 3923 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 3924 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3925 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3926 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3927 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3928 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3929 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3930 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3931 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 3932 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3933 | |
| 3934 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3935 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3936 | if (next) |
| 3937 | return next; |
| 3938 | |
| 3939 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3940 | while (pos != root) { |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3941 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3942 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3943 | return next; |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3944 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3945 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3946 | |
| 3947 | return NULL; |
| 3948 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3949 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3950 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3951 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 3952 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3953 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3954 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 3955 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3956 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3957 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 3958 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3959 | * doesn't require the whole traversal to be contained in a single critical |
| 3960 | * section. This function will return the correct rightmost descendant as |
| 3961 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3962 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3963 | struct cgroup_subsys_state * |
| 3964 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3965 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3966 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3967 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 3968 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3969 | |
| 3970 | do { |
| 3971 | last = pos; |
| 3972 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3973 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3974 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3975 | pos = tmp; |
| 3976 | } while (pos); |
| 3977 | |
| 3978 | return last; |
| 3979 | } |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3980 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3981 | static struct cgroup_subsys_state * |
| 3982 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3983 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3984 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3985 | |
| 3986 | do { |
| 3987 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3988 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3989 | } while (pos); |
| 3990 | |
| 3991 | return last; |
| 3992 | } |
| 3993 | |
| 3994 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3995 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3996 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3997 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3998 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3999 | * 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] | 4000 | * to visit for post-order traversal of @root's descendants. @root is |
| 4001 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4002 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 4003 | * While this function requires cgroup_mutex or RCU read locking, it |
| 4004 | * doesn't require the whole traversal to be contained in a single critical |
| 4005 | * section. This function will return the correct next descendant as long |
| 4006 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 4007 | * @cgroup. |
Tejun Heo | c2931b7 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 4008 | * |
| 4009 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 4010 | * css which finished ->css_online() is guaranteed to be visible in the |
| 4011 | * future iterations and will stay visible until the last reference is put. |
| 4012 | * A css which hasn't finished ->css_online() or already finished |
| 4013 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 4014 | * responsibility to synchronize against on/offlining. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4015 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4016 | struct cgroup_subsys_state * |
| 4017 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 4018 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4019 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4020 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4021 | |
Tejun Heo | 8353da1 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 4022 | cgroup_assert_mutex_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4023 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 4024 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 4025 | if (!pos) |
| 4026 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4027 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 4028 | /* if we visited @root, we're done */ |
| 4029 | if (pos == root) |
| 4030 | return NULL; |
| 4031 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4032 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4033 | next = css_next_child(pos, pos->parent); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4034 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4035 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4036 | |
| 4037 | /* no sibling left, visit parent */ |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4038 | return pos->parent; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4039 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4040 | |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4041 | /** |
| 4042 | * css_has_online_children - does a css have online children |
| 4043 | * @css: the target css |
| 4044 | * |
| 4045 | * Returns %true if @css has any online children; otherwise, %false. This |
| 4046 | * function can be called from any context but the caller is responsible |
| 4047 | * for synchronizing against on/offlining as necessary. |
| 4048 | */ |
| 4049 | bool css_has_online_children(struct cgroup_subsys_state *css) |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4050 | { |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4051 | struct cgroup_subsys_state *child; |
| 4052 | bool ret = false; |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4053 | |
| 4054 | rcu_read_lock(); |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4055 | css_for_each_child(child, css) { |
Li Zefan | 99bae5f | 2014-06-12 14:31:31 +0800 | [diff] [blame] | 4056 | if (child->flags & CSS_ONLINE) { |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4057 | ret = true; |
| 4058 | break; |
Tejun Heo | cbc125e | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 4059 | } |
| 4060 | } |
| 4061 | rcu_read_unlock(); |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 4062 | return ret; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 4063 | } |
| 4064 | |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4065 | static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it) |
| 4066 | { |
| 4067 | struct list_head *l; |
| 4068 | struct cgrp_cset_link *link; |
| 4069 | struct css_set *cset; |
| 4070 | |
| 4071 | lockdep_assert_held(&css_set_lock); |
| 4072 | |
| 4073 | /* find the next threaded cset */ |
| 4074 | if (it->tcset_pos) { |
| 4075 | l = it->tcset_pos->next; |
| 4076 | |
| 4077 | if (l != it->tcset_head) { |
| 4078 | it->tcset_pos = l; |
| 4079 | return container_of(l, struct css_set, |
| 4080 | threaded_csets_node); |
| 4081 | } |
| 4082 | |
| 4083 | it->tcset_pos = NULL; |
| 4084 | } |
| 4085 | |
| 4086 | /* find the next cset */ |
| 4087 | l = it->cset_pos; |
| 4088 | l = l->next; |
| 4089 | if (l == it->cset_head) { |
| 4090 | it->cset_pos = NULL; |
| 4091 | return NULL; |
| 4092 | } |
| 4093 | |
| 4094 | if (it->ss) { |
| 4095 | cset = container_of(l, struct css_set, e_cset_node[it->ss->id]); |
| 4096 | } else { |
| 4097 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 4098 | cset = link->cset; |
| 4099 | } |
| 4100 | |
| 4101 | it->cset_pos = l; |
| 4102 | |
| 4103 | /* initialize threaded css_set walking */ |
| 4104 | if (it->flags & CSS_TASK_ITER_THREADED) { |
| 4105 | if (it->cur_dcset) |
| 4106 | put_css_set_locked(it->cur_dcset); |
| 4107 | it->cur_dcset = cset; |
| 4108 | get_css_set(cset); |
| 4109 | |
| 4110 | it->tcset_head = &cset->threaded_csets; |
| 4111 | it->tcset_pos = &cset->threaded_csets; |
| 4112 | } |
| 4113 | |
| 4114 | return cset; |
| 4115 | } |
| 4116 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4117 | /** |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4118 | * 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] | 4119 | * @it: the iterator to advance |
| 4120 | * |
| 4121 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4122 | */ |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4123 | 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] | 4124 | { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4125 | struct css_set *cset; |
| 4126 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 4127 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4128 | |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4129 | /* Advance to the next non-empty css_set */ |
| 4130 | do { |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4131 | cset = css_task_iter_next_css_set(it); |
| 4132 | if (!cset) { |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4133 | it->task_pos = NULL; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4134 | return; |
| 4135 | } |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 4136 | } while (!css_set_populated(cset)); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4137 | |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4138 | if (!list_empty(&cset->tasks)) |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4139 | it->task_pos = cset->tasks.next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4140 | else |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4141 | it->task_pos = cset->mg_tasks.next; |
| 4142 | |
| 4143 | it->tasks_head = &cset->tasks; |
| 4144 | it->mg_tasks_head = &cset->mg_tasks; |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4145 | |
| 4146 | /* |
| 4147 | * We don't keep css_sets locked across iteration steps and thus |
| 4148 | * need to take steps to ensure that iteration can be resumed after |
| 4149 | * the lock is re-acquired. Iteration is performed at two levels - |
| 4150 | * css_sets and tasks in them. |
| 4151 | * |
| 4152 | * Once created, a css_set never leaves its cgroup lists, so a |
| 4153 | * pinned css_set is guaranteed to stay put and we can resume |
| 4154 | * iteration afterwards. |
| 4155 | * |
| 4156 | * Tasks may leave @cset across iteration steps. This is resolved |
| 4157 | * by registering each iterator with the css_set currently being |
| 4158 | * walked and making css_set_move_task() advance iterators whose |
| 4159 | * next task is leaving. |
| 4160 | */ |
| 4161 | if (it->cur_cset) { |
| 4162 | list_del(&it->iters_node); |
| 4163 | put_css_set_locked(it->cur_cset); |
| 4164 | } |
| 4165 | get_css_set(cset); |
| 4166 | it->cur_cset = cset; |
| 4167 | list_add(&it->iters_node, &cset->task_iters); |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4168 | } |
| 4169 | |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4170 | static void css_task_iter_advance(struct css_task_iter *it) |
| 4171 | { |
Tejun Heo | 74d0833 | 2017-12-20 07:09:19 -0800 | [diff] [blame] | 4172 | struct list_head *next; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4173 | |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 4174 | lockdep_assert_held(&css_set_lock); |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4175 | repeat: |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4176 | /* |
| 4177 | * Advance iterator to find next entry. cset->tasks is consumed |
| 4178 | * first and then ->mg_tasks. After ->mg_tasks, we move onto the |
| 4179 | * next cset. |
| 4180 | */ |
Tejun Heo | 74d0833 | 2017-12-20 07:09:19 -0800 | [diff] [blame] | 4181 | next = it->task_pos->next; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4182 | |
Tejun Heo | 74d0833 | 2017-12-20 07:09:19 -0800 | [diff] [blame] | 4183 | if (next == it->tasks_head) |
| 4184 | next = it->mg_tasks_head->next; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4185 | |
Tejun Heo | 74d0833 | 2017-12-20 07:09:19 -0800 | [diff] [blame] | 4186 | if (next == it->mg_tasks_head) |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4187 | css_task_iter_advance_css_set(it); |
| 4188 | else |
Tejun Heo | 74d0833 | 2017-12-20 07:09:19 -0800 | [diff] [blame] | 4189 | it->task_pos = next; |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4190 | |
| 4191 | /* if PROCS, skip over tasks which aren't group leaders */ |
| 4192 | if ((it->flags & CSS_TASK_ITER_PROCS) && it->task_pos && |
| 4193 | !thread_group_leader(list_entry(it->task_pos, struct task_struct, |
| 4194 | cg_list))) |
| 4195 | goto repeat; |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4196 | } |
| 4197 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4198 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4199 | * css_task_iter_start - initiate task iteration |
| 4200 | * @css: the css to walk tasks of |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4201 | * @flags: CSS_TASK_ITER_* flags |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4202 | * @it: the task iterator to use |
| 4203 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4204 | * Initiate iteration through the tasks of @css. The caller can call |
| 4205 | * css_task_iter_next() to walk through the tasks until the function |
| 4206 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 4207 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4208 | */ |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4209 | 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] | 4210 | struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4211 | { |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 4212 | /* no one should try to iterate before mounting cgroups */ |
| 4213 | WARN_ON_ONCE(!use_task_css_set_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4214 | |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4215 | memset(it, 0, sizeof(*it)); |
| 4216 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4217 | spin_lock_irq(&css_set_lock); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4218 | |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4219 | it->ss = css->ss; |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4220 | it->flags = flags; |
Tejun Heo | 3ebb2b6 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4221 | |
| 4222 | if (it->ss) |
| 4223 | it->cset_pos = &css->cgroup->e_csets[css->ss->id]; |
| 4224 | else |
| 4225 | it->cset_pos = &css->cgroup->cset_links; |
| 4226 | |
Tejun Heo | 0f0a2b4 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4227 | it->cset_head = it->cset_pos; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4228 | |
Tejun Heo | ecb9d53 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4229 | css_task_iter_advance_css_set(it); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4230 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4231 | spin_unlock_irq(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4232 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4233 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4234 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4235 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4236 | * @it: the task iterator being iterated |
| 4237 | * |
| 4238 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4239 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 4240 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4241 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4242 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4243 | { |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4244 | if (it->cur_task) { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4245 | put_task_struct(it->cur_task); |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4246 | it->cur_task = NULL; |
| 4247 | } |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4248 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4249 | spin_lock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4250 | |
Tejun Heo | d574567 | 2015-10-29 11:43:05 +0900 | [diff] [blame] | 4251 | if (it->task_pos) { |
| 4252 | it->cur_task = list_entry(it->task_pos, struct task_struct, |
| 4253 | cg_list); |
| 4254 | get_task_struct(it->cur_task); |
| 4255 | css_task_iter_advance(it); |
| 4256 | } |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4257 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4258 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4259 | |
| 4260 | return it->cur_task; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4261 | } |
| 4262 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4263 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4264 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4265 | * @it: the task iterator to finish |
| 4266 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4267 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4268 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4269 | void css_task_iter_end(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4270 | { |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4271 | if (it->cur_cset) { |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4272 | spin_lock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4273 | list_del(&it->iters_node); |
| 4274 | put_css_set_locked(it->cur_cset); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 4275 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4276 | } |
| 4277 | |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4278 | if (it->cur_dcset) |
| 4279 | put_css_set(it->cur_dcset); |
| 4280 | |
Tejun Heo | ed27b9f | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 4281 | if (it->cur_task) |
| 4282 | put_task_struct(it->cur_task); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4283 | } |
| 4284 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4285 | static void cgroup_procs_release(struct kernfs_open_file *of) |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4286 | { |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4287 | if (of->priv) { |
| 4288 | css_task_iter_end(of->priv); |
| 4289 | kfree(of->priv); |
| 4290 | } |
| 4291 | } |
| 4292 | |
| 4293 | static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos) |
| 4294 | { |
| 4295 | struct kernfs_open_file *of = s->private; |
| 4296 | struct css_task_iter *it = of->priv; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 4297 | |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4298 | return css_task_iter_next(it); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 4299 | } |
| 4300 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4301 | static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos, |
| 4302 | unsigned int iter_flags) |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 4303 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4304 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4305 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4306 | struct css_task_iter *it = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4307 | |
| 4308 | /* |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4309 | * When a seq_file is seeked, it's always traversed sequentially |
| 4310 | * from position 0, so we can simply keep iterating on !0 *pos. |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4311 | */ |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4312 | if (!it) { |
| 4313 | if (WARN_ON_ONCE((*pos)++)) |
| 4314 | return ERR_PTR(-EINVAL); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4315 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4316 | it = kzalloc(sizeof(*it), GFP_KERNEL); |
| 4317 | if (!it) |
| 4318 | return ERR_PTR(-ENOMEM); |
| 4319 | of->priv = it; |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4320 | css_task_iter_start(&cgrp->self, iter_flags, it); |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4321 | } else if (!(*pos)++) { |
| 4322 | css_task_iter_end(it); |
Tejun Heo | 450ee0c | 2017-05-15 09:34:03 -0400 | [diff] [blame] | 4323 | css_task_iter_start(&cgrp->self, iter_flags, it); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4324 | } |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 4325 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4326 | return cgroup_procs_next(s, NULL, NULL); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4327 | } |
| 4328 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4329 | static void *cgroup_procs_start(struct seq_file *s, loff_t *pos) |
| 4330 | { |
| 4331 | struct cgroup *cgrp = seq_css(s)->cgroup; |
| 4332 | |
| 4333 | /* |
| 4334 | * All processes of a threaded subtree belong to the domain cgroup |
| 4335 | * of the subtree. Only threads can be distributed across the |
| 4336 | * subtree. Reject reads on cgroup.procs in the subtree proper. |
| 4337 | * They're always empty anyway. |
| 4338 | */ |
| 4339 | if (cgroup_is_threaded(cgrp)) |
| 4340 | return ERR_PTR(-EOPNOTSUPP); |
| 4341 | |
| 4342 | return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS | |
| 4343 | CSS_TASK_ITER_THREADED); |
| 4344 | } |
| 4345 | |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4346 | static int cgroup_procs_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4347 | { |
Tejun Heo | bc2fb7e | 2017-05-15 09:34:01 -0400 | [diff] [blame] | 4348 | seq_printf(s, "%d\n", task_pid_vnr(v)); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4349 | return 0; |
| 4350 | } |
| 4351 | |
Tejun Heo | 715c809 | 2017-05-15 09:34:00 -0400 | [diff] [blame] | 4352 | static int cgroup_procs_write_permission(struct cgroup *src_cgrp, |
| 4353 | struct cgroup *dst_cgrp, |
| 4354 | struct super_block *sb) |
| 4355 | { |
| 4356 | struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; |
| 4357 | struct cgroup *com_cgrp = src_cgrp; |
| 4358 | struct inode *inode; |
| 4359 | int ret; |
| 4360 | |
| 4361 | lockdep_assert_held(&cgroup_mutex); |
| 4362 | |
| 4363 | /* find the common ancestor */ |
| 4364 | while (!cgroup_is_descendant(dst_cgrp, com_cgrp)) |
| 4365 | com_cgrp = cgroup_parent(com_cgrp); |
| 4366 | |
| 4367 | /* %current should be authorized to migrate to the common ancestor */ |
| 4368 | inode = kernfs_get_inode(sb, com_cgrp->procs_file.kn); |
| 4369 | if (!inode) |
| 4370 | return -ENOMEM; |
| 4371 | |
| 4372 | ret = inode_permission(inode, MAY_WRITE); |
| 4373 | iput(inode); |
| 4374 | if (ret) |
| 4375 | return ret; |
| 4376 | |
| 4377 | /* |
| 4378 | * If namespaces are delegation boundaries, %current must be able |
| 4379 | * to see both source and destination cgroups from its namespace. |
| 4380 | */ |
| 4381 | if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) && |
| 4382 | (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) || |
| 4383 | !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp))) |
| 4384 | return -ENOENT; |
| 4385 | |
| 4386 | return 0; |
| 4387 | } |
| 4388 | |
| 4389 | static ssize_t cgroup_procs_write(struct kernfs_open_file *of, |
| 4390 | char *buf, size_t nbytes, loff_t off) |
| 4391 | { |
| 4392 | struct cgroup *src_cgrp, *dst_cgrp; |
| 4393 | struct task_struct *task; |
| 4394 | ssize_t ret; |
| 4395 | |
| 4396 | dst_cgrp = cgroup_kn_lock_live(of->kn, false); |
| 4397 | if (!dst_cgrp) |
| 4398 | return -ENODEV; |
| 4399 | |
| 4400 | task = cgroup_procs_write_start(buf, true); |
| 4401 | ret = PTR_ERR_OR_ZERO(task); |
| 4402 | if (ret) |
| 4403 | goto out_unlock; |
| 4404 | |
| 4405 | /* find the source cgroup */ |
| 4406 | spin_lock_irq(&css_set_lock); |
| 4407 | src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); |
| 4408 | spin_unlock_irq(&css_set_lock); |
| 4409 | |
| 4410 | ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, |
| 4411 | of->file->f_path.dentry->d_sb); |
| 4412 | if (ret) |
| 4413 | goto out_finish; |
| 4414 | |
| 4415 | ret = cgroup_attach_task(dst_cgrp, task, true); |
| 4416 | |
| 4417 | out_finish: |
| 4418 | cgroup_procs_write_finish(task); |
| 4419 | out_unlock: |
| 4420 | cgroup_kn_unlock(of->kn); |
| 4421 | |
| 4422 | return ret ?: nbytes; |
| 4423 | } |
| 4424 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4425 | static void *cgroup_threads_start(struct seq_file *s, loff_t *pos) |
| 4426 | { |
| 4427 | return __cgroup_procs_start(s, pos, 0); |
| 4428 | } |
| 4429 | |
| 4430 | static ssize_t cgroup_threads_write(struct kernfs_open_file *of, |
| 4431 | char *buf, size_t nbytes, loff_t off) |
| 4432 | { |
| 4433 | struct cgroup *src_cgrp, *dst_cgrp; |
| 4434 | struct task_struct *task; |
| 4435 | ssize_t ret; |
| 4436 | |
| 4437 | buf = strstrip(buf); |
| 4438 | |
| 4439 | dst_cgrp = cgroup_kn_lock_live(of->kn, false); |
| 4440 | if (!dst_cgrp) |
| 4441 | return -ENODEV; |
| 4442 | |
| 4443 | task = cgroup_procs_write_start(buf, false); |
| 4444 | ret = PTR_ERR_OR_ZERO(task); |
| 4445 | if (ret) |
| 4446 | goto out_unlock; |
| 4447 | |
| 4448 | /* find the source cgroup */ |
| 4449 | spin_lock_irq(&css_set_lock); |
| 4450 | src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); |
| 4451 | spin_unlock_irq(&css_set_lock); |
| 4452 | |
| 4453 | /* thread migrations follow the cgroup.procs delegation rule */ |
| 4454 | ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, |
| 4455 | of->file->f_path.dentry->d_sb); |
| 4456 | if (ret) |
| 4457 | goto out_finish; |
| 4458 | |
| 4459 | /* and must be contained in the same domain */ |
| 4460 | ret = -EOPNOTSUPP; |
| 4461 | if (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp) |
| 4462 | goto out_finish; |
| 4463 | |
| 4464 | ret = cgroup_attach_task(dst_cgrp, task, false); |
| 4465 | |
| 4466 | out_finish: |
| 4467 | cgroup_procs_write_finish(task); |
| 4468 | out_unlock: |
| 4469 | cgroup_kn_unlock(of->kn); |
| 4470 | |
| 4471 | return ret ?: nbytes; |
| 4472 | } |
| 4473 | |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4474 | /* cgroup core interface files for the default hierarchy */ |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 4475 | static struct cftype cgroup_base_files[] = { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4476 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4477 | .name = "cgroup.type", |
| 4478 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4479 | .seq_show = cgroup_type_show, |
| 4480 | .write = cgroup_type_write, |
| 4481 | }, |
| 4482 | { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4483 | .name = "cgroup.procs", |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 4484 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4485 | .file_offset = offsetof(struct cgroup, procs_file), |
Tejun Heo | b4b90a8 | 2016-12-27 14:49:04 -0500 | [diff] [blame] | 4486 | .release = cgroup_procs_release, |
| 4487 | .seq_start = cgroup_procs_start, |
| 4488 | .seq_next = cgroup_procs_next, |
| 4489 | .seq_show = cgroup_procs_show, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4490 | .write = cgroup_procs_write, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4491 | }, |
| 4492 | { |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4493 | .name = "cgroup.threads", |
Roman Gushchin | 4f58424 | 2018-01-10 04:35:12 -0800 | [diff] [blame] | 4494 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 4495 | .release = cgroup_procs_release, |
| 4496 | .seq_start = cgroup_threads_start, |
| 4497 | .seq_next = cgroup_procs_next, |
| 4498 | .seq_show = cgroup_procs_show, |
| 4499 | .write = cgroup_threads_write, |
| 4500 | }, |
| 4501 | { |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4502 | .name = "cgroup.controllers", |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4503 | .seq_show = cgroup_controllers_show, |
| 4504 | }, |
| 4505 | { |
| 4506 | .name = "cgroup.subtree_control", |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 4507 | .flags = CFTYPE_NS_DELEGATABLE, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4508 | .seq_show = cgroup_subtree_control_show, |
| 4509 | .write = cgroup_subtree_control_write, |
| 4510 | }, |
| 4511 | { |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 4512 | .name = "cgroup.events", |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4513 | .flags = CFTYPE_NOT_ON_ROOT, |
Tejun Heo | 6f60ead | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 4514 | .file_offset = offsetof(struct cgroup, events_file), |
Tejun Heo | 4a07c22 | 2015-09-18 17:54:22 -0400 | [diff] [blame] | 4515 | .seq_show = cgroup_events_show, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4516 | }, |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 4517 | { |
| 4518 | .name = "cgroup.max.descendants", |
| 4519 | .seq_show = cgroup_max_descendants_show, |
| 4520 | .write = cgroup_max_descendants_write, |
| 4521 | }, |
| 4522 | { |
| 4523 | .name = "cgroup.max.depth", |
| 4524 | .seq_show = cgroup_max_depth_show, |
| 4525 | .write = cgroup_max_depth_write, |
| 4526 | }, |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 4527 | { |
| 4528 | .name = "cgroup.stat", |
Tejun Heo | 3e48930 | 2017-08-11 05:49:01 -0700 | [diff] [blame] | 4529 | .seq_show = cgroup_stat_show, |
Roman Gushchin | ec39225 | 2017-08-02 17:55:31 +0100 | [diff] [blame] | 4530 | }, |
Tejun Heo | d41bf8c | 2017-10-23 16:18:27 -0700 | [diff] [blame] | 4531 | { |
| 4532 | .name = "cpu.stat", |
| 4533 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4534 | .seq_show = cpu_stat_show, |
| 4535 | }, |
Tejun Heo | a14c687 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 4536 | { } /* terminate */ |
| 4537 | }; |
| 4538 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4539 | /* |
| 4540 | * css destruction is four-stage process. |
| 4541 | * |
| 4542 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 4543 | * Implemented in kill_css(). |
| 4544 | * |
| 4545 | * 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] | 4546 | * and thus css_tryget_online() is guaranteed to fail, the css can be |
| 4547 | * offlined by invoking offline_css(). After offlining, the base ref is |
| 4548 | * put. Implemented in css_killed_work_fn(). |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4549 | * |
| 4550 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 4551 | * accessors are inside RCU read sections. css_release() schedules the |
| 4552 | * RCU callback. |
| 4553 | * |
| 4554 | * 4. After the grace period, the css can be freed. Implemented in |
| 4555 | * css_free_work_fn(). |
| 4556 | * |
| 4557 | * It is actually hairier because both step 2 and 4 require process context |
| 4558 | * and thus involve punting to css->destroy_work adding two additional |
| 4559 | * steps to the already complex sequence. |
| 4560 | */ |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4561 | static void css_free_rwork_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4562 | { |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4563 | struct cgroup_subsys_state *css = container_of(to_rcu_work(work), |
| 4564 | struct cgroup_subsys_state, destroy_rwork); |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4565 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4566 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4567 | |
Tejun Heo | 9a1049d | 2014-06-28 08:10:14 -0400 | [diff] [blame] | 4568 | percpu_ref_exit(&css->refcnt); |
| 4569 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4570 | if (ss) { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4571 | /* css free path */ |
Tejun Heo | 8bb5ef7 | 2016-01-21 15:32:15 -0500 | [diff] [blame] | 4572 | struct cgroup_subsys_state *parent = css->parent; |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4573 | int id = css->id; |
| 4574 | |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4575 | ss->css_free(css); |
| 4576 | cgroup_idr_remove(&ss->css_idr, id); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4577 | cgroup_put(cgrp); |
Tejun Heo | 8bb5ef7 | 2016-01-21 15:32:15 -0500 | [diff] [blame] | 4578 | |
| 4579 | if (parent) |
| 4580 | css_put(parent); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4581 | } else { |
| 4582 | /* cgroup free path */ |
| 4583 | atomic_dec(&cgrp->root->nr_cgrps); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 4584 | cgroup1_pidlist_destroy_all(cgrp); |
Zefan Li | 971ff49 | 2014-09-18 16:06:19 +0800 | [diff] [blame] | 4585 | cancel_work_sync(&cgrp->release_agent_work); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4586 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4587 | if (cgroup_parent(cgrp)) { |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4588 | /* |
| 4589 | * We get a ref to the parent, and put the ref when |
| 4590 | * this cgroup is being freed, so it's guaranteed |
| 4591 | * that the parent won't be destroyed before its |
| 4592 | * children. |
| 4593 | */ |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4594 | cgroup_put(cgroup_parent(cgrp)); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4595 | kernfs_put(cgrp->kn); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4596 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4597 | cgroup_rstat_exit(cgrp); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4598 | kfree(cgrp); |
| 4599 | } else { |
| 4600 | /* |
| 4601 | * This is root cgroup's refcnt reaching zero, |
| 4602 | * which indicates that the root should be |
| 4603 | * released. |
| 4604 | */ |
| 4605 | cgroup_destroy_root(cgrp->root); |
| 4606 | } |
| 4607 | } |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 4608 | } |
| 4609 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4610 | static void css_release_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4611 | { |
| 4612 | struct cgroup_subsys_state *css = |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4613 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4614 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4615 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4616 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4617 | mutex_lock(&cgroup_mutex); |
| 4618 | |
Tejun Heo | de3f034 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4619 | css->flags |= CSS_RELEASED; |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4620 | list_del_rcu(&css->sibling); |
| 4621 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4622 | if (ss) { |
| 4623 | /* css release path */ |
Vladimir Davydov | 01e5865 | 2015-02-12 14:59:26 -0800 | [diff] [blame] | 4624 | cgroup_idr_replace(&ss->css_idr, NULL, css->id); |
Tejun Heo | 7d172cc | 2014-11-18 02:49:51 -0500 | [diff] [blame] | 4625 | if (ss->css_released) |
| 4626 | ss->css_released(css); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4627 | } else { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4628 | struct cgroup *tcgrp; |
| 4629 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4630 | /* cgroup release path */ |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 4631 | trace_cgroup_release(cgrp); |
| 4632 | |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4633 | if (cgroup_on_dfl(cgrp)) |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4634 | cgroup_rstat_flush(cgrp); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4635 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4636 | for (tcgrp = cgroup_parent(cgrp); tcgrp; |
| 4637 | tcgrp = cgroup_parent(tcgrp)) |
| 4638 | tcgrp->nr_dying_descendants--; |
| 4639 | |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4640 | cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 4641 | cgrp->id = -1; |
Li Zefan | a418948 | 2014-09-04 14:43:07 +0800 | [diff] [blame] | 4642 | |
| 4643 | /* |
| 4644 | * There are two control paths which try to determine |
| 4645 | * cgroup from dentry without going through kernfs - |
| 4646 | * cgroupstats_build() and css_tryget_online_from_dir(). |
| 4647 | * Those are supported by RCU protecting clearing of |
| 4648 | * cgrp->kn->priv backpointer. |
| 4649 | */ |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4650 | if (cgrp->kn) |
| 4651 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, |
| 4652 | NULL); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 4653 | |
| 4654 | cgroup_bpf_put(cgrp); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4655 | } |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4656 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4657 | mutex_unlock(&cgroup_mutex); |
| 4658 | |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4659 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); |
| 4660 | queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4661 | } |
| 4662 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4663 | static void css_release(struct percpu_ref *ref) |
| 4664 | { |
| 4665 | struct cgroup_subsys_state *css = |
| 4666 | container_of(ref, struct cgroup_subsys_state, refcnt); |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4667 | |
Tejun Heo | 25e15d8 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4668 | INIT_WORK(&css->destroy_work, css_release_work_fn); |
| 4669 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4670 | } |
| 4671 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4672 | static void init_and_link_css(struct cgroup_subsys_state *css, |
| 4673 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4674 | { |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4675 | lockdep_assert_held(&cgroup_mutex); |
| 4676 | |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 4677 | cgroup_get_live(cgrp); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4678 | |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4679 | memset(css, 0, sizeof(*css)); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4680 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4681 | css->ss = ss; |
Tejun Heo | 8fa3b8d | 2016-05-26 15:42:13 -0400 | [diff] [blame] | 4682 | css->id = -1; |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4683 | INIT_LIST_HEAD(&css->sibling); |
| 4684 | INIT_LIST_HEAD(&css->children); |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4685 | css->serial_nr = css_serial_nr_next++; |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 4686 | atomic_set(&css->online_cnt, 0); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4687 | |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4688 | if (cgroup_parent(cgrp)) { |
| 4689 | css->parent = cgroup_css(cgroup_parent(cgrp), ss); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4690 | css_get(css->parent); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4691 | } |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4692 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4693 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4694 | } |
| 4695 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4696 | /* 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] | 4697 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4698 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4699 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4700 | int ret = 0; |
| 4701 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4702 | lockdep_assert_held(&cgroup_mutex); |
| 4703 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4704 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4705 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4706 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4707 | css->flags |= CSS_ONLINE; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4708 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 4709 | |
| 4710 | atomic_inc(&css->online_cnt); |
| 4711 | if (css->parent) |
| 4712 | atomic_inc(&css->parent->online_cnt); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4713 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4714 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4715 | } |
| 4716 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4717 | /* 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] | 4718 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4719 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 4720 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4721 | |
| 4722 | lockdep_assert_held(&cgroup_mutex); |
| 4723 | |
| 4724 | if (!(css->flags & CSS_ONLINE)) |
| 4725 | return; |
| 4726 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4727 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4728 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4729 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4730 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | e329780 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 4731 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); |
Tejun Heo | f8f22e5 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4732 | |
| 4733 | wake_up_all(&css->cgroup->offline_waitq); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4734 | } |
| 4735 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4736 | /** |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4737 | * css_create - create a cgroup_subsys_state |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4738 | * @cgrp: the cgroup new css will be associated with |
| 4739 | * @ss: the subsys of new css |
| 4740 | * |
| 4741 | * 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] | 4742 | * css is online and installed in @cgrp. This function doesn't create the |
| 4743 | * interface files. Returns 0 on success, -errno on failure. |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4744 | */ |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4745 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 4746 | struct cgroup_subsys *ss) |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4747 | { |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4748 | struct cgroup *parent = cgroup_parent(cgrp); |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4749 | struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4750 | struct cgroup_subsys_state *css; |
| 4751 | int err; |
| 4752 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4753 | lockdep_assert_held(&cgroup_mutex); |
| 4754 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4755 | css = ss->css_alloc(parent_css); |
Tejun Heo | e7e15b8 | 2016-06-21 13:06:24 -0400 | [diff] [blame] | 4756 | if (!css) |
| 4757 | css = ERR_PTR(-ENOMEM); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4758 | if (IS_ERR(css)) |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4759 | return css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4760 | |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4761 | init_and_link_css(css, ss, cgrp); |
Tejun Heo | a2bed82 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4762 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 4763 | err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4764 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4765 | goto err_free_css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4766 | |
Vladimir Davydov | cf780b7 | 2015-08-03 15:32:26 +0300 | [diff] [blame] | 4767 | err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4768 | if (err < 0) |
Wenwei Tao | b00c52d | 2016-05-13 22:59:20 +0800 | [diff] [blame] | 4769 | goto err_free_css; |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4770 | css->id = err; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4771 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4772 | /* @css is ready to be brought online now, make it visible */ |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4773 | list_add_tail_rcu(&css->sibling, &parent_css->children); |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 4774 | cgroup_idr_replace(&ss->css_idr, css, css->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4775 | |
| 4776 | err = online_css(css); |
| 4777 | if (err) |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4778 | goto err_list_del; |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4779 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4780 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
Tejun Heo | d51f39b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4781 | cgroup_parent(parent)) { |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4782 | 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] | 4783 | current->comm, current->pid, ss->name); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4784 | if (!strcmp(ss->name, "memory")) |
Joe Perches | ed3d261 | 2014-04-25 18:28:03 -0400 | [diff] [blame] | 4785 | 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] | 4786 | ss->warned_broken_hierarchy = true; |
| 4787 | } |
| 4788 | |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4789 | return css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4790 | |
Tejun Heo | 1fed1b2 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4791 | err_list_del: |
| 4792 | list_del_rcu(&css->sibling); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 4793 | err_free_css: |
Tejun Heo | 8f36aae | 2018-03-14 12:45:14 -0700 | [diff] [blame] | 4794 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); |
| 4795 | queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); |
Tejun Heo | 6cd0f5b | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4796 | return ERR_PTR(err); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4797 | } |
| 4798 | |
Tejun Heo | 07cd129 | 2017-01-26 16:47:28 -0500 | [diff] [blame] | 4799 | /* |
| 4800 | * The returned cgroup is fully initialized including its control mask, but |
| 4801 | * it isn't associated with its kernfs_node and doesn't have the control |
| 4802 | * mask applied. |
| 4803 | */ |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4804 | static struct cgroup *cgroup_create(struct cgroup *parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4805 | { |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4806 | struct cgroup_root *root = parent->root; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4807 | struct cgroup *cgrp, *tcgrp; |
| 4808 | int level = parent->level + 1; |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4809 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4810 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4811 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 4812 | cgrp = kzalloc(sizeof(*cgrp) + |
| 4813 | sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4814 | if (!cgrp) |
| 4815 | return ERR_PTR(-ENOMEM); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4816 | |
Tejun Heo | 2aad2a8 | 2014-09-24 13:31:50 -0400 | [diff] [blame] | 4817 | ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL); |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 4818 | if (ret) |
| 4819 | goto out_free_cgrp; |
| 4820 | |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4821 | if (cgroup_on_dfl(parent)) { |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4822 | ret = cgroup_rstat_init(cgrp); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4823 | if (ret) |
| 4824 | goto out_cancel_ref; |
| 4825 | } |
| 4826 | |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 4827 | /* |
| 4828 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4829 | * a half-baked cgroup. |
| 4830 | */ |
Vladimir Davydov | cf780b7 | 2015-08-03 15:32:26 +0300 | [diff] [blame] | 4831 | 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] | 4832 | if (cgrp->id < 0) { |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4833 | ret = -ENOMEM; |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4834 | goto out_stat_exit; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4835 | } |
| 4836 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4837 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4838 | |
Tejun Heo | 9d800df | 2014-05-14 09:15:00 -0400 | [diff] [blame] | 4839 | cgrp->self.parent = &parent->self; |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4840 | cgrp->root = root; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 4841 | cgrp->level = level; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 4842 | ret = cgroup_bpf_inherit(cgrp); |
| 4843 | if (ret) |
| 4844 | goto out_idr_free; |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 4845 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4846 | for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) { |
Tejun Heo | b11cfb5 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 4847 | cgrp->ancestor_ids[tcgrp->level] = tcgrp->id; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4848 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 4849 | if (tcgrp != cgrp) |
| 4850 | tcgrp->nr_descendants++; |
| 4851 | } |
| 4852 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4853 | if (notify_on_release(parent)) |
| 4854 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4855 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4856 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4857 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4858 | |
Tejun Heo | 0cb51d7 | 2014-05-16 13:22:49 -0400 | [diff] [blame] | 4859 | cgrp->self.serial_nr = css_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4860 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4861 | /* allocation complete, commit to creation */ |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 4862 | list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4863 | atomic_inc(&root->nr_cgrps); |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 4864 | cgroup_get_live(parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4865 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 4866 | /* |
| 4867 | * @cgrp is now fully operational. If something fails after this |
| 4868 | * point, it'll be released via the normal destruction path. |
| 4869 | */ |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 4870 | cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4871 | |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4872 | /* |
| 4873 | * On the default hierarchy, a child doesn't automatically inherit |
Tejun Heo | 667c249 | 2014-07-08 18:02:56 -0400 | [diff] [blame] | 4874 | * subtree_control from the parent. Each is configured manually. |
Tejun Heo | bd53d61 | 2014-04-23 11:13:16 -0400 | [diff] [blame] | 4875 | */ |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4876 | if (!cgroup_on_dfl(cgrp)) |
Tejun Heo | 5531dc9 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4877 | cgrp->subtree_control = cgroup_control(cgrp); |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4878 | |
| 4879 | cgroup_propagate_control(cgrp); |
| 4880 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4881 | return cgrp; |
| 4882 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 4883 | out_idr_free: |
| 4884 | cgroup_idr_remove(&root->cgroup_idr, cgrp->id); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 4885 | out_stat_exit: |
| 4886 | if (cgroup_on_dfl(parent)) |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 4887 | cgroup_rstat_exit(cgrp); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4888 | out_cancel_ref: |
| 4889 | percpu_ref_exit(&cgrp->self.refcnt); |
| 4890 | out_free_cgrp: |
| 4891 | kfree(cgrp); |
| 4892 | return ERR_PTR(ret); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4893 | } |
| 4894 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 4895 | static bool cgroup_check_hierarchy_limits(struct cgroup *parent) |
| 4896 | { |
| 4897 | struct cgroup *cgroup; |
| 4898 | int ret = false; |
| 4899 | int level = 1; |
| 4900 | |
| 4901 | lockdep_assert_held(&cgroup_mutex); |
| 4902 | |
| 4903 | for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) { |
| 4904 | if (cgroup->nr_descendants >= cgroup->max_descendants) |
| 4905 | goto fail; |
| 4906 | |
| 4907 | if (level > cgroup->max_depth) |
| 4908 | goto fail; |
| 4909 | |
| 4910 | level++; |
| 4911 | } |
| 4912 | |
| 4913 | ret = true; |
| 4914 | fail: |
| 4915 | return ret; |
| 4916 | } |
| 4917 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 4918 | 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] | 4919 | { |
| 4920 | struct cgroup *parent, *cgrp; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4921 | struct kernfs_node *kn; |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4922 | int ret; |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4923 | |
| 4924 | /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */ |
| 4925 | if (strchr(name, '\n')) |
| 4926 | return -EINVAL; |
| 4927 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4928 | parent = cgroup_kn_lock_live(parent_kn, false); |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4929 | if (!parent) |
| 4930 | return -ENODEV; |
| 4931 | |
Roman Gushchin | 1a926e0 | 2017-07-28 18:28:44 +0100 | [diff] [blame] | 4932 | if (!cgroup_check_hierarchy_limits(parent)) { |
| 4933 | ret = -EAGAIN; |
| 4934 | goto out_unlock; |
| 4935 | } |
| 4936 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4937 | cgrp = cgroup_create(parent); |
| 4938 | if (IS_ERR(cgrp)) { |
| 4939 | ret = PTR_ERR(cgrp); |
| 4940 | goto out_unlock; |
| 4941 | } |
| 4942 | |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4943 | /* create the directory */ |
| 4944 | kn = kernfs_create_dir(parent->kn, name, mode, cgrp); |
| 4945 | if (IS_ERR(kn)) { |
| 4946 | ret = PTR_ERR(kn); |
| 4947 | goto out_destroy; |
| 4948 | } |
| 4949 | cgrp->kn = kn; |
| 4950 | |
| 4951 | /* |
| 4952 | * This extra ref will be put in cgroup_free_fn() and guarantees |
| 4953 | * that @cgrp->kn is always accessible. |
| 4954 | */ |
| 4955 | kernfs_get(kn); |
| 4956 | |
| 4957 | ret = cgroup_kn_set_ugid(kn); |
| 4958 | if (ret) |
| 4959 | goto out_destroy; |
| 4960 | |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 4961 | ret = css_populate_dir(&cgrp->self); |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4962 | if (ret) |
| 4963 | goto out_destroy; |
| 4964 | |
Tejun Heo | 03970d3 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 4965 | ret = cgroup_apply_control_enable(cgrp); |
| 4966 | if (ret) |
| 4967 | goto out_destroy; |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4968 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 4969 | trace_cgroup_mkdir(cgrp); |
| 4970 | |
Tejun Heo | 195e9b6c | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4971 | /* let's create and online css's */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4972 | kernfs_activate(kn); |
| 4973 | |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4974 | ret = 0; |
| 4975 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4976 | |
Tejun Heo | a5bca21 | 2016-03-03 09:57:58 -0500 | [diff] [blame] | 4977 | out_destroy: |
| 4978 | cgroup_destroy_locked(cgrp); |
Tejun Heo | ba0f4d7 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4979 | out_unlock: |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 4980 | cgroup_kn_unlock(parent_kn); |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 4981 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4982 | } |
| 4983 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4984 | /* |
| 4985 | * 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] | 4986 | * css_tryget_online() is now guaranteed to fail. Tell the subsystem to |
| 4987 | * initate destruction and put the css ref from kill_css(). |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4988 | */ |
| 4989 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4990 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4991 | struct cgroup_subsys_state *css = |
| 4992 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4993 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4994 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4995 | |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 4996 | do { |
| 4997 | offline_css(css); |
| 4998 | css_put(css); |
| 4999 | /* @css can't go away while we're holding cgroup_mutex */ |
| 5000 | css = css->parent; |
| 5001 | } while (css && atomic_dec_and_test(&css->online_cnt)); |
| 5002 | |
| 5003 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5004 | } |
| 5005 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5006 | /* css kill confirmation processing requires process context, bounce */ |
| 5007 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5008 | { |
| 5009 | struct cgroup_subsys_state *css = |
| 5010 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 5011 | |
Tejun Heo | aa226ff | 2016-01-21 15:31:11 -0500 | [diff] [blame] | 5012 | if (atomic_dec_and_test(&css->online_cnt)) { |
| 5013 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
| 5014 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
| 5015 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5016 | } |
| 5017 | |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 5018 | /** |
| 5019 | * kill_css - destroy a css |
| 5020 | * @css: css to destroy |
| 5021 | * |
| 5022 | * This function initiates destruction of @css by removing cgroup interface |
| 5023 | * files and putting its base reference. ->css_offline() will be invoked |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5024 | * asynchronously once css_tryget_online() is guaranteed to fail and when |
| 5025 | * the reference count reaches zero, @css will be released. |
Tejun Heo | f392e51 | 2014-04-23 11:13:14 -0400 | [diff] [blame] | 5026 | */ |
| 5027 | static void kill_css(struct cgroup_subsys_state *css) |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5028 | { |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 5029 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5030 | |
Waiman Long | 33c35aa | 2017-05-15 09:34:06 -0400 | [diff] [blame] | 5031 | if (css->flags & CSS_DYING) |
| 5032 | return; |
| 5033 | |
| 5034 | css->flags |= CSS_DYING; |
| 5035 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5036 | /* |
| 5037 | * This must happen before css is disassociated with its cgroup. |
| 5038 | * See seq_css() for details. |
| 5039 | */ |
Tejun Heo | 334c367 | 2016-03-03 09:58:01 -0500 | [diff] [blame] | 5040 | css_clear_dir(css); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5041 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5042 | /* |
| 5043 | * Killing would put the base ref, but we need to keep it alive |
| 5044 | * until after ->css_offline(). |
| 5045 | */ |
| 5046 | css_get(css); |
| 5047 | |
| 5048 | /* |
| 5049 | * cgroup core guarantees that, by the time ->css_offline() is |
| 5050 | * invoked, no new css reference will be given out via |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5051 | * css_tryget_online(). We can't simply call percpu_ref_kill() and |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 5052 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 5053 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 5054 | * |
| 5055 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 5056 | * css is confirmed to be seen as killed on all CPUs. |
| 5057 | */ |
| 5058 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5059 | } |
| 5060 | |
| 5061 | /** |
| 5062 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 5063 | * @cgrp: cgroup to be destroyed |
| 5064 | * |
| 5065 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 5066 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5067 | * guarantee that css_tryget_online() won't succeed by the time |
| 5068 | * ->css_offline() is invoked. To satisfy all the requirements, |
| 5069 | * destruction is implemented in the following two steps. |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5070 | * |
| 5071 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 5072 | * userland visible parts and start killing the percpu refcnts of |
| 5073 | * css's. Set up so that the next stage will be kicked off once all |
| 5074 | * the percpu refcnts are confirmed to be killed. |
| 5075 | * |
| 5076 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 5077 | * rest of destruction. Once all cgroup references are gone, the |
| 5078 | * cgroup is RCU-freed. |
| 5079 | * |
| 5080 | * This function implements s1. After this step, @cgrp is gone as far as |
| 5081 | * the userland is concerned and a new cgroup with the same name may be |
| 5082 | * created. As cgroup doesn't care about the names internally, this |
| 5083 | * doesn't cause any problem. |
| 5084 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5085 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 5086 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5087 | { |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5088 | struct cgroup *tcgrp, *parent = cgroup_parent(cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5089 | struct cgroup_subsys_state *css; |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5090 | struct cgrp_cset_link *link; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 5091 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5092 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5093 | lockdep_assert_held(&cgroup_mutex); |
| 5094 | |
Tejun Heo | 91486f6 | 2015-10-15 16:41:51 -0400 | [diff] [blame] | 5095 | /* |
| 5096 | * Only migration can raise populated from zero and we're already |
| 5097 | * holding cgroup_mutex. |
| 5098 | */ |
| 5099 | if (cgroup_is_populated(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5100 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5101 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5102 | /* |
Tejun Heo | d5c419b | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 5103 | * Make sure there's no live children. We can't test emptiness of |
| 5104 | * ->self.children as dead children linger on it while being |
| 5105 | * drained; otherwise, "rmdir parent/child parent" may fail. |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 5106 | */ |
Tejun Heo | f3d4650 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 5107 | if (css_has_online_children(&cgrp->self)) |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 5108 | return -EBUSY; |
| 5109 | |
| 5110 | /* |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5111 | * Mark @cgrp and the associated csets dead. The former prevents |
| 5112 | * further task migration and child creation by disabling |
| 5113 | * cgroup_lock_live_group(). The latter makes the csets ignored by |
| 5114 | * the migration path. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5115 | */ |
Tejun Heo | 184faf3 | 2014-05-16 13:22:51 -0400 | [diff] [blame] | 5116 | cgrp->self.flags &= ~CSS_ONLINE; |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5117 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5118 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5119 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 5120 | link->cset->dead = true; |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5121 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2b021cb | 2016-03-15 20:43:04 -0400 | [diff] [blame] | 5122 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5123 | /* initiate massacre of all css's */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 5124 | for_each_css(css, ssid, cgrp) |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5125 | kill_css(css); |
| 5126 | |
Tejun Heo | 5faaf05 | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 5127 | /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */ |
| 5128 | css_clear_dir(&cgrp->self); |
Tejun Heo | 01f6474 | 2014-05-13 12:19:23 -0400 | [diff] [blame] | 5129 | kernfs_remove(cgrp->kn); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5130 | |
Tejun Heo | 454000a | 2017-05-15 09:34:02 -0400 | [diff] [blame] | 5131 | if (parent && cgroup_is_threaded(cgrp)) |
| 5132 | parent->nr_threaded_children--; |
| 5133 | |
Roman Gushchin | 0679dee | 2017-08-02 17:55:29 +0100 | [diff] [blame] | 5134 | for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) { |
| 5135 | tcgrp->nr_descendants--; |
| 5136 | tcgrp->nr_dying_descendants++; |
| 5137 | } |
| 5138 | |
Roman Gushchin | 5a621e6 | 2017-08-02 17:55:32 +0100 | [diff] [blame] | 5139 | cgroup1_check_for_release(parent); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5140 | |
Tejun Heo | 249f346 | 2014-05-14 09:15:01 -0400 | [diff] [blame] | 5141 | /* put the base reference */ |
Tejun Heo | 9d755d3 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5142 | percpu_ref_kill(&cgrp->self.refcnt); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 5143 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 5144 | return 0; |
| 5145 | }; |
| 5146 | |
Tejun Heo | 1592c9b | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5147 | int cgroup_rmdir(struct kernfs_node *kn) |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5148 | { |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5149 | struct cgroup *cgrp; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5150 | int ret = 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5151 | |
Tejun Heo | 945ba19 | 2016-03-03 09:58:00 -0500 | [diff] [blame] | 5152 | cgrp = cgroup_kn_lock_live(kn, false); |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5153 | if (!cgrp) |
| 5154 | return 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5155 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5156 | ret = cgroup_destroy_locked(cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5157 | |
Tejun Heo | ed1777d | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5158 | if (!ret) |
| 5159 | trace_cgroup_rmdir(cgrp); |
| 5160 | |
Tejun Heo | a9746d8 | 2014-05-13 12:19:22 -0400 | [diff] [blame] | 5161 | cgroup_kn_unlock(kn); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 5162 | return ret; |
| 5163 | } |
| 5164 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5165 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
Tejun Heo | 5136f63 | 2017-06-27 14:30:28 -0400 | [diff] [blame] | 5166 | .show_options = cgroup_show_options, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5167 | .remount_fs = cgroup_remount, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5168 | .mkdir = cgroup_mkdir, |
| 5169 | .rmdir = cgroup_rmdir, |
Serge E. Hallyn | 4f41fc5 | 2016-05-09 09:59:55 -0500 | [diff] [blame] | 5170 | .show_path = cgroup_show_path, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5171 | }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5172 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5173 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5174 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5175 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 5176 | |
Tejun Heo | a5ae989 | 2015-12-29 14:53:56 -0500 | [diff] [blame] | 5177 | pr_debug("Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5178 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5179 | mutex_lock(&cgroup_mutex); |
| 5180 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5181 | idr_init(&ss->css_idr); |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 5182 | INIT_LIST_HEAD(&ss->cfts); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5183 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5184 | /* Create the root cgroup state for this subsystem */ |
| 5185 | ss->root = &cgrp_dfl_root; |
| 5186 | css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5187 | /* We don't handle early failures gracefully */ |
| 5188 | BUG_ON(IS_ERR(css)); |
Tejun Heo | ddfcada | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5189 | init_and_link_css(css, ss, &cgrp_dfl_root.cgrp); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 5190 | |
| 5191 | /* |
| 5192 | * Root csses are never destroyed and we can't initialize |
| 5193 | * percpu_ref during early init. Disable refcnting. |
| 5194 | */ |
| 5195 | css->flags |= CSS_NO_REF; |
| 5196 | |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5197 | if (early) { |
Tejun Heo | 9395a45 | 2014-05-14 09:15:02 -0400 | [diff] [blame] | 5198 | /* allocation can't be done safely during early init */ |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5199 | css->id = 1; |
| 5200 | } else { |
| 5201 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL); |
| 5202 | BUG_ON(css->id < 0); |
| 5203 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5204 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5205 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5206 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5207 | * newly registered, all tasks and hence the |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5208 | * init_css_set is in the subsystem's root cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5209 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5210 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 5211 | have_fork_callback |= (bool)ss->fork << ss->id; |
| 5212 | have_exit_callback |= (bool)ss->exit << ss->id; |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5213 | have_free_callback |= (bool)ss->free << ss->id; |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5214 | have_canfork_callback |= (bool)ss->can_fork << ss->id; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5215 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 5216 | /* At system boot, before all subsystems have been |
| 5217 | * registered, no tasks have been forked, so we don't |
| 5218 | * need to invoke fork callbacks here. */ |
| 5219 | BUG_ON(!list_empty(&init_task.tasks)); |
| 5220 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 5221 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 5222 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 5223 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5227 | * cgroup_init_early - cgroup initialization at system boot |
| 5228 | * |
| 5229 | * Initialize cgroups at system boot, and initialize any |
| 5230 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5231 | */ |
| 5232 | int __init cgroup_init_early(void) |
| 5233 | { |
Tejun Heo | 7b9a6ba | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 5234 | static struct cgroup_sb_opts __initdata opts; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5235 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5236 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5237 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5238 | init_cgroup_root(&cgrp_dfl_root, &opts); |
Tejun Heo | 3b514d2 | 2014-05-16 13:22:47 -0400 | [diff] [blame] | 5239 | cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF; |
| 5240 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5241 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5242 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5243 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5244 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Xiubo Li | 63253ad | 2016-02-26 13:07:38 +0800 | [diff] [blame] | 5245 | "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] | 5246 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5247 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5248 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 5249 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5250 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5251 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5252 | ss->name = cgroup_subsys_name[i]; |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5253 | if (!ss->legacy_name) |
| 5254 | ss->legacy_name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5255 | |
| 5256 | if (ss->early_init) |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5257 | cgroup_init_subsys(ss, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5258 | } |
| 5259 | return 0; |
| 5260 | } |
| 5261 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 5262 | static u16 cgroup_disable_mask __initdata; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5263 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5264 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5265 | * cgroup_init - cgroup initialization |
| 5266 | * |
| 5267 | * Register cgroup filesystem and /proc file, and initialize |
| 5268 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5269 | */ |
| 5270 | int __init cgroup_init(void) |
| 5271 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5272 | struct cgroup_subsys *ss; |
Tejun Heo | 035f4f5 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 5273 | int ssid; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5274 | |
Tejun Heo | 6e5c830 | 2016-02-22 22:25:47 -0500 | [diff] [blame] | 5275 | BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16); |
Tejun Heo | 1ed1328 | 2015-09-16 12:53:17 -0400 | [diff] [blame] | 5276 | BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem)); |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5277 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); |
| 5278 | BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5279 | |
Tejun Heo | c58632b | 2018-04-26 14:29:04 -0700 | [diff] [blame] | 5280 | cgroup_rstat_boot(); |
Tejun Heo | 041cd64 | 2017-09-25 08:12:05 -0700 | [diff] [blame] | 5281 | |
Peter Zijlstra | 3942a9b | 2016-08-11 18:54:13 +0200 | [diff] [blame] | 5282 | /* |
| 5283 | * The latency of the synchronize_sched() is too high for cgroups, |
| 5284 | * avoid it at the cost of forcing all readers into the slow path. |
| 5285 | */ |
| 5286 | rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss); |
| 5287 | |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 5288 | get_user_ns(init_cgroup_ns.user_ns); |
| 5289 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5290 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5291 | |
Tejun Heo | 2378d8b | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 5292 | /* |
| 5293 | * Add init_css_set to the hash table so that dfl_root can link to |
| 5294 | * it during init. |
| 5295 | */ |
| 5296 | hash_add(css_set_table, &init_css_set.hlist, |
| 5297 | css_set_hash(init_css_set.subsys)); |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5298 | |
Zefan Li | 9732adc | 2017-04-19 10:15:59 +0800 | [diff] [blame] | 5299 | BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0, 0)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5300 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5301 | mutex_unlock(&cgroup_mutex); |
| 5302 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5303 | for_each_subsys(ss, ssid) { |
Tejun Heo | 15a4c83 | 2014-05-04 15:09:14 -0400 | [diff] [blame] | 5304 | if (ss->early_init) { |
| 5305 | struct cgroup_subsys_state *css = |
| 5306 | init_css_set.subsys[ss->id]; |
| 5307 | |
| 5308 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, |
| 5309 | GFP_KERNEL); |
| 5310 | BUG_ON(css->id < 0); |
| 5311 | } else { |
| 5312 | cgroup_init_subsys(ss, false); |
| 5313 | } |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5314 | |
Tejun Heo | 2d8f243 | 2014-04-23 11:13:15 -0400 | [diff] [blame] | 5315 | list_add_tail(&init_css_set.e_cset_node[ssid], |
| 5316 | &cgrp_dfl_root.cgrp.e_csets[ssid]); |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5317 | |
| 5318 | /* |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 5319 | * Setting dfl_root subsys_mask needs to consider the |
| 5320 | * disabled flag and cftype registration needs kmalloc, |
| 5321 | * both of which aren't available during early_init. |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5322 | */ |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5323 | if (cgroup_disable_mask & (1 << ssid)) { |
| 5324 | static_branch_disable(cgroup_subsys_enabled_key[ssid]); |
| 5325 | printk(KERN_INFO "Disabling %s control group subsystem\n", |
| 5326 | ss->name); |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5327 | continue; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5328 | } |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5329 | |
Tejun Heo | d62beb7 | 2016-12-27 14:49:08 -0500 | [diff] [blame] | 5330 | if (cgroup1_ssid_disabled(ssid)) |
Johannes Weiner | 223ffb2 | 2016-02-11 13:34:49 -0500 | [diff] [blame] | 5331 | printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n", |
| 5332 | ss->name); |
| 5333 | |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5334 | cgrp_dfl_root.subsys_mask |= 1 << ss->id; |
| 5335 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5336 | /* implicit controllers must be threaded too */ |
| 5337 | WARN_ON(ss->implicit_on_dfl && !ss->threaded); |
| 5338 | |
Tejun Heo | f6d635ad | 2016-03-08 11:51:26 -0500 | [diff] [blame] | 5339 | if (ss->implicit_on_dfl) |
| 5340 | cgrp_dfl_implicit_ss_mask |= 1 << ss->id; |
| 5341 | else if (!ss->dfl_cftypes) |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 5342 | cgrp_dfl_inhibit_ss_mask |= 1 << ss->id; |
Tejun Heo | 5de4fa1 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5343 | |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 5344 | if (ss->threaded) |
| 5345 | cgrp_dfl_threaded_ss_mask |= 1 << ss->id; |
| 5346 | |
Tejun Heo | a8ddc82 | 2014-07-15 11:05:10 -0400 | [diff] [blame] | 5347 | if (ss->dfl_cftypes == ss->legacy_cftypes) { |
| 5348 | WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes)); |
| 5349 | } else { |
| 5350 | WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes)); |
| 5351 | WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes)); |
Li Zefan | c731ae1 | 2014-06-05 17:16:30 +0800 | [diff] [blame] | 5352 | } |
Vladimir Davydov | 295458e | 2015-02-19 17:34:46 +0300 | [diff] [blame] | 5353 | |
| 5354 | if (ss->bind) |
| 5355 | ss->bind(init_css_set.subsys[ssid]); |
Tejun Heo | 7af608e | 2017-07-18 17:57:46 -0400 | [diff] [blame] | 5356 | |
| 5357 | mutex_lock(&cgroup_mutex); |
| 5358 | css_populate_dir(init_css_set.subsys[ssid]); |
| 5359 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5360 | } |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5361 | |
Tejun Heo | 2378d8b | 2016-03-03 09:57:57 -0500 | [diff] [blame] | 5362 | /* init_css_set.subsys[] has been updated, re-hash */ |
| 5363 | hash_del(&init_css_set.hlist); |
| 5364 | hash_add(css_set_table, &init_css_set.hlist, |
| 5365 | css_set_hash(init_css_set.subsys)); |
| 5366 | |
Tejun Heo | 035f4f5 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 5367 | WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup")); |
| 5368 | WARN_ON(register_filesystem(&cgroup_fs_type)); |
Tejun Heo | 67e9c74 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 5369 | WARN_ON(register_filesystem(&cgroup2_fs_type)); |
Tejun Heo | 035f4f5 | 2015-10-15 17:00:43 -0400 | [diff] [blame] | 5370 | WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations)); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5371 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5372 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5373 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5374 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5375 | static int __init cgroup_wq_init(void) |
| 5376 | { |
| 5377 | /* |
| 5378 | * There isn't much point in executing destruction path in |
| 5379 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 5380 | * Use 1 for @max_active. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5381 | * |
| 5382 | * We would prefer to do this in cgroup_init() above, but that |
| 5383 | * is called before init_workqueues(): so leave this until after. |
| 5384 | */ |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 5385 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 5386 | BUG_ON(!cgroup_destroy_wq); |
| 5387 | return 0; |
| 5388 | } |
| 5389 | core_initcall(cgroup_wq_init); |
| 5390 | |
Shaohua Li | 69fd5c3 | 2017-07-12 11:49:55 -0700 | [diff] [blame] | 5391 | void cgroup_path_from_kernfs_id(const union kernfs_node_id *id, |
| 5392 | char *buf, size_t buflen) |
| 5393 | { |
| 5394 | struct kernfs_node *kn; |
| 5395 | |
| 5396 | kn = kernfs_get_node_by_id(cgrp_dfl_root.kf_root, id); |
| 5397 | if (!kn) |
| 5398 | return; |
| 5399 | kernfs_path(kn, buf, buflen); |
| 5400 | kernfs_put(kn); |
| 5401 | } |
| 5402 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5403 | /* |
| 5404 | * proc_cgroup_show() |
| 5405 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 5406 | * - Used for /proc/<pid>/cgroup. |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5407 | */ |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 5408 | int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, |
| 5409 | struct pid *pid, struct task_struct *tsk) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5410 | { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5411 | char *buf; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5412 | int retval; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 5413 | struct cgroup_root *root; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5414 | |
| 5415 | retval = -ENOMEM; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 5416 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5417 | if (!buf) |
| 5418 | goto out; |
| 5419 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5420 | mutex_lock(&cgroup_mutex); |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5421 | spin_lock_irq(&css_set_lock); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5422 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5423 | for_each_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5424 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5425 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 5426 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5427 | |
Tejun Heo | a716526 | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 5428 | if (root == &cgrp_dfl_root && !cgrp_dfl_visible) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 5429 | continue; |
| 5430 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5431 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | d98817d | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5432 | if (root != &cgrp_dfl_root) |
| 5433 | for_each_subsys(ss, ssid) |
| 5434 | if (root->subsys_mask & (1 << ssid)) |
| 5435 | seq_printf(m, "%s%s", count++ ? "," : "", |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5436 | ss->legacy_name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 5437 | if (strlen(root->name)) |
| 5438 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 5439 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5440 | seq_putc(m, ':'); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5441 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5442 | cgrp = task_cgroup_from_root(tsk, root); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5443 | |
| 5444 | /* |
| 5445 | * On traditional hierarchies, all zombie tasks show up as |
| 5446 | * belonging to the root cgroup. On the default hierarchy, |
| 5447 | * while a zombie doesn't show up in "cgroup.procs" and |
| 5448 | * thus can't be migrated, its /proc/PID/cgroup keeps |
| 5449 | * reporting the cgroup it belonged to before exiting. If |
| 5450 | * the cgroup is removed before the zombie is reaped, |
| 5451 | * " (deleted)" is appended to the cgroup path. |
| 5452 | */ |
| 5453 | if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5454 | retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX, |
Aditya Kali | a79a908 | 2016-01-29 02:54:06 -0600 | [diff] [blame] | 5455 | current->nsproxy->cgroup_ns); |
Tejun Heo | e022300 | 2016-09-29 15:49:40 +0200 | [diff] [blame] | 5456 | if (retval >= PATH_MAX) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5457 | retval = -ENAMETOOLONG; |
Tejun Heo | e022300 | 2016-09-29 15:49:40 +0200 | [diff] [blame] | 5458 | if (retval < 0) |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5459 | goto out_unlock; |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5460 | |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 5461 | seq_puts(m, buf); |
| 5462 | } else { |
| 5463 | seq_puts(m, "/"); |
| 5464 | } |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5465 | |
| 5466 | if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp)) |
| 5467 | seq_puts(m, " (deleted)\n"); |
| 5468 | else |
| 5469 | seq_putc(m, '\n'); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5470 | } |
| 5471 | |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 5472 | retval = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5473 | out_unlock: |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5474 | spin_unlock_irq(&css_set_lock); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5475 | mutex_unlock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5476 | kfree(buf); |
| 5477 | out: |
| 5478 | return retval; |
| 5479 | } |
| 5480 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5481 | /** |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5482 | * cgroup_fork - initialize cgroup related fields during copy_process() |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5483 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5484 | * |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5485 | * A task is associated with the init_css_set until cgroup_post_fork() |
| 5486 | * attaches it to the parent's css_set. Empty cg_list indicates that |
| 5487 | * @child isn't holding reference to its css_set. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5488 | */ |
| 5489 | void cgroup_fork(struct task_struct *child) |
| 5490 | { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5491 | RCU_INIT_POINTER(child->cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5492 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | /** |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5496 | * cgroup_can_fork - called on a new task before the process is exposed |
| 5497 | * @child: the task in question. |
| 5498 | * |
| 5499 | * This calls the subsystem can_fork() callbacks. If the can_fork() callback |
| 5500 | * returns an error, the fork aborts with that error code. This allows for |
| 5501 | * a cgroup subsystem to conditionally allow or deny new forks. |
| 5502 | */ |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5503 | int cgroup_can_fork(struct task_struct *child) |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5504 | { |
| 5505 | struct cgroup_subsys *ss; |
| 5506 | int i, j, ret; |
| 5507 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5508 | do_each_subsys_mask(ss, i, have_canfork_callback) { |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5509 | ret = ss->can_fork(child); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5510 | if (ret) |
| 5511 | goto out_revert; |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5512 | } while_each_subsys_mask(); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5513 | |
| 5514 | return 0; |
| 5515 | |
| 5516 | out_revert: |
| 5517 | for_each_subsys(ss, j) { |
| 5518 | if (j >= i) |
| 5519 | break; |
| 5520 | if (ss->cancel_fork) |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5521 | ss->cancel_fork(child); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5522 | } |
| 5523 | |
| 5524 | return ret; |
| 5525 | } |
| 5526 | |
| 5527 | /** |
| 5528 | * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork() |
| 5529 | * @child: the task in question |
| 5530 | * |
| 5531 | * This calls the cancel_fork() callbacks if a fork failed *after* |
| 5532 | * cgroup_can_fork() succeded. |
| 5533 | */ |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5534 | void cgroup_cancel_fork(struct task_struct *child) |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5535 | { |
| 5536 | struct cgroup_subsys *ss; |
| 5537 | int i; |
| 5538 | |
| 5539 | for_each_subsys(ss, i) |
| 5540 | if (ss->cancel_fork) |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5541 | ss->cancel_fork(child); |
Aleksa Sarai | 7e47682 | 2015-06-09 21:32:09 +1000 | [diff] [blame] | 5542 | } |
| 5543 | |
| 5544 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5545 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5546 | * @child: the task in question |
| 5547 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5548 | * Adds the task to the list running through its css_set if necessary and |
| 5549 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5550 | * 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] | 5551 | * 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] | 5552 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5553 | */ |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5554 | void cgroup_post_fork(struct task_struct *child) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5555 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5556 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5557 | int i; |
| 5558 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5559 | /* |
Dongsheng Yang | 251f8c0 | 2014-08-25 19:27:52 +0800 | [diff] [blame] | 5560 | * This may race against cgroup_enable_task_cg_lists(). As that |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5561 | * function sets use_task_css_set_links before grabbing |
| 5562 | * tasklist_lock and we just went through tasklist_lock to add |
| 5563 | * @child, it's guaranteed that either we see the set |
| 5564 | * use_task_css_set_links or cgroup_enable_task_cg_lists() sees |
| 5565 | * @child during its iteration. |
| 5566 | * |
| 5567 | * If we won the race, @child is associated with %current's |
Tejun Heo | f0d9a5f | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5568 | * css_set. Grabbing css_set_lock guarantees both that the |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5569 | * association is stable, and, on completion of the parent's |
| 5570 | * migration, @child is visible in the source of migration or |
| 5571 | * already in the destination cgroup. This guarantee is necessary |
| 5572 | * when implementing operations which need to migrate all tasks of |
| 5573 | * a cgroup to another. |
| 5574 | * |
Dongsheng Yang | 251f8c0 | 2014-08-25 19:27:52 +0800 | [diff] [blame] | 5575 | * Note that if we lose to cgroup_enable_task_cg_lists(), @child |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5576 | * will remain in init_css_set. This is safe because all tasks are |
| 5577 | * in the init_css_set before cg_links is enabled and there's no |
| 5578 | * operation which transfers all tasks out of init_css_set. |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5579 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5580 | if (use_task_css_set_links) { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5581 | struct css_set *cset; |
| 5582 | |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5583 | spin_lock_irq(&css_set_lock); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5584 | cset = task_css_set(current); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5585 | if (list_empty(&child->cg_list)) { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5586 | get_css_set(cset); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 5587 | cset->nr_tasks++; |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 5588 | css_set_move_task(child, NULL, cset, false); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5589 | } |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5590 | spin_unlock_irq(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5591 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5592 | |
| 5593 | /* |
| 5594 | * Call ss->fork(). This must happen after @child is linked on |
| 5595 | * css_set; otherwise, @child might change state between ->fork() |
| 5596 | * and addition to css_set. |
| 5597 | */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5598 | do_each_subsys_mask(ss, i, have_fork_callback) { |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 5599 | ss->fork(child); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5600 | } while_each_subsys_mask(); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5601 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5602 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5603 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5604 | * cgroup_exit - detach cgroup from exiting task |
| 5605 | * @tsk: pointer to task_struct of exiting process |
| 5606 | * |
| 5607 | * Description: Detach cgroup from @tsk and release it. |
| 5608 | * |
| 5609 | * Note that cgroups marked notify_on_release force every task in |
| 5610 | * them to take the global cgroup_mutex mutex when exiting. |
| 5611 | * This could impact scaling on very large systems. Be reluctant to |
| 5612 | * use notify_on_release cgroups where very high task exit scaling |
| 5613 | * is required on large systems. |
| 5614 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5615 | * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We |
| 5616 | * call cgroup_exit() while the task is still competent to handle |
| 5617 | * notify_on_release(), then leave the task attached to the root cgroup in |
| 5618 | * each hierarchy for the remainder of its exit. No need to bother with |
| 5619 | * 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] | 5620 | * with migration path - PF_EXITING is visible to migration path. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5621 | */ |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 5622 | void cgroup_exit(struct task_struct *tsk) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5623 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5624 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5625 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5626 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5627 | |
| 5628 | /* |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 5629 | * 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] | 5630 | * with us, we can check css_set and cg_list without synchronization. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5631 | */ |
Tejun Heo | 0de0942 | 2015-10-15 16:41:49 -0400 | [diff] [blame] | 5632 | cset = task_css_set(tsk); |
| 5633 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5634 | if (!list_empty(&tsk->cg_list)) { |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5635 | spin_lock_irq(&css_set_lock); |
Tejun Heo | f6d7d04 | 2015-10-15 16:41:52 -0400 | [diff] [blame] | 5636 | css_set_move_task(tsk, cset, NULL, false); |
Waiman Long | 73a7242 | 2017-06-13 17:18:01 -0400 | [diff] [blame] | 5637 | cset->nr_tasks--; |
Daniel Bristot de Oliveira | 82d6489 | 2016-06-22 17:28:41 -0300 | [diff] [blame] | 5638 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5639 | } else { |
| 5640 | get_css_set(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5641 | } |
| 5642 | |
Aleksa Sarai | cb4a316 | 2015-06-06 10:02:14 +1000 | [diff] [blame] | 5643 | /* see cgroup_post_fork() for details */ |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5644 | do_each_subsys_mask(ss, i, have_exit_callback) { |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5645 | ss->exit(tsk); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5646 | } while_each_subsys_mask(); |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5647 | } |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5648 | |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5649 | void cgroup_free(struct task_struct *task) |
| 5650 | { |
| 5651 | struct css_set *cset = task_css_set(task); |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5652 | struct cgroup_subsys *ss; |
| 5653 | int ssid; |
| 5654 | |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5655 | do_each_subsys_mask(ss, ssid, have_free_callback) { |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5656 | ss->free(task); |
Tejun Heo | b4e0eea | 2016-02-22 22:25:46 -0500 | [diff] [blame] | 5657 | } while_each_subsys_mask(); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5658 | |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 5659 | put_css_set(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5660 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5661 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5662 | static int __init cgroup_disable(char *str) |
| 5663 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5664 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5665 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5666 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5667 | |
| 5668 | while ((token = strsep(&str, ",")) != NULL) { |
| 5669 | if (!*token) |
| 5670 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5671 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 5672 | for_each_subsys(ss, i) { |
Tejun Heo | 3e1d2ee | 2015-08-18 13:58:16 -0700 | [diff] [blame] | 5673 | if (strcmp(token, ss->name) && |
| 5674 | strcmp(token, ss->legacy_name)) |
| 5675 | continue; |
Tejun Heo | a3e7273 | 2015-09-25 16:24:27 -0400 | [diff] [blame] | 5676 | cgroup_disable_mask |= 1 << i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5677 | } |
| 5678 | } |
| 5679 | return 1; |
| 5680 | } |
| 5681 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5682 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5683 | /** |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5684 | * css_tryget_online_from_dir - get corresponding css from a cgroup dentry |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5685 | * @dentry: directory dentry of interest |
| 5686 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 5687 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5688 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 5689 | * to get the corresponding css and return it. If such css doesn't exist |
| 5690 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5691 | */ |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5692 | struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, |
| 5693 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5694 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5695 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
Tejun Heo | f17fc25 | 2016-02-23 10:00:51 -0500 | [diff] [blame] | 5696 | struct file_system_type *s_type = dentry->d_sb->s_type; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5697 | struct cgroup_subsys_state *css = NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5698 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5699 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 5700 | /* is @dentry a cgroup dir? */ |
Tejun Heo | f17fc25 | 2016-02-23 10:00:51 -0500 | [diff] [blame] | 5701 | if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) || |
| 5702 | !kn || kernfs_type(kn) != KERNFS_DIR) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5703 | return ERR_PTR(-EBADF); |
| 5704 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5705 | rcu_read_lock(); |
| 5706 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5707 | /* |
| 5708 | * This path doesn't originate from kernfs and @kn could already |
| 5709 | * have been or be removed at any point. @kn->priv is RCU |
Li Zefan | a418948 | 2014-09-04 14:43:07 +0800 | [diff] [blame] | 5710 | * protected for this access. See css_release_work_fn() for details. |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5711 | */ |
Tejun Heo | e0aed7c | 2016-12-27 14:49:09 -0500 | [diff] [blame] | 5712 | cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 5713 | if (cgrp) |
| 5714 | css = cgroup_css(cgrp, ss); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5715 | |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5716 | if (!css || !css_tryget_online(css)) |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5717 | css = ERR_PTR(-ENOENT); |
| 5718 | |
| 5719 | rcu_read_unlock(); |
| 5720 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5721 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5722 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 5723 | /** |
| 5724 | * css_from_id - lookup css by id |
| 5725 | * @id: the cgroup id |
| 5726 | * @ss: cgroup subsys to be looked into |
| 5727 | * |
| 5728 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 5729 | * Should be called under rcu_read_lock(). |
| 5730 | */ |
| 5731 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 5732 | { |
Tejun Heo | 6fa4918 | 2014-05-04 15:09:13 -0400 | [diff] [blame] | 5733 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Johannes Weiner | d6ccc55 | 2016-06-17 12:24:27 -0400 | [diff] [blame] | 5734 | return idr_find(&ss->css_idr, id); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5735 | } |
| 5736 | |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 5737 | /** |
| 5738 | * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path |
| 5739 | * @path: path on the default hierarchy |
| 5740 | * |
| 5741 | * Find the cgroup at @path on the default hierarchy, increment its |
| 5742 | * reference count and return it. Returns pointer to the found cgroup on |
| 5743 | * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR) |
| 5744 | * if @path points to a non-directory. |
| 5745 | */ |
| 5746 | struct cgroup *cgroup_get_from_path(const char *path) |
| 5747 | { |
| 5748 | struct kernfs_node *kn; |
| 5749 | struct cgroup *cgrp; |
| 5750 | |
| 5751 | mutex_lock(&cgroup_mutex); |
| 5752 | |
| 5753 | kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path); |
| 5754 | if (kn) { |
| 5755 | if (kernfs_type(kn) == KERNFS_DIR) { |
| 5756 | cgrp = kn->priv; |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 5757 | cgroup_get_live(cgrp); |
Tejun Heo | 16af439 | 2015-11-20 15:55:52 -0500 | [diff] [blame] | 5758 | } else { |
| 5759 | cgrp = ERR_PTR(-ENOTDIR); |
| 5760 | } |
| 5761 | kernfs_put(kn); |
| 5762 | } else { |
| 5763 | cgrp = ERR_PTR(-ENOENT); |
| 5764 | } |
| 5765 | |
| 5766 | mutex_unlock(&cgroup_mutex); |
| 5767 | return cgrp; |
| 5768 | } |
| 5769 | EXPORT_SYMBOL_GPL(cgroup_get_from_path); |
| 5770 | |
Martin KaFai Lau | 1f3fe7e | 2016-06-30 10:28:42 -0700 | [diff] [blame] | 5771 | /** |
| 5772 | * cgroup_get_from_fd - get a cgroup pointer from a fd |
| 5773 | * @fd: fd obtained by open(cgroup2_dir) |
| 5774 | * |
| 5775 | * Find the cgroup from a fd which should be obtained |
| 5776 | * by opening a cgroup directory. Returns a pointer to the |
| 5777 | * cgroup on success. ERR_PTR is returned if the cgroup |
| 5778 | * cannot be found. |
| 5779 | */ |
| 5780 | struct cgroup *cgroup_get_from_fd(int fd) |
| 5781 | { |
| 5782 | struct cgroup_subsys_state *css; |
| 5783 | struct cgroup *cgrp; |
| 5784 | struct file *f; |
| 5785 | |
| 5786 | f = fget_raw(fd); |
| 5787 | if (!f) |
| 5788 | return ERR_PTR(-EBADF); |
| 5789 | |
| 5790 | css = css_tryget_online_from_dir(f->f_path.dentry, NULL); |
| 5791 | fput(f); |
| 5792 | if (IS_ERR(css)) |
| 5793 | return ERR_CAST(css); |
| 5794 | |
| 5795 | cgrp = css->cgroup; |
| 5796 | if (!cgroup_on_dfl(cgrp)) { |
| 5797 | cgroup_put(cgrp); |
| 5798 | return ERR_PTR(-EBADF); |
| 5799 | } |
| 5800 | |
| 5801 | return cgrp; |
| 5802 | } |
| 5803 | EXPORT_SYMBOL_GPL(cgroup_get_from_fd); |
| 5804 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 5805 | /* |
| 5806 | * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data |
| 5807 | * definition in cgroup-defs.h. |
| 5808 | */ |
| 5809 | #ifdef CONFIG_SOCK_CGROUP_DATA |
| 5810 | |
| 5811 | #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID) |
| 5812 | |
Tejun Heo | 3fa4cc9 | 2015-12-14 11:24:06 -0500 | [diff] [blame] | 5813 | DEFINE_SPINLOCK(cgroup_sk_update_lock); |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 5814 | static bool cgroup_sk_alloc_disabled __read_mostly; |
| 5815 | |
| 5816 | void cgroup_sk_alloc_disable(void) |
| 5817 | { |
| 5818 | if (cgroup_sk_alloc_disabled) |
| 5819 | return; |
| 5820 | pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n"); |
| 5821 | cgroup_sk_alloc_disabled = true; |
| 5822 | } |
| 5823 | |
| 5824 | #else |
| 5825 | |
| 5826 | #define cgroup_sk_alloc_disabled false |
| 5827 | |
| 5828 | #endif |
| 5829 | |
| 5830 | void cgroup_sk_alloc(struct sock_cgroup_data *skcd) |
| 5831 | { |
| 5832 | if (cgroup_sk_alloc_disabled) |
| 5833 | return; |
| 5834 | |
Johannes Weiner | d979a39 | 2016-09-19 14:44:38 -0700 | [diff] [blame] | 5835 | /* Socket clone path */ |
| 5836 | if (skcd->val) { |
Tejun Heo | a590b90 | 2017-04-28 15:14:55 -0400 | [diff] [blame] | 5837 | /* |
| 5838 | * We might be cloning a socket which is left in an empty |
| 5839 | * cgroup and the cgroup might have already been rmdir'd. |
| 5840 | * Don't use cgroup_get_live(). |
| 5841 | */ |
Johannes Weiner | d979a39 | 2016-09-19 14:44:38 -0700 | [diff] [blame] | 5842 | cgroup_get(sock_cgroup_ptr(skcd)); |
| 5843 | return; |
| 5844 | } |
| 5845 | |
Tejun Heo | bd1060a | 2015-12-07 17:38:53 -0500 | [diff] [blame] | 5846 | rcu_read_lock(); |
| 5847 | |
| 5848 | while (true) { |
| 5849 | struct css_set *cset; |
| 5850 | |
| 5851 | cset = task_css_set(current); |
| 5852 | if (likely(cgroup_tryget(cset->dfl_cgrp))) { |
| 5853 | skcd->val = (unsigned long)cset->dfl_cgrp; |
| 5854 | break; |
| 5855 | } |
| 5856 | cpu_relax(); |
| 5857 | } |
| 5858 | |
| 5859 | rcu_read_unlock(); |
| 5860 | } |
| 5861 | |
| 5862 | void cgroup_sk_free(struct sock_cgroup_data *skcd) |
| 5863 | { |
| 5864 | cgroup_put(sock_cgroup_ptr(skcd)); |
| 5865 | } |
| 5866 | |
| 5867 | #endif /* CONFIG_SOCK_CGROUP_DATA */ |
| 5868 | |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5869 | #ifdef CONFIG_CGROUP_BPF |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 5870 | int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 5871 | enum bpf_attach_type type, u32 flags) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5872 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 5873 | int ret; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5874 | |
| 5875 | mutex_lock(&cgroup_mutex); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 5876 | ret = __cgroup_bpf_attach(cgrp, prog, type, flags); |
| 5877 | mutex_unlock(&cgroup_mutex); |
| 5878 | return ret; |
| 5879 | } |
| 5880 | int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 5881 | enum bpf_attach_type type, u32 flags) |
| 5882 | { |
| 5883 | int ret; |
| 5884 | |
| 5885 | mutex_lock(&cgroup_mutex); |
| 5886 | ret = __cgroup_bpf_detach(cgrp, prog, type, flags); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5887 | mutex_unlock(&cgroup_mutex); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 5888 | return ret; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5889 | } |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 5890 | int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr, |
| 5891 | union bpf_attr __user *uattr) |
| 5892 | { |
| 5893 | int ret; |
| 5894 | |
| 5895 | mutex_lock(&cgroup_mutex); |
| 5896 | ret = __cgroup_bpf_query(cgrp, attr, uattr); |
| 5897 | mutex_unlock(&cgroup_mutex); |
| 5898 | return ret; |
| 5899 | } |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5900 | #endif /* CONFIG_CGROUP_BPF */ |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 5901 | |
| 5902 | #ifdef CONFIG_SYSFS |
| 5903 | static ssize_t show_delegatable_files(struct cftype *files, char *buf, |
| 5904 | ssize_t size, const char *prefix) |
| 5905 | { |
| 5906 | struct cftype *cft; |
| 5907 | ssize_t ret = 0; |
| 5908 | |
| 5909 | for (cft = files; cft && cft->name[0] != '\0'; cft++) { |
| 5910 | if (!(cft->flags & CFTYPE_NS_DELEGATABLE)) |
| 5911 | continue; |
| 5912 | |
| 5913 | if (prefix) |
| 5914 | ret += snprintf(buf + ret, size - ret, "%s.", prefix); |
| 5915 | |
| 5916 | ret += snprintf(buf + ret, size - ret, "%s\n", cft->name); |
| 5917 | |
| 5918 | if (unlikely(ret >= size)) { |
| 5919 | WARN_ON(1); |
| 5920 | break; |
| 5921 | } |
| 5922 | } |
| 5923 | |
| 5924 | return ret; |
| 5925 | } |
| 5926 | |
| 5927 | static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 5928 | char *buf) |
| 5929 | { |
| 5930 | struct cgroup_subsys *ss; |
| 5931 | int ssid; |
| 5932 | ssize_t ret = 0; |
| 5933 | |
| 5934 | ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret, |
| 5935 | NULL); |
| 5936 | |
| 5937 | for_each_subsys(ss, ssid) |
| 5938 | ret += show_delegatable_files(ss->dfl_cftypes, buf + ret, |
| 5939 | PAGE_SIZE - ret, |
| 5940 | cgroup_subsys_name[ssid]); |
| 5941 | |
| 5942 | return ret; |
| 5943 | } |
| 5944 | static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate); |
| 5945 | |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 5946 | static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 5947 | char *buf) |
| 5948 | { |
| 5949 | return snprintf(buf, PAGE_SIZE, "nsdelegate\n"); |
| 5950 | } |
| 5951 | static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features); |
| 5952 | |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 5953 | static struct attribute *cgroup_sysfs_attrs[] = { |
| 5954 | &cgroup_delegate_attr.attr, |
Roman Gushchin | 5f2e673 | 2017-11-06 13:30:29 -0500 | [diff] [blame] | 5955 | &cgroup_features_attr.attr, |
Roman Gushchin | 01ee6cf | 2017-11-06 13:30:28 -0500 | [diff] [blame] | 5956 | NULL, |
| 5957 | }; |
| 5958 | |
| 5959 | static const struct attribute_group cgroup_sysfs_attr_group = { |
| 5960 | .attrs = cgroup_sysfs_attrs, |
| 5961 | .name = "cgroup", |
| 5962 | }; |
| 5963 | |
| 5964 | static int __init cgroup_sysfs_init(void) |
| 5965 | { |
| 5966 | return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group); |
| 5967 | } |
| 5968 | subsys_initcall(cgroup_sysfs_init); |
| 5969 | #endif /* CONFIG_SYSFS */ |