| 1 | /* | 
|---|
| 2 | *  Generic process-grouping system. | 
|---|
| 3 | * | 
|---|
| 4 | *  Based originally on the cpuset system, extracted by Paul Menage | 
|---|
| 5 | *  Copyright (C) 2006 Google, Inc | 
|---|
| 6 | * | 
|---|
| 7 | *  Notifications support | 
|---|
| 8 | *  Copyright (C) 2009 Nokia Corporation | 
|---|
| 9 | *  Author: Kirill A. Shutemov | 
|---|
| 10 | * | 
|---|
| 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 |  | 
|---|
| 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | 
|---|
| 30 |  | 
|---|
| 31 | #include "cgroup-internal.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include <linux/bpf-cgroup.h> | 
|---|
| 34 | #include <linux/cred.h> | 
|---|
| 35 | #include <linux/errno.h> | 
|---|
| 36 | #include <linux/init_task.h> | 
|---|
| 37 | #include <linux/kernel.h> | 
|---|
| 38 | #include <linux/magic.h> | 
|---|
| 39 | #include <linux/mutex.h> | 
|---|
| 40 | #include <linux/mount.h> | 
|---|
| 41 | #include <linux/pagemap.h> | 
|---|
| 42 | #include <linux/proc_fs.h> | 
|---|
| 43 | #include <linux/rcupdate.h> | 
|---|
| 44 | #include <linux/sched.h> | 
|---|
| 45 | #include <linux/sched/task.h> | 
|---|
| 46 | #include <linux/slab.h> | 
|---|
| 47 | #include <linux/spinlock.h> | 
|---|
| 48 | #include <linux/percpu-rwsem.h> | 
|---|
| 49 | #include <linux/string.h> | 
|---|
| 50 | #include <linux/hashtable.h> | 
|---|
| 51 | #include <linux/idr.h> | 
|---|
| 52 | #include <linux/kthread.h> | 
|---|
| 53 | #include <linux/atomic.h> | 
|---|
| 54 | #include <linux/cpuset.h> | 
|---|
| 55 | #include <linux/proc_ns.h> | 
|---|
| 56 | #include <linux/nsproxy.h> | 
|---|
| 57 | #include <linux/file.h> | 
|---|
| 58 | #include <linux/fs_parser.h> | 
|---|
| 59 | #include <linux/sched/cputime.h> | 
|---|
| 60 | #include <linux/sched/deadline.h> | 
|---|
| 61 | #include <linux/psi.h> | 
|---|
| 62 | #include <linux/nstree.h> | 
|---|
| 63 | #include <net/sock.h> | 
|---|
| 64 |  | 
|---|
| 65 | #define CREATE_TRACE_POINTS | 
|---|
| 66 | #include <trace/events/cgroup.h> | 
|---|
| 67 |  | 
|---|
| 68 | #define CGROUP_FILE_NAME_MAX		(MAX_CGROUP_TYPE_NAMELEN +	\ | 
|---|
| 69 | MAX_CFTYPE_NAME + 2) | 
|---|
| 70 | /* let's not notify more than 100 times per second */ | 
|---|
| 71 | #define CGROUP_FILE_NOTIFY_MIN_INTV	DIV_ROUND_UP(HZ, 100) | 
|---|
| 72 |  | 
|---|
| 73 | /* | 
|---|
| 74 | * To avoid confusing the compiler (and generating warnings) with code | 
|---|
| 75 | * that attempts to access what would be a 0-element array (i.e. sized | 
|---|
| 76 | * to a potentially empty array when CGROUP_SUBSYS_COUNT == 0), this | 
|---|
| 77 | * constant expression can be added. | 
|---|
| 78 | */ | 
|---|
| 79 | #define CGROUP_HAS_SUBSYS_CONFIG	(CGROUP_SUBSYS_COUNT > 0) | 
|---|
| 80 |  | 
|---|
| 81 | /* | 
|---|
| 82 | * cgroup_mutex is the master lock.  Any modification to cgroup or its | 
|---|
| 83 | * hierarchy must be performed while holding it. | 
|---|
| 84 | * | 
|---|
| 85 | * css_set_lock protects task->cgroups pointer, the list of css_set | 
|---|
| 86 | * objects, and the chain of tasks off each css_set. | 
|---|
| 87 | * | 
|---|
| 88 | * These locks are exported if CONFIG_PROVE_RCU so that accessors in | 
|---|
| 89 | * cgroup.h can use them for lockdep annotations. | 
|---|
| 90 | */ | 
|---|
| 91 | DEFINE_MUTEX(cgroup_mutex); | 
|---|
| 92 | DEFINE_SPINLOCK(css_set_lock); | 
|---|
| 93 |  | 
|---|
| 94 | #if (defined CONFIG_PROVE_RCU || defined CONFIG_LOCKDEP) | 
|---|
| 95 | EXPORT_SYMBOL_GPL(cgroup_mutex); | 
|---|
| 96 | EXPORT_SYMBOL_GPL(css_set_lock); | 
|---|
| 97 | #endif | 
|---|
| 98 |  | 
|---|
| 99 | struct blocking_notifier_head cgroup_lifetime_notifier = | 
|---|
| 100 | BLOCKING_NOTIFIER_INIT(cgroup_lifetime_notifier); | 
|---|
| 101 |  | 
|---|
| 102 | DEFINE_SPINLOCK(trace_cgroup_path_lock); | 
|---|
| 103 | char trace_cgroup_path[TRACE_CGROUP_PATH_LEN]; | 
|---|
| 104 | static bool cgroup_debug __read_mostly; | 
|---|
| 105 |  | 
|---|
| 106 | /* | 
|---|
| 107 | * Protects cgroup_idr and css_idr so that IDs can be released without | 
|---|
| 108 | * grabbing cgroup_mutex. | 
|---|
| 109 | */ | 
|---|
| 110 | static DEFINE_SPINLOCK(cgroup_idr_lock); | 
|---|
| 111 |  | 
|---|
| 112 | /* | 
|---|
| 113 | * Protects cgroup_file->kn for !self csses.  It synchronizes notifications | 
|---|
| 114 | * against file removal/re-creation across css hiding. | 
|---|
| 115 | */ | 
|---|
| 116 | static DEFINE_SPINLOCK(cgroup_file_kn_lock); | 
|---|
| 117 |  | 
|---|
| 118 | DEFINE_PERCPU_RWSEM(cgroup_threadgroup_rwsem); | 
|---|
| 119 |  | 
|---|
| 120 | #define cgroup_assert_mutex_or_rcu_locked()				\ | 
|---|
| 121 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&			\ | 
|---|
| 122 | !lockdep_is_held(&cgroup_mutex),		\ | 
|---|
| 123 | "cgroup_mutex or RCU read lock required"); | 
|---|
| 124 |  | 
|---|
| 125 | /* | 
|---|
| 126 | * cgroup destruction makes heavy use of work items and there can be a lot | 
|---|
| 127 | * of concurrent destructions.  Use a separate workqueue so that cgroup | 
|---|
| 128 | * destruction work items don't end up filling up max_active of system_percpu_wq | 
|---|
| 129 | * which may lead to deadlock. | 
|---|
| 130 | * | 
|---|
| 131 | * A cgroup destruction should enqueue work sequentially to: | 
|---|
| 132 | * cgroup_offline_wq: use for css offline work | 
|---|
| 133 | * cgroup_release_wq: use for css release work | 
|---|
| 134 | * cgroup_free_wq: use for free work | 
|---|
| 135 | * | 
|---|
| 136 | * Rationale for using separate workqueues: | 
|---|
| 137 | * The cgroup root free work may depend on completion of other css offline | 
|---|
| 138 | * operations. If all tasks were enqueued to a single workqueue, this could | 
|---|
| 139 | * create a deadlock scenario where: | 
|---|
| 140 | * - Free work waits for other css offline work to complete. | 
|---|
| 141 | * - But other css offline work is queued after free work in the same queue. | 
|---|
| 142 | * | 
|---|
| 143 | * Example deadlock scenario with single workqueue (cgroup_destroy_wq): | 
|---|
| 144 | * 1. umount net_prio | 
|---|
| 145 | * 2. net_prio root destruction enqueues work to cgroup_destroy_wq (CPUx) | 
|---|
| 146 | * 3. perf_event CSS A offline enqueues work to same cgroup_destroy_wq (CPUx) | 
|---|
| 147 | * 4. net_prio cgroup_destroy_root->cgroup_lock_and_drain_offline. | 
|---|
| 148 | * 5. net_prio root destruction blocks waiting for perf_event CSS A offline, | 
|---|
| 149 | *    which can never complete as it's behind in the same queue and | 
|---|
| 150 | *    workqueue's max_active is 1. | 
|---|
| 151 | */ | 
|---|
| 152 | static struct workqueue_struct *cgroup_offline_wq; | 
|---|
| 153 | static struct workqueue_struct *cgroup_release_wq; | 
|---|
| 154 | static struct workqueue_struct *cgroup_free_wq; | 
|---|
| 155 |  | 
|---|
| 156 | /* generate an array of cgroup subsystem pointers */ | 
|---|
| 157 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, | 
|---|
| 158 | struct cgroup_subsys *cgroup_subsys[] = { | 
|---|
| 159 | #include <linux/cgroup_subsys.h> | 
|---|
| 160 | }; | 
|---|
| 161 | #undef SUBSYS | 
|---|
| 162 |  | 
|---|
| 163 | /* array of cgroup subsystem names */ | 
|---|
| 164 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, | 
|---|
| 165 | static const char *cgroup_subsys_name[] = { | 
|---|
| 166 | #include <linux/cgroup_subsys.h> | 
|---|
| 167 | }; | 
|---|
| 168 | #undef SUBSYS | 
|---|
| 169 |  | 
|---|
| 170 | /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */ | 
|---|
| 171 | #define SUBSYS(_x)								\ | 
|---|
| 172 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key);			\ | 
|---|
| 173 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key);			\ | 
|---|
| 174 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key);			\ | 
|---|
| 175 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key); | 
|---|
| 176 | #include <linux/cgroup_subsys.h> | 
|---|
| 177 | #undef SUBSYS | 
|---|
| 178 |  | 
|---|
| 179 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key, | 
|---|
| 180 | static struct static_key_true *cgroup_subsys_enabled_key[] = { | 
|---|
| 181 | #include <linux/cgroup_subsys.h> | 
|---|
| 182 | }; | 
|---|
| 183 | #undef SUBSYS | 
|---|
| 184 |  | 
|---|
| 185 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key, | 
|---|
| 186 | static struct static_key_true *cgroup_subsys_on_dfl_key[] = { | 
|---|
| 187 | #include <linux/cgroup_subsys.h> | 
|---|
| 188 | }; | 
|---|
| 189 | #undef SUBSYS | 
|---|
| 190 |  | 
|---|
| 191 | static DEFINE_PER_CPU(struct css_rstat_cpu, root_rstat_cpu); | 
|---|
| 192 | static DEFINE_PER_CPU(struct cgroup_rstat_base_cpu, root_rstat_base_cpu); | 
|---|
| 193 |  | 
|---|
| 194 | /* the default hierarchy */ | 
|---|
| 195 | struct cgroup_root cgrp_dfl_root = { | 
|---|
| 196 | .cgrp.self.rstat_cpu = &root_rstat_cpu, | 
|---|
| 197 | .cgrp.rstat_base_cpu = &root_rstat_base_cpu, | 
|---|
| 198 | }; | 
|---|
| 199 | EXPORT_SYMBOL_GPL(cgrp_dfl_root); | 
|---|
| 200 |  | 
|---|
| 201 | /* | 
|---|
| 202 | * The default hierarchy always exists but is hidden until mounted for the | 
|---|
| 203 | * first time.  This is for backward compatibility. | 
|---|
| 204 | */ | 
|---|
| 205 | bool cgrp_dfl_visible; | 
|---|
| 206 |  | 
|---|
| 207 | /* some controllers are not supported in the default hierarchy */ | 
|---|
| 208 | static u16 cgrp_dfl_inhibit_ss_mask; | 
|---|
| 209 |  | 
|---|
| 210 | /* some controllers are implicitly enabled on the default hierarchy */ | 
|---|
| 211 | static u16 cgrp_dfl_implicit_ss_mask; | 
|---|
| 212 |  | 
|---|
| 213 | /* some controllers can be threaded on the default hierarchy */ | 
|---|
| 214 | static u16 cgrp_dfl_threaded_ss_mask; | 
|---|
| 215 |  | 
|---|
| 216 | /* The list of hierarchy roots */ | 
|---|
| 217 | LIST_HEAD(cgroup_roots); | 
|---|
| 218 | static int cgroup_root_count; | 
|---|
| 219 |  | 
|---|
| 220 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ | 
|---|
| 221 | static DEFINE_IDR(cgroup_hierarchy_idr); | 
|---|
| 222 |  | 
|---|
| 223 | /* | 
|---|
| 224 | * Assign a monotonically increasing serial number to csses.  It guarantees | 
|---|
| 225 | * cgroups with bigger numbers are newer than those with smaller numbers. | 
|---|
| 226 | * Also, as csses are always appended to the parent's ->children list, it | 
|---|
| 227 | * guarantees that sibling csses are always sorted in the ascending serial | 
|---|
| 228 | * number order on the list.  Protected by cgroup_mutex. | 
|---|
| 229 | */ | 
|---|
| 230 | static u64 css_serial_nr_next = 1; | 
|---|
| 231 |  | 
|---|
| 232 | /* | 
|---|
| 233 | * These bitmasks identify subsystems with specific features to avoid | 
|---|
| 234 | * having to do iterative checks repeatedly. | 
|---|
| 235 | */ | 
|---|
| 236 | static u16 have_fork_callback __read_mostly; | 
|---|
| 237 | static u16 have_exit_callback __read_mostly; | 
|---|
| 238 | static u16 have_release_callback __read_mostly; | 
|---|
| 239 | static u16 have_canfork_callback __read_mostly; | 
|---|
| 240 |  | 
|---|
| 241 | static bool have_favordynmods __ro_after_init = IS_ENABLED(CONFIG_CGROUP_FAVOR_DYNMODS); | 
|---|
| 242 |  | 
|---|
| 243 | /* | 
|---|
| 244 | * Write protected by cgroup_mutex and write-lock of cgroup_threadgroup_rwsem, | 
|---|
| 245 | * read protected by either. | 
|---|
| 246 | * | 
|---|
| 247 | * Can only be turned on, but not turned off. | 
|---|
| 248 | */ | 
|---|
| 249 | bool cgroup_enable_per_threadgroup_rwsem __read_mostly; | 
|---|
| 250 |  | 
|---|
| 251 | /* cgroup namespace for init task */ | 
|---|
| 252 | struct cgroup_namespace init_cgroup_ns = { | 
|---|
| 253 | .ns.__ns_ref	= REFCOUNT_INIT(2), | 
|---|
| 254 | .user_ns	= &init_user_ns, | 
|---|
| 255 | .ns.ops		= &cgroupns_operations, | 
|---|
| 256 | .ns.inum	= ns_init_inum(&init_cgroup_ns), | 
|---|
| 257 | .root_cset	= &init_css_set, | 
|---|
| 258 | .ns.ns_type	= ns_common_type(&init_cgroup_ns), | 
|---|
| 259 | }; | 
|---|
| 260 |  | 
|---|
| 261 | static struct file_system_type cgroup2_fs_type; | 
|---|
| 262 | static struct cftype cgroup_base_files[]; | 
|---|
| 263 | static struct cftype cgroup_psi_files[]; | 
|---|
| 264 |  | 
|---|
| 265 | /* cgroup optional features */ | 
|---|
| 266 | enum cgroup_opt_features { | 
|---|
| 267 | #ifdef CONFIG_PSI | 
|---|
| 268 | OPT_FEATURE_PRESSURE, | 
|---|
| 269 | #endif | 
|---|
| 270 | OPT_FEATURE_COUNT | 
|---|
| 271 | }; | 
|---|
| 272 |  | 
|---|
| 273 | static const char *cgroup_opt_feature_names[OPT_FEATURE_COUNT] = { | 
|---|
| 274 | #ifdef CONFIG_PSI | 
|---|
| 275 | "pressure", | 
|---|
| 276 | #endif | 
|---|
| 277 | }; | 
|---|
| 278 |  | 
|---|
| 279 | static u16 cgroup_feature_disable_mask __read_mostly; | 
|---|
| 280 |  | 
|---|
| 281 | static int cgroup_apply_control(struct cgroup *cgrp); | 
|---|
| 282 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret); | 
|---|
| 283 | static void css_task_iter_skip(struct css_task_iter *it, | 
|---|
| 284 | struct task_struct *task); | 
|---|
| 285 | static int cgroup_destroy_locked(struct cgroup *cgrp); | 
|---|
| 286 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, | 
|---|
| 287 | struct cgroup_subsys *ss); | 
|---|
| 288 | static void css_release(struct percpu_ref *ref); | 
|---|
| 289 | static void kill_css(struct cgroup_subsys_state *css); | 
|---|
| 290 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, | 
|---|
| 291 | struct cgroup *cgrp, struct cftype cfts[], | 
|---|
| 292 | bool is_add); | 
|---|
| 293 |  | 
|---|
| 294 | #ifdef CONFIG_DEBUG_CGROUP_REF | 
|---|
| 295 | #define CGROUP_REF_FN_ATTRS	noinline | 
|---|
| 296 | #define CGROUP_REF_EXPORT(fn)	EXPORT_SYMBOL_GPL(fn); | 
|---|
| 297 | #include <linux/cgroup_refcnt.h> | 
|---|
| 298 | #endif | 
|---|
| 299 |  | 
|---|
| 300 | /** | 
|---|
| 301 | * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID | 
|---|
| 302 | * @ssid: subsys ID of interest | 
|---|
| 303 | * | 
|---|
| 304 | * cgroup_subsys_enabled() can only be used with literal subsys names which | 
|---|
| 305 | * is fine for individual subsystems but unsuitable for cgroup core.  This | 
|---|
| 306 | * is slower static_key_enabled() based test indexed by @ssid. | 
|---|
| 307 | */ | 
|---|
| 308 | bool cgroup_ssid_enabled(int ssid) | 
|---|
| 309 | { | 
|---|
| 310 | if (!CGROUP_HAS_SUBSYS_CONFIG) | 
|---|
| 311 | return false; | 
|---|
| 312 |  | 
|---|
| 313 | return static_key_enabled(cgroup_subsys_enabled_key[ssid]); | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | /** | 
|---|
| 317 | * cgroup_on_dfl - test whether a cgroup is on the default hierarchy | 
|---|
| 318 | * @cgrp: the cgroup of interest | 
|---|
| 319 | * | 
|---|
| 320 | * The default hierarchy is the v2 interface of cgroup and this function | 
|---|
| 321 | * can be used to test whether a cgroup is on the default hierarchy for | 
|---|
| 322 | * cases where a subsystem should behave differently depending on the | 
|---|
| 323 | * interface version. | 
|---|
| 324 | * | 
|---|
| 325 | * List of changed behaviors: | 
|---|
| 326 | * | 
|---|
| 327 | * - Mount options "noprefix", "xattr", "clone_children", "release_agent" | 
|---|
| 328 | *   and "name" are disallowed. | 
|---|
| 329 | * | 
|---|
| 330 | * - When mounting an existing superblock, mount options should match. | 
|---|
| 331 | * | 
|---|
| 332 | * - rename(2) is disallowed. | 
|---|
| 333 | * | 
|---|
| 334 | * - "tasks" is removed.  Everything should be at process granularity.  Use | 
|---|
| 335 | *   "cgroup.procs" instead. | 
|---|
| 336 | * | 
|---|
| 337 | * - "cgroup.procs" is not sorted.  pids will be unique unless they got | 
|---|
| 338 | *   recycled in-between reads. | 
|---|
| 339 | * | 
|---|
| 340 | * - "release_agent" and "notify_on_release" are removed.  Replacement | 
|---|
| 341 | *   notification mechanism will be implemented. | 
|---|
| 342 | * | 
|---|
| 343 | * - "cgroup.clone_children" is removed. | 
|---|
| 344 | * | 
|---|
| 345 | * - "cgroup.subtree_populated" is available.  Its value is 0 if the cgroup | 
|---|
| 346 | *   and its descendants contain no task; otherwise, 1.  The file also | 
|---|
| 347 | *   generates kernfs notification which can be monitored through poll and | 
|---|
| 348 | *   [di]notify when the value of the file changes. | 
|---|
| 349 | * | 
|---|
| 350 | * - cpuset: tasks will be kept in empty cpusets when hotplug happens and | 
|---|
| 351 | *   take masks of ancestors with non-empty cpus/mems, instead of being | 
|---|
| 352 | *   moved to an ancestor. | 
|---|
| 353 | * | 
|---|
| 354 | * - cpuset: a task can be moved into an empty cpuset, and again it takes | 
|---|
| 355 | *   masks of ancestors. | 
|---|
| 356 | * | 
|---|
| 357 | * - blkcg: blk-throttle becomes properly hierarchical. | 
|---|
| 358 | */ | 
|---|
| 359 | bool cgroup_on_dfl(const struct cgroup *cgrp) | 
|---|
| 360 | { | 
|---|
| 361 | return cgrp->root == &cgrp_dfl_root; | 
|---|
| 362 | } | 
|---|
| 363 |  | 
|---|
| 364 | /* IDR wrappers which synchronize using cgroup_idr_lock */ | 
|---|
| 365 | static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end, | 
|---|
| 366 | gfp_t gfp_mask) | 
|---|
| 367 | { | 
|---|
| 368 | int ret; | 
|---|
| 369 |  | 
|---|
| 370 | idr_preload(gfp_mask); | 
|---|
| 371 | spin_lock_bh(lock: &cgroup_idr_lock); | 
|---|
| 372 | ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM); | 
|---|
| 373 | spin_unlock_bh(lock: &cgroup_idr_lock); | 
|---|
| 374 | idr_preload_end(); | 
|---|
| 375 | return ret; | 
|---|
| 376 | } | 
|---|
| 377 |  | 
|---|
| 378 | static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id) | 
|---|
| 379 | { | 
|---|
| 380 | void *ret; | 
|---|
| 381 |  | 
|---|
| 382 | spin_lock_bh(lock: &cgroup_idr_lock); | 
|---|
| 383 | ret = idr_replace(idr, ptr, id); | 
|---|
| 384 | spin_unlock_bh(lock: &cgroup_idr_lock); | 
|---|
| 385 | return ret; | 
|---|
| 386 | } | 
|---|
| 387 |  | 
|---|
| 388 | static void cgroup_idr_remove(struct idr *idr, int id) | 
|---|
| 389 | { | 
|---|
| 390 | spin_lock_bh(lock: &cgroup_idr_lock); | 
|---|
| 391 | idr_remove(idr, id); | 
|---|
| 392 | spin_unlock_bh(lock: &cgroup_idr_lock); | 
|---|
| 393 | } | 
|---|
| 394 |  | 
|---|
| 395 | static bool cgroup_has_tasks(struct cgroup *cgrp) | 
|---|
| 396 | { | 
|---|
| 397 | return cgrp->nr_populated_csets; | 
|---|
| 398 | } | 
|---|
| 399 |  | 
|---|
| 400 | static bool cgroup_is_threaded(struct cgroup *cgrp) | 
|---|
| 401 | { | 
|---|
| 402 | return cgrp->dom_cgrp != cgrp; | 
|---|
| 403 | } | 
|---|
| 404 |  | 
|---|
| 405 | /* can @cgrp host both domain and threaded children? */ | 
|---|
| 406 | static bool cgroup_is_mixable(struct cgroup *cgrp) | 
|---|
| 407 | { | 
|---|
| 408 | /* | 
|---|
| 409 | * Root isn't under domain level resource control exempting it from | 
|---|
| 410 | * the no-internal-process constraint, so it can serve as a thread | 
|---|
| 411 | * root and a parent of resource domains at the same time. | 
|---|
| 412 | */ | 
|---|
| 413 | return !cgroup_parent(cgrp); | 
|---|
| 414 | } | 
|---|
| 415 |  | 
|---|
| 416 | /* can @cgrp become a thread root? Should always be true for a thread root */ | 
|---|
| 417 | static bool cgroup_can_be_thread_root(struct cgroup *cgrp) | 
|---|
| 418 | { | 
|---|
| 419 | /* mixables don't care */ | 
|---|
| 420 | if (cgroup_is_mixable(cgrp)) | 
|---|
| 421 | return true; | 
|---|
| 422 |  | 
|---|
| 423 | /* domain roots can't be nested under threaded */ | 
|---|
| 424 | if (cgroup_is_threaded(cgrp)) | 
|---|
| 425 | return false; | 
|---|
| 426 |  | 
|---|
| 427 | /* can only have either domain or threaded children */ | 
|---|
| 428 | if (cgrp->nr_populated_domain_children) | 
|---|
| 429 | return false; | 
|---|
| 430 |  | 
|---|
| 431 | /* and no domain controllers can be enabled */ | 
|---|
| 432 | if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) | 
|---|
| 433 | return false; | 
|---|
| 434 |  | 
|---|
| 435 | return true; | 
|---|
| 436 | } | 
|---|
| 437 |  | 
|---|
| 438 | /* is @cgrp root of a threaded subtree? */ | 
|---|
| 439 | static bool cgroup_is_thread_root(struct cgroup *cgrp) | 
|---|
| 440 | { | 
|---|
| 441 | /* thread root should be a domain */ | 
|---|
| 442 | if (cgroup_is_threaded(cgrp)) | 
|---|
| 443 | return false; | 
|---|
| 444 |  | 
|---|
| 445 | /* a domain w/ threaded children is a thread root */ | 
|---|
| 446 | if (cgrp->nr_threaded_children) | 
|---|
| 447 | return true; | 
|---|
| 448 |  | 
|---|
| 449 | /* | 
|---|
| 450 | * A domain which has tasks and explicit threaded controllers | 
|---|
| 451 | * enabled is a thread root. | 
|---|
| 452 | */ | 
|---|
| 453 | if (cgroup_has_tasks(cgrp) && | 
|---|
| 454 | (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask)) | 
|---|
| 455 | return true; | 
|---|
| 456 |  | 
|---|
| 457 | return false; | 
|---|
| 458 | } | 
|---|
| 459 |  | 
|---|
| 460 | /* a domain which isn't connected to the root w/o brekage can't be used */ | 
|---|
| 461 | static bool cgroup_is_valid_domain(struct cgroup *cgrp) | 
|---|
| 462 | { | 
|---|
| 463 | /* the cgroup itself can be a thread root */ | 
|---|
| 464 | if (cgroup_is_threaded(cgrp)) | 
|---|
| 465 | return false; | 
|---|
| 466 |  | 
|---|
| 467 | /* but the ancestors can't be unless mixable */ | 
|---|
| 468 | while ((cgrp = cgroup_parent(cgrp))) { | 
|---|
| 469 | if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp)) | 
|---|
| 470 | return false; | 
|---|
| 471 | if (cgroup_is_threaded(cgrp)) | 
|---|
| 472 | return false; | 
|---|
| 473 | } | 
|---|
| 474 |  | 
|---|
| 475 | return true; | 
|---|
| 476 | } | 
|---|
| 477 |  | 
|---|
| 478 | /* subsystems visibly enabled on a cgroup */ | 
|---|
| 479 | static u16 cgroup_control(struct cgroup *cgrp) | 
|---|
| 480 | { | 
|---|
| 481 | struct cgroup *parent = cgroup_parent(cgrp); | 
|---|
| 482 | u16 root_ss_mask = cgrp->root->subsys_mask; | 
|---|
| 483 |  | 
|---|
| 484 | if (parent) { | 
|---|
| 485 | u16 ss_mask = parent->subtree_control; | 
|---|
| 486 |  | 
|---|
| 487 | /* threaded cgroups can only have threaded controllers */ | 
|---|
| 488 | if (cgroup_is_threaded(cgrp)) | 
|---|
| 489 | ss_mask &= cgrp_dfl_threaded_ss_mask; | 
|---|
| 490 | return ss_mask; | 
|---|
| 491 | } | 
|---|
| 492 |  | 
|---|
| 493 | if (cgroup_on_dfl(cgrp)) | 
|---|
| 494 | root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask | | 
|---|
| 495 | cgrp_dfl_implicit_ss_mask); | 
|---|
| 496 | return root_ss_mask; | 
|---|
| 497 | } | 
|---|
| 498 |  | 
|---|
| 499 | /* subsystems enabled on a cgroup */ | 
|---|
| 500 | static u16 cgroup_ss_mask(struct cgroup *cgrp) | 
|---|
| 501 | { | 
|---|
| 502 | struct cgroup *parent = cgroup_parent(cgrp); | 
|---|
| 503 |  | 
|---|
| 504 | if (parent) { | 
|---|
| 505 | u16 ss_mask = parent->subtree_ss_mask; | 
|---|
| 506 |  | 
|---|
| 507 | /* threaded cgroups can only have threaded controllers */ | 
|---|
| 508 | if (cgroup_is_threaded(cgrp)) | 
|---|
| 509 | ss_mask &= cgrp_dfl_threaded_ss_mask; | 
|---|
| 510 | return ss_mask; | 
|---|
| 511 | } | 
|---|
| 512 |  | 
|---|
| 513 | return cgrp->root->subsys_mask; | 
|---|
| 514 | } | 
|---|
| 515 |  | 
|---|
| 516 | /** | 
|---|
| 517 | * cgroup_css - obtain a cgroup's css for the specified subsystem | 
|---|
| 518 | * @cgrp: the cgroup of interest | 
|---|
| 519 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) | 
|---|
| 520 | * | 
|---|
| 521 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss.  This | 
|---|
| 522 | * function must be called either under cgroup_mutex or rcu_read_lock() and | 
|---|
| 523 | * the caller is responsible for pinning the returned css if it wants to | 
|---|
| 524 | * keep accessing it outside the said locks.  This function may return | 
|---|
| 525 | * %NULL if @cgrp doesn't have @subsys_id enabled. | 
|---|
| 526 | */ | 
|---|
| 527 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, | 
|---|
| 528 | struct cgroup_subsys *ss) | 
|---|
| 529 | { | 
|---|
| 530 | if (CGROUP_HAS_SUBSYS_CONFIG && ss) | 
|---|
| 531 | return rcu_dereference_check(cgrp->subsys[ss->id], | 
|---|
| 532 | lockdep_is_held(&cgroup_mutex)); | 
|---|
| 533 | else | 
|---|
| 534 | return &cgrp->self; | 
|---|
| 535 | } | 
|---|
| 536 |  | 
|---|
| 537 | /** | 
|---|
| 538 | * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss | 
|---|
| 539 | * @cgrp: the cgroup of interest | 
|---|
| 540 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) | 
|---|
| 541 | * | 
|---|
| 542 | * Similar to cgroup_css() but returns the effective css, which is defined | 
|---|
| 543 | * as the matching css of the nearest ancestor including self which has @ss | 
|---|
| 544 | * enabled.  If @ss is associated with the hierarchy @cgrp is on, this | 
|---|
| 545 | * function is guaranteed to return non-NULL css. | 
|---|
| 546 | */ | 
|---|
| 547 | static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp, | 
|---|
| 548 | struct cgroup_subsys *ss) | 
|---|
| 549 | { | 
|---|
| 550 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 551 |  | 
|---|
| 552 | if (!ss) | 
|---|
| 553 | return &cgrp->self; | 
|---|
| 554 |  | 
|---|
| 555 | /* | 
|---|
| 556 | * This function is used while updating css associations and thus | 
|---|
| 557 | * can't test the csses directly.  Test ss_mask. | 
|---|
| 558 | */ | 
|---|
| 559 | while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) { | 
|---|
| 560 | cgrp = cgroup_parent(cgrp); | 
|---|
| 561 | if (!cgrp) | 
|---|
| 562 | return NULL; | 
|---|
| 563 | } | 
|---|
| 564 |  | 
|---|
| 565 | return cgroup_css(cgrp, ss); | 
|---|
| 566 | } | 
|---|
| 567 |  | 
|---|
| 568 | /** | 
|---|
| 569 | * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem | 
|---|
| 570 | * @cgrp: the cgroup of interest | 
|---|
| 571 | * @ss: the subsystem of interest | 
|---|
| 572 | * | 
|---|
| 573 | * Find and get the effective css of @cgrp for @ss.  The effective css is | 
|---|
| 574 | * defined as the matching css of the nearest ancestor including self which | 
|---|
| 575 | * has @ss enabled.  If @ss is not mounted on the hierarchy @cgrp is on, | 
|---|
| 576 | * the root css is returned, so this function always returns a valid css. | 
|---|
| 577 | * | 
|---|
| 578 | * The returned css is not guaranteed to be online, and therefore it is the | 
|---|
| 579 | * callers responsibility to try get a reference for it. | 
|---|
| 580 | */ | 
|---|
| 581 | struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp, | 
|---|
| 582 | struct cgroup_subsys *ss) | 
|---|
| 583 | { | 
|---|
| 584 | struct cgroup_subsys_state *css; | 
|---|
| 585 |  | 
|---|
| 586 | if (!CGROUP_HAS_SUBSYS_CONFIG) | 
|---|
| 587 | return NULL; | 
|---|
| 588 |  | 
|---|
| 589 | do { | 
|---|
| 590 | css = cgroup_css(cgrp, ss); | 
|---|
| 591 |  | 
|---|
| 592 | if (css) | 
|---|
| 593 | return css; | 
|---|
| 594 | cgrp = cgroup_parent(cgrp); | 
|---|
| 595 | } while (cgrp); | 
|---|
| 596 |  | 
|---|
| 597 | return init_css_set.subsys[ss->id]; | 
|---|
| 598 | } | 
|---|
| 599 |  | 
|---|
| 600 | /** | 
|---|
| 601 | * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem | 
|---|
| 602 | * @cgrp: the cgroup of interest | 
|---|
| 603 | * @ss: the subsystem of interest | 
|---|
| 604 | * | 
|---|
| 605 | * Find and get the effective css of @cgrp for @ss.  The effective css is | 
|---|
| 606 | * defined as the matching css of the nearest ancestor including self which | 
|---|
| 607 | * has @ss enabled.  If @ss is not mounted on the hierarchy @cgrp is on, | 
|---|
| 608 | * the root css is returned, so this function always returns a valid css. | 
|---|
| 609 | * The returned css must be put using css_put(). | 
|---|
| 610 | */ | 
|---|
| 611 | struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp, | 
|---|
| 612 | struct cgroup_subsys *ss) | 
|---|
| 613 | { | 
|---|
| 614 | struct cgroup_subsys_state *css; | 
|---|
| 615 |  | 
|---|
| 616 | if (!CGROUP_HAS_SUBSYS_CONFIG) | 
|---|
| 617 | return NULL; | 
|---|
| 618 |  | 
|---|
| 619 | rcu_read_lock(); | 
|---|
| 620 |  | 
|---|
| 621 | do { | 
|---|
| 622 | css = cgroup_css(cgrp, ss); | 
|---|
| 623 |  | 
|---|
| 624 | if (css && css_tryget_online(css)) | 
|---|
| 625 | goto out_unlock; | 
|---|
| 626 | cgrp = cgroup_parent(cgrp); | 
|---|
| 627 | } while (cgrp); | 
|---|
| 628 |  | 
|---|
| 629 | css = init_css_set.subsys[ss->id]; | 
|---|
| 630 | css_get(css); | 
|---|
| 631 | out_unlock: | 
|---|
| 632 | rcu_read_unlock(); | 
|---|
| 633 | return css; | 
|---|
| 634 | } | 
|---|
| 635 | EXPORT_SYMBOL_GPL(cgroup_get_e_css); | 
|---|
| 636 |  | 
|---|
| 637 | static void cgroup_get_live(struct cgroup *cgrp) | 
|---|
| 638 | { | 
|---|
| 639 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); | 
|---|
| 640 | cgroup_get(cgrp); | 
|---|
| 641 | } | 
|---|
| 642 |  | 
|---|
| 643 | /** | 
|---|
| 644 | * __cgroup_task_count - count the number of tasks in a cgroup. The caller | 
|---|
| 645 | * is responsible for taking the css_set_lock. | 
|---|
| 646 | * @cgrp: the cgroup in question | 
|---|
| 647 | */ | 
|---|
| 648 | int __cgroup_task_count(const struct cgroup *cgrp) | 
|---|
| 649 | { | 
|---|
| 650 | int count = 0; | 
|---|
| 651 | struct cgrp_cset_link *link; | 
|---|
| 652 |  | 
|---|
| 653 | lockdep_assert_held(&css_set_lock); | 
|---|
| 654 |  | 
|---|
| 655 | list_for_each_entry(link, &cgrp->cset_links, cset_link) | 
|---|
| 656 | count += link->cset->nr_tasks; | 
|---|
| 657 |  | 
|---|
| 658 | return count; | 
|---|
| 659 | } | 
|---|
| 660 |  | 
|---|
| 661 | /** | 
|---|
| 662 | * cgroup_task_count - count the number of tasks in a cgroup. | 
|---|
| 663 | * @cgrp: the cgroup in question | 
|---|
| 664 | */ | 
|---|
| 665 | int cgroup_task_count(const struct cgroup *cgrp) | 
|---|
| 666 | { | 
|---|
| 667 | int count; | 
|---|
| 668 |  | 
|---|
| 669 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 670 | count = __cgroup_task_count(cgrp); | 
|---|
| 671 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 672 |  | 
|---|
| 673 | return count; | 
|---|
| 674 | } | 
|---|
| 675 |  | 
|---|
| 676 | static struct cgroup *kn_priv(struct kernfs_node *kn) | 
|---|
| 677 | { | 
|---|
| 678 | struct kernfs_node *parent; | 
|---|
| 679 | /* | 
|---|
| 680 | * The parent can not be replaced due to KERNFS_ROOT_INVARIANT_PARENT. | 
|---|
| 681 | * Therefore it is always safe to dereference this pointer outside of a | 
|---|
| 682 | * RCU section. | 
|---|
| 683 | */ | 
|---|
| 684 | parent = rcu_dereference_check(kn->__parent, | 
|---|
| 685 | kernfs_root_flags(kn) & KERNFS_ROOT_INVARIANT_PARENT); | 
|---|
| 686 | return parent->priv; | 
|---|
| 687 | } | 
|---|
| 688 |  | 
|---|
| 689 | struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) | 
|---|
| 690 | { | 
|---|
| 691 | struct cgroup *cgrp = kn_priv(kn: of->kn); | 
|---|
| 692 | struct cftype *cft = of_cft(of); | 
|---|
| 693 |  | 
|---|
| 694 | /* | 
|---|
| 695 | * This is open and unprotected implementation of cgroup_css(). | 
|---|
| 696 | * seq_css() is only called from a kernfs file operation which has | 
|---|
| 697 | * an active reference on the file.  Because all the subsystem | 
|---|
| 698 | * files are drained before a css is disassociated with a cgroup, | 
|---|
| 699 | * the matching css from the cgroup's subsys table is guaranteed to | 
|---|
| 700 | * be and stay valid until the enclosing operation is complete. | 
|---|
| 701 | */ | 
|---|
| 702 | if (CGROUP_HAS_SUBSYS_CONFIG && cft->ss) | 
|---|
| 703 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); | 
|---|
| 704 | else | 
|---|
| 705 | return &cgrp->self; | 
|---|
| 706 | } | 
|---|
| 707 | EXPORT_SYMBOL_GPL(of_css); | 
|---|
| 708 |  | 
|---|
| 709 | /** | 
|---|
| 710 | * for_each_css - iterate all css's of a cgroup | 
|---|
| 711 | * @css: the iteration cursor | 
|---|
| 712 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end | 
|---|
| 713 | * @cgrp: the target cgroup to iterate css's of | 
|---|
| 714 | * | 
|---|
| 715 | * Should be called under cgroup_mutex. | 
|---|
| 716 | */ | 
|---|
| 717 | #define for_each_css(css, ssid, cgrp)					\ | 
|---|
| 718 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++)	\ | 
|---|
| 719 | if (!((css) = rcu_dereference_check(			\ | 
|---|
| 720 | (cgrp)->subsys[(ssid)],			\ | 
|---|
| 721 | lockdep_is_held(&cgroup_mutex)))) { }	\ | 
|---|
| 722 | else | 
|---|
| 723 |  | 
|---|
| 724 | /** | 
|---|
| 725 | * do_each_subsys_mask - filter for_each_subsys with a bitmask | 
|---|
| 726 | * @ss: the iteration cursor | 
|---|
| 727 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end | 
|---|
| 728 | * @ss_mask: the bitmask | 
|---|
| 729 | * | 
|---|
| 730 | * The block will only run for cases where the ssid-th bit (1 << ssid) of | 
|---|
| 731 | * @ss_mask is set. | 
|---|
| 732 | */ | 
|---|
| 733 | #define do_each_subsys_mask(ss, ssid, ss_mask) do {			\ | 
|---|
| 734 | unsigned long __ss_mask = (ss_mask);				\ | 
|---|
| 735 | if (!CGROUP_HAS_SUBSYS_CONFIG) {				\ | 
|---|
| 736 | (ssid) = 0;						\ | 
|---|
| 737 | break;							\ | 
|---|
| 738 | }								\ | 
|---|
| 739 | for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) {	\ | 
|---|
| 740 | (ss) = cgroup_subsys[ssid];				\ | 
|---|
| 741 | { | 
|---|
| 742 |  | 
|---|
| 743 | #define while_each_subsys_mask()					\ | 
|---|
| 744 | }							\ | 
|---|
| 745 | }								\ | 
|---|
| 746 | } while (false) | 
|---|
| 747 |  | 
|---|
| 748 | /* iterate over child cgrps, lock should be held throughout iteration */ | 
|---|
| 749 | #define cgroup_for_each_live_child(child, cgrp)				\ | 
|---|
| 750 | list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \ | 
|---|
| 751 | if (({ lockdep_assert_held(&cgroup_mutex);		\ | 
|---|
| 752 | cgroup_is_dead(child); }))			\ | 
|---|
| 753 | ;						\ | 
|---|
| 754 | else | 
|---|
| 755 |  | 
|---|
| 756 | /* walk live descendants in pre order */ | 
|---|
| 757 | #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp)		\ | 
|---|
| 758 | css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL))	\ | 
|---|
| 759 | if (({ lockdep_assert_held(&cgroup_mutex);		\ | 
|---|
| 760 | (dsct) = (d_css)->cgroup;			\ | 
|---|
| 761 | cgroup_is_dead(dsct); }))			\ | 
|---|
| 762 | ;						\ | 
|---|
| 763 | else | 
|---|
| 764 |  | 
|---|
| 765 | /* walk live descendants in postorder */ | 
|---|
| 766 | #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp)		\ | 
|---|
| 767 | css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL))	\ | 
|---|
| 768 | if (({ lockdep_assert_held(&cgroup_mutex);		\ | 
|---|
| 769 | (dsct) = (d_css)->cgroup;			\ | 
|---|
| 770 | cgroup_is_dead(dsct); }))			\ | 
|---|
| 771 | ;						\ | 
|---|
| 772 | else | 
|---|
| 773 |  | 
|---|
| 774 | /* | 
|---|
| 775 | * The default css_set - used by init and its children prior to any | 
|---|
| 776 | * hierarchies being mounted. It contains a pointer to the root state | 
|---|
| 777 | * for each subsystem. Also used to anchor the list of css_sets. Not | 
|---|
| 778 | * reference-counted, to improve performance when child cgroups | 
|---|
| 779 | * haven't been created. | 
|---|
| 780 | */ | 
|---|
| 781 | struct css_set init_css_set = { | 
|---|
| 782 | .refcount		= REFCOUNT_INIT(1), | 
|---|
| 783 | .dom_cset		= &init_css_set, | 
|---|
| 784 | .tasks			= LIST_HEAD_INIT(init_css_set.tasks), | 
|---|
| 785 | .mg_tasks		= LIST_HEAD_INIT(init_css_set.mg_tasks), | 
|---|
| 786 | .dying_tasks		= LIST_HEAD_INIT(init_css_set.dying_tasks), | 
|---|
| 787 | .task_iters		= LIST_HEAD_INIT(init_css_set.task_iters), | 
|---|
| 788 | .threaded_csets		= LIST_HEAD_INIT(init_css_set.threaded_csets), | 
|---|
| 789 | .cgrp_links		= LIST_HEAD_INIT(init_css_set.cgrp_links), | 
|---|
| 790 | .mg_src_preload_node	= LIST_HEAD_INIT(init_css_set.mg_src_preload_node), | 
|---|
| 791 | .mg_dst_preload_node	= LIST_HEAD_INIT(init_css_set.mg_dst_preload_node), | 
|---|
| 792 | .mg_node		= LIST_HEAD_INIT(init_css_set.mg_node), | 
|---|
| 793 |  | 
|---|
| 794 | /* | 
|---|
| 795 | * The following field is re-initialized when this cset gets linked | 
|---|
| 796 | * in cgroup_init().  However, let's initialize the field | 
|---|
| 797 | * statically too so that the default cgroup can be accessed safely | 
|---|
| 798 | * early during boot. | 
|---|
| 799 | */ | 
|---|
| 800 | .dfl_cgrp		= &cgrp_dfl_root.cgrp, | 
|---|
| 801 | }; | 
|---|
| 802 |  | 
|---|
| 803 | static int css_set_count	= 1;	/* 1 for init_css_set */ | 
|---|
| 804 |  | 
|---|
| 805 | static bool css_set_threaded(struct css_set *cset) | 
|---|
| 806 | { | 
|---|
| 807 | return cset->dom_cset != cset; | 
|---|
| 808 | } | 
|---|
| 809 |  | 
|---|
| 810 | /** | 
|---|
| 811 | * css_set_populated - does a css_set contain any tasks? | 
|---|
| 812 | * @cset: target css_set | 
|---|
| 813 | * | 
|---|
| 814 | * css_set_populated() should be the same as !!cset->nr_tasks at steady | 
|---|
| 815 | * state. However, css_set_populated() can be called while a task is being | 
|---|
| 816 | * added to or removed from the linked list before the nr_tasks is | 
|---|
| 817 | * properly updated. Hence, we can't just look at ->nr_tasks here. | 
|---|
| 818 | */ | 
|---|
| 819 | static bool css_set_populated(struct css_set *cset) | 
|---|
| 820 | { | 
|---|
| 821 | lockdep_assert_held(&css_set_lock); | 
|---|
| 822 |  | 
|---|
| 823 | return !list_empty(head: &cset->tasks) || !list_empty(head: &cset->mg_tasks); | 
|---|
| 824 | } | 
|---|
| 825 |  | 
|---|
| 826 | /** | 
|---|
| 827 | * cgroup_update_populated - update the populated count of a cgroup | 
|---|
| 828 | * @cgrp: the target cgroup | 
|---|
| 829 | * @populated: inc or dec populated count | 
|---|
| 830 | * | 
|---|
| 831 | * One of the css_sets associated with @cgrp is either getting its first | 
|---|
| 832 | * task or losing the last.  Update @cgrp->nr_populated_* accordingly.  The | 
|---|
| 833 | * count is propagated towards root so that a given cgroup's | 
|---|
| 834 | * nr_populated_children is zero iff none of its descendants contain any | 
|---|
| 835 | * tasks. | 
|---|
| 836 | * | 
|---|
| 837 | * @cgrp's interface file "cgroup.populated" is zero if both | 
|---|
| 838 | * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and | 
|---|
| 839 | * 1 otherwise.  When the sum changes from or to zero, userland is notified | 
|---|
| 840 | * that the content of the interface file has changed.  This can be used to | 
|---|
| 841 | * detect when @cgrp and its descendants become populated or empty. | 
|---|
| 842 | */ | 
|---|
| 843 | static void cgroup_update_populated(struct cgroup *cgrp, bool populated) | 
|---|
| 844 | { | 
|---|
| 845 | struct cgroup *child = NULL; | 
|---|
| 846 | int adj = populated ? 1 : -1; | 
|---|
| 847 |  | 
|---|
| 848 | lockdep_assert_held(&css_set_lock); | 
|---|
| 849 |  | 
|---|
| 850 | do { | 
|---|
| 851 | bool was_populated = cgroup_is_populated(cgrp); | 
|---|
| 852 |  | 
|---|
| 853 | if (!child) { | 
|---|
| 854 | cgrp->nr_populated_csets += adj; | 
|---|
| 855 | } else { | 
|---|
| 856 | if (cgroup_is_threaded(cgrp: child)) | 
|---|
| 857 | cgrp->nr_populated_threaded_children += adj; | 
|---|
| 858 | else | 
|---|
| 859 | cgrp->nr_populated_domain_children += adj; | 
|---|
| 860 | } | 
|---|
| 861 |  | 
|---|
| 862 | if (was_populated == cgroup_is_populated(cgrp)) | 
|---|
| 863 | break; | 
|---|
| 864 |  | 
|---|
| 865 | cgroup1_check_for_release(cgrp); | 
|---|
| 866 | TRACE_CGROUP_PATH(notify_populated, cgrp, | 
|---|
| 867 | cgroup_is_populated(cgrp)); | 
|---|
| 868 | cgroup_file_notify(cfile: &cgrp->events_file); | 
|---|
| 869 |  | 
|---|
| 870 | child = cgrp; | 
|---|
| 871 | cgrp = cgroup_parent(cgrp); | 
|---|
| 872 | } while (cgrp); | 
|---|
| 873 | } | 
|---|
| 874 |  | 
|---|
| 875 | /** | 
|---|
| 876 | * css_set_update_populated - update populated state of a css_set | 
|---|
| 877 | * @cset: target css_set | 
|---|
| 878 | * @populated: whether @cset is populated or depopulated | 
|---|
| 879 | * | 
|---|
| 880 | * @cset is either getting the first task or losing the last.  Update the | 
|---|
| 881 | * populated counters of all associated cgroups accordingly. | 
|---|
| 882 | */ | 
|---|
| 883 | static void css_set_update_populated(struct css_set *cset, bool populated) | 
|---|
| 884 | { | 
|---|
| 885 | struct cgrp_cset_link *link; | 
|---|
| 886 |  | 
|---|
| 887 | lockdep_assert_held(&css_set_lock); | 
|---|
| 888 |  | 
|---|
| 889 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) | 
|---|
| 890 | cgroup_update_populated(cgrp: link->cgrp, populated); | 
|---|
| 891 | } | 
|---|
| 892 |  | 
|---|
| 893 | /* | 
|---|
| 894 | * @task is leaving, advance task iterators which are pointing to it so | 
|---|
| 895 | * that they can resume at the next position.  Advancing an iterator might | 
|---|
| 896 | * remove it from the list, use safe walk.  See css_task_iter_skip() for | 
|---|
| 897 | * details. | 
|---|
| 898 | */ | 
|---|
| 899 | static void css_set_skip_task_iters(struct css_set *cset, | 
|---|
| 900 | struct task_struct *task) | 
|---|
| 901 | { | 
|---|
| 902 | struct css_task_iter *it, *pos; | 
|---|
| 903 |  | 
|---|
| 904 | list_for_each_entry_safe(it, pos, &cset->task_iters, iters_node) | 
|---|
| 905 | css_task_iter_skip(it, task); | 
|---|
| 906 | } | 
|---|
| 907 |  | 
|---|
| 908 | /** | 
|---|
| 909 | * css_set_move_task - move a task from one css_set to another | 
|---|
| 910 | * @task: task being moved | 
|---|
| 911 | * @from_cset: css_set @task currently belongs to (may be NULL) | 
|---|
| 912 | * @to_cset: new css_set @task is being moved to (may be NULL) | 
|---|
| 913 | * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks | 
|---|
| 914 | * | 
|---|
| 915 | * Move @task from @from_cset to @to_cset.  If @task didn't belong to any | 
|---|
| 916 | * css_set, @from_cset can be NULL.  If @task is being disassociated | 
|---|
| 917 | * instead of moved, @to_cset can be NULL. | 
|---|
| 918 | * | 
|---|
| 919 | * This function automatically handles populated counter updates and | 
|---|
| 920 | * css_task_iter adjustments but the caller is responsible for managing | 
|---|
| 921 | * @from_cset and @to_cset's reference counts. | 
|---|
| 922 | */ | 
|---|
| 923 | static void css_set_move_task(struct task_struct *task, | 
|---|
| 924 | struct css_set *from_cset, struct css_set *to_cset, | 
|---|
| 925 | bool use_mg_tasks) | 
|---|
| 926 | { | 
|---|
| 927 | lockdep_assert_held(&css_set_lock); | 
|---|
| 928 |  | 
|---|
| 929 | if (to_cset && !css_set_populated(cset: to_cset)) | 
|---|
| 930 | css_set_update_populated(cset: to_cset, populated: true); | 
|---|
| 931 |  | 
|---|
| 932 | if (from_cset) { | 
|---|
| 933 | WARN_ON_ONCE(list_empty(&task->cg_list)); | 
|---|
| 934 |  | 
|---|
| 935 | css_set_skip_task_iters(cset: from_cset, task); | 
|---|
| 936 | list_del_init(entry: &task->cg_list); | 
|---|
| 937 | if (!css_set_populated(cset: from_cset)) | 
|---|
| 938 | css_set_update_populated(cset: from_cset, populated: false); | 
|---|
| 939 | } else { | 
|---|
| 940 | WARN_ON_ONCE(!list_empty(&task->cg_list)); | 
|---|
| 941 | } | 
|---|
| 942 |  | 
|---|
| 943 | if (to_cset) { | 
|---|
| 944 | /* | 
|---|
| 945 | * We are synchronized through cgroup_threadgroup_rwsem | 
|---|
| 946 | * against PF_EXITING setting such that we can't race | 
|---|
| 947 | * against cgroup_exit()/cgroup_free() dropping the css_set. | 
|---|
| 948 | */ | 
|---|
| 949 | WARN_ON_ONCE(task->flags & PF_EXITING); | 
|---|
| 950 |  | 
|---|
| 951 | cgroup_move_task(p: task, to: to_cset); | 
|---|
| 952 | list_add_tail(new: &task->cg_list, head: use_mg_tasks ? &to_cset->mg_tasks : | 
|---|
| 953 | &to_cset->tasks); | 
|---|
| 954 | } | 
|---|
| 955 | } | 
|---|
| 956 |  | 
|---|
| 957 | /* | 
|---|
| 958 | * hash table for cgroup groups. This improves the performance to find | 
|---|
| 959 | * an existing css_set. This hash doesn't (currently) take into | 
|---|
| 960 | * account cgroups in empty hierarchies. | 
|---|
| 961 | */ | 
|---|
| 962 | #define CSS_SET_HASH_BITS	7 | 
|---|
| 963 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); | 
|---|
| 964 |  | 
|---|
| 965 | static unsigned long css_set_hash(struct cgroup_subsys_state **css) | 
|---|
| 966 | { | 
|---|
| 967 | unsigned long key = 0UL; | 
|---|
| 968 | struct cgroup_subsys *ss; | 
|---|
| 969 | int i; | 
|---|
| 970 |  | 
|---|
| 971 | for_each_subsys(ss, i) | 
|---|
| 972 | key += (unsigned long)css[i]; | 
|---|
| 973 | key = (key >> 16) ^ key; | 
|---|
| 974 |  | 
|---|
| 975 | return key; | 
|---|
| 976 | } | 
|---|
| 977 |  | 
|---|
| 978 | void put_css_set_locked(struct css_set *cset) | 
|---|
| 979 | { | 
|---|
| 980 | struct cgrp_cset_link *link, *tmp_link; | 
|---|
| 981 | struct cgroup_subsys *ss; | 
|---|
| 982 | int ssid; | 
|---|
| 983 |  | 
|---|
| 984 | lockdep_assert_held(&css_set_lock); | 
|---|
| 985 |  | 
|---|
| 986 | if (!refcount_dec_and_test(r: &cset->refcount)) | 
|---|
| 987 | return; | 
|---|
| 988 |  | 
|---|
| 989 | WARN_ON_ONCE(!list_empty(&cset->threaded_csets)); | 
|---|
| 990 |  | 
|---|
| 991 | /* This css_set is dead. Unlink it and release cgroup and css refs */ | 
|---|
| 992 | for_each_subsys(ss, ssid) { | 
|---|
| 993 | list_del(entry: &cset->e_cset_node[ssid]); | 
|---|
| 994 | css_put(css: cset->subsys[ssid]); | 
|---|
| 995 | } | 
|---|
| 996 | hash_del(node: &cset->hlist); | 
|---|
| 997 | css_set_count--; | 
|---|
| 998 |  | 
|---|
| 999 | list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { | 
|---|
| 1000 | list_del(entry: &link->cset_link); | 
|---|
| 1001 | list_del(entry: &link->cgrp_link); | 
|---|
| 1002 | if (cgroup_parent(cgrp: link->cgrp)) | 
|---|
| 1003 | cgroup_put(cgrp: link->cgrp); | 
|---|
| 1004 | kfree(objp: link); | 
|---|
| 1005 | } | 
|---|
| 1006 |  | 
|---|
| 1007 | if (css_set_threaded(cset)) { | 
|---|
| 1008 | list_del(entry: &cset->threaded_csets_node); | 
|---|
| 1009 | put_css_set_locked(cset: cset->dom_cset); | 
|---|
| 1010 | } | 
|---|
| 1011 |  | 
|---|
| 1012 | kfree_rcu(cset, rcu_head); | 
|---|
| 1013 | } | 
|---|
| 1014 |  | 
|---|
| 1015 | /** | 
|---|
| 1016 | * compare_css_sets - helper function for find_existing_css_set(). | 
|---|
| 1017 | * @cset: candidate css_set being tested | 
|---|
| 1018 | * @old_cset: existing css_set for a task | 
|---|
| 1019 | * @new_cgrp: cgroup that's being entered by the task | 
|---|
| 1020 | * @template: desired set of css pointers in css_set (pre-calculated) | 
|---|
| 1021 | * | 
|---|
| 1022 | * Returns true if "cset" matches "old_cset" except for the hierarchy | 
|---|
| 1023 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". | 
|---|
| 1024 | */ | 
|---|
| 1025 | static bool compare_css_sets(struct css_set *cset, | 
|---|
| 1026 | struct css_set *old_cset, | 
|---|
| 1027 | struct cgroup *new_cgrp, | 
|---|
| 1028 | struct cgroup_subsys_state *template[]) | 
|---|
| 1029 | { | 
|---|
| 1030 | struct cgroup *new_dfl_cgrp; | 
|---|
| 1031 | struct list_head *l1, *l2; | 
|---|
| 1032 |  | 
|---|
| 1033 | /* | 
|---|
| 1034 | * On the default hierarchy, there can be csets which are | 
|---|
| 1035 | * associated with the same set of cgroups but different csses. | 
|---|
| 1036 | * Let's first ensure that csses match. | 
|---|
| 1037 | */ | 
|---|
| 1038 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) | 
|---|
| 1039 | return false; | 
|---|
| 1040 |  | 
|---|
| 1041 |  | 
|---|
| 1042 | /* @cset's domain should match the default cgroup's */ | 
|---|
| 1043 | if (cgroup_on_dfl(cgrp: new_cgrp)) | 
|---|
| 1044 | new_dfl_cgrp = new_cgrp; | 
|---|
| 1045 | else | 
|---|
| 1046 | new_dfl_cgrp = old_cset->dfl_cgrp; | 
|---|
| 1047 |  | 
|---|
| 1048 | if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp) | 
|---|
| 1049 | return false; | 
|---|
| 1050 |  | 
|---|
| 1051 | /* | 
|---|
| 1052 | * Compare cgroup pointers in order to distinguish between | 
|---|
| 1053 | * different cgroups in hierarchies.  As different cgroups may | 
|---|
| 1054 | * share the same effective css, this comparison is always | 
|---|
| 1055 | * necessary. | 
|---|
| 1056 | */ | 
|---|
| 1057 | l1 = &cset->cgrp_links; | 
|---|
| 1058 | l2 = &old_cset->cgrp_links; | 
|---|
| 1059 | while (1) { | 
|---|
| 1060 | struct cgrp_cset_link *link1, *link2; | 
|---|
| 1061 | struct cgroup *cgrp1, *cgrp2; | 
|---|
| 1062 |  | 
|---|
| 1063 | l1 = l1->next; | 
|---|
| 1064 | l2 = l2->next; | 
|---|
| 1065 | /* See if we reached the end - both lists are equal length. */ | 
|---|
| 1066 | if (l1 == &cset->cgrp_links) { | 
|---|
| 1067 | BUG_ON(l2 != &old_cset->cgrp_links); | 
|---|
| 1068 | break; | 
|---|
| 1069 | } else { | 
|---|
| 1070 | BUG_ON(l2 == &old_cset->cgrp_links); | 
|---|
| 1071 | } | 
|---|
| 1072 | /* Locate the cgroups associated with these links. */ | 
|---|
| 1073 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); | 
|---|
| 1074 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); | 
|---|
| 1075 | cgrp1 = link1->cgrp; | 
|---|
| 1076 | cgrp2 = link2->cgrp; | 
|---|
| 1077 | /* Hierarchies should be linked in the same order. */ | 
|---|
| 1078 | BUG_ON(cgrp1->root != cgrp2->root); | 
|---|
| 1079 |  | 
|---|
| 1080 | /* | 
|---|
| 1081 | * If this hierarchy is the hierarchy of the cgroup | 
|---|
| 1082 | * that's changing, then we need to check that this | 
|---|
| 1083 | * css_set points to the new cgroup; if it's any other | 
|---|
| 1084 | * hierarchy, then this css_set should point to the | 
|---|
| 1085 | * same cgroup as the old css_set. | 
|---|
| 1086 | */ | 
|---|
| 1087 | if (cgrp1->root == new_cgrp->root) { | 
|---|
| 1088 | if (cgrp1 != new_cgrp) | 
|---|
| 1089 | return false; | 
|---|
| 1090 | } else { | 
|---|
| 1091 | if (cgrp1 != cgrp2) | 
|---|
| 1092 | return false; | 
|---|
| 1093 | } | 
|---|
| 1094 | } | 
|---|
| 1095 | return true; | 
|---|
| 1096 | } | 
|---|
| 1097 |  | 
|---|
| 1098 | /** | 
|---|
| 1099 | * find_existing_css_set - init css array and find the matching css_set | 
|---|
| 1100 | * @old_cset: the css_set that we're using before the cgroup transition | 
|---|
| 1101 | * @cgrp: the cgroup that we're moving into | 
|---|
| 1102 | * @template: out param for the new set of csses, should be clear on entry | 
|---|
| 1103 | */ | 
|---|
| 1104 | static struct css_set *find_existing_css_set(struct css_set *old_cset, | 
|---|
| 1105 | struct cgroup *cgrp, | 
|---|
| 1106 | struct cgroup_subsys_state **template) | 
|---|
| 1107 | { | 
|---|
| 1108 | struct cgroup_root *root = cgrp->root; | 
|---|
| 1109 | struct cgroup_subsys *ss; | 
|---|
| 1110 | struct css_set *cset; | 
|---|
| 1111 | unsigned long key; | 
|---|
| 1112 | int i; | 
|---|
| 1113 |  | 
|---|
| 1114 | /* | 
|---|
| 1115 | * Build the set of subsystem state objects that we want to see in the | 
|---|
| 1116 | * new css_set. While subsystems can change globally, the entries here | 
|---|
| 1117 | * won't change, so no need for locking. | 
|---|
| 1118 | */ | 
|---|
| 1119 | for_each_subsys(ss, i) { | 
|---|
| 1120 | if (root->subsys_mask & (1UL << i)) { | 
|---|
| 1121 | /* | 
|---|
| 1122 | * @ss is in this hierarchy, so we want the | 
|---|
| 1123 | * effective css from @cgrp. | 
|---|
| 1124 | */ | 
|---|
| 1125 | template[i] = cgroup_e_css_by_mask(cgrp, ss); | 
|---|
| 1126 | } else { | 
|---|
| 1127 | /* | 
|---|
| 1128 | * @ss is not in this hierarchy, so we don't want | 
|---|
| 1129 | * to change the css. | 
|---|
| 1130 | */ | 
|---|
| 1131 | template[i] = old_cset->subsys[i]; | 
|---|
| 1132 | } | 
|---|
| 1133 | } | 
|---|
| 1134 |  | 
|---|
| 1135 | key = css_set_hash(css: template); | 
|---|
| 1136 | hash_for_each_possible(css_set_table, cset, hlist, key) { | 
|---|
| 1137 | if (!compare_css_sets(cset, old_cset, new_cgrp: cgrp, template)) | 
|---|
| 1138 | continue; | 
|---|
| 1139 |  | 
|---|
| 1140 | /* This css_set matches what we need */ | 
|---|
| 1141 | return cset; | 
|---|
| 1142 | } | 
|---|
| 1143 |  | 
|---|
| 1144 | /* No existing cgroup group matched */ | 
|---|
| 1145 | return NULL; | 
|---|
| 1146 | } | 
|---|
| 1147 |  | 
|---|
| 1148 | static void free_cgrp_cset_links(struct list_head *links_to_free) | 
|---|
| 1149 | { | 
|---|
| 1150 | struct cgrp_cset_link *link, *tmp_link; | 
|---|
| 1151 |  | 
|---|
| 1152 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { | 
|---|
| 1153 | list_del(entry: &link->cset_link); | 
|---|
| 1154 | kfree(objp: link); | 
|---|
| 1155 | } | 
|---|
| 1156 | } | 
|---|
| 1157 |  | 
|---|
| 1158 | /** | 
|---|
| 1159 | * allocate_cgrp_cset_links - allocate cgrp_cset_links | 
|---|
| 1160 | * @count: the number of links to allocate | 
|---|
| 1161 | * @tmp_links: list_head the allocated links are put on | 
|---|
| 1162 | * | 
|---|
| 1163 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links | 
|---|
| 1164 | * through ->cset_link.  Returns 0 on success or -errno. | 
|---|
| 1165 | */ | 
|---|
| 1166 | static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links) | 
|---|
| 1167 | { | 
|---|
| 1168 | struct cgrp_cset_link *link; | 
|---|
| 1169 | int i; | 
|---|
| 1170 |  | 
|---|
| 1171 | INIT_LIST_HEAD(list: tmp_links); | 
|---|
| 1172 |  | 
|---|
| 1173 | for (i = 0; i < count; i++) { | 
|---|
| 1174 | link = kzalloc(sizeof(*link), GFP_KERNEL); | 
|---|
| 1175 | if (!link) { | 
|---|
| 1176 | free_cgrp_cset_links(links_to_free: tmp_links); | 
|---|
| 1177 | return -ENOMEM; | 
|---|
| 1178 | } | 
|---|
| 1179 | list_add(new: &link->cset_link, head: tmp_links); | 
|---|
| 1180 | } | 
|---|
| 1181 | return 0; | 
|---|
| 1182 | } | 
|---|
| 1183 |  | 
|---|
| 1184 | /** | 
|---|
| 1185 | * link_css_set - a helper function to link a css_set to a cgroup | 
|---|
| 1186 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() | 
|---|
| 1187 | * @cset: the css_set to be linked | 
|---|
| 1188 | * @cgrp: the destination cgroup | 
|---|
| 1189 | */ | 
|---|
| 1190 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, | 
|---|
| 1191 | struct cgroup *cgrp) | 
|---|
| 1192 | { | 
|---|
| 1193 | struct cgrp_cset_link *link; | 
|---|
| 1194 |  | 
|---|
| 1195 | BUG_ON(list_empty(tmp_links)); | 
|---|
| 1196 |  | 
|---|
| 1197 | if (cgroup_on_dfl(cgrp)) | 
|---|
| 1198 | cset->dfl_cgrp = cgrp; | 
|---|
| 1199 |  | 
|---|
| 1200 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); | 
|---|
| 1201 | link->cset = cset; | 
|---|
| 1202 | link->cgrp = cgrp; | 
|---|
| 1203 |  | 
|---|
| 1204 | /* | 
|---|
| 1205 | * Always add links to the tail of the lists so that the lists are | 
|---|
| 1206 | * in chronological order. | 
|---|
| 1207 | */ | 
|---|
| 1208 | list_move_tail(list: &link->cset_link, head: &cgrp->cset_links); | 
|---|
| 1209 | list_add_tail(new: &link->cgrp_link, head: &cset->cgrp_links); | 
|---|
| 1210 |  | 
|---|
| 1211 | if (cgroup_parent(cgrp)) | 
|---|
| 1212 | cgroup_get_live(cgrp); | 
|---|
| 1213 | } | 
|---|
| 1214 |  | 
|---|
| 1215 | /** | 
|---|
| 1216 | * find_css_set - return a new css_set with one cgroup updated | 
|---|
| 1217 | * @old_cset: the baseline css_set | 
|---|
| 1218 | * @cgrp: the cgroup to be updated | 
|---|
| 1219 | * | 
|---|
| 1220 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp | 
|---|
| 1221 | * substituted into the appropriate hierarchy. | 
|---|
| 1222 | */ | 
|---|
| 1223 | static struct css_set *find_css_set(struct css_set *old_cset, | 
|---|
| 1224 | struct cgroup *cgrp) | 
|---|
| 1225 | { | 
|---|
| 1226 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; | 
|---|
| 1227 | struct css_set *cset; | 
|---|
| 1228 | struct list_head tmp_links; | 
|---|
| 1229 | struct cgrp_cset_link *link; | 
|---|
| 1230 | struct cgroup_subsys *ss; | 
|---|
| 1231 | unsigned long key; | 
|---|
| 1232 | int ssid; | 
|---|
| 1233 |  | 
|---|
| 1234 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 1235 |  | 
|---|
| 1236 | /* First see if we already have a cgroup group that matches | 
|---|
| 1237 | * the desired set */ | 
|---|
| 1238 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 1239 | cset = find_existing_css_set(old_cset, cgrp, template); | 
|---|
| 1240 | if (cset) | 
|---|
| 1241 | get_css_set(cset); | 
|---|
| 1242 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 1243 |  | 
|---|
| 1244 | if (cset) | 
|---|
| 1245 | return cset; | 
|---|
| 1246 |  | 
|---|
| 1247 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); | 
|---|
| 1248 | if (!cset) | 
|---|
| 1249 | return NULL; | 
|---|
| 1250 |  | 
|---|
| 1251 | /* Allocate all the cgrp_cset_link objects that we'll need */ | 
|---|
| 1252 | if (allocate_cgrp_cset_links(count: cgroup_root_count, tmp_links: &tmp_links) < 0) { | 
|---|
| 1253 | kfree(objp: cset); | 
|---|
| 1254 | return NULL; | 
|---|
| 1255 | } | 
|---|
| 1256 |  | 
|---|
| 1257 | refcount_set(r: &cset->refcount, n: 1); | 
|---|
| 1258 | cset->dom_cset = cset; | 
|---|
| 1259 | INIT_LIST_HEAD(list: &cset->tasks); | 
|---|
| 1260 | INIT_LIST_HEAD(list: &cset->mg_tasks); | 
|---|
| 1261 | INIT_LIST_HEAD(list: &cset->dying_tasks); | 
|---|
| 1262 | INIT_LIST_HEAD(list: &cset->task_iters); | 
|---|
| 1263 | INIT_LIST_HEAD(list: &cset->threaded_csets); | 
|---|
| 1264 | INIT_HLIST_NODE(h: &cset->hlist); | 
|---|
| 1265 | INIT_LIST_HEAD(list: &cset->cgrp_links); | 
|---|
| 1266 | INIT_LIST_HEAD(list: &cset->mg_src_preload_node); | 
|---|
| 1267 | INIT_LIST_HEAD(list: &cset->mg_dst_preload_node); | 
|---|
| 1268 | INIT_LIST_HEAD(list: &cset->mg_node); | 
|---|
| 1269 |  | 
|---|
| 1270 | /* Copy the set of subsystem state objects generated in | 
|---|
| 1271 | * find_existing_css_set() */ | 
|---|
| 1272 | memcpy(to: cset->subsys, from: template, len: sizeof(cset->subsys)); | 
|---|
| 1273 |  | 
|---|
| 1274 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 1275 | /* Add reference counts and links from the new css_set. */ | 
|---|
| 1276 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { | 
|---|
| 1277 | struct cgroup *c = link->cgrp; | 
|---|
| 1278 |  | 
|---|
| 1279 | if (c->root == cgrp->root) | 
|---|
| 1280 | c = cgrp; | 
|---|
| 1281 | link_css_set(tmp_links: &tmp_links, cset, cgrp: c); | 
|---|
| 1282 | } | 
|---|
| 1283 |  | 
|---|
| 1284 | BUG_ON(!list_empty(&tmp_links)); | 
|---|
| 1285 |  | 
|---|
| 1286 | css_set_count++; | 
|---|
| 1287 |  | 
|---|
| 1288 | /* Add @cset to the hash table */ | 
|---|
| 1289 | key = css_set_hash(css: cset->subsys); | 
|---|
| 1290 | hash_add(css_set_table, &cset->hlist, key); | 
|---|
| 1291 |  | 
|---|
| 1292 | for_each_subsys(ss, ssid) { | 
|---|
| 1293 | struct cgroup_subsys_state *css = cset->subsys[ssid]; | 
|---|
| 1294 |  | 
|---|
| 1295 | list_add_tail(new: &cset->e_cset_node[ssid], | 
|---|
| 1296 | head: &css->cgroup->e_csets[ssid]); | 
|---|
| 1297 | css_get(css); | 
|---|
| 1298 | } | 
|---|
| 1299 |  | 
|---|
| 1300 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 1301 |  | 
|---|
| 1302 | /* | 
|---|
| 1303 | * If @cset should be threaded, look up the matching dom_cset and | 
|---|
| 1304 | * link them up.  We first fully initialize @cset then look for the | 
|---|
| 1305 | * dom_cset.  It's simpler this way and safe as @cset is guaranteed | 
|---|
| 1306 | * to stay empty until we return. | 
|---|
| 1307 | */ | 
|---|
| 1308 | if (cgroup_is_threaded(cgrp: cset->dfl_cgrp)) { | 
|---|
| 1309 | struct css_set *dcset; | 
|---|
| 1310 |  | 
|---|
| 1311 | dcset = find_css_set(old_cset: cset, cgrp: cset->dfl_cgrp->dom_cgrp); | 
|---|
| 1312 | if (!dcset) { | 
|---|
| 1313 | put_css_set(cset); | 
|---|
| 1314 | return NULL; | 
|---|
| 1315 | } | 
|---|
| 1316 |  | 
|---|
| 1317 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 1318 | cset->dom_cset = dcset; | 
|---|
| 1319 | list_add_tail(new: &cset->threaded_csets_node, | 
|---|
| 1320 | head: &dcset->threaded_csets); | 
|---|
| 1321 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 1322 | } | 
|---|
| 1323 |  | 
|---|
| 1324 | return cset; | 
|---|
| 1325 | } | 
|---|
| 1326 |  | 
|---|
| 1327 | struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) | 
|---|
| 1328 | { | 
|---|
| 1329 | struct cgroup *root_cgrp = kernfs_root_to_node(root: kf_root)->priv; | 
|---|
| 1330 |  | 
|---|
| 1331 | return root_cgrp->root; | 
|---|
| 1332 | } | 
|---|
| 1333 |  | 
|---|
| 1334 | void cgroup_favor_dynmods(struct cgroup_root *root, bool favor) | 
|---|
| 1335 | { | 
|---|
| 1336 | bool favoring = root->flags & CGRP_ROOT_FAVOR_DYNMODS; | 
|---|
| 1337 |  | 
|---|
| 1338 | /* | 
|---|
| 1339 | * see the comment above CGRP_ROOT_FAVOR_DYNMODS definition. | 
|---|
| 1340 | * favordynmods can flip while task is between | 
|---|
| 1341 | * cgroup_threadgroup_change_begin() and end(), so down_write global | 
|---|
| 1342 | * cgroup_threadgroup_rwsem to synchronize them. | 
|---|
| 1343 | * | 
|---|
| 1344 | * Once cgroup_enable_per_threadgroup_rwsem is enabled, holding | 
|---|
| 1345 | * cgroup_threadgroup_rwsem doesn't exlude tasks between | 
|---|
| 1346 | * cgroup_thread_group_change_begin() and end() and thus it's unsafe to | 
|---|
| 1347 | * turn off. As the scenario is unlikely, simply disallow disabling once | 
|---|
| 1348 | * enabled and print out a warning. | 
|---|
| 1349 | */ | 
|---|
| 1350 | percpu_down_write(&cgroup_threadgroup_rwsem); | 
|---|
| 1351 | if (favor && !favoring) { | 
|---|
| 1352 | cgroup_enable_per_threadgroup_rwsem = true; | 
|---|
| 1353 | rcu_sync_enter(&cgroup_threadgroup_rwsem.rss); | 
|---|
| 1354 | root->flags |= CGRP_ROOT_FAVOR_DYNMODS; | 
|---|
| 1355 | } else if (!favor && favoring) { | 
|---|
| 1356 | if (cgroup_enable_per_threadgroup_rwsem) | 
|---|
| 1357 | pr_warn_once( "cgroup favordynmods: per threadgroup rwsem mechanism can't be disabled\n"); | 
|---|
| 1358 | rcu_sync_exit(&cgroup_threadgroup_rwsem.rss); | 
|---|
| 1359 | root->flags &= ~CGRP_ROOT_FAVOR_DYNMODS; | 
|---|
| 1360 | } | 
|---|
| 1361 | percpu_up_write(&cgroup_threadgroup_rwsem); | 
|---|
| 1362 | } | 
|---|
| 1363 |  | 
|---|
| 1364 | static int cgroup_init_root_id(struct cgroup_root *root) | 
|---|
| 1365 | { | 
|---|
| 1366 | int id; | 
|---|
| 1367 |  | 
|---|
| 1368 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 1369 |  | 
|---|
| 1370 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, ptr: root, start: 0, end: 0, GFP_KERNEL); | 
|---|
| 1371 | if (id < 0) | 
|---|
| 1372 | return id; | 
|---|
| 1373 |  | 
|---|
| 1374 | root->hierarchy_id = id; | 
|---|
| 1375 | return 0; | 
|---|
| 1376 | } | 
|---|
| 1377 |  | 
|---|
| 1378 | static void cgroup_exit_root_id(struct cgroup_root *root) | 
|---|
| 1379 | { | 
|---|
| 1380 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 1381 |  | 
|---|
| 1382 | idr_remove(&cgroup_hierarchy_idr, id: root->hierarchy_id); | 
|---|
| 1383 | } | 
|---|
| 1384 |  | 
|---|
| 1385 | void cgroup_free_root(struct cgroup_root *root) | 
|---|
| 1386 | { | 
|---|
| 1387 | kfree_rcu(root, rcu); | 
|---|
| 1388 | } | 
|---|
| 1389 |  | 
|---|
| 1390 | static void cgroup_destroy_root(struct cgroup_root *root) | 
|---|
| 1391 | { | 
|---|
| 1392 | struct cgroup *cgrp = &root->cgrp; | 
|---|
| 1393 | struct cgrp_cset_link *link, *tmp_link; | 
|---|
| 1394 | int ret; | 
|---|
| 1395 |  | 
|---|
| 1396 | trace_cgroup_destroy_root(root); | 
|---|
| 1397 |  | 
|---|
| 1398 | cgroup_lock_and_drain_offline(cgrp: &cgrp_dfl_root.cgrp); | 
|---|
| 1399 |  | 
|---|
| 1400 | BUG_ON(atomic_read(&root->nr_cgrps)); | 
|---|
| 1401 | BUG_ON(!list_empty(&cgrp->self.children)); | 
|---|
| 1402 |  | 
|---|
| 1403 | ret = blocking_notifier_call_chain(nh: &cgroup_lifetime_notifier, | 
|---|
| 1404 | val: CGROUP_LIFETIME_OFFLINE, v: cgrp); | 
|---|
| 1405 | WARN_ON_ONCE(notifier_to_errno(ret)); | 
|---|
| 1406 |  | 
|---|
| 1407 | /* Rebind all subsystems back to the default hierarchy */ | 
|---|
| 1408 | WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask)); | 
|---|
| 1409 |  | 
|---|
| 1410 | /* | 
|---|
| 1411 | * Release all the links from cset_links to this hierarchy's | 
|---|
| 1412 | * root cgroup | 
|---|
| 1413 | */ | 
|---|
| 1414 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 1415 |  | 
|---|
| 1416 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { | 
|---|
| 1417 | list_del(entry: &link->cset_link); | 
|---|
| 1418 | list_del(entry: &link->cgrp_link); | 
|---|
| 1419 | kfree(objp: link); | 
|---|
| 1420 | } | 
|---|
| 1421 |  | 
|---|
| 1422 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 1423 |  | 
|---|
| 1424 | WARN_ON_ONCE(list_empty(&root->root_list)); | 
|---|
| 1425 | list_del_rcu(entry: &root->root_list); | 
|---|
| 1426 | cgroup_root_count--; | 
|---|
| 1427 |  | 
|---|
| 1428 | if (!have_favordynmods) | 
|---|
| 1429 | cgroup_favor_dynmods(root, favor: false); | 
|---|
| 1430 |  | 
|---|
| 1431 | cgroup_exit_root_id(root); | 
|---|
| 1432 |  | 
|---|
| 1433 | cgroup_unlock(); | 
|---|
| 1434 |  | 
|---|
| 1435 | kernfs_destroy_root(root: root->kf_root); | 
|---|
| 1436 | cgroup_free_root(root); | 
|---|
| 1437 | } | 
|---|
| 1438 |  | 
|---|
| 1439 | /* | 
|---|
| 1440 | * Returned cgroup is without refcount but it's valid as long as cset pins it. | 
|---|
| 1441 | */ | 
|---|
| 1442 | static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset, | 
|---|
| 1443 | struct cgroup_root *root) | 
|---|
| 1444 | { | 
|---|
| 1445 | struct cgroup *res_cgroup = NULL; | 
|---|
| 1446 |  | 
|---|
| 1447 | if (cset == &init_css_set) { | 
|---|
| 1448 | res_cgroup = &root->cgrp; | 
|---|
| 1449 | } else if (root == &cgrp_dfl_root) { | 
|---|
| 1450 | res_cgroup = cset->dfl_cgrp; | 
|---|
| 1451 | } else { | 
|---|
| 1452 | struct cgrp_cset_link *link; | 
|---|
| 1453 | lockdep_assert_held(&css_set_lock); | 
|---|
| 1454 |  | 
|---|
| 1455 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { | 
|---|
| 1456 | struct cgroup *c = link->cgrp; | 
|---|
| 1457 |  | 
|---|
| 1458 | if (c->root == root) { | 
|---|
| 1459 | res_cgroup = c; | 
|---|
| 1460 | break; | 
|---|
| 1461 | } | 
|---|
| 1462 | } | 
|---|
| 1463 | } | 
|---|
| 1464 |  | 
|---|
| 1465 | /* | 
|---|
| 1466 | * If cgroup_mutex is not held, the cgrp_cset_link will be freed | 
|---|
| 1467 | * before we remove the cgroup root from the root_list. Consequently, | 
|---|
| 1468 | * when accessing a cgroup root, the cset_link may have already been | 
|---|
| 1469 | * freed, resulting in a NULL res_cgroup. However, by holding the | 
|---|
| 1470 | * cgroup_mutex, we ensure that res_cgroup can't be NULL. | 
|---|
| 1471 | * If we don't hold cgroup_mutex in the caller, we must do the NULL | 
|---|
| 1472 | * check. | 
|---|
| 1473 | */ | 
|---|
| 1474 | return res_cgroup; | 
|---|
| 1475 | } | 
|---|
| 1476 |  | 
|---|
| 1477 | /* | 
|---|
| 1478 | * look up cgroup associated with current task's cgroup namespace on the | 
|---|
| 1479 | * specified hierarchy | 
|---|
| 1480 | */ | 
|---|
| 1481 | static struct cgroup * | 
|---|
| 1482 | current_cgns_cgroup_from_root(struct cgroup_root *root) | 
|---|
| 1483 | { | 
|---|
| 1484 | struct cgroup *res = NULL; | 
|---|
| 1485 | struct css_set *cset; | 
|---|
| 1486 |  | 
|---|
| 1487 | lockdep_assert_held(&css_set_lock); | 
|---|
| 1488 |  | 
|---|
| 1489 | rcu_read_lock(); | 
|---|
| 1490 |  | 
|---|
| 1491 | cset = current->nsproxy->cgroup_ns->root_cset; | 
|---|
| 1492 | res = __cset_cgroup_from_root(cset, root); | 
|---|
| 1493 |  | 
|---|
| 1494 | rcu_read_unlock(); | 
|---|
| 1495 |  | 
|---|
| 1496 | /* | 
|---|
| 1497 | * The namespace_sem is held by current, so the root cgroup can't | 
|---|
| 1498 | * be umounted. Therefore, we can ensure that the res is non-NULL. | 
|---|
| 1499 | */ | 
|---|
| 1500 | WARN_ON_ONCE(!res); | 
|---|
| 1501 | return res; | 
|---|
| 1502 | } | 
|---|
| 1503 |  | 
|---|
| 1504 | /* | 
|---|
| 1505 | * Look up cgroup associated with current task's cgroup namespace on the default | 
|---|
| 1506 | * hierarchy. | 
|---|
| 1507 | * | 
|---|
| 1508 | * Unlike current_cgns_cgroup_from_root(), this doesn't need locks: | 
|---|
| 1509 | * - Internal rcu_read_lock is unnecessary because we don't dereference any rcu | 
|---|
| 1510 | *   pointers. | 
|---|
| 1511 | * - css_set_lock is not needed because we just read cset->dfl_cgrp. | 
|---|
| 1512 | * - As a bonus returned cgrp is pinned with the current because it cannot | 
|---|
| 1513 | *   switch cgroup_ns asynchronously. | 
|---|
| 1514 | */ | 
|---|
| 1515 | static struct cgroup *current_cgns_cgroup_dfl(void) | 
|---|
| 1516 | { | 
|---|
| 1517 | struct css_set *cset; | 
|---|
| 1518 |  | 
|---|
| 1519 | if (current->nsproxy) { | 
|---|
| 1520 | cset = current->nsproxy->cgroup_ns->root_cset; | 
|---|
| 1521 | return __cset_cgroup_from_root(cset, root: &cgrp_dfl_root); | 
|---|
| 1522 | } else { | 
|---|
| 1523 | /* | 
|---|
| 1524 | * NOTE: This function may be called from bpf_cgroup_from_id() | 
|---|
| 1525 | * on a task which has already passed exit_task_namespaces() and | 
|---|
| 1526 | * nsproxy == NULL. Fall back to cgrp_dfl_root which will make all | 
|---|
| 1527 | * cgroups visible for lookups. | 
|---|
| 1528 | */ | 
|---|
| 1529 | return &cgrp_dfl_root.cgrp; | 
|---|
| 1530 | } | 
|---|
| 1531 | } | 
|---|
| 1532 |  | 
|---|
| 1533 | /* look up cgroup associated with given css_set on the specified hierarchy */ | 
|---|
| 1534 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, | 
|---|
| 1535 | struct cgroup_root *root) | 
|---|
| 1536 | { | 
|---|
| 1537 | lockdep_assert_held(&css_set_lock); | 
|---|
| 1538 |  | 
|---|
| 1539 | return __cset_cgroup_from_root(cset, root); | 
|---|
| 1540 | } | 
|---|
| 1541 |  | 
|---|
| 1542 | /* | 
|---|
| 1543 | * Return the cgroup for "task" from the given hierarchy. Must be | 
|---|
| 1544 | * called with css_set_lock held to prevent task's groups from being modified. | 
|---|
| 1545 | * Must be called with either cgroup_mutex or rcu read lock to prevent the | 
|---|
| 1546 | * cgroup root from being destroyed. | 
|---|
| 1547 | */ | 
|---|
| 1548 | struct cgroup *task_cgroup_from_root(struct task_struct *task, | 
|---|
| 1549 | struct cgroup_root *root) | 
|---|
| 1550 | { | 
|---|
| 1551 | /* | 
|---|
| 1552 | * No need to lock the task - since we hold css_set_lock the | 
|---|
| 1553 | * task can't change groups. | 
|---|
| 1554 | */ | 
|---|
| 1555 | return cset_cgroup_from_root(cset: task_css_set(task), root); | 
|---|
| 1556 | } | 
|---|
| 1557 |  | 
|---|
| 1558 | /* | 
|---|
| 1559 | * A task must hold cgroup_mutex to modify cgroups. | 
|---|
| 1560 | * | 
|---|
| 1561 | * Any task can increment and decrement the count field without lock. | 
|---|
| 1562 | * So in general, code holding cgroup_mutex can't rely on the count | 
|---|
| 1563 | * field not changing.  However, if the count goes to zero, then only | 
|---|
| 1564 | * cgroup_attach_task() can increment it again.  Because a count of zero | 
|---|
| 1565 | * means that no tasks are currently attached, therefore there is no | 
|---|
| 1566 | * way a task attached to that cgroup can fork (the other way to | 
|---|
| 1567 | * increment the count).  So code holding cgroup_mutex can safely | 
|---|
| 1568 | * assume that if the count is zero, it will stay zero. Similarly, if | 
|---|
| 1569 | * a task holds cgroup_mutex on a cgroup with zero count, it | 
|---|
| 1570 | * knows that the cgroup won't be removed, as cgroup_rmdir() | 
|---|
| 1571 | * needs that mutex. | 
|---|
| 1572 | * | 
|---|
| 1573 | * A cgroup can only be deleted if both its 'count' of using tasks | 
|---|
| 1574 | * is zero, and its list of 'children' cgroups is empty.  Since all | 
|---|
| 1575 | * tasks in the system use _some_ cgroup, and since there is always at | 
|---|
| 1576 | * least one task in the system (init, pid == 1), therefore, root cgroup | 
|---|
| 1577 | * always has either children cgroups and/or using tasks.  So we don't | 
|---|
| 1578 | * need a special hack to ensure that root cgroup cannot be deleted. | 
|---|
| 1579 | * | 
|---|
| 1580 | * P.S.  One more locking exception.  RCU is used to guard the | 
|---|
| 1581 | * update of a tasks cgroup pointer by cgroup_attach_task() | 
|---|
| 1582 | */ | 
|---|
| 1583 |  | 
|---|
| 1584 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; | 
|---|
| 1585 |  | 
|---|
| 1586 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, | 
|---|
| 1587 | char *buf) | 
|---|
| 1588 | { | 
|---|
| 1589 | struct cgroup_subsys *ss = cft->ss; | 
|---|
| 1590 |  | 
|---|
| 1591 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && | 
|---|
| 1592 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { | 
|---|
| 1593 | const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__.": ""; | 
|---|
| 1594 |  | 
|---|
| 1595 | snprintf(buf, CGROUP_FILE_NAME_MAX, fmt: "%s%s.%s", | 
|---|
| 1596 | dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name, | 
|---|
| 1597 | cft->name); | 
|---|
| 1598 | } else { | 
|---|
| 1599 | strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX); | 
|---|
| 1600 | } | 
|---|
| 1601 | return buf; | 
|---|
| 1602 | } | 
|---|
| 1603 |  | 
|---|
| 1604 | /** | 
|---|
| 1605 | * cgroup_file_mode - deduce file mode of a control file | 
|---|
| 1606 | * @cft: the control file in question | 
|---|
| 1607 | * | 
|---|
| 1608 | * S_IRUGO for read, S_IWUSR for write. | 
|---|
| 1609 | */ | 
|---|
| 1610 | static umode_t cgroup_file_mode(const struct cftype *cft) | 
|---|
| 1611 | { | 
|---|
| 1612 | umode_t mode = 0; | 
|---|
| 1613 |  | 
|---|
| 1614 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) | 
|---|
| 1615 | mode |= S_IRUGO; | 
|---|
| 1616 |  | 
|---|
| 1617 | if (cft->write_u64 || cft->write_s64 || cft->write) { | 
|---|
| 1618 | if (cft->flags & CFTYPE_WORLD_WRITABLE) | 
|---|
| 1619 | mode |= S_IWUGO; | 
|---|
| 1620 | else | 
|---|
| 1621 | mode |= S_IWUSR; | 
|---|
| 1622 | } | 
|---|
| 1623 |  | 
|---|
| 1624 | return mode; | 
|---|
| 1625 | } | 
|---|
| 1626 |  | 
|---|
| 1627 | /** | 
|---|
| 1628 | * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask | 
|---|
| 1629 | * @subtree_control: the new subtree_control mask to consider | 
|---|
| 1630 | * @this_ss_mask: available subsystems | 
|---|
| 1631 | * | 
|---|
| 1632 | * On the default hierarchy, a subsystem may request other subsystems to be | 
|---|
| 1633 | * enabled together through its ->depends_on mask.  In such cases, more | 
|---|
| 1634 | * subsystems than specified in "cgroup.subtree_control" may be enabled. | 
|---|
| 1635 | * | 
|---|
| 1636 | * This function calculates which subsystems need to be enabled if | 
|---|
| 1637 | * @subtree_control is to be applied while restricted to @this_ss_mask. | 
|---|
| 1638 | */ | 
|---|
| 1639 | static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask) | 
|---|
| 1640 | { | 
|---|
| 1641 | u16 cur_ss_mask = subtree_control; | 
|---|
| 1642 | struct cgroup_subsys *ss; | 
|---|
| 1643 | int ssid; | 
|---|
| 1644 |  | 
|---|
| 1645 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 1646 |  | 
|---|
| 1647 | cur_ss_mask |= cgrp_dfl_implicit_ss_mask; | 
|---|
| 1648 |  | 
|---|
| 1649 | while (true) { | 
|---|
| 1650 | u16 new_ss_mask = cur_ss_mask; | 
|---|
| 1651 |  | 
|---|
| 1652 | do_each_subsys_mask(ss, ssid, cur_ss_mask) { | 
|---|
| 1653 | new_ss_mask |= ss->depends_on; | 
|---|
| 1654 | } while_each_subsys_mask(); | 
|---|
| 1655 |  | 
|---|
| 1656 | /* | 
|---|
| 1657 | * Mask out subsystems which aren't available.  This can | 
|---|
| 1658 | * happen only if some depended-upon subsystems were bound | 
|---|
| 1659 | * to non-default hierarchies. | 
|---|
| 1660 | */ | 
|---|
| 1661 | new_ss_mask &= this_ss_mask; | 
|---|
| 1662 |  | 
|---|
| 1663 | if (new_ss_mask == cur_ss_mask) | 
|---|
| 1664 | break; | 
|---|
| 1665 | cur_ss_mask = new_ss_mask; | 
|---|
| 1666 | } | 
|---|
| 1667 |  | 
|---|
| 1668 | return cur_ss_mask; | 
|---|
| 1669 | } | 
|---|
| 1670 |  | 
|---|
| 1671 | /** | 
|---|
| 1672 | * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods | 
|---|
| 1673 | * @kn: the kernfs_node being serviced | 
|---|
| 1674 | * | 
|---|
| 1675 | * This helper undoes cgroup_kn_lock_live() and should be invoked before | 
|---|
| 1676 | * the method finishes if locking succeeded.  Note that once this function | 
|---|
| 1677 | * returns the cgroup returned by cgroup_kn_lock_live() may become | 
|---|
| 1678 | * inaccessible any time.  If the caller intends to continue to access the | 
|---|
| 1679 | * cgroup, it should pin it before invoking this function. | 
|---|
| 1680 | */ | 
|---|
| 1681 | void cgroup_kn_unlock(struct kernfs_node *kn) | 
|---|
| 1682 | { | 
|---|
| 1683 | struct cgroup *cgrp; | 
|---|
| 1684 |  | 
|---|
| 1685 | if (kernfs_type(kn) == KERNFS_DIR) | 
|---|
| 1686 | cgrp = kn->priv; | 
|---|
| 1687 | else | 
|---|
| 1688 | cgrp = kn_priv(kn); | 
|---|
| 1689 |  | 
|---|
| 1690 | cgroup_unlock(); | 
|---|
| 1691 |  | 
|---|
| 1692 | kernfs_unbreak_active_protection(kn); | 
|---|
| 1693 | cgroup_put(cgrp); | 
|---|
| 1694 | } | 
|---|
| 1695 |  | 
|---|
| 1696 | /** | 
|---|
| 1697 | * cgroup_kn_lock_live - locking helper for cgroup kernfs methods | 
|---|
| 1698 | * @kn: the kernfs_node being serviced | 
|---|
| 1699 | * @drain_offline: perform offline draining on the cgroup | 
|---|
| 1700 | * | 
|---|
| 1701 | * This helper is to be used by a cgroup kernfs method currently servicing | 
|---|
| 1702 | * @kn.  It breaks the active protection, performs cgroup locking and | 
|---|
| 1703 | * verifies that the associated cgroup is alive.  Returns the cgroup if | 
|---|
| 1704 | * alive; otherwise, %NULL.  A successful return should be undone by a | 
|---|
| 1705 | * matching cgroup_kn_unlock() invocation.  If @drain_offline is %true, the | 
|---|
| 1706 | * cgroup is drained of offlining csses before return. | 
|---|
| 1707 | * | 
|---|
| 1708 | * Any cgroup kernfs method implementation which requires locking the | 
|---|
| 1709 | * associated cgroup should use this helper.  It avoids nesting cgroup | 
|---|
| 1710 | * locking under kernfs active protection and allows all kernfs operations | 
|---|
| 1711 | * including self-removal. | 
|---|
| 1712 | */ | 
|---|
| 1713 | struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline) | 
|---|
| 1714 | { | 
|---|
| 1715 | struct cgroup *cgrp; | 
|---|
| 1716 |  | 
|---|
| 1717 | if (kernfs_type(kn) == KERNFS_DIR) | 
|---|
| 1718 | cgrp = kn->priv; | 
|---|
| 1719 | else | 
|---|
| 1720 | cgrp = kn_priv(kn); | 
|---|
| 1721 |  | 
|---|
| 1722 | /* | 
|---|
| 1723 | * We're gonna grab cgroup_mutex which nests outside kernfs | 
|---|
| 1724 | * active_ref.  cgroup liveliness check alone provides enough | 
|---|
| 1725 | * protection against removal.  Ensure @cgrp stays accessible and | 
|---|
| 1726 | * break the active_ref protection. | 
|---|
| 1727 | */ | 
|---|
| 1728 | if (!cgroup_tryget(cgrp)) | 
|---|
| 1729 | return NULL; | 
|---|
| 1730 | kernfs_break_active_protection(kn); | 
|---|
| 1731 |  | 
|---|
| 1732 | if (drain_offline) | 
|---|
| 1733 | cgroup_lock_and_drain_offline(cgrp); | 
|---|
| 1734 | else | 
|---|
| 1735 | cgroup_lock(); | 
|---|
| 1736 |  | 
|---|
| 1737 | if (!cgroup_is_dead(cgrp)) | 
|---|
| 1738 | return cgrp; | 
|---|
| 1739 |  | 
|---|
| 1740 | cgroup_kn_unlock(kn); | 
|---|
| 1741 | return NULL; | 
|---|
| 1742 | } | 
|---|
| 1743 |  | 
|---|
| 1744 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) | 
|---|
| 1745 | { | 
|---|
| 1746 | char name[CGROUP_FILE_NAME_MAX]; | 
|---|
| 1747 |  | 
|---|
| 1748 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 1749 |  | 
|---|
| 1750 | if (cft->file_offset) { | 
|---|
| 1751 | struct cgroup_subsys_state *css = cgroup_css(cgrp, ss: cft->ss); | 
|---|
| 1752 | struct cgroup_file *cfile = (void *)css + cft->file_offset; | 
|---|
| 1753 |  | 
|---|
| 1754 | spin_lock_irq(lock: &cgroup_file_kn_lock); | 
|---|
| 1755 | cfile->kn = NULL; | 
|---|
| 1756 | spin_unlock_irq(lock: &cgroup_file_kn_lock); | 
|---|
| 1757 |  | 
|---|
| 1758 | timer_delete_sync(timer: &cfile->notify_timer); | 
|---|
| 1759 | } | 
|---|
| 1760 |  | 
|---|
| 1761 | kernfs_remove_by_name(parent: cgrp->kn, name: cgroup_file_name(cgrp, cft, buf: name)); | 
|---|
| 1762 | } | 
|---|
| 1763 |  | 
|---|
| 1764 | /** | 
|---|
| 1765 | * css_clear_dir - remove subsys files in a cgroup directory | 
|---|
| 1766 | * @css: target css | 
|---|
| 1767 | */ | 
|---|
| 1768 | static void css_clear_dir(struct cgroup_subsys_state *css) | 
|---|
| 1769 | { | 
|---|
| 1770 | struct cgroup *cgrp = css->cgroup; | 
|---|
| 1771 | struct cftype *cfts; | 
|---|
| 1772 |  | 
|---|
| 1773 | if (!(css->flags & CSS_VISIBLE)) | 
|---|
| 1774 | return; | 
|---|
| 1775 |  | 
|---|
| 1776 | css->flags &= ~CSS_VISIBLE; | 
|---|
| 1777 |  | 
|---|
| 1778 | if (css_is_self(css)) { | 
|---|
| 1779 | if (cgroup_on_dfl(cgrp)) { | 
|---|
| 1780 | cgroup_addrm_files(css, cgrp, | 
|---|
| 1781 | cfts: cgroup_base_files, is_add: false); | 
|---|
| 1782 | if (cgroup_psi_enabled()) | 
|---|
| 1783 | cgroup_addrm_files(css, cgrp, | 
|---|
| 1784 | cfts: cgroup_psi_files, is_add: false); | 
|---|
| 1785 | } else { | 
|---|
| 1786 | cgroup_addrm_files(css, cgrp, | 
|---|
| 1787 | cfts: cgroup1_base_files, is_add: false); | 
|---|
| 1788 | } | 
|---|
| 1789 | } else { | 
|---|
| 1790 | list_for_each_entry(cfts, &css->ss->cfts, node) | 
|---|
| 1791 | cgroup_addrm_files(css, cgrp, cfts, is_add: false); | 
|---|
| 1792 | } | 
|---|
| 1793 | } | 
|---|
| 1794 |  | 
|---|
| 1795 | /** | 
|---|
| 1796 | * css_populate_dir - create subsys files in a cgroup directory | 
|---|
| 1797 | * @css: target css | 
|---|
| 1798 | * | 
|---|
| 1799 | * On failure, no file is added. | 
|---|
| 1800 | */ | 
|---|
| 1801 | static int css_populate_dir(struct cgroup_subsys_state *css) | 
|---|
| 1802 | { | 
|---|
| 1803 | struct cgroup *cgrp = css->cgroup; | 
|---|
| 1804 | struct cftype *cfts, *failed_cfts; | 
|---|
| 1805 | int ret; | 
|---|
| 1806 |  | 
|---|
| 1807 | if (css->flags & CSS_VISIBLE) | 
|---|
| 1808 | return 0; | 
|---|
| 1809 |  | 
|---|
| 1810 | if (css_is_self(css)) { | 
|---|
| 1811 | if (cgroup_on_dfl(cgrp)) { | 
|---|
| 1812 | ret = cgroup_addrm_files(css, cgrp, | 
|---|
| 1813 | cfts: cgroup_base_files, is_add: true); | 
|---|
| 1814 | if (ret < 0) | 
|---|
| 1815 | return ret; | 
|---|
| 1816 |  | 
|---|
| 1817 | if (cgroup_psi_enabled()) { | 
|---|
| 1818 | ret = cgroup_addrm_files(css, cgrp, | 
|---|
| 1819 | cfts: cgroup_psi_files, is_add: true); | 
|---|
| 1820 | if (ret < 0) { | 
|---|
| 1821 | cgroup_addrm_files(css, cgrp, | 
|---|
| 1822 | cfts: cgroup_base_files, is_add: false); | 
|---|
| 1823 | return ret; | 
|---|
| 1824 | } | 
|---|
| 1825 | } | 
|---|
| 1826 | } else { | 
|---|
| 1827 | ret = cgroup_addrm_files(css, cgrp, | 
|---|
| 1828 | cfts: cgroup1_base_files, is_add: true); | 
|---|
| 1829 | if (ret < 0) | 
|---|
| 1830 | return ret; | 
|---|
| 1831 | } | 
|---|
| 1832 | } else { | 
|---|
| 1833 | list_for_each_entry(cfts, &css->ss->cfts, node) { | 
|---|
| 1834 | ret = cgroup_addrm_files(css, cgrp, cfts, is_add: true); | 
|---|
| 1835 | if (ret < 0) { | 
|---|
| 1836 | failed_cfts = cfts; | 
|---|
| 1837 | goto err; | 
|---|
| 1838 | } | 
|---|
| 1839 | } | 
|---|
| 1840 | } | 
|---|
| 1841 |  | 
|---|
| 1842 | css->flags |= CSS_VISIBLE; | 
|---|
| 1843 |  | 
|---|
| 1844 | return 0; | 
|---|
| 1845 | err: | 
|---|
| 1846 | list_for_each_entry(cfts, &css->ss->cfts, node) { | 
|---|
| 1847 | if (cfts == failed_cfts) | 
|---|
| 1848 | break; | 
|---|
| 1849 | cgroup_addrm_files(css, cgrp, cfts, is_add: false); | 
|---|
| 1850 | } | 
|---|
| 1851 | return ret; | 
|---|
| 1852 | } | 
|---|
| 1853 |  | 
|---|
| 1854 | int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask) | 
|---|
| 1855 | { | 
|---|
| 1856 | struct cgroup *dcgrp = &dst_root->cgrp; | 
|---|
| 1857 | struct cgroup_subsys *ss; | 
|---|
| 1858 | int ssid, ret; | 
|---|
| 1859 | u16 dfl_disable_ss_mask = 0; | 
|---|
| 1860 |  | 
|---|
| 1861 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 1862 |  | 
|---|
| 1863 | do_each_subsys_mask(ss, ssid, ss_mask) { | 
|---|
| 1864 | /* | 
|---|
| 1865 | * If @ss has non-root csses attached to it, can't move. | 
|---|
| 1866 | * If @ss is an implicit controller, it is exempt from this | 
|---|
| 1867 | * rule and can be stolen. | 
|---|
| 1868 | */ | 
|---|
| 1869 | if (css_next_child(NULL, parent: cgroup_css(cgrp: &ss->root->cgrp, ss)) && | 
|---|
| 1870 | !ss->implicit_on_dfl) | 
|---|
| 1871 | return -EBUSY; | 
|---|
| 1872 |  | 
|---|
| 1873 | /* can't move between two non-dummy roots either */ | 
|---|
| 1874 | if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) | 
|---|
| 1875 | return -EBUSY; | 
|---|
| 1876 |  | 
|---|
| 1877 | /* | 
|---|
| 1878 | * Collect ssid's that need to be disabled from default | 
|---|
| 1879 | * hierarchy. | 
|---|
| 1880 | */ | 
|---|
| 1881 | if (ss->root == &cgrp_dfl_root) | 
|---|
| 1882 | dfl_disable_ss_mask |= 1 << ssid; | 
|---|
| 1883 |  | 
|---|
| 1884 | } while_each_subsys_mask(); | 
|---|
| 1885 |  | 
|---|
| 1886 | if (dfl_disable_ss_mask) { | 
|---|
| 1887 | struct cgroup *scgrp = &cgrp_dfl_root.cgrp; | 
|---|
| 1888 |  | 
|---|
| 1889 | /* | 
|---|
| 1890 | * Controllers from default hierarchy that need to be rebound | 
|---|
| 1891 | * are all disabled together in one go. | 
|---|
| 1892 | */ | 
|---|
| 1893 | cgrp_dfl_root.subsys_mask &= ~dfl_disable_ss_mask; | 
|---|
| 1894 | WARN_ON(cgroup_apply_control(scgrp)); | 
|---|
| 1895 | cgroup_finalize_control(cgrp: scgrp, ret: 0); | 
|---|
| 1896 | } | 
|---|
| 1897 |  | 
|---|
| 1898 | do_each_subsys_mask(ss, ssid, ss_mask) { | 
|---|
| 1899 | struct cgroup_root *src_root = ss->root; | 
|---|
| 1900 | struct cgroup *scgrp = &src_root->cgrp; | 
|---|
| 1901 | struct cgroup_subsys_state *css = cgroup_css(cgrp: scgrp, ss); | 
|---|
| 1902 | struct css_set *cset, *cset_pos; | 
|---|
| 1903 | struct css_task_iter *it; | 
|---|
| 1904 |  | 
|---|
| 1905 | WARN_ON(!css || cgroup_css(dcgrp, ss)); | 
|---|
| 1906 |  | 
|---|
| 1907 | if (src_root != &cgrp_dfl_root) { | 
|---|
| 1908 | /* disable from the source */ | 
|---|
| 1909 | src_root->subsys_mask &= ~(1 << ssid); | 
|---|
| 1910 | WARN_ON(cgroup_apply_control(scgrp)); | 
|---|
| 1911 | cgroup_finalize_control(cgrp: scgrp, ret: 0); | 
|---|
| 1912 | } | 
|---|
| 1913 |  | 
|---|
| 1914 | /* rebind */ | 
|---|
| 1915 | RCU_INIT_POINTER(scgrp->subsys[ssid], NULL); | 
|---|
| 1916 | rcu_assign_pointer(dcgrp->subsys[ssid], css); | 
|---|
| 1917 | ss->root = dst_root; | 
|---|
| 1918 |  | 
|---|
| 1919 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 1920 | css->cgroup = dcgrp; | 
|---|
| 1921 | WARN_ON(!list_empty(&dcgrp->e_csets[ss->id])); | 
|---|
| 1922 | list_for_each_entry_safe(cset, cset_pos, &scgrp->e_csets[ss->id], | 
|---|
| 1923 | e_cset_node[ss->id]) { | 
|---|
| 1924 | list_move_tail(list: &cset->e_cset_node[ss->id], | 
|---|
| 1925 | head: &dcgrp->e_csets[ss->id]); | 
|---|
| 1926 | /* | 
|---|
| 1927 | * all css_sets of scgrp together in same order to dcgrp, | 
|---|
| 1928 | * patch in-flight iterators to preserve correct iteration. | 
|---|
| 1929 | * since the iterator is always advanced right away and | 
|---|
| 1930 | * finished when it->cset_pos meets it->cset_head, so only | 
|---|
| 1931 | * update it->cset_head is enough here. | 
|---|
| 1932 | */ | 
|---|
| 1933 | list_for_each_entry(it, &cset->task_iters, iters_node) | 
|---|
| 1934 | if (it->cset_head == &scgrp->e_csets[ss->id]) | 
|---|
| 1935 | it->cset_head = &dcgrp->e_csets[ss->id]; | 
|---|
| 1936 | } | 
|---|
| 1937 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 1938 |  | 
|---|
| 1939 | /* default hierarchy doesn't enable controllers by default */ | 
|---|
| 1940 | dst_root->subsys_mask |= 1 << ssid; | 
|---|
| 1941 | if (dst_root == &cgrp_dfl_root) { | 
|---|
| 1942 | static_branch_enable(cgroup_subsys_on_dfl_key[ssid]); | 
|---|
| 1943 | } else { | 
|---|
| 1944 | dcgrp->subtree_control |= 1 << ssid; | 
|---|
| 1945 | static_branch_disable(cgroup_subsys_on_dfl_key[ssid]); | 
|---|
| 1946 | } | 
|---|
| 1947 |  | 
|---|
| 1948 | ret = cgroup_apply_control(cgrp: dcgrp); | 
|---|
| 1949 | if (ret) | 
|---|
| 1950 | pr_warn( "partial failure to rebind %s controller (err=%d)\n", | 
|---|
| 1951 | ss->name, ret); | 
|---|
| 1952 |  | 
|---|
| 1953 | if (ss->bind) | 
|---|
| 1954 | ss->bind(css); | 
|---|
| 1955 | } while_each_subsys_mask(); | 
|---|
| 1956 |  | 
|---|
| 1957 | kernfs_activate(kn: dcgrp->kn); | 
|---|
| 1958 | return 0; | 
|---|
| 1959 | } | 
|---|
| 1960 |  | 
|---|
| 1961 | int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node, | 
|---|
| 1962 | struct kernfs_root *kf_root) | 
|---|
| 1963 | { | 
|---|
| 1964 | int len = 0; | 
|---|
| 1965 | char *buf = NULL; | 
|---|
| 1966 | struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root); | 
|---|
| 1967 | struct cgroup *ns_cgroup; | 
|---|
| 1968 |  | 
|---|
| 1969 | buf = kmalloc(PATH_MAX, GFP_KERNEL); | 
|---|
| 1970 | if (!buf) | 
|---|
| 1971 | return -ENOMEM; | 
|---|
| 1972 |  | 
|---|
| 1973 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 1974 | ns_cgroup = current_cgns_cgroup_from_root(root: kf_cgroot); | 
|---|
| 1975 | len = kernfs_path_from_node(kn_to: kf_node, kn_from: ns_cgroup->kn, buf, PATH_MAX); | 
|---|
| 1976 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 1977 |  | 
|---|
| 1978 | if (len == -E2BIG) | 
|---|
| 1979 | len = -ERANGE; | 
|---|
| 1980 | else if (len > 0) { | 
|---|
| 1981 | seq_escape(m: sf, s: buf, esc: " \t\n\\"); | 
|---|
| 1982 | len = 0; | 
|---|
| 1983 | } | 
|---|
| 1984 | kfree(objp: buf); | 
|---|
| 1985 | return len; | 
|---|
| 1986 | } | 
|---|
| 1987 |  | 
|---|
| 1988 | enum cgroup2_param { | 
|---|
| 1989 | Opt_nsdelegate, | 
|---|
| 1990 | Opt_favordynmods, | 
|---|
| 1991 | Opt_memory_localevents, | 
|---|
| 1992 | Opt_memory_recursiveprot, | 
|---|
| 1993 | Opt_memory_hugetlb_accounting, | 
|---|
| 1994 | Opt_pids_localevents, | 
|---|
| 1995 | nr__cgroup2_params | 
|---|
| 1996 | }; | 
|---|
| 1997 |  | 
|---|
| 1998 | static const struct fs_parameter_spec cgroup2_fs_parameters[] = { | 
|---|
| 1999 | fsparam_flag( "nsdelegate",		Opt_nsdelegate), | 
|---|
| 2000 | fsparam_flag( "favordynmods",		Opt_favordynmods), | 
|---|
| 2001 | fsparam_flag( "memory_localevents",	Opt_memory_localevents), | 
|---|
| 2002 | fsparam_flag( "memory_recursiveprot",	Opt_memory_recursiveprot), | 
|---|
| 2003 | fsparam_flag( "memory_hugetlb_accounting", Opt_memory_hugetlb_accounting), | 
|---|
| 2004 | fsparam_flag( "pids_localevents",	Opt_pids_localevents), | 
|---|
| 2005 | {} | 
|---|
| 2006 | }; | 
|---|
| 2007 |  | 
|---|
| 2008 | static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param) | 
|---|
| 2009 | { | 
|---|
| 2010 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); | 
|---|
| 2011 | struct fs_parse_result result; | 
|---|
| 2012 | int opt; | 
|---|
| 2013 |  | 
|---|
| 2014 | opt = fs_parse(fc, desc: cgroup2_fs_parameters, param, result: &result); | 
|---|
| 2015 | if (opt < 0) | 
|---|
| 2016 | return opt; | 
|---|
| 2017 |  | 
|---|
| 2018 | switch (opt) { | 
|---|
| 2019 | case Opt_nsdelegate: | 
|---|
| 2020 | ctx->flags |= CGRP_ROOT_NS_DELEGATE; | 
|---|
| 2021 | return 0; | 
|---|
| 2022 | case Opt_favordynmods: | 
|---|
| 2023 | ctx->flags |= CGRP_ROOT_FAVOR_DYNMODS; | 
|---|
| 2024 | return 0; | 
|---|
| 2025 | case Opt_memory_localevents: | 
|---|
| 2026 | ctx->flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS; | 
|---|
| 2027 | return 0; | 
|---|
| 2028 | case Opt_memory_recursiveprot: | 
|---|
| 2029 | ctx->flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT; | 
|---|
| 2030 | return 0; | 
|---|
| 2031 | case Opt_memory_hugetlb_accounting: | 
|---|
| 2032 | ctx->flags |= CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING; | 
|---|
| 2033 | return 0; | 
|---|
| 2034 | case Opt_pids_localevents: | 
|---|
| 2035 | ctx->flags |= CGRP_ROOT_PIDS_LOCAL_EVENTS; | 
|---|
| 2036 | return 0; | 
|---|
| 2037 | } | 
|---|
| 2038 | return -EINVAL; | 
|---|
| 2039 | } | 
|---|
| 2040 |  | 
|---|
| 2041 | struct cgroup_of_peak *of_peak(struct kernfs_open_file *of) | 
|---|
| 2042 | { | 
|---|
| 2043 | struct cgroup_file_ctx *ctx = of->priv; | 
|---|
| 2044 |  | 
|---|
| 2045 | return &ctx->peak; | 
|---|
| 2046 | } | 
|---|
| 2047 |  | 
|---|
| 2048 | static void apply_cgroup_root_flags(unsigned int root_flags) | 
|---|
| 2049 | { | 
|---|
| 2050 | if (current->nsproxy->cgroup_ns == &init_cgroup_ns) { | 
|---|
| 2051 | if (root_flags & CGRP_ROOT_NS_DELEGATE) | 
|---|
| 2052 | cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE; | 
|---|
| 2053 | else | 
|---|
| 2054 | cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE; | 
|---|
| 2055 |  | 
|---|
| 2056 | cgroup_favor_dynmods(root: &cgrp_dfl_root, | 
|---|
| 2057 | favor: root_flags & CGRP_ROOT_FAVOR_DYNMODS); | 
|---|
| 2058 |  | 
|---|
| 2059 | if (root_flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS) | 
|---|
| 2060 | cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS; | 
|---|
| 2061 | else | 
|---|
| 2062 | cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_LOCAL_EVENTS; | 
|---|
| 2063 |  | 
|---|
| 2064 | if (root_flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT) | 
|---|
| 2065 | cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT; | 
|---|
| 2066 | else | 
|---|
| 2067 | cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_RECURSIVE_PROT; | 
|---|
| 2068 |  | 
|---|
| 2069 | if (root_flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING) | 
|---|
| 2070 | cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING; | 
|---|
| 2071 | else | 
|---|
| 2072 | cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING; | 
|---|
| 2073 |  | 
|---|
| 2074 | if (root_flags & CGRP_ROOT_PIDS_LOCAL_EVENTS) | 
|---|
| 2075 | cgrp_dfl_root.flags |= CGRP_ROOT_PIDS_LOCAL_EVENTS; | 
|---|
| 2076 | else | 
|---|
| 2077 | cgrp_dfl_root.flags &= ~CGRP_ROOT_PIDS_LOCAL_EVENTS; | 
|---|
| 2078 | } | 
|---|
| 2079 | } | 
|---|
| 2080 |  | 
|---|
| 2081 | static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root) | 
|---|
| 2082 | { | 
|---|
| 2083 | if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) | 
|---|
| 2084 | seq_puts(m: seq, s: ",nsdelegate"); | 
|---|
| 2085 | if (cgrp_dfl_root.flags & CGRP_ROOT_FAVOR_DYNMODS) | 
|---|
| 2086 | seq_puts(m: seq, s: ",favordynmods"); | 
|---|
| 2087 | if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS) | 
|---|
| 2088 | seq_puts(m: seq, s: ",memory_localevents"); | 
|---|
| 2089 | if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT) | 
|---|
| 2090 | seq_puts(m: seq, s: ",memory_recursiveprot"); | 
|---|
| 2091 | if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING) | 
|---|
| 2092 | seq_puts(m: seq, s: ",memory_hugetlb_accounting"); | 
|---|
| 2093 | if (cgrp_dfl_root.flags & CGRP_ROOT_PIDS_LOCAL_EVENTS) | 
|---|
| 2094 | seq_puts(m: seq, s: ",pids_localevents"); | 
|---|
| 2095 | return 0; | 
|---|
| 2096 | } | 
|---|
| 2097 |  | 
|---|
| 2098 | static int cgroup_reconfigure(struct fs_context *fc) | 
|---|
| 2099 | { | 
|---|
| 2100 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); | 
|---|
| 2101 |  | 
|---|
| 2102 | apply_cgroup_root_flags(root_flags: ctx->flags); | 
|---|
| 2103 | return 0; | 
|---|
| 2104 | } | 
|---|
| 2105 |  | 
|---|
| 2106 | static void init_cgroup_housekeeping(struct cgroup *cgrp) | 
|---|
| 2107 | { | 
|---|
| 2108 | struct cgroup_subsys *ss; | 
|---|
| 2109 | int ssid; | 
|---|
| 2110 |  | 
|---|
| 2111 | INIT_LIST_HEAD(list: &cgrp->self.sibling); | 
|---|
| 2112 | INIT_LIST_HEAD(list: &cgrp->self.children); | 
|---|
| 2113 | INIT_LIST_HEAD(list: &cgrp->cset_links); | 
|---|
| 2114 | INIT_LIST_HEAD(list: &cgrp->pidlists); | 
|---|
| 2115 | mutex_init(&cgrp->pidlist_mutex); | 
|---|
| 2116 | cgrp->self.cgroup = cgrp; | 
|---|
| 2117 | cgrp->self.flags |= CSS_ONLINE; | 
|---|
| 2118 | cgrp->dom_cgrp = cgrp; | 
|---|
| 2119 | cgrp->max_descendants = INT_MAX; | 
|---|
| 2120 | cgrp->max_depth = INT_MAX; | 
|---|
| 2121 | prev_cputime_init(prev: &cgrp->prev_cputime); | 
|---|
| 2122 |  | 
|---|
| 2123 | for_each_subsys(ss, ssid) | 
|---|
| 2124 | INIT_LIST_HEAD(list: &cgrp->e_csets[ssid]); | 
|---|
| 2125 |  | 
|---|
| 2126 | #ifdef CONFIG_CGROUP_BPF | 
|---|
| 2127 | for (int i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++) | 
|---|
| 2128 | cgrp->bpf.revisions[i] = 1; | 
|---|
| 2129 | #endif | 
|---|
| 2130 |  | 
|---|
| 2131 | init_waitqueue_head(&cgrp->offline_waitq); | 
|---|
| 2132 | INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent); | 
|---|
| 2133 | } | 
|---|
| 2134 |  | 
|---|
| 2135 | void init_cgroup_root(struct cgroup_fs_context *ctx) | 
|---|
| 2136 | { | 
|---|
| 2137 | struct cgroup_root *root = ctx->root; | 
|---|
| 2138 | struct cgroup *cgrp = &root->cgrp; | 
|---|
| 2139 |  | 
|---|
| 2140 | INIT_LIST_HEAD_RCU(list: &root->root_list); | 
|---|
| 2141 | atomic_set(v: &root->nr_cgrps, i: 1); | 
|---|
| 2142 | cgrp->root = root; | 
|---|
| 2143 | init_cgroup_housekeeping(cgrp); | 
|---|
| 2144 |  | 
|---|
| 2145 | /* DYNMODS must be modified through cgroup_favor_dynmods() */ | 
|---|
| 2146 | root->flags = ctx->flags & ~CGRP_ROOT_FAVOR_DYNMODS; | 
|---|
| 2147 | if (ctx->release_agent) | 
|---|
| 2148 | strscpy(root->release_agent_path, ctx->release_agent, PATH_MAX); | 
|---|
| 2149 | if (ctx->name) | 
|---|
| 2150 | strscpy(root->name, ctx->name, MAX_CGROUP_ROOT_NAMELEN); | 
|---|
| 2151 | if (ctx->cpuset_clone_children) | 
|---|
| 2152 | set_bit(nr: CGRP_CPUSET_CLONE_CHILDREN, addr: &root->cgrp.flags); | 
|---|
| 2153 | } | 
|---|
| 2154 |  | 
|---|
| 2155 | int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask) | 
|---|
| 2156 | { | 
|---|
| 2157 | LIST_HEAD(tmp_links); | 
|---|
| 2158 | struct cgroup *root_cgrp = &root->cgrp; | 
|---|
| 2159 | struct kernfs_syscall_ops *kf_sops; | 
|---|
| 2160 | struct css_set *cset; | 
|---|
| 2161 | int i, ret; | 
|---|
| 2162 |  | 
|---|
| 2163 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 2164 |  | 
|---|
| 2165 | ret = percpu_ref_init(ref: &root_cgrp->self.refcnt, release: css_release, | 
|---|
| 2166 | flags: 0, GFP_KERNEL); | 
|---|
| 2167 | if (ret) | 
|---|
| 2168 | goto out; | 
|---|
| 2169 |  | 
|---|
| 2170 | /* | 
|---|
| 2171 | * We're accessing css_set_count without locking css_set_lock here, | 
|---|
| 2172 | * but that's OK - it can only be increased by someone holding | 
|---|
| 2173 | * cgroup_lock, and that's us.  Later rebinding may disable | 
|---|
| 2174 | * controllers on the default hierarchy and thus create new csets, | 
|---|
| 2175 | * which can't be more than the existing ones.  Allocate 2x. | 
|---|
| 2176 | */ | 
|---|
| 2177 | ret = allocate_cgrp_cset_links(count: 2 * css_set_count, tmp_links: &tmp_links); | 
|---|
| 2178 | if (ret) | 
|---|
| 2179 | goto cancel_ref; | 
|---|
| 2180 |  | 
|---|
| 2181 | ret = cgroup_init_root_id(root); | 
|---|
| 2182 | if (ret) | 
|---|
| 2183 | goto cancel_ref; | 
|---|
| 2184 |  | 
|---|
| 2185 | kf_sops = root == &cgrp_dfl_root ? | 
|---|
| 2186 | &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops; | 
|---|
| 2187 |  | 
|---|
| 2188 | root->kf_root = kernfs_create_root(scops: kf_sops, | 
|---|
| 2189 | flags: KERNFS_ROOT_CREATE_DEACTIVATED | | 
|---|
| 2190 | KERNFS_ROOT_SUPPORT_EXPORTOP | | 
|---|
| 2191 | KERNFS_ROOT_SUPPORT_USER_XATTR | | 
|---|
| 2192 | KERNFS_ROOT_INVARIANT_PARENT, | 
|---|
| 2193 | priv: root_cgrp); | 
|---|
| 2194 | if (IS_ERR(ptr: root->kf_root)) { | 
|---|
| 2195 | ret = PTR_ERR(ptr: root->kf_root); | 
|---|
| 2196 | goto exit_root_id; | 
|---|
| 2197 | } | 
|---|
| 2198 | root_cgrp->kn = kernfs_root_to_node(root: root->kf_root); | 
|---|
| 2199 | WARN_ON_ONCE(cgroup_ino(root_cgrp) != 1); | 
|---|
| 2200 | root_cgrp->ancestors[0] = root_cgrp; | 
|---|
| 2201 |  | 
|---|
| 2202 | ret = css_populate_dir(css: &root_cgrp->self); | 
|---|
| 2203 | if (ret) | 
|---|
| 2204 | goto destroy_root; | 
|---|
| 2205 |  | 
|---|
| 2206 | ret = css_rstat_init(css: &root_cgrp->self); | 
|---|
| 2207 | if (ret) | 
|---|
| 2208 | goto destroy_root; | 
|---|
| 2209 |  | 
|---|
| 2210 | ret = rebind_subsystems(dst_root: root, ss_mask); | 
|---|
| 2211 | if (ret) | 
|---|
| 2212 | goto exit_stats; | 
|---|
| 2213 |  | 
|---|
| 2214 | ret = blocking_notifier_call_chain(nh: &cgroup_lifetime_notifier, | 
|---|
| 2215 | val: CGROUP_LIFETIME_ONLINE, v: root_cgrp); | 
|---|
| 2216 | WARN_ON_ONCE(notifier_to_errno(ret)); | 
|---|
| 2217 |  | 
|---|
| 2218 | trace_cgroup_setup_root(root); | 
|---|
| 2219 |  | 
|---|
| 2220 | /* | 
|---|
| 2221 | * There must be no failure case after here, since rebinding takes | 
|---|
| 2222 | * care of subsystems' refcounts, which are explicitly dropped in | 
|---|
| 2223 | * the failure exit path. | 
|---|
| 2224 | */ | 
|---|
| 2225 | list_add_rcu(new: &root->root_list, head: &cgroup_roots); | 
|---|
| 2226 | cgroup_root_count++; | 
|---|
| 2227 |  | 
|---|
| 2228 | /* | 
|---|
| 2229 | * Link the root cgroup in this hierarchy into all the css_set | 
|---|
| 2230 | * objects. | 
|---|
| 2231 | */ | 
|---|
| 2232 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 2233 | hash_for_each(css_set_table, i, cset, hlist) { | 
|---|
| 2234 | link_css_set(tmp_links: &tmp_links, cset, cgrp: root_cgrp); | 
|---|
| 2235 | if (css_set_populated(cset)) | 
|---|
| 2236 | cgroup_update_populated(cgrp: root_cgrp, populated: true); | 
|---|
| 2237 | } | 
|---|
| 2238 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 2239 |  | 
|---|
| 2240 | BUG_ON(!list_empty(&root_cgrp->self.children)); | 
|---|
| 2241 | BUG_ON(atomic_read(&root->nr_cgrps) != 1); | 
|---|
| 2242 |  | 
|---|
| 2243 | ret = 0; | 
|---|
| 2244 | goto out; | 
|---|
| 2245 |  | 
|---|
| 2246 | exit_stats: | 
|---|
| 2247 | css_rstat_exit(css: &root_cgrp->self); | 
|---|
| 2248 | destroy_root: | 
|---|
| 2249 | kernfs_destroy_root(root: root->kf_root); | 
|---|
| 2250 | root->kf_root = NULL; | 
|---|
| 2251 | exit_root_id: | 
|---|
| 2252 | cgroup_exit_root_id(root); | 
|---|
| 2253 | cancel_ref: | 
|---|
| 2254 | percpu_ref_exit(ref: &root_cgrp->self.refcnt); | 
|---|
| 2255 | out: | 
|---|
| 2256 | free_cgrp_cset_links(links_to_free: &tmp_links); | 
|---|
| 2257 | return ret; | 
|---|
| 2258 | } | 
|---|
| 2259 |  | 
|---|
| 2260 | int cgroup_do_get_tree(struct fs_context *fc) | 
|---|
| 2261 | { | 
|---|
| 2262 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); | 
|---|
| 2263 | int ret; | 
|---|
| 2264 |  | 
|---|
| 2265 | ctx->kfc.root = ctx->root->kf_root; | 
|---|
| 2266 | if (fc->fs_type == &cgroup2_fs_type) | 
|---|
| 2267 | ctx->kfc.magic = CGROUP2_SUPER_MAGIC; | 
|---|
| 2268 | else | 
|---|
| 2269 | ctx->kfc.magic = CGROUP_SUPER_MAGIC; | 
|---|
| 2270 | ret = kernfs_get_tree(fc); | 
|---|
| 2271 |  | 
|---|
| 2272 | /* | 
|---|
| 2273 | * In non-init cgroup namespace, instead of root cgroup's dentry, | 
|---|
| 2274 | * we return the dentry corresponding to the cgroupns->root_cgrp. | 
|---|
| 2275 | */ | 
|---|
| 2276 | if (!ret && ctx->ns != &init_cgroup_ns) { | 
|---|
| 2277 | struct dentry *nsdentry; | 
|---|
| 2278 | struct super_block *sb = fc->root->d_sb; | 
|---|
| 2279 | struct cgroup *cgrp; | 
|---|
| 2280 |  | 
|---|
| 2281 | cgroup_lock(); | 
|---|
| 2282 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 2283 |  | 
|---|
| 2284 | cgrp = cset_cgroup_from_root(cset: ctx->ns->root_cset, root: ctx->root); | 
|---|
| 2285 |  | 
|---|
| 2286 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 2287 | cgroup_unlock(); | 
|---|
| 2288 |  | 
|---|
| 2289 | nsdentry = kernfs_node_dentry(kn: cgrp->kn, sb); | 
|---|
| 2290 | dput(fc->root); | 
|---|
| 2291 | if (IS_ERR(ptr: nsdentry)) { | 
|---|
| 2292 | deactivate_locked_super(sb); | 
|---|
| 2293 | ret = PTR_ERR(ptr: nsdentry); | 
|---|
| 2294 | nsdentry = NULL; | 
|---|
| 2295 | } | 
|---|
| 2296 | fc->root = nsdentry; | 
|---|
| 2297 | } | 
|---|
| 2298 |  | 
|---|
| 2299 | if (!ctx->kfc.new_sb_created) | 
|---|
| 2300 | cgroup_put(cgrp: &ctx->root->cgrp); | 
|---|
| 2301 |  | 
|---|
| 2302 | return ret; | 
|---|
| 2303 | } | 
|---|
| 2304 |  | 
|---|
| 2305 | /* | 
|---|
| 2306 | * Destroy a cgroup filesystem context. | 
|---|
| 2307 | */ | 
|---|
| 2308 | static void cgroup_fs_context_free(struct fs_context *fc) | 
|---|
| 2309 | { | 
|---|
| 2310 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); | 
|---|
| 2311 |  | 
|---|
| 2312 | kfree(objp: ctx->name); | 
|---|
| 2313 | kfree(objp: ctx->release_agent); | 
|---|
| 2314 | put_cgroup_ns(ns: ctx->ns); | 
|---|
| 2315 | kernfs_free_fs_context(fc); | 
|---|
| 2316 | kfree(objp: ctx); | 
|---|
| 2317 | } | 
|---|
| 2318 |  | 
|---|
| 2319 | static int cgroup_get_tree(struct fs_context *fc) | 
|---|
| 2320 | { | 
|---|
| 2321 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); | 
|---|
| 2322 | int ret; | 
|---|
| 2323 |  | 
|---|
| 2324 | WRITE_ONCE(cgrp_dfl_visible, true); | 
|---|
| 2325 | cgroup_get_live(cgrp: &cgrp_dfl_root.cgrp); | 
|---|
| 2326 | ctx->root = &cgrp_dfl_root; | 
|---|
| 2327 |  | 
|---|
| 2328 | ret = cgroup_do_get_tree(fc); | 
|---|
| 2329 | if (!ret) | 
|---|
| 2330 | apply_cgroup_root_flags(root_flags: ctx->flags); | 
|---|
| 2331 | return ret; | 
|---|
| 2332 | } | 
|---|
| 2333 |  | 
|---|
| 2334 | static const struct fs_context_operations cgroup_fs_context_ops = { | 
|---|
| 2335 | .free		= cgroup_fs_context_free, | 
|---|
| 2336 | .parse_param	= cgroup2_parse_param, | 
|---|
| 2337 | .get_tree	= cgroup_get_tree, | 
|---|
| 2338 | .reconfigure	= cgroup_reconfigure, | 
|---|
| 2339 | }; | 
|---|
| 2340 |  | 
|---|
| 2341 | static const struct fs_context_operations cgroup1_fs_context_ops = { | 
|---|
| 2342 | .free		= cgroup_fs_context_free, | 
|---|
| 2343 | .parse_param	= cgroup1_parse_param, | 
|---|
| 2344 | .get_tree	= cgroup1_get_tree, | 
|---|
| 2345 | .reconfigure	= cgroup1_reconfigure, | 
|---|
| 2346 | }; | 
|---|
| 2347 |  | 
|---|
| 2348 | /* | 
|---|
| 2349 | * Initialise the cgroup filesystem creation/reconfiguration context.  Notably, | 
|---|
| 2350 | * we select the namespace we're going to use. | 
|---|
| 2351 | */ | 
|---|
| 2352 | static int cgroup_init_fs_context(struct fs_context *fc) | 
|---|
| 2353 | { | 
|---|
| 2354 | struct cgroup_fs_context *ctx; | 
|---|
| 2355 |  | 
|---|
| 2356 | ctx = kzalloc(sizeof(struct cgroup_fs_context), GFP_KERNEL); | 
|---|
| 2357 | if (!ctx) | 
|---|
| 2358 | return -ENOMEM; | 
|---|
| 2359 |  | 
|---|
| 2360 | ctx->ns = current->nsproxy->cgroup_ns; | 
|---|
| 2361 | get_cgroup_ns(ns: ctx->ns); | 
|---|
| 2362 | fc->fs_private = &ctx->kfc; | 
|---|
| 2363 | if (fc->fs_type == &cgroup2_fs_type) | 
|---|
| 2364 | fc->ops = &cgroup_fs_context_ops; | 
|---|
| 2365 | else | 
|---|
| 2366 | fc->ops = &cgroup1_fs_context_ops; | 
|---|
| 2367 | put_user_ns(ns: fc->user_ns); | 
|---|
| 2368 | fc->user_ns = get_user_ns(ns: ctx->ns->user_ns); | 
|---|
| 2369 | fc->global = true; | 
|---|
| 2370 |  | 
|---|
| 2371 | if (have_favordynmods) | 
|---|
| 2372 | ctx->flags |= CGRP_ROOT_FAVOR_DYNMODS; | 
|---|
| 2373 |  | 
|---|
| 2374 | return 0; | 
|---|
| 2375 | } | 
|---|
| 2376 |  | 
|---|
| 2377 | static void cgroup_kill_sb(struct super_block *sb) | 
|---|
| 2378 | { | 
|---|
| 2379 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); | 
|---|
| 2380 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); | 
|---|
| 2381 |  | 
|---|
| 2382 | /* | 
|---|
| 2383 | * If @root doesn't have any children, start killing it. | 
|---|
| 2384 | * This prevents new mounts by disabling percpu_ref_tryget_live(). | 
|---|
| 2385 | * | 
|---|
| 2386 | * And don't kill the default root. | 
|---|
| 2387 | */ | 
|---|
| 2388 | if (list_empty(head: &root->cgrp.self.children) && root != &cgrp_dfl_root && | 
|---|
| 2389 | !percpu_ref_is_dying(ref: &root->cgrp.self.refcnt)) | 
|---|
| 2390 | percpu_ref_kill(ref: &root->cgrp.self.refcnt); | 
|---|
| 2391 | cgroup_put(cgrp: &root->cgrp); | 
|---|
| 2392 | kernfs_kill_sb(sb); | 
|---|
| 2393 | } | 
|---|
| 2394 |  | 
|---|
| 2395 | struct file_system_type cgroup_fs_type = { | 
|---|
| 2396 | .name			= "cgroup", | 
|---|
| 2397 | .init_fs_context	= cgroup_init_fs_context, | 
|---|
| 2398 | .parameters		= cgroup1_fs_parameters, | 
|---|
| 2399 | .kill_sb		= cgroup_kill_sb, | 
|---|
| 2400 | .fs_flags		= FS_USERNS_MOUNT, | 
|---|
| 2401 | }; | 
|---|
| 2402 |  | 
|---|
| 2403 | static struct file_system_type cgroup2_fs_type = { | 
|---|
| 2404 | .name			= "cgroup2", | 
|---|
| 2405 | .init_fs_context	= cgroup_init_fs_context, | 
|---|
| 2406 | .parameters		= cgroup2_fs_parameters, | 
|---|
| 2407 | .kill_sb		= cgroup_kill_sb, | 
|---|
| 2408 | .fs_flags		= FS_USERNS_MOUNT, | 
|---|
| 2409 | }; | 
|---|
| 2410 |  | 
|---|
| 2411 | #ifdef CONFIG_CPUSETS_V1 | 
|---|
| 2412 | enum cpuset_param { | 
|---|
| 2413 | Opt_cpuset_v2_mode, | 
|---|
| 2414 | }; | 
|---|
| 2415 |  | 
|---|
| 2416 | static const struct fs_parameter_spec cpuset_fs_parameters[] = { | 
|---|
| 2417 | fsparam_flag  ( "cpuset_v2_mode", Opt_cpuset_v2_mode), | 
|---|
| 2418 | {} | 
|---|
| 2419 | }; | 
|---|
| 2420 |  | 
|---|
| 2421 | static int cpuset_parse_param(struct fs_context *fc, struct fs_parameter *param) | 
|---|
| 2422 | { | 
|---|
| 2423 | struct cgroup_fs_context *ctx = cgroup_fc2context(fc); | 
|---|
| 2424 | struct fs_parse_result result; | 
|---|
| 2425 | int opt; | 
|---|
| 2426 |  | 
|---|
| 2427 | opt = fs_parse(fc, cpuset_fs_parameters, param, &result); | 
|---|
| 2428 | if (opt < 0) | 
|---|
| 2429 | return opt; | 
|---|
| 2430 |  | 
|---|
| 2431 | switch (opt) { | 
|---|
| 2432 | case Opt_cpuset_v2_mode: | 
|---|
| 2433 | ctx->flags |= CGRP_ROOT_CPUSET_V2_MODE; | 
|---|
| 2434 | return 0; | 
|---|
| 2435 | } | 
|---|
| 2436 | return -EINVAL; | 
|---|
| 2437 | } | 
|---|
| 2438 |  | 
|---|
| 2439 | static const struct fs_context_operations cpuset_fs_context_ops = { | 
|---|
| 2440 | .get_tree	= cgroup1_get_tree, | 
|---|
| 2441 | .free		= cgroup_fs_context_free, | 
|---|
| 2442 | .parse_param	= cpuset_parse_param, | 
|---|
| 2443 | }; | 
|---|
| 2444 |  | 
|---|
| 2445 | /* | 
|---|
| 2446 | * This is ugly, but preserves the userspace API for existing cpuset | 
|---|
| 2447 | * users. If someone tries to mount the "cpuset" filesystem, we | 
|---|
| 2448 | * silently switch it to mount "cgroup" instead | 
|---|
| 2449 | */ | 
|---|
| 2450 | static int cpuset_init_fs_context(struct fs_context *fc) | 
|---|
| 2451 | { | 
|---|
| 2452 | char *agent = kstrdup( "/sbin/cpuset_release_agent", GFP_USER); | 
|---|
| 2453 | struct cgroup_fs_context *ctx; | 
|---|
| 2454 | int err; | 
|---|
| 2455 |  | 
|---|
| 2456 | err = cgroup_init_fs_context(fc); | 
|---|
| 2457 | if (err) { | 
|---|
| 2458 | kfree(agent); | 
|---|
| 2459 | return err; | 
|---|
| 2460 | } | 
|---|
| 2461 |  | 
|---|
| 2462 | fc->ops = &cpuset_fs_context_ops; | 
|---|
| 2463 |  | 
|---|
| 2464 | ctx = cgroup_fc2context(fc); | 
|---|
| 2465 | ctx->subsys_mask = 1 << cpuset_cgrp_id; | 
|---|
| 2466 | ctx->flags |= CGRP_ROOT_NOPREFIX; | 
|---|
| 2467 | ctx->release_agent = agent; | 
|---|
| 2468 |  | 
|---|
| 2469 | get_filesystem(&cgroup_fs_type); | 
|---|
| 2470 | put_filesystem(fc->fs_type); | 
|---|
| 2471 | fc->fs_type = &cgroup_fs_type; | 
|---|
| 2472 |  | 
|---|
| 2473 | return 0; | 
|---|
| 2474 | } | 
|---|
| 2475 |  | 
|---|
| 2476 | static struct file_system_type cpuset_fs_type = { | 
|---|
| 2477 | .name			= "cpuset", | 
|---|
| 2478 | .init_fs_context	= cpuset_init_fs_context, | 
|---|
| 2479 | .parameters		= cpuset_fs_parameters, | 
|---|
| 2480 | .fs_flags		= FS_USERNS_MOUNT, | 
|---|
| 2481 | }; | 
|---|
| 2482 | #endif | 
|---|
| 2483 |  | 
|---|
| 2484 | int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen, | 
|---|
| 2485 | struct cgroup_namespace *ns) | 
|---|
| 2486 | { | 
|---|
| 2487 | struct cgroup *root = cset_cgroup_from_root(cset: ns->root_cset, root: cgrp->root); | 
|---|
| 2488 |  | 
|---|
| 2489 | return kernfs_path_from_node(kn_to: cgrp->kn, kn_from: root->kn, buf, buflen); | 
|---|
| 2490 | } | 
|---|
| 2491 |  | 
|---|
| 2492 | int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen, | 
|---|
| 2493 | struct cgroup_namespace *ns) | 
|---|
| 2494 | { | 
|---|
| 2495 | int ret; | 
|---|
| 2496 |  | 
|---|
| 2497 | cgroup_lock(); | 
|---|
| 2498 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 2499 |  | 
|---|
| 2500 | ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns); | 
|---|
| 2501 |  | 
|---|
| 2502 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 2503 | cgroup_unlock(); | 
|---|
| 2504 |  | 
|---|
| 2505 | return ret; | 
|---|
| 2506 | } | 
|---|
| 2507 | EXPORT_SYMBOL_GPL(cgroup_path_ns); | 
|---|
| 2508 |  | 
|---|
| 2509 | /** | 
|---|
| 2510 | * cgroup_attach_lock - Lock for ->attach() | 
|---|
| 2511 | * @lock_mode: whether acquire and acquire which rwsem | 
|---|
| 2512 | * @tsk: thread group to lock | 
|---|
| 2513 | * | 
|---|
| 2514 | * cgroup migration sometimes needs to stabilize threadgroups against forks and | 
|---|
| 2515 | * exits by write-locking cgroup_threadgroup_rwsem. However, some ->attach() | 
|---|
| 2516 | * implementations (e.g. cpuset), also need to disable CPU hotplug. | 
|---|
| 2517 | * Unfortunately, letting ->attach() operations acquire cpus_read_lock() can | 
|---|
| 2518 | * lead to deadlocks. | 
|---|
| 2519 | * | 
|---|
| 2520 | * Bringing up a CPU may involve creating and destroying tasks which requires | 
|---|
| 2521 | * read-locking threadgroup_rwsem, so threadgroup_rwsem nests inside | 
|---|
| 2522 | * cpus_read_lock(). If we call an ->attach() which acquires the cpus lock while | 
|---|
| 2523 | * write-locking threadgroup_rwsem, the locking order is reversed and we end up | 
|---|
| 2524 | * waiting for an on-going CPU hotplug operation which in turn is waiting for | 
|---|
| 2525 | * the threadgroup_rwsem to be released to create new tasks. For more details: | 
|---|
| 2526 | * | 
|---|
| 2527 | *   http://lkml.kernel.org/r/20220711174629.uehfmqegcwn2lqzu@wubuntu | 
|---|
| 2528 | * | 
|---|
| 2529 | * Resolve the situation by always acquiring cpus_read_lock() before optionally | 
|---|
| 2530 | * write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that | 
|---|
| 2531 | * CPU hotplug is disabled on entry. | 
|---|
| 2532 | * | 
|---|
| 2533 | * When favordynmods is enabled, take per threadgroup rwsem to reduce overhead | 
|---|
| 2534 | * on dynamic cgroup modifications. see the comment above | 
|---|
| 2535 | * CGRP_ROOT_FAVOR_DYNMODS definition. | 
|---|
| 2536 | * | 
|---|
| 2537 | * tsk is not NULL only when writing to cgroup.procs. | 
|---|
| 2538 | */ | 
|---|
| 2539 | void cgroup_attach_lock(enum cgroup_attach_lock_mode lock_mode, | 
|---|
| 2540 | struct task_struct *tsk) | 
|---|
| 2541 | { | 
|---|
| 2542 | cpus_read_lock(); | 
|---|
| 2543 |  | 
|---|
| 2544 | switch (lock_mode) { | 
|---|
| 2545 | case CGRP_ATTACH_LOCK_NONE: | 
|---|
| 2546 | break; | 
|---|
| 2547 | case CGRP_ATTACH_LOCK_GLOBAL: | 
|---|
| 2548 | percpu_down_write(&cgroup_threadgroup_rwsem); | 
|---|
| 2549 | break; | 
|---|
| 2550 | case CGRP_ATTACH_LOCK_PER_THREADGROUP: | 
|---|
| 2551 | down_write(sem: &tsk->signal->cgroup_threadgroup_rwsem); | 
|---|
| 2552 | break; | 
|---|
| 2553 | default: | 
|---|
| 2554 | pr_warn( "cgroup: Unexpected attach lock mode."); | 
|---|
| 2555 | break; | 
|---|
| 2556 | } | 
|---|
| 2557 | } | 
|---|
| 2558 |  | 
|---|
| 2559 | /** | 
|---|
| 2560 | * cgroup_attach_unlock - Undo cgroup_attach_lock() | 
|---|
| 2561 | * @lock_mode: whether release and release which rwsem | 
|---|
| 2562 | * @tsk: thread group to lock | 
|---|
| 2563 | */ | 
|---|
| 2564 | void cgroup_attach_unlock(enum cgroup_attach_lock_mode lock_mode, | 
|---|
| 2565 | struct task_struct *tsk) | 
|---|
| 2566 | { | 
|---|
| 2567 | switch (lock_mode) { | 
|---|
| 2568 | case CGRP_ATTACH_LOCK_NONE: | 
|---|
| 2569 | break; | 
|---|
| 2570 | case CGRP_ATTACH_LOCK_GLOBAL: | 
|---|
| 2571 | percpu_up_write(&cgroup_threadgroup_rwsem); | 
|---|
| 2572 | break; | 
|---|
| 2573 | case CGRP_ATTACH_LOCK_PER_THREADGROUP: | 
|---|
| 2574 | up_write(sem: &tsk->signal->cgroup_threadgroup_rwsem); | 
|---|
| 2575 | break; | 
|---|
| 2576 | default: | 
|---|
| 2577 | pr_warn( "cgroup: Unexpected attach lock mode."); | 
|---|
| 2578 | break; | 
|---|
| 2579 | } | 
|---|
| 2580 |  | 
|---|
| 2581 | cpus_read_unlock(); | 
|---|
| 2582 | } | 
|---|
| 2583 |  | 
|---|
| 2584 | /** | 
|---|
| 2585 | * cgroup_migrate_add_task - add a migration target task to a migration context | 
|---|
| 2586 | * @task: target task | 
|---|
| 2587 | * @mgctx: target migration context | 
|---|
| 2588 | * | 
|---|
| 2589 | * Add @task, which is a migration target, to @mgctx->tset.  This function | 
|---|
| 2590 | * becomes noop if @task doesn't need to be migrated.  @task's css_set | 
|---|
| 2591 | * should have been added as a migration source and @task->cg_list will be | 
|---|
| 2592 | * moved from the css_set's tasks list to mg_tasks one. | 
|---|
| 2593 | */ | 
|---|
| 2594 | static void cgroup_migrate_add_task(struct task_struct *task, | 
|---|
| 2595 | struct cgroup_mgctx *mgctx) | 
|---|
| 2596 | { | 
|---|
| 2597 | struct css_set *cset; | 
|---|
| 2598 |  | 
|---|
| 2599 | lockdep_assert_held(&css_set_lock); | 
|---|
| 2600 |  | 
|---|
| 2601 | /* @task either already exited or can't exit until the end */ | 
|---|
| 2602 | if (task->flags & PF_EXITING) | 
|---|
| 2603 | return; | 
|---|
| 2604 |  | 
|---|
| 2605 | /* cgroup_threadgroup_rwsem protects racing against forks */ | 
|---|
| 2606 | WARN_ON_ONCE(list_empty(&task->cg_list)); | 
|---|
| 2607 |  | 
|---|
| 2608 | cset = task_css_set(task); | 
|---|
| 2609 | if (!cset->mg_src_cgrp) | 
|---|
| 2610 | return; | 
|---|
| 2611 |  | 
|---|
| 2612 | mgctx->tset.nr_tasks++; | 
|---|
| 2613 |  | 
|---|
| 2614 | list_move_tail(list: &task->cg_list, head: &cset->mg_tasks); | 
|---|
| 2615 | if (list_empty(head: &cset->mg_node)) | 
|---|
| 2616 | list_add_tail(new: &cset->mg_node, | 
|---|
| 2617 | head: &mgctx->tset.src_csets); | 
|---|
| 2618 | if (list_empty(head: &cset->mg_dst_cset->mg_node)) | 
|---|
| 2619 | list_add_tail(new: &cset->mg_dst_cset->mg_node, | 
|---|
| 2620 | head: &mgctx->tset.dst_csets); | 
|---|
| 2621 | } | 
|---|
| 2622 |  | 
|---|
| 2623 | /** | 
|---|
| 2624 | * cgroup_taskset_first - reset taskset and return the first task | 
|---|
| 2625 | * @tset: taskset of interest | 
|---|
| 2626 | * @dst_cssp: output variable for the destination css | 
|---|
| 2627 | * | 
|---|
| 2628 | * @tset iteration is initialized and the first task is returned. | 
|---|
| 2629 | */ | 
|---|
| 2630 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset, | 
|---|
| 2631 | struct cgroup_subsys_state **dst_cssp) | 
|---|
| 2632 | { | 
|---|
| 2633 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); | 
|---|
| 2634 | tset->cur_task = NULL; | 
|---|
| 2635 |  | 
|---|
| 2636 | return cgroup_taskset_next(tset, dst_cssp); | 
|---|
| 2637 | } | 
|---|
| 2638 |  | 
|---|
| 2639 | /** | 
|---|
| 2640 | * cgroup_taskset_next - iterate to the next task in taskset | 
|---|
| 2641 | * @tset: taskset of interest | 
|---|
| 2642 | * @dst_cssp: output variable for the destination css | 
|---|
| 2643 | * | 
|---|
| 2644 | * Return the next task in @tset.  Iteration must have been initialized | 
|---|
| 2645 | * with cgroup_taskset_first(). | 
|---|
| 2646 | */ | 
|---|
| 2647 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset, | 
|---|
| 2648 | struct cgroup_subsys_state **dst_cssp) | 
|---|
| 2649 | { | 
|---|
| 2650 | struct css_set *cset = tset->cur_cset; | 
|---|
| 2651 | struct task_struct *task = tset->cur_task; | 
|---|
| 2652 |  | 
|---|
| 2653 | while (CGROUP_HAS_SUBSYS_CONFIG && &cset->mg_node != tset->csets) { | 
|---|
| 2654 | if (!task) | 
|---|
| 2655 | task = list_first_entry(&cset->mg_tasks, | 
|---|
| 2656 | struct task_struct, cg_list); | 
|---|
| 2657 | else | 
|---|
| 2658 | task = list_next_entry(task, cg_list); | 
|---|
| 2659 |  | 
|---|
| 2660 | if (&task->cg_list != &cset->mg_tasks) { | 
|---|
| 2661 | tset->cur_cset = cset; | 
|---|
| 2662 | tset->cur_task = task; | 
|---|
| 2663 |  | 
|---|
| 2664 | /* | 
|---|
| 2665 | * This function may be called both before and | 
|---|
| 2666 | * after cgroup_migrate_execute().  The two cases | 
|---|
| 2667 | * can be distinguished by looking at whether @cset | 
|---|
| 2668 | * has its ->mg_dst_cset set. | 
|---|
| 2669 | */ | 
|---|
| 2670 | if (cset->mg_dst_cset) | 
|---|
| 2671 | *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid]; | 
|---|
| 2672 | else | 
|---|
| 2673 | *dst_cssp = cset->subsys[tset->ssid]; | 
|---|
| 2674 |  | 
|---|
| 2675 | return task; | 
|---|
| 2676 | } | 
|---|
| 2677 |  | 
|---|
| 2678 | cset = list_next_entry(cset, mg_node); | 
|---|
| 2679 | task = NULL; | 
|---|
| 2680 | } | 
|---|
| 2681 |  | 
|---|
| 2682 | return NULL; | 
|---|
| 2683 | } | 
|---|
| 2684 |  | 
|---|
| 2685 | /** | 
|---|
| 2686 | * cgroup_migrate_execute - migrate a taskset | 
|---|
| 2687 | * @mgctx: migration context | 
|---|
| 2688 | * | 
|---|
| 2689 | * Migrate tasks in @mgctx as setup by migration preparation functions. | 
|---|
| 2690 | * This function fails iff one of the ->can_attach callbacks fails and | 
|---|
| 2691 | * guarantees that either all or none of the tasks in @mgctx are migrated. | 
|---|
| 2692 | * @mgctx is consumed regardless of success. | 
|---|
| 2693 | */ | 
|---|
| 2694 | static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx) | 
|---|
| 2695 | { | 
|---|
| 2696 | struct cgroup_taskset *tset = &mgctx->tset; | 
|---|
| 2697 | struct cgroup_subsys *ss; | 
|---|
| 2698 | struct task_struct *task, *tmp_task; | 
|---|
| 2699 | struct css_set *cset, *tmp_cset; | 
|---|
| 2700 | int ssid, failed_ssid, ret; | 
|---|
| 2701 |  | 
|---|
| 2702 | /* check that we can legitimately attach to the cgroup */ | 
|---|
| 2703 | if (tset->nr_tasks) { | 
|---|
| 2704 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { | 
|---|
| 2705 | if (ss->can_attach) { | 
|---|
| 2706 | tset->ssid = ssid; | 
|---|
| 2707 | ret = ss->can_attach(tset); | 
|---|
| 2708 | if (ret) { | 
|---|
| 2709 | failed_ssid = ssid; | 
|---|
| 2710 | goto out_cancel_attach; | 
|---|
| 2711 | } | 
|---|
| 2712 | } | 
|---|
| 2713 | } while_each_subsys_mask(); | 
|---|
| 2714 | } | 
|---|
| 2715 |  | 
|---|
| 2716 | /* | 
|---|
| 2717 | * Now that we're guaranteed success, proceed to move all tasks to | 
|---|
| 2718 | * the new cgroup.  There are no failure cases after here, so this | 
|---|
| 2719 | * is the commit point. | 
|---|
| 2720 | */ | 
|---|
| 2721 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 2722 | list_for_each_entry(cset, &tset->src_csets, mg_node) { | 
|---|
| 2723 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) { | 
|---|
| 2724 | struct css_set *from_cset = task_css_set(task); | 
|---|
| 2725 | struct css_set *to_cset = cset->mg_dst_cset; | 
|---|
| 2726 |  | 
|---|
| 2727 | get_css_set(cset: to_cset); | 
|---|
| 2728 | to_cset->nr_tasks++; | 
|---|
| 2729 | css_set_move_task(task, from_cset, to_cset, use_mg_tasks: true); | 
|---|
| 2730 | from_cset->nr_tasks--; | 
|---|
| 2731 | /* | 
|---|
| 2732 | * If the source or destination cgroup is frozen, | 
|---|
| 2733 | * the task might require to change its state. | 
|---|
| 2734 | */ | 
|---|
| 2735 | cgroup_freezer_migrate_task(task, src: from_cset->dfl_cgrp, | 
|---|
| 2736 | dst: to_cset->dfl_cgrp); | 
|---|
| 2737 | put_css_set_locked(cset: from_cset); | 
|---|
| 2738 |  | 
|---|
| 2739 | } | 
|---|
| 2740 | } | 
|---|
| 2741 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 2742 |  | 
|---|
| 2743 | /* | 
|---|
| 2744 | * Migration is committed, all target tasks are now on dst_csets. | 
|---|
| 2745 | * Nothing is sensitive to fork() after this point.  Notify | 
|---|
| 2746 | * controllers that migration is complete. | 
|---|
| 2747 | */ | 
|---|
| 2748 | tset->csets = &tset->dst_csets; | 
|---|
| 2749 |  | 
|---|
| 2750 | if (tset->nr_tasks) { | 
|---|
| 2751 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { | 
|---|
| 2752 | if (ss->attach) { | 
|---|
| 2753 | tset->ssid = ssid; | 
|---|
| 2754 | ss->attach(tset); | 
|---|
| 2755 | } | 
|---|
| 2756 | } while_each_subsys_mask(); | 
|---|
| 2757 | } | 
|---|
| 2758 |  | 
|---|
| 2759 | ret = 0; | 
|---|
| 2760 | goto out_release_tset; | 
|---|
| 2761 |  | 
|---|
| 2762 | out_cancel_attach: | 
|---|
| 2763 | if (tset->nr_tasks) { | 
|---|
| 2764 | do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { | 
|---|
| 2765 | if (ssid == failed_ssid) | 
|---|
| 2766 | break; | 
|---|
| 2767 | if (ss->cancel_attach) { | 
|---|
| 2768 | tset->ssid = ssid; | 
|---|
| 2769 | ss->cancel_attach(tset); | 
|---|
| 2770 | } | 
|---|
| 2771 | } while_each_subsys_mask(); | 
|---|
| 2772 | } | 
|---|
| 2773 | out_release_tset: | 
|---|
| 2774 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 2775 | list_splice_init(list: &tset->dst_csets, head: &tset->src_csets); | 
|---|
| 2776 | list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) { | 
|---|
| 2777 | list_splice_tail_init(list: &cset->mg_tasks, head: &cset->tasks); | 
|---|
| 2778 | list_del_init(entry: &cset->mg_node); | 
|---|
| 2779 | } | 
|---|
| 2780 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 2781 |  | 
|---|
| 2782 | /* | 
|---|
| 2783 | * Re-initialize the cgroup_taskset structure in case it is reused | 
|---|
| 2784 | * again in another cgroup_migrate_add_task()/cgroup_migrate_execute() | 
|---|
| 2785 | * iteration. | 
|---|
| 2786 | */ | 
|---|
| 2787 | tset->nr_tasks = 0; | 
|---|
| 2788 | tset->csets    = &tset->src_csets; | 
|---|
| 2789 | return ret; | 
|---|
| 2790 | } | 
|---|
| 2791 |  | 
|---|
| 2792 | /** | 
|---|
| 2793 | * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination | 
|---|
| 2794 | * @dst_cgrp: destination cgroup to test | 
|---|
| 2795 | * | 
|---|
| 2796 | * On the default hierarchy, except for the mixable, (possible) thread root | 
|---|
| 2797 | * and threaded cgroups, subtree_control must be zero for migration | 
|---|
| 2798 | * destination cgroups with tasks so that child cgroups don't compete | 
|---|
| 2799 | * against tasks. | 
|---|
| 2800 | */ | 
|---|
| 2801 | int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp) | 
|---|
| 2802 | { | 
|---|
| 2803 | /* v1 doesn't have any restriction */ | 
|---|
| 2804 | if (!cgroup_on_dfl(cgrp: dst_cgrp)) | 
|---|
| 2805 | return 0; | 
|---|
| 2806 |  | 
|---|
| 2807 | /* verify @dst_cgrp can host resources */ | 
|---|
| 2808 | if (!cgroup_is_valid_domain(cgrp: dst_cgrp->dom_cgrp)) | 
|---|
| 2809 | return -EOPNOTSUPP; | 
|---|
| 2810 |  | 
|---|
| 2811 | /* | 
|---|
| 2812 | * If @dst_cgrp is already or can become a thread root or is | 
|---|
| 2813 | * threaded, it doesn't matter. | 
|---|
| 2814 | */ | 
|---|
| 2815 | if (cgroup_can_be_thread_root(cgrp: dst_cgrp) || cgroup_is_threaded(cgrp: dst_cgrp)) | 
|---|
| 2816 | return 0; | 
|---|
| 2817 |  | 
|---|
| 2818 | /* apply no-internal-process constraint */ | 
|---|
| 2819 | if (dst_cgrp->subtree_control) | 
|---|
| 2820 | return -EBUSY; | 
|---|
| 2821 |  | 
|---|
| 2822 | return 0; | 
|---|
| 2823 | } | 
|---|
| 2824 |  | 
|---|
| 2825 | /** | 
|---|
| 2826 | * cgroup_migrate_finish - cleanup after attach | 
|---|
| 2827 | * @mgctx: migration context | 
|---|
| 2828 | * | 
|---|
| 2829 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst().  See | 
|---|
| 2830 | * those functions for details. | 
|---|
| 2831 | */ | 
|---|
| 2832 | void cgroup_migrate_finish(struct cgroup_mgctx *mgctx) | 
|---|
| 2833 | { | 
|---|
| 2834 | struct css_set *cset, *tmp_cset; | 
|---|
| 2835 |  | 
|---|
| 2836 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 2837 |  | 
|---|
| 2838 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 2839 |  | 
|---|
| 2840 | list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_src_csets, | 
|---|
| 2841 | mg_src_preload_node) { | 
|---|
| 2842 | cset->mg_src_cgrp = NULL; | 
|---|
| 2843 | cset->mg_dst_cgrp = NULL; | 
|---|
| 2844 | cset->mg_dst_cset = NULL; | 
|---|
| 2845 | list_del_init(entry: &cset->mg_src_preload_node); | 
|---|
| 2846 | put_css_set_locked(cset); | 
|---|
| 2847 | } | 
|---|
| 2848 |  | 
|---|
| 2849 | list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_dst_csets, | 
|---|
| 2850 | mg_dst_preload_node) { | 
|---|
| 2851 | cset->mg_src_cgrp = NULL; | 
|---|
| 2852 | cset->mg_dst_cgrp = NULL; | 
|---|
| 2853 | cset->mg_dst_cset = NULL; | 
|---|
| 2854 | list_del_init(entry: &cset->mg_dst_preload_node); | 
|---|
| 2855 | put_css_set_locked(cset); | 
|---|
| 2856 | } | 
|---|
| 2857 |  | 
|---|
| 2858 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 2859 | } | 
|---|
| 2860 |  | 
|---|
| 2861 | /** | 
|---|
| 2862 | * cgroup_migrate_add_src - add a migration source css_set | 
|---|
| 2863 | * @src_cset: the source css_set to add | 
|---|
| 2864 | * @dst_cgrp: the destination cgroup | 
|---|
| 2865 | * @mgctx: migration context | 
|---|
| 2866 | * | 
|---|
| 2867 | * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp.  Pin | 
|---|
| 2868 | * @src_cset and add it to @mgctx->src_csets, which should later be cleaned | 
|---|
| 2869 | * up by cgroup_migrate_finish(). | 
|---|
| 2870 | * | 
|---|
| 2871 | * This function may be called without holding cgroup_threadgroup_rwsem | 
|---|
| 2872 | * even if the target is a process.  Threads may be created and destroyed | 
|---|
| 2873 | * but as long as cgroup_mutex is not dropped, no new css_set can be put | 
|---|
| 2874 | * into play and the preloaded css_sets are guaranteed to cover all | 
|---|
| 2875 | * migrations. | 
|---|
| 2876 | */ | 
|---|
| 2877 | void cgroup_migrate_add_src(struct css_set *src_cset, | 
|---|
| 2878 | struct cgroup *dst_cgrp, | 
|---|
| 2879 | struct cgroup_mgctx *mgctx) | 
|---|
| 2880 | { | 
|---|
| 2881 | struct cgroup *src_cgrp; | 
|---|
| 2882 |  | 
|---|
| 2883 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 2884 | lockdep_assert_held(&css_set_lock); | 
|---|
| 2885 |  | 
|---|
| 2886 | /* | 
|---|
| 2887 | * If ->dead, @src_set is associated with one or more dead cgroups | 
|---|
| 2888 | * and doesn't contain any migratable tasks.  Ignore it early so | 
|---|
| 2889 | * that the rest of migration path doesn't get confused by it. | 
|---|
| 2890 | */ | 
|---|
| 2891 | if (src_cset->dead) | 
|---|
| 2892 | return; | 
|---|
| 2893 |  | 
|---|
| 2894 | if (!list_empty(head: &src_cset->mg_src_preload_node)) | 
|---|
| 2895 | return; | 
|---|
| 2896 |  | 
|---|
| 2897 | src_cgrp = cset_cgroup_from_root(cset: src_cset, root: dst_cgrp->root); | 
|---|
| 2898 |  | 
|---|
| 2899 | WARN_ON(src_cset->mg_src_cgrp); | 
|---|
| 2900 | WARN_ON(src_cset->mg_dst_cgrp); | 
|---|
| 2901 | WARN_ON(!list_empty(&src_cset->mg_tasks)); | 
|---|
| 2902 | WARN_ON(!list_empty(&src_cset->mg_node)); | 
|---|
| 2903 |  | 
|---|
| 2904 | src_cset->mg_src_cgrp = src_cgrp; | 
|---|
| 2905 | src_cset->mg_dst_cgrp = dst_cgrp; | 
|---|
| 2906 | get_css_set(cset: src_cset); | 
|---|
| 2907 | list_add_tail(new: &src_cset->mg_src_preload_node, head: &mgctx->preloaded_src_csets); | 
|---|
| 2908 | } | 
|---|
| 2909 |  | 
|---|
| 2910 | /** | 
|---|
| 2911 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration | 
|---|
| 2912 | * @mgctx: migration context | 
|---|
| 2913 | * | 
|---|
| 2914 | * Tasks are about to be moved and all the source css_sets have been | 
|---|
| 2915 | * preloaded to @mgctx->preloaded_src_csets.  This function looks up and | 
|---|
| 2916 | * pins all destination css_sets, links each to its source, and append them | 
|---|
| 2917 | * to @mgctx->preloaded_dst_csets. | 
|---|
| 2918 | * | 
|---|
| 2919 | * This function must be called after cgroup_migrate_add_src() has been | 
|---|
| 2920 | * called on each migration source css_set.  After migration is performed | 
|---|
| 2921 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on | 
|---|
| 2922 | * @mgctx. | 
|---|
| 2923 | */ | 
|---|
| 2924 | int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx) | 
|---|
| 2925 | { | 
|---|
| 2926 | struct css_set *src_cset, *tmp_cset; | 
|---|
| 2927 |  | 
|---|
| 2928 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 2929 |  | 
|---|
| 2930 | /* look up the dst cset for each src cset and link it to src */ | 
|---|
| 2931 | list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets, | 
|---|
| 2932 | mg_src_preload_node) { | 
|---|
| 2933 | struct css_set *dst_cset; | 
|---|
| 2934 | struct cgroup_subsys *ss; | 
|---|
| 2935 | int ssid; | 
|---|
| 2936 |  | 
|---|
| 2937 | dst_cset = find_css_set(old_cset: src_cset, cgrp: src_cset->mg_dst_cgrp); | 
|---|
| 2938 | if (!dst_cset) | 
|---|
| 2939 | return -ENOMEM; | 
|---|
| 2940 |  | 
|---|
| 2941 | WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset); | 
|---|
| 2942 |  | 
|---|
| 2943 | /* | 
|---|
| 2944 | * If src cset equals dst, it's noop.  Drop the src. | 
|---|
| 2945 | * cgroup_migrate() will skip the cset too.  Note that we | 
|---|
| 2946 | * can't handle src == dst as some nodes are used by both. | 
|---|
| 2947 | */ | 
|---|
| 2948 | if (src_cset == dst_cset) { | 
|---|
| 2949 | src_cset->mg_src_cgrp = NULL; | 
|---|
| 2950 | src_cset->mg_dst_cgrp = NULL; | 
|---|
| 2951 | list_del_init(entry: &src_cset->mg_src_preload_node); | 
|---|
| 2952 | put_css_set(cset: src_cset); | 
|---|
| 2953 | put_css_set(cset: dst_cset); | 
|---|
| 2954 | continue; | 
|---|
| 2955 | } | 
|---|
| 2956 |  | 
|---|
| 2957 | src_cset->mg_dst_cset = dst_cset; | 
|---|
| 2958 |  | 
|---|
| 2959 | if (list_empty(head: &dst_cset->mg_dst_preload_node)) | 
|---|
| 2960 | list_add_tail(new: &dst_cset->mg_dst_preload_node, | 
|---|
| 2961 | head: &mgctx->preloaded_dst_csets); | 
|---|
| 2962 | else | 
|---|
| 2963 | put_css_set(cset: dst_cset); | 
|---|
| 2964 |  | 
|---|
| 2965 | for_each_subsys(ss, ssid) | 
|---|
| 2966 | if (src_cset->subsys[ssid] != dst_cset->subsys[ssid]) | 
|---|
| 2967 | mgctx->ss_mask |= 1 << ssid; | 
|---|
| 2968 | } | 
|---|
| 2969 |  | 
|---|
| 2970 | return 0; | 
|---|
| 2971 | } | 
|---|
| 2972 |  | 
|---|
| 2973 | /** | 
|---|
| 2974 | * cgroup_migrate - migrate a process or task to a cgroup | 
|---|
| 2975 | * @leader: the leader of the process or the task to migrate | 
|---|
| 2976 | * @threadgroup: whether @leader points to the whole process or a single task | 
|---|
| 2977 | * @mgctx: migration context | 
|---|
| 2978 | * | 
|---|
| 2979 | * Migrate a process or task denoted by @leader.  If migrating a process, | 
|---|
| 2980 | * the caller must be holding cgroup_threadgroup_rwsem.  The caller is also | 
|---|
| 2981 | * responsible for invoking cgroup_migrate_add_src() and | 
|---|
| 2982 | * cgroup_migrate_prepare_dst() on the targets before invoking this | 
|---|
| 2983 | * function and following up with cgroup_migrate_finish(). | 
|---|
| 2984 | * | 
|---|
| 2985 | * As long as a controller's ->can_attach() doesn't fail, this function is | 
|---|
| 2986 | * guaranteed to succeed.  This means that, excluding ->can_attach() | 
|---|
| 2987 | * failure, when migrating multiple targets, the success or failure can be | 
|---|
| 2988 | * decided for all targets by invoking group_migrate_prepare_dst() before | 
|---|
| 2989 | * actually starting migrating. | 
|---|
| 2990 | */ | 
|---|
| 2991 | int cgroup_migrate(struct task_struct *leader, bool threadgroup, | 
|---|
| 2992 | struct cgroup_mgctx *mgctx) | 
|---|
| 2993 | { | 
|---|
| 2994 | struct task_struct *task; | 
|---|
| 2995 |  | 
|---|
| 2996 | /* | 
|---|
| 2997 | * The following thread iteration should be inside an RCU critical | 
|---|
| 2998 | * section to prevent tasks from being freed while taking the snapshot. | 
|---|
| 2999 | * spin_lock_irq() implies RCU critical section here. | 
|---|
| 3000 | */ | 
|---|
| 3001 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 3002 | task = leader; | 
|---|
| 3003 | do { | 
|---|
| 3004 | cgroup_migrate_add_task(task, mgctx); | 
|---|
| 3005 | if (!threadgroup) | 
|---|
| 3006 | break; | 
|---|
| 3007 | } while_each_thread(leader, task); | 
|---|
| 3008 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 3009 |  | 
|---|
| 3010 | return cgroup_migrate_execute(mgctx); | 
|---|
| 3011 | } | 
|---|
| 3012 |  | 
|---|
| 3013 | /** | 
|---|
| 3014 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup | 
|---|
| 3015 | * @dst_cgrp: the cgroup to attach to | 
|---|
| 3016 | * @leader: the task or the leader of the threadgroup to be attached | 
|---|
| 3017 | * @threadgroup: attach the whole threadgroup? | 
|---|
| 3018 | * | 
|---|
| 3019 | * Call holding cgroup_mutex and cgroup_threadgroup_rwsem. | 
|---|
| 3020 | */ | 
|---|
| 3021 | int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, | 
|---|
| 3022 | bool threadgroup) | 
|---|
| 3023 | { | 
|---|
| 3024 | DEFINE_CGROUP_MGCTX(mgctx); | 
|---|
| 3025 | struct task_struct *task; | 
|---|
| 3026 | int ret = 0; | 
|---|
| 3027 |  | 
|---|
| 3028 | /* look up all src csets */ | 
|---|
| 3029 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 3030 | task = leader; | 
|---|
| 3031 | do { | 
|---|
| 3032 | cgroup_migrate_add_src(src_cset: task_css_set(task), dst_cgrp, mgctx: &mgctx); | 
|---|
| 3033 | if (!threadgroup) | 
|---|
| 3034 | break; | 
|---|
| 3035 | } while_each_thread(leader, task); | 
|---|
| 3036 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 3037 |  | 
|---|
| 3038 | /* prepare dst csets and commit */ | 
|---|
| 3039 | ret = cgroup_migrate_prepare_dst(mgctx: &mgctx); | 
|---|
| 3040 | if (!ret) | 
|---|
| 3041 | ret = cgroup_migrate(leader, threadgroup, mgctx: &mgctx); | 
|---|
| 3042 |  | 
|---|
| 3043 | cgroup_migrate_finish(mgctx: &mgctx); | 
|---|
| 3044 |  | 
|---|
| 3045 | if (!ret) | 
|---|
| 3046 | TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup); | 
|---|
| 3047 |  | 
|---|
| 3048 | return ret; | 
|---|
| 3049 | } | 
|---|
| 3050 |  | 
|---|
| 3051 | struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, | 
|---|
| 3052 | enum cgroup_attach_lock_mode *lock_mode) | 
|---|
| 3053 | { | 
|---|
| 3054 | struct task_struct *tsk; | 
|---|
| 3055 | pid_t pid; | 
|---|
| 3056 |  | 
|---|
| 3057 | if (kstrtoint(s: strstrip(str: buf), base: 0, res: &pid) || pid < 0) | 
|---|
| 3058 | return ERR_PTR(error: -EINVAL); | 
|---|
| 3059 |  | 
|---|
| 3060 | retry_find_task: | 
|---|
| 3061 | rcu_read_lock(); | 
|---|
| 3062 | if (pid) { | 
|---|
| 3063 | tsk = find_task_by_vpid(nr: pid); | 
|---|
| 3064 | if (!tsk) { | 
|---|
| 3065 | tsk = ERR_PTR(error: -ESRCH); | 
|---|
| 3066 | goto out_unlock_rcu; | 
|---|
| 3067 | } | 
|---|
| 3068 | } else { | 
|---|
| 3069 | tsk = current; | 
|---|
| 3070 | } | 
|---|
| 3071 |  | 
|---|
| 3072 | if (threadgroup) | 
|---|
| 3073 | tsk = tsk->group_leader; | 
|---|
| 3074 |  | 
|---|
| 3075 | /* | 
|---|
| 3076 | * kthreads may acquire PF_NO_SETAFFINITY during initialization. | 
|---|
| 3077 | * If userland migrates such a kthread to a non-root cgroup, it can | 
|---|
| 3078 | * become trapped in a cpuset, or RT kthread may be born in a | 
|---|
| 3079 | * cgroup with no rt_runtime allocated.  Just say no. | 
|---|
| 3080 | */ | 
|---|
| 3081 | if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) { | 
|---|
| 3082 | tsk = ERR_PTR(error: -EINVAL); | 
|---|
| 3083 | goto out_unlock_rcu; | 
|---|
| 3084 | } | 
|---|
| 3085 | get_task_struct(t: tsk); | 
|---|
| 3086 | rcu_read_unlock(); | 
|---|
| 3087 |  | 
|---|
| 3088 | /* | 
|---|
| 3089 | * If we migrate a single thread, we don't care about threadgroup | 
|---|
| 3090 | * stability. If the thread is `current`, it won't exit(2) under our | 
|---|
| 3091 | * hands or change PID through exec(2). We exclude | 
|---|
| 3092 | * cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write callers | 
|---|
| 3093 | * by cgroup_mutex. Therefore, we can skip the global lock. | 
|---|
| 3094 | */ | 
|---|
| 3095 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 3096 |  | 
|---|
| 3097 | if (pid || threadgroup) { | 
|---|
| 3098 | if (cgroup_enable_per_threadgroup_rwsem) | 
|---|
| 3099 | *lock_mode = CGRP_ATTACH_LOCK_PER_THREADGROUP; | 
|---|
| 3100 | else | 
|---|
| 3101 | *lock_mode = CGRP_ATTACH_LOCK_GLOBAL; | 
|---|
| 3102 | } else { | 
|---|
| 3103 | *lock_mode = CGRP_ATTACH_LOCK_NONE; | 
|---|
| 3104 | } | 
|---|
| 3105 |  | 
|---|
| 3106 | cgroup_attach_lock(lock_mode: *lock_mode, tsk); | 
|---|
| 3107 |  | 
|---|
| 3108 | if (threadgroup) { | 
|---|
| 3109 | if (!thread_group_leader(p: tsk)) { | 
|---|
| 3110 | /* | 
|---|
| 3111 | * A race with de_thread from another thread's exec() | 
|---|
| 3112 | * may strip us of our leadership. If this happens, | 
|---|
| 3113 | * throw this task away and try again. | 
|---|
| 3114 | */ | 
|---|
| 3115 | cgroup_attach_unlock(lock_mode: *lock_mode, tsk); | 
|---|
| 3116 | put_task_struct(t: tsk); | 
|---|
| 3117 | goto retry_find_task; | 
|---|
| 3118 | } | 
|---|
| 3119 | } | 
|---|
| 3120 |  | 
|---|
| 3121 | return tsk; | 
|---|
| 3122 |  | 
|---|
| 3123 | out_unlock_rcu: | 
|---|
| 3124 | rcu_read_unlock(); | 
|---|
| 3125 | return tsk; | 
|---|
| 3126 | } | 
|---|
| 3127 |  | 
|---|
| 3128 | void cgroup_procs_write_finish(struct task_struct *task, | 
|---|
| 3129 | enum cgroup_attach_lock_mode lock_mode) | 
|---|
| 3130 | { | 
|---|
| 3131 | cgroup_attach_unlock(lock_mode, tsk: task); | 
|---|
| 3132 |  | 
|---|
| 3133 | /* release reference from cgroup_procs_write_start() */ | 
|---|
| 3134 | put_task_struct(t: task); | 
|---|
| 3135 | } | 
|---|
| 3136 |  | 
|---|
| 3137 | static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask) | 
|---|
| 3138 | { | 
|---|
| 3139 | struct cgroup_subsys *ss; | 
|---|
| 3140 | bool printed = false; | 
|---|
| 3141 | int ssid; | 
|---|
| 3142 |  | 
|---|
| 3143 | do_each_subsys_mask(ss, ssid, ss_mask) { | 
|---|
| 3144 | if (printed) | 
|---|
| 3145 | seq_putc(m: seq, c: ' '); | 
|---|
| 3146 | seq_puts(m: seq, s: ss->name); | 
|---|
| 3147 | printed = true; | 
|---|
| 3148 | } while_each_subsys_mask(); | 
|---|
| 3149 | if (printed) | 
|---|
| 3150 | seq_putc(m: seq, c: '\n'); | 
|---|
| 3151 | } | 
|---|
| 3152 |  | 
|---|
| 3153 | /* show controllers which are enabled from the parent */ | 
|---|
| 3154 | static int cgroup_controllers_show(struct seq_file *seq, void *v) | 
|---|
| 3155 | { | 
|---|
| 3156 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3157 |  | 
|---|
| 3158 | cgroup_print_ss_mask(seq, ss_mask: cgroup_control(cgrp)); | 
|---|
| 3159 | return 0; | 
|---|
| 3160 | } | 
|---|
| 3161 |  | 
|---|
| 3162 | /* show controllers which are enabled for a given cgroup's children */ | 
|---|
| 3163 | static int cgroup_subtree_control_show(struct seq_file *seq, void *v) | 
|---|
| 3164 | { | 
|---|
| 3165 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3166 |  | 
|---|
| 3167 | cgroup_print_ss_mask(seq, ss_mask: cgrp->subtree_control); | 
|---|
| 3168 | return 0; | 
|---|
| 3169 | } | 
|---|
| 3170 |  | 
|---|
| 3171 | /** | 
|---|
| 3172 | * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy | 
|---|
| 3173 | * @cgrp: root of the subtree to update csses for | 
|---|
| 3174 | * | 
|---|
| 3175 | * @cgrp's control masks have changed and its subtree's css associations | 
|---|
| 3176 | * need to be updated accordingly.  This function looks up all css_sets | 
|---|
| 3177 | * which are attached to the subtree, creates the matching updated css_sets | 
|---|
| 3178 | * and migrates the tasks to the new ones. | 
|---|
| 3179 | */ | 
|---|
| 3180 | static int cgroup_update_dfl_csses(struct cgroup *cgrp) | 
|---|
| 3181 | { | 
|---|
| 3182 | DEFINE_CGROUP_MGCTX(mgctx); | 
|---|
| 3183 | struct cgroup_subsys_state *d_css; | 
|---|
| 3184 | struct cgroup *dsct; | 
|---|
| 3185 | struct css_set *src_cset; | 
|---|
| 3186 | enum cgroup_attach_lock_mode lock_mode; | 
|---|
| 3187 | bool has_tasks; | 
|---|
| 3188 | int ret; | 
|---|
| 3189 |  | 
|---|
| 3190 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 3191 |  | 
|---|
| 3192 | /* look up all csses currently attached to @cgrp's subtree */ | 
|---|
| 3193 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 3194 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { | 
|---|
| 3195 | struct cgrp_cset_link *link; | 
|---|
| 3196 |  | 
|---|
| 3197 | /* | 
|---|
| 3198 | * As cgroup_update_dfl_csses() is only called by | 
|---|
| 3199 | * cgroup_apply_control(). The csses associated with the | 
|---|
| 3200 | * given cgrp will not be affected by changes made to | 
|---|
| 3201 | * its subtree_control file. We can skip them. | 
|---|
| 3202 | */ | 
|---|
| 3203 | if (dsct == cgrp) | 
|---|
| 3204 | continue; | 
|---|
| 3205 |  | 
|---|
| 3206 | list_for_each_entry(link, &dsct->cset_links, cset_link) | 
|---|
| 3207 | cgroup_migrate_add_src(src_cset: link->cset, dst_cgrp: dsct, mgctx: &mgctx); | 
|---|
| 3208 | } | 
|---|
| 3209 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 3210 |  | 
|---|
| 3211 | /* | 
|---|
| 3212 | * We need to write-lock threadgroup_rwsem while migrating tasks. | 
|---|
| 3213 | * However, if there are no source csets for @cgrp, changing its | 
|---|
| 3214 | * controllers isn't gonna produce any task migrations and the | 
|---|
| 3215 | * write-locking can be skipped safely. | 
|---|
| 3216 | */ | 
|---|
| 3217 | has_tasks = !list_empty(head: &mgctx.preloaded_src_csets); | 
|---|
| 3218 |  | 
|---|
| 3219 | if (has_tasks) | 
|---|
| 3220 | lock_mode = CGRP_ATTACH_LOCK_GLOBAL; | 
|---|
| 3221 | else | 
|---|
| 3222 | lock_mode = CGRP_ATTACH_LOCK_NONE; | 
|---|
| 3223 |  | 
|---|
| 3224 | cgroup_attach_lock(lock_mode, NULL); | 
|---|
| 3225 |  | 
|---|
| 3226 | /* NULL dst indicates self on default hierarchy */ | 
|---|
| 3227 | ret = cgroup_migrate_prepare_dst(mgctx: &mgctx); | 
|---|
| 3228 | if (ret) | 
|---|
| 3229 | goto out_finish; | 
|---|
| 3230 |  | 
|---|
| 3231 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 3232 | list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, | 
|---|
| 3233 | mg_src_preload_node) { | 
|---|
| 3234 | struct task_struct *task, *ntask; | 
|---|
| 3235 |  | 
|---|
| 3236 | /* all tasks in src_csets need to be migrated */ | 
|---|
| 3237 | list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list) | 
|---|
| 3238 | cgroup_migrate_add_task(task, mgctx: &mgctx); | 
|---|
| 3239 | } | 
|---|
| 3240 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 3241 |  | 
|---|
| 3242 | ret = cgroup_migrate_execute(mgctx: &mgctx); | 
|---|
| 3243 | out_finish: | 
|---|
| 3244 | cgroup_migrate_finish(mgctx: &mgctx); | 
|---|
| 3245 | cgroup_attach_unlock(lock_mode, NULL); | 
|---|
| 3246 | return ret; | 
|---|
| 3247 | } | 
|---|
| 3248 |  | 
|---|
| 3249 | /** | 
|---|
| 3250 | * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses | 
|---|
| 3251 | * @cgrp: root of the target subtree | 
|---|
| 3252 | * | 
|---|
| 3253 | * Because css offlining is asynchronous, userland may try to re-enable a | 
|---|
| 3254 | * controller while the previous css is still around.  This function grabs | 
|---|
| 3255 | * cgroup_mutex and drains the previous css instances of @cgrp's subtree. | 
|---|
| 3256 | */ | 
|---|
| 3257 | void cgroup_lock_and_drain_offline(struct cgroup *cgrp) | 
|---|
| 3258 | __acquires(&cgroup_mutex) | 
|---|
| 3259 | { | 
|---|
| 3260 | struct cgroup *dsct; | 
|---|
| 3261 | struct cgroup_subsys_state *d_css; | 
|---|
| 3262 | struct cgroup_subsys *ss; | 
|---|
| 3263 | int ssid; | 
|---|
| 3264 |  | 
|---|
| 3265 | restart: | 
|---|
| 3266 | cgroup_lock(); | 
|---|
| 3267 |  | 
|---|
| 3268 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { | 
|---|
| 3269 | for_each_subsys(ss, ssid) { | 
|---|
| 3270 | struct cgroup_subsys_state *css = cgroup_css(cgrp: dsct, ss); | 
|---|
| 3271 | DEFINE_WAIT(wait); | 
|---|
| 3272 |  | 
|---|
| 3273 | if (!css || !percpu_ref_is_dying(ref: &css->refcnt)) | 
|---|
| 3274 | continue; | 
|---|
| 3275 |  | 
|---|
| 3276 | cgroup_get_live(cgrp: dsct); | 
|---|
| 3277 | prepare_to_wait(wq_head: &dsct->offline_waitq, wq_entry: &wait, | 
|---|
| 3278 | TASK_UNINTERRUPTIBLE); | 
|---|
| 3279 |  | 
|---|
| 3280 | cgroup_unlock(); | 
|---|
| 3281 | schedule(); | 
|---|
| 3282 | finish_wait(wq_head: &dsct->offline_waitq, wq_entry: &wait); | 
|---|
| 3283 |  | 
|---|
| 3284 | cgroup_put(cgrp: dsct); | 
|---|
| 3285 | goto restart; | 
|---|
| 3286 | } | 
|---|
| 3287 | } | 
|---|
| 3288 | } | 
|---|
| 3289 |  | 
|---|
| 3290 | /** | 
|---|
| 3291 | * cgroup_save_control - save control masks and dom_cgrp of a subtree | 
|---|
| 3292 | * @cgrp: root of the target subtree | 
|---|
| 3293 | * | 
|---|
| 3294 | * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the | 
|---|
| 3295 | * respective old_ prefixed fields for @cgrp's subtree including @cgrp | 
|---|
| 3296 | * itself. | 
|---|
| 3297 | */ | 
|---|
| 3298 | static void cgroup_save_control(struct cgroup *cgrp) | 
|---|
| 3299 | { | 
|---|
| 3300 | struct cgroup *dsct; | 
|---|
| 3301 | struct cgroup_subsys_state *d_css; | 
|---|
| 3302 |  | 
|---|
| 3303 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { | 
|---|
| 3304 | dsct->old_subtree_control = dsct->subtree_control; | 
|---|
| 3305 | dsct->old_subtree_ss_mask = dsct->subtree_ss_mask; | 
|---|
| 3306 | dsct->old_dom_cgrp = dsct->dom_cgrp; | 
|---|
| 3307 | } | 
|---|
| 3308 | } | 
|---|
| 3309 |  | 
|---|
| 3310 | /** | 
|---|
| 3311 | * cgroup_propagate_control - refresh control masks of a subtree | 
|---|
| 3312 | * @cgrp: root of the target subtree | 
|---|
| 3313 | * | 
|---|
| 3314 | * For @cgrp and its subtree, ensure ->subtree_ss_mask matches | 
|---|
| 3315 | * ->subtree_control and propagate controller availability through the | 
|---|
| 3316 | * subtree so that descendants don't have unavailable controllers enabled. | 
|---|
| 3317 | */ | 
|---|
| 3318 | static void cgroup_propagate_control(struct cgroup *cgrp) | 
|---|
| 3319 | { | 
|---|
| 3320 | struct cgroup *dsct; | 
|---|
| 3321 | struct cgroup_subsys_state *d_css; | 
|---|
| 3322 |  | 
|---|
| 3323 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { | 
|---|
| 3324 | dsct->subtree_control &= cgroup_control(cgrp: dsct); | 
|---|
| 3325 | dsct->subtree_ss_mask = | 
|---|
| 3326 | cgroup_calc_subtree_ss_mask(subtree_control: dsct->subtree_control, | 
|---|
| 3327 | this_ss_mask: cgroup_ss_mask(cgrp: dsct)); | 
|---|
| 3328 | } | 
|---|
| 3329 | } | 
|---|
| 3330 |  | 
|---|
| 3331 | /** | 
|---|
| 3332 | * cgroup_restore_control - restore control masks and dom_cgrp of a subtree | 
|---|
| 3333 | * @cgrp: root of the target subtree | 
|---|
| 3334 | * | 
|---|
| 3335 | * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the | 
|---|
| 3336 | * respective old_ prefixed fields for @cgrp's subtree including @cgrp | 
|---|
| 3337 | * itself. | 
|---|
| 3338 | */ | 
|---|
| 3339 | static void cgroup_restore_control(struct cgroup *cgrp) | 
|---|
| 3340 | { | 
|---|
| 3341 | struct cgroup *dsct; | 
|---|
| 3342 | struct cgroup_subsys_state *d_css; | 
|---|
| 3343 |  | 
|---|
| 3344 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { | 
|---|
| 3345 | dsct->subtree_control = dsct->old_subtree_control; | 
|---|
| 3346 | dsct->subtree_ss_mask = dsct->old_subtree_ss_mask; | 
|---|
| 3347 | dsct->dom_cgrp = dsct->old_dom_cgrp; | 
|---|
| 3348 | } | 
|---|
| 3349 | } | 
|---|
| 3350 |  | 
|---|
| 3351 | static bool css_visible(struct cgroup_subsys_state *css) | 
|---|
| 3352 | { | 
|---|
| 3353 | struct cgroup_subsys *ss = css->ss; | 
|---|
| 3354 | struct cgroup *cgrp = css->cgroup; | 
|---|
| 3355 |  | 
|---|
| 3356 | if (cgroup_control(cgrp) & (1 << ss->id)) | 
|---|
| 3357 | return true; | 
|---|
| 3358 | if (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) | 
|---|
| 3359 | return false; | 
|---|
| 3360 | return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl; | 
|---|
| 3361 | } | 
|---|
| 3362 |  | 
|---|
| 3363 | /** | 
|---|
| 3364 | * cgroup_apply_control_enable - enable or show csses according to control | 
|---|
| 3365 | * @cgrp: root of the target subtree | 
|---|
| 3366 | * | 
|---|
| 3367 | * Walk @cgrp's subtree and create new csses or make the existing ones | 
|---|
| 3368 | * visible.  A css is created invisible if it's being implicitly enabled | 
|---|
| 3369 | * through dependency.  An invisible css is made visible when the userland | 
|---|
| 3370 | * explicitly enables it. | 
|---|
| 3371 | * | 
|---|
| 3372 | * Returns 0 on success, -errno on failure.  On failure, csses which have | 
|---|
| 3373 | * been processed already aren't cleaned up.  The caller is responsible for | 
|---|
| 3374 | * cleaning up with cgroup_apply_control_disable(). | 
|---|
| 3375 | */ | 
|---|
| 3376 | static int cgroup_apply_control_enable(struct cgroup *cgrp) | 
|---|
| 3377 | { | 
|---|
| 3378 | struct cgroup *dsct; | 
|---|
| 3379 | struct cgroup_subsys_state *d_css; | 
|---|
| 3380 | struct cgroup_subsys *ss; | 
|---|
| 3381 | int ssid, ret; | 
|---|
| 3382 |  | 
|---|
| 3383 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { | 
|---|
| 3384 | for_each_subsys(ss, ssid) { | 
|---|
| 3385 | struct cgroup_subsys_state *css = cgroup_css(cgrp: dsct, ss); | 
|---|
| 3386 |  | 
|---|
| 3387 | if (!(cgroup_ss_mask(cgrp: dsct) & (1 << ss->id))) | 
|---|
| 3388 | continue; | 
|---|
| 3389 |  | 
|---|
| 3390 | if (!css) { | 
|---|
| 3391 | css = css_create(cgrp: dsct, ss); | 
|---|
| 3392 | if (IS_ERR(ptr: css)) | 
|---|
| 3393 | return PTR_ERR(ptr: css); | 
|---|
| 3394 | } | 
|---|
| 3395 |  | 
|---|
| 3396 | WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt)); | 
|---|
| 3397 |  | 
|---|
| 3398 | if (css_visible(css)) { | 
|---|
| 3399 | ret = css_populate_dir(css); | 
|---|
| 3400 | if (ret) | 
|---|
| 3401 | return ret; | 
|---|
| 3402 | } | 
|---|
| 3403 | } | 
|---|
| 3404 | } | 
|---|
| 3405 |  | 
|---|
| 3406 | return 0; | 
|---|
| 3407 | } | 
|---|
| 3408 |  | 
|---|
| 3409 | /** | 
|---|
| 3410 | * cgroup_apply_control_disable - kill or hide csses according to control | 
|---|
| 3411 | * @cgrp: root of the target subtree | 
|---|
| 3412 | * | 
|---|
| 3413 | * Walk @cgrp's subtree and kill and hide csses so that they match | 
|---|
| 3414 | * cgroup_ss_mask() and cgroup_visible_mask(). | 
|---|
| 3415 | * | 
|---|
| 3416 | * A css is hidden when the userland requests it to be disabled while other | 
|---|
| 3417 | * subsystems are still depending on it.  The css must not actively control | 
|---|
| 3418 | * resources and be in the vanilla state if it's made visible again later. | 
|---|
| 3419 | * Controllers which may be depended upon should provide ->css_reset() for | 
|---|
| 3420 | * this purpose. | 
|---|
| 3421 | */ | 
|---|
| 3422 | static void cgroup_apply_control_disable(struct cgroup *cgrp) | 
|---|
| 3423 | { | 
|---|
| 3424 | struct cgroup *dsct; | 
|---|
| 3425 | struct cgroup_subsys_state *d_css; | 
|---|
| 3426 | struct cgroup_subsys *ss; | 
|---|
| 3427 | int ssid; | 
|---|
| 3428 |  | 
|---|
| 3429 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { | 
|---|
| 3430 | for_each_subsys(ss, ssid) { | 
|---|
| 3431 | struct cgroup_subsys_state *css = cgroup_css(cgrp: dsct, ss); | 
|---|
| 3432 |  | 
|---|
| 3433 | if (!css) | 
|---|
| 3434 | continue; | 
|---|
| 3435 |  | 
|---|
| 3436 | WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt)); | 
|---|
| 3437 |  | 
|---|
| 3438 | if (css->parent && | 
|---|
| 3439 | !(cgroup_ss_mask(cgrp: dsct) & (1 << ss->id))) { | 
|---|
| 3440 | kill_css(css); | 
|---|
| 3441 | } else if (!css_visible(css)) { | 
|---|
| 3442 | css_clear_dir(css); | 
|---|
| 3443 | if (ss->css_reset) | 
|---|
| 3444 | ss->css_reset(css); | 
|---|
| 3445 | } | 
|---|
| 3446 | } | 
|---|
| 3447 | } | 
|---|
| 3448 | } | 
|---|
| 3449 |  | 
|---|
| 3450 | /** | 
|---|
| 3451 | * cgroup_apply_control - apply control mask updates to the subtree | 
|---|
| 3452 | * @cgrp: root of the target subtree | 
|---|
| 3453 | * | 
|---|
| 3454 | * subsystems can be enabled and disabled in a subtree using the following | 
|---|
| 3455 | * steps. | 
|---|
| 3456 | * | 
|---|
| 3457 | * 1. Call cgroup_save_control() to stash the current state. | 
|---|
| 3458 | * 2. Update ->subtree_control masks in the subtree as desired. | 
|---|
| 3459 | * 3. Call cgroup_apply_control() to apply the changes. | 
|---|
| 3460 | * 4. Optionally perform other related operations. | 
|---|
| 3461 | * 5. Call cgroup_finalize_control() to finish up. | 
|---|
| 3462 | * | 
|---|
| 3463 | * This function implements step 3 and propagates the mask changes | 
|---|
| 3464 | * throughout @cgrp's subtree, updates csses accordingly and perform | 
|---|
| 3465 | * process migrations. | 
|---|
| 3466 | */ | 
|---|
| 3467 | static int cgroup_apply_control(struct cgroup *cgrp) | 
|---|
| 3468 | { | 
|---|
| 3469 | int ret; | 
|---|
| 3470 |  | 
|---|
| 3471 | cgroup_propagate_control(cgrp); | 
|---|
| 3472 |  | 
|---|
| 3473 | ret = cgroup_apply_control_enable(cgrp); | 
|---|
| 3474 | if (ret) | 
|---|
| 3475 | return ret; | 
|---|
| 3476 |  | 
|---|
| 3477 | /* | 
|---|
| 3478 | * At this point, cgroup_e_css_by_mask() results reflect the new csses | 
|---|
| 3479 | * making the following cgroup_update_dfl_csses() properly update | 
|---|
| 3480 | * css associations of all tasks in the subtree. | 
|---|
| 3481 | */ | 
|---|
| 3482 | return cgroup_update_dfl_csses(cgrp); | 
|---|
| 3483 | } | 
|---|
| 3484 |  | 
|---|
| 3485 | /** | 
|---|
| 3486 | * cgroup_finalize_control - finalize control mask update | 
|---|
| 3487 | * @cgrp: root of the target subtree | 
|---|
| 3488 | * @ret: the result of the update | 
|---|
| 3489 | * | 
|---|
| 3490 | * Finalize control mask update.  See cgroup_apply_control() for more info. | 
|---|
| 3491 | */ | 
|---|
| 3492 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret) | 
|---|
| 3493 | { | 
|---|
| 3494 | if (ret) { | 
|---|
| 3495 | cgroup_restore_control(cgrp); | 
|---|
| 3496 | cgroup_propagate_control(cgrp); | 
|---|
| 3497 | } | 
|---|
| 3498 |  | 
|---|
| 3499 | cgroup_apply_control_disable(cgrp); | 
|---|
| 3500 | } | 
|---|
| 3501 |  | 
|---|
| 3502 | static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable) | 
|---|
| 3503 | { | 
|---|
| 3504 | u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask; | 
|---|
| 3505 |  | 
|---|
| 3506 | /* if nothing is getting enabled, nothing to worry about */ | 
|---|
| 3507 | if (!enable) | 
|---|
| 3508 | return 0; | 
|---|
| 3509 |  | 
|---|
| 3510 | /* can @cgrp host any resources? */ | 
|---|
| 3511 | if (!cgroup_is_valid_domain(cgrp: cgrp->dom_cgrp)) | 
|---|
| 3512 | return -EOPNOTSUPP; | 
|---|
| 3513 |  | 
|---|
| 3514 | /* mixables don't care */ | 
|---|
| 3515 | if (cgroup_is_mixable(cgrp)) | 
|---|
| 3516 | return 0; | 
|---|
| 3517 |  | 
|---|
| 3518 | if (domain_enable) { | 
|---|
| 3519 | /* can't enable domain controllers inside a thread subtree */ | 
|---|
| 3520 | if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp)) | 
|---|
| 3521 | return -EOPNOTSUPP; | 
|---|
| 3522 | } else { | 
|---|
| 3523 | /* | 
|---|
| 3524 | * Threaded controllers can handle internal competitions | 
|---|
| 3525 | * and are always allowed inside a (prospective) thread | 
|---|
| 3526 | * subtree. | 
|---|
| 3527 | */ | 
|---|
| 3528 | if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp)) | 
|---|
| 3529 | return 0; | 
|---|
| 3530 | } | 
|---|
| 3531 |  | 
|---|
| 3532 | /* | 
|---|
| 3533 | * Controllers can't be enabled for a cgroup with tasks to avoid | 
|---|
| 3534 | * child cgroups competing against tasks. | 
|---|
| 3535 | */ | 
|---|
| 3536 | if (cgroup_has_tasks(cgrp)) | 
|---|
| 3537 | return -EBUSY; | 
|---|
| 3538 |  | 
|---|
| 3539 | return 0; | 
|---|
| 3540 | } | 
|---|
| 3541 |  | 
|---|
| 3542 | /* change the enabled child controllers for a cgroup in the default hierarchy */ | 
|---|
| 3543 | static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, | 
|---|
| 3544 | char *buf, size_t nbytes, | 
|---|
| 3545 | loff_t off) | 
|---|
| 3546 | { | 
|---|
| 3547 | u16 enable = 0, disable = 0; | 
|---|
| 3548 | struct cgroup *cgrp, *child; | 
|---|
| 3549 | struct cgroup_subsys *ss; | 
|---|
| 3550 | char *tok; | 
|---|
| 3551 | int ssid, ret; | 
|---|
| 3552 |  | 
|---|
| 3553 | /* | 
|---|
| 3554 | * Parse input - space separated list of subsystem names prefixed | 
|---|
| 3555 | * with either + or -. | 
|---|
| 3556 | */ | 
|---|
| 3557 | buf = strstrip(str: buf); | 
|---|
| 3558 | while ((tok = strsep(&buf, " "))) { | 
|---|
| 3559 | if (tok[0] == '\0') | 
|---|
| 3560 | continue; | 
|---|
| 3561 | do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) { | 
|---|
| 3562 | if (!cgroup_ssid_enabled(ssid) || | 
|---|
| 3563 | strcmp(tok + 1, ss->name)) | 
|---|
| 3564 | continue; | 
|---|
| 3565 |  | 
|---|
| 3566 | if (*tok == '+') { | 
|---|
| 3567 | enable |= 1 << ssid; | 
|---|
| 3568 | disable &= ~(1 << ssid); | 
|---|
| 3569 | } else if (*tok == '-') { | 
|---|
| 3570 | disable |= 1 << ssid; | 
|---|
| 3571 | enable &= ~(1 << ssid); | 
|---|
| 3572 | } else { | 
|---|
| 3573 | return -EINVAL; | 
|---|
| 3574 | } | 
|---|
| 3575 | break; | 
|---|
| 3576 | } while_each_subsys_mask(); | 
|---|
| 3577 | if (ssid == CGROUP_SUBSYS_COUNT) | 
|---|
| 3578 | return -EINVAL; | 
|---|
| 3579 | } | 
|---|
| 3580 |  | 
|---|
| 3581 | cgrp = cgroup_kn_lock_live(kn: of->kn, drain_offline: true); | 
|---|
| 3582 | if (!cgrp) | 
|---|
| 3583 | return -ENODEV; | 
|---|
| 3584 |  | 
|---|
| 3585 | for_each_subsys(ss, ssid) { | 
|---|
| 3586 | if (enable & (1 << ssid)) { | 
|---|
| 3587 | if (cgrp->subtree_control & (1 << ssid)) { | 
|---|
| 3588 | enable &= ~(1 << ssid); | 
|---|
| 3589 | continue; | 
|---|
| 3590 | } | 
|---|
| 3591 |  | 
|---|
| 3592 | if (!(cgroup_control(cgrp) & (1 << ssid))) { | 
|---|
| 3593 | ret = -ENOENT; | 
|---|
| 3594 | goto out_unlock; | 
|---|
| 3595 | } | 
|---|
| 3596 | } else if (disable & (1 << ssid)) { | 
|---|
| 3597 | if (!(cgrp->subtree_control & (1 << ssid))) { | 
|---|
| 3598 | disable &= ~(1 << ssid); | 
|---|
| 3599 | continue; | 
|---|
| 3600 | } | 
|---|
| 3601 |  | 
|---|
| 3602 | /* a child has it enabled? */ | 
|---|
| 3603 | cgroup_for_each_live_child(child, cgrp) { | 
|---|
| 3604 | if (child->subtree_control & (1 << ssid)) { | 
|---|
| 3605 | ret = -EBUSY; | 
|---|
| 3606 | goto out_unlock; | 
|---|
| 3607 | } | 
|---|
| 3608 | } | 
|---|
| 3609 | } | 
|---|
| 3610 | } | 
|---|
| 3611 |  | 
|---|
| 3612 | if (!enable && !disable) { | 
|---|
| 3613 | ret = 0; | 
|---|
| 3614 | goto out_unlock; | 
|---|
| 3615 | } | 
|---|
| 3616 |  | 
|---|
| 3617 | ret = cgroup_vet_subtree_control_enable(cgrp, enable); | 
|---|
| 3618 | if (ret) | 
|---|
| 3619 | goto out_unlock; | 
|---|
| 3620 |  | 
|---|
| 3621 | /* save and update control masks and prepare csses */ | 
|---|
| 3622 | cgroup_save_control(cgrp); | 
|---|
| 3623 |  | 
|---|
| 3624 | cgrp->subtree_control |= enable; | 
|---|
| 3625 | cgrp->subtree_control &= ~disable; | 
|---|
| 3626 |  | 
|---|
| 3627 | ret = cgroup_apply_control(cgrp); | 
|---|
| 3628 | cgroup_finalize_control(cgrp, ret); | 
|---|
| 3629 | if (ret) | 
|---|
| 3630 | goto out_unlock; | 
|---|
| 3631 |  | 
|---|
| 3632 | kernfs_activate(kn: cgrp->kn); | 
|---|
| 3633 | out_unlock: | 
|---|
| 3634 | cgroup_kn_unlock(kn: of->kn); | 
|---|
| 3635 | return ret ?: nbytes; | 
|---|
| 3636 | } | 
|---|
| 3637 |  | 
|---|
| 3638 | /** | 
|---|
| 3639 | * cgroup_enable_threaded - make @cgrp threaded | 
|---|
| 3640 | * @cgrp: the target cgroup | 
|---|
| 3641 | * | 
|---|
| 3642 | * Called when "threaded" is written to the cgroup.type interface file and | 
|---|
| 3643 | * tries to make @cgrp threaded and join the parent's resource domain. | 
|---|
| 3644 | * This function is never called on the root cgroup as cgroup.type doesn't | 
|---|
| 3645 | * exist on it. | 
|---|
| 3646 | */ | 
|---|
| 3647 | static int cgroup_enable_threaded(struct cgroup *cgrp) | 
|---|
| 3648 | { | 
|---|
| 3649 | struct cgroup *parent = cgroup_parent(cgrp); | 
|---|
| 3650 | struct cgroup *dom_cgrp = parent->dom_cgrp; | 
|---|
| 3651 | struct cgroup *dsct; | 
|---|
| 3652 | struct cgroup_subsys_state *d_css; | 
|---|
| 3653 | int ret; | 
|---|
| 3654 |  | 
|---|
| 3655 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 3656 |  | 
|---|
| 3657 | /* noop if already threaded */ | 
|---|
| 3658 | if (cgroup_is_threaded(cgrp)) | 
|---|
| 3659 | return 0; | 
|---|
| 3660 |  | 
|---|
| 3661 | /* | 
|---|
| 3662 | * If @cgroup is populated or has domain controllers enabled, it | 
|---|
| 3663 | * can't be switched.  While the below cgroup_can_be_thread_root() | 
|---|
| 3664 | * test can catch the same conditions, that's only when @parent is | 
|---|
| 3665 | * not mixable, so let's check it explicitly. | 
|---|
| 3666 | */ | 
|---|
| 3667 | if (cgroup_is_populated(cgrp) || | 
|---|
| 3668 | cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) | 
|---|
| 3669 | return -EOPNOTSUPP; | 
|---|
| 3670 |  | 
|---|
| 3671 | /* we're joining the parent's domain, ensure its validity */ | 
|---|
| 3672 | if (!cgroup_is_valid_domain(cgrp: dom_cgrp) || | 
|---|
| 3673 | !cgroup_can_be_thread_root(cgrp: dom_cgrp)) | 
|---|
| 3674 | return -EOPNOTSUPP; | 
|---|
| 3675 |  | 
|---|
| 3676 | /* | 
|---|
| 3677 | * The following shouldn't cause actual migrations and should | 
|---|
| 3678 | * always succeed. | 
|---|
| 3679 | */ | 
|---|
| 3680 | cgroup_save_control(cgrp); | 
|---|
| 3681 |  | 
|---|
| 3682 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) | 
|---|
| 3683 | if (dsct == cgrp || cgroup_is_threaded(cgrp: dsct)) | 
|---|
| 3684 | dsct->dom_cgrp = dom_cgrp; | 
|---|
| 3685 |  | 
|---|
| 3686 | ret = cgroup_apply_control(cgrp); | 
|---|
| 3687 | if (!ret) | 
|---|
| 3688 | parent->nr_threaded_children++; | 
|---|
| 3689 |  | 
|---|
| 3690 | cgroup_finalize_control(cgrp, ret); | 
|---|
| 3691 | return ret; | 
|---|
| 3692 | } | 
|---|
| 3693 |  | 
|---|
| 3694 | static int cgroup_type_show(struct seq_file *seq, void *v) | 
|---|
| 3695 | { | 
|---|
| 3696 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3697 |  | 
|---|
| 3698 | if (cgroup_is_threaded(cgrp)) | 
|---|
| 3699 | seq_puts(m: seq, s: "threaded\n"); | 
|---|
| 3700 | else if (!cgroup_is_valid_domain(cgrp)) | 
|---|
| 3701 | seq_puts(m: seq, s: "domain invalid\n"); | 
|---|
| 3702 | else if (cgroup_is_thread_root(cgrp)) | 
|---|
| 3703 | seq_puts(m: seq, s: "domain threaded\n"); | 
|---|
| 3704 | else | 
|---|
| 3705 | seq_puts(m: seq, s: "domain\n"); | 
|---|
| 3706 |  | 
|---|
| 3707 | return 0; | 
|---|
| 3708 | } | 
|---|
| 3709 |  | 
|---|
| 3710 | static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf, | 
|---|
| 3711 | size_t nbytes, loff_t off) | 
|---|
| 3712 | { | 
|---|
| 3713 | struct cgroup *cgrp; | 
|---|
| 3714 | int ret; | 
|---|
| 3715 |  | 
|---|
| 3716 | /* only switching to threaded mode is supported */ | 
|---|
| 3717 | if (strcmp(strstrip(str: buf), "threaded")) | 
|---|
| 3718 | return -EINVAL; | 
|---|
| 3719 |  | 
|---|
| 3720 | /* drain dying csses before we re-apply (threaded) subtree control */ | 
|---|
| 3721 | cgrp = cgroup_kn_lock_live(kn: of->kn, drain_offline: true); | 
|---|
| 3722 | if (!cgrp) | 
|---|
| 3723 | return -ENOENT; | 
|---|
| 3724 |  | 
|---|
| 3725 | /* threaded can only be enabled */ | 
|---|
| 3726 | ret = cgroup_enable_threaded(cgrp); | 
|---|
| 3727 |  | 
|---|
| 3728 | cgroup_kn_unlock(kn: of->kn); | 
|---|
| 3729 | return ret ?: nbytes; | 
|---|
| 3730 | } | 
|---|
| 3731 |  | 
|---|
| 3732 | static int cgroup_max_descendants_show(struct seq_file *seq, void *v) | 
|---|
| 3733 | { | 
|---|
| 3734 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3735 | int descendants = READ_ONCE(cgrp->max_descendants); | 
|---|
| 3736 |  | 
|---|
| 3737 | if (descendants == INT_MAX) | 
|---|
| 3738 | seq_puts(m: seq, s: "max\n"); | 
|---|
| 3739 | else | 
|---|
| 3740 | seq_printf(m: seq, fmt: "%d\n", descendants); | 
|---|
| 3741 |  | 
|---|
| 3742 | return 0; | 
|---|
| 3743 | } | 
|---|
| 3744 |  | 
|---|
| 3745 | static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of, | 
|---|
| 3746 | char *buf, size_t nbytes, loff_t off) | 
|---|
| 3747 | { | 
|---|
| 3748 | struct cgroup *cgrp; | 
|---|
| 3749 | int descendants; | 
|---|
| 3750 | ssize_t ret; | 
|---|
| 3751 |  | 
|---|
| 3752 | buf = strstrip(str: buf); | 
|---|
| 3753 | if (!strcmp(buf, "max")) { | 
|---|
| 3754 | descendants = INT_MAX; | 
|---|
| 3755 | } else { | 
|---|
| 3756 | ret = kstrtoint(s: buf, base: 0, res: &descendants); | 
|---|
| 3757 | if (ret) | 
|---|
| 3758 | return ret; | 
|---|
| 3759 | } | 
|---|
| 3760 |  | 
|---|
| 3761 | if (descendants < 0) | 
|---|
| 3762 | return -ERANGE; | 
|---|
| 3763 |  | 
|---|
| 3764 | cgrp = cgroup_kn_lock_live(kn: of->kn, drain_offline: false); | 
|---|
| 3765 | if (!cgrp) | 
|---|
| 3766 | return -ENOENT; | 
|---|
| 3767 |  | 
|---|
| 3768 | cgrp->max_descendants = descendants; | 
|---|
| 3769 |  | 
|---|
| 3770 | cgroup_kn_unlock(kn: of->kn); | 
|---|
| 3771 |  | 
|---|
| 3772 | return nbytes; | 
|---|
| 3773 | } | 
|---|
| 3774 |  | 
|---|
| 3775 | static int cgroup_max_depth_show(struct seq_file *seq, void *v) | 
|---|
| 3776 | { | 
|---|
| 3777 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3778 | int depth = READ_ONCE(cgrp->max_depth); | 
|---|
| 3779 |  | 
|---|
| 3780 | if (depth == INT_MAX) | 
|---|
| 3781 | seq_puts(m: seq, s: "max\n"); | 
|---|
| 3782 | else | 
|---|
| 3783 | seq_printf(m: seq, fmt: "%d\n", depth); | 
|---|
| 3784 |  | 
|---|
| 3785 | return 0; | 
|---|
| 3786 | } | 
|---|
| 3787 |  | 
|---|
| 3788 | static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of, | 
|---|
| 3789 | char *buf, size_t nbytes, loff_t off) | 
|---|
| 3790 | { | 
|---|
| 3791 | struct cgroup *cgrp; | 
|---|
| 3792 | ssize_t ret; | 
|---|
| 3793 | int depth; | 
|---|
| 3794 |  | 
|---|
| 3795 | buf = strstrip(str: buf); | 
|---|
| 3796 | if (!strcmp(buf, "max")) { | 
|---|
| 3797 | depth = INT_MAX; | 
|---|
| 3798 | } else { | 
|---|
| 3799 | ret = kstrtoint(s: buf, base: 0, res: &depth); | 
|---|
| 3800 | if (ret) | 
|---|
| 3801 | return ret; | 
|---|
| 3802 | } | 
|---|
| 3803 |  | 
|---|
| 3804 | if (depth < 0) | 
|---|
| 3805 | return -ERANGE; | 
|---|
| 3806 |  | 
|---|
| 3807 | cgrp = cgroup_kn_lock_live(kn: of->kn, drain_offline: false); | 
|---|
| 3808 | if (!cgrp) | 
|---|
| 3809 | return -ENOENT; | 
|---|
| 3810 |  | 
|---|
| 3811 | cgrp->max_depth = depth; | 
|---|
| 3812 |  | 
|---|
| 3813 | cgroup_kn_unlock(kn: of->kn); | 
|---|
| 3814 |  | 
|---|
| 3815 | return nbytes; | 
|---|
| 3816 | } | 
|---|
| 3817 |  | 
|---|
| 3818 | static int cgroup_events_show(struct seq_file *seq, void *v) | 
|---|
| 3819 | { | 
|---|
| 3820 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3821 |  | 
|---|
| 3822 | seq_printf(m: seq, fmt: "populated %d\n", cgroup_is_populated(cgrp)); | 
|---|
| 3823 | seq_printf(m: seq, fmt: "frozen %d\n", test_bit(CGRP_FROZEN, &cgrp->flags)); | 
|---|
| 3824 |  | 
|---|
| 3825 | return 0; | 
|---|
| 3826 | } | 
|---|
| 3827 |  | 
|---|
| 3828 | static int cgroup_stat_show(struct seq_file *seq, void *v) | 
|---|
| 3829 | { | 
|---|
| 3830 | struct cgroup *cgroup = seq_css(seq)->cgroup; | 
|---|
| 3831 | struct cgroup_subsys_state *css; | 
|---|
| 3832 | int dying_cnt[CGROUP_SUBSYS_COUNT]; | 
|---|
| 3833 | int ssid; | 
|---|
| 3834 |  | 
|---|
| 3835 | seq_printf(m: seq, fmt: "nr_descendants %d\n", | 
|---|
| 3836 | cgroup->nr_descendants); | 
|---|
| 3837 |  | 
|---|
| 3838 | /* | 
|---|
| 3839 | * Show the number of live and dying csses associated with each of | 
|---|
| 3840 | * non-inhibited cgroup subsystems that is bound to cgroup v2. | 
|---|
| 3841 | * | 
|---|
| 3842 | * Without proper lock protection, racing is possible. So the | 
|---|
| 3843 | * numbers may not be consistent when that happens. | 
|---|
| 3844 | */ | 
|---|
| 3845 | rcu_read_lock(); | 
|---|
| 3846 | for (ssid = 0; ssid < CGROUP_SUBSYS_COUNT; ssid++) { | 
|---|
| 3847 | dying_cnt[ssid] = -1; | 
|---|
| 3848 | if ((BIT(ssid) & cgrp_dfl_inhibit_ss_mask) || | 
|---|
| 3849 | (cgroup_subsys[ssid]->root !=  &cgrp_dfl_root)) | 
|---|
| 3850 | continue; | 
|---|
| 3851 | css = rcu_dereference_raw(cgroup->subsys[ssid]); | 
|---|
| 3852 | dying_cnt[ssid] = cgroup->nr_dying_subsys[ssid]; | 
|---|
| 3853 | seq_printf(m: seq, fmt: "nr_subsys_%s %d\n", cgroup_subsys[ssid]->name, | 
|---|
| 3854 | css ? (css->nr_descendants + 1) : 0); | 
|---|
| 3855 | } | 
|---|
| 3856 |  | 
|---|
| 3857 | seq_printf(m: seq, fmt: "nr_dying_descendants %d\n", | 
|---|
| 3858 | cgroup->nr_dying_descendants); | 
|---|
| 3859 | for (ssid = 0; ssid < CGROUP_SUBSYS_COUNT; ssid++) { | 
|---|
| 3860 | if (dying_cnt[ssid] >= 0) | 
|---|
| 3861 | seq_printf(m: seq, fmt: "nr_dying_subsys_%s %d\n", | 
|---|
| 3862 | cgroup_subsys[ssid]->name, dying_cnt[ssid]); | 
|---|
| 3863 | } | 
|---|
| 3864 | rcu_read_unlock(); | 
|---|
| 3865 | return 0; | 
|---|
| 3866 | } | 
|---|
| 3867 |  | 
|---|
| 3868 | static int cgroup_core_local_stat_show(struct seq_file *seq, void *v) | 
|---|
| 3869 | { | 
|---|
| 3870 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3871 | unsigned int sequence; | 
|---|
| 3872 | u64 freeze_time; | 
|---|
| 3873 |  | 
|---|
| 3874 | do { | 
|---|
| 3875 | sequence = read_seqcount_begin(&cgrp->freezer.freeze_seq); | 
|---|
| 3876 | freeze_time = cgrp->freezer.frozen_nsec; | 
|---|
| 3877 | /* Add in current freezer interval if the cgroup is freezing. */ | 
|---|
| 3878 | if (test_bit(CGRP_FREEZE, &cgrp->flags)) | 
|---|
| 3879 | freeze_time += (ktime_get_ns() - | 
|---|
| 3880 | cgrp->freezer.freeze_start_nsec); | 
|---|
| 3881 | } while (read_seqcount_retry(&cgrp->freezer.freeze_seq, sequence)); | 
|---|
| 3882 |  | 
|---|
| 3883 | do_div(freeze_time, NSEC_PER_USEC); | 
|---|
| 3884 | seq_printf(m: seq, fmt: "frozen_usec %llu\n", freeze_time); | 
|---|
| 3885 |  | 
|---|
| 3886 | return 0; | 
|---|
| 3887 | } | 
|---|
| 3888 |  | 
|---|
| 3889 | #ifdef CONFIG_CGROUP_SCHED | 
|---|
| 3890 | /** | 
|---|
| 3891 | * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem | 
|---|
| 3892 | * @cgrp: the cgroup of interest | 
|---|
| 3893 | * @ss: the subsystem of interest | 
|---|
| 3894 | * | 
|---|
| 3895 | * Find and get @cgrp's css associated with @ss.  If the css doesn't exist | 
|---|
| 3896 | * or is offline, %NULL is returned. | 
|---|
| 3897 | */ | 
|---|
| 3898 | static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp, | 
|---|
| 3899 | struct cgroup_subsys *ss) | 
|---|
| 3900 | { | 
|---|
| 3901 | struct cgroup_subsys_state *css; | 
|---|
| 3902 |  | 
|---|
| 3903 | rcu_read_lock(); | 
|---|
| 3904 | css = cgroup_css(cgrp, ss); | 
|---|
| 3905 | if (css && !css_tryget_online(css)) | 
|---|
| 3906 | css = NULL; | 
|---|
| 3907 | rcu_read_unlock(); | 
|---|
| 3908 |  | 
|---|
| 3909 | return css; | 
|---|
| 3910 | } | 
|---|
| 3911 |  | 
|---|
| 3912 | static int (struct seq_file *seq, int ssid) | 
|---|
| 3913 | { | 
|---|
| 3914 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3915 | struct cgroup_subsys *ss = cgroup_subsys[ssid]; | 
|---|
| 3916 | struct cgroup_subsys_state *css; | 
|---|
| 3917 | int ret; | 
|---|
| 3918 |  | 
|---|
| 3919 | if (!ss->css_extra_stat_show) | 
|---|
| 3920 | return 0; | 
|---|
| 3921 |  | 
|---|
| 3922 | css = cgroup_tryget_css(cgrp, ss); | 
|---|
| 3923 | if (!css) | 
|---|
| 3924 | return 0; | 
|---|
| 3925 |  | 
|---|
| 3926 | ret = ss->css_extra_stat_show(seq, css); | 
|---|
| 3927 | css_put(css); | 
|---|
| 3928 | return ret; | 
|---|
| 3929 | } | 
|---|
| 3930 |  | 
|---|
| 3931 | static int cgroup_local_stat_show(struct seq_file *seq, | 
|---|
| 3932 | struct cgroup *cgrp, int ssid) | 
|---|
| 3933 | { | 
|---|
| 3934 | struct cgroup_subsys *ss = cgroup_subsys[ssid]; | 
|---|
| 3935 | struct cgroup_subsys_state *css; | 
|---|
| 3936 | int ret; | 
|---|
| 3937 |  | 
|---|
| 3938 | if (!ss->css_local_stat_show) | 
|---|
| 3939 | return 0; | 
|---|
| 3940 |  | 
|---|
| 3941 | css = cgroup_tryget_css(cgrp, ss); | 
|---|
| 3942 | if (!css) | 
|---|
| 3943 | return 0; | 
|---|
| 3944 |  | 
|---|
| 3945 | ret = ss->css_local_stat_show(seq, css); | 
|---|
| 3946 | css_put(css); | 
|---|
| 3947 | return ret; | 
|---|
| 3948 | } | 
|---|
| 3949 | #endif | 
|---|
| 3950 |  | 
|---|
| 3951 | static int cpu_stat_show(struct seq_file *seq, void *v) | 
|---|
| 3952 | { | 
|---|
| 3953 | int ret = 0; | 
|---|
| 3954 |  | 
|---|
| 3955 | cgroup_base_stat_cputime_show(seq); | 
|---|
| 3956 | #ifdef CONFIG_CGROUP_SCHED | 
|---|
| 3957 | ret = cgroup_extra_stat_show(seq, ssid: cpu_cgrp_id); | 
|---|
| 3958 | #endif | 
|---|
| 3959 | return ret; | 
|---|
| 3960 | } | 
|---|
| 3961 |  | 
|---|
| 3962 | static int cpu_local_stat_show(struct seq_file *seq, void *v) | 
|---|
| 3963 | { | 
|---|
| 3964 | struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3965 | int ret = 0; | 
|---|
| 3966 |  | 
|---|
| 3967 | #ifdef CONFIG_CGROUP_SCHED | 
|---|
| 3968 | ret = cgroup_local_stat_show(seq, cgrp, ssid: cpu_cgrp_id); | 
|---|
| 3969 | #endif | 
|---|
| 3970 | return ret; | 
|---|
| 3971 | } | 
|---|
| 3972 |  | 
|---|
| 3973 | #ifdef CONFIG_PSI | 
|---|
| 3974 | static int cgroup_io_pressure_show(struct seq_file *seq, void *v) | 
|---|
| 3975 | { | 
|---|
| 3976 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3977 | struct psi_group *psi = cgroup_psi(cgrp); | 
|---|
| 3978 |  | 
|---|
| 3979 | return psi_show(seq, psi, PSI_IO); | 
|---|
| 3980 | } | 
|---|
| 3981 | static int cgroup_memory_pressure_show(struct seq_file *seq, void *v) | 
|---|
| 3982 | { | 
|---|
| 3983 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3984 | struct psi_group *psi = cgroup_psi(cgrp); | 
|---|
| 3985 |  | 
|---|
| 3986 | return psi_show(seq, psi, PSI_MEM); | 
|---|
| 3987 | } | 
|---|
| 3988 | static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v) | 
|---|
| 3989 | { | 
|---|
| 3990 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 3991 | struct psi_group *psi = cgroup_psi(cgrp); | 
|---|
| 3992 |  | 
|---|
| 3993 | return psi_show(seq, psi, PSI_CPU); | 
|---|
| 3994 | } | 
|---|
| 3995 |  | 
|---|
| 3996 | static ssize_t pressure_write(struct kernfs_open_file *of, char *buf, | 
|---|
| 3997 | size_t nbytes, enum psi_res res) | 
|---|
| 3998 | { | 
|---|
| 3999 | struct cgroup_file_ctx *ctx = of->priv; | 
|---|
| 4000 | struct psi_trigger *new; | 
|---|
| 4001 | struct cgroup *cgrp; | 
|---|
| 4002 | struct psi_group *psi; | 
|---|
| 4003 |  | 
|---|
| 4004 | cgrp = cgroup_kn_lock_live(of->kn, false); | 
|---|
| 4005 | if (!cgrp) | 
|---|
| 4006 | return -ENODEV; | 
|---|
| 4007 |  | 
|---|
| 4008 | cgroup_get(cgrp); | 
|---|
| 4009 | cgroup_kn_unlock(of->kn); | 
|---|
| 4010 |  | 
|---|
| 4011 | /* Allow only one trigger per file descriptor */ | 
|---|
| 4012 | if (ctx->psi.trigger) { | 
|---|
| 4013 | cgroup_put(cgrp); | 
|---|
| 4014 | return -EBUSY; | 
|---|
| 4015 | } | 
|---|
| 4016 |  | 
|---|
| 4017 | psi = cgroup_psi(cgrp); | 
|---|
| 4018 | new = psi_trigger_create(psi, buf, res, of->file, of); | 
|---|
| 4019 | if (IS_ERR(new)) { | 
|---|
| 4020 | cgroup_put(cgrp); | 
|---|
| 4021 | return PTR_ERR(new); | 
|---|
| 4022 | } | 
|---|
| 4023 |  | 
|---|
| 4024 | smp_store_release(&ctx->psi.trigger, new); | 
|---|
| 4025 | cgroup_put(cgrp); | 
|---|
| 4026 |  | 
|---|
| 4027 | return nbytes; | 
|---|
| 4028 | } | 
|---|
| 4029 |  | 
|---|
| 4030 | static ssize_t cgroup_io_pressure_write(struct kernfs_open_file *of, | 
|---|
| 4031 | char *buf, size_t nbytes, | 
|---|
| 4032 | loff_t off) | 
|---|
| 4033 | { | 
|---|
| 4034 | return pressure_write(of, buf, nbytes, PSI_IO); | 
|---|
| 4035 | } | 
|---|
| 4036 |  | 
|---|
| 4037 | static ssize_t cgroup_memory_pressure_write(struct kernfs_open_file *of, | 
|---|
| 4038 | char *buf, size_t nbytes, | 
|---|
| 4039 | loff_t off) | 
|---|
| 4040 | { | 
|---|
| 4041 | return pressure_write(of, buf, nbytes, PSI_MEM); | 
|---|
| 4042 | } | 
|---|
| 4043 |  | 
|---|
| 4044 | static ssize_t cgroup_cpu_pressure_write(struct kernfs_open_file *of, | 
|---|
| 4045 | char *buf, size_t nbytes, | 
|---|
| 4046 | loff_t off) | 
|---|
| 4047 | { | 
|---|
| 4048 | return pressure_write(of, buf, nbytes, PSI_CPU); | 
|---|
| 4049 | } | 
|---|
| 4050 |  | 
|---|
| 4051 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | 
|---|
| 4052 | static int cgroup_irq_pressure_show(struct seq_file *seq, void *v) | 
|---|
| 4053 | { | 
|---|
| 4054 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 4055 | struct psi_group *psi = cgroup_psi(cgrp); | 
|---|
| 4056 |  | 
|---|
| 4057 | return psi_show(seq, psi, PSI_IRQ); | 
|---|
| 4058 | } | 
|---|
| 4059 |  | 
|---|
| 4060 | static ssize_t cgroup_irq_pressure_write(struct kernfs_open_file *of, | 
|---|
| 4061 | char *buf, size_t nbytes, | 
|---|
| 4062 | loff_t off) | 
|---|
| 4063 | { | 
|---|
| 4064 | return pressure_write(of, buf, nbytes, PSI_IRQ); | 
|---|
| 4065 | } | 
|---|
| 4066 | #endif | 
|---|
| 4067 |  | 
|---|
| 4068 | static int cgroup_pressure_show(struct seq_file *seq, void *v) | 
|---|
| 4069 | { | 
|---|
| 4070 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 4071 | struct psi_group *psi = cgroup_psi(cgrp); | 
|---|
| 4072 |  | 
|---|
| 4073 | seq_printf(seq, "%d\n", psi->enabled); | 
|---|
| 4074 |  | 
|---|
| 4075 | return 0; | 
|---|
| 4076 | } | 
|---|
| 4077 |  | 
|---|
| 4078 | static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, | 
|---|
| 4079 | char *buf, size_t nbytes, | 
|---|
| 4080 | loff_t off) | 
|---|
| 4081 | { | 
|---|
| 4082 | ssize_t ret; | 
|---|
| 4083 | int enable; | 
|---|
| 4084 | struct cgroup *cgrp; | 
|---|
| 4085 | struct psi_group *psi; | 
|---|
| 4086 |  | 
|---|
| 4087 | ret = kstrtoint(strstrip(buf), 0, &enable); | 
|---|
| 4088 | if (ret) | 
|---|
| 4089 | return ret; | 
|---|
| 4090 |  | 
|---|
| 4091 | if (enable < 0 || enable > 1) | 
|---|
| 4092 | return -ERANGE; | 
|---|
| 4093 |  | 
|---|
| 4094 | cgrp = cgroup_kn_lock_live(of->kn, false); | 
|---|
| 4095 | if (!cgrp) | 
|---|
| 4096 | return -ENOENT; | 
|---|
| 4097 |  | 
|---|
| 4098 | psi = cgroup_psi(cgrp); | 
|---|
| 4099 | if (psi->enabled != enable) { | 
|---|
| 4100 | int i; | 
|---|
| 4101 |  | 
|---|
| 4102 | /* show or hide {cpu,memory,io,irq}.pressure files */ | 
|---|
| 4103 | for (i = 0; i < NR_PSI_RESOURCES; i++) | 
|---|
| 4104 | cgroup_file_show(&cgrp->psi_files[i], enable); | 
|---|
| 4105 |  | 
|---|
| 4106 | psi->enabled = enable; | 
|---|
| 4107 | if (enable) | 
|---|
| 4108 | psi_cgroup_restart(psi); | 
|---|
| 4109 | } | 
|---|
| 4110 |  | 
|---|
| 4111 | cgroup_kn_unlock(of->kn); | 
|---|
| 4112 |  | 
|---|
| 4113 | return nbytes; | 
|---|
| 4114 | } | 
|---|
| 4115 |  | 
|---|
| 4116 | static __poll_t cgroup_pressure_poll(struct kernfs_open_file *of, | 
|---|
| 4117 | poll_table *pt) | 
|---|
| 4118 | { | 
|---|
| 4119 | struct cgroup_file_ctx *ctx = of->priv; | 
|---|
| 4120 |  | 
|---|
| 4121 | return psi_trigger_poll(&ctx->psi.trigger, of->file, pt); | 
|---|
| 4122 | } | 
|---|
| 4123 |  | 
|---|
| 4124 | static void cgroup_pressure_release(struct kernfs_open_file *of) | 
|---|
| 4125 | { | 
|---|
| 4126 | struct cgroup_file_ctx *ctx = of->priv; | 
|---|
| 4127 |  | 
|---|
| 4128 | psi_trigger_destroy(ctx->psi.trigger); | 
|---|
| 4129 | } | 
|---|
| 4130 |  | 
|---|
| 4131 | bool cgroup_psi_enabled(void) | 
|---|
| 4132 | { | 
|---|
| 4133 | if (static_branch_likely(&psi_disabled)) | 
|---|
| 4134 | return false; | 
|---|
| 4135 |  | 
|---|
| 4136 | return (cgroup_feature_disable_mask & (1 << OPT_FEATURE_PRESSURE)) == 0; | 
|---|
| 4137 | } | 
|---|
| 4138 |  | 
|---|
| 4139 | #else /* CONFIG_PSI */ | 
|---|
| 4140 | bool cgroup_psi_enabled(void) | 
|---|
| 4141 | { | 
|---|
| 4142 | return false; | 
|---|
| 4143 | } | 
|---|
| 4144 |  | 
|---|
| 4145 | #endif /* CONFIG_PSI */ | 
|---|
| 4146 |  | 
|---|
| 4147 | static int cgroup_freeze_show(struct seq_file *seq, void *v) | 
|---|
| 4148 | { | 
|---|
| 4149 | struct cgroup *cgrp = seq_css(seq)->cgroup; | 
|---|
| 4150 |  | 
|---|
| 4151 | seq_printf(m: seq, fmt: "%d\n", cgrp->freezer.freeze); | 
|---|
| 4152 |  | 
|---|
| 4153 | return 0; | 
|---|
| 4154 | } | 
|---|
| 4155 |  | 
|---|
| 4156 | static ssize_t cgroup_freeze_write(struct kernfs_open_file *of, | 
|---|
| 4157 | char *buf, size_t nbytes, loff_t off) | 
|---|
| 4158 | { | 
|---|
| 4159 | struct cgroup *cgrp; | 
|---|
| 4160 | ssize_t ret; | 
|---|
| 4161 | int freeze; | 
|---|
| 4162 |  | 
|---|
| 4163 | ret = kstrtoint(s: strstrip(str: buf), base: 0, res: &freeze); | 
|---|
| 4164 | if (ret) | 
|---|
| 4165 | return ret; | 
|---|
| 4166 |  | 
|---|
| 4167 | if (freeze < 0 || freeze > 1) | 
|---|
| 4168 | return -ERANGE; | 
|---|
| 4169 |  | 
|---|
| 4170 | cgrp = cgroup_kn_lock_live(kn: of->kn, drain_offline: false); | 
|---|
| 4171 | if (!cgrp) | 
|---|
| 4172 | return -ENOENT; | 
|---|
| 4173 |  | 
|---|
| 4174 | cgroup_freeze(cgrp, freeze); | 
|---|
| 4175 |  | 
|---|
| 4176 | cgroup_kn_unlock(kn: of->kn); | 
|---|
| 4177 |  | 
|---|
| 4178 | return nbytes; | 
|---|
| 4179 | } | 
|---|
| 4180 |  | 
|---|
| 4181 | static void __cgroup_kill(struct cgroup *cgrp) | 
|---|
| 4182 | { | 
|---|
| 4183 | struct css_task_iter it; | 
|---|
| 4184 | struct task_struct *task; | 
|---|
| 4185 |  | 
|---|
| 4186 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 4187 |  | 
|---|
| 4188 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 4189 | cgrp->kill_seq++; | 
|---|
| 4190 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 4191 |  | 
|---|
| 4192 | css_task_iter_start(css: &cgrp->self, flags: CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED, it: &it); | 
|---|
| 4193 | while ((task = css_task_iter_next(it: &it))) { | 
|---|
| 4194 | /* Ignore kernel threads here. */ | 
|---|
| 4195 | if (task->flags & PF_KTHREAD) | 
|---|
| 4196 | continue; | 
|---|
| 4197 |  | 
|---|
| 4198 | /* Skip tasks that are already dying. */ | 
|---|
| 4199 | if (__fatal_signal_pending(p: task)) | 
|---|
| 4200 | continue; | 
|---|
| 4201 |  | 
|---|
| 4202 | send_sig(SIGKILL, task, 0); | 
|---|
| 4203 | } | 
|---|
| 4204 | css_task_iter_end(it: &it); | 
|---|
| 4205 | } | 
|---|
| 4206 |  | 
|---|
| 4207 | static void cgroup_kill(struct cgroup *cgrp) | 
|---|
| 4208 | { | 
|---|
| 4209 | struct cgroup_subsys_state *css; | 
|---|
| 4210 | struct cgroup *dsct; | 
|---|
| 4211 |  | 
|---|
| 4212 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 4213 |  | 
|---|
| 4214 | cgroup_for_each_live_descendant_pre(dsct, css, cgrp) | 
|---|
| 4215 | __cgroup_kill(cgrp: dsct); | 
|---|
| 4216 | } | 
|---|
| 4217 |  | 
|---|
| 4218 | static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf, | 
|---|
| 4219 | size_t nbytes, loff_t off) | 
|---|
| 4220 | { | 
|---|
| 4221 | ssize_t ret = 0; | 
|---|
| 4222 | int kill; | 
|---|
| 4223 | struct cgroup *cgrp; | 
|---|
| 4224 |  | 
|---|
| 4225 | ret = kstrtoint(s: strstrip(str: buf), base: 0, res: &kill); | 
|---|
| 4226 | if (ret) | 
|---|
| 4227 | return ret; | 
|---|
| 4228 |  | 
|---|
| 4229 | if (kill != 1) | 
|---|
| 4230 | return -ERANGE; | 
|---|
| 4231 |  | 
|---|
| 4232 | cgrp = cgroup_kn_lock_live(kn: of->kn, drain_offline: false); | 
|---|
| 4233 | if (!cgrp) | 
|---|
| 4234 | return -ENOENT; | 
|---|
| 4235 |  | 
|---|
| 4236 | /* | 
|---|
| 4237 | * Killing is a process directed operation, i.e. the whole thread-group | 
|---|
| 4238 | * is taken down so act like we do for cgroup.procs and only make this | 
|---|
| 4239 | * writable in non-threaded cgroups. | 
|---|
| 4240 | */ | 
|---|
| 4241 | if (cgroup_is_threaded(cgrp)) | 
|---|
| 4242 | ret = -EOPNOTSUPP; | 
|---|
| 4243 | else | 
|---|
| 4244 | cgroup_kill(cgrp); | 
|---|
| 4245 |  | 
|---|
| 4246 | cgroup_kn_unlock(kn: of->kn); | 
|---|
| 4247 |  | 
|---|
| 4248 | return ret ?: nbytes; | 
|---|
| 4249 | } | 
|---|
| 4250 |  | 
|---|
| 4251 | static int cgroup_file_open(struct kernfs_open_file *of) | 
|---|
| 4252 | { | 
|---|
| 4253 | struct cftype *cft = of_cft(of); | 
|---|
| 4254 | struct cgroup_file_ctx *ctx; | 
|---|
| 4255 | int ret; | 
|---|
| 4256 |  | 
|---|
| 4257 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | 
|---|
| 4258 | if (!ctx) | 
|---|
| 4259 | return -ENOMEM; | 
|---|
| 4260 |  | 
|---|
| 4261 | ctx->ns = current->nsproxy->cgroup_ns; | 
|---|
| 4262 | get_cgroup_ns(ns: ctx->ns); | 
|---|
| 4263 | of->priv = ctx; | 
|---|
| 4264 |  | 
|---|
| 4265 | if (!cft->open) | 
|---|
| 4266 | return 0; | 
|---|
| 4267 |  | 
|---|
| 4268 | ret = cft->open(of); | 
|---|
| 4269 | if (ret) { | 
|---|
| 4270 | put_cgroup_ns(ns: ctx->ns); | 
|---|
| 4271 | kfree(objp: ctx); | 
|---|
| 4272 | } | 
|---|
| 4273 | return ret; | 
|---|
| 4274 | } | 
|---|
| 4275 |  | 
|---|
| 4276 | static void cgroup_file_release(struct kernfs_open_file *of) | 
|---|
| 4277 | { | 
|---|
| 4278 | struct cftype *cft = of_cft(of); | 
|---|
| 4279 | struct cgroup_file_ctx *ctx = of->priv; | 
|---|
| 4280 |  | 
|---|
| 4281 | if (cft->release) | 
|---|
| 4282 | cft->release(of); | 
|---|
| 4283 | put_cgroup_ns(ns: ctx->ns); | 
|---|
| 4284 | kfree(objp: ctx); | 
|---|
| 4285 | of->priv = NULL; | 
|---|
| 4286 | } | 
|---|
| 4287 |  | 
|---|
| 4288 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, | 
|---|
| 4289 | size_t nbytes, loff_t off) | 
|---|
| 4290 | { | 
|---|
| 4291 | struct cgroup_file_ctx *ctx = of->priv; | 
|---|
| 4292 | struct cgroup *cgrp = kn_priv(kn: of->kn); | 
|---|
| 4293 | struct cftype *cft = of_cft(of); | 
|---|
| 4294 | struct cgroup_subsys_state *css; | 
|---|
| 4295 | int ret; | 
|---|
| 4296 |  | 
|---|
| 4297 | if (!nbytes) | 
|---|
| 4298 | return 0; | 
|---|
| 4299 |  | 
|---|
| 4300 | /* | 
|---|
| 4301 | * If namespaces are delegation boundaries, disallow writes to | 
|---|
| 4302 | * files in an non-init namespace root from inside the namespace | 
|---|
| 4303 | * except for the files explicitly marked delegatable - | 
|---|
| 4304 | * eg. cgroup.procs, cgroup.threads and cgroup.subtree_control. | 
|---|
| 4305 | */ | 
|---|
| 4306 | if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) && | 
|---|
| 4307 | !(cft->flags & CFTYPE_NS_DELEGATABLE) && | 
|---|
| 4308 | ctx->ns != &init_cgroup_ns && ctx->ns->root_cset->dfl_cgrp == cgrp) | 
|---|
| 4309 | return -EPERM; | 
|---|
| 4310 |  | 
|---|
| 4311 | if (cft->write) | 
|---|
| 4312 | return cft->write(of, buf, nbytes, off); | 
|---|
| 4313 |  | 
|---|
| 4314 | /* | 
|---|
| 4315 | * kernfs guarantees that a file isn't deleted with operations in | 
|---|
| 4316 | * flight, which means that the matching css is and stays alive and | 
|---|
| 4317 | * doesn't need to be pinned.  The RCU locking is not necessary | 
|---|
| 4318 | * either.  It's just for the convenience of using cgroup_css(). | 
|---|
| 4319 | */ | 
|---|
| 4320 | rcu_read_lock(); | 
|---|
| 4321 | css = cgroup_css(cgrp, ss: cft->ss); | 
|---|
| 4322 | rcu_read_unlock(); | 
|---|
| 4323 |  | 
|---|
| 4324 | if (cft->write_u64) { | 
|---|
| 4325 | unsigned long long v; | 
|---|
| 4326 | ret = kstrtoull(s: buf, base: 0, res: &v); | 
|---|
| 4327 | if (!ret) | 
|---|
| 4328 | ret = cft->write_u64(css, cft, v); | 
|---|
| 4329 | } else if (cft->write_s64) { | 
|---|
| 4330 | long long v; | 
|---|
| 4331 | ret = kstrtoll(s: buf, base: 0, res: &v); | 
|---|
| 4332 | if (!ret) | 
|---|
| 4333 | ret = cft->write_s64(css, cft, v); | 
|---|
| 4334 | } else { | 
|---|
| 4335 | ret = -EINVAL; | 
|---|
| 4336 | } | 
|---|
| 4337 |  | 
|---|
| 4338 | return ret ?: nbytes; | 
|---|
| 4339 | } | 
|---|
| 4340 |  | 
|---|
| 4341 | static __poll_t cgroup_file_poll(struct kernfs_open_file *of, poll_table *pt) | 
|---|
| 4342 | { | 
|---|
| 4343 | struct cftype *cft = of_cft(of); | 
|---|
| 4344 |  | 
|---|
| 4345 | if (cft->poll) | 
|---|
| 4346 | return cft->poll(of, pt); | 
|---|
| 4347 |  | 
|---|
| 4348 | return kernfs_generic_poll(of, pt); | 
|---|
| 4349 | } | 
|---|
| 4350 |  | 
|---|
| 4351 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) | 
|---|
| 4352 | { | 
|---|
| 4353 | return seq_cft(seq)->seq_start(seq, ppos); | 
|---|
| 4354 | } | 
|---|
| 4355 |  | 
|---|
| 4356 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) | 
|---|
| 4357 | { | 
|---|
| 4358 | return seq_cft(seq)->seq_next(seq, v, ppos); | 
|---|
| 4359 | } | 
|---|
| 4360 |  | 
|---|
| 4361 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) | 
|---|
| 4362 | { | 
|---|
| 4363 | if (seq_cft(seq)->seq_stop) | 
|---|
| 4364 | seq_cft(seq)->seq_stop(seq, v); | 
|---|
| 4365 | } | 
|---|
| 4366 |  | 
|---|
| 4367 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) | 
|---|
| 4368 | { | 
|---|
| 4369 | struct cftype *cft = seq_cft(seq: m); | 
|---|
| 4370 | struct cgroup_subsys_state *css = seq_css(seq: m); | 
|---|
| 4371 |  | 
|---|
| 4372 | if (cft->seq_show) | 
|---|
| 4373 | return cft->seq_show(m, arg); | 
|---|
| 4374 |  | 
|---|
| 4375 | if (cft->read_u64) | 
|---|
| 4376 | seq_printf(m, fmt: "%llu\n", cft->read_u64(css, cft)); | 
|---|
| 4377 | else if (cft->read_s64) | 
|---|
| 4378 | seq_printf(m, fmt: "%lld\n", cft->read_s64(css, cft)); | 
|---|
| 4379 | else | 
|---|
| 4380 | return -EINVAL; | 
|---|
| 4381 | return 0; | 
|---|
| 4382 | } | 
|---|
| 4383 |  | 
|---|
| 4384 | static struct kernfs_ops cgroup_kf_single_ops = { | 
|---|
| 4385 | .atomic_write_len	= PAGE_SIZE, | 
|---|
| 4386 | .open			= cgroup_file_open, | 
|---|
| 4387 | .release		= cgroup_file_release, | 
|---|
| 4388 | .write			= cgroup_file_write, | 
|---|
| 4389 | .poll			= cgroup_file_poll, | 
|---|
| 4390 | .seq_show		= cgroup_seqfile_show, | 
|---|
| 4391 | }; | 
|---|
| 4392 |  | 
|---|
| 4393 | static struct kernfs_ops cgroup_kf_ops = { | 
|---|
| 4394 | .atomic_write_len	= PAGE_SIZE, | 
|---|
| 4395 | .open			= cgroup_file_open, | 
|---|
| 4396 | .release		= cgroup_file_release, | 
|---|
| 4397 | .write			= cgroup_file_write, | 
|---|
| 4398 | .poll			= cgroup_file_poll, | 
|---|
| 4399 | .seq_start		= cgroup_seqfile_start, | 
|---|
| 4400 | .seq_next		= cgroup_seqfile_next, | 
|---|
| 4401 | .seq_stop		= cgroup_seqfile_stop, | 
|---|
| 4402 | .seq_show		= cgroup_seqfile_show, | 
|---|
| 4403 | }; | 
|---|
| 4404 |  | 
|---|
| 4405 | static void cgroup_file_notify_timer(struct timer_list *timer) | 
|---|
| 4406 | { | 
|---|
| 4407 | cgroup_file_notify(container_of(timer, struct cgroup_file, | 
|---|
| 4408 | notify_timer)); | 
|---|
| 4409 | } | 
|---|
| 4410 |  | 
|---|
| 4411 | static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, | 
|---|
| 4412 | struct cftype *cft) | 
|---|
| 4413 | { | 
|---|
| 4414 | char name[CGROUP_FILE_NAME_MAX]; | 
|---|
| 4415 | struct kernfs_node *kn; | 
|---|
| 4416 | struct lock_class_key *key = NULL; | 
|---|
| 4417 |  | 
|---|
| 4418 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | 
|---|
| 4419 | key = &cft->lockdep_key; | 
|---|
| 4420 | #endif | 
|---|
| 4421 | kn = __kernfs_create_file(parent: cgrp->kn, name: cgroup_file_name(cgrp, cft, buf: name), | 
|---|
| 4422 | mode: cgroup_file_mode(cft), | 
|---|
| 4423 | current_fsuid(), current_fsgid(), | 
|---|
| 4424 | size: 0, ops: cft->kf_ops, priv: cft, | 
|---|
| 4425 | NULL, key); | 
|---|
| 4426 | if (IS_ERR(ptr: kn)) | 
|---|
| 4427 | return PTR_ERR(ptr: kn); | 
|---|
| 4428 |  | 
|---|
| 4429 | if (cft->file_offset) { | 
|---|
| 4430 | struct cgroup_file *cfile = (void *)css + cft->file_offset; | 
|---|
| 4431 |  | 
|---|
| 4432 | timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0); | 
|---|
| 4433 |  | 
|---|
| 4434 | spin_lock_irq(lock: &cgroup_file_kn_lock); | 
|---|
| 4435 | cfile->kn = kn; | 
|---|
| 4436 | spin_unlock_irq(lock: &cgroup_file_kn_lock); | 
|---|
| 4437 | } | 
|---|
| 4438 |  | 
|---|
| 4439 | return 0; | 
|---|
| 4440 | } | 
|---|
| 4441 |  | 
|---|
| 4442 | /** | 
|---|
| 4443 | * cgroup_addrm_files - add or remove files to a cgroup directory | 
|---|
| 4444 | * @css: the target css | 
|---|
| 4445 | * @cgrp: the target cgroup (usually css->cgroup) | 
|---|
| 4446 | * @cfts: array of cftypes to be added | 
|---|
| 4447 | * @is_add: whether to add or remove | 
|---|
| 4448 | * | 
|---|
| 4449 | * Depending on @is_add, add or remove files defined by @cfts on @cgrp. | 
|---|
| 4450 | * For removals, this function never fails. | 
|---|
| 4451 | */ | 
|---|
| 4452 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, | 
|---|
| 4453 | struct cgroup *cgrp, struct cftype cfts[], | 
|---|
| 4454 | bool is_add) | 
|---|
| 4455 | { | 
|---|
| 4456 | struct cftype *cft, *cft_end = NULL; | 
|---|
| 4457 | int ret = 0; | 
|---|
| 4458 |  | 
|---|
| 4459 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 4460 |  | 
|---|
| 4461 | restart: | 
|---|
| 4462 | for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) { | 
|---|
| 4463 | /* does cft->flags tell us to skip this file on @cgrp? */ | 
|---|
| 4464 | if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) | 
|---|
| 4465 | continue; | 
|---|
| 4466 | if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp)) | 
|---|
| 4467 | continue; | 
|---|
| 4468 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp)) | 
|---|
| 4469 | continue; | 
|---|
| 4470 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp)) | 
|---|
| 4471 | continue; | 
|---|
| 4472 | if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug) | 
|---|
| 4473 | continue; | 
|---|
| 4474 | if (is_add) { | 
|---|
| 4475 | ret = cgroup_add_file(css, cgrp, cft); | 
|---|
| 4476 | if (ret) { | 
|---|
| 4477 | pr_warn( "%s: failed to add %s, err=%d\n", | 
|---|
| 4478 | __func__, cft->name, ret); | 
|---|
| 4479 | cft_end = cft; | 
|---|
| 4480 | is_add = false; | 
|---|
| 4481 | goto restart; | 
|---|
| 4482 | } | 
|---|
| 4483 | } else { | 
|---|
| 4484 | cgroup_rm_file(cgrp, cft); | 
|---|
| 4485 | } | 
|---|
| 4486 | } | 
|---|
| 4487 | return ret; | 
|---|
| 4488 | } | 
|---|
| 4489 |  | 
|---|
| 4490 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) | 
|---|
| 4491 | { | 
|---|
| 4492 | struct cgroup_subsys *ss = cfts[0].ss; | 
|---|
| 4493 | struct cgroup *root = &ss->root->cgrp; | 
|---|
| 4494 | struct cgroup_subsys_state *css; | 
|---|
| 4495 | int ret = 0; | 
|---|
| 4496 |  | 
|---|
| 4497 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 4498 |  | 
|---|
| 4499 | /* add/rm files for all cgroups created before */ | 
|---|
| 4500 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { | 
|---|
| 4501 | struct cgroup *cgrp = css->cgroup; | 
|---|
| 4502 |  | 
|---|
| 4503 | if (!(css->flags & CSS_VISIBLE)) | 
|---|
| 4504 | continue; | 
|---|
| 4505 |  | 
|---|
| 4506 | ret = cgroup_addrm_files(css, cgrp, cfts, is_add); | 
|---|
| 4507 | if (ret) | 
|---|
| 4508 | break; | 
|---|
| 4509 | } | 
|---|
| 4510 |  | 
|---|
| 4511 | if (is_add && !ret) | 
|---|
| 4512 | kernfs_activate(kn: root->kn); | 
|---|
| 4513 | return ret; | 
|---|
| 4514 | } | 
|---|
| 4515 |  | 
|---|
| 4516 | static void cgroup_exit_cftypes(struct cftype *cfts) | 
|---|
| 4517 | { | 
|---|
| 4518 | struct cftype *cft; | 
|---|
| 4519 |  | 
|---|
| 4520 | for (cft = cfts; cft->name[0] != '\0'; cft++) { | 
|---|
| 4521 | /* free copy for custom atomic_write_len, see init_cftypes() */ | 
|---|
| 4522 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) | 
|---|
| 4523 | kfree(objp: cft->kf_ops); | 
|---|
| 4524 | cft->kf_ops = NULL; | 
|---|
| 4525 | cft->ss = NULL; | 
|---|
| 4526 |  | 
|---|
| 4527 | /* revert flags set by cgroup core while adding @cfts */ | 
|---|
| 4528 | cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL | | 
|---|
| 4529 | __CFTYPE_ADDED); | 
|---|
| 4530 | } | 
|---|
| 4531 | } | 
|---|
| 4532 |  | 
|---|
| 4533 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) | 
|---|
| 4534 | { | 
|---|
| 4535 | struct cftype *cft; | 
|---|
| 4536 | int ret = 0; | 
|---|
| 4537 |  | 
|---|
| 4538 | for (cft = cfts; cft->name[0] != '\0'; cft++) { | 
|---|
| 4539 | struct kernfs_ops *kf_ops; | 
|---|
| 4540 |  | 
|---|
| 4541 | WARN_ON(cft->ss || cft->kf_ops); | 
|---|
| 4542 |  | 
|---|
| 4543 | if (cft->flags & __CFTYPE_ADDED) { | 
|---|
| 4544 | ret = -EBUSY; | 
|---|
| 4545 | break; | 
|---|
| 4546 | } | 
|---|
| 4547 |  | 
|---|
| 4548 | if (cft->seq_start) | 
|---|
| 4549 | kf_ops = &cgroup_kf_ops; | 
|---|
| 4550 | else | 
|---|
| 4551 | kf_ops = &cgroup_kf_single_ops; | 
|---|
| 4552 |  | 
|---|
| 4553 | /* | 
|---|
| 4554 | * Ugh... if @cft wants a custom max_write_len, we need to | 
|---|
| 4555 | * make a copy of kf_ops to set its atomic_write_len. | 
|---|
| 4556 | */ | 
|---|
| 4557 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { | 
|---|
| 4558 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); | 
|---|
| 4559 | if (!kf_ops) { | 
|---|
| 4560 | ret = -ENOMEM; | 
|---|
| 4561 | break; | 
|---|
| 4562 | } | 
|---|
| 4563 | kf_ops->atomic_write_len = cft->max_write_len; | 
|---|
| 4564 | } | 
|---|
| 4565 |  | 
|---|
| 4566 | cft->kf_ops = kf_ops; | 
|---|
| 4567 | cft->ss = ss; | 
|---|
| 4568 | cft->flags |= __CFTYPE_ADDED; | 
|---|
| 4569 | } | 
|---|
| 4570 |  | 
|---|
| 4571 | if (ret) | 
|---|
| 4572 | cgroup_exit_cftypes(cfts); | 
|---|
| 4573 | return ret; | 
|---|
| 4574 | } | 
|---|
| 4575 |  | 
|---|
| 4576 | static void cgroup_rm_cftypes_locked(struct cftype *cfts) | 
|---|
| 4577 | { | 
|---|
| 4578 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 4579 |  | 
|---|
| 4580 | list_del(entry: &cfts->node); | 
|---|
| 4581 | cgroup_apply_cftypes(cfts, is_add: false); | 
|---|
| 4582 | cgroup_exit_cftypes(cfts); | 
|---|
| 4583 | } | 
|---|
| 4584 |  | 
|---|
| 4585 | /** | 
|---|
| 4586 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem | 
|---|
| 4587 | * @cfts: zero-length name terminated array of cftypes | 
|---|
| 4588 | * | 
|---|
| 4589 | * Unregister @cfts.  Files described by @cfts are removed from all | 
|---|
| 4590 | * existing cgroups and all future cgroups won't have them either.  This | 
|---|
| 4591 | * function can be called anytime whether @cfts' subsys is attached or not. | 
|---|
| 4592 | * | 
|---|
| 4593 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not | 
|---|
| 4594 | * registered. | 
|---|
| 4595 | */ | 
|---|
| 4596 | int cgroup_rm_cftypes(struct cftype *cfts) | 
|---|
| 4597 | { | 
|---|
| 4598 | if (!cfts || cfts[0].name[0] == '\0') | 
|---|
| 4599 | return 0; | 
|---|
| 4600 |  | 
|---|
| 4601 | if (!(cfts[0].flags & __CFTYPE_ADDED)) | 
|---|
| 4602 | return -ENOENT; | 
|---|
| 4603 |  | 
|---|
| 4604 | cgroup_lock(); | 
|---|
| 4605 | cgroup_rm_cftypes_locked(cfts); | 
|---|
| 4606 | cgroup_unlock(); | 
|---|
| 4607 | return 0; | 
|---|
| 4608 | } | 
|---|
| 4609 |  | 
|---|
| 4610 | /** | 
|---|
| 4611 | * cgroup_add_cftypes - add an array of cftypes to a subsystem | 
|---|
| 4612 | * @ss: target cgroup subsystem | 
|---|
| 4613 | * @cfts: zero-length name terminated array of cftypes | 
|---|
| 4614 | * | 
|---|
| 4615 | * Register @cfts to @ss.  Files described by @cfts are created for all | 
|---|
| 4616 | * existing cgroups to which @ss is attached and all future cgroups will | 
|---|
| 4617 | * have them too.  This function can be called anytime whether @ss is | 
|---|
| 4618 | * attached or not. | 
|---|
| 4619 | * | 
|---|
| 4620 | * Returns 0 on successful registration, -errno on failure.  Note that this | 
|---|
| 4621 | * function currently returns 0 as long as @cfts registration is successful | 
|---|
| 4622 | * even if some file creation attempts on existing cgroups fail. | 
|---|
| 4623 | */ | 
|---|
| 4624 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) | 
|---|
| 4625 | { | 
|---|
| 4626 | int ret; | 
|---|
| 4627 |  | 
|---|
| 4628 | if (!cgroup_ssid_enabled(ssid: ss->id)) | 
|---|
| 4629 | return 0; | 
|---|
| 4630 |  | 
|---|
| 4631 | if (!cfts || cfts[0].name[0] == '\0') | 
|---|
| 4632 | return 0; | 
|---|
| 4633 |  | 
|---|
| 4634 | ret = cgroup_init_cftypes(ss, cfts); | 
|---|
| 4635 | if (ret) | 
|---|
| 4636 | return ret; | 
|---|
| 4637 |  | 
|---|
| 4638 | cgroup_lock(); | 
|---|
| 4639 |  | 
|---|
| 4640 | list_add_tail(new: &cfts->node, head: &ss->cfts); | 
|---|
| 4641 | ret = cgroup_apply_cftypes(cfts, is_add: true); | 
|---|
| 4642 | if (ret) | 
|---|
| 4643 | cgroup_rm_cftypes_locked(cfts); | 
|---|
| 4644 |  | 
|---|
| 4645 | cgroup_unlock(); | 
|---|
| 4646 | return ret; | 
|---|
| 4647 | } | 
|---|
| 4648 |  | 
|---|
| 4649 | /** | 
|---|
| 4650 | * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy | 
|---|
| 4651 | * @ss: target cgroup subsystem | 
|---|
| 4652 | * @cfts: zero-length name terminated array of cftypes | 
|---|
| 4653 | * | 
|---|
| 4654 | * Similar to cgroup_add_cftypes() but the added files are only used for | 
|---|
| 4655 | * the default hierarchy. | 
|---|
| 4656 | */ | 
|---|
| 4657 | int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) | 
|---|
| 4658 | { | 
|---|
| 4659 | struct cftype *cft; | 
|---|
| 4660 |  | 
|---|
| 4661 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) | 
|---|
| 4662 | cft->flags |= __CFTYPE_ONLY_ON_DFL; | 
|---|
| 4663 | return cgroup_add_cftypes(ss, cfts); | 
|---|
| 4664 | } | 
|---|
| 4665 |  | 
|---|
| 4666 | /** | 
|---|
| 4667 | * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies | 
|---|
| 4668 | * @ss: target cgroup subsystem | 
|---|
| 4669 | * @cfts: zero-length name terminated array of cftypes | 
|---|
| 4670 | * | 
|---|
| 4671 | * Similar to cgroup_add_cftypes() but the added files are only used for | 
|---|
| 4672 | * the legacy hierarchies. | 
|---|
| 4673 | */ | 
|---|
| 4674 | int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) | 
|---|
| 4675 | { | 
|---|
| 4676 | struct cftype *cft; | 
|---|
| 4677 |  | 
|---|
| 4678 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) | 
|---|
| 4679 | cft->flags |= __CFTYPE_NOT_ON_DFL; | 
|---|
| 4680 | return cgroup_add_cftypes(ss, cfts); | 
|---|
| 4681 | } | 
|---|
| 4682 |  | 
|---|
| 4683 | /** | 
|---|
| 4684 | * cgroup_file_notify - generate a file modified event for a cgroup_file | 
|---|
| 4685 | * @cfile: target cgroup_file | 
|---|
| 4686 | * | 
|---|
| 4687 | * @cfile must have been obtained by setting cftype->file_offset. | 
|---|
| 4688 | */ | 
|---|
| 4689 | void cgroup_file_notify(struct cgroup_file *cfile) | 
|---|
| 4690 | { | 
|---|
| 4691 | unsigned long flags; | 
|---|
| 4692 |  | 
|---|
| 4693 | spin_lock_irqsave(&cgroup_file_kn_lock, flags); | 
|---|
| 4694 | if (cfile->kn) { | 
|---|
| 4695 | unsigned long last = cfile->notified_at; | 
|---|
| 4696 | unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV; | 
|---|
| 4697 |  | 
|---|
| 4698 | if (time_in_range(jiffies, last, next)) { | 
|---|
| 4699 | timer_reduce(timer: &cfile->notify_timer, expires: next); | 
|---|
| 4700 | } else { | 
|---|
| 4701 | kernfs_notify(kn: cfile->kn); | 
|---|
| 4702 | cfile->notified_at = jiffies; | 
|---|
| 4703 | } | 
|---|
| 4704 | } | 
|---|
| 4705 | spin_unlock_irqrestore(lock: &cgroup_file_kn_lock, flags); | 
|---|
| 4706 | } | 
|---|
| 4707 |  | 
|---|
| 4708 | /** | 
|---|
| 4709 | * cgroup_file_show - show or hide a hidden cgroup file | 
|---|
| 4710 | * @cfile: target cgroup_file obtained by setting cftype->file_offset | 
|---|
| 4711 | * @show: whether to show or hide | 
|---|
| 4712 | */ | 
|---|
| 4713 | void cgroup_file_show(struct cgroup_file *cfile, bool show) | 
|---|
| 4714 | { | 
|---|
| 4715 | struct kernfs_node *kn; | 
|---|
| 4716 |  | 
|---|
| 4717 | spin_lock_irq(lock: &cgroup_file_kn_lock); | 
|---|
| 4718 | kn = cfile->kn; | 
|---|
| 4719 | kernfs_get(kn); | 
|---|
| 4720 | spin_unlock_irq(lock: &cgroup_file_kn_lock); | 
|---|
| 4721 |  | 
|---|
| 4722 | if (kn) | 
|---|
| 4723 | kernfs_show(kn, show); | 
|---|
| 4724 |  | 
|---|
| 4725 | kernfs_put(kn); | 
|---|
| 4726 | } | 
|---|
| 4727 |  | 
|---|
| 4728 | /** | 
|---|
| 4729 | * css_next_child - find the next child of a given css | 
|---|
| 4730 | * @pos: the current position (%NULL to initiate traversal) | 
|---|
| 4731 | * @parent: css whose children to walk | 
|---|
| 4732 | * | 
|---|
| 4733 | * This function returns the next child of @parent and should be called | 
|---|
| 4734 | * under either cgroup_mutex or RCU read lock.  The only requirement is | 
|---|
| 4735 | * that @parent and @pos are accessible.  The next sibling is guaranteed to | 
|---|
| 4736 | * be returned regardless of their states. | 
|---|
| 4737 | * | 
|---|
| 4738 | * If a subsystem synchronizes ->css_online() and the start of iteration, a | 
|---|
| 4739 | * css which finished ->css_online() is guaranteed to be visible in the | 
|---|
| 4740 | * future iterations and will stay visible until the last reference is put. | 
|---|
| 4741 | * A css which hasn't finished ->css_online() or already finished | 
|---|
| 4742 | * ->css_offline() may show up during traversal.  It's each subsystem's | 
|---|
| 4743 | * responsibility to synchronize against on/offlining. | 
|---|
| 4744 | */ | 
|---|
| 4745 | struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, | 
|---|
| 4746 | struct cgroup_subsys_state *parent) | 
|---|
| 4747 | { | 
|---|
| 4748 | struct cgroup_subsys_state *next; | 
|---|
| 4749 |  | 
|---|
| 4750 | cgroup_assert_mutex_or_rcu_locked(); | 
|---|
| 4751 |  | 
|---|
| 4752 | /* | 
|---|
| 4753 | * @pos could already have been unlinked from the sibling list. | 
|---|
| 4754 | * Once a cgroup is removed, its ->sibling.next is no longer | 
|---|
| 4755 | * updated when its next sibling changes.  CSS_RELEASED is set when | 
|---|
| 4756 | * @pos is taken off list, at which time its next pointer is valid, | 
|---|
| 4757 | * and, as releases are serialized, the one pointed to by the next | 
|---|
| 4758 | * pointer is guaranteed to not have started release yet.  This | 
|---|
| 4759 | * implies that if we observe !CSS_RELEASED on @pos in this RCU | 
|---|
| 4760 | * critical section, the one pointed to by its next pointer is | 
|---|
| 4761 | * guaranteed to not have finished its RCU grace period even if we | 
|---|
| 4762 | * have dropped rcu_read_lock() in-between iterations. | 
|---|
| 4763 | * | 
|---|
| 4764 | * If @pos has CSS_RELEASED set, its next pointer can't be | 
|---|
| 4765 | * dereferenced; however, as each css is given a monotonically | 
|---|
| 4766 | * increasing unique serial number and always appended to the | 
|---|
| 4767 | * sibling list, the next one can be found by walking the parent's | 
|---|
| 4768 | * children until the first css with higher serial number than | 
|---|
| 4769 | * @pos's.  While this path can be slower, it happens iff iteration | 
|---|
| 4770 | * races against release and the race window is very small. | 
|---|
| 4771 | */ | 
|---|
| 4772 | if (!pos) { | 
|---|
| 4773 | next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling); | 
|---|
| 4774 | } else if (likely(!(pos->flags & CSS_RELEASED))) { | 
|---|
| 4775 | next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling); | 
|---|
| 4776 | } else { | 
|---|
| 4777 | list_for_each_entry_rcu(next, &parent->children, sibling, | 
|---|
| 4778 | lockdep_is_held(&cgroup_mutex)) | 
|---|
| 4779 | if (next->serial_nr > pos->serial_nr) | 
|---|
| 4780 | break; | 
|---|
| 4781 | } | 
|---|
| 4782 |  | 
|---|
| 4783 | /* | 
|---|
| 4784 | * @next, if not pointing to the head, can be dereferenced and is | 
|---|
| 4785 | * the next sibling. | 
|---|
| 4786 | */ | 
|---|
| 4787 | if (&next->sibling != &parent->children) | 
|---|
| 4788 | return next; | 
|---|
| 4789 | return NULL; | 
|---|
| 4790 | } | 
|---|
| 4791 |  | 
|---|
| 4792 | /** | 
|---|
| 4793 | * css_next_descendant_pre - find the next descendant for pre-order walk | 
|---|
| 4794 | * @pos: the current position (%NULL to initiate traversal) | 
|---|
| 4795 | * @root: css whose descendants to walk | 
|---|
| 4796 | * | 
|---|
| 4797 | * To be used by css_for_each_descendant_pre().  Find the next descendant | 
|---|
| 4798 | * to visit for pre-order traversal of @root's descendants.  @root is | 
|---|
| 4799 | * included in the iteration and the first node to be visited. | 
|---|
| 4800 | * | 
|---|
| 4801 | * While this function requires cgroup_mutex or RCU read locking, it | 
|---|
| 4802 | * doesn't require the whole traversal to be contained in a single critical | 
|---|
| 4803 | * section. Additionally, it isn't necessary to hold onto a reference to @pos. | 
|---|
| 4804 | * This function will return the correct next descendant as long as both @pos | 
|---|
| 4805 | * and @root are accessible and @pos is a descendant of @root. | 
|---|
| 4806 | * | 
|---|
| 4807 | * If a subsystem synchronizes ->css_online() and the start of iteration, a | 
|---|
| 4808 | * css which finished ->css_online() is guaranteed to be visible in the | 
|---|
| 4809 | * future iterations and will stay visible until the last reference is put. | 
|---|
| 4810 | * A css which hasn't finished ->css_online() or already finished | 
|---|
| 4811 | * ->css_offline() may show up during traversal.  It's each subsystem's | 
|---|
| 4812 | * responsibility to synchronize against on/offlining. | 
|---|
| 4813 | */ | 
|---|
| 4814 | struct cgroup_subsys_state * | 
|---|
| 4815 | css_next_descendant_pre(struct cgroup_subsys_state *pos, | 
|---|
| 4816 | struct cgroup_subsys_state *root) | 
|---|
| 4817 | { | 
|---|
| 4818 | struct cgroup_subsys_state *next; | 
|---|
| 4819 |  | 
|---|
| 4820 | cgroup_assert_mutex_or_rcu_locked(); | 
|---|
| 4821 |  | 
|---|
| 4822 | /* if first iteration, visit @root */ | 
|---|
| 4823 | if (!pos) | 
|---|
| 4824 | return root; | 
|---|
| 4825 |  | 
|---|
| 4826 | /* visit the first child if exists */ | 
|---|
| 4827 | next = css_next_child(NULL, parent: pos); | 
|---|
| 4828 | if (next) | 
|---|
| 4829 | return next; | 
|---|
| 4830 |  | 
|---|
| 4831 | /* no child, visit my or the closest ancestor's next sibling */ | 
|---|
| 4832 | while (pos != root) { | 
|---|
| 4833 | next = css_next_child(pos, parent: pos->parent); | 
|---|
| 4834 | if (next) | 
|---|
| 4835 | return next; | 
|---|
| 4836 | pos = pos->parent; | 
|---|
| 4837 | } | 
|---|
| 4838 |  | 
|---|
| 4839 | return NULL; | 
|---|
| 4840 | } | 
|---|
| 4841 | EXPORT_SYMBOL_GPL(css_next_descendant_pre); | 
|---|
| 4842 |  | 
|---|
| 4843 | /** | 
|---|
| 4844 | * css_rightmost_descendant - return the rightmost descendant of a css | 
|---|
| 4845 | * @pos: css of interest | 
|---|
| 4846 | * | 
|---|
| 4847 | * Return the rightmost descendant of @pos.  If there's no descendant, @pos | 
|---|
| 4848 | * is returned.  This can be used during pre-order traversal to skip | 
|---|
| 4849 | * subtree of @pos. | 
|---|
| 4850 | * | 
|---|
| 4851 | * While this function requires cgroup_mutex or RCU read locking, it | 
|---|
| 4852 | * doesn't require the whole traversal to be contained in a single critical | 
|---|
| 4853 | * section. Additionally, it isn't necessary to hold onto a reference to @pos. | 
|---|
| 4854 | * This function will return the correct rightmost descendant as long as @pos | 
|---|
| 4855 | * is accessible. | 
|---|
| 4856 | */ | 
|---|
| 4857 | struct cgroup_subsys_state * | 
|---|
| 4858 | css_rightmost_descendant(struct cgroup_subsys_state *pos) | 
|---|
| 4859 | { | 
|---|
| 4860 | struct cgroup_subsys_state *last, *tmp; | 
|---|
| 4861 |  | 
|---|
| 4862 | cgroup_assert_mutex_or_rcu_locked(); | 
|---|
| 4863 |  | 
|---|
| 4864 | do { | 
|---|
| 4865 | last = pos; | 
|---|
| 4866 | /* ->prev isn't RCU safe, walk ->next till the end */ | 
|---|
| 4867 | pos = NULL; | 
|---|
| 4868 | css_for_each_child(tmp, last) | 
|---|
| 4869 | pos = tmp; | 
|---|
| 4870 | } while (pos); | 
|---|
| 4871 |  | 
|---|
| 4872 | return last; | 
|---|
| 4873 | } | 
|---|
| 4874 |  | 
|---|
| 4875 | static struct cgroup_subsys_state * | 
|---|
| 4876 | css_leftmost_descendant(struct cgroup_subsys_state *pos) | 
|---|
| 4877 | { | 
|---|
| 4878 | struct cgroup_subsys_state *last; | 
|---|
| 4879 |  | 
|---|
| 4880 | do { | 
|---|
| 4881 | last = pos; | 
|---|
| 4882 | pos = css_next_child(NULL, parent: pos); | 
|---|
| 4883 | } while (pos); | 
|---|
| 4884 |  | 
|---|
| 4885 | return last; | 
|---|
| 4886 | } | 
|---|
| 4887 |  | 
|---|
| 4888 | /** | 
|---|
| 4889 | * css_next_descendant_post - find the next descendant for post-order walk | 
|---|
| 4890 | * @pos: the current position (%NULL to initiate traversal) | 
|---|
| 4891 | * @root: css whose descendants to walk | 
|---|
| 4892 | * | 
|---|
| 4893 | * To be used by css_for_each_descendant_post().  Find the next descendant | 
|---|
| 4894 | * to visit for post-order traversal of @root's descendants.  @root is | 
|---|
| 4895 | * included in the iteration and the last node to be visited. | 
|---|
| 4896 | * | 
|---|
| 4897 | * While this function requires cgroup_mutex or RCU read locking, it | 
|---|
| 4898 | * doesn't require the whole traversal to be contained in a single critical | 
|---|
| 4899 | * section. Additionally, it isn't necessary to hold onto a reference to @pos. | 
|---|
| 4900 | * This function will return the correct next descendant as long as both @pos | 
|---|
| 4901 | * and @cgroup are accessible and @pos is a descendant of @cgroup. | 
|---|
| 4902 | * | 
|---|
| 4903 | * If a subsystem synchronizes ->css_online() and the start of iteration, a | 
|---|
| 4904 | * css which finished ->css_online() is guaranteed to be visible in the | 
|---|
| 4905 | * future iterations and will stay visible until the last reference is put. | 
|---|
| 4906 | * A css which hasn't finished ->css_online() or already finished | 
|---|
| 4907 | * ->css_offline() may show up during traversal.  It's each subsystem's | 
|---|
| 4908 | * responsibility to synchronize against on/offlining. | 
|---|
| 4909 | */ | 
|---|
| 4910 | struct cgroup_subsys_state * | 
|---|
| 4911 | css_next_descendant_post(struct cgroup_subsys_state *pos, | 
|---|
| 4912 | struct cgroup_subsys_state *root) | 
|---|
| 4913 | { | 
|---|
| 4914 | struct cgroup_subsys_state *next; | 
|---|
| 4915 |  | 
|---|
| 4916 | cgroup_assert_mutex_or_rcu_locked(); | 
|---|
| 4917 |  | 
|---|
| 4918 | /* if first iteration, visit leftmost descendant which may be @root */ | 
|---|
| 4919 | if (!pos) | 
|---|
| 4920 | return css_leftmost_descendant(pos: root); | 
|---|
| 4921 |  | 
|---|
| 4922 | /* if we visited @root, we're done */ | 
|---|
| 4923 | if (pos == root) | 
|---|
| 4924 | return NULL; | 
|---|
| 4925 |  | 
|---|
| 4926 | /* if there's an unvisited sibling, visit its leftmost descendant */ | 
|---|
| 4927 | next = css_next_child(pos, parent: pos->parent); | 
|---|
| 4928 | if (next) | 
|---|
| 4929 | return css_leftmost_descendant(pos: next); | 
|---|
| 4930 |  | 
|---|
| 4931 | /* no sibling left, visit parent */ | 
|---|
| 4932 | return pos->parent; | 
|---|
| 4933 | } | 
|---|
| 4934 |  | 
|---|
| 4935 | /** | 
|---|
| 4936 | * css_has_online_children - does a css have online children | 
|---|
| 4937 | * @css: the target css | 
|---|
| 4938 | * | 
|---|
| 4939 | * Returns %true if @css has any online children; otherwise, %false.  This | 
|---|
| 4940 | * function can be called from any context but the caller is responsible | 
|---|
| 4941 | * for synchronizing against on/offlining as necessary. | 
|---|
| 4942 | */ | 
|---|
| 4943 | bool css_has_online_children(struct cgroup_subsys_state *css) | 
|---|
| 4944 | { | 
|---|
| 4945 | struct cgroup_subsys_state *child; | 
|---|
| 4946 | bool ret = false; | 
|---|
| 4947 |  | 
|---|
| 4948 | rcu_read_lock(); | 
|---|
| 4949 | css_for_each_child(child, css) { | 
|---|
| 4950 | if (child->flags & CSS_ONLINE) { | 
|---|
| 4951 | ret = true; | 
|---|
| 4952 | break; | 
|---|
| 4953 | } | 
|---|
| 4954 | } | 
|---|
| 4955 | rcu_read_unlock(); | 
|---|
| 4956 | return ret; | 
|---|
| 4957 | } | 
|---|
| 4958 |  | 
|---|
| 4959 | static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it) | 
|---|
| 4960 | { | 
|---|
| 4961 | struct list_head *l; | 
|---|
| 4962 | struct cgrp_cset_link *link; | 
|---|
| 4963 | struct css_set *cset; | 
|---|
| 4964 |  | 
|---|
| 4965 | lockdep_assert_held(&css_set_lock); | 
|---|
| 4966 |  | 
|---|
| 4967 | /* find the next threaded cset */ | 
|---|
| 4968 | if (it->tcset_pos) { | 
|---|
| 4969 | l = it->tcset_pos->next; | 
|---|
| 4970 |  | 
|---|
| 4971 | if (l != it->tcset_head) { | 
|---|
| 4972 | it->tcset_pos = l; | 
|---|
| 4973 | return container_of(l, struct css_set, | 
|---|
| 4974 | threaded_csets_node); | 
|---|
| 4975 | } | 
|---|
| 4976 |  | 
|---|
| 4977 | it->tcset_pos = NULL; | 
|---|
| 4978 | } | 
|---|
| 4979 |  | 
|---|
| 4980 | /* find the next cset */ | 
|---|
| 4981 | l = it->cset_pos; | 
|---|
| 4982 | l = l->next; | 
|---|
| 4983 | if (l == it->cset_head) { | 
|---|
| 4984 | it->cset_pos = NULL; | 
|---|
| 4985 | return NULL; | 
|---|
| 4986 | } | 
|---|
| 4987 |  | 
|---|
| 4988 | if (it->ss) { | 
|---|
| 4989 | cset = container_of(l, struct css_set, e_cset_node[it->ss->id]); | 
|---|
| 4990 | } else { | 
|---|
| 4991 | link = list_entry(l, struct cgrp_cset_link, cset_link); | 
|---|
| 4992 | cset = link->cset; | 
|---|
| 4993 | } | 
|---|
| 4994 |  | 
|---|
| 4995 | it->cset_pos = l; | 
|---|
| 4996 |  | 
|---|
| 4997 | /* initialize threaded css_set walking */ | 
|---|
| 4998 | if (it->flags & CSS_TASK_ITER_THREADED) { | 
|---|
| 4999 | if (it->cur_dcset) | 
|---|
| 5000 | put_css_set_locked(cset: it->cur_dcset); | 
|---|
| 5001 | it->cur_dcset = cset; | 
|---|
| 5002 | get_css_set(cset); | 
|---|
| 5003 |  | 
|---|
| 5004 | it->tcset_head = &cset->threaded_csets; | 
|---|
| 5005 | it->tcset_pos = &cset->threaded_csets; | 
|---|
| 5006 | } | 
|---|
| 5007 |  | 
|---|
| 5008 | return cset; | 
|---|
| 5009 | } | 
|---|
| 5010 |  | 
|---|
| 5011 | /** | 
|---|
| 5012 | * css_task_iter_advance_css_set - advance a task iterator to the next css_set | 
|---|
| 5013 | * @it: the iterator to advance | 
|---|
| 5014 | * | 
|---|
| 5015 | * Advance @it to the next css_set to walk. | 
|---|
| 5016 | */ | 
|---|
| 5017 | static void css_task_iter_advance_css_set(struct css_task_iter *it) | 
|---|
| 5018 | { | 
|---|
| 5019 | struct css_set *cset; | 
|---|
| 5020 |  | 
|---|
| 5021 | lockdep_assert_held(&css_set_lock); | 
|---|
| 5022 |  | 
|---|
| 5023 | /* Advance to the next non-empty css_set and find first non-empty tasks list*/ | 
|---|
| 5024 | while ((cset = css_task_iter_next_css_set(it))) { | 
|---|
| 5025 | if (!list_empty(head: &cset->tasks)) { | 
|---|
| 5026 | it->cur_tasks_head = &cset->tasks; | 
|---|
| 5027 | break; | 
|---|
| 5028 | } else if (!list_empty(head: &cset->mg_tasks)) { | 
|---|
| 5029 | it->cur_tasks_head = &cset->mg_tasks; | 
|---|
| 5030 | break; | 
|---|
| 5031 | } else if (!list_empty(head: &cset->dying_tasks)) { | 
|---|
| 5032 | it->cur_tasks_head = &cset->dying_tasks; | 
|---|
| 5033 | break; | 
|---|
| 5034 | } | 
|---|
| 5035 | } | 
|---|
| 5036 | if (!cset) { | 
|---|
| 5037 | it->task_pos = NULL; | 
|---|
| 5038 | return; | 
|---|
| 5039 | } | 
|---|
| 5040 | it->task_pos = it->cur_tasks_head->next; | 
|---|
| 5041 |  | 
|---|
| 5042 | /* | 
|---|
| 5043 | * We don't keep css_sets locked across iteration steps and thus | 
|---|
| 5044 | * need to take steps to ensure that iteration can be resumed after | 
|---|
| 5045 | * the lock is re-acquired.  Iteration is performed at two levels - | 
|---|
| 5046 | * css_sets and tasks in them. | 
|---|
| 5047 | * | 
|---|
| 5048 | * Once created, a css_set never leaves its cgroup lists, so a | 
|---|
| 5049 | * pinned css_set is guaranteed to stay put and we can resume | 
|---|
| 5050 | * iteration afterwards. | 
|---|
| 5051 | * | 
|---|
| 5052 | * Tasks may leave @cset across iteration steps.  This is resolved | 
|---|
| 5053 | * by registering each iterator with the css_set currently being | 
|---|
| 5054 | * walked and making css_set_move_task() advance iterators whose | 
|---|
| 5055 | * next task is leaving. | 
|---|
| 5056 | */ | 
|---|
| 5057 | if (it->cur_cset) { | 
|---|
| 5058 | list_del(entry: &it->iters_node); | 
|---|
| 5059 | put_css_set_locked(cset: it->cur_cset); | 
|---|
| 5060 | } | 
|---|
| 5061 | get_css_set(cset); | 
|---|
| 5062 | it->cur_cset = cset; | 
|---|
| 5063 | list_add(new: &it->iters_node, head: &cset->task_iters); | 
|---|
| 5064 | } | 
|---|
| 5065 |  | 
|---|
| 5066 | static void css_task_iter_skip(struct css_task_iter *it, | 
|---|
| 5067 | struct task_struct *task) | 
|---|
| 5068 | { | 
|---|
| 5069 | lockdep_assert_held(&css_set_lock); | 
|---|
| 5070 |  | 
|---|
| 5071 | if (it->task_pos == &task->cg_list) { | 
|---|
| 5072 | it->task_pos = it->task_pos->next; | 
|---|
| 5073 | it->flags |= CSS_TASK_ITER_SKIPPED; | 
|---|
| 5074 | } | 
|---|
| 5075 | } | 
|---|
| 5076 |  | 
|---|
| 5077 | static void css_task_iter_advance(struct css_task_iter *it) | 
|---|
| 5078 | { | 
|---|
| 5079 | struct task_struct *task; | 
|---|
| 5080 |  | 
|---|
| 5081 | lockdep_assert_held(&css_set_lock); | 
|---|
| 5082 | repeat: | 
|---|
| 5083 | if (it->task_pos) { | 
|---|
| 5084 | /* | 
|---|
| 5085 | * Advance iterator to find next entry. We go through cset | 
|---|
| 5086 | * tasks, mg_tasks and dying_tasks, when consumed we move onto | 
|---|
| 5087 | * the next cset. | 
|---|
| 5088 | */ | 
|---|
| 5089 | if (it->flags & CSS_TASK_ITER_SKIPPED) | 
|---|
| 5090 | it->flags &= ~CSS_TASK_ITER_SKIPPED; | 
|---|
| 5091 | else | 
|---|
| 5092 | it->task_pos = it->task_pos->next; | 
|---|
| 5093 |  | 
|---|
| 5094 | if (it->task_pos == &it->cur_cset->tasks) { | 
|---|
| 5095 | it->cur_tasks_head = &it->cur_cset->mg_tasks; | 
|---|
| 5096 | it->task_pos = it->cur_tasks_head->next; | 
|---|
| 5097 | } | 
|---|
| 5098 | if (it->task_pos == &it->cur_cset->mg_tasks) { | 
|---|
| 5099 | it->cur_tasks_head = &it->cur_cset->dying_tasks; | 
|---|
| 5100 | it->task_pos = it->cur_tasks_head->next; | 
|---|
| 5101 | } | 
|---|
| 5102 | if (it->task_pos == &it->cur_cset->dying_tasks) | 
|---|
| 5103 | css_task_iter_advance_css_set(it); | 
|---|
| 5104 | } else { | 
|---|
| 5105 | /* called from start, proceed to the first cset */ | 
|---|
| 5106 | css_task_iter_advance_css_set(it); | 
|---|
| 5107 | } | 
|---|
| 5108 |  | 
|---|
| 5109 | if (!it->task_pos) | 
|---|
| 5110 | return; | 
|---|
| 5111 |  | 
|---|
| 5112 | task = list_entry(it->task_pos, struct task_struct, cg_list); | 
|---|
| 5113 |  | 
|---|
| 5114 | if (it->flags & CSS_TASK_ITER_PROCS) { | 
|---|
| 5115 | /* if PROCS, skip over tasks which aren't group leaders */ | 
|---|
| 5116 | if (!thread_group_leader(p: task)) | 
|---|
| 5117 | goto repeat; | 
|---|
| 5118 |  | 
|---|
| 5119 | /* and dying leaders w/o live member threads */ | 
|---|
| 5120 | if (it->cur_tasks_head == &it->cur_cset->dying_tasks && | 
|---|
| 5121 | !atomic_read(v: &task->signal->live)) | 
|---|
| 5122 | goto repeat; | 
|---|
| 5123 | } else { | 
|---|
| 5124 | /* skip all dying ones */ | 
|---|
| 5125 | if (it->cur_tasks_head == &it->cur_cset->dying_tasks) | 
|---|
| 5126 | goto repeat; | 
|---|
| 5127 | } | 
|---|
| 5128 | } | 
|---|
| 5129 |  | 
|---|
| 5130 | /** | 
|---|
| 5131 | * css_task_iter_start - initiate task iteration | 
|---|
| 5132 | * @css: the css to walk tasks of | 
|---|
| 5133 | * @flags: CSS_TASK_ITER_* flags | 
|---|
| 5134 | * @it: the task iterator to use | 
|---|
| 5135 | * | 
|---|
| 5136 | * Initiate iteration through the tasks of @css.  The caller can call | 
|---|
| 5137 | * css_task_iter_next() to walk through the tasks until the function | 
|---|
| 5138 | * returns NULL.  On completion of iteration, css_task_iter_end() must be | 
|---|
| 5139 | * called. | 
|---|
| 5140 | */ | 
|---|
| 5141 | void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags, | 
|---|
| 5142 | struct css_task_iter *it) | 
|---|
| 5143 | { | 
|---|
| 5144 | unsigned long irqflags; | 
|---|
| 5145 |  | 
|---|
| 5146 | memset(s: it, c: 0, n: sizeof(*it)); | 
|---|
| 5147 |  | 
|---|
| 5148 | spin_lock_irqsave(&css_set_lock, irqflags); | 
|---|
| 5149 |  | 
|---|
| 5150 | it->ss = css->ss; | 
|---|
| 5151 | it->flags = flags; | 
|---|
| 5152 |  | 
|---|
| 5153 | if (CGROUP_HAS_SUBSYS_CONFIG && it->ss) | 
|---|
| 5154 | it->cset_pos = &css->cgroup->e_csets[css->ss->id]; | 
|---|
| 5155 | else | 
|---|
| 5156 | it->cset_pos = &css->cgroup->cset_links; | 
|---|
| 5157 |  | 
|---|
| 5158 | it->cset_head = it->cset_pos; | 
|---|
| 5159 |  | 
|---|
| 5160 | css_task_iter_advance(it); | 
|---|
| 5161 |  | 
|---|
| 5162 | spin_unlock_irqrestore(lock: &css_set_lock, flags: irqflags); | 
|---|
| 5163 | } | 
|---|
| 5164 |  | 
|---|
| 5165 | /** | 
|---|
| 5166 | * css_task_iter_next - return the next task for the iterator | 
|---|
| 5167 | * @it: the task iterator being iterated | 
|---|
| 5168 | * | 
|---|
| 5169 | * The "next" function for task iteration.  @it should have been | 
|---|
| 5170 | * initialized via css_task_iter_start().  Returns NULL when the iteration | 
|---|
| 5171 | * reaches the end. | 
|---|
| 5172 | */ | 
|---|
| 5173 | struct task_struct *css_task_iter_next(struct css_task_iter *it) | 
|---|
| 5174 | { | 
|---|
| 5175 | unsigned long irqflags; | 
|---|
| 5176 |  | 
|---|
| 5177 | if (it->cur_task) { | 
|---|
| 5178 | put_task_struct(t: it->cur_task); | 
|---|
| 5179 | it->cur_task = NULL; | 
|---|
| 5180 | } | 
|---|
| 5181 |  | 
|---|
| 5182 | spin_lock_irqsave(&css_set_lock, irqflags); | 
|---|
| 5183 |  | 
|---|
| 5184 | /* @it may be half-advanced by skips, finish advancing */ | 
|---|
| 5185 | if (it->flags & CSS_TASK_ITER_SKIPPED) | 
|---|
| 5186 | css_task_iter_advance(it); | 
|---|
| 5187 |  | 
|---|
| 5188 | if (it->task_pos) { | 
|---|
| 5189 | it->cur_task = list_entry(it->task_pos, struct task_struct, | 
|---|
| 5190 | cg_list); | 
|---|
| 5191 | get_task_struct(t: it->cur_task); | 
|---|
| 5192 | css_task_iter_advance(it); | 
|---|
| 5193 | } | 
|---|
| 5194 |  | 
|---|
| 5195 | spin_unlock_irqrestore(lock: &css_set_lock, flags: irqflags); | 
|---|
| 5196 |  | 
|---|
| 5197 | return it->cur_task; | 
|---|
| 5198 | } | 
|---|
| 5199 |  | 
|---|
| 5200 | /** | 
|---|
| 5201 | * css_task_iter_end - finish task iteration | 
|---|
| 5202 | * @it: the task iterator to finish | 
|---|
| 5203 | * | 
|---|
| 5204 | * Finish task iteration started by css_task_iter_start(). | 
|---|
| 5205 | */ | 
|---|
| 5206 | void css_task_iter_end(struct css_task_iter *it) | 
|---|
| 5207 | { | 
|---|
| 5208 | unsigned long irqflags; | 
|---|
| 5209 |  | 
|---|
| 5210 | if (it->cur_cset) { | 
|---|
| 5211 | spin_lock_irqsave(&css_set_lock, irqflags); | 
|---|
| 5212 | list_del(entry: &it->iters_node); | 
|---|
| 5213 | put_css_set_locked(cset: it->cur_cset); | 
|---|
| 5214 | spin_unlock_irqrestore(lock: &css_set_lock, flags: irqflags); | 
|---|
| 5215 | } | 
|---|
| 5216 |  | 
|---|
| 5217 | if (it->cur_dcset) | 
|---|
| 5218 | put_css_set(cset: it->cur_dcset); | 
|---|
| 5219 |  | 
|---|
| 5220 | if (it->cur_task) | 
|---|
| 5221 | put_task_struct(t: it->cur_task); | 
|---|
| 5222 | } | 
|---|
| 5223 |  | 
|---|
| 5224 | static void cgroup_procs_release(struct kernfs_open_file *of) | 
|---|
| 5225 | { | 
|---|
| 5226 | struct cgroup_file_ctx *ctx = of->priv; | 
|---|
| 5227 |  | 
|---|
| 5228 | if (ctx->procs.started) | 
|---|
| 5229 | css_task_iter_end(it: &ctx->procs.iter); | 
|---|
| 5230 | } | 
|---|
| 5231 |  | 
|---|
| 5232 | static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos) | 
|---|
| 5233 | { | 
|---|
| 5234 | struct kernfs_open_file *of = s->private; | 
|---|
| 5235 | struct cgroup_file_ctx *ctx = of->priv; | 
|---|
| 5236 |  | 
|---|
| 5237 | if (pos) | 
|---|
| 5238 | (*pos)++; | 
|---|
| 5239 |  | 
|---|
| 5240 | return css_task_iter_next(it: &ctx->procs.iter); | 
|---|
| 5241 | } | 
|---|
| 5242 |  | 
|---|
| 5243 | static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos, | 
|---|
| 5244 | unsigned int iter_flags) | 
|---|
| 5245 | { | 
|---|
| 5246 | struct kernfs_open_file *of = s->private; | 
|---|
| 5247 | struct cgroup *cgrp = seq_css(seq: s)->cgroup; | 
|---|
| 5248 | struct cgroup_file_ctx *ctx = of->priv; | 
|---|
| 5249 | struct css_task_iter *it = &ctx->procs.iter; | 
|---|
| 5250 |  | 
|---|
| 5251 | /* | 
|---|
| 5252 | * When a seq_file is seeked, it's always traversed sequentially | 
|---|
| 5253 | * from position 0, so we can simply keep iterating on !0 *pos. | 
|---|
| 5254 | */ | 
|---|
| 5255 | if (!ctx->procs.started) { | 
|---|
| 5256 | if (WARN_ON_ONCE((*pos))) | 
|---|
| 5257 | return ERR_PTR(error: -EINVAL); | 
|---|
| 5258 | css_task_iter_start(css: &cgrp->self, flags: iter_flags, it); | 
|---|
| 5259 | ctx->procs.started = true; | 
|---|
| 5260 | } else if (!(*pos)) { | 
|---|
| 5261 | css_task_iter_end(it); | 
|---|
| 5262 | css_task_iter_start(css: &cgrp->self, flags: iter_flags, it); | 
|---|
| 5263 | } else | 
|---|
| 5264 | return it->cur_task; | 
|---|
| 5265 |  | 
|---|
| 5266 | return cgroup_procs_next(s, NULL, NULL); | 
|---|
| 5267 | } | 
|---|
| 5268 |  | 
|---|
| 5269 | static void *cgroup_procs_start(struct seq_file *s, loff_t *pos) | 
|---|
| 5270 | { | 
|---|
| 5271 | struct cgroup *cgrp = seq_css(seq: s)->cgroup; | 
|---|
| 5272 |  | 
|---|
| 5273 | /* | 
|---|
| 5274 | * All processes of a threaded subtree belong to the domain cgroup | 
|---|
| 5275 | * of the subtree.  Only threads can be distributed across the | 
|---|
| 5276 | * subtree.  Reject reads on cgroup.procs in the subtree proper. | 
|---|
| 5277 | * They're always empty anyway. | 
|---|
| 5278 | */ | 
|---|
| 5279 | if (cgroup_is_threaded(cgrp)) | 
|---|
| 5280 | return ERR_PTR(error: -EOPNOTSUPP); | 
|---|
| 5281 |  | 
|---|
| 5282 | return __cgroup_procs_start(s, pos, iter_flags: CSS_TASK_ITER_PROCS | | 
|---|
| 5283 | CSS_TASK_ITER_THREADED); | 
|---|
| 5284 | } | 
|---|
| 5285 |  | 
|---|
| 5286 | static int cgroup_procs_show(struct seq_file *s, void *v) | 
|---|
| 5287 | { | 
|---|
| 5288 | seq_printf(m: s, fmt: "%d\n", task_pid_vnr(tsk: v)); | 
|---|
| 5289 | return 0; | 
|---|
| 5290 | } | 
|---|
| 5291 |  | 
|---|
| 5292 | static int cgroup_may_write(const struct cgroup *cgrp, struct super_block *sb) | 
|---|
| 5293 | { | 
|---|
| 5294 | int ret; | 
|---|
| 5295 | struct inode *inode; | 
|---|
| 5296 |  | 
|---|
| 5297 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 5298 |  | 
|---|
| 5299 | inode = kernfs_get_inode(sb, kn: cgrp->procs_file.kn); | 
|---|
| 5300 | if (!inode) | 
|---|
| 5301 | return -ENOMEM; | 
|---|
| 5302 |  | 
|---|
| 5303 | ret = inode_permission(&nop_mnt_idmap, inode, MAY_WRITE); | 
|---|
| 5304 | iput(inode); | 
|---|
| 5305 | return ret; | 
|---|
| 5306 | } | 
|---|
| 5307 |  | 
|---|
| 5308 | static int cgroup_procs_write_permission(struct cgroup *src_cgrp, | 
|---|
| 5309 | struct cgroup *dst_cgrp, | 
|---|
| 5310 | struct super_block *sb, | 
|---|
| 5311 | struct cgroup_namespace *ns) | 
|---|
| 5312 | { | 
|---|
| 5313 | struct cgroup *com_cgrp = src_cgrp; | 
|---|
| 5314 | int ret; | 
|---|
| 5315 |  | 
|---|
| 5316 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 5317 |  | 
|---|
| 5318 | /* find the common ancestor */ | 
|---|
| 5319 | while (!cgroup_is_descendant(cgrp: dst_cgrp, ancestor: com_cgrp)) | 
|---|
| 5320 | com_cgrp = cgroup_parent(cgrp: com_cgrp); | 
|---|
| 5321 |  | 
|---|
| 5322 | /* %current should be authorized to migrate to the common ancestor */ | 
|---|
| 5323 | ret = cgroup_may_write(cgrp: com_cgrp, sb); | 
|---|
| 5324 | if (ret) | 
|---|
| 5325 | return ret; | 
|---|
| 5326 |  | 
|---|
| 5327 | /* | 
|---|
| 5328 | * If namespaces are delegation boundaries, %current must be able | 
|---|
| 5329 | * to see both source and destination cgroups from its namespace. | 
|---|
| 5330 | */ | 
|---|
| 5331 | if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) && | 
|---|
| 5332 | (!cgroup_is_descendant(cgrp: src_cgrp, ancestor: ns->root_cset->dfl_cgrp) || | 
|---|
| 5333 | !cgroup_is_descendant(cgrp: dst_cgrp, ancestor: ns->root_cset->dfl_cgrp))) | 
|---|
| 5334 | return -ENOENT; | 
|---|
| 5335 |  | 
|---|
| 5336 | return 0; | 
|---|
| 5337 | } | 
|---|
| 5338 |  | 
|---|
| 5339 | static int cgroup_attach_permissions(struct cgroup *src_cgrp, | 
|---|
| 5340 | struct cgroup *dst_cgrp, | 
|---|
| 5341 | struct super_block *sb, bool threadgroup, | 
|---|
| 5342 | struct cgroup_namespace *ns) | 
|---|
| 5343 | { | 
|---|
| 5344 | int ret = 0; | 
|---|
| 5345 |  | 
|---|
| 5346 | ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, sb, ns); | 
|---|
| 5347 | if (ret) | 
|---|
| 5348 | return ret; | 
|---|
| 5349 |  | 
|---|
| 5350 | ret = cgroup_migrate_vet_dst(dst_cgrp); | 
|---|
| 5351 | if (ret) | 
|---|
| 5352 | return ret; | 
|---|
| 5353 |  | 
|---|
| 5354 | if (!threadgroup && (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp)) | 
|---|
| 5355 | ret = -EOPNOTSUPP; | 
|---|
| 5356 |  | 
|---|
| 5357 | return ret; | 
|---|
| 5358 | } | 
|---|
| 5359 |  | 
|---|
| 5360 | static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf, | 
|---|
| 5361 | bool threadgroup) | 
|---|
| 5362 | { | 
|---|
| 5363 | struct cgroup_file_ctx *ctx = of->priv; | 
|---|
| 5364 | struct cgroup *src_cgrp, *dst_cgrp; | 
|---|
| 5365 | struct task_struct *task; | 
|---|
| 5366 | const struct cred *saved_cred; | 
|---|
| 5367 | ssize_t ret; | 
|---|
| 5368 | enum cgroup_attach_lock_mode lock_mode; | 
|---|
| 5369 |  | 
|---|
| 5370 | dst_cgrp = cgroup_kn_lock_live(kn: of->kn, drain_offline: false); | 
|---|
| 5371 | if (!dst_cgrp) | 
|---|
| 5372 | return -ENODEV; | 
|---|
| 5373 |  | 
|---|
| 5374 | task = cgroup_procs_write_start(buf, threadgroup, lock_mode: &lock_mode); | 
|---|
| 5375 | ret = PTR_ERR_OR_ZERO(ptr: task); | 
|---|
| 5376 | if (ret) | 
|---|
| 5377 | goto out_unlock; | 
|---|
| 5378 |  | 
|---|
| 5379 | /* find the source cgroup */ | 
|---|
| 5380 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 5381 | src_cgrp = task_cgroup_from_root(task, root: &cgrp_dfl_root); | 
|---|
| 5382 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 5383 |  | 
|---|
| 5384 | /* | 
|---|
| 5385 | * Process and thread migrations follow same delegation rule. Check | 
|---|
| 5386 | * permissions using the credentials from file open to protect against | 
|---|
| 5387 | * inherited fd attacks. | 
|---|
| 5388 | */ | 
|---|
| 5389 | saved_cred = override_creds(override_cred: of->file->f_cred); | 
|---|
| 5390 | ret = cgroup_attach_permissions(src_cgrp, dst_cgrp, | 
|---|
| 5391 | sb: of->file->f_path.dentry->d_sb, | 
|---|
| 5392 | threadgroup, ns: ctx->ns); | 
|---|
| 5393 | revert_creds(revert_cred: saved_cred); | 
|---|
| 5394 | if (ret) | 
|---|
| 5395 | goto out_finish; | 
|---|
| 5396 |  | 
|---|
| 5397 | ret = cgroup_attach_task(dst_cgrp, leader: task, threadgroup); | 
|---|
| 5398 |  | 
|---|
| 5399 | out_finish: | 
|---|
| 5400 | cgroup_procs_write_finish(task, lock_mode); | 
|---|
| 5401 | out_unlock: | 
|---|
| 5402 | cgroup_kn_unlock(kn: of->kn); | 
|---|
| 5403 |  | 
|---|
| 5404 | return ret; | 
|---|
| 5405 | } | 
|---|
| 5406 |  | 
|---|
| 5407 | static ssize_t cgroup_procs_write(struct kernfs_open_file *of, | 
|---|
| 5408 | char *buf, size_t nbytes, loff_t off) | 
|---|
| 5409 | { | 
|---|
| 5410 | return __cgroup_procs_write(of, buf, threadgroup: true) ?: nbytes; | 
|---|
| 5411 | } | 
|---|
| 5412 |  | 
|---|
| 5413 | static void *cgroup_threads_start(struct seq_file *s, loff_t *pos) | 
|---|
| 5414 | { | 
|---|
| 5415 | return __cgroup_procs_start(s, pos, iter_flags: 0); | 
|---|
| 5416 | } | 
|---|
| 5417 |  | 
|---|
| 5418 | static ssize_t cgroup_threads_write(struct kernfs_open_file *of, | 
|---|
| 5419 | char *buf, size_t nbytes, loff_t off) | 
|---|
| 5420 | { | 
|---|
| 5421 | return __cgroup_procs_write(of, buf, threadgroup: false) ?: nbytes; | 
|---|
| 5422 | } | 
|---|
| 5423 |  | 
|---|
| 5424 | /* cgroup core interface files for the default hierarchy */ | 
|---|
| 5425 | static struct cftype cgroup_base_files[] = { | 
|---|
| 5426 | { | 
|---|
| 5427 | .name = "cgroup.type", | 
|---|
| 5428 | .flags = CFTYPE_NOT_ON_ROOT, | 
|---|
| 5429 | .seq_show = cgroup_type_show, | 
|---|
| 5430 | .write = cgroup_type_write, | 
|---|
| 5431 | }, | 
|---|
| 5432 | { | 
|---|
| 5433 | .name = "cgroup.procs", | 
|---|
| 5434 | .flags = CFTYPE_NS_DELEGATABLE, | 
|---|
| 5435 | .file_offset = offsetof(struct cgroup, procs_file), | 
|---|
| 5436 | .release = cgroup_procs_release, | 
|---|
| 5437 | .seq_start = cgroup_procs_start, | 
|---|
| 5438 | .seq_next = cgroup_procs_next, | 
|---|
| 5439 | .seq_show = cgroup_procs_show, | 
|---|
| 5440 | .write = cgroup_procs_write, | 
|---|
| 5441 | }, | 
|---|
| 5442 | { | 
|---|
| 5443 | .name = "cgroup.threads", | 
|---|
| 5444 | .flags = CFTYPE_NS_DELEGATABLE, | 
|---|
| 5445 | .release = cgroup_procs_release, | 
|---|
| 5446 | .seq_start = cgroup_threads_start, | 
|---|
| 5447 | .seq_next = cgroup_procs_next, | 
|---|
| 5448 | .seq_show = cgroup_procs_show, | 
|---|
| 5449 | .write = cgroup_threads_write, | 
|---|
| 5450 | }, | 
|---|
| 5451 | { | 
|---|
| 5452 | .name = "cgroup.controllers", | 
|---|
| 5453 | .seq_show = cgroup_controllers_show, | 
|---|
| 5454 | }, | 
|---|
| 5455 | { | 
|---|
| 5456 | .name = "cgroup.subtree_control", | 
|---|
| 5457 | .flags = CFTYPE_NS_DELEGATABLE, | 
|---|
| 5458 | .seq_show = cgroup_subtree_control_show, | 
|---|
| 5459 | .write = cgroup_subtree_control_write, | 
|---|
| 5460 | }, | 
|---|
| 5461 | { | 
|---|
| 5462 | .name = "cgroup.events", | 
|---|
| 5463 | .flags = CFTYPE_NOT_ON_ROOT, | 
|---|
| 5464 | .file_offset = offsetof(struct cgroup, events_file), | 
|---|
| 5465 | .seq_show = cgroup_events_show, | 
|---|
| 5466 | }, | 
|---|
| 5467 | { | 
|---|
| 5468 | .name = "cgroup.max.descendants", | 
|---|
| 5469 | .seq_show = cgroup_max_descendants_show, | 
|---|
| 5470 | .write = cgroup_max_descendants_write, | 
|---|
| 5471 | }, | 
|---|
| 5472 | { | 
|---|
| 5473 | .name = "cgroup.max.depth", | 
|---|
| 5474 | .seq_show = cgroup_max_depth_show, | 
|---|
| 5475 | .write = cgroup_max_depth_write, | 
|---|
| 5476 | }, | 
|---|
| 5477 | { | 
|---|
| 5478 | .name = "cgroup.stat", | 
|---|
| 5479 | .seq_show = cgroup_stat_show, | 
|---|
| 5480 | }, | 
|---|
| 5481 | { | 
|---|
| 5482 | .name = "cgroup.stat.local", | 
|---|
| 5483 | .flags = CFTYPE_NOT_ON_ROOT, | 
|---|
| 5484 | .seq_show = cgroup_core_local_stat_show, | 
|---|
| 5485 | }, | 
|---|
| 5486 | { | 
|---|
| 5487 | .name = "cgroup.freeze", | 
|---|
| 5488 | .flags = CFTYPE_NOT_ON_ROOT, | 
|---|
| 5489 | .seq_show = cgroup_freeze_show, | 
|---|
| 5490 | .write = cgroup_freeze_write, | 
|---|
| 5491 | }, | 
|---|
| 5492 | { | 
|---|
| 5493 | .name = "cgroup.kill", | 
|---|
| 5494 | .flags = CFTYPE_NOT_ON_ROOT, | 
|---|
| 5495 | .write = cgroup_kill_write, | 
|---|
| 5496 | }, | 
|---|
| 5497 | { | 
|---|
| 5498 | .name = "cpu.stat", | 
|---|
| 5499 | .seq_show = cpu_stat_show, | 
|---|
| 5500 | }, | 
|---|
| 5501 | { | 
|---|
| 5502 | .name = "cpu.stat.local", | 
|---|
| 5503 | .seq_show = cpu_local_stat_show, | 
|---|
| 5504 | }, | 
|---|
| 5505 | { }	/* terminate */ | 
|---|
| 5506 | }; | 
|---|
| 5507 |  | 
|---|
| 5508 | static struct cftype cgroup_psi_files[] = { | 
|---|
| 5509 | #ifdef CONFIG_PSI | 
|---|
| 5510 | { | 
|---|
| 5511 | .name = "io.pressure", | 
|---|
| 5512 | .file_offset = offsetof(struct cgroup, psi_files[PSI_IO]), | 
|---|
| 5513 | .seq_show = cgroup_io_pressure_show, | 
|---|
| 5514 | .write = cgroup_io_pressure_write, | 
|---|
| 5515 | .poll = cgroup_pressure_poll, | 
|---|
| 5516 | .release = cgroup_pressure_release, | 
|---|
| 5517 | }, | 
|---|
| 5518 | { | 
|---|
| 5519 | .name = "memory.pressure", | 
|---|
| 5520 | .file_offset = offsetof(struct cgroup, psi_files[PSI_MEM]), | 
|---|
| 5521 | .seq_show = cgroup_memory_pressure_show, | 
|---|
| 5522 | .write = cgroup_memory_pressure_write, | 
|---|
| 5523 | .poll = cgroup_pressure_poll, | 
|---|
| 5524 | .release = cgroup_pressure_release, | 
|---|
| 5525 | }, | 
|---|
| 5526 | { | 
|---|
| 5527 | .name = "cpu.pressure", | 
|---|
| 5528 | .file_offset = offsetof(struct cgroup, psi_files[PSI_CPU]), | 
|---|
| 5529 | .seq_show = cgroup_cpu_pressure_show, | 
|---|
| 5530 | .write = cgroup_cpu_pressure_write, | 
|---|
| 5531 | .poll = cgroup_pressure_poll, | 
|---|
| 5532 | .release = cgroup_pressure_release, | 
|---|
| 5533 | }, | 
|---|
| 5534 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | 
|---|
| 5535 | { | 
|---|
| 5536 | .name = "irq.pressure", | 
|---|
| 5537 | .file_offset = offsetof(struct cgroup, psi_files[PSI_IRQ]), | 
|---|
| 5538 | .seq_show = cgroup_irq_pressure_show, | 
|---|
| 5539 | .write = cgroup_irq_pressure_write, | 
|---|
| 5540 | .poll = cgroup_pressure_poll, | 
|---|
| 5541 | .release = cgroup_pressure_release, | 
|---|
| 5542 | }, | 
|---|
| 5543 | #endif | 
|---|
| 5544 | { | 
|---|
| 5545 | .name = "cgroup.pressure", | 
|---|
| 5546 | .seq_show = cgroup_pressure_show, | 
|---|
| 5547 | .write = cgroup_pressure_write, | 
|---|
| 5548 | }, | 
|---|
| 5549 | #endif /* CONFIG_PSI */ | 
|---|
| 5550 | { }	/* terminate */ | 
|---|
| 5551 | }; | 
|---|
| 5552 |  | 
|---|
| 5553 | /* | 
|---|
| 5554 | * css destruction is four-stage process. | 
|---|
| 5555 | * | 
|---|
| 5556 | * 1. Destruction starts.  Killing of the percpu_ref is initiated. | 
|---|
| 5557 | *    Implemented in kill_css(). | 
|---|
| 5558 | * | 
|---|
| 5559 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs | 
|---|
| 5560 | *    and thus css_tryget_online() is guaranteed to fail, the css can be | 
|---|
| 5561 | *    offlined by invoking offline_css().  After offlining, the base ref is | 
|---|
| 5562 | *    put.  Implemented in css_killed_work_fn(). | 
|---|
| 5563 | * | 
|---|
| 5564 | * 3. When the percpu_ref reaches zero, the only possible remaining | 
|---|
| 5565 | *    accessors are inside RCU read sections.  css_release() schedules the | 
|---|
| 5566 | *    RCU callback. | 
|---|
| 5567 | * | 
|---|
| 5568 | * 4. After the grace period, the css can be freed.  Implemented in | 
|---|
| 5569 | *    css_free_rwork_fn(). | 
|---|
| 5570 | * | 
|---|
| 5571 | * It is actually hairier because both step 2 and 4 require process context | 
|---|
| 5572 | * and thus involve punting to css->destroy_work adding two additional | 
|---|
| 5573 | * steps to the already complex sequence. | 
|---|
| 5574 | */ | 
|---|
| 5575 | static void css_free_rwork_fn(struct work_struct *work) | 
|---|
| 5576 | { | 
|---|
| 5577 | struct cgroup_subsys_state *css = container_of(to_rcu_work(work), | 
|---|
| 5578 | struct cgroup_subsys_state, destroy_rwork); | 
|---|
| 5579 | struct cgroup_subsys *ss = css->ss; | 
|---|
| 5580 | struct cgroup *cgrp = css->cgroup; | 
|---|
| 5581 |  | 
|---|
| 5582 | percpu_ref_exit(ref: &css->refcnt); | 
|---|
| 5583 | css_rstat_exit(css); | 
|---|
| 5584 |  | 
|---|
| 5585 | if (!css_is_self(css)) { | 
|---|
| 5586 | /* css free path */ | 
|---|
| 5587 | struct cgroup_subsys_state *parent = css->parent; | 
|---|
| 5588 | int id = css->id; | 
|---|
| 5589 |  | 
|---|
| 5590 | ss->css_free(css); | 
|---|
| 5591 | cgroup_idr_remove(idr: &ss->css_idr, id); | 
|---|
| 5592 | cgroup_put(cgrp); | 
|---|
| 5593 |  | 
|---|
| 5594 | if (parent) | 
|---|
| 5595 | css_put(css: parent); | 
|---|
| 5596 | } else { | 
|---|
| 5597 | /* cgroup free path */ | 
|---|
| 5598 | atomic_dec(v: &cgrp->root->nr_cgrps); | 
|---|
| 5599 | if (!cgroup_on_dfl(cgrp)) | 
|---|
| 5600 | cgroup1_pidlist_destroy_all(cgrp); | 
|---|
| 5601 | cancel_work_sync(work: &cgrp->release_agent_work); | 
|---|
| 5602 | bpf_cgrp_storage_free(cgroup: cgrp); | 
|---|
| 5603 |  | 
|---|
| 5604 | if (cgroup_parent(cgrp)) { | 
|---|
| 5605 | /* | 
|---|
| 5606 | * We get a ref to the parent, and put the ref when | 
|---|
| 5607 | * this cgroup is being freed, so it's guaranteed | 
|---|
| 5608 | * that the parent won't be destroyed before its | 
|---|
| 5609 | * children. | 
|---|
| 5610 | */ | 
|---|
| 5611 | cgroup_put(cgrp: cgroup_parent(cgrp)); | 
|---|
| 5612 | kernfs_put(kn: cgrp->kn); | 
|---|
| 5613 | psi_cgroup_free(cgrp); | 
|---|
| 5614 | kfree(objp: cgrp); | 
|---|
| 5615 | } else { | 
|---|
| 5616 | /* | 
|---|
| 5617 | * This is root cgroup's refcnt reaching zero, | 
|---|
| 5618 | * which indicates that the root should be | 
|---|
| 5619 | * released. | 
|---|
| 5620 | */ | 
|---|
| 5621 | cgroup_destroy_root(root: cgrp->root); | 
|---|
| 5622 | } | 
|---|
| 5623 | } | 
|---|
| 5624 | } | 
|---|
| 5625 |  | 
|---|
| 5626 | static void css_release_work_fn(struct work_struct *work) | 
|---|
| 5627 | { | 
|---|
| 5628 | struct cgroup_subsys_state *css = | 
|---|
| 5629 | container_of(work, struct cgroup_subsys_state, destroy_work); | 
|---|
| 5630 | struct cgroup_subsys *ss = css->ss; | 
|---|
| 5631 | struct cgroup *cgrp = css->cgroup; | 
|---|
| 5632 |  | 
|---|
| 5633 | cgroup_lock(); | 
|---|
| 5634 |  | 
|---|
| 5635 | css->flags |= CSS_RELEASED; | 
|---|
| 5636 | list_del_rcu(entry: &css->sibling); | 
|---|
| 5637 |  | 
|---|
| 5638 | if (!css_is_self(css)) { | 
|---|
| 5639 | struct cgroup *parent_cgrp; | 
|---|
| 5640 |  | 
|---|
| 5641 | css_rstat_flush(css); | 
|---|
| 5642 |  | 
|---|
| 5643 | cgroup_idr_replace(idr: &ss->css_idr, NULL, id: css->id); | 
|---|
| 5644 | if (ss->css_released) | 
|---|
| 5645 | ss->css_released(css); | 
|---|
| 5646 |  | 
|---|
| 5647 | cgrp->nr_dying_subsys[ss->id]--; | 
|---|
| 5648 | /* | 
|---|
| 5649 | * When a css is released and ready to be freed, its | 
|---|
| 5650 | * nr_descendants must be zero. However, the corresponding | 
|---|
| 5651 | * cgrp->nr_dying_subsys[ss->id] may not be 0 if a subsystem | 
|---|
| 5652 | * is activated and deactivated multiple times with one or | 
|---|
| 5653 | * more of its previous activation leaving behind dying csses. | 
|---|
| 5654 | */ | 
|---|
| 5655 | WARN_ON_ONCE(css->nr_descendants); | 
|---|
| 5656 | parent_cgrp = cgroup_parent(cgrp); | 
|---|
| 5657 | while (parent_cgrp) { | 
|---|
| 5658 | parent_cgrp->nr_dying_subsys[ss->id]--; | 
|---|
| 5659 | parent_cgrp = cgroup_parent(cgrp: parent_cgrp); | 
|---|
| 5660 | } | 
|---|
| 5661 | } else { | 
|---|
| 5662 | struct cgroup *tcgrp; | 
|---|
| 5663 |  | 
|---|
| 5664 | /* cgroup release path */ | 
|---|
| 5665 | TRACE_CGROUP_PATH(release, cgrp); | 
|---|
| 5666 |  | 
|---|
| 5667 | css_rstat_flush(css: &cgrp->self); | 
|---|
| 5668 |  | 
|---|
| 5669 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 5670 | for (tcgrp = cgroup_parent(cgrp); tcgrp; | 
|---|
| 5671 | tcgrp = cgroup_parent(cgrp: tcgrp)) | 
|---|
| 5672 | tcgrp->nr_dying_descendants--; | 
|---|
| 5673 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 5674 |  | 
|---|
| 5675 | /* | 
|---|
| 5676 | * There are two control paths which try to determine | 
|---|
| 5677 | * cgroup from dentry without going through kernfs - | 
|---|
| 5678 | * cgroupstats_build() and css_tryget_online_from_dir(). | 
|---|
| 5679 | * Those are supported by RCU protecting clearing of | 
|---|
| 5680 | * cgrp->kn->priv backpointer. | 
|---|
| 5681 | */ | 
|---|
| 5682 | if (cgrp->kn) | 
|---|
| 5683 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, | 
|---|
| 5684 | NULL); | 
|---|
| 5685 | } | 
|---|
| 5686 |  | 
|---|
| 5687 | cgroup_unlock(); | 
|---|
| 5688 |  | 
|---|
| 5689 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); | 
|---|
| 5690 | queue_rcu_work(wq: cgroup_free_wq, rwork: &css->destroy_rwork); | 
|---|
| 5691 | } | 
|---|
| 5692 |  | 
|---|
| 5693 | static void css_release(struct percpu_ref *ref) | 
|---|
| 5694 | { | 
|---|
| 5695 | struct cgroup_subsys_state *css = | 
|---|
| 5696 | container_of(ref, struct cgroup_subsys_state, refcnt); | 
|---|
| 5697 |  | 
|---|
| 5698 | INIT_WORK(&css->destroy_work, css_release_work_fn); | 
|---|
| 5699 | queue_work(wq: cgroup_release_wq, work: &css->destroy_work); | 
|---|
| 5700 | } | 
|---|
| 5701 |  | 
|---|
| 5702 | static void init_and_link_css(struct cgroup_subsys_state *css, | 
|---|
| 5703 | struct cgroup_subsys *ss, struct cgroup *cgrp) | 
|---|
| 5704 | { | 
|---|
| 5705 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 5706 |  | 
|---|
| 5707 | cgroup_get_live(cgrp); | 
|---|
| 5708 |  | 
|---|
| 5709 | memset(s: css, c: 0, n: sizeof(*css)); | 
|---|
| 5710 | css->cgroup = cgrp; | 
|---|
| 5711 | css->ss = ss; | 
|---|
| 5712 | css->id = -1; | 
|---|
| 5713 | INIT_LIST_HEAD(list: &css->sibling); | 
|---|
| 5714 | INIT_LIST_HEAD(list: &css->children); | 
|---|
| 5715 | css->serial_nr = css_serial_nr_next++; | 
|---|
| 5716 | atomic_set(v: &css->online_cnt, i: 0); | 
|---|
| 5717 |  | 
|---|
| 5718 | if (cgroup_parent(cgrp)) { | 
|---|
| 5719 | css->parent = cgroup_css(cgrp: cgroup_parent(cgrp), ss); | 
|---|
| 5720 | css_get(css: css->parent); | 
|---|
| 5721 | } | 
|---|
| 5722 |  | 
|---|
| 5723 | BUG_ON(cgroup_css(cgrp, ss)); | 
|---|
| 5724 | } | 
|---|
| 5725 |  | 
|---|
| 5726 | /* invoke ->css_online() on a new CSS and mark it online if successful */ | 
|---|
| 5727 | static int online_css(struct cgroup_subsys_state *css) | 
|---|
| 5728 | { | 
|---|
| 5729 | struct cgroup_subsys *ss = css->ss; | 
|---|
| 5730 | int ret = 0; | 
|---|
| 5731 |  | 
|---|
| 5732 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 5733 |  | 
|---|
| 5734 | if (ss->css_online) | 
|---|
| 5735 | ret = ss->css_online(css); | 
|---|
| 5736 | if (!ret) { | 
|---|
| 5737 | css->flags |= CSS_ONLINE; | 
|---|
| 5738 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); | 
|---|
| 5739 |  | 
|---|
| 5740 | atomic_inc(v: &css->online_cnt); | 
|---|
| 5741 | if (css->parent) { | 
|---|
| 5742 | atomic_inc(v: &css->parent->online_cnt); | 
|---|
| 5743 | while ((css = css->parent)) | 
|---|
| 5744 | css->nr_descendants++; | 
|---|
| 5745 | } | 
|---|
| 5746 | } | 
|---|
| 5747 | return ret; | 
|---|
| 5748 | } | 
|---|
| 5749 |  | 
|---|
| 5750 | /* if the CSS is online, invoke ->css_offline() on it and mark it offline */ | 
|---|
| 5751 | static void offline_css(struct cgroup_subsys_state *css) | 
|---|
| 5752 | { | 
|---|
| 5753 | struct cgroup_subsys *ss = css->ss; | 
|---|
| 5754 |  | 
|---|
| 5755 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 5756 |  | 
|---|
| 5757 | if (!(css->flags & CSS_ONLINE)) | 
|---|
| 5758 | return; | 
|---|
| 5759 |  | 
|---|
| 5760 | if (ss->css_offline) | 
|---|
| 5761 | ss->css_offline(css); | 
|---|
| 5762 |  | 
|---|
| 5763 | css->flags &= ~CSS_ONLINE; | 
|---|
| 5764 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); | 
|---|
| 5765 |  | 
|---|
| 5766 | wake_up_all(&css->cgroup->offline_waitq); | 
|---|
| 5767 |  | 
|---|
| 5768 | css->cgroup->nr_dying_subsys[ss->id]++; | 
|---|
| 5769 | /* | 
|---|
| 5770 | * Parent css and cgroup cannot be freed until after the freeing | 
|---|
| 5771 | * of child css, see css_free_rwork_fn(). | 
|---|
| 5772 | */ | 
|---|
| 5773 | while ((css = css->parent)) { | 
|---|
| 5774 | css->nr_descendants--; | 
|---|
| 5775 | css->cgroup->nr_dying_subsys[ss->id]++; | 
|---|
| 5776 | } | 
|---|
| 5777 | } | 
|---|
| 5778 |  | 
|---|
| 5779 | /** | 
|---|
| 5780 | * css_create - create a cgroup_subsys_state | 
|---|
| 5781 | * @cgrp: the cgroup new css will be associated with | 
|---|
| 5782 | * @ss: the subsys of new css | 
|---|
| 5783 | * | 
|---|
| 5784 | * Create a new css associated with @cgrp - @ss pair.  On success, the new | 
|---|
| 5785 | * css is online and installed in @cgrp.  This function doesn't create the | 
|---|
| 5786 | * interface files.  Returns 0 on success, -errno on failure. | 
|---|
| 5787 | */ | 
|---|
| 5788 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, | 
|---|
| 5789 | struct cgroup_subsys *ss) | 
|---|
| 5790 | { | 
|---|
| 5791 | struct cgroup *parent = cgroup_parent(cgrp); | 
|---|
| 5792 | struct cgroup_subsys_state *parent_css = cgroup_css(cgrp: parent, ss); | 
|---|
| 5793 | struct cgroup_subsys_state *css; | 
|---|
| 5794 | int err; | 
|---|
| 5795 |  | 
|---|
| 5796 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 5797 |  | 
|---|
| 5798 | css = ss->css_alloc(parent_css); | 
|---|
| 5799 | if (!css) | 
|---|
| 5800 | css = ERR_PTR(error: -ENOMEM); | 
|---|
| 5801 | if (IS_ERR(ptr: css)) | 
|---|
| 5802 | return css; | 
|---|
| 5803 |  | 
|---|
| 5804 | init_and_link_css(css, ss, cgrp); | 
|---|
| 5805 |  | 
|---|
| 5806 | err = percpu_ref_init(ref: &css->refcnt, release: css_release, flags: 0, GFP_KERNEL); | 
|---|
| 5807 | if (err) | 
|---|
| 5808 | goto err_free_css; | 
|---|
| 5809 |  | 
|---|
| 5810 | err = cgroup_idr_alloc(idr: &ss->css_idr, NULL, start: 2, end: 0, GFP_KERNEL); | 
|---|
| 5811 | if (err < 0) | 
|---|
| 5812 | goto err_free_css; | 
|---|
| 5813 | css->id = err; | 
|---|
| 5814 |  | 
|---|
| 5815 | err = css_rstat_init(css); | 
|---|
| 5816 | if (err) | 
|---|
| 5817 | goto err_free_css; | 
|---|
| 5818 |  | 
|---|
| 5819 | /* @css is ready to be brought online now, make it visible */ | 
|---|
| 5820 | list_add_tail_rcu(new: &css->sibling, head: &parent_css->children); | 
|---|
| 5821 | cgroup_idr_replace(idr: &ss->css_idr, ptr: css, id: css->id); | 
|---|
| 5822 |  | 
|---|
| 5823 | err = online_css(css); | 
|---|
| 5824 | if (err) | 
|---|
| 5825 | goto err_list_del; | 
|---|
| 5826 |  | 
|---|
| 5827 | return css; | 
|---|
| 5828 |  | 
|---|
| 5829 | err_list_del: | 
|---|
| 5830 | list_del_rcu(entry: &css->sibling); | 
|---|
| 5831 | err_free_css: | 
|---|
| 5832 | INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); | 
|---|
| 5833 | queue_rcu_work(wq: cgroup_free_wq, rwork: &css->destroy_rwork); | 
|---|
| 5834 | return ERR_PTR(error: err); | 
|---|
| 5835 | } | 
|---|
| 5836 |  | 
|---|
| 5837 | /* | 
|---|
| 5838 | * The returned cgroup is fully initialized including its control mask, but | 
|---|
| 5839 | * it doesn't have the control mask applied. | 
|---|
| 5840 | */ | 
|---|
| 5841 | static struct cgroup *cgroup_create(struct cgroup *parent, const char *name, | 
|---|
| 5842 | umode_t mode) | 
|---|
| 5843 | { | 
|---|
| 5844 | struct cgroup_root *root = parent->root; | 
|---|
| 5845 | struct cgroup *cgrp, *tcgrp; | 
|---|
| 5846 | struct kernfs_node *kn; | 
|---|
| 5847 | int i, level = parent->level + 1; | 
|---|
| 5848 | int ret; | 
|---|
| 5849 |  | 
|---|
| 5850 | /* allocate the cgroup and its ID, 0 is reserved for the root */ | 
|---|
| 5851 | cgrp = kzalloc(struct_size(cgrp, ancestors, (level + 1)), GFP_KERNEL); | 
|---|
| 5852 | if (!cgrp) | 
|---|
| 5853 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 5854 |  | 
|---|
| 5855 | ret = percpu_ref_init(ref: &cgrp->self.refcnt, release: css_release, flags: 0, GFP_KERNEL); | 
|---|
| 5856 | if (ret) | 
|---|
| 5857 | goto out_free_cgrp; | 
|---|
| 5858 |  | 
|---|
| 5859 | /* create the directory */ | 
|---|
| 5860 | kn = kernfs_create_dir_ns(parent: parent->kn, name, mode, | 
|---|
| 5861 | current_fsuid(), current_fsgid(), | 
|---|
| 5862 | priv: cgrp, NULL); | 
|---|
| 5863 | if (IS_ERR(ptr: kn)) { | 
|---|
| 5864 | ret = PTR_ERR(ptr: kn); | 
|---|
| 5865 | goto out_cancel_ref; | 
|---|
| 5866 | } | 
|---|
| 5867 | cgrp->kn = kn; | 
|---|
| 5868 |  | 
|---|
| 5869 | init_cgroup_housekeeping(cgrp); | 
|---|
| 5870 |  | 
|---|
| 5871 | cgrp->self.parent = &parent->self; | 
|---|
| 5872 | cgrp->root = root; | 
|---|
| 5873 | cgrp->level = level; | 
|---|
| 5874 |  | 
|---|
| 5875 | /* | 
|---|
| 5876 | * Now that init_cgroup_housekeeping() has been called and cgrp->self | 
|---|
| 5877 | * is setup, it is safe to perform rstat initialization on it. | 
|---|
| 5878 | */ | 
|---|
| 5879 | ret = css_rstat_init(css: &cgrp->self); | 
|---|
| 5880 | if (ret) | 
|---|
| 5881 | goto out_kernfs_remove; | 
|---|
| 5882 |  | 
|---|
| 5883 | ret = psi_cgroup_alloc(cgrp); | 
|---|
| 5884 | if (ret) | 
|---|
| 5885 | goto out_stat_exit; | 
|---|
| 5886 |  | 
|---|
| 5887 | for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(cgrp: tcgrp)) | 
|---|
| 5888 | cgrp->ancestors[tcgrp->level] = tcgrp; | 
|---|
| 5889 |  | 
|---|
| 5890 | /* | 
|---|
| 5891 | * New cgroup inherits effective freeze counter, and | 
|---|
| 5892 | * if the parent has to be frozen, the child has too. | 
|---|
| 5893 | */ | 
|---|
| 5894 | cgrp->freezer.e_freeze = parent->freezer.e_freeze; | 
|---|
| 5895 | seqcount_init(&cgrp->freezer.freeze_seq); | 
|---|
| 5896 | if (cgrp->freezer.e_freeze) { | 
|---|
| 5897 | /* | 
|---|
| 5898 | * Set the CGRP_FREEZE flag, so when a process will be | 
|---|
| 5899 | * attached to the child cgroup, it will become frozen. | 
|---|
| 5900 | * At this point the new cgroup is unpopulated, so we can | 
|---|
| 5901 | * consider it frozen immediately. | 
|---|
| 5902 | */ | 
|---|
| 5903 | set_bit(nr: CGRP_FREEZE, addr: &cgrp->flags); | 
|---|
| 5904 | cgrp->freezer.freeze_start_nsec = ktime_get_ns(); | 
|---|
| 5905 | set_bit(nr: CGRP_FROZEN, addr: &cgrp->flags); | 
|---|
| 5906 | } | 
|---|
| 5907 |  | 
|---|
| 5908 | if (notify_on_release(cgrp: parent)) | 
|---|
| 5909 | set_bit(nr: CGRP_NOTIFY_ON_RELEASE, addr: &cgrp->flags); | 
|---|
| 5910 |  | 
|---|
| 5911 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) | 
|---|
| 5912 | set_bit(nr: CGRP_CPUSET_CLONE_CHILDREN, addr: &cgrp->flags); | 
|---|
| 5913 |  | 
|---|
| 5914 | cgrp->self.serial_nr = css_serial_nr_next++; | 
|---|
| 5915 |  | 
|---|
| 5916 | ret = blocking_notifier_call_chain_robust(nh: &cgroup_lifetime_notifier, | 
|---|
| 5917 | val_up: CGROUP_LIFETIME_ONLINE, | 
|---|
| 5918 | val_down: CGROUP_LIFETIME_OFFLINE, v: cgrp); | 
|---|
| 5919 | ret = notifier_to_errno(ret); | 
|---|
| 5920 | if (ret) | 
|---|
| 5921 | goto out_psi_free; | 
|---|
| 5922 |  | 
|---|
| 5923 | /* allocation complete, commit to creation */ | 
|---|
| 5924 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 5925 | for (i = 0; i < level; i++) { | 
|---|
| 5926 | tcgrp = cgrp->ancestors[i]; | 
|---|
| 5927 | tcgrp->nr_descendants++; | 
|---|
| 5928 |  | 
|---|
| 5929 | /* | 
|---|
| 5930 | * If the new cgroup is frozen, all ancestor cgroups get a new | 
|---|
| 5931 | * frozen descendant, but their state can't change because of | 
|---|
| 5932 | * this. | 
|---|
| 5933 | */ | 
|---|
| 5934 | if (cgrp->freezer.e_freeze) | 
|---|
| 5935 | tcgrp->freezer.nr_frozen_descendants++; | 
|---|
| 5936 | } | 
|---|
| 5937 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 5938 |  | 
|---|
| 5939 | list_add_tail_rcu(new: &cgrp->self.sibling, head: &cgroup_parent(cgrp)->self.children); | 
|---|
| 5940 | atomic_inc(v: &root->nr_cgrps); | 
|---|
| 5941 | cgroup_get_live(cgrp: parent); | 
|---|
| 5942 |  | 
|---|
| 5943 | /* | 
|---|
| 5944 | * On the default hierarchy, a child doesn't automatically inherit | 
|---|
| 5945 | * subtree_control from the parent.  Each is configured manually. | 
|---|
| 5946 | */ | 
|---|
| 5947 | if (!cgroup_on_dfl(cgrp)) | 
|---|
| 5948 | cgrp->subtree_control = cgroup_control(cgrp); | 
|---|
| 5949 |  | 
|---|
| 5950 | cgroup_propagate_control(cgrp); | 
|---|
| 5951 |  | 
|---|
| 5952 | return cgrp; | 
|---|
| 5953 |  | 
|---|
| 5954 | out_psi_free: | 
|---|
| 5955 | psi_cgroup_free(cgrp); | 
|---|
| 5956 | out_stat_exit: | 
|---|
| 5957 | css_rstat_exit(css: &cgrp->self); | 
|---|
| 5958 | out_kernfs_remove: | 
|---|
| 5959 | kernfs_remove(kn: cgrp->kn); | 
|---|
| 5960 | out_cancel_ref: | 
|---|
| 5961 | percpu_ref_exit(ref: &cgrp->self.refcnt); | 
|---|
| 5962 | out_free_cgrp: | 
|---|
| 5963 | kfree(objp: cgrp); | 
|---|
| 5964 | return ERR_PTR(error: ret); | 
|---|
| 5965 | } | 
|---|
| 5966 |  | 
|---|
| 5967 | static bool cgroup_check_hierarchy_limits(struct cgroup *parent) | 
|---|
| 5968 | { | 
|---|
| 5969 | struct cgroup *cgroup; | 
|---|
| 5970 | int ret = false; | 
|---|
| 5971 | int level = 0; | 
|---|
| 5972 |  | 
|---|
| 5973 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 5974 |  | 
|---|
| 5975 | for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgrp: cgroup)) { | 
|---|
| 5976 | if (cgroup->nr_descendants >= cgroup->max_descendants) | 
|---|
| 5977 | goto fail; | 
|---|
| 5978 |  | 
|---|
| 5979 | if (level >= cgroup->max_depth) | 
|---|
| 5980 | goto fail; | 
|---|
| 5981 |  | 
|---|
| 5982 | level++; | 
|---|
| 5983 | } | 
|---|
| 5984 |  | 
|---|
| 5985 | ret = true; | 
|---|
| 5986 | fail: | 
|---|
| 5987 | return ret; | 
|---|
| 5988 | } | 
|---|
| 5989 |  | 
|---|
| 5990 | int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode) | 
|---|
| 5991 | { | 
|---|
| 5992 | struct cgroup *parent, *cgrp; | 
|---|
| 5993 | int ret; | 
|---|
| 5994 |  | 
|---|
| 5995 | /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */ | 
|---|
| 5996 | if (strchr(name, '\n')) | 
|---|
| 5997 | return -EINVAL; | 
|---|
| 5998 |  | 
|---|
| 5999 | parent = cgroup_kn_lock_live(kn: parent_kn, drain_offline: false); | 
|---|
| 6000 | if (!parent) | 
|---|
| 6001 | return -ENODEV; | 
|---|
| 6002 |  | 
|---|
| 6003 | if (!cgroup_check_hierarchy_limits(parent)) { | 
|---|
| 6004 | ret = -EAGAIN; | 
|---|
| 6005 | goto out_unlock; | 
|---|
| 6006 | } | 
|---|
| 6007 |  | 
|---|
| 6008 | cgrp = cgroup_create(parent, name, mode); | 
|---|
| 6009 | if (IS_ERR(ptr: cgrp)) { | 
|---|
| 6010 | ret = PTR_ERR(ptr: cgrp); | 
|---|
| 6011 | goto out_unlock; | 
|---|
| 6012 | } | 
|---|
| 6013 |  | 
|---|
| 6014 | /* | 
|---|
| 6015 | * This extra ref will be put in css_free_rwork_fn() and guarantees | 
|---|
| 6016 | * that @cgrp->kn is always accessible. | 
|---|
| 6017 | */ | 
|---|
| 6018 | kernfs_get(kn: cgrp->kn); | 
|---|
| 6019 |  | 
|---|
| 6020 | ret = css_populate_dir(css: &cgrp->self); | 
|---|
| 6021 | if (ret) | 
|---|
| 6022 | goto out_destroy; | 
|---|
| 6023 |  | 
|---|
| 6024 | ret = cgroup_apply_control_enable(cgrp); | 
|---|
| 6025 | if (ret) | 
|---|
| 6026 | goto out_destroy; | 
|---|
| 6027 |  | 
|---|
| 6028 | TRACE_CGROUP_PATH(mkdir, cgrp); | 
|---|
| 6029 |  | 
|---|
| 6030 | /* let's create and online css's */ | 
|---|
| 6031 | kernfs_activate(kn: cgrp->kn); | 
|---|
| 6032 |  | 
|---|
| 6033 | ret = 0; | 
|---|
| 6034 | goto out_unlock; | 
|---|
| 6035 |  | 
|---|
| 6036 | out_destroy: | 
|---|
| 6037 | cgroup_destroy_locked(cgrp); | 
|---|
| 6038 | out_unlock: | 
|---|
| 6039 | cgroup_kn_unlock(kn: parent_kn); | 
|---|
| 6040 | return ret; | 
|---|
| 6041 | } | 
|---|
| 6042 |  | 
|---|
| 6043 | /* | 
|---|
| 6044 | * This is called when the refcnt of a css is confirmed to be killed. | 
|---|
| 6045 | * css_tryget_online() is now guaranteed to fail.  Tell the subsystem to | 
|---|
| 6046 | * initiate destruction and put the css ref from kill_css(). | 
|---|
| 6047 | */ | 
|---|
| 6048 | static void css_killed_work_fn(struct work_struct *work) | 
|---|
| 6049 | { | 
|---|
| 6050 | struct cgroup_subsys_state *css = | 
|---|
| 6051 | container_of(work, struct cgroup_subsys_state, destroy_work); | 
|---|
| 6052 |  | 
|---|
| 6053 | cgroup_lock(); | 
|---|
| 6054 |  | 
|---|
| 6055 | do { | 
|---|
| 6056 | offline_css(css); | 
|---|
| 6057 | css_put(css); | 
|---|
| 6058 | /* @css can't go away while we're holding cgroup_mutex */ | 
|---|
| 6059 | css = css->parent; | 
|---|
| 6060 | } while (css && atomic_dec_and_test(v: &css->online_cnt)); | 
|---|
| 6061 |  | 
|---|
| 6062 | cgroup_unlock(); | 
|---|
| 6063 | } | 
|---|
| 6064 |  | 
|---|
| 6065 | /* css kill confirmation processing requires process context, bounce */ | 
|---|
| 6066 | static void css_killed_ref_fn(struct percpu_ref *ref) | 
|---|
| 6067 | { | 
|---|
| 6068 | struct cgroup_subsys_state *css = | 
|---|
| 6069 | container_of(ref, struct cgroup_subsys_state, refcnt); | 
|---|
| 6070 |  | 
|---|
| 6071 | if (atomic_dec_and_test(v: &css->online_cnt)) { | 
|---|
| 6072 | INIT_WORK(&css->destroy_work, css_killed_work_fn); | 
|---|
| 6073 | queue_work(wq: cgroup_offline_wq, work: &css->destroy_work); | 
|---|
| 6074 | } | 
|---|
| 6075 | } | 
|---|
| 6076 |  | 
|---|
| 6077 | /** | 
|---|
| 6078 | * kill_css - destroy a css | 
|---|
| 6079 | * @css: css to destroy | 
|---|
| 6080 | * | 
|---|
| 6081 | * This function initiates destruction of @css by removing cgroup interface | 
|---|
| 6082 | * files and putting its base reference.  ->css_offline() will be invoked | 
|---|
| 6083 | * asynchronously once css_tryget_online() is guaranteed to fail and when | 
|---|
| 6084 | * the reference count reaches zero, @css will be released. | 
|---|
| 6085 | */ | 
|---|
| 6086 | static void kill_css(struct cgroup_subsys_state *css) | 
|---|
| 6087 | { | 
|---|
| 6088 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 6089 |  | 
|---|
| 6090 | if (css->flags & CSS_DYING) | 
|---|
| 6091 | return; | 
|---|
| 6092 |  | 
|---|
| 6093 | /* | 
|---|
| 6094 | * Call css_killed(), if defined, before setting the CSS_DYING flag | 
|---|
| 6095 | */ | 
|---|
| 6096 | if (css->ss->css_killed) | 
|---|
| 6097 | css->ss->css_killed(css); | 
|---|
| 6098 |  | 
|---|
| 6099 | css->flags |= CSS_DYING; | 
|---|
| 6100 |  | 
|---|
| 6101 | /* | 
|---|
| 6102 | * This must happen before css is disassociated with its cgroup. | 
|---|
| 6103 | * See seq_css() for details. | 
|---|
| 6104 | */ | 
|---|
| 6105 | css_clear_dir(css); | 
|---|
| 6106 |  | 
|---|
| 6107 | /* | 
|---|
| 6108 | * Killing would put the base ref, but we need to keep it alive | 
|---|
| 6109 | * until after ->css_offline(). | 
|---|
| 6110 | */ | 
|---|
| 6111 | css_get(css); | 
|---|
| 6112 |  | 
|---|
| 6113 | /* | 
|---|
| 6114 | * cgroup core guarantees that, by the time ->css_offline() is | 
|---|
| 6115 | * invoked, no new css reference will be given out via | 
|---|
| 6116 | * css_tryget_online().  We can't simply call percpu_ref_kill() and | 
|---|
| 6117 | * proceed to offlining css's because percpu_ref_kill() doesn't | 
|---|
| 6118 | * guarantee that the ref is seen as killed on all CPUs on return. | 
|---|
| 6119 | * | 
|---|
| 6120 | * Use percpu_ref_kill_and_confirm() to get notifications as each | 
|---|
| 6121 | * css is confirmed to be seen as killed on all CPUs. | 
|---|
| 6122 | */ | 
|---|
| 6123 | percpu_ref_kill_and_confirm(ref: &css->refcnt, confirm_kill: css_killed_ref_fn); | 
|---|
| 6124 | } | 
|---|
| 6125 |  | 
|---|
| 6126 | /** | 
|---|
| 6127 | * cgroup_destroy_locked - the first stage of cgroup destruction | 
|---|
| 6128 | * @cgrp: cgroup to be destroyed | 
|---|
| 6129 | * | 
|---|
| 6130 | * css's make use of percpu refcnts whose killing latency shouldn't be | 
|---|
| 6131 | * exposed to userland and are RCU protected.  Also, cgroup core needs to | 
|---|
| 6132 | * guarantee that css_tryget_online() won't succeed by the time | 
|---|
| 6133 | * ->css_offline() is invoked.  To satisfy all the requirements, | 
|---|
| 6134 | * destruction is implemented in the following two steps. | 
|---|
| 6135 | * | 
|---|
| 6136 | * s1. Verify @cgrp can be destroyed and mark it dying.  Remove all | 
|---|
| 6137 | *     userland visible parts and start killing the percpu refcnts of | 
|---|
| 6138 | *     css's.  Set up so that the next stage will be kicked off once all | 
|---|
| 6139 | *     the percpu refcnts are confirmed to be killed. | 
|---|
| 6140 | * | 
|---|
| 6141 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the | 
|---|
| 6142 | *     rest of destruction.  Once all cgroup references are gone, the | 
|---|
| 6143 | *     cgroup is RCU-freed. | 
|---|
| 6144 | * | 
|---|
| 6145 | * This function implements s1.  After this step, @cgrp is gone as far as | 
|---|
| 6146 | * the userland is concerned and a new cgroup with the same name may be | 
|---|
| 6147 | * created.  As cgroup doesn't care about the names internally, this | 
|---|
| 6148 | * doesn't cause any problem. | 
|---|
| 6149 | */ | 
|---|
| 6150 | static int cgroup_destroy_locked(struct cgroup *cgrp) | 
|---|
| 6151 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) | 
|---|
| 6152 | { | 
|---|
| 6153 | struct cgroup *tcgrp, *parent = cgroup_parent(cgrp); | 
|---|
| 6154 | struct cgroup_subsys_state *css; | 
|---|
| 6155 | struct cgrp_cset_link *link; | 
|---|
| 6156 | int ssid, ret; | 
|---|
| 6157 |  | 
|---|
| 6158 | lockdep_assert_held(&cgroup_mutex); | 
|---|
| 6159 |  | 
|---|
| 6160 | /* | 
|---|
| 6161 | * Only migration can raise populated from zero and we're already | 
|---|
| 6162 | * holding cgroup_mutex. | 
|---|
| 6163 | */ | 
|---|
| 6164 | if (cgroup_is_populated(cgrp)) | 
|---|
| 6165 | return -EBUSY; | 
|---|
| 6166 |  | 
|---|
| 6167 | /* | 
|---|
| 6168 | * Make sure there's no live children.  We can't test emptiness of | 
|---|
| 6169 | * ->self.children as dead children linger on it while being | 
|---|
| 6170 | * drained; otherwise, "rmdir parent/child parent" may fail. | 
|---|
| 6171 | */ | 
|---|
| 6172 | if (css_has_online_children(css: &cgrp->self)) | 
|---|
| 6173 | return -EBUSY; | 
|---|
| 6174 |  | 
|---|
| 6175 | /* | 
|---|
| 6176 | * Mark @cgrp and the associated csets dead.  The former prevents | 
|---|
| 6177 | * further task migration and child creation by disabling | 
|---|
| 6178 | * cgroup_kn_lock_live().  The latter makes the csets ignored by | 
|---|
| 6179 | * the migration path. | 
|---|
| 6180 | */ | 
|---|
| 6181 | cgrp->self.flags &= ~CSS_ONLINE; | 
|---|
| 6182 |  | 
|---|
| 6183 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 6184 | list_for_each_entry(link, &cgrp->cset_links, cset_link) | 
|---|
| 6185 | link->cset->dead = true; | 
|---|
| 6186 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 6187 |  | 
|---|
| 6188 | /* initiate massacre of all css's */ | 
|---|
| 6189 | for_each_css(css, ssid, cgrp) | 
|---|
| 6190 | kill_css(css); | 
|---|
| 6191 |  | 
|---|
| 6192 | /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */ | 
|---|
| 6193 | css_clear_dir(css: &cgrp->self); | 
|---|
| 6194 | kernfs_remove(kn: cgrp->kn); | 
|---|
| 6195 |  | 
|---|
| 6196 | if (cgroup_is_threaded(cgrp)) | 
|---|
| 6197 | parent->nr_threaded_children--; | 
|---|
| 6198 |  | 
|---|
| 6199 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 6200 | for (tcgrp = parent; tcgrp; tcgrp = cgroup_parent(cgrp: tcgrp)) { | 
|---|
| 6201 | tcgrp->nr_descendants--; | 
|---|
| 6202 | tcgrp->nr_dying_descendants++; | 
|---|
| 6203 | /* | 
|---|
| 6204 | * If the dying cgroup is frozen, decrease frozen descendants | 
|---|
| 6205 | * counters of ancestor cgroups. | 
|---|
| 6206 | */ | 
|---|
| 6207 | if (test_bit(CGRP_FROZEN, &cgrp->flags)) | 
|---|
| 6208 | tcgrp->freezer.nr_frozen_descendants--; | 
|---|
| 6209 | } | 
|---|
| 6210 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 6211 |  | 
|---|
| 6212 | cgroup1_check_for_release(cgrp: parent); | 
|---|
| 6213 |  | 
|---|
| 6214 | ret = blocking_notifier_call_chain(nh: &cgroup_lifetime_notifier, | 
|---|
| 6215 | val: CGROUP_LIFETIME_OFFLINE, v: cgrp); | 
|---|
| 6216 | WARN_ON_ONCE(notifier_to_errno(ret)); | 
|---|
| 6217 |  | 
|---|
| 6218 | /* put the base reference */ | 
|---|
| 6219 | percpu_ref_kill(ref: &cgrp->self.refcnt); | 
|---|
| 6220 |  | 
|---|
| 6221 | return 0; | 
|---|
| 6222 | }; | 
|---|
| 6223 |  | 
|---|
| 6224 | int cgroup_rmdir(struct kernfs_node *kn) | 
|---|
| 6225 | { | 
|---|
| 6226 | struct cgroup *cgrp; | 
|---|
| 6227 | int ret = 0; | 
|---|
| 6228 |  | 
|---|
| 6229 | cgrp = cgroup_kn_lock_live(kn, drain_offline: false); | 
|---|
| 6230 | if (!cgrp) | 
|---|
| 6231 | return 0; | 
|---|
| 6232 |  | 
|---|
| 6233 | ret = cgroup_destroy_locked(cgrp); | 
|---|
| 6234 | if (!ret) | 
|---|
| 6235 | TRACE_CGROUP_PATH(rmdir, cgrp); | 
|---|
| 6236 |  | 
|---|
| 6237 | cgroup_kn_unlock(kn); | 
|---|
| 6238 | return ret; | 
|---|
| 6239 | } | 
|---|
| 6240 |  | 
|---|
| 6241 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { | 
|---|
| 6242 | .show_options		= cgroup_show_options, | 
|---|
| 6243 | .mkdir			= cgroup_mkdir, | 
|---|
| 6244 | .rmdir			= cgroup_rmdir, | 
|---|
| 6245 | .show_path		= cgroup_show_path, | 
|---|
| 6246 | }; | 
|---|
| 6247 |  | 
|---|
| 6248 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) | 
|---|
| 6249 | { | 
|---|
| 6250 | struct cgroup_subsys_state *css; | 
|---|
| 6251 |  | 
|---|
| 6252 | pr_debug( "Initializing cgroup subsys %s\n", ss->name); | 
|---|
| 6253 |  | 
|---|
| 6254 | cgroup_lock(); | 
|---|
| 6255 |  | 
|---|
| 6256 | idr_init(idr: &ss->css_idr); | 
|---|
| 6257 | INIT_LIST_HEAD(list: &ss->cfts); | 
|---|
| 6258 |  | 
|---|
| 6259 | /* Create the root cgroup state for this subsystem */ | 
|---|
| 6260 | ss->root = &cgrp_dfl_root; | 
|---|
| 6261 | css = ss->css_alloc(NULL); | 
|---|
| 6262 | /* We don't handle early failures gracefully */ | 
|---|
| 6263 | BUG_ON(IS_ERR(css)); | 
|---|
| 6264 | init_and_link_css(css, ss, cgrp: &cgrp_dfl_root.cgrp); | 
|---|
| 6265 |  | 
|---|
| 6266 | /* | 
|---|
| 6267 | * Root csses are never destroyed and we can't initialize | 
|---|
| 6268 | * percpu_ref during early init.  Disable refcnting. | 
|---|
| 6269 | */ | 
|---|
| 6270 | css->flags |= CSS_NO_REF; | 
|---|
| 6271 |  | 
|---|
| 6272 | if (early) { | 
|---|
| 6273 | /* allocation can't be done safely during early init */ | 
|---|
| 6274 | css->id = 1; | 
|---|
| 6275 | } else { | 
|---|
| 6276 | css->id = cgroup_idr_alloc(idr: &ss->css_idr, ptr: css, start: 1, end: 2, GFP_KERNEL); | 
|---|
| 6277 | BUG_ON(css->id < 0); | 
|---|
| 6278 |  | 
|---|
| 6279 | BUG_ON(ss_rstat_init(ss)); | 
|---|
| 6280 | BUG_ON(css_rstat_init(css)); | 
|---|
| 6281 | } | 
|---|
| 6282 |  | 
|---|
| 6283 | /* Update the init_css_set to contain a subsys | 
|---|
| 6284 | * pointer to this state - since the subsystem is | 
|---|
| 6285 | * newly registered, all tasks and hence the | 
|---|
| 6286 | * init_css_set is in the subsystem's root cgroup. */ | 
|---|
| 6287 | init_css_set.subsys[ss->id] = css; | 
|---|
| 6288 |  | 
|---|
| 6289 | have_fork_callback |= (bool)ss->fork << ss->id; | 
|---|
| 6290 | have_exit_callback |= (bool)ss->exit << ss->id; | 
|---|
| 6291 | have_release_callback |= (bool)ss->release << ss->id; | 
|---|
| 6292 | have_canfork_callback |= (bool)ss->can_fork << ss->id; | 
|---|
| 6293 |  | 
|---|
| 6294 | /* At system boot, before all subsystems have been | 
|---|
| 6295 | * registered, no tasks have been forked, so we don't | 
|---|
| 6296 | * need to invoke fork callbacks here. */ | 
|---|
| 6297 | BUG_ON(!list_empty(&init_task.tasks)); | 
|---|
| 6298 |  | 
|---|
| 6299 | BUG_ON(online_css(css)); | 
|---|
| 6300 |  | 
|---|
| 6301 | cgroup_unlock(); | 
|---|
| 6302 | } | 
|---|
| 6303 |  | 
|---|
| 6304 | /** | 
|---|
| 6305 | * cgroup_init_early - cgroup initialization at system boot | 
|---|
| 6306 | * | 
|---|
| 6307 | * Initialize cgroups at system boot, and initialize any | 
|---|
| 6308 | * subsystems that request early init. | 
|---|
| 6309 | */ | 
|---|
| 6310 | int __init cgroup_init_early(void) | 
|---|
| 6311 | { | 
|---|
| 6312 | static struct cgroup_fs_context __initdata ctx; | 
|---|
| 6313 | struct cgroup_subsys *ss; | 
|---|
| 6314 | int i; | 
|---|
| 6315 |  | 
|---|
| 6316 | ctx.root = &cgrp_dfl_root; | 
|---|
| 6317 | init_cgroup_root(ctx: &ctx); | 
|---|
| 6318 | cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF; | 
|---|
| 6319 |  | 
|---|
| 6320 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); | 
|---|
| 6321 |  | 
|---|
| 6322 | for_each_subsys(ss, i) { | 
|---|
| 6323 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, | 
|---|
| 6324 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n", | 
|---|
| 6325 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, | 
|---|
| 6326 | ss->id, ss->name); | 
|---|
| 6327 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, | 
|---|
| 6328 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); | 
|---|
| 6329 | WARN(ss->early_init && ss->css_rstat_flush, | 
|---|
| 6330 | "cgroup rstat cannot be used with early init subsystem\n"); | 
|---|
| 6331 |  | 
|---|
| 6332 | ss->id = i; | 
|---|
| 6333 | ss->name = cgroup_subsys_name[i]; | 
|---|
| 6334 | if (!ss->legacy_name) | 
|---|
| 6335 | ss->legacy_name = cgroup_subsys_name[i]; | 
|---|
| 6336 |  | 
|---|
| 6337 | if (ss->early_init) | 
|---|
| 6338 | cgroup_init_subsys(ss, early: true); | 
|---|
| 6339 | } | 
|---|
| 6340 | return 0; | 
|---|
| 6341 | } | 
|---|
| 6342 |  | 
|---|
| 6343 | /** | 
|---|
| 6344 | * cgroup_init - cgroup initialization | 
|---|
| 6345 | * | 
|---|
| 6346 | * Register cgroup filesystem and /proc file, and initialize | 
|---|
| 6347 | * any subsystems that didn't request early init. | 
|---|
| 6348 | */ | 
|---|
| 6349 | int __init cgroup_init(void) | 
|---|
| 6350 | { | 
|---|
| 6351 | struct cgroup_subsys *ss; | 
|---|
| 6352 | int ssid; | 
|---|
| 6353 |  | 
|---|
| 6354 | BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16); | 
|---|
| 6355 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); | 
|---|
| 6356 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_psi_files)); | 
|---|
| 6357 | BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files)); | 
|---|
| 6358 |  | 
|---|
| 6359 | BUG_ON(ss_rstat_init(NULL)); | 
|---|
| 6360 |  | 
|---|
| 6361 | get_user_ns(ns: init_cgroup_ns.user_ns); | 
|---|
| 6362 |  | 
|---|
| 6363 | cgroup_lock(); | 
|---|
| 6364 |  | 
|---|
| 6365 | /* | 
|---|
| 6366 | * Add init_css_set to the hash table so that dfl_root can link to | 
|---|
| 6367 | * it during init. | 
|---|
| 6368 | */ | 
|---|
| 6369 | hash_add(css_set_table, &init_css_set.hlist, | 
|---|
| 6370 | css_set_hash(init_css_set.subsys)); | 
|---|
| 6371 |  | 
|---|
| 6372 | cgroup_bpf_lifetime_notifier_init(); | 
|---|
| 6373 |  | 
|---|
| 6374 | BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0)); | 
|---|
| 6375 |  | 
|---|
| 6376 | cgroup_unlock(); | 
|---|
| 6377 |  | 
|---|
| 6378 | for_each_subsys(ss, ssid) { | 
|---|
| 6379 | if (ss->early_init) { | 
|---|
| 6380 | struct cgroup_subsys_state *css = | 
|---|
| 6381 | init_css_set.subsys[ss->id]; | 
|---|
| 6382 |  | 
|---|
| 6383 | css->id = cgroup_idr_alloc(idr: &ss->css_idr, ptr: css, start: 1, end: 2, | 
|---|
| 6384 | GFP_KERNEL); | 
|---|
| 6385 | BUG_ON(css->id < 0); | 
|---|
| 6386 | } else { | 
|---|
| 6387 | cgroup_init_subsys(ss, early: false); | 
|---|
| 6388 | } | 
|---|
| 6389 |  | 
|---|
| 6390 | list_add_tail(new: &init_css_set.e_cset_node[ssid], | 
|---|
| 6391 | head: &cgrp_dfl_root.cgrp.e_csets[ssid]); | 
|---|
| 6392 |  | 
|---|
| 6393 | /* | 
|---|
| 6394 | * Setting dfl_root subsys_mask needs to consider the | 
|---|
| 6395 | * disabled flag and cftype registration needs kmalloc, | 
|---|
| 6396 | * both of which aren't available during early_init. | 
|---|
| 6397 | */ | 
|---|
| 6398 | if (!cgroup_ssid_enabled(ssid)) | 
|---|
| 6399 | continue; | 
|---|
| 6400 |  | 
|---|
| 6401 | if (cgroup1_ssid_disabled(ssid)) | 
|---|
| 6402 | pr_info( "Disabling %s control group subsystem in v1 mounts\n", | 
|---|
| 6403 | ss->legacy_name); | 
|---|
| 6404 |  | 
|---|
| 6405 | cgrp_dfl_root.subsys_mask |= 1 << ss->id; | 
|---|
| 6406 |  | 
|---|
| 6407 | /* implicit controllers must be threaded too */ | 
|---|
| 6408 | WARN_ON(ss->implicit_on_dfl && !ss->threaded); | 
|---|
| 6409 |  | 
|---|
| 6410 | if (ss->implicit_on_dfl) | 
|---|
| 6411 | cgrp_dfl_implicit_ss_mask |= 1 << ss->id; | 
|---|
| 6412 | else if (!ss->dfl_cftypes) | 
|---|
| 6413 | cgrp_dfl_inhibit_ss_mask |= 1 << ss->id; | 
|---|
| 6414 |  | 
|---|
| 6415 | if (ss->threaded) | 
|---|
| 6416 | cgrp_dfl_threaded_ss_mask |= 1 << ss->id; | 
|---|
| 6417 |  | 
|---|
| 6418 | if (ss->dfl_cftypes == ss->legacy_cftypes) { | 
|---|
| 6419 | WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes)); | 
|---|
| 6420 | } else { | 
|---|
| 6421 | WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes)); | 
|---|
| 6422 | WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes)); | 
|---|
| 6423 | } | 
|---|
| 6424 |  | 
|---|
| 6425 | if (ss->bind) | 
|---|
| 6426 | ss->bind(init_css_set.subsys[ssid]); | 
|---|
| 6427 |  | 
|---|
| 6428 | cgroup_lock(); | 
|---|
| 6429 | css_populate_dir(css: init_css_set.subsys[ssid]); | 
|---|
| 6430 | cgroup_unlock(); | 
|---|
| 6431 | } | 
|---|
| 6432 |  | 
|---|
| 6433 | /* init_css_set.subsys[] has been updated, re-hash */ | 
|---|
| 6434 | hash_del(node: &init_css_set.hlist); | 
|---|
| 6435 | hash_add(css_set_table, &init_css_set.hlist, | 
|---|
| 6436 | css_set_hash(init_css_set.subsys)); | 
|---|
| 6437 |  | 
|---|
| 6438 | WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup")); | 
|---|
| 6439 | WARN_ON(register_filesystem(&cgroup_fs_type)); | 
|---|
| 6440 | WARN_ON(register_filesystem(&cgroup2_fs_type)); | 
|---|
| 6441 | WARN_ON(!proc_create_single( "cgroups", 0, NULL, proc_cgroupstats_show)); | 
|---|
| 6442 | #ifdef CONFIG_CPUSETS_V1 | 
|---|
| 6443 | WARN_ON(register_filesystem(&cpuset_fs_type)); | 
|---|
| 6444 | #endif | 
|---|
| 6445 |  | 
|---|
| 6446 | ns_tree_add(&init_cgroup_ns); | 
|---|
| 6447 | return 0; | 
|---|
| 6448 | } | 
|---|
| 6449 |  | 
|---|
| 6450 | static int __init cgroup_wq_init(void) | 
|---|
| 6451 | { | 
|---|
| 6452 | /* | 
|---|
| 6453 | * There isn't much point in executing destruction path in | 
|---|
| 6454 | * parallel.  Good chunk is serialized with cgroup_mutex anyway. | 
|---|
| 6455 | * Use 1 for @max_active. | 
|---|
| 6456 | * | 
|---|
| 6457 | * We would prefer to do this in cgroup_init() above, but that | 
|---|
| 6458 | * is called before init_workqueues(): so leave this until after. | 
|---|
| 6459 | */ | 
|---|
| 6460 | cgroup_offline_wq = alloc_workqueue( "cgroup_offline", WQ_PERCPU, 1); | 
|---|
| 6461 | BUG_ON(!cgroup_offline_wq); | 
|---|
| 6462 |  | 
|---|
| 6463 | cgroup_release_wq = alloc_workqueue( "cgroup_release", WQ_PERCPU, 1); | 
|---|
| 6464 | BUG_ON(!cgroup_release_wq); | 
|---|
| 6465 |  | 
|---|
| 6466 | cgroup_free_wq = alloc_workqueue( "cgroup_free", WQ_PERCPU, 1); | 
|---|
| 6467 | BUG_ON(!cgroup_free_wq); | 
|---|
| 6468 | return 0; | 
|---|
| 6469 | } | 
|---|
| 6470 | core_initcall(cgroup_wq_init); | 
|---|
| 6471 |  | 
|---|
| 6472 | void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen) | 
|---|
| 6473 | { | 
|---|
| 6474 | struct kernfs_node *kn; | 
|---|
| 6475 |  | 
|---|
| 6476 | kn = kernfs_find_and_get_node_by_id(root: cgrp_dfl_root.kf_root, id); | 
|---|
| 6477 | if (!kn) | 
|---|
| 6478 | return; | 
|---|
| 6479 | kernfs_path(kn, buf, buflen); | 
|---|
| 6480 | kernfs_put(kn); | 
|---|
| 6481 | } | 
|---|
| 6482 |  | 
|---|
| 6483 | /* | 
|---|
| 6484 | * __cgroup_get_from_id : get the cgroup associated with cgroup id | 
|---|
| 6485 | * @id: cgroup id | 
|---|
| 6486 | * On success return the cgrp or ERR_PTR on failure | 
|---|
| 6487 | * There are no cgroup NS restrictions. | 
|---|
| 6488 | */ | 
|---|
| 6489 | struct cgroup *__cgroup_get_from_id(u64 id) | 
|---|
| 6490 | { | 
|---|
| 6491 | struct kernfs_node *kn; | 
|---|
| 6492 | struct cgroup *cgrp; | 
|---|
| 6493 |  | 
|---|
| 6494 | kn = kernfs_find_and_get_node_by_id(root: cgrp_dfl_root.kf_root, id); | 
|---|
| 6495 | if (!kn) | 
|---|
| 6496 | return ERR_PTR(error: -ENOENT); | 
|---|
| 6497 |  | 
|---|
| 6498 | if (kernfs_type(kn) != KERNFS_DIR) { | 
|---|
| 6499 | kernfs_put(kn); | 
|---|
| 6500 | return ERR_PTR(error: -ENOENT); | 
|---|
| 6501 | } | 
|---|
| 6502 |  | 
|---|
| 6503 | rcu_read_lock(); | 
|---|
| 6504 |  | 
|---|
| 6505 | cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); | 
|---|
| 6506 | if (cgrp && !cgroup_tryget(cgrp)) | 
|---|
| 6507 | cgrp = NULL; | 
|---|
| 6508 |  | 
|---|
| 6509 | rcu_read_unlock(); | 
|---|
| 6510 | kernfs_put(kn); | 
|---|
| 6511 |  | 
|---|
| 6512 | if (!cgrp) | 
|---|
| 6513 | return ERR_PTR(error: -ENOENT); | 
|---|
| 6514 | return cgrp; | 
|---|
| 6515 | } | 
|---|
| 6516 |  | 
|---|
| 6517 | /* | 
|---|
| 6518 | * cgroup_get_from_id : get the cgroup associated with cgroup id | 
|---|
| 6519 | * @id: cgroup id | 
|---|
| 6520 | * On success return the cgrp or ERR_PTR on failure | 
|---|
| 6521 | * Only cgroups within current task's cgroup NS are valid. | 
|---|
| 6522 | */ | 
|---|
| 6523 | struct cgroup *cgroup_get_from_id(u64 id) | 
|---|
| 6524 | { | 
|---|
| 6525 | struct cgroup *cgrp, *root_cgrp; | 
|---|
| 6526 |  | 
|---|
| 6527 | cgrp = __cgroup_get_from_id(id); | 
|---|
| 6528 | if (IS_ERR(ptr: cgrp)) | 
|---|
| 6529 | return cgrp; | 
|---|
| 6530 |  | 
|---|
| 6531 | root_cgrp = current_cgns_cgroup_dfl(); | 
|---|
| 6532 | if (!cgroup_is_descendant(cgrp, ancestor: root_cgrp)) { | 
|---|
| 6533 | cgroup_put(cgrp); | 
|---|
| 6534 | return ERR_PTR(error: -ENOENT); | 
|---|
| 6535 | } | 
|---|
| 6536 |  | 
|---|
| 6537 | return cgrp; | 
|---|
| 6538 | } | 
|---|
| 6539 | EXPORT_SYMBOL_GPL(cgroup_get_from_id); | 
|---|
| 6540 |  | 
|---|
| 6541 | /* | 
|---|
| 6542 | * proc_cgroup_show() | 
|---|
| 6543 | *  - Print task's cgroup paths into seq_file, one line for each hierarchy | 
|---|
| 6544 | *  - Used for /proc/<pid>/cgroup. | 
|---|
| 6545 | */ | 
|---|
| 6546 | int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, | 
|---|
| 6547 | struct pid *pid, struct task_struct *tsk) | 
|---|
| 6548 | { | 
|---|
| 6549 | char *buf; | 
|---|
| 6550 | int retval; | 
|---|
| 6551 | struct cgroup_root *root; | 
|---|
| 6552 |  | 
|---|
| 6553 | retval = -ENOMEM; | 
|---|
| 6554 | buf = kmalloc(PATH_MAX, GFP_KERNEL); | 
|---|
| 6555 | if (!buf) | 
|---|
| 6556 | goto out; | 
|---|
| 6557 |  | 
|---|
| 6558 | rcu_read_lock(); | 
|---|
| 6559 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 6560 |  | 
|---|
| 6561 | for_each_root(root) { | 
|---|
| 6562 | struct cgroup_subsys *ss; | 
|---|
| 6563 | struct cgroup *cgrp; | 
|---|
| 6564 | int ssid, count = 0; | 
|---|
| 6565 |  | 
|---|
| 6566 | if (root == &cgrp_dfl_root && !READ_ONCE(cgrp_dfl_visible)) | 
|---|
| 6567 | continue; | 
|---|
| 6568 |  | 
|---|
| 6569 | cgrp = task_cgroup_from_root(task: tsk, root); | 
|---|
| 6570 | /* The root has already been unmounted. */ | 
|---|
| 6571 | if (!cgrp) | 
|---|
| 6572 | continue; | 
|---|
| 6573 |  | 
|---|
| 6574 | seq_printf(m, fmt: "%d:", root->hierarchy_id); | 
|---|
| 6575 | if (root != &cgrp_dfl_root) | 
|---|
| 6576 | for_each_subsys(ss, ssid) | 
|---|
| 6577 | if (root->subsys_mask & (1 << ssid)) | 
|---|
| 6578 | seq_printf(m, fmt: "%s%s", count++ ? ",": "", | 
|---|
| 6579 | ss->legacy_name); | 
|---|
| 6580 | if (strlen(root->name)) | 
|---|
| 6581 | seq_printf(m, fmt: "%sname=%s", count ? ",": "", | 
|---|
| 6582 | root->name); | 
|---|
| 6583 | seq_putc(m, c: ':'); | 
|---|
| 6584 | /* | 
|---|
| 6585 | * On traditional hierarchies, all zombie tasks show up as | 
|---|
| 6586 | * belonging to the root cgroup.  On the default hierarchy, | 
|---|
| 6587 | * while a zombie doesn't show up in "cgroup.procs" and | 
|---|
| 6588 | * thus can't be migrated, its /proc/PID/cgroup keeps | 
|---|
| 6589 | * reporting the cgroup it belonged to before exiting.  If | 
|---|
| 6590 | * the cgroup is removed before the zombie is reaped, | 
|---|
| 6591 | * " (deleted)" is appended to the cgroup path. | 
|---|
| 6592 | */ | 
|---|
| 6593 | if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) { | 
|---|
| 6594 | retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX, | 
|---|
| 6595 | current->nsproxy->cgroup_ns); | 
|---|
| 6596 | if (retval == -E2BIG) | 
|---|
| 6597 | retval = -ENAMETOOLONG; | 
|---|
| 6598 | if (retval < 0) | 
|---|
| 6599 | goto out_unlock; | 
|---|
| 6600 |  | 
|---|
| 6601 | seq_puts(m, s: buf); | 
|---|
| 6602 | } else { | 
|---|
| 6603 | seq_puts(m, s: "/"); | 
|---|
| 6604 | } | 
|---|
| 6605 |  | 
|---|
| 6606 | if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp)) | 
|---|
| 6607 | seq_puts(m, s: " (deleted)\n"); | 
|---|
| 6608 | else | 
|---|
| 6609 | seq_putc(m, c: '\n'); | 
|---|
| 6610 | } | 
|---|
| 6611 |  | 
|---|
| 6612 | retval = 0; | 
|---|
| 6613 | out_unlock: | 
|---|
| 6614 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 6615 | rcu_read_unlock(); | 
|---|
| 6616 | kfree(objp: buf); | 
|---|
| 6617 | out: | 
|---|
| 6618 | return retval; | 
|---|
| 6619 | } | 
|---|
| 6620 |  | 
|---|
| 6621 | /** | 
|---|
| 6622 | * cgroup_fork - initialize cgroup related fields during copy_process() | 
|---|
| 6623 | * @child: pointer to task_struct of forking parent process. | 
|---|
| 6624 | * | 
|---|
| 6625 | * A task is associated with the init_css_set until cgroup_post_fork() | 
|---|
| 6626 | * attaches it to the target css_set. | 
|---|
| 6627 | */ | 
|---|
| 6628 | void cgroup_fork(struct task_struct *child) | 
|---|
| 6629 | { | 
|---|
| 6630 | RCU_INIT_POINTER(child->cgroups, &init_css_set); | 
|---|
| 6631 | INIT_LIST_HEAD(list: &child->cg_list); | 
|---|
| 6632 | } | 
|---|
| 6633 |  | 
|---|
| 6634 | /** | 
|---|
| 6635 | * cgroup_v1v2_get_from_file - get a cgroup pointer from a file pointer | 
|---|
| 6636 | * @f: file corresponding to cgroup_dir | 
|---|
| 6637 | * | 
|---|
| 6638 | * Find the cgroup from a file pointer associated with a cgroup directory. | 
|---|
| 6639 | * Returns a pointer to the cgroup on success. ERR_PTR is returned if the | 
|---|
| 6640 | * cgroup cannot be found. | 
|---|
| 6641 | */ | 
|---|
| 6642 | static struct cgroup *cgroup_v1v2_get_from_file(struct file *f) | 
|---|
| 6643 | { | 
|---|
| 6644 | struct cgroup_subsys_state *css; | 
|---|
| 6645 |  | 
|---|
| 6646 | css = css_tryget_online_from_dir(dentry: f->f_path.dentry, NULL); | 
|---|
| 6647 | if (IS_ERR(ptr: css)) | 
|---|
| 6648 | return ERR_CAST(ptr: css); | 
|---|
| 6649 |  | 
|---|
| 6650 | return css->cgroup; | 
|---|
| 6651 | } | 
|---|
| 6652 |  | 
|---|
| 6653 | /** | 
|---|
| 6654 | * cgroup_get_from_file - same as cgroup_v1v2_get_from_file, but only supports | 
|---|
| 6655 | * cgroup2. | 
|---|
| 6656 | * @f: file corresponding to cgroup2_dir | 
|---|
| 6657 | */ | 
|---|
| 6658 | static struct cgroup *cgroup_get_from_file(struct file *f) | 
|---|
| 6659 | { | 
|---|
| 6660 | struct cgroup *cgrp = cgroup_v1v2_get_from_file(f); | 
|---|
| 6661 |  | 
|---|
| 6662 | if (IS_ERR(ptr: cgrp)) | 
|---|
| 6663 | return ERR_CAST(ptr: cgrp); | 
|---|
| 6664 |  | 
|---|
| 6665 | if (!cgroup_on_dfl(cgrp)) { | 
|---|
| 6666 | cgroup_put(cgrp); | 
|---|
| 6667 | return ERR_PTR(error: -EBADF); | 
|---|
| 6668 | } | 
|---|
| 6669 |  | 
|---|
| 6670 | return cgrp; | 
|---|
| 6671 | } | 
|---|
| 6672 |  | 
|---|
| 6673 | /** | 
|---|
| 6674 | * cgroup_css_set_fork - find or create a css_set for a child process | 
|---|
| 6675 | * @kargs: the arguments passed to create the child process | 
|---|
| 6676 | * | 
|---|
| 6677 | * This functions finds or creates a new css_set which the child | 
|---|
| 6678 | * process will be attached to in cgroup_post_fork(). By default, | 
|---|
| 6679 | * the child process will be given the same css_set as its parent. | 
|---|
| 6680 | * | 
|---|
| 6681 | * If CLONE_INTO_CGROUP is specified this function will try to find an | 
|---|
| 6682 | * existing css_set which includes the requested cgroup and if not create | 
|---|
| 6683 | * a new css_set that the child will be attached to later. If this function | 
|---|
| 6684 | * succeeds it will hold cgroup_threadgroup_rwsem on return. If | 
|---|
| 6685 | * CLONE_INTO_CGROUP is requested this function will grab cgroup mutex | 
|---|
| 6686 | * before grabbing cgroup_threadgroup_rwsem and will hold a reference | 
|---|
| 6687 | * to the target cgroup. | 
|---|
| 6688 | */ | 
|---|
| 6689 | static int cgroup_css_set_fork(struct kernel_clone_args *kargs) | 
|---|
| 6690 | __acquires(&cgroup_mutex) __acquires(&cgroup_threadgroup_rwsem) | 
|---|
| 6691 | { | 
|---|
| 6692 | int ret; | 
|---|
| 6693 | struct cgroup *dst_cgrp = NULL; | 
|---|
| 6694 | struct css_set *cset; | 
|---|
| 6695 | struct super_block *sb; | 
|---|
| 6696 |  | 
|---|
| 6697 | if (kargs->flags & CLONE_INTO_CGROUP) | 
|---|
| 6698 | cgroup_lock(); | 
|---|
| 6699 |  | 
|---|
| 6700 | cgroup_threadgroup_change_begin(current); | 
|---|
| 6701 |  | 
|---|
| 6702 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 6703 | cset = task_css_set(current); | 
|---|
| 6704 | get_css_set(cset); | 
|---|
| 6705 | if (kargs->cgrp) | 
|---|
| 6706 | kargs->kill_seq = kargs->cgrp->kill_seq; | 
|---|
| 6707 | else | 
|---|
| 6708 | kargs->kill_seq = cset->dfl_cgrp->kill_seq; | 
|---|
| 6709 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 6710 |  | 
|---|
| 6711 | if (!(kargs->flags & CLONE_INTO_CGROUP)) { | 
|---|
| 6712 | kargs->cset = cset; | 
|---|
| 6713 | return 0; | 
|---|
| 6714 | } | 
|---|
| 6715 |  | 
|---|
| 6716 | CLASS(fd_raw, f)(fd: kargs->cgroup); | 
|---|
| 6717 | if (fd_empty(f)) { | 
|---|
| 6718 | ret = -EBADF; | 
|---|
| 6719 | goto err; | 
|---|
| 6720 | } | 
|---|
| 6721 | sb = fd_file(f)->f_path.dentry->d_sb; | 
|---|
| 6722 |  | 
|---|
| 6723 | dst_cgrp = cgroup_get_from_file(fd_file(f)); | 
|---|
| 6724 | if (IS_ERR(ptr: dst_cgrp)) { | 
|---|
| 6725 | ret = PTR_ERR(ptr: dst_cgrp); | 
|---|
| 6726 | dst_cgrp = NULL; | 
|---|
| 6727 | goto err; | 
|---|
| 6728 | } | 
|---|
| 6729 |  | 
|---|
| 6730 | if (cgroup_is_dead(cgrp: dst_cgrp)) { | 
|---|
| 6731 | ret = -ENODEV; | 
|---|
| 6732 | goto err; | 
|---|
| 6733 | } | 
|---|
| 6734 |  | 
|---|
| 6735 | /* | 
|---|
| 6736 | * Verify that we the target cgroup is writable for us. This is | 
|---|
| 6737 | * usually done by the vfs layer but since we're not going through | 
|---|
| 6738 | * the vfs layer here we need to do it "manually". | 
|---|
| 6739 | */ | 
|---|
| 6740 | ret = cgroup_may_write(cgrp: dst_cgrp, sb); | 
|---|
| 6741 | if (ret) | 
|---|
| 6742 | goto err; | 
|---|
| 6743 |  | 
|---|
| 6744 | /* | 
|---|
| 6745 | * Spawning a task directly into a cgroup works by passing a file | 
|---|
| 6746 | * descriptor to the target cgroup directory. This can even be an O_PATH | 
|---|
| 6747 | * file descriptor. But it can never be a cgroup.procs file descriptor. | 
|---|
| 6748 | * This was done on purpose so spawning into a cgroup could be | 
|---|
| 6749 | * conceptualized as an atomic | 
|---|
| 6750 | * | 
|---|
| 6751 | *   fd = openat(dfd_cgroup, "cgroup.procs", ...); | 
|---|
| 6752 | *   write(fd, <child-pid>, ...); | 
|---|
| 6753 | * | 
|---|
| 6754 | * sequence, i.e. it's a shorthand for the caller opening and writing | 
|---|
| 6755 | * cgroup.procs of the cgroup indicated by @dfd_cgroup. This allows us | 
|---|
| 6756 | * to always use the caller's credentials. | 
|---|
| 6757 | */ | 
|---|
| 6758 | ret = cgroup_attach_permissions(src_cgrp: cset->dfl_cgrp, dst_cgrp, sb, | 
|---|
| 6759 | threadgroup: !(kargs->flags & CLONE_THREAD), | 
|---|
| 6760 | current->nsproxy->cgroup_ns); | 
|---|
| 6761 | if (ret) | 
|---|
| 6762 | goto err; | 
|---|
| 6763 |  | 
|---|
| 6764 | kargs->cset = find_css_set(old_cset: cset, cgrp: dst_cgrp); | 
|---|
| 6765 | if (!kargs->cset) { | 
|---|
| 6766 | ret = -ENOMEM; | 
|---|
| 6767 | goto err; | 
|---|
| 6768 | } | 
|---|
| 6769 |  | 
|---|
| 6770 | put_css_set(cset); | 
|---|
| 6771 | kargs->cgrp = dst_cgrp; | 
|---|
| 6772 | return ret; | 
|---|
| 6773 |  | 
|---|
| 6774 | err: | 
|---|
| 6775 | cgroup_threadgroup_change_end(current); | 
|---|
| 6776 | cgroup_unlock(); | 
|---|
| 6777 | if (dst_cgrp) | 
|---|
| 6778 | cgroup_put(cgrp: dst_cgrp); | 
|---|
| 6779 | put_css_set(cset); | 
|---|
| 6780 | if (kargs->cset) | 
|---|
| 6781 | put_css_set(cset: kargs->cset); | 
|---|
| 6782 | return ret; | 
|---|
| 6783 | } | 
|---|
| 6784 |  | 
|---|
| 6785 | /** | 
|---|
| 6786 | * cgroup_css_set_put_fork - drop references we took during fork | 
|---|
| 6787 | * @kargs: the arguments passed to create the child process | 
|---|
| 6788 | * | 
|---|
| 6789 | * Drop references to the prepared css_set and target cgroup if | 
|---|
| 6790 | * CLONE_INTO_CGROUP was requested. | 
|---|
| 6791 | */ | 
|---|
| 6792 | static void cgroup_css_set_put_fork(struct kernel_clone_args *kargs) | 
|---|
| 6793 | __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex) | 
|---|
| 6794 | { | 
|---|
| 6795 | struct cgroup *cgrp = kargs->cgrp; | 
|---|
| 6796 | struct css_set *cset = kargs->cset; | 
|---|
| 6797 |  | 
|---|
| 6798 | cgroup_threadgroup_change_end(current); | 
|---|
| 6799 |  | 
|---|
| 6800 | if (cset) { | 
|---|
| 6801 | put_css_set(cset); | 
|---|
| 6802 | kargs->cset = NULL; | 
|---|
| 6803 | } | 
|---|
| 6804 |  | 
|---|
| 6805 | if (kargs->flags & CLONE_INTO_CGROUP) { | 
|---|
| 6806 | cgroup_unlock(); | 
|---|
| 6807 | if (cgrp) { | 
|---|
| 6808 | cgroup_put(cgrp); | 
|---|
| 6809 | kargs->cgrp = NULL; | 
|---|
| 6810 | } | 
|---|
| 6811 | } | 
|---|
| 6812 | } | 
|---|
| 6813 |  | 
|---|
| 6814 | /** | 
|---|
| 6815 | * cgroup_can_fork - called on a new task before the process is exposed | 
|---|
| 6816 | * @child: the child process | 
|---|
| 6817 | * @kargs: the arguments passed to create the child process | 
|---|
| 6818 | * | 
|---|
| 6819 | * This prepares a new css_set for the child process which the child will | 
|---|
| 6820 | * be attached to in cgroup_post_fork(). | 
|---|
| 6821 | * This calls the subsystem can_fork() callbacks. If the cgroup_can_fork() | 
|---|
| 6822 | * callback returns an error, the fork aborts with that error code. This | 
|---|
| 6823 | * allows for a cgroup subsystem to conditionally allow or deny new forks. | 
|---|
| 6824 | */ | 
|---|
| 6825 | int cgroup_can_fork(struct task_struct *child, struct kernel_clone_args *kargs) | 
|---|
| 6826 | { | 
|---|
| 6827 | struct cgroup_subsys *ss; | 
|---|
| 6828 | int i, j, ret; | 
|---|
| 6829 |  | 
|---|
| 6830 | ret = cgroup_css_set_fork(kargs); | 
|---|
| 6831 | if (ret) | 
|---|
| 6832 | return ret; | 
|---|
| 6833 |  | 
|---|
| 6834 | do_each_subsys_mask(ss, i, have_canfork_callback) { | 
|---|
| 6835 | ret = ss->can_fork(child, kargs->cset); | 
|---|
| 6836 | if (ret) | 
|---|
| 6837 | goto out_revert; | 
|---|
| 6838 | } while_each_subsys_mask(); | 
|---|
| 6839 |  | 
|---|
| 6840 | return 0; | 
|---|
| 6841 |  | 
|---|
| 6842 | out_revert: | 
|---|
| 6843 | for_each_subsys(ss, j) { | 
|---|
| 6844 | if (j >= i) | 
|---|
| 6845 | break; | 
|---|
| 6846 | if (ss->cancel_fork) | 
|---|
| 6847 | ss->cancel_fork(child, kargs->cset); | 
|---|
| 6848 | } | 
|---|
| 6849 |  | 
|---|
| 6850 | cgroup_css_set_put_fork(kargs); | 
|---|
| 6851 |  | 
|---|
| 6852 | return ret; | 
|---|
| 6853 | } | 
|---|
| 6854 |  | 
|---|
| 6855 | /** | 
|---|
| 6856 | * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork() | 
|---|
| 6857 | * @child: the child process | 
|---|
| 6858 | * @kargs: the arguments passed to create the child process | 
|---|
| 6859 | * | 
|---|
| 6860 | * This calls the cancel_fork() callbacks if a fork failed *after* | 
|---|
| 6861 | * cgroup_can_fork() succeeded and cleans up references we took to | 
|---|
| 6862 | * prepare a new css_set for the child process in cgroup_can_fork(). | 
|---|
| 6863 | */ | 
|---|
| 6864 | void cgroup_cancel_fork(struct task_struct *child, | 
|---|
| 6865 | struct kernel_clone_args *kargs) | 
|---|
| 6866 | { | 
|---|
| 6867 | struct cgroup_subsys *ss; | 
|---|
| 6868 | int i; | 
|---|
| 6869 |  | 
|---|
| 6870 | for_each_subsys(ss, i) | 
|---|
| 6871 | if (ss->cancel_fork) | 
|---|
| 6872 | ss->cancel_fork(child, kargs->cset); | 
|---|
| 6873 |  | 
|---|
| 6874 | cgroup_css_set_put_fork(kargs); | 
|---|
| 6875 | } | 
|---|
| 6876 |  | 
|---|
| 6877 | /** | 
|---|
| 6878 | * cgroup_post_fork - finalize cgroup setup for the child process | 
|---|
| 6879 | * @child: the child process | 
|---|
| 6880 | * @kargs: the arguments passed to create the child process | 
|---|
| 6881 | * | 
|---|
| 6882 | * Attach the child process to its css_set calling the subsystem fork() | 
|---|
| 6883 | * callbacks. | 
|---|
| 6884 | */ | 
|---|
| 6885 | void cgroup_post_fork(struct task_struct *child, | 
|---|
| 6886 | struct kernel_clone_args *kargs) | 
|---|
| 6887 | __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex) | 
|---|
| 6888 | { | 
|---|
| 6889 | unsigned int cgrp_kill_seq = 0; | 
|---|
| 6890 | unsigned long cgrp_flags = 0; | 
|---|
| 6891 | bool kill = false; | 
|---|
| 6892 | struct cgroup_subsys *ss; | 
|---|
| 6893 | struct css_set *cset; | 
|---|
| 6894 | int i; | 
|---|
| 6895 |  | 
|---|
| 6896 | cset = kargs->cset; | 
|---|
| 6897 | kargs->cset = NULL; | 
|---|
| 6898 |  | 
|---|
| 6899 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 6900 |  | 
|---|
| 6901 | /* init tasks are special, only link regular threads */ | 
|---|
| 6902 | if (likely(child->pid)) { | 
|---|
| 6903 | if (kargs->cgrp) { | 
|---|
| 6904 | cgrp_flags = kargs->cgrp->flags; | 
|---|
| 6905 | cgrp_kill_seq = kargs->cgrp->kill_seq; | 
|---|
| 6906 | } else { | 
|---|
| 6907 | cgrp_flags = cset->dfl_cgrp->flags; | 
|---|
| 6908 | cgrp_kill_seq = cset->dfl_cgrp->kill_seq; | 
|---|
| 6909 | } | 
|---|
| 6910 |  | 
|---|
| 6911 | WARN_ON_ONCE(!list_empty(&child->cg_list)); | 
|---|
| 6912 | cset->nr_tasks++; | 
|---|
| 6913 | css_set_move_task(task: child, NULL, to_cset: cset, use_mg_tasks: false); | 
|---|
| 6914 | } else { | 
|---|
| 6915 | put_css_set(cset); | 
|---|
| 6916 | cset = NULL; | 
|---|
| 6917 | } | 
|---|
| 6918 |  | 
|---|
| 6919 | if (!(child->flags & PF_KTHREAD)) { | 
|---|
| 6920 | if (unlikely(test_bit(CGRP_FREEZE, &cgrp_flags))) { | 
|---|
| 6921 | /* | 
|---|
| 6922 | * If the cgroup has to be frozen, the new task has | 
|---|
| 6923 | * too. Let's set the JOBCTL_TRAP_FREEZE jobctl bit to | 
|---|
| 6924 | * get the task into the frozen state. | 
|---|
| 6925 | */ | 
|---|
| 6926 | spin_lock(lock: &child->sighand->siglock); | 
|---|
| 6927 | WARN_ON_ONCE(child->frozen); | 
|---|
| 6928 | child->jobctl |= JOBCTL_TRAP_FREEZE; | 
|---|
| 6929 | spin_unlock(lock: &child->sighand->siglock); | 
|---|
| 6930 |  | 
|---|
| 6931 | /* | 
|---|
| 6932 | * Calling cgroup_update_frozen() isn't required here, | 
|---|
| 6933 | * because it will be called anyway a bit later from | 
|---|
| 6934 | * do_freezer_trap(). So we avoid cgroup's transient | 
|---|
| 6935 | * switch from the frozen state and back. | 
|---|
| 6936 | */ | 
|---|
| 6937 | } | 
|---|
| 6938 |  | 
|---|
| 6939 | /* | 
|---|
| 6940 | * If the cgroup is to be killed notice it now and take the | 
|---|
| 6941 | * child down right after we finished preparing it for | 
|---|
| 6942 | * userspace. | 
|---|
| 6943 | */ | 
|---|
| 6944 | kill = kargs->kill_seq != cgrp_kill_seq; | 
|---|
| 6945 | } | 
|---|
| 6946 |  | 
|---|
| 6947 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 6948 |  | 
|---|
| 6949 | /* | 
|---|
| 6950 | * Call ss->fork().  This must happen after @child is linked on | 
|---|
| 6951 | * css_set; otherwise, @child might change state between ->fork() | 
|---|
| 6952 | * and addition to css_set. | 
|---|
| 6953 | */ | 
|---|
| 6954 | do_each_subsys_mask(ss, i, have_fork_callback) { | 
|---|
| 6955 | ss->fork(child); | 
|---|
| 6956 | } while_each_subsys_mask(); | 
|---|
| 6957 |  | 
|---|
| 6958 | /* Make the new cset the root_cset of the new cgroup namespace. */ | 
|---|
| 6959 | if (kargs->flags & CLONE_NEWCGROUP) { | 
|---|
| 6960 | struct css_set *rcset = child->nsproxy->cgroup_ns->root_cset; | 
|---|
| 6961 |  | 
|---|
| 6962 | get_css_set(cset); | 
|---|
| 6963 | child->nsproxy->cgroup_ns->root_cset = cset; | 
|---|
| 6964 | put_css_set(cset: rcset); | 
|---|
| 6965 | } | 
|---|
| 6966 |  | 
|---|
| 6967 | /* Cgroup has to be killed so take down child immediately. */ | 
|---|
| 6968 | if (unlikely(kill)) | 
|---|
| 6969 | do_send_sig_info(SIGKILL, SEND_SIG_NOINFO, p: child, type: PIDTYPE_TGID); | 
|---|
| 6970 |  | 
|---|
| 6971 | cgroup_css_set_put_fork(kargs); | 
|---|
| 6972 | } | 
|---|
| 6973 |  | 
|---|
| 6974 | /** | 
|---|
| 6975 | * cgroup_exit - detach cgroup from exiting task | 
|---|
| 6976 | * @tsk: pointer to task_struct of exiting process | 
|---|
| 6977 | * | 
|---|
| 6978 | * Description: Detach cgroup from @tsk. | 
|---|
| 6979 | * | 
|---|
| 6980 | */ | 
|---|
| 6981 | void cgroup_exit(struct task_struct *tsk) | 
|---|
| 6982 | { | 
|---|
| 6983 | struct cgroup_subsys *ss; | 
|---|
| 6984 | struct css_set *cset; | 
|---|
| 6985 | int i; | 
|---|
| 6986 |  | 
|---|
| 6987 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 6988 |  | 
|---|
| 6989 | WARN_ON_ONCE(list_empty(&tsk->cg_list)); | 
|---|
| 6990 | cset = task_css_set(task: tsk); | 
|---|
| 6991 | css_set_move_task(task: tsk, from_cset: cset, NULL, use_mg_tasks: false); | 
|---|
| 6992 | cset->nr_tasks--; | 
|---|
| 6993 | /* matches the signal->live check in css_task_iter_advance() */ | 
|---|
| 6994 | if (thread_group_leader(p: tsk) && atomic_read(v: &tsk->signal->live)) | 
|---|
| 6995 | list_add_tail(new: &tsk->cg_list, head: &cset->dying_tasks); | 
|---|
| 6996 |  | 
|---|
| 6997 | if (dl_task(p: tsk)) | 
|---|
| 6998 | dec_dl_tasks_cs(task: tsk); | 
|---|
| 6999 |  | 
|---|
| 7000 | WARN_ON_ONCE(cgroup_task_frozen(tsk)); | 
|---|
| 7001 | if (unlikely(!(tsk->flags & PF_KTHREAD) && | 
|---|
| 7002 | test_bit(CGRP_FREEZE, &task_dfl_cgroup(tsk)->flags))) | 
|---|
| 7003 | cgroup_update_frozen(cgrp: task_dfl_cgroup(task: tsk)); | 
|---|
| 7004 |  | 
|---|
| 7005 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 7006 |  | 
|---|
| 7007 | /* see cgroup_post_fork() for details */ | 
|---|
| 7008 | do_each_subsys_mask(ss, i, have_exit_callback) { | 
|---|
| 7009 | ss->exit(tsk); | 
|---|
| 7010 | } while_each_subsys_mask(); | 
|---|
| 7011 | } | 
|---|
| 7012 |  | 
|---|
| 7013 | void cgroup_release(struct task_struct *task) | 
|---|
| 7014 | { | 
|---|
| 7015 | struct cgroup_subsys *ss; | 
|---|
| 7016 | int ssid; | 
|---|
| 7017 |  | 
|---|
| 7018 | do_each_subsys_mask(ss, ssid, have_release_callback) { | 
|---|
| 7019 | ss->release(task); | 
|---|
| 7020 | } while_each_subsys_mask(); | 
|---|
| 7021 |  | 
|---|
| 7022 | if (!list_empty(head: &task->cg_list)) { | 
|---|
| 7023 | spin_lock_irq(lock: &css_set_lock); | 
|---|
| 7024 | css_set_skip_task_iters(cset: task_css_set(task), task); | 
|---|
| 7025 | list_del_init(entry: &task->cg_list); | 
|---|
| 7026 | spin_unlock_irq(lock: &css_set_lock); | 
|---|
| 7027 | } | 
|---|
| 7028 | } | 
|---|
| 7029 |  | 
|---|
| 7030 | void cgroup_free(struct task_struct *task) | 
|---|
| 7031 | { | 
|---|
| 7032 | struct css_set *cset = task_css_set(task); | 
|---|
| 7033 | put_css_set(cset); | 
|---|
| 7034 | } | 
|---|
| 7035 |  | 
|---|
| 7036 | static int __init cgroup_disable(char *str) | 
|---|
| 7037 | { | 
|---|
| 7038 | struct cgroup_subsys *ss; | 
|---|
| 7039 | char *token; | 
|---|
| 7040 | int i; | 
|---|
| 7041 |  | 
|---|
| 7042 | while ((token = strsep(&str, ",")) != NULL) { | 
|---|
| 7043 | if (!*token) | 
|---|
| 7044 | continue; | 
|---|
| 7045 |  | 
|---|
| 7046 | for_each_subsys(ss, i) { | 
|---|
| 7047 | if (strcmp(token, ss->name) && | 
|---|
| 7048 | strcmp(token, ss->legacy_name)) | 
|---|
| 7049 | continue; | 
|---|
| 7050 |  | 
|---|
| 7051 | static_branch_disable(cgroup_subsys_enabled_key[i]); | 
|---|
| 7052 | pr_info( "Disabling %s control group subsystem\n", | 
|---|
| 7053 | ss->name); | 
|---|
| 7054 | } | 
|---|
| 7055 |  | 
|---|
| 7056 | for (i = 0; i < OPT_FEATURE_COUNT; i++) { | 
|---|
| 7057 | if (strcmp(token, cgroup_opt_feature_names[i])) | 
|---|
| 7058 | continue; | 
|---|
| 7059 | cgroup_feature_disable_mask |= 1 << i; | 
|---|
| 7060 | pr_info( "Disabling %s control group feature\n", | 
|---|
| 7061 | cgroup_opt_feature_names[i]); | 
|---|
| 7062 | break; | 
|---|
| 7063 | } | 
|---|
| 7064 | } | 
|---|
| 7065 | return 1; | 
|---|
| 7066 | } | 
|---|
| 7067 | __setup( "cgroup_disable=", cgroup_disable); | 
|---|
| 7068 |  | 
|---|
| 7069 | void __init __weak enable_debug_cgroup(void) { } | 
|---|
| 7070 |  | 
|---|
| 7071 | static int __init enable_cgroup_debug(char *str) | 
|---|
| 7072 | { | 
|---|
| 7073 | cgroup_debug = true; | 
|---|
| 7074 | enable_debug_cgroup(); | 
|---|
| 7075 | return 1; | 
|---|
| 7076 | } | 
|---|
| 7077 | __setup( "cgroup_debug", enable_cgroup_debug); | 
|---|
| 7078 |  | 
|---|
| 7079 | static int __init cgroup_favordynmods_setup(char *str) | 
|---|
| 7080 | { | 
|---|
| 7081 | return (kstrtobool(s: str, res: &have_favordynmods) == 0); | 
|---|
| 7082 | } | 
|---|
| 7083 | __setup( "cgroup_favordynmods=", cgroup_favordynmods_setup); | 
|---|
| 7084 |  | 
|---|
| 7085 | /** | 
|---|
| 7086 | * css_tryget_online_from_dir - get corresponding css from a cgroup dentry | 
|---|
| 7087 | * @dentry: directory dentry of interest | 
|---|
| 7088 | * @ss: subsystem of interest | 
|---|
| 7089 | * | 
|---|
| 7090 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try | 
|---|
| 7091 | * to get the corresponding css and return it.  If such css doesn't exist | 
|---|
| 7092 | * or can't be pinned, an ERR_PTR value is returned. | 
|---|
| 7093 | */ | 
|---|
| 7094 | struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, | 
|---|
| 7095 | struct cgroup_subsys *ss) | 
|---|
| 7096 | { | 
|---|
| 7097 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); | 
|---|
| 7098 | struct file_system_type *s_type = dentry->d_sb->s_type; | 
|---|
| 7099 | struct cgroup_subsys_state *css = NULL; | 
|---|
| 7100 | struct cgroup *cgrp; | 
|---|
| 7101 |  | 
|---|
| 7102 | /* is @dentry a cgroup dir? */ | 
|---|
| 7103 | if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) || | 
|---|
| 7104 | !kn || kernfs_type(kn) != KERNFS_DIR) | 
|---|
| 7105 | return ERR_PTR(error: -EBADF); | 
|---|
| 7106 |  | 
|---|
| 7107 | rcu_read_lock(); | 
|---|
| 7108 |  | 
|---|
| 7109 | /* | 
|---|
| 7110 | * This path doesn't originate from kernfs and @kn could already | 
|---|
| 7111 | * have been or be removed at any point.  @kn->priv is RCU | 
|---|
| 7112 | * protected for this access.  See css_release_work_fn() for details. | 
|---|
| 7113 | */ | 
|---|
| 7114 | cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); | 
|---|
| 7115 | if (cgrp) | 
|---|
| 7116 | css = cgroup_css(cgrp, ss); | 
|---|
| 7117 |  | 
|---|
| 7118 | if (!css || !css_tryget_online(css)) | 
|---|
| 7119 | css = ERR_PTR(error: -ENOENT); | 
|---|
| 7120 |  | 
|---|
| 7121 | rcu_read_unlock(); | 
|---|
| 7122 | return css; | 
|---|
| 7123 | } | 
|---|
| 7124 |  | 
|---|
| 7125 | /** | 
|---|
| 7126 | * css_from_id - lookup css by id | 
|---|
| 7127 | * @id: the cgroup id | 
|---|
| 7128 | * @ss: cgroup subsys to be looked into | 
|---|
| 7129 | * | 
|---|
| 7130 | * Returns the css if there's valid one with @id, otherwise returns NULL. | 
|---|
| 7131 | * Should be called under rcu_read_lock(). | 
|---|
| 7132 | */ | 
|---|
| 7133 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) | 
|---|
| 7134 | { | 
|---|
| 7135 | WARN_ON_ONCE(!rcu_read_lock_held()); | 
|---|
| 7136 | return idr_find(&ss->css_idr, id); | 
|---|
| 7137 | } | 
|---|
| 7138 |  | 
|---|
| 7139 | /** | 
|---|
| 7140 | * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path | 
|---|
| 7141 | * @path: path on the default hierarchy | 
|---|
| 7142 | * | 
|---|
| 7143 | * Find the cgroup at @path on the default hierarchy, increment its | 
|---|
| 7144 | * reference count and return it.  Returns pointer to the found cgroup on | 
|---|
| 7145 | * success, ERR_PTR(-ENOENT) if @path doesn't exist or if the cgroup has already | 
|---|
| 7146 | * been released and ERR_PTR(-ENOTDIR) if @path points to a non-directory. | 
|---|
| 7147 | */ | 
|---|
| 7148 | struct cgroup *cgroup_get_from_path(const char *path) | 
|---|
| 7149 | { | 
|---|
| 7150 | struct kernfs_node *kn; | 
|---|
| 7151 | struct cgroup *cgrp = ERR_PTR(error: -ENOENT); | 
|---|
| 7152 | struct cgroup *root_cgrp; | 
|---|
| 7153 |  | 
|---|
| 7154 | root_cgrp = current_cgns_cgroup_dfl(); | 
|---|
| 7155 | kn = kernfs_walk_and_get(kn: root_cgrp->kn, path); | 
|---|
| 7156 | if (!kn) | 
|---|
| 7157 | goto out; | 
|---|
| 7158 |  | 
|---|
| 7159 | if (kernfs_type(kn) != KERNFS_DIR) { | 
|---|
| 7160 | cgrp = ERR_PTR(error: -ENOTDIR); | 
|---|
| 7161 | goto out_kernfs; | 
|---|
| 7162 | } | 
|---|
| 7163 |  | 
|---|
| 7164 | rcu_read_lock(); | 
|---|
| 7165 |  | 
|---|
| 7166 | cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); | 
|---|
| 7167 | if (!cgrp || !cgroup_tryget(cgrp)) | 
|---|
| 7168 | cgrp = ERR_PTR(error: -ENOENT); | 
|---|
| 7169 |  | 
|---|
| 7170 | rcu_read_unlock(); | 
|---|
| 7171 |  | 
|---|
| 7172 | out_kernfs: | 
|---|
| 7173 | kernfs_put(kn); | 
|---|
| 7174 | out: | 
|---|
| 7175 | return cgrp; | 
|---|
| 7176 | } | 
|---|
| 7177 | EXPORT_SYMBOL_GPL(cgroup_get_from_path); | 
|---|
| 7178 |  | 
|---|
| 7179 | /** | 
|---|
| 7180 | * cgroup_v1v2_get_from_fd - get a cgroup pointer from a fd | 
|---|
| 7181 | * @fd: fd obtained by open(cgroup_dir) | 
|---|
| 7182 | * | 
|---|
| 7183 | * Find the cgroup from a fd which should be obtained | 
|---|
| 7184 | * by opening a cgroup directory.  Returns a pointer to the | 
|---|
| 7185 | * cgroup on success. ERR_PTR is returned if the cgroup | 
|---|
| 7186 | * cannot be found. | 
|---|
| 7187 | */ | 
|---|
| 7188 | struct cgroup *cgroup_v1v2_get_from_fd(int fd) | 
|---|
| 7189 | { | 
|---|
| 7190 | CLASS(fd_raw, f)(fd); | 
|---|
| 7191 | if (fd_empty(f)) | 
|---|
| 7192 | return ERR_PTR(error: -EBADF); | 
|---|
| 7193 |  | 
|---|
| 7194 | return cgroup_v1v2_get_from_file(fd_file(f)); | 
|---|
| 7195 | } | 
|---|
| 7196 |  | 
|---|
| 7197 | /** | 
|---|
| 7198 | * cgroup_get_from_fd - same as cgroup_v1v2_get_from_fd, but only supports | 
|---|
| 7199 | * cgroup2. | 
|---|
| 7200 | * @fd: fd obtained by open(cgroup2_dir) | 
|---|
| 7201 | */ | 
|---|
| 7202 | struct cgroup *cgroup_get_from_fd(int fd) | 
|---|
| 7203 | { | 
|---|
| 7204 | struct cgroup *cgrp = cgroup_v1v2_get_from_fd(fd); | 
|---|
| 7205 |  | 
|---|
| 7206 | if (IS_ERR(ptr: cgrp)) | 
|---|
| 7207 | return ERR_CAST(ptr: cgrp); | 
|---|
| 7208 |  | 
|---|
| 7209 | if (!cgroup_on_dfl(cgrp)) { | 
|---|
| 7210 | cgroup_put(cgrp); | 
|---|
| 7211 | return ERR_PTR(error: -EBADF); | 
|---|
| 7212 | } | 
|---|
| 7213 | return cgrp; | 
|---|
| 7214 | } | 
|---|
| 7215 | EXPORT_SYMBOL_GPL(cgroup_get_from_fd); | 
|---|
| 7216 |  | 
|---|
| 7217 | static u64 power_of_ten(int power) | 
|---|
| 7218 | { | 
|---|
| 7219 | u64 v = 1; | 
|---|
| 7220 | while (power--) | 
|---|
| 7221 | v *= 10; | 
|---|
| 7222 | return v; | 
|---|
| 7223 | } | 
|---|
| 7224 |  | 
|---|
| 7225 | /** | 
|---|
| 7226 | * cgroup_parse_float - parse a floating number | 
|---|
| 7227 | * @input: input string | 
|---|
| 7228 | * @dec_shift: number of decimal digits to shift | 
|---|
| 7229 | * @v: output | 
|---|
| 7230 | * | 
|---|
| 7231 | * Parse a decimal floating point number in @input and store the result in | 
|---|
| 7232 | * @v with decimal point right shifted @dec_shift times.  For example, if | 
|---|
| 7233 | * @input is "12.3456" and @dec_shift is 3, *@v will be set to 12345. | 
|---|
| 7234 | * Returns 0 on success, -errno otherwise. | 
|---|
| 7235 | * | 
|---|
| 7236 | * There's nothing cgroup specific about this function except that it's | 
|---|
| 7237 | * currently the only user. | 
|---|
| 7238 | */ | 
|---|
| 7239 | int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v) | 
|---|
| 7240 | { | 
|---|
| 7241 | s64 whole, frac = 0; | 
|---|
| 7242 | int fstart = 0, fend = 0, flen; | 
|---|
| 7243 |  | 
|---|
| 7244 | if (!sscanf(input, "%lld.%n%lld%n", &whole, &fstart, &frac, &fend)) | 
|---|
| 7245 | return -EINVAL; | 
|---|
| 7246 | if (frac < 0) | 
|---|
| 7247 | return -EINVAL; | 
|---|
| 7248 |  | 
|---|
| 7249 | flen = fend > fstart ? fend - fstart : 0; | 
|---|
| 7250 | if (flen < dec_shift) | 
|---|
| 7251 | frac *= power_of_ten(power: dec_shift - flen); | 
|---|
| 7252 | else | 
|---|
| 7253 | frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift)); | 
|---|
| 7254 |  | 
|---|
| 7255 | *v = whole * power_of_ten(power: dec_shift) + frac; | 
|---|
| 7256 | return 0; | 
|---|
| 7257 | } | 
|---|
| 7258 |  | 
|---|
| 7259 | /* | 
|---|
| 7260 | * sock->sk_cgrp_data handling.  For more info, see sock_cgroup_data | 
|---|
| 7261 | * definition in cgroup-defs.h. | 
|---|
| 7262 | */ | 
|---|
| 7263 | #ifdef CONFIG_SOCK_CGROUP_DATA | 
|---|
| 7264 |  | 
|---|
| 7265 | void cgroup_sk_alloc(struct sock_cgroup_data *skcd) | 
|---|
| 7266 | { | 
|---|
| 7267 | struct cgroup *cgroup; | 
|---|
| 7268 |  | 
|---|
| 7269 | rcu_read_lock(); | 
|---|
| 7270 | /* Don't associate the sock with unrelated interrupted task's cgroup. */ | 
|---|
| 7271 | if (in_interrupt()) { | 
|---|
| 7272 | cgroup = &cgrp_dfl_root.cgrp; | 
|---|
| 7273 | cgroup_get(cgrp: cgroup); | 
|---|
| 7274 | goto out; | 
|---|
| 7275 | } | 
|---|
| 7276 |  | 
|---|
| 7277 | while (true) { | 
|---|
| 7278 | struct css_set *cset; | 
|---|
| 7279 |  | 
|---|
| 7280 | cset = task_css_set(current); | 
|---|
| 7281 | if (likely(cgroup_tryget(cset->dfl_cgrp))) { | 
|---|
| 7282 | cgroup = cset->dfl_cgrp; | 
|---|
| 7283 | break; | 
|---|
| 7284 | } | 
|---|
| 7285 | cpu_relax(); | 
|---|
| 7286 | } | 
|---|
| 7287 | out: | 
|---|
| 7288 | skcd->cgroup = cgroup; | 
|---|
| 7289 | cgroup_bpf_get(cgrp: cgroup); | 
|---|
| 7290 | rcu_read_unlock(); | 
|---|
| 7291 | } | 
|---|
| 7292 |  | 
|---|
| 7293 | void cgroup_sk_clone(struct sock_cgroup_data *skcd) | 
|---|
| 7294 | { | 
|---|
| 7295 | struct cgroup *cgrp = sock_cgroup_ptr(skcd); | 
|---|
| 7296 |  | 
|---|
| 7297 | /* | 
|---|
| 7298 | * We might be cloning a socket which is left in an empty | 
|---|
| 7299 | * cgroup and the cgroup might have already been rmdir'd. | 
|---|
| 7300 | * Don't use cgroup_get_live(). | 
|---|
| 7301 | */ | 
|---|
| 7302 | cgroup_get(cgrp); | 
|---|
| 7303 | cgroup_bpf_get(cgrp); | 
|---|
| 7304 | } | 
|---|
| 7305 |  | 
|---|
| 7306 | void cgroup_sk_free(struct sock_cgroup_data *skcd) | 
|---|
| 7307 | { | 
|---|
| 7308 | struct cgroup *cgrp = sock_cgroup_ptr(skcd); | 
|---|
| 7309 |  | 
|---|
| 7310 | cgroup_bpf_put(cgrp); | 
|---|
| 7311 | cgroup_put(cgrp); | 
|---|
| 7312 | } | 
|---|
| 7313 |  | 
|---|
| 7314 | #endif	/* CONFIG_SOCK_CGROUP_DATA */ | 
|---|
| 7315 |  | 
|---|
| 7316 | #ifdef CONFIG_SYSFS | 
|---|
| 7317 | static ssize_t show_delegatable_files(struct cftype *files, char *buf, | 
|---|
| 7318 | ssize_t size, const char *prefix) | 
|---|
| 7319 | { | 
|---|
| 7320 | struct cftype *cft; | 
|---|
| 7321 | ssize_t ret = 0; | 
|---|
| 7322 |  | 
|---|
| 7323 | for (cft = files; cft && cft->name[0] != '\0'; cft++) { | 
|---|
| 7324 | if (!(cft->flags & CFTYPE_NS_DELEGATABLE)) | 
|---|
| 7325 | continue; | 
|---|
| 7326 |  | 
|---|
| 7327 | if (prefix) | 
|---|
| 7328 | ret += snprintf(buf: buf + ret, size: size - ret, fmt: "%s.", prefix); | 
|---|
| 7329 |  | 
|---|
| 7330 | ret += snprintf(buf: buf + ret, size: size - ret, fmt: "%s\n", cft->name); | 
|---|
| 7331 |  | 
|---|
| 7332 | if (WARN_ON(ret >= size)) | 
|---|
| 7333 | break; | 
|---|
| 7334 | } | 
|---|
| 7335 |  | 
|---|
| 7336 | return ret; | 
|---|
| 7337 | } | 
|---|
| 7338 |  | 
|---|
| 7339 | static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr, | 
|---|
| 7340 | char *buf) | 
|---|
| 7341 | { | 
|---|
| 7342 | struct cgroup_subsys *ss; | 
|---|
| 7343 | int ssid; | 
|---|
| 7344 | ssize_t ret = 0; | 
|---|
| 7345 |  | 
|---|
| 7346 | ret = show_delegatable_files(files: cgroup_base_files, buf: buf + ret, | 
|---|
| 7347 | PAGE_SIZE - ret, NULL); | 
|---|
| 7348 | if (cgroup_psi_enabled()) | 
|---|
| 7349 | ret += show_delegatable_files(files: cgroup_psi_files, buf: buf + ret, | 
|---|
| 7350 | PAGE_SIZE - ret, NULL); | 
|---|
| 7351 |  | 
|---|
| 7352 | for_each_subsys(ss, ssid) | 
|---|
| 7353 | ret += show_delegatable_files(files: ss->dfl_cftypes, buf: buf + ret, | 
|---|
| 7354 | PAGE_SIZE - ret, | 
|---|
| 7355 | prefix: cgroup_subsys_name[ssid]); | 
|---|
| 7356 |  | 
|---|
| 7357 | return ret; | 
|---|
| 7358 | } | 
|---|
| 7359 | static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate); | 
|---|
| 7360 |  | 
|---|
| 7361 | static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr, | 
|---|
| 7362 | char *buf) | 
|---|
| 7363 | { | 
|---|
| 7364 | return snprintf(buf, PAGE_SIZE, | 
|---|
| 7365 | fmt: "nsdelegate\n" | 
|---|
| 7366 | "favordynmods\n" | 
|---|
| 7367 | "memory_localevents\n" | 
|---|
| 7368 | "memory_recursiveprot\n" | 
|---|
| 7369 | "memory_hugetlb_accounting\n" | 
|---|
| 7370 | "pids_localevents\n"); | 
|---|
| 7371 | } | 
|---|
| 7372 | static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features); | 
|---|
| 7373 |  | 
|---|
| 7374 | static struct attribute *cgroup_sysfs_attrs[] = { | 
|---|
| 7375 | &cgroup_delegate_attr.attr, | 
|---|
| 7376 | &cgroup_features_attr.attr, | 
|---|
| 7377 | NULL, | 
|---|
| 7378 | }; | 
|---|
| 7379 |  | 
|---|
| 7380 | static const struct attribute_group cgroup_sysfs_attr_group = { | 
|---|
| 7381 | .attrs = cgroup_sysfs_attrs, | 
|---|
| 7382 | .name = "cgroup", | 
|---|
| 7383 | }; | 
|---|
| 7384 |  | 
|---|
| 7385 | static int __init cgroup_sysfs_init(void) | 
|---|
| 7386 | { | 
|---|
| 7387 | return sysfs_create_group(kobj: kernel_kobj, grp: &cgroup_sysfs_attr_group); | 
|---|
| 7388 | } | 
|---|
| 7389 | subsys_initcall(cgroup_sysfs_init); | 
|---|
| 7390 |  | 
|---|
| 7391 | #endif /* CONFIG_SYSFS */ | 
|---|
| 7392 |  | 
|---|