| 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 <linux/cgroup.h> |
| 32 | #include <linux/cred.h> |
| 33 | #include <linux/ctype.h> |
| 34 | #include <linux/errno.h> |
| 35 | #include <linux/init_task.h> |
| 36 | #include <linux/kernel.h> |
| 37 | #include <linux/list.h> |
| 38 | #include <linux/magic.h> |
| 39 | #include <linux/mm.h> |
| 40 | #include <linux/mutex.h> |
| 41 | #include <linux/mount.h> |
| 42 | #include <linux/pagemap.h> |
| 43 | #include <linux/proc_fs.h> |
| 44 | #include <linux/rcupdate.h> |
| 45 | #include <linux/sched.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/sort.h> |
| 51 | #include <linux/kmod.h> |
| 52 | #include <linux/delayacct.h> |
| 53 | #include <linux/cgroupstats.h> |
| 54 | #include <linux/hashtable.h> |
| 55 | #include <linux/pid_namespace.h> |
| 56 | #include <linux/idr.h> |
| 57 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
| 58 | #include <linux/kthread.h> |
| 59 | #include <linux/delay.h> |
| 60 | #include <linux/atomic.h> |
| 61 | #include <linux/cpuset.h> |
| 62 | #include <net/sock.h> |
| 63 | |
| 64 | /* |
| 65 | * pidlists linger the following amount before being destroyed. The goal |
| 66 | * is avoiding frequent destruction in the middle of consecutive read calls |
| 67 | * Expiring in the middle is a performance problem not a correctness one. |
| 68 | * 1 sec should be enough. |
| 69 | */ |
| 70 | #define CGROUP_PIDLIST_DESTROY_DELAY HZ |
| 71 | |
| 72 | #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ |
| 73 | MAX_CFTYPE_NAME + 2) |
| 74 | |
| 75 | /* |
| 76 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 77 | * hierarchy must be performed while holding it. |
| 78 | * |
| 79 | * css_set_lock protects task->cgroups pointer, the list of css_set |
| 80 | * objects, and the chain of tasks off each css_set. |
| 81 | * |
| 82 | * These locks are exported if CONFIG_PROVE_RCU so that accessors in |
| 83 | * cgroup.h can use them for lockdep annotations. |
| 84 | */ |
| 85 | #ifdef CONFIG_PROVE_RCU |
| 86 | DEFINE_MUTEX(cgroup_mutex); |
| 87 | DEFINE_SPINLOCK(css_set_lock); |
| 88 | EXPORT_SYMBOL_GPL(cgroup_mutex); |
| 89 | EXPORT_SYMBOL_GPL(css_set_lock); |
| 90 | #else |
| 91 | static DEFINE_MUTEX(cgroup_mutex); |
| 92 | static DEFINE_SPINLOCK(css_set_lock); |
| 93 | #endif |
| 94 | |
| 95 | /* |
| 96 | * Protects cgroup_idr and css_idr so that IDs can be released without |
| 97 | * grabbing cgroup_mutex. |
| 98 | */ |
| 99 | static DEFINE_SPINLOCK(cgroup_idr_lock); |
| 100 | |
| 101 | /* |
| 102 | * Protects cgroup_file->kn for !self csses. It synchronizes notifications |
| 103 | * against file removal/re-creation across css hiding. |
| 104 | */ |
| 105 | static DEFINE_SPINLOCK(cgroup_file_kn_lock); |
| 106 | |
| 107 | /* |
| 108 | * Protects cgroup_subsys->release_agent_path. Modifying it also requires |
| 109 | * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock. |
| 110 | */ |
| 111 | static DEFINE_SPINLOCK(release_agent_path_lock); |
| 112 | |
| 113 | struct percpu_rw_semaphore cgroup_threadgroup_rwsem; |
| 114 | |
| 115 | #define cgroup_assert_mutex_or_rcu_locked() \ |
| 116 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ |
| 117 | !lockdep_is_held(&cgroup_mutex), \ |
| 118 | "cgroup_mutex or RCU read lock required"); |
| 119 | |
| 120 | /* |
| 121 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 122 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 123 | * destruction work items don't end up filling up max_active of system_wq |
| 124 | * which may lead to deadlock. |
| 125 | */ |
| 126 | static struct workqueue_struct *cgroup_destroy_wq; |
| 127 | |
| 128 | /* |
| 129 | * pidlist destructions need to be flushed on cgroup destruction. Use a |
| 130 | * separate workqueue as flush domain. |
| 131 | */ |
| 132 | static struct workqueue_struct *cgroup_pidlist_destroy_wq; |
| 133 | |
| 134 | /* generate an array of cgroup subsystem pointers */ |
| 135 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
| 136 | static struct cgroup_subsys *cgroup_subsys[] = { |
| 137 | #include <linux/cgroup_subsys.h> |
| 138 | }; |
| 139 | #undef SUBSYS |
| 140 | |
| 141 | /* array of cgroup subsystem names */ |
| 142 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 143 | static const char *cgroup_subsys_name[] = { |
| 144 | #include <linux/cgroup_subsys.h> |
| 145 | }; |
| 146 | #undef SUBSYS |
| 147 | |
| 148 | /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */ |
| 149 | #define SUBSYS(_x) \ |
| 150 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \ |
| 151 | DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \ |
| 152 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \ |
| 153 | EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key); |
| 154 | #include <linux/cgroup_subsys.h> |
| 155 | #undef SUBSYS |
| 156 | |
| 157 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key, |
| 158 | static struct static_key_true *cgroup_subsys_enabled_key[] = { |
| 159 | #include <linux/cgroup_subsys.h> |
| 160 | }; |
| 161 | #undef SUBSYS |
| 162 | |
| 163 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key, |
| 164 | static struct static_key_true *cgroup_subsys_on_dfl_key[] = { |
| 165 | #include <linux/cgroup_subsys.h> |
| 166 | }; |
| 167 | #undef SUBSYS |
| 168 | |
| 169 | /* |
| 170 | * The default hierarchy, reserved for the subsystems that are otherwise |
| 171 | * unattached - it never has more than a single cgroup, and all tasks are |
| 172 | * part of that cgroup. |
| 173 | */ |
| 174 | struct cgroup_root cgrp_dfl_root; |
| 175 | EXPORT_SYMBOL_GPL(cgrp_dfl_root); |
| 176 | |
| 177 | /* |
| 178 | * The default hierarchy always exists but is hidden until mounted for the |
| 179 | * first time. This is for backward compatibility. |
| 180 | */ |
| 181 | static bool cgrp_dfl_visible; |
| 182 | |
| 183 | /* Controllers blocked by the commandline in v1 */ |
| 184 | static u16 cgroup_no_v1_mask; |
| 185 | |
| 186 | /* some controllers are not supported in the default hierarchy */ |
| 187 | static u16 cgrp_dfl_inhibit_ss_mask; |
| 188 | |
| 189 | /* The list of hierarchy roots */ |
| 190 | |
| 191 | static LIST_HEAD(cgroup_roots); |
| 192 | static int cgroup_root_count; |
| 193 | |
| 194 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
| 195 | static DEFINE_IDR(cgroup_hierarchy_idr); |
| 196 | |
| 197 | /* |
| 198 | * Assign a monotonically increasing serial number to csses. It guarantees |
| 199 | * cgroups with bigger numbers are newer than those with smaller numbers. |
| 200 | * Also, as csses are always appended to the parent's ->children list, it |
| 201 | * guarantees that sibling csses are always sorted in the ascending serial |
| 202 | * number order on the list. Protected by cgroup_mutex. |
| 203 | */ |
| 204 | static u64 css_serial_nr_next = 1; |
| 205 | |
| 206 | /* |
| 207 | * These bitmask flags indicate whether tasks in the fork and exit paths have |
| 208 | * fork/exit handlers to call. This avoids us having to do extra work in the |
| 209 | * fork/exit path to check which subsystems have fork/exit callbacks. |
| 210 | */ |
| 211 | static u16 have_fork_callback __read_mostly; |
| 212 | static u16 have_exit_callback __read_mostly; |
| 213 | static u16 have_free_callback __read_mostly; |
| 214 | |
| 215 | /* Ditto for the can_fork callback. */ |
| 216 | static u16 have_canfork_callback __read_mostly; |
| 217 | |
| 218 | static struct file_system_type cgroup2_fs_type; |
| 219 | static struct cftype cgroup_dfl_base_files[]; |
| 220 | static struct cftype cgroup_legacy_base_files[]; |
| 221 | |
| 222 | static int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask); |
| 223 | static void cgroup_lock_and_drain_offline(struct cgroup *cgrp); |
| 224 | static void css_task_iter_advance(struct css_task_iter *it); |
| 225 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
| 226 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 227 | struct cgroup_subsys *ss); |
| 228 | static void css_release(struct percpu_ref *ref); |
| 229 | static void kill_css(struct cgroup_subsys_state *css); |
| 230 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 231 | struct cgroup *cgrp, struct cftype cfts[], |
| 232 | bool is_add); |
| 233 | |
| 234 | /** |
| 235 | * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID |
| 236 | * @ssid: subsys ID of interest |
| 237 | * |
| 238 | * cgroup_subsys_enabled() can only be used with literal subsys names which |
| 239 | * is fine for individual subsystems but unsuitable for cgroup core. This |
| 240 | * is slower static_key_enabled() based test indexed by @ssid. |
| 241 | */ |
| 242 | static bool cgroup_ssid_enabled(int ssid) |
| 243 | { |
| 244 | return static_key_enabled(cgroup_subsys_enabled_key[ssid]); |
| 245 | } |
| 246 | |
| 247 | static bool cgroup_ssid_no_v1(int ssid) |
| 248 | { |
| 249 | return cgroup_no_v1_mask & (1 << ssid); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * cgroup_on_dfl - test whether a cgroup is on the default hierarchy |
| 254 | * @cgrp: the cgroup of interest |
| 255 | * |
| 256 | * The default hierarchy is the v2 interface of cgroup and this function |
| 257 | * can be used to test whether a cgroup is on the default hierarchy for |
| 258 | * cases where a subsystem should behave differnetly depending on the |
| 259 | * interface version. |
| 260 | * |
| 261 | * The set of behaviors which change on the default hierarchy are still |
| 262 | * being determined and the mount option is prefixed with __DEVEL__. |
| 263 | * |
| 264 | * List of changed behaviors: |
| 265 | * |
| 266 | * - Mount options "noprefix", "xattr", "clone_children", "release_agent" |
| 267 | * and "name" are disallowed. |
| 268 | * |
| 269 | * - When mounting an existing superblock, mount options should match. |
| 270 | * |
| 271 | * - Remount is disallowed. |
| 272 | * |
| 273 | * - rename(2) is disallowed. |
| 274 | * |
| 275 | * - "tasks" is removed. Everything should be at process granularity. Use |
| 276 | * "cgroup.procs" instead. |
| 277 | * |
| 278 | * - "cgroup.procs" is not sorted. pids will be unique unless they got |
| 279 | * recycled inbetween reads. |
| 280 | * |
| 281 | * - "release_agent" and "notify_on_release" are removed. Replacement |
| 282 | * notification mechanism will be implemented. |
| 283 | * |
| 284 | * - "cgroup.clone_children" is removed. |
| 285 | * |
| 286 | * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup |
| 287 | * and its descendants contain no task; otherwise, 1. The file also |
| 288 | * generates kernfs notification which can be monitored through poll and |
| 289 | * [di]notify when the value of the file changes. |
| 290 | * |
| 291 | * - cpuset: tasks will be kept in empty cpusets when hotplug happens and |
| 292 | * take masks of ancestors with non-empty cpus/mems, instead of being |
| 293 | * moved to an ancestor. |
| 294 | * |
| 295 | * - cpuset: a task can be moved into an empty cpuset, and again it takes |
| 296 | * masks of ancestors. |
| 297 | * |
| 298 | * - memcg: use_hierarchy is on by default and the cgroup file for the flag |
| 299 | * is not created. |
| 300 | * |
| 301 | * - blkcg: blk-throttle becomes properly hierarchical. |
| 302 | * |
| 303 | * - debug: disallowed on the default hierarchy. |
| 304 | */ |
| 305 | static bool cgroup_on_dfl(const struct cgroup *cgrp) |
| 306 | { |
| 307 | return cgrp->root == &cgrp_dfl_root; |
| 308 | } |
| 309 | |
| 310 | /* IDR wrappers which synchronize using cgroup_idr_lock */ |
| 311 | static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end, |
| 312 | gfp_t gfp_mask) |
| 313 | { |
| 314 | int ret; |
| 315 | |
| 316 | idr_preload(gfp_mask); |
| 317 | spin_lock_bh(&cgroup_idr_lock); |
| 318 | ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM); |
| 319 | spin_unlock_bh(&cgroup_idr_lock); |
| 320 | idr_preload_end(); |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id) |
| 325 | { |
| 326 | void *ret; |
| 327 | |
| 328 | spin_lock_bh(&cgroup_idr_lock); |
| 329 | ret = idr_replace(idr, ptr, id); |
| 330 | spin_unlock_bh(&cgroup_idr_lock); |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | static void cgroup_idr_remove(struct idr *idr, int id) |
| 335 | { |
| 336 | spin_lock_bh(&cgroup_idr_lock); |
| 337 | idr_remove(idr, id); |
| 338 | spin_unlock_bh(&cgroup_idr_lock); |
| 339 | } |
| 340 | |
| 341 | static struct cgroup *cgroup_parent(struct cgroup *cgrp) |
| 342 | { |
| 343 | struct cgroup_subsys_state *parent_css = cgrp->self.parent; |
| 344 | |
| 345 | if (parent_css) |
| 346 | return container_of(parent_css, struct cgroup, self); |
| 347 | return NULL; |
| 348 | } |
| 349 | |
| 350 | /* subsystems visibly enabled on a cgroup */ |
| 351 | static u16 cgroup_control(struct cgroup *cgrp) |
| 352 | { |
| 353 | struct cgroup *parent = cgroup_parent(cgrp); |
| 354 | u16 root_ss_mask = cgrp->root->subsys_mask; |
| 355 | |
| 356 | if (parent) |
| 357 | return parent->subtree_control; |
| 358 | |
| 359 | if (cgroup_on_dfl(cgrp)) |
| 360 | root_ss_mask &= ~cgrp_dfl_inhibit_ss_mask; |
| 361 | |
| 362 | return root_ss_mask; |
| 363 | } |
| 364 | |
| 365 | /* subsystems enabled on a cgroup */ |
| 366 | static u16 cgroup_ss_mask(struct cgroup *cgrp) |
| 367 | { |
| 368 | struct cgroup *parent = cgroup_parent(cgrp); |
| 369 | |
| 370 | if (parent) |
| 371 | return parent->subtree_ss_mask; |
| 372 | |
| 373 | return cgrp->root->subsys_mask; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 378 | * @cgrp: the cgroup of interest |
| 379 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
| 380 | * |
| 381 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 382 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 383 | * the caller is responsible for pinning the returned css if it wants to |
| 384 | * keep accessing it outside the said locks. This function may return |
| 385 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
| 386 | */ |
| 387 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
| 388 | struct cgroup_subsys *ss) |
| 389 | { |
| 390 | if (ss) |
| 391 | return rcu_dereference_check(cgrp->subsys[ss->id], |
| 392 | lockdep_is_held(&cgroup_mutex)); |
| 393 | else |
| 394 | return &cgrp->self; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem |
| 399 | * @cgrp: the cgroup of interest |
| 400 | * @ss: the subsystem of interest (%NULL returns @cgrp->self) |
| 401 | * |
| 402 | * Similar to cgroup_css() but returns the effective css, which is defined |
| 403 | * as the matching css of the nearest ancestor including self which has @ss |
| 404 | * enabled. If @ss is associated with the hierarchy @cgrp is on, this |
| 405 | * function is guaranteed to return non-NULL css. |
| 406 | */ |
| 407 | static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp, |
| 408 | struct cgroup_subsys *ss) |
| 409 | { |
| 410 | lockdep_assert_held(&cgroup_mutex); |
| 411 | |
| 412 | if (!ss) |
| 413 | return &cgrp->self; |
| 414 | |
| 415 | /* |
| 416 | * This function is used while updating css associations and thus |
| 417 | * can't test the csses directly. Test ss_mask. |
| 418 | */ |
| 419 | while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) { |
| 420 | cgrp = cgroup_parent(cgrp); |
| 421 | if (!cgrp) |
| 422 | return NULL; |
| 423 | } |
| 424 | |
| 425 | return cgroup_css(cgrp, ss); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem |
| 430 | * @cgrp: the cgroup of interest |
| 431 | * @ss: the subsystem of interest |
| 432 | * |
| 433 | * Find and get the effective css of @cgrp for @ss. The effective css is |
| 434 | * defined as the matching css of the nearest ancestor including self which |
| 435 | * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, |
| 436 | * the root css is returned, so this function always returns a valid css. |
| 437 | * The returned css must be put using css_put(). |
| 438 | */ |
| 439 | struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp, |
| 440 | struct cgroup_subsys *ss) |
| 441 | { |
| 442 | struct cgroup_subsys_state *css; |
| 443 | |
| 444 | rcu_read_lock(); |
| 445 | |
| 446 | do { |
| 447 | css = cgroup_css(cgrp, ss); |
| 448 | |
| 449 | if (css && css_tryget_online(css)) |
| 450 | goto out_unlock; |
| 451 | cgrp = cgroup_parent(cgrp); |
| 452 | } while (cgrp); |
| 453 | |
| 454 | css = init_css_set.subsys[ss->id]; |
| 455 | css_get(css); |
| 456 | out_unlock: |
| 457 | rcu_read_unlock(); |
| 458 | return css; |
| 459 | } |
| 460 | |
| 461 | /* convenient tests for these bits */ |
| 462 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
| 463 | { |
| 464 | return !(cgrp->self.flags & CSS_ONLINE); |
| 465 | } |
| 466 | |
| 467 | static void cgroup_get(struct cgroup *cgrp) |
| 468 | { |
| 469 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
| 470 | css_get(&cgrp->self); |
| 471 | } |
| 472 | |
| 473 | static bool cgroup_tryget(struct cgroup *cgrp) |
| 474 | { |
| 475 | return css_tryget(&cgrp->self); |
| 476 | } |
| 477 | |
| 478 | struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) |
| 479 | { |
| 480 | struct cgroup *cgrp = of->kn->parent->priv; |
| 481 | struct cftype *cft = of_cft(of); |
| 482 | |
| 483 | /* |
| 484 | * This is open and unprotected implementation of cgroup_css(). |
| 485 | * seq_css() is only called from a kernfs file operation which has |
| 486 | * an active reference on the file. Because all the subsystem |
| 487 | * files are drained before a css is disassociated with a cgroup, |
| 488 | * the matching css from the cgroup's subsys table is guaranteed to |
| 489 | * be and stay valid until the enclosing operation is complete. |
| 490 | */ |
| 491 | if (cft->ss) |
| 492 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 493 | else |
| 494 | return &cgrp->self; |
| 495 | } |
| 496 | EXPORT_SYMBOL_GPL(of_css); |
| 497 | |
| 498 | static int notify_on_release(const struct cgroup *cgrp) |
| 499 | { |
| 500 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * for_each_css - iterate all css's of a cgroup |
| 505 | * @css: the iteration cursor |
| 506 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 507 | * @cgrp: the target cgroup to iterate css's of |
| 508 | * |
| 509 | * Should be called under cgroup_[tree_]mutex. |
| 510 | */ |
| 511 | #define for_each_css(css, ssid, cgrp) \ |
| 512 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 513 | if (!((css) = rcu_dereference_check( \ |
| 514 | (cgrp)->subsys[(ssid)], \ |
| 515 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 516 | else |
| 517 | |
| 518 | /** |
| 519 | * for_each_e_css - iterate all effective css's of a cgroup |
| 520 | * @css: the iteration cursor |
| 521 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 522 | * @cgrp: the target cgroup to iterate css's of |
| 523 | * |
| 524 | * Should be called under cgroup_[tree_]mutex. |
| 525 | */ |
| 526 | #define for_each_e_css(css, ssid, cgrp) \ |
| 527 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 528 | if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \ |
| 529 | ; \ |
| 530 | else |
| 531 | |
| 532 | /** |
| 533 | * for_each_subsys - iterate all enabled cgroup subsystems |
| 534 | * @ss: the iteration cursor |
| 535 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
| 536 | */ |
| 537 | #define for_each_subsys(ss, ssid) \ |
| 538 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \ |
| 539 | (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) |
| 540 | |
| 541 | /** |
| 542 | * do_each_subsys_mask - filter for_each_subsys with a bitmask |
| 543 | * @ss: the iteration cursor |
| 544 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
| 545 | * @ss_mask: the bitmask |
| 546 | * |
| 547 | * The block will only run for cases where the ssid-th bit (1 << ssid) of |
| 548 | * @ss_mask is set. |
| 549 | */ |
| 550 | #define do_each_subsys_mask(ss, ssid, ss_mask) do { \ |
| 551 | unsigned long __ss_mask = (ss_mask); \ |
| 552 | if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \ |
| 553 | (ssid) = 0; \ |
| 554 | break; \ |
| 555 | } \ |
| 556 | for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \ |
| 557 | (ss) = cgroup_subsys[ssid]; \ |
| 558 | { |
| 559 | |
| 560 | #define while_each_subsys_mask() \ |
| 561 | } \ |
| 562 | } \ |
| 563 | } while (false) |
| 564 | |
| 565 | /* iterate across the hierarchies */ |
| 566 | #define for_each_root(root) \ |
| 567 | list_for_each_entry((root), &cgroup_roots, root_list) |
| 568 | |
| 569 | /* iterate over child cgrps, lock should be held throughout iteration */ |
| 570 | #define cgroup_for_each_live_child(child, cgrp) \ |
| 571 | list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \ |
| 572 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 573 | cgroup_is_dead(child); })) \ |
| 574 | ; \ |
| 575 | else |
| 576 | |
| 577 | /* walk live descendants in preorder */ |
| 578 | #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \ |
| 579 | css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \ |
| 580 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 581 | (dsct) = (d_css)->cgroup; \ |
| 582 | cgroup_is_dead(dsct); })) \ |
| 583 | ; \ |
| 584 | else |
| 585 | |
| 586 | /* walk live descendants in postorder */ |
| 587 | #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \ |
| 588 | css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \ |
| 589 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 590 | (dsct) = (d_css)->cgroup; \ |
| 591 | cgroup_is_dead(dsct); })) \ |
| 592 | ; \ |
| 593 | else |
| 594 | |
| 595 | static void cgroup_release_agent(struct work_struct *work); |
| 596 | static void check_for_release(struct cgroup *cgrp); |
| 597 | |
| 598 | /* |
| 599 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 600 | * belong to different cgroups on different hierarchies. In the other |
| 601 | * direction, a css_set is naturally associated with multiple cgroups. |
| 602 | * This M:N relationship is represented by the following link structure |
| 603 | * which exists for each association and allows traversing the associations |
| 604 | * from both sides. |
| 605 | */ |
| 606 | struct cgrp_cset_link { |
| 607 | /* the cgroup and css_set this link associates */ |
| 608 | struct cgroup *cgrp; |
| 609 | struct css_set *cset; |
| 610 | |
| 611 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 612 | struct list_head cset_link; |
| 613 | |
| 614 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 615 | struct list_head cgrp_link; |
| 616 | }; |
| 617 | |
| 618 | /* |
| 619 | * The default css_set - used by init and its children prior to any |
| 620 | * hierarchies being mounted. It contains a pointer to the root state |
| 621 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 622 | * reference-counted, to improve performance when child cgroups |
| 623 | * haven't been created. |
| 624 | */ |
| 625 | struct css_set init_css_set = { |
| 626 | .refcount = ATOMIC_INIT(1), |
| 627 | .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), |
| 628 | .tasks = LIST_HEAD_INIT(init_css_set.tasks), |
| 629 | .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), |
| 630 | .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node), |
| 631 | .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), |
| 632 | .task_iters = LIST_HEAD_INIT(init_css_set.task_iters), |
| 633 | }; |
| 634 | |
| 635 | static int css_set_count = 1; /* 1 for init_css_set */ |
| 636 | |
| 637 | /** |
| 638 | * css_set_populated - does a css_set contain any tasks? |
| 639 | * @cset: target css_set |
| 640 | */ |
| 641 | static bool css_set_populated(struct css_set *cset) |
| 642 | { |
| 643 | lockdep_assert_held(&css_set_lock); |
| 644 | |
| 645 | return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks); |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * cgroup_update_populated - updated populated count of a cgroup |
| 650 | * @cgrp: the target cgroup |
| 651 | * @populated: inc or dec populated count |
| 652 | * |
| 653 | * One of the css_sets associated with @cgrp is either getting its first |
| 654 | * task or losing the last. Update @cgrp->populated_cnt accordingly. The |
| 655 | * count is propagated towards root so that a given cgroup's populated_cnt |
| 656 | * is zero iff the cgroup and all its descendants don't contain any tasks. |
| 657 | * |
| 658 | * @cgrp's interface file "cgroup.populated" is zero if |
| 659 | * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt |
| 660 | * changes from or to zero, userland is notified that the content of the |
| 661 | * interface file has changed. This can be used to detect when @cgrp and |
| 662 | * its descendants become populated or empty. |
| 663 | */ |
| 664 | static void cgroup_update_populated(struct cgroup *cgrp, bool populated) |
| 665 | { |
| 666 | lockdep_assert_held(&css_set_lock); |
| 667 | |
| 668 | do { |
| 669 | bool trigger; |
| 670 | |
| 671 | if (populated) |
| 672 | trigger = !cgrp->populated_cnt++; |
| 673 | else |
| 674 | trigger = !--cgrp->populated_cnt; |
| 675 | |
| 676 | if (!trigger) |
| 677 | break; |
| 678 | |
| 679 | check_for_release(cgrp); |
| 680 | cgroup_file_notify(&cgrp->events_file); |
| 681 | |
| 682 | cgrp = cgroup_parent(cgrp); |
| 683 | } while (cgrp); |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * css_set_update_populated - update populated state of a css_set |
| 688 | * @cset: target css_set |
| 689 | * @populated: whether @cset is populated or depopulated |
| 690 | * |
| 691 | * @cset is either getting the first task or losing the last. Update the |
| 692 | * ->populated_cnt of all associated cgroups accordingly. |
| 693 | */ |
| 694 | static void css_set_update_populated(struct css_set *cset, bool populated) |
| 695 | { |
| 696 | struct cgrp_cset_link *link; |
| 697 | |
| 698 | lockdep_assert_held(&css_set_lock); |
| 699 | |
| 700 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) |
| 701 | cgroup_update_populated(link->cgrp, populated); |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * css_set_move_task - move a task from one css_set to another |
| 706 | * @task: task being moved |
| 707 | * @from_cset: css_set @task currently belongs to (may be NULL) |
| 708 | * @to_cset: new css_set @task is being moved to (may be NULL) |
| 709 | * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks |
| 710 | * |
| 711 | * Move @task from @from_cset to @to_cset. If @task didn't belong to any |
| 712 | * css_set, @from_cset can be NULL. If @task is being disassociated |
| 713 | * instead of moved, @to_cset can be NULL. |
| 714 | * |
| 715 | * This function automatically handles populated_cnt updates and |
| 716 | * css_task_iter adjustments but the caller is responsible for managing |
| 717 | * @from_cset and @to_cset's reference counts. |
| 718 | */ |
| 719 | static void css_set_move_task(struct task_struct *task, |
| 720 | struct css_set *from_cset, struct css_set *to_cset, |
| 721 | bool use_mg_tasks) |
| 722 | { |
| 723 | lockdep_assert_held(&css_set_lock); |
| 724 | |
| 725 | if (to_cset && !css_set_populated(to_cset)) |
| 726 | css_set_update_populated(to_cset, true); |
| 727 | |
| 728 | if (from_cset) { |
| 729 | struct css_task_iter *it, *pos; |
| 730 | |
| 731 | WARN_ON_ONCE(list_empty(&task->cg_list)); |
| 732 | |
| 733 | /* |
| 734 | * @task is leaving, advance task iterators which are |
| 735 | * pointing to it so that they can resume at the next |
| 736 | * position. Advancing an iterator might remove it from |
| 737 | * the list, use safe walk. See css_task_iter_advance*() |
| 738 | * for details. |
| 739 | */ |
| 740 | list_for_each_entry_safe(it, pos, &from_cset->task_iters, |
| 741 | iters_node) |
| 742 | if (it->task_pos == &task->cg_list) |
| 743 | css_task_iter_advance(it); |
| 744 | |
| 745 | list_del_init(&task->cg_list); |
| 746 | if (!css_set_populated(from_cset)) |
| 747 | css_set_update_populated(from_cset, false); |
| 748 | } else { |
| 749 | WARN_ON_ONCE(!list_empty(&task->cg_list)); |
| 750 | } |
| 751 | |
| 752 | if (to_cset) { |
| 753 | /* |
| 754 | * We are synchronized through cgroup_threadgroup_rwsem |
| 755 | * against PF_EXITING setting such that we can't race |
| 756 | * against cgroup_exit() changing the css_set to |
| 757 | * init_css_set and dropping the old one. |
| 758 | */ |
| 759 | WARN_ON_ONCE(task->flags & PF_EXITING); |
| 760 | |
| 761 | rcu_assign_pointer(task->cgroups, to_cset); |
| 762 | list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks : |
| 763 | &to_cset->tasks); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * hash table for cgroup groups. This improves the performance to find |
| 769 | * an existing css_set. This hash doesn't (currently) take into |
| 770 | * account cgroups in empty hierarchies. |
| 771 | */ |
| 772 | #define CSS_SET_HASH_BITS 7 |
| 773 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
| 774 | |
| 775 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
| 776 | { |
| 777 | unsigned long key = 0UL; |
| 778 | struct cgroup_subsys *ss; |
| 779 | int i; |
| 780 | |
| 781 | for_each_subsys(ss, i) |
| 782 | key += (unsigned long)css[i]; |
| 783 | key = (key >> 16) ^ key; |
| 784 | |
| 785 | return key; |
| 786 | } |
| 787 | |
| 788 | static void put_css_set_locked(struct css_set *cset) |
| 789 | { |
| 790 | struct cgrp_cset_link *link, *tmp_link; |
| 791 | struct cgroup_subsys *ss; |
| 792 | int ssid; |
| 793 | |
| 794 | lockdep_assert_held(&css_set_lock); |
| 795 | |
| 796 | if (!atomic_dec_and_test(&cset->refcount)) |
| 797 | return; |
| 798 | |
| 799 | /* This css_set is dead. unlink it and release cgroup and css refs */ |
| 800 | for_each_subsys(ss, ssid) { |
| 801 | list_del(&cset->e_cset_node[ssid]); |
| 802 | css_put(cset->subsys[ssid]); |
| 803 | } |
| 804 | hash_del(&cset->hlist); |
| 805 | css_set_count--; |
| 806 | |
| 807 | list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { |
| 808 | list_del(&link->cset_link); |
| 809 | list_del(&link->cgrp_link); |
| 810 | if (cgroup_parent(link->cgrp)) |
| 811 | cgroup_put(link->cgrp); |
| 812 | kfree(link); |
| 813 | } |
| 814 | |
| 815 | kfree_rcu(cset, rcu_head); |
| 816 | } |
| 817 | |
| 818 | static void put_css_set(struct css_set *cset) |
| 819 | { |
| 820 | /* |
| 821 | * Ensure that the refcount doesn't hit zero while any readers |
| 822 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 823 | * rwlock |
| 824 | */ |
| 825 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
| 826 | return; |
| 827 | |
| 828 | spin_lock_bh(&css_set_lock); |
| 829 | put_css_set_locked(cset); |
| 830 | spin_unlock_bh(&css_set_lock); |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | * refcounted get/put for css_set objects |
| 835 | */ |
| 836 | static inline void get_css_set(struct css_set *cset) |
| 837 | { |
| 838 | atomic_inc(&cset->refcount); |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * compare_css_sets - helper function for find_existing_css_set(). |
| 843 | * @cset: candidate css_set being tested |
| 844 | * @old_cset: existing css_set for a task |
| 845 | * @new_cgrp: cgroup that's being entered by the task |
| 846 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 847 | * |
| 848 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
| 849 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 850 | */ |
| 851 | static bool compare_css_sets(struct css_set *cset, |
| 852 | struct css_set *old_cset, |
| 853 | struct cgroup *new_cgrp, |
| 854 | struct cgroup_subsys_state *template[]) |
| 855 | { |
| 856 | struct list_head *l1, *l2; |
| 857 | |
| 858 | /* |
| 859 | * On the default hierarchy, there can be csets which are |
| 860 | * associated with the same set of cgroups but different csses. |
| 861 | * Let's first ensure that csses match. |
| 862 | */ |
| 863 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) |
| 864 | return false; |
| 865 | |
| 866 | /* |
| 867 | * Compare cgroup pointers in order to distinguish between |
| 868 | * different cgroups in hierarchies. As different cgroups may |
| 869 | * share the same effective css, this comparison is always |
| 870 | * necessary. |
| 871 | */ |
| 872 | l1 = &cset->cgrp_links; |
| 873 | l2 = &old_cset->cgrp_links; |
| 874 | while (1) { |
| 875 | struct cgrp_cset_link *link1, *link2; |
| 876 | struct cgroup *cgrp1, *cgrp2; |
| 877 | |
| 878 | l1 = l1->next; |
| 879 | l2 = l2->next; |
| 880 | /* See if we reached the end - both lists are equal length. */ |
| 881 | if (l1 == &cset->cgrp_links) { |
| 882 | BUG_ON(l2 != &old_cset->cgrp_links); |
| 883 | break; |
| 884 | } else { |
| 885 | BUG_ON(l2 == &old_cset->cgrp_links); |
| 886 | } |
| 887 | /* Locate the cgroups associated with these links. */ |
| 888 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 889 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 890 | cgrp1 = link1->cgrp; |
| 891 | cgrp2 = link2->cgrp; |
| 892 | /* Hierarchies should be linked in the same order. */ |
| 893 | BUG_ON(cgrp1->root != cgrp2->root); |
| 894 | |
| 895 | /* |
| 896 | * If this hierarchy is the hierarchy of the cgroup |
| 897 | * that's changing, then we need to check that this |
| 898 | * css_set points to the new cgroup; if it's any other |
| 899 | * hierarchy, then this css_set should point to the |
| 900 | * same cgroup as the old css_set. |
| 901 | */ |
| 902 | if (cgrp1->root == new_cgrp->root) { |
| 903 | if (cgrp1 != new_cgrp) |
| 904 | return false; |
| 905 | } else { |
| 906 | if (cgrp1 != cgrp2) |
| 907 | return false; |
| 908 | } |
| 909 | } |
| 910 | return true; |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * find_existing_css_set - init css array and find the matching css_set |
| 915 | * @old_cset: the css_set that we're using before the cgroup transition |
| 916 | * @cgrp: the cgroup that we're moving into |
| 917 | * @template: out param for the new set of csses, should be clear on entry |
| 918 | */ |
| 919 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 920 | struct cgroup *cgrp, |
| 921 | struct cgroup_subsys_state *template[]) |
| 922 | { |
| 923 | struct cgroup_root *root = cgrp->root; |
| 924 | struct cgroup_subsys *ss; |
| 925 | struct css_set *cset; |
| 926 | unsigned long key; |
| 927 | int i; |
| 928 | |
| 929 | /* |
| 930 | * Build the set of subsystem state objects that we want to see in the |
| 931 | * new css_set. while subsystems can change globally, the entries here |
| 932 | * won't change, so no need for locking. |
| 933 | */ |
| 934 | for_each_subsys(ss, i) { |
| 935 | if (root->subsys_mask & (1UL << i)) { |
| 936 | /* |
| 937 | * @ss is in this hierarchy, so we want the |
| 938 | * effective css from @cgrp. |
| 939 | */ |
| 940 | template[i] = cgroup_e_css(cgrp, ss); |
| 941 | } else { |
| 942 | /* |
| 943 | * @ss is not in this hierarchy, so we don't want |
| 944 | * to change the css. |
| 945 | */ |
| 946 | template[i] = old_cset->subsys[i]; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | key = css_set_hash(template); |
| 951 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 952 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
| 953 | continue; |
| 954 | |
| 955 | /* This css_set matches what we need */ |
| 956 | return cset; |
| 957 | } |
| 958 | |
| 959 | /* No existing cgroup group matched */ |
| 960 | return NULL; |
| 961 | } |
| 962 | |
| 963 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
| 964 | { |
| 965 | struct cgrp_cset_link *link, *tmp_link; |
| 966 | |
| 967 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 968 | list_del(&link->cset_link); |
| 969 | kfree(link); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 975 | * @count: the number of links to allocate |
| 976 | * @tmp_links: list_head the allocated links are put on |
| 977 | * |
| 978 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 979 | * through ->cset_link. Returns 0 on success or -errno. |
| 980 | */ |
| 981 | static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links) |
| 982 | { |
| 983 | struct cgrp_cset_link *link; |
| 984 | int i; |
| 985 | |
| 986 | INIT_LIST_HEAD(tmp_links); |
| 987 | |
| 988 | for (i = 0; i < count; i++) { |
| 989 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 990 | if (!link) { |
| 991 | free_cgrp_cset_links(tmp_links); |
| 992 | return -ENOMEM; |
| 993 | } |
| 994 | list_add(&link->cset_link, tmp_links); |
| 995 | } |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * link_css_set - a helper function to link a css_set to a cgroup |
| 1001 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
| 1002 | * @cset: the css_set to be linked |
| 1003 | * @cgrp: the destination cgroup |
| 1004 | */ |
| 1005 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 1006 | struct cgroup *cgrp) |
| 1007 | { |
| 1008 | struct cgrp_cset_link *link; |
| 1009 | |
| 1010 | BUG_ON(list_empty(tmp_links)); |
| 1011 | |
| 1012 | if (cgroup_on_dfl(cgrp)) |
| 1013 | cset->dfl_cgrp = cgrp; |
| 1014 | |
| 1015 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 1016 | link->cset = cset; |
| 1017 | link->cgrp = cgrp; |
| 1018 | |
| 1019 | /* |
| 1020 | * Always add links to the tail of the lists so that the lists are |
| 1021 | * in choronological order. |
| 1022 | */ |
| 1023 | list_move_tail(&link->cset_link, &cgrp->cset_links); |
| 1024 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
| 1025 | |
| 1026 | if (cgroup_parent(cgrp)) |
| 1027 | cgroup_get(cgrp); |
| 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * find_css_set - return a new css_set with one cgroup updated |
| 1032 | * @old_cset: the baseline css_set |
| 1033 | * @cgrp: the cgroup to be updated |
| 1034 | * |
| 1035 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 1036 | * substituted into the appropriate hierarchy. |
| 1037 | */ |
| 1038 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 1039 | struct cgroup *cgrp) |
| 1040 | { |
| 1041 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
| 1042 | struct css_set *cset; |
| 1043 | struct list_head tmp_links; |
| 1044 | struct cgrp_cset_link *link; |
| 1045 | struct cgroup_subsys *ss; |
| 1046 | unsigned long key; |
| 1047 | int ssid; |
| 1048 | |
| 1049 | lockdep_assert_held(&cgroup_mutex); |
| 1050 | |
| 1051 | /* First see if we already have a cgroup group that matches |
| 1052 | * the desired set */ |
| 1053 | spin_lock_bh(&css_set_lock); |
| 1054 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 1055 | if (cset) |
| 1056 | get_css_set(cset); |
| 1057 | spin_unlock_bh(&css_set_lock); |
| 1058 | |
| 1059 | if (cset) |
| 1060 | return cset; |
| 1061 | |
| 1062 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
| 1063 | if (!cset) |
| 1064 | return NULL; |
| 1065 | |
| 1066 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
| 1067 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
| 1068 | kfree(cset); |
| 1069 | return NULL; |
| 1070 | } |
| 1071 | |
| 1072 | atomic_set(&cset->refcount, 1); |
| 1073 | INIT_LIST_HEAD(&cset->cgrp_links); |
| 1074 | INIT_LIST_HEAD(&cset->tasks); |
| 1075 | INIT_LIST_HEAD(&cset->mg_tasks); |
| 1076 | INIT_LIST_HEAD(&cset->mg_preload_node); |
| 1077 | INIT_LIST_HEAD(&cset->mg_node); |
| 1078 | INIT_LIST_HEAD(&cset->task_iters); |
| 1079 | INIT_HLIST_NODE(&cset->hlist); |
| 1080 | |
| 1081 | /* Copy the set of subsystem state objects generated in |
| 1082 | * find_existing_css_set() */ |
| 1083 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
| 1084 | |
| 1085 | spin_lock_bh(&css_set_lock); |
| 1086 | /* Add reference counts and links from the new css_set. */ |
| 1087 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
| 1088 | struct cgroup *c = link->cgrp; |
| 1089 | |
| 1090 | if (c->root == cgrp->root) |
| 1091 | c = cgrp; |
| 1092 | link_css_set(&tmp_links, cset, c); |
| 1093 | } |
| 1094 | |
| 1095 | BUG_ON(!list_empty(&tmp_links)); |
| 1096 | |
| 1097 | css_set_count++; |
| 1098 | |
| 1099 | /* Add @cset to the hash table */ |
| 1100 | key = css_set_hash(cset->subsys); |
| 1101 | hash_add(css_set_table, &cset->hlist, key); |
| 1102 | |
| 1103 | for_each_subsys(ss, ssid) { |
| 1104 | struct cgroup_subsys_state *css = cset->subsys[ssid]; |
| 1105 | |
| 1106 | list_add_tail(&cset->e_cset_node[ssid], |
| 1107 | &css->cgroup->e_csets[ssid]); |
| 1108 | css_get(css); |
| 1109 | } |
| 1110 | |
| 1111 | spin_unlock_bh(&css_set_lock); |
| 1112 | |
| 1113 | return cset; |
| 1114 | } |
| 1115 | |
| 1116 | static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) |
| 1117 | { |
| 1118 | struct cgroup *root_cgrp = kf_root->kn->priv; |
| 1119 | |
| 1120 | return root_cgrp->root; |
| 1121 | } |
| 1122 | |
| 1123 | static int cgroup_init_root_id(struct cgroup_root *root) |
| 1124 | { |
| 1125 | int id; |
| 1126 | |
| 1127 | lockdep_assert_held(&cgroup_mutex); |
| 1128 | |
| 1129 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); |
| 1130 | if (id < 0) |
| 1131 | return id; |
| 1132 | |
| 1133 | root->hierarchy_id = id; |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | static void cgroup_exit_root_id(struct cgroup_root *root) |
| 1138 | { |
| 1139 | lockdep_assert_held(&cgroup_mutex); |
| 1140 | |
| 1141 | if (root->hierarchy_id) { |
| 1142 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
| 1143 | root->hierarchy_id = 0; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | static void cgroup_free_root(struct cgroup_root *root) |
| 1148 | { |
| 1149 | if (root) { |
| 1150 | /* hierarchy ID should already have been released */ |
| 1151 | WARN_ON_ONCE(root->hierarchy_id); |
| 1152 | |
| 1153 | idr_destroy(&root->cgroup_idr); |
| 1154 | kfree(root); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | static void cgroup_destroy_root(struct cgroup_root *root) |
| 1159 | { |
| 1160 | struct cgroup *cgrp = &root->cgrp; |
| 1161 | struct cgrp_cset_link *link, *tmp_link; |
| 1162 | |
| 1163 | mutex_lock(&cgroup_mutex); |
| 1164 | |
| 1165 | BUG_ON(atomic_read(&root->nr_cgrps)); |
| 1166 | BUG_ON(!list_empty(&cgrp->self.children)); |
| 1167 | |
| 1168 | /* Rebind all subsystems back to the default hierarchy */ |
| 1169 | rebind_subsystems(&cgrp_dfl_root, root->subsys_mask); |
| 1170 | |
| 1171 | /* |
| 1172 | * Release all the links from cset_links to this hierarchy's |
| 1173 | * root cgroup |
| 1174 | */ |
| 1175 | spin_lock_bh(&css_set_lock); |
| 1176 | |
| 1177 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1178 | list_del(&link->cset_link); |
| 1179 | list_del(&link->cgrp_link); |
| 1180 | kfree(link); |
| 1181 | } |
| 1182 | |
| 1183 | spin_unlock_bh(&css_set_lock); |
| 1184 | |
| 1185 | if (!list_empty(&root->root_list)) { |
| 1186 | list_del(&root->root_list); |
| 1187 | cgroup_root_count--; |
| 1188 | } |
| 1189 | |
| 1190 | cgroup_exit_root_id(root); |
| 1191 | |
| 1192 | mutex_unlock(&cgroup_mutex); |
| 1193 | |
| 1194 | kernfs_destroy_root(root->kf_root); |
| 1195 | cgroup_free_root(root); |
| 1196 | } |
| 1197 | |
| 1198 | /* look up cgroup associated with given css_set on the specified hierarchy */ |
| 1199 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, |
| 1200 | struct cgroup_root *root) |
| 1201 | { |
| 1202 | struct cgroup *res = NULL; |
| 1203 | |
| 1204 | lockdep_assert_held(&cgroup_mutex); |
| 1205 | lockdep_assert_held(&css_set_lock); |
| 1206 | |
| 1207 | if (cset == &init_css_set) { |
| 1208 | res = &root->cgrp; |
| 1209 | } else { |
| 1210 | struct cgrp_cset_link *link; |
| 1211 | |
| 1212 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
| 1213 | struct cgroup *c = link->cgrp; |
| 1214 | |
| 1215 | if (c->root == root) { |
| 1216 | res = c; |
| 1217 | break; |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | BUG_ON(!res); |
| 1223 | return res; |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 1228 | * called with cgroup_mutex and css_set_lock held. |
| 1229 | */ |
| 1230 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 1231 | struct cgroup_root *root) |
| 1232 | { |
| 1233 | /* |
| 1234 | * No need to lock the task - since we hold cgroup_mutex the |
| 1235 | * task can't change groups, so the only thing that can happen |
| 1236 | * is that it exits and its css is set back to init_css_set. |
| 1237 | */ |
| 1238 | return cset_cgroup_from_root(task_css_set(task), root); |
| 1239 | } |
| 1240 | |
| 1241 | /* |
| 1242 | * A task must hold cgroup_mutex to modify cgroups. |
| 1243 | * |
| 1244 | * Any task can increment and decrement the count field without lock. |
| 1245 | * So in general, code holding cgroup_mutex can't rely on the count |
| 1246 | * field not changing. However, if the count goes to zero, then only |
| 1247 | * cgroup_attach_task() can increment it again. Because a count of zero |
| 1248 | * means that no tasks are currently attached, therefore there is no |
| 1249 | * way a task attached to that cgroup can fork (the other way to |
| 1250 | * increment the count). So code holding cgroup_mutex can safely |
| 1251 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 1252 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 1253 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 1254 | * needs that mutex. |
| 1255 | * |
| 1256 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 1257 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 1258 | * tasks in the system use _some_ cgroup, and since there is always at |
| 1259 | * least one task in the system (init, pid == 1), therefore, root cgroup |
| 1260 | * always has either children cgroups and/or using tasks. So we don't |
| 1261 | * need a special hack to ensure that root cgroup cannot be deleted. |
| 1262 | * |
| 1263 | * P.S. One more locking exception. RCU is used to guard the |
| 1264 | * update of a tasks cgroup pointer by cgroup_attach_task() |
| 1265 | */ |
| 1266 | |
| 1267 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
| 1268 | static const struct file_operations proc_cgroupstats_operations; |
| 1269 | |
| 1270 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 1271 | char *buf) |
| 1272 | { |
| 1273 | struct cgroup_subsys *ss = cft->ss; |
| 1274 | |
| 1275 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 1276 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) |
| 1277 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s", |
| 1278 | cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name, |
| 1279 | cft->name); |
| 1280 | else |
| 1281 | strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
| 1282 | return buf; |
| 1283 | } |
| 1284 | |
| 1285 | /** |
| 1286 | * cgroup_file_mode - deduce file mode of a control file |
| 1287 | * @cft: the control file in question |
| 1288 | * |
| 1289 | * S_IRUGO for read, S_IWUSR for write. |
| 1290 | */ |
| 1291 | static umode_t cgroup_file_mode(const struct cftype *cft) |
| 1292 | { |
| 1293 | umode_t mode = 0; |
| 1294 | |
| 1295 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 1296 | mode |= S_IRUGO; |
| 1297 | |
| 1298 | if (cft->write_u64 || cft->write_s64 || cft->write) { |
| 1299 | if (cft->flags & CFTYPE_WORLD_WRITABLE) |
| 1300 | mode |= S_IWUGO; |
| 1301 | else |
| 1302 | mode |= S_IWUSR; |
| 1303 | } |
| 1304 | |
| 1305 | return mode; |
| 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask |
| 1310 | * @cgrp: the target cgroup |
| 1311 | * @subtree_control: the new subtree_control mask to consider |
| 1312 | * |
| 1313 | * On the default hierarchy, a subsystem may request other subsystems to be |
| 1314 | * enabled together through its ->depends_on mask. In such cases, more |
| 1315 | * subsystems than specified in "cgroup.subtree_control" may be enabled. |
| 1316 | * |
| 1317 | * This function calculates which subsystems need to be enabled if |
| 1318 | * @subtree_control is to be applied to @cgrp. The returned mask is always |
| 1319 | * a superset of @subtree_control and follows the usual hierarchy rules. |
| 1320 | */ |
| 1321 | static u16 cgroup_calc_subtree_ss_mask(struct cgroup *cgrp, u16 subtree_control) |
| 1322 | { |
| 1323 | u16 cur_ss_mask = subtree_control; |
| 1324 | struct cgroup_subsys *ss; |
| 1325 | int ssid; |
| 1326 | |
| 1327 | lockdep_assert_held(&cgroup_mutex); |
| 1328 | |
| 1329 | if (!cgroup_on_dfl(cgrp)) |
| 1330 | return cur_ss_mask; |
| 1331 | |
| 1332 | while (true) { |
| 1333 | u16 new_ss_mask = cur_ss_mask; |
| 1334 | |
| 1335 | do_each_subsys_mask(ss, ssid, cur_ss_mask) { |
| 1336 | new_ss_mask |= ss->depends_on; |
| 1337 | } while_each_subsys_mask(); |
| 1338 | |
| 1339 | /* |
| 1340 | * Mask out subsystems which aren't available. This can |
| 1341 | * happen only if some depended-upon subsystems were bound |
| 1342 | * to non-default hierarchies. |
| 1343 | */ |
| 1344 | new_ss_mask &= cgroup_ss_mask(cgrp); |
| 1345 | |
| 1346 | if (new_ss_mask == cur_ss_mask) |
| 1347 | break; |
| 1348 | cur_ss_mask = new_ss_mask; |
| 1349 | } |
| 1350 | |
| 1351 | return cur_ss_mask; |
| 1352 | } |
| 1353 | |
| 1354 | /** |
| 1355 | * cgroup_refresh_subtree_ss_mask - update subtree_ss_mask |
| 1356 | * @cgrp: the target cgroup |
| 1357 | * |
| 1358 | * Update @cgrp->subtree_ss_mask according to the current |
| 1359 | * @cgrp->subtree_control using cgroup_calc_subtree_ss_mask(). |
| 1360 | */ |
| 1361 | static void cgroup_refresh_subtree_ss_mask(struct cgroup *cgrp) |
| 1362 | { |
| 1363 | cgrp->subtree_ss_mask = |
| 1364 | cgroup_calc_subtree_ss_mask(cgrp, cgrp->subtree_control); |
| 1365 | } |
| 1366 | |
| 1367 | /** |
| 1368 | * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods |
| 1369 | * @kn: the kernfs_node being serviced |
| 1370 | * |
| 1371 | * This helper undoes cgroup_kn_lock_live() and should be invoked before |
| 1372 | * the method finishes if locking succeeded. Note that once this function |
| 1373 | * returns the cgroup returned by cgroup_kn_lock_live() may become |
| 1374 | * inaccessible any time. If the caller intends to continue to access the |
| 1375 | * cgroup, it should pin it before invoking this function. |
| 1376 | */ |
| 1377 | static void cgroup_kn_unlock(struct kernfs_node *kn) |
| 1378 | { |
| 1379 | struct cgroup *cgrp; |
| 1380 | |
| 1381 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1382 | cgrp = kn->priv; |
| 1383 | else |
| 1384 | cgrp = kn->parent->priv; |
| 1385 | |
| 1386 | mutex_unlock(&cgroup_mutex); |
| 1387 | |
| 1388 | kernfs_unbreak_active_protection(kn); |
| 1389 | cgroup_put(cgrp); |
| 1390 | } |
| 1391 | |
| 1392 | /** |
| 1393 | * cgroup_kn_lock_live - locking helper for cgroup kernfs methods |
| 1394 | * @kn: the kernfs_node being serviced |
| 1395 | * @drain_offline: perform offline draining on the cgroup |
| 1396 | * |
| 1397 | * This helper is to be used by a cgroup kernfs method currently servicing |
| 1398 | * @kn. It breaks the active protection, performs cgroup locking and |
| 1399 | * verifies that the associated cgroup is alive. Returns the cgroup if |
| 1400 | * alive; otherwise, %NULL. A successful return should be undone by a |
| 1401 | * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the |
| 1402 | * cgroup is drained of offlining csses before return. |
| 1403 | * |
| 1404 | * Any cgroup kernfs method implementation which requires locking the |
| 1405 | * associated cgroup should use this helper. It avoids nesting cgroup |
| 1406 | * locking under kernfs active protection and allows all kernfs operations |
| 1407 | * including self-removal. |
| 1408 | */ |
| 1409 | static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, |
| 1410 | bool drain_offline) |
| 1411 | { |
| 1412 | struct cgroup *cgrp; |
| 1413 | |
| 1414 | if (kernfs_type(kn) == KERNFS_DIR) |
| 1415 | cgrp = kn->priv; |
| 1416 | else |
| 1417 | cgrp = kn->parent->priv; |
| 1418 | |
| 1419 | /* |
| 1420 | * We're gonna grab cgroup_mutex which nests outside kernfs |
| 1421 | * active_ref. cgroup liveliness check alone provides enough |
| 1422 | * protection against removal. Ensure @cgrp stays accessible and |
| 1423 | * break the active_ref protection. |
| 1424 | */ |
| 1425 | if (!cgroup_tryget(cgrp)) |
| 1426 | return NULL; |
| 1427 | kernfs_break_active_protection(kn); |
| 1428 | |
| 1429 | if (drain_offline) |
| 1430 | cgroup_lock_and_drain_offline(cgrp); |
| 1431 | else |
| 1432 | mutex_lock(&cgroup_mutex); |
| 1433 | |
| 1434 | if (!cgroup_is_dead(cgrp)) |
| 1435 | return cgrp; |
| 1436 | |
| 1437 | cgroup_kn_unlock(kn); |
| 1438 | return NULL; |
| 1439 | } |
| 1440 | |
| 1441 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
| 1442 | { |
| 1443 | char name[CGROUP_FILE_NAME_MAX]; |
| 1444 | |
| 1445 | lockdep_assert_held(&cgroup_mutex); |
| 1446 | |
| 1447 | if (cft->file_offset) { |
| 1448 | struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss); |
| 1449 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 1450 | |
| 1451 | spin_lock_irq(&cgroup_file_kn_lock); |
| 1452 | cfile->kn = NULL; |
| 1453 | spin_unlock_irq(&cgroup_file_kn_lock); |
| 1454 | } |
| 1455 | |
| 1456 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
| 1457 | } |
| 1458 | |
| 1459 | /** |
| 1460 | * css_clear_dir - remove subsys files in a cgroup directory |
| 1461 | * @css: taget css |
| 1462 | * @cgrp_override: specify if target cgroup is different from css->cgroup |
| 1463 | */ |
| 1464 | static void css_clear_dir(struct cgroup_subsys_state *css, |
| 1465 | struct cgroup *cgrp_override) |
| 1466 | { |
| 1467 | struct cgroup *cgrp = cgrp_override ?: css->cgroup; |
| 1468 | struct cftype *cfts; |
| 1469 | |
| 1470 | if (!(css->flags & CSS_VISIBLE)) |
| 1471 | return; |
| 1472 | |
| 1473 | css->flags &= ~CSS_VISIBLE; |
| 1474 | |
| 1475 | list_for_each_entry(cfts, &css->ss->cfts, node) |
| 1476 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1477 | } |
| 1478 | |
| 1479 | /** |
| 1480 | * css_populate_dir - create subsys files in a cgroup directory |
| 1481 | * @css: target css |
| 1482 | * @cgrp_overried: specify if target cgroup is different from css->cgroup |
| 1483 | * |
| 1484 | * On failure, no file is added. |
| 1485 | */ |
| 1486 | static int css_populate_dir(struct cgroup_subsys_state *css, |
| 1487 | struct cgroup *cgrp_override) |
| 1488 | { |
| 1489 | struct cgroup *cgrp = cgrp_override ?: css->cgroup; |
| 1490 | struct cftype *cfts, *failed_cfts; |
| 1491 | int ret; |
| 1492 | |
| 1493 | if ((css->flags & CSS_VISIBLE) || !cgrp->kn) |
| 1494 | return 0; |
| 1495 | |
| 1496 | if (!css->ss) { |
| 1497 | if (cgroup_on_dfl(cgrp)) |
| 1498 | cfts = cgroup_dfl_base_files; |
| 1499 | else |
| 1500 | cfts = cgroup_legacy_base_files; |
| 1501 | |
| 1502 | return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true); |
| 1503 | } |
| 1504 | |
| 1505 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1506 | ret = cgroup_addrm_files(css, cgrp, cfts, true); |
| 1507 | if (ret < 0) { |
| 1508 | failed_cfts = cfts; |
| 1509 | goto err; |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | css->flags |= CSS_VISIBLE; |
| 1514 | |
| 1515 | return 0; |
| 1516 | err: |
| 1517 | list_for_each_entry(cfts, &css->ss->cfts, node) { |
| 1518 | if (cfts == failed_cfts) |
| 1519 | break; |
| 1520 | cgroup_addrm_files(css, cgrp, cfts, false); |
| 1521 | } |
| 1522 | return ret; |
| 1523 | } |
| 1524 | |
| 1525 | static int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask) |
| 1526 | { |
| 1527 | struct cgroup *dcgrp = &dst_root->cgrp; |
| 1528 | struct cgroup_subsys *ss; |
| 1529 | u16 tmp_ss_mask; |
| 1530 | int ssid, i, ret; |
| 1531 | |
| 1532 | lockdep_assert_held(&cgroup_mutex); |
| 1533 | |
| 1534 | do_each_subsys_mask(ss, ssid, ss_mask) { |
| 1535 | /* if @ss has non-root csses attached to it, can't move */ |
| 1536 | if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss))) |
| 1537 | return -EBUSY; |
| 1538 | |
| 1539 | /* can't move between two non-dummy roots either */ |
| 1540 | if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) |
| 1541 | return -EBUSY; |
| 1542 | } while_each_subsys_mask(); |
| 1543 | |
| 1544 | /* skip creating root files on dfl_root for inhibited subsystems */ |
| 1545 | tmp_ss_mask = ss_mask; |
| 1546 | if (dst_root == &cgrp_dfl_root) |
| 1547 | tmp_ss_mask &= ~cgrp_dfl_inhibit_ss_mask; |
| 1548 | |
| 1549 | do_each_subsys_mask(ss, ssid, tmp_ss_mask) { |
| 1550 | struct cgroup *scgrp = &ss->root->cgrp; |
| 1551 | int tssid; |
| 1552 | |
| 1553 | ret = css_populate_dir(cgroup_css(scgrp, ss), dcgrp); |
| 1554 | if (!ret) |
| 1555 | continue; |
| 1556 | |
| 1557 | /* |
| 1558 | * Rebinding back to the default root is not allowed to |
| 1559 | * fail. Using both default and non-default roots should |
| 1560 | * be rare. Moving subsystems back and forth even more so. |
| 1561 | * Just warn about it and continue. |
| 1562 | */ |
| 1563 | if (dst_root == &cgrp_dfl_root) { |
| 1564 | if (cgrp_dfl_visible) { |
| 1565 | pr_warn("failed to create files (%d) while rebinding 0x%x to default root\n", |
| 1566 | ret, ss_mask); |
| 1567 | pr_warn("you may retry by moving them to a different hierarchy and unbinding\n"); |
| 1568 | } |
| 1569 | continue; |
| 1570 | } |
| 1571 | |
| 1572 | do_each_subsys_mask(ss, tssid, tmp_ss_mask) { |
| 1573 | if (tssid == ssid) |
| 1574 | break; |
| 1575 | css_clear_dir(cgroup_css(scgrp, ss), dcgrp); |
| 1576 | } while_each_subsys_mask(); |
| 1577 | return ret; |
| 1578 | } while_each_subsys_mask(); |
| 1579 | |
| 1580 | /* |
| 1581 | * Nothing can fail from this point on. Remove files for the |
| 1582 | * removed subsystems and rebind each subsystem. |
| 1583 | */ |
| 1584 | do_each_subsys_mask(ss, ssid, ss_mask) { |
| 1585 | struct cgroup_root *src_root = ss->root; |
| 1586 | struct cgroup *scgrp = &src_root->cgrp; |
| 1587 | struct cgroup_subsys_state *css = cgroup_css(scgrp, ss); |
| 1588 | struct css_set *cset; |
| 1589 | |
| 1590 | WARN_ON(!css || cgroup_css(dcgrp, ss)); |
| 1591 | |
| 1592 | css_clear_dir(css, NULL); |
| 1593 | |
| 1594 | RCU_INIT_POINTER(scgrp->subsys[ssid], NULL); |
| 1595 | rcu_assign_pointer(dcgrp->subsys[ssid], css); |
| 1596 | ss->root = dst_root; |
| 1597 | css->cgroup = dcgrp; |
| 1598 | |
| 1599 | spin_lock_bh(&css_set_lock); |
| 1600 | hash_for_each(css_set_table, i, cset, hlist) |
| 1601 | list_move_tail(&cset->e_cset_node[ss->id], |
| 1602 | &dcgrp->e_csets[ss->id]); |
| 1603 | spin_unlock_bh(&css_set_lock); |
| 1604 | |
| 1605 | src_root->subsys_mask &= ~(1 << ssid); |
| 1606 | scgrp->subtree_control &= ~(1 << ssid); |
| 1607 | cgroup_refresh_subtree_ss_mask(scgrp); |
| 1608 | |
| 1609 | /* default hierarchy doesn't enable controllers by default */ |
| 1610 | dst_root->subsys_mask |= 1 << ssid; |
| 1611 | if (dst_root == &cgrp_dfl_root) { |
| 1612 | static_branch_enable(cgroup_subsys_on_dfl_key[ssid]); |
| 1613 | } else { |
| 1614 | dcgrp->subtree_control |= 1 << ssid; |
| 1615 | cgroup_refresh_subtree_ss_mask(dcgrp); |
| 1616 | static_branch_disable(cgroup_subsys_on_dfl_key[ssid]); |
| 1617 | } |
| 1618 | |
| 1619 | if (ss->bind) |
| 1620 | ss->bind(css); |
| 1621 | } while_each_subsys_mask(); |
| 1622 | |
| 1623 | kernfs_activate(dcgrp->kn); |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | static int cgroup_show_options(struct seq_file *seq, |
| 1628 | struct kernfs_root *kf_root) |
| 1629 | { |
| 1630 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
| 1631 | struct cgroup_subsys *ss; |
| 1632 | int ssid; |
| 1633 | |
| 1634 | if (root != &cgrp_dfl_root) |
| 1635 | for_each_subsys(ss, ssid) |
| 1636 | if (root->subsys_mask & (1 << ssid)) |
| 1637 | seq_show_option(seq, ss->legacy_name, NULL); |
| 1638 | if (root->flags & CGRP_ROOT_NOPREFIX) |
| 1639 | seq_puts(seq, ",noprefix"); |
| 1640 | if (root->flags & CGRP_ROOT_XATTR) |
| 1641 | seq_puts(seq, ",xattr"); |
| 1642 | |
| 1643 | spin_lock(&release_agent_path_lock); |
| 1644 | if (strlen(root->release_agent_path)) |
| 1645 | seq_show_option(seq, "release_agent", |
| 1646 | root->release_agent_path); |
| 1647 | spin_unlock(&release_agent_path_lock); |
| 1648 | |
| 1649 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags)) |
| 1650 | seq_puts(seq, ",clone_children"); |
| 1651 | if (strlen(root->name)) |
| 1652 | seq_show_option(seq, "name", root->name); |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | struct cgroup_sb_opts { |
| 1657 | u16 subsys_mask; |
| 1658 | unsigned int flags; |
| 1659 | char *release_agent; |
| 1660 | bool cpuset_clone_children; |
| 1661 | char *name; |
| 1662 | /* User explicitly requested empty subsystem */ |
| 1663 | bool none; |
| 1664 | }; |
| 1665 | |
| 1666 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
| 1667 | { |
| 1668 | char *token, *o = data; |
| 1669 | bool all_ss = false, one_ss = false; |
| 1670 | u16 mask = U16_MAX; |
| 1671 | struct cgroup_subsys *ss; |
| 1672 | int nr_opts = 0; |
| 1673 | int i; |
| 1674 | |
| 1675 | #ifdef CONFIG_CPUSETS |
| 1676 | mask = ~((u16)1 << cpuset_cgrp_id); |
| 1677 | #endif |
| 1678 | |
| 1679 | memset(opts, 0, sizeof(*opts)); |
| 1680 | |
| 1681 | while ((token = strsep(&o, ",")) != NULL) { |
| 1682 | nr_opts++; |
| 1683 | |
| 1684 | if (!*token) |
| 1685 | return -EINVAL; |
| 1686 | if (!strcmp(token, "none")) { |
| 1687 | /* Explicitly have no subsystems */ |
| 1688 | opts->none = true; |
| 1689 | continue; |
| 1690 | } |
| 1691 | if (!strcmp(token, "all")) { |
| 1692 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1693 | if (one_ss) |
| 1694 | return -EINVAL; |
| 1695 | all_ss = true; |
| 1696 | continue; |
| 1697 | } |
| 1698 | if (!strcmp(token, "noprefix")) { |
| 1699 | opts->flags |= CGRP_ROOT_NOPREFIX; |
| 1700 | continue; |
| 1701 | } |
| 1702 | if (!strcmp(token, "clone_children")) { |
| 1703 | opts->cpuset_clone_children = true; |
| 1704 | continue; |
| 1705 | } |
| 1706 | if (!strcmp(token, "xattr")) { |
| 1707 | opts->flags |= CGRP_ROOT_XATTR; |
| 1708 | continue; |
| 1709 | } |
| 1710 | if (!strncmp(token, "release_agent=", 14)) { |
| 1711 | /* Specifying two release agents is forbidden */ |
| 1712 | if (opts->release_agent) |
| 1713 | return -EINVAL; |
| 1714 | opts->release_agent = |
| 1715 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
| 1716 | if (!opts->release_agent) |
| 1717 | return -ENOMEM; |
| 1718 | continue; |
| 1719 | } |
| 1720 | if (!strncmp(token, "name=", 5)) { |
| 1721 | const char *name = token + 5; |
| 1722 | /* Can't specify an empty name */ |
| 1723 | if (!strlen(name)) |
| 1724 | return -EINVAL; |
| 1725 | /* Must match [\w.-]+ */ |
| 1726 | for (i = 0; i < strlen(name); i++) { |
| 1727 | char c = name[i]; |
| 1728 | if (isalnum(c)) |
| 1729 | continue; |
| 1730 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1731 | continue; |
| 1732 | return -EINVAL; |
| 1733 | } |
| 1734 | /* Specifying two names is forbidden */ |
| 1735 | if (opts->name) |
| 1736 | return -EINVAL; |
| 1737 | opts->name = kstrndup(name, |
| 1738 | MAX_CGROUP_ROOT_NAMELEN - 1, |
| 1739 | GFP_KERNEL); |
| 1740 | if (!opts->name) |
| 1741 | return -ENOMEM; |
| 1742 | |
| 1743 | continue; |
| 1744 | } |
| 1745 | |
| 1746 | for_each_subsys(ss, i) { |
| 1747 | if (strcmp(token, ss->legacy_name)) |
| 1748 | continue; |
| 1749 | if (!cgroup_ssid_enabled(i)) |
| 1750 | continue; |
| 1751 | if (cgroup_ssid_no_v1(i)) |
| 1752 | continue; |
| 1753 | |
| 1754 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1755 | if (all_ss) |
| 1756 | return -EINVAL; |
| 1757 | opts->subsys_mask |= (1 << i); |
| 1758 | one_ss = true; |
| 1759 | |
| 1760 | break; |
| 1761 | } |
| 1762 | if (i == CGROUP_SUBSYS_COUNT) |
| 1763 | return -ENOENT; |
| 1764 | } |
| 1765 | |
| 1766 | /* |
| 1767 | * If the 'all' option was specified select all the subsystems, |
| 1768 | * otherwise if 'none', 'name=' and a subsystem name options were |
| 1769 | * not specified, let's default to 'all' |
| 1770 | */ |
| 1771 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1772 | for_each_subsys(ss, i) |
| 1773 | if (cgroup_ssid_enabled(i) && !cgroup_ssid_no_v1(i)) |
| 1774 | opts->subsys_mask |= (1 << i); |
| 1775 | |
| 1776 | /* |
| 1777 | * We either have to specify by name or by subsystems. (So all |
| 1778 | * empty hierarchies must have a name). |
| 1779 | */ |
| 1780 | if (!opts->subsys_mask && !opts->name) |
| 1781 | return -EINVAL; |
| 1782 | |
| 1783 | /* |
| 1784 | * Option noprefix was introduced just for backward compatibility |
| 1785 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1786 | * the cpuset subsystem. |
| 1787 | */ |
| 1788 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
| 1789 | return -EINVAL; |
| 1790 | |
| 1791 | /* Can't specify "none" and some subsystems */ |
| 1792 | if (opts->subsys_mask && opts->none) |
| 1793 | return -EINVAL; |
| 1794 | |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data) |
| 1799 | { |
| 1800 | int ret = 0; |
| 1801 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
| 1802 | struct cgroup_sb_opts opts; |
| 1803 | u16 added_mask, removed_mask; |
| 1804 | |
| 1805 | if (root == &cgrp_dfl_root) { |
| 1806 | pr_err("remount is not allowed\n"); |
| 1807 | return -EINVAL; |
| 1808 | } |
| 1809 | |
| 1810 | mutex_lock(&cgroup_mutex); |
| 1811 | |
| 1812 | /* See what subsystems are wanted */ |
| 1813 | ret = parse_cgroupfs_options(data, &opts); |
| 1814 | if (ret) |
| 1815 | goto out_unlock; |
| 1816 | |
| 1817 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
| 1818 | pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1819 | task_tgid_nr(current), current->comm); |
| 1820 | |
| 1821 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1822 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
| 1823 | |
| 1824 | /* Don't allow flags or name to change at remount */ |
| 1825 | if ((opts.flags ^ root->flags) || |
| 1826 | (opts.name && strcmp(opts.name, root->name))) { |
| 1827 | pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n", |
| 1828 | opts.flags, opts.name ?: "", root->flags, root->name); |
| 1829 | ret = -EINVAL; |
| 1830 | goto out_unlock; |
| 1831 | } |
| 1832 | |
| 1833 | /* remounting is not allowed for populated hierarchies */ |
| 1834 | if (!list_empty(&root->cgrp.self.children)) { |
| 1835 | ret = -EBUSY; |
| 1836 | goto out_unlock; |
| 1837 | } |
| 1838 | |
| 1839 | ret = rebind_subsystems(root, added_mask); |
| 1840 | if (ret) |
| 1841 | goto out_unlock; |
| 1842 | |
| 1843 | rebind_subsystems(&cgrp_dfl_root, removed_mask); |
| 1844 | |
| 1845 | if (opts.release_agent) { |
| 1846 | spin_lock(&release_agent_path_lock); |
| 1847 | strcpy(root->release_agent_path, opts.release_agent); |
| 1848 | spin_unlock(&release_agent_path_lock); |
| 1849 | } |
| 1850 | out_unlock: |
| 1851 | kfree(opts.release_agent); |
| 1852 | kfree(opts.name); |
| 1853 | mutex_unlock(&cgroup_mutex); |
| 1854 | return ret; |
| 1855 | } |
| 1856 | |
| 1857 | /* |
| 1858 | * To reduce the fork() overhead for systems that are not actually using |
| 1859 | * their cgroups capability, we don't maintain the lists running through |
| 1860 | * each css_set to its tasks until we see the list actually used - in other |
| 1861 | * words after the first mount. |
| 1862 | */ |
| 1863 | static bool use_task_css_set_links __read_mostly; |
| 1864 | |
| 1865 | static void cgroup_enable_task_cg_lists(void) |
| 1866 | { |
| 1867 | struct task_struct *p, *g; |
| 1868 | |
| 1869 | spin_lock_bh(&css_set_lock); |
| 1870 | |
| 1871 | if (use_task_css_set_links) |
| 1872 | goto out_unlock; |
| 1873 | |
| 1874 | use_task_css_set_links = true; |
| 1875 | |
| 1876 | /* |
| 1877 | * We need tasklist_lock because RCU is not safe against |
| 1878 | * while_each_thread(). Besides, a forking task that has passed |
| 1879 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 1880 | * is not guaranteed to have its child immediately visible in the |
| 1881 | * tasklist if we walk through it with RCU. |
| 1882 | */ |
| 1883 | read_lock(&tasklist_lock); |
| 1884 | do_each_thread(g, p) { |
| 1885 | WARN_ON_ONCE(!list_empty(&p->cg_list) || |
| 1886 | task_css_set(p) != &init_css_set); |
| 1887 | |
| 1888 | /* |
| 1889 | * We should check if the process is exiting, otherwise |
| 1890 | * it will race with cgroup_exit() in that the list |
| 1891 | * entry won't be deleted though the process has exited. |
| 1892 | * Do it while holding siglock so that we don't end up |
| 1893 | * racing against cgroup_exit(). |
| 1894 | */ |
| 1895 | spin_lock_irq(&p->sighand->siglock); |
| 1896 | if (!(p->flags & PF_EXITING)) { |
| 1897 | struct css_set *cset = task_css_set(p); |
| 1898 | |
| 1899 | if (!css_set_populated(cset)) |
| 1900 | css_set_update_populated(cset, true); |
| 1901 | list_add_tail(&p->cg_list, &cset->tasks); |
| 1902 | get_css_set(cset); |
| 1903 | } |
| 1904 | spin_unlock_irq(&p->sighand->siglock); |
| 1905 | } while_each_thread(g, p); |
| 1906 | read_unlock(&tasklist_lock); |
| 1907 | out_unlock: |
| 1908 | spin_unlock_bh(&css_set_lock); |
| 1909 | } |
| 1910 | |
| 1911 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1912 | { |
| 1913 | struct cgroup_subsys *ss; |
| 1914 | int ssid; |
| 1915 | |
| 1916 | INIT_LIST_HEAD(&cgrp->self.sibling); |
| 1917 | INIT_LIST_HEAD(&cgrp->self.children); |
| 1918 | INIT_LIST_HEAD(&cgrp->cset_links); |
| 1919 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1920 | mutex_init(&cgrp->pidlist_mutex); |
| 1921 | cgrp->self.cgroup = cgrp; |
| 1922 | cgrp->self.flags |= CSS_ONLINE; |
| 1923 | |
| 1924 | for_each_subsys(ss, ssid) |
| 1925 | INIT_LIST_HEAD(&cgrp->e_csets[ssid]); |
| 1926 | |
| 1927 | init_waitqueue_head(&cgrp->offline_waitq); |
| 1928 | INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent); |
| 1929 | } |
| 1930 | |
| 1931 | static void init_cgroup_root(struct cgroup_root *root, |
| 1932 | struct cgroup_sb_opts *opts) |
| 1933 | { |
| 1934 | struct cgroup *cgrp = &root->cgrp; |
| 1935 | |
| 1936 | INIT_LIST_HEAD(&root->root_list); |
| 1937 | atomic_set(&root->nr_cgrps, 1); |
| 1938 | cgrp->root = root; |
| 1939 | init_cgroup_housekeeping(cgrp); |
| 1940 | idr_init(&root->cgroup_idr); |
| 1941 | |
| 1942 | root->flags = opts->flags; |
| 1943 | if (opts->release_agent) |
| 1944 | strcpy(root->release_agent_path, opts->release_agent); |
| 1945 | if (opts->name) |
| 1946 | strcpy(root->name, opts->name); |
| 1947 | if (opts->cpuset_clone_children) |
| 1948 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); |
| 1949 | } |
| 1950 | |
| 1951 | static int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask) |
| 1952 | { |
| 1953 | LIST_HEAD(tmp_links); |
| 1954 | struct cgroup *root_cgrp = &root->cgrp; |
| 1955 | struct css_set *cset; |
| 1956 | int i, ret; |
| 1957 | |
| 1958 | lockdep_assert_held(&cgroup_mutex); |
| 1959 | |
| 1960 | ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL); |
| 1961 | if (ret < 0) |
| 1962 | goto out; |
| 1963 | root_cgrp->id = ret; |
| 1964 | root_cgrp->ancestor_ids[0] = ret; |
| 1965 | |
| 1966 | ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0, |
| 1967 | GFP_KERNEL); |
| 1968 | if (ret) |
| 1969 | goto out; |
| 1970 | |
| 1971 | /* |
| 1972 | * We're accessing css_set_count without locking css_set_lock here, |
| 1973 | * but that's OK - it can only be increased by someone holding |
| 1974 | * cgroup_lock, and that's us. The worst that can happen is that we |
| 1975 | * have some link structures left over |
| 1976 | */ |
| 1977 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
| 1978 | if (ret) |
| 1979 | goto cancel_ref; |
| 1980 | |
| 1981 | ret = cgroup_init_root_id(root); |
| 1982 | if (ret) |
| 1983 | goto cancel_ref; |
| 1984 | |
| 1985 | root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops, |
| 1986 | KERNFS_ROOT_CREATE_DEACTIVATED, |
| 1987 | root_cgrp); |
| 1988 | if (IS_ERR(root->kf_root)) { |
| 1989 | ret = PTR_ERR(root->kf_root); |
| 1990 | goto exit_root_id; |
| 1991 | } |
| 1992 | root_cgrp->kn = root->kf_root->kn; |
| 1993 | |
| 1994 | ret = css_populate_dir(&root_cgrp->self, NULL); |
| 1995 | if (ret) |
| 1996 | goto destroy_root; |
| 1997 | |
| 1998 | ret = rebind_subsystems(root, ss_mask); |
| 1999 | if (ret) |
| 2000 | goto destroy_root; |
| 2001 | |
| 2002 | /* |
| 2003 | * There must be no failure case after here, since rebinding takes |
| 2004 | * care of subsystems' refcounts, which are explicitly dropped in |
| 2005 | * the failure exit path. |
| 2006 | */ |
| 2007 | list_add(&root->root_list, &cgroup_roots); |
| 2008 | cgroup_root_count++; |
| 2009 | |
| 2010 | /* |
| 2011 | * Link the root cgroup in this hierarchy into all the css_set |
| 2012 | * objects. |
| 2013 | */ |
| 2014 | spin_lock_bh(&css_set_lock); |
| 2015 | hash_for_each(css_set_table, i, cset, hlist) { |
| 2016 | link_css_set(&tmp_links, cset, root_cgrp); |
| 2017 | if (css_set_populated(cset)) |
| 2018 | cgroup_update_populated(root_cgrp, true); |
| 2019 | } |
| 2020 | spin_unlock_bh(&css_set_lock); |
| 2021 | |
| 2022 | BUG_ON(!list_empty(&root_cgrp->self.children)); |
| 2023 | BUG_ON(atomic_read(&root->nr_cgrps) != 1); |
| 2024 | |
| 2025 | kernfs_activate(root_cgrp->kn); |
| 2026 | ret = 0; |
| 2027 | goto out; |
| 2028 | |
| 2029 | destroy_root: |
| 2030 | kernfs_destroy_root(root->kf_root); |
| 2031 | root->kf_root = NULL; |
| 2032 | exit_root_id: |
| 2033 | cgroup_exit_root_id(root); |
| 2034 | cancel_ref: |
| 2035 | percpu_ref_exit(&root_cgrp->self.refcnt); |
| 2036 | out: |
| 2037 | free_cgrp_cset_links(&tmp_links); |
| 2038 | return ret; |
| 2039 | } |
| 2040 | |
| 2041 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
| 2042 | int flags, const char *unused_dev_name, |
| 2043 | void *data) |
| 2044 | { |
| 2045 | bool is_v2 = fs_type == &cgroup2_fs_type; |
| 2046 | struct super_block *pinned_sb = NULL; |
| 2047 | struct cgroup_subsys *ss; |
| 2048 | struct cgroup_root *root; |
| 2049 | struct cgroup_sb_opts opts; |
| 2050 | struct dentry *dentry; |
| 2051 | int ret; |
| 2052 | int i; |
| 2053 | bool new_sb; |
| 2054 | |
| 2055 | /* |
| 2056 | * The first time anyone tries to mount a cgroup, enable the list |
| 2057 | * linking each css_set to its tasks and fix up all existing tasks. |
| 2058 | */ |
| 2059 | if (!use_task_css_set_links) |
| 2060 | cgroup_enable_task_cg_lists(); |
| 2061 | |
| 2062 | if (is_v2) { |
| 2063 | if (data) { |
| 2064 | pr_err("cgroup2: unknown option \"%s\"\n", (char *)data); |
| 2065 | return ERR_PTR(-EINVAL); |
| 2066 | } |
| 2067 | cgrp_dfl_visible = true; |
| 2068 | root = &cgrp_dfl_root; |
| 2069 | cgroup_get(&root->cgrp); |
| 2070 | goto out_mount; |
| 2071 | } |
| 2072 | |
| 2073 | mutex_lock(&cgroup_mutex); |
| 2074 | |
| 2075 | /* First find the desired set of subsystems */ |
| 2076 | ret = parse_cgroupfs_options(data, &opts); |
| 2077 | if (ret) |
| 2078 | goto out_unlock; |
| 2079 | |
| 2080 | /* |
| 2081 | * Destruction of cgroup root is asynchronous, so subsystems may |
| 2082 | * still be dying after the previous unmount. Let's drain the |
| 2083 | * dying subsystems. We just need to ensure that the ones |
| 2084 | * unmounted previously finish dying and don't care about new ones |
| 2085 | * starting. Testing ref liveliness is good enough. |
| 2086 | */ |
| 2087 | for_each_subsys(ss, i) { |
| 2088 | if (!(opts.subsys_mask & (1 << i)) || |
| 2089 | ss->root == &cgrp_dfl_root) |
| 2090 | continue; |
| 2091 | |
| 2092 | if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) { |
| 2093 | mutex_unlock(&cgroup_mutex); |
| 2094 | msleep(10); |
| 2095 | ret = restart_syscall(); |
| 2096 | goto out_free; |
| 2097 | } |
| 2098 | cgroup_put(&ss->root->cgrp); |
| 2099 | } |
| 2100 | |
| 2101 | for_each_root(root) { |
| 2102 | bool name_match = false; |
| 2103 | |
| 2104 | if (root == &cgrp_dfl_root) |
| 2105 | continue; |
| 2106 | |
| 2107 | /* |
| 2108 | * If we asked for a name then it must match. Also, if |
| 2109 | * name matches but sybsys_mask doesn't, we should fail. |
| 2110 | * Remember whether name matched. |
| 2111 | */ |
| 2112 | if (opts.name) { |
| 2113 | if (strcmp(opts.name, root->name)) |
| 2114 | continue; |
| 2115 | name_match = true; |
| 2116 | } |
| 2117 | |
| 2118 | /* |
| 2119 | * If we asked for subsystems (or explicitly for no |
| 2120 | * subsystems) then they must match. |
| 2121 | */ |
| 2122 | if ((opts.subsys_mask || opts.none) && |
| 2123 | (opts.subsys_mask != root->subsys_mask)) { |
| 2124 | if (!name_match) |
| 2125 | continue; |
| 2126 | ret = -EBUSY; |
| 2127 | goto out_unlock; |
| 2128 | } |
| 2129 | |
| 2130 | if (root->flags ^ opts.flags) |
| 2131 | pr_warn("new mount options do not match the existing superblock, will be ignored\n"); |
| 2132 | |
| 2133 | /* |
| 2134 | * We want to reuse @root whose lifetime is governed by its |
| 2135 | * ->cgrp. Let's check whether @root is alive and keep it |
| 2136 | * that way. As cgroup_kill_sb() can happen anytime, we |
| 2137 | * want to block it by pinning the sb so that @root doesn't |
| 2138 | * get killed before mount is complete. |
| 2139 | * |
| 2140 | * With the sb pinned, tryget_live can reliably indicate |
| 2141 | * whether @root can be reused. If it's being killed, |
| 2142 | * drain it. We can use wait_queue for the wait but this |
| 2143 | * path is super cold. Let's just sleep a bit and retry. |
| 2144 | */ |
| 2145 | pinned_sb = kernfs_pin_sb(root->kf_root, NULL); |
| 2146 | if (IS_ERR(pinned_sb) || |
| 2147 | !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) { |
| 2148 | mutex_unlock(&cgroup_mutex); |
| 2149 | if (!IS_ERR_OR_NULL(pinned_sb)) |
| 2150 | deactivate_super(pinned_sb); |
| 2151 | msleep(10); |
| 2152 | ret = restart_syscall(); |
| 2153 | goto out_free; |
| 2154 | } |
| 2155 | |
| 2156 | ret = 0; |
| 2157 | goto out_unlock; |
| 2158 | } |
| 2159 | |
| 2160 | /* |
| 2161 | * No such thing, create a new one. name= matching without subsys |
| 2162 | * specification is allowed for already existing hierarchies but we |
| 2163 | * can't create new one without subsys specification. |
| 2164 | */ |
| 2165 | if (!opts.subsys_mask && !opts.none) { |
| 2166 | ret = -EINVAL; |
| 2167 | goto out_unlock; |
| 2168 | } |
| 2169 | |
| 2170 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 2171 | if (!root) { |
| 2172 | ret = -ENOMEM; |
| 2173 | goto out_unlock; |
| 2174 | } |
| 2175 | |
| 2176 | init_cgroup_root(root, &opts); |
| 2177 | |
| 2178 | ret = cgroup_setup_root(root, opts.subsys_mask); |
| 2179 | if (ret) |
| 2180 | cgroup_free_root(root); |
| 2181 | |
| 2182 | out_unlock: |
| 2183 | mutex_unlock(&cgroup_mutex); |
| 2184 | out_free: |
| 2185 | kfree(opts.release_agent); |
| 2186 | kfree(opts.name); |
| 2187 | |
| 2188 | if (ret) |
| 2189 | return ERR_PTR(ret); |
| 2190 | out_mount: |
| 2191 | dentry = kernfs_mount(fs_type, flags, root->kf_root, |
| 2192 | is_v2 ? CGROUP2_SUPER_MAGIC : CGROUP_SUPER_MAGIC, |
| 2193 | &new_sb); |
| 2194 | if (IS_ERR(dentry) || !new_sb) |
| 2195 | cgroup_put(&root->cgrp); |
| 2196 | |
| 2197 | /* |
| 2198 | * If @pinned_sb, we're reusing an existing root and holding an |
| 2199 | * extra ref on its sb. Mount is complete. Put the extra ref. |
| 2200 | */ |
| 2201 | if (pinned_sb) { |
| 2202 | WARN_ON(new_sb); |
| 2203 | deactivate_super(pinned_sb); |
| 2204 | } |
| 2205 | |
| 2206 | return dentry; |
| 2207 | } |
| 2208 | |
| 2209 | static void cgroup_kill_sb(struct super_block *sb) |
| 2210 | { |
| 2211 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
| 2212 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
| 2213 | |
| 2214 | /* |
| 2215 | * If @root doesn't have any mounts or children, start killing it. |
| 2216 | * This prevents new mounts by disabling percpu_ref_tryget_live(). |
| 2217 | * cgroup_mount() may wait for @root's release. |
| 2218 | * |
| 2219 | * And don't kill the default root. |
| 2220 | */ |
| 2221 | if (!list_empty(&root->cgrp.self.children) || |
| 2222 | root == &cgrp_dfl_root) |
| 2223 | cgroup_put(&root->cgrp); |
| 2224 | else |
| 2225 | percpu_ref_kill(&root->cgrp.self.refcnt); |
| 2226 | |
| 2227 | kernfs_kill_sb(sb); |
| 2228 | } |
| 2229 | |
| 2230 | static struct file_system_type cgroup_fs_type = { |
| 2231 | .name = "cgroup", |
| 2232 | .mount = cgroup_mount, |
| 2233 | .kill_sb = cgroup_kill_sb, |
| 2234 | }; |
| 2235 | |
| 2236 | static struct file_system_type cgroup2_fs_type = { |
| 2237 | .name = "cgroup2", |
| 2238 | .mount = cgroup_mount, |
| 2239 | .kill_sb = cgroup_kill_sb, |
| 2240 | }; |
| 2241 | |
| 2242 | /** |
| 2243 | * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy |
| 2244 | * @task: target task |
| 2245 | * @buf: the buffer to write the path into |
| 2246 | * @buflen: the length of the buffer |
| 2247 | * |
| 2248 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 2249 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 2250 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 2251 | * cgroup controller callbacks. |
| 2252 | * |
| 2253 | * Return value is the same as kernfs_path(). |
| 2254 | */ |
| 2255 | char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) |
| 2256 | { |
| 2257 | struct cgroup_root *root; |
| 2258 | struct cgroup *cgrp; |
| 2259 | int hierarchy_id = 1; |
| 2260 | char *path = NULL; |
| 2261 | |
| 2262 | mutex_lock(&cgroup_mutex); |
| 2263 | spin_lock_bh(&css_set_lock); |
| 2264 | |
| 2265 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 2266 | |
| 2267 | if (root) { |
| 2268 | cgrp = task_cgroup_from_root(task, root); |
| 2269 | path = cgroup_path(cgrp, buf, buflen); |
| 2270 | } else { |
| 2271 | /* if no hierarchy exists, everyone is in "/" */ |
| 2272 | if (strlcpy(buf, "/", buflen) < buflen) |
| 2273 | path = buf; |
| 2274 | } |
| 2275 | |
| 2276 | spin_unlock_bh(&css_set_lock); |
| 2277 | mutex_unlock(&cgroup_mutex); |
| 2278 | return path; |
| 2279 | } |
| 2280 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
| 2281 | |
| 2282 | /* used to track tasks and other necessary states during migration */ |
| 2283 | struct cgroup_taskset { |
| 2284 | /* the src and dst cset list running through cset->mg_node */ |
| 2285 | struct list_head src_csets; |
| 2286 | struct list_head dst_csets; |
| 2287 | |
| 2288 | /* the subsys currently being processed */ |
| 2289 | int ssid; |
| 2290 | |
| 2291 | /* |
| 2292 | * Fields for cgroup_taskset_*() iteration. |
| 2293 | * |
| 2294 | * Before migration is committed, the target migration tasks are on |
| 2295 | * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of |
| 2296 | * the csets on ->dst_csets. ->csets point to either ->src_csets |
| 2297 | * or ->dst_csets depending on whether migration is committed. |
| 2298 | * |
| 2299 | * ->cur_csets and ->cur_task point to the current task position |
| 2300 | * during iteration. |
| 2301 | */ |
| 2302 | struct list_head *csets; |
| 2303 | struct css_set *cur_cset; |
| 2304 | struct task_struct *cur_task; |
| 2305 | }; |
| 2306 | |
| 2307 | #define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \ |
| 2308 | .src_csets = LIST_HEAD_INIT(tset.src_csets), \ |
| 2309 | .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \ |
| 2310 | .csets = &tset.src_csets, \ |
| 2311 | } |
| 2312 | |
| 2313 | /** |
| 2314 | * cgroup_taskset_add - try to add a migration target task to a taskset |
| 2315 | * @task: target task |
| 2316 | * @tset: target taskset |
| 2317 | * |
| 2318 | * Add @task, which is a migration target, to @tset. This function becomes |
| 2319 | * noop if @task doesn't need to be migrated. @task's css_set should have |
| 2320 | * been added as a migration source and @task->cg_list will be moved from |
| 2321 | * the css_set's tasks list to mg_tasks one. |
| 2322 | */ |
| 2323 | static void cgroup_taskset_add(struct task_struct *task, |
| 2324 | struct cgroup_taskset *tset) |
| 2325 | { |
| 2326 | struct css_set *cset; |
| 2327 | |
| 2328 | lockdep_assert_held(&css_set_lock); |
| 2329 | |
| 2330 | /* @task either already exited or can't exit until the end */ |
| 2331 | if (task->flags & PF_EXITING) |
| 2332 | return; |
| 2333 | |
| 2334 | /* leave @task alone if post_fork() hasn't linked it yet */ |
| 2335 | if (list_empty(&task->cg_list)) |
| 2336 | return; |
| 2337 | |
| 2338 | cset = task_css_set(task); |
| 2339 | if (!cset->mg_src_cgrp) |
| 2340 | return; |
| 2341 | |
| 2342 | list_move_tail(&task->cg_list, &cset->mg_tasks); |
| 2343 | if (list_empty(&cset->mg_node)) |
| 2344 | list_add_tail(&cset->mg_node, &tset->src_csets); |
| 2345 | if (list_empty(&cset->mg_dst_cset->mg_node)) |
| 2346 | list_move_tail(&cset->mg_dst_cset->mg_node, |
| 2347 | &tset->dst_csets); |
| 2348 | } |
| 2349 | |
| 2350 | /** |
| 2351 | * cgroup_taskset_first - reset taskset and return the first task |
| 2352 | * @tset: taskset of interest |
| 2353 | * @dst_cssp: output variable for the destination css |
| 2354 | * |
| 2355 | * @tset iteration is initialized and the first task is returned. |
| 2356 | */ |
| 2357 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset, |
| 2358 | struct cgroup_subsys_state **dst_cssp) |
| 2359 | { |
| 2360 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); |
| 2361 | tset->cur_task = NULL; |
| 2362 | |
| 2363 | return cgroup_taskset_next(tset, dst_cssp); |
| 2364 | } |
| 2365 | |
| 2366 | /** |
| 2367 | * cgroup_taskset_next - iterate to the next task in taskset |
| 2368 | * @tset: taskset of interest |
| 2369 | * @dst_cssp: output variable for the destination css |
| 2370 | * |
| 2371 | * Return the next task in @tset. Iteration must have been initialized |
| 2372 | * with cgroup_taskset_first(). |
| 2373 | */ |
| 2374 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset, |
| 2375 | struct cgroup_subsys_state **dst_cssp) |
| 2376 | { |
| 2377 | struct css_set *cset = tset->cur_cset; |
| 2378 | struct task_struct *task = tset->cur_task; |
| 2379 | |
| 2380 | while (&cset->mg_node != tset->csets) { |
| 2381 | if (!task) |
| 2382 | task = list_first_entry(&cset->mg_tasks, |
| 2383 | struct task_struct, cg_list); |
| 2384 | else |
| 2385 | task = list_next_entry(task, cg_list); |
| 2386 | |
| 2387 | if (&task->cg_list != &cset->mg_tasks) { |
| 2388 | tset->cur_cset = cset; |
| 2389 | tset->cur_task = task; |
| 2390 | |
| 2391 | /* |
| 2392 | * This function may be called both before and |
| 2393 | * after cgroup_taskset_migrate(). The two cases |
| 2394 | * can be distinguished by looking at whether @cset |
| 2395 | * has its ->mg_dst_cset set. |
| 2396 | */ |
| 2397 | if (cset->mg_dst_cset) |
| 2398 | *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid]; |
| 2399 | else |
| 2400 | *dst_cssp = cset->subsys[tset->ssid]; |
| 2401 | |
| 2402 | return task; |
| 2403 | } |
| 2404 | |
| 2405 | cset = list_next_entry(cset, mg_node); |
| 2406 | task = NULL; |
| 2407 | } |
| 2408 | |
| 2409 | return NULL; |
| 2410 | } |
| 2411 | |
| 2412 | /** |
| 2413 | * cgroup_taskset_migrate - migrate a taskset to a cgroup |
| 2414 | * @tset: taget taskset |
| 2415 | * @dst_cgrp: destination cgroup |
| 2416 | * |
| 2417 | * Migrate tasks in @tset to @dst_cgrp. This function fails iff one of the |
| 2418 | * ->can_attach callbacks fails and guarantees that either all or none of |
| 2419 | * the tasks in @tset are migrated. @tset is consumed regardless of |
| 2420 | * success. |
| 2421 | */ |
| 2422 | static int cgroup_taskset_migrate(struct cgroup_taskset *tset, |
| 2423 | struct cgroup *dst_cgrp) |
| 2424 | { |
| 2425 | struct cgroup_subsys_state *css, *failed_css = NULL; |
| 2426 | struct task_struct *task, *tmp_task; |
| 2427 | struct css_set *cset, *tmp_cset; |
| 2428 | int i, ret; |
| 2429 | |
| 2430 | /* methods shouldn't be called if no task is actually migrating */ |
| 2431 | if (list_empty(&tset->src_csets)) |
| 2432 | return 0; |
| 2433 | |
| 2434 | /* check that we can legitimately attach to the cgroup */ |
| 2435 | for_each_e_css(css, i, dst_cgrp) { |
| 2436 | if (css->ss->can_attach) { |
| 2437 | tset->ssid = i; |
| 2438 | ret = css->ss->can_attach(tset); |
| 2439 | if (ret) { |
| 2440 | failed_css = css; |
| 2441 | goto out_cancel_attach; |
| 2442 | } |
| 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | /* |
| 2447 | * Now that we're guaranteed success, proceed to move all tasks to |
| 2448 | * the new cgroup. There are no failure cases after here, so this |
| 2449 | * is the commit point. |
| 2450 | */ |
| 2451 | spin_lock_bh(&css_set_lock); |
| 2452 | list_for_each_entry(cset, &tset->src_csets, mg_node) { |
| 2453 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) { |
| 2454 | struct css_set *from_cset = task_css_set(task); |
| 2455 | struct css_set *to_cset = cset->mg_dst_cset; |
| 2456 | |
| 2457 | get_css_set(to_cset); |
| 2458 | css_set_move_task(task, from_cset, to_cset, true); |
| 2459 | put_css_set_locked(from_cset); |
| 2460 | } |
| 2461 | } |
| 2462 | spin_unlock_bh(&css_set_lock); |
| 2463 | |
| 2464 | /* |
| 2465 | * Migration is committed, all target tasks are now on dst_csets. |
| 2466 | * Nothing is sensitive to fork() after this point. Notify |
| 2467 | * controllers that migration is complete. |
| 2468 | */ |
| 2469 | tset->csets = &tset->dst_csets; |
| 2470 | |
| 2471 | for_each_e_css(css, i, dst_cgrp) { |
| 2472 | if (css->ss->attach) { |
| 2473 | tset->ssid = i; |
| 2474 | css->ss->attach(tset); |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | ret = 0; |
| 2479 | goto out_release_tset; |
| 2480 | |
| 2481 | out_cancel_attach: |
| 2482 | for_each_e_css(css, i, dst_cgrp) { |
| 2483 | if (css == failed_css) |
| 2484 | break; |
| 2485 | if (css->ss->cancel_attach) { |
| 2486 | tset->ssid = i; |
| 2487 | css->ss->cancel_attach(tset); |
| 2488 | } |
| 2489 | } |
| 2490 | out_release_tset: |
| 2491 | spin_lock_bh(&css_set_lock); |
| 2492 | list_splice_init(&tset->dst_csets, &tset->src_csets); |
| 2493 | list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) { |
| 2494 | list_splice_tail_init(&cset->mg_tasks, &cset->tasks); |
| 2495 | list_del_init(&cset->mg_node); |
| 2496 | } |
| 2497 | spin_unlock_bh(&css_set_lock); |
| 2498 | return ret; |
| 2499 | } |
| 2500 | |
| 2501 | /** |
| 2502 | * cgroup_migrate_finish - cleanup after attach |
| 2503 | * @preloaded_csets: list of preloaded css_sets |
| 2504 | * |
| 2505 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See |
| 2506 | * those functions for details. |
| 2507 | */ |
| 2508 | static void cgroup_migrate_finish(struct list_head *preloaded_csets) |
| 2509 | { |
| 2510 | struct css_set *cset, *tmp_cset; |
| 2511 | |
| 2512 | lockdep_assert_held(&cgroup_mutex); |
| 2513 | |
| 2514 | spin_lock_bh(&css_set_lock); |
| 2515 | list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) { |
| 2516 | cset->mg_src_cgrp = NULL; |
| 2517 | cset->mg_dst_cset = NULL; |
| 2518 | list_del_init(&cset->mg_preload_node); |
| 2519 | put_css_set_locked(cset); |
| 2520 | } |
| 2521 | spin_unlock_bh(&css_set_lock); |
| 2522 | } |
| 2523 | |
| 2524 | /** |
| 2525 | * cgroup_migrate_add_src - add a migration source css_set |
| 2526 | * @src_cset: the source css_set to add |
| 2527 | * @dst_cgrp: the destination cgroup |
| 2528 | * @preloaded_csets: list of preloaded css_sets |
| 2529 | * |
| 2530 | * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin |
| 2531 | * @src_cset and add it to @preloaded_csets, which should later be cleaned |
| 2532 | * up by cgroup_migrate_finish(). |
| 2533 | * |
| 2534 | * This function may be called without holding cgroup_threadgroup_rwsem |
| 2535 | * even if the target is a process. Threads may be created and destroyed |
| 2536 | * but as long as cgroup_mutex is not dropped, no new css_set can be put |
| 2537 | * into play and the preloaded css_sets are guaranteed to cover all |
| 2538 | * migrations. |
| 2539 | */ |
| 2540 | static void cgroup_migrate_add_src(struct css_set *src_cset, |
| 2541 | struct cgroup *dst_cgrp, |
| 2542 | struct list_head *preloaded_csets) |
| 2543 | { |
| 2544 | struct cgroup *src_cgrp; |
| 2545 | |
| 2546 | lockdep_assert_held(&cgroup_mutex); |
| 2547 | lockdep_assert_held(&css_set_lock); |
| 2548 | |
| 2549 | src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); |
| 2550 | |
| 2551 | if (!list_empty(&src_cset->mg_preload_node)) |
| 2552 | return; |
| 2553 | |
| 2554 | WARN_ON(src_cset->mg_src_cgrp); |
| 2555 | WARN_ON(!list_empty(&src_cset->mg_tasks)); |
| 2556 | WARN_ON(!list_empty(&src_cset->mg_node)); |
| 2557 | |
| 2558 | src_cset->mg_src_cgrp = src_cgrp; |
| 2559 | get_css_set(src_cset); |
| 2560 | list_add(&src_cset->mg_preload_node, preloaded_csets); |
| 2561 | } |
| 2562 | |
| 2563 | /** |
| 2564 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration |
| 2565 | * @dst_cgrp: the destination cgroup (may be %NULL) |
| 2566 | * @preloaded_csets: list of preloaded source css_sets |
| 2567 | * |
| 2568 | * Tasks are about to be moved to @dst_cgrp and all the source css_sets |
| 2569 | * have been preloaded to @preloaded_csets. This function looks up and |
| 2570 | * pins all destination css_sets, links each to its source, and append them |
| 2571 | * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each |
| 2572 | * source css_set is assumed to be its cgroup on the default hierarchy. |
| 2573 | * |
| 2574 | * This function must be called after cgroup_migrate_add_src() has been |
| 2575 | * called on each migration source css_set. After migration is performed |
| 2576 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on |
| 2577 | * @preloaded_csets. |
| 2578 | */ |
| 2579 | static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp, |
| 2580 | struct list_head *preloaded_csets) |
| 2581 | { |
| 2582 | LIST_HEAD(csets); |
| 2583 | struct css_set *src_cset, *tmp_cset; |
| 2584 | |
| 2585 | lockdep_assert_held(&cgroup_mutex); |
| 2586 | |
| 2587 | /* |
| 2588 | * Except for the root, subtree_control must be zero for a cgroup |
| 2589 | * with tasks so that child cgroups don't compete against tasks. |
| 2590 | */ |
| 2591 | if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) && |
| 2592 | dst_cgrp->subtree_control) |
| 2593 | return -EBUSY; |
| 2594 | |
| 2595 | /* look up the dst cset for each src cset and link it to src */ |
| 2596 | list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) { |
| 2597 | struct css_set *dst_cset; |
| 2598 | |
| 2599 | dst_cset = find_css_set(src_cset, |
| 2600 | dst_cgrp ?: src_cset->dfl_cgrp); |
| 2601 | if (!dst_cset) |
| 2602 | goto err; |
| 2603 | |
| 2604 | WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset); |
| 2605 | |
| 2606 | /* |
| 2607 | * If src cset equals dst, it's noop. Drop the src. |
| 2608 | * cgroup_migrate() will skip the cset too. Note that we |
| 2609 | * can't handle src == dst as some nodes are used by both. |
| 2610 | */ |
| 2611 | if (src_cset == dst_cset) { |
| 2612 | src_cset->mg_src_cgrp = NULL; |
| 2613 | list_del_init(&src_cset->mg_preload_node); |
| 2614 | put_css_set(src_cset); |
| 2615 | put_css_set(dst_cset); |
| 2616 | continue; |
| 2617 | } |
| 2618 | |
| 2619 | src_cset->mg_dst_cset = dst_cset; |
| 2620 | |
| 2621 | if (list_empty(&dst_cset->mg_preload_node)) |
| 2622 | list_add(&dst_cset->mg_preload_node, &csets); |
| 2623 | else |
| 2624 | put_css_set(dst_cset); |
| 2625 | } |
| 2626 | |
| 2627 | list_splice_tail(&csets, preloaded_csets); |
| 2628 | return 0; |
| 2629 | err: |
| 2630 | cgroup_migrate_finish(&csets); |
| 2631 | return -ENOMEM; |
| 2632 | } |
| 2633 | |
| 2634 | /** |
| 2635 | * cgroup_migrate - migrate a process or task to a cgroup |
| 2636 | * @leader: the leader of the process or the task to migrate |
| 2637 | * @threadgroup: whether @leader points to the whole process or a single task |
| 2638 | * @cgrp: the destination cgroup |
| 2639 | * |
| 2640 | * Migrate a process or task denoted by @leader to @cgrp. If migrating a |
| 2641 | * process, the caller must be holding cgroup_threadgroup_rwsem. The |
| 2642 | * caller is also responsible for invoking cgroup_migrate_add_src() and |
| 2643 | * cgroup_migrate_prepare_dst() on the targets before invoking this |
| 2644 | * function and following up with cgroup_migrate_finish(). |
| 2645 | * |
| 2646 | * As long as a controller's ->can_attach() doesn't fail, this function is |
| 2647 | * guaranteed to succeed. This means that, excluding ->can_attach() |
| 2648 | * failure, when migrating multiple targets, the success or failure can be |
| 2649 | * decided for all targets by invoking group_migrate_prepare_dst() before |
| 2650 | * actually starting migrating. |
| 2651 | */ |
| 2652 | static int cgroup_migrate(struct task_struct *leader, bool threadgroup, |
| 2653 | struct cgroup *cgrp) |
| 2654 | { |
| 2655 | struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset); |
| 2656 | struct task_struct *task; |
| 2657 | |
| 2658 | /* |
| 2659 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2660 | * already PF_EXITING could be freed from underneath us unless we |
| 2661 | * take an rcu_read_lock. |
| 2662 | */ |
| 2663 | spin_lock_bh(&css_set_lock); |
| 2664 | rcu_read_lock(); |
| 2665 | task = leader; |
| 2666 | do { |
| 2667 | cgroup_taskset_add(task, &tset); |
| 2668 | if (!threadgroup) |
| 2669 | break; |
| 2670 | } while_each_thread(leader, task); |
| 2671 | rcu_read_unlock(); |
| 2672 | spin_unlock_bh(&css_set_lock); |
| 2673 | |
| 2674 | return cgroup_taskset_migrate(&tset, cgrp); |
| 2675 | } |
| 2676 | |
| 2677 | /** |
| 2678 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
| 2679 | * @dst_cgrp: the cgroup to attach to |
| 2680 | * @leader: the task or the leader of the threadgroup to be attached |
| 2681 | * @threadgroup: attach the whole threadgroup? |
| 2682 | * |
| 2683 | * Call holding cgroup_mutex and cgroup_threadgroup_rwsem. |
| 2684 | */ |
| 2685 | static int cgroup_attach_task(struct cgroup *dst_cgrp, |
| 2686 | struct task_struct *leader, bool threadgroup) |
| 2687 | { |
| 2688 | LIST_HEAD(preloaded_csets); |
| 2689 | struct task_struct *task; |
| 2690 | int ret; |
| 2691 | |
| 2692 | /* look up all src csets */ |
| 2693 | spin_lock_bh(&css_set_lock); |
| 2694 | rcu_read_lock(); |
| 2695 | task = leader; |
| 2696 | do { |
| 2697 | cgroup_migrate_add_src(task_css_set(task), dst_cgrp, |
| 2698 | &preloaded_csets); |
| 2699 | if (!threadgroup) |
| 2700 | break; |
| 2701 | } while_each_thread(leader, task); |
| 2702 | rcu_read_unlock(); |
| 2703 | spin_unlock_bh(&css_set_lock); |
| 2704 | |
| 2705 | /* prepare dst csets and commit */ |
| 2706 | ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets); |
| 2707 | if (!ret) |
| 2708 | ret = cgroup_migrate(leader, threadgroup, dst_cgrp); |
| 2709 | |
| 2710 | cgroup_migrate_finish(&preloaded_csets); |
| 2711 | return ret; |
| 2712 | } |
| 2713 | |
| 2714 | static int cgroup_procs_write_permission(struct task_struct *task, |
| 2715 | struct cgroup *dst_cgrp, |
| 2716 | struct kernfs_open_file *of) |
| 2717 | { |
| 2718 | const struct cred *cred = current_cred(); |
| 2719 | const struct cred *tcred = get_task_cred(task); |
| 2720 | int ret = 0; |
| 2721 | |
| 2722 | /* |
| 2723 | * even if we're attaching all tasks in the thread group, we only |
| 2724 | * need to check permissions on one of them. |
| 2725 | */ |
| 2726 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2727 | !uid_eq(cred->euid, tcred->uid) && |
| 2728 | !uid_eq(cred->euid, tcred->suid)) |
| 2729 | ret = -EACCES; |
| 2730 | |
| 2731 | if (!ret && cgroup_on_dfl(dst_cgrp)) { |
| 2732 | struct super_block *sb = of->file->f_path.dentry->d_sb; |
| 2733 | struct cgroup *cgrp; |
| 2734 | struct inode *inode; |
| 2735 | |
| 2736 | spin_lock_bh(&css_set_lock); |
| 2737 | cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); |
| 2738 | spin_unlock_bh(&css_set_lock); |
| 2739 | |
| 2740 | while (!cgroup_is_descendant(dst_cgrp, cgrp)) |
| 2741 | cgrp = cgroup_parent(cgrp); |
| 2742 | |
| 2743 | ret = -ENOMEM; |
| 2744 | inode = kernfs_get_inode(sb, cgrp->procs_file.kn); |
| 2745 | if (inode) { |
| 2746 | ret = inode_permission(inode, MAY_WRITE); |
| 2747 | iput(inode); |
| 2748 | } |
| 2749 | } |
| 2750 | |
| 2751 | put_cred(tcred); |
| 2752 | return ret; |
| 2753 | } |
| 2754 | |
| 2755 | /* |
| 2756 | * Find the task_struct of the task to attach by vpid and pass it along to the |
| 2757 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2758 | * cgroup_mutex and threadgroup. |
| 2759 | */ |
| 2760 | static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf, |
| 2761 | size_t nbytes, loff_t off, bool threadgroup) |
| 2762 | { |
| 2763 | struct task_struct *tsk; |
| 2764 | struct cgroup *cgrp; |
| 2765 | pid_t pid; |
| 2766 | int ret; |
| 2767 | |
| 2768 | if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) |
| 2769 | return -EINVAL; |
| 2770 | |
| 2771 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 2772 | if (!cgrp) |
| 2773 | return -ENODEV; |
| 2774 | |
| 2775 | percpu_down_write(&cgroup_threadgroup_rwsem); |
| 2776 | rcu_read_lock(); |
| 2777 | if (pid) { |
| 2778 | tsk = find_task_by_vpid(pid); |
| 2779 | if (!tsk) { |
| 2780 | ret = -ESRCH; |
| 2781 | goto out_unlock_rcu; |
| 2782 | } |
| 2783 | } else { |
| 2784 | tsk = current; |
| 2785 | } |
| 2786 | |
| 2787 | if (threadgroup) |
| 2788 | tsk = tsk->group_leader; |
| 2789 | |
| 2790 | /* |
| 2791 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
| 2792 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2793 | * with no rt_runtime allocated. Just say no. |
| 2794 | */ |
| 2795 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
| 2796 | ret = -EINVAL; |
| 2797 | goto out_unlock_rcu; |
| 2798 | } |
| 2799 | |
| 2800 | get_task_struct(tsk); |
| 2801 | rcu_read_unlock(); |
| 2802 | |
| 2803 | ret = cgroup_procs_write_permission(tsk, cgrp, of); |
| 2804 | if (!ret) |
| 2805 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2806 | |
| 2807 | put_task_struct(tsk); |
| 2808 | goto out_unlock_threadgroup; |
| 2809 | |
| 2810 | out_unlock_rcu: |
| 2811 | rcu_read_unlock(); |
| 2812 | out_unlock_threadgroup: |
| 2813 | percpu_up_write(&cgroup_threadgroup_rwsem); |
| 2814 | cgroup_kn_unlock(of->kn); |
| 2815 | cpuset_post_attach_flush(); |
| 2816 | return ret ?: nbytes; |
| 2817 | } |
| 2818 | |
| 2819 | /** |
| 2820 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2821 | * @from: attach to all cgroups of a given task |
| 2822 | * @tsk: the task to be attached |
| 2823 | */ |
| 2824 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2825 | { |
| 2826 | struct cgroup_root *root; |
| 2827 | int retval = 0; |
| 2828 | |
| 2829 | mutex_lock(&cgroup_mutex); |
| 2830 | for_each_root(root) { |
| 2831 | struct cgroup *from_cgrp; |
| 2832 | |
| 2833 | if (root == &cgrp_dfl_root) |
| 2834 | continue; |
| 2835 | |
| 2836 | spin_lock_bh(&css_set_lock); |
| 2837 | from_cgrp = task_cgroup_from_root(from, root); |
| 2838 | spin_unlock_bh(&css_set_lock); |
| 2839 | |
| 2840 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
| 2841 | if (retval) |
| 2842 | break; |
| 2843 | } |
| 2844 | mutex_unlock(&cgroup_mutex); |
| 2845 | |
| 2846 | return retval; |
| 2847 | } |
| 2848 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2849 | |
| 2850 | static ssize_t cgroup_tasks_write(struct kernfs_open_file *of, |
| 2851 | char *buf, size_t nbytes, loff_t off) |
| 2852 | { |
| 2853 | return __cgroup_procs_write(of, buf, nbytes, off, false); |
| 2854 | } |
| 2855 | |
| 2856 | static ssize_t cgroup_procs_write(struct kernfs_open_file *of, |
| 2857 | char *buf, size_t nbytes, loff_t off) |
| 2858 | { |
| 2859 | return __cgroup_procs_write(of, buf, nbytes, off, true); |
| 2860 | } |
| 2861 | |
| 2862 | static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of, |
| 2863 | char *buf, size_t nbytes, loff_t off) |
| 2864 | { |
| 2865 | struct cgroup *cgrp; |
| 2866 | |
| 2867 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
| 2868 | |
| 2869 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 2870 | if (!cgrp) |
| 2871 | return -ENODEV; |
| 2872 | spin_lock(&release_agent_path_lock); |
| 2873 | strlcpy(cgrp->root->release_agent_path, strstrip(buf), |
| 2874 | sizeof(cgrp->root->release_agent_path)); |
| 2875 | spin_unlock(&release_agent_path_lock); |
| 2876 | cgroup_kn_unlock(of->kn); |
| 2877 | return nbytes; |
| 2878 | } |
| 2879 | |
| 2880 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
| 2881 | { |
| 2882 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2883 | |
| 2884 | spin_lock(&release_agent_path_lock); |
| 2885 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2886 | spin_unlock(&release_agent_path_lock); |
| 2887 | seq_putc(seq, '\n'); |
| 2888 | return 0; |
| 2889 | } |
| 2890 | |
| 2891 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
| 2892 | { |
| 2893 | seq_puts(seq, "0\n"); |
| 2894 | return 0; |
| 2895 | } |
| 2896 | |
| 2897 | static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask) |
| 2898 | { |
| 2899 | struct cgroup_subsys *ss; |
| 2900 | bool printed = false; |
| 2901 | int ssid; |
| 2902 | |
| 2903 | do_each_subsys_mask(ss, ssid, ss_mask) { |
| 2904 | if (printed) |
| 2905 | seq_putc(seq, ' '); |
| 2906 | seq_printf(seq, "%s", ss->name); |
| 2907 | printed = true; |
| 2908 | } while_each_subsys_mask(); |
| 2909 | if (printed) |
| 2910 | seq_putc(seq, '\n'); |
| 2911 | } |
| 2912 | |
| 2913 | /* show controllers which are enabled from the parent */ |
| 2914 | static int cgroup_controllers_show(struct seq_file *seq, void *v) |
| 2915 | { |
| 2916 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2917 | |
| 2918 | cgroup_print_ss_mask(seq, cgroup_control(cgrp)); |
| 2919 | return 0; |
| 2920 | } |
| 2921 | |
| 2922 | /* show controllers which are enabled for a given cgroup's children */ |
| 2923 | static int cgroup_subtree_control_show(struct seq_file *seq, void *v) |
| 2924 | { |
| 2925 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2926 | |
| 2927 | cgroup_print_ss_mask(seq, cgrp->subtree_control); |
| 2928 | return 0; |
| 2929 | } |
| 2930 | |
| 2931 | /** |
| 2932 | * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy |
| 2933 | * @cgrp: root of the subtree to update csses for |
| 2934 | * |
| 2935 | * @cgrp's subtree_ss_mask has changed and its subtree's (self excluded) |
| 2936 | * css associations need to be updated accordingly. This function looks up |
| 2937 | * all css_sets which are attached to the subtree, creates the matching |
| 2938 | * updated css_sets and migrates the tasks to the new ones. |
| 2939 | */ |
| 2940 | static int cgroup_update_dfl_csses(struct cgroup *cgrp) |
| 2941 | { |
| 2942 | LIST_HEAD(preloaded_csets); |
| 2943 | struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset); |
| 2944 | struct cgroup_subsys_state *css; |
| 2945 | struct css_set *src_cset; |
| 2946 | int ret; |
| 2947 | |
| 2948 | lockdep_assert_held(&cgroup_mutex); |
| 2949 | |
| 2950 | percpu_down_write(&cgroup_threadgroup_rwsem); |
| 2951 | |
| 2952 | /* look up all csses currently attached to @cgrp's subtree */ |
| 2953 | spin_lock_bh(&css_set_lock); |
| 2954 | css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) { |
| 2955 | struct cgrp_cset_link *link; |
| 2956 | |
| 2957 | /* self is not affected by subtree_ss_mask change */ |
| 2958 | if (css->cgroup == cgrp) |
| 2959 | continue; |
| 2960 | |
| 2961 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) |
| 2962 | cgroup_migrate_add_src(link->cset, cgrp, |
| 2963 | &preloaded_csets); |
| 2964 | } |
| 2965 | spin_unlock_bh(&css_set_lock); |
| 2966 | |
| 2967 | /* NULL dst indicates self on default hierarchy */ |
| 2968 | ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets); |
| 2969 | if (ret) |
| 2970 | goto out_finish; |
| 2971 | |
| 2972 | spin_lock_bh(&css_set_lock); |
| 2973 | list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) { |
| 2974 | struct task_struct *task, *ntask; |
| 2975 | |
| 2976 | /* src_csets precede dst_csets, break on the first dst_cset */ |
| 2977 | if (!src_cset->mg_src_cgrp) |
| 2978 | break; |
| 2979 | |
| 2980 | /* all tasks in src_csets need to be migrated */ |
| 2981 | list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list) |
| 2982 | cgroup_taskset_add(task, &tset); |
| 2983 | } |
| 2984 | spin_unlock_bh(&css_set_lock); |
| 2985 | |
| 2986 | ret = cgroup_taskset_migrate(&tset, cgrp); |
| 2987 | out_finish: |
| 2988 | cgroup_migrate_finish(&preloaded_csets); |
| 2989 | percpu_up_write(&cgroup_threadgroup_rwsem); |
| 2990 | return ret; |
| 2991 | } |
| 2992 | |
| 2993 | /** |
| 2994 | * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses |
| 2995 | * @cgrp: root of the target subtree |
| 2996 | * |
| 2997 | * Because css offlining is asynchronous, userland may try to re-enable a |
| 2998 | * controller while the previous css is still around. This function grabs |
| 2999 | * cgroup_mutex and drains the previous css instances of @cgrp's subtree. |
| 3000 | */ |
| 3001 | static void cgroup_lock_and_drain_offline(struct cgroup *cgrp) |
| 3002 | __acquires(&cgroup_mutex) |
| 3003 | { |
| 3004 | struct cgroup *dsct; |
| 3005 | struct cgroup_subsys_state *d_css; |
| 3006 | struct cgroup_subsys *ss; |
| 3007 | int ssid; |
| 3008 | |
| 3009 | restart: |
| 3010 | mutex_lock(&cgroup_mutex); |
| 3011 | |
| 3012 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
| 3013 | for_each_subsys(ss, ssid) { |
| 3014 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 3015 | DEFINE_WAIT(wait); |
| 3016 | |
| 3017 | if (!css || !percpu_ref_is_dying(&css->refcnt)) |
| 3018 | continue; |
| 3019 | |
| 3020 | cgroup_get(dsct); |
| 3021 | prepare_to_wait(&dsct->offline_waitq, &wait, |
| 3022 | TASK_UNINTERRUPTIBLE); |
| 3023 | |
| 3024 | mutex_unlock(&cgroup_mutex); |
| 3025 | schedule(); |
| 3026 | finish_wait(&dsct->offline_waitq, &wait); |
| 3027 | |
| 3028 | cgroup_put(dsct); |
| 3029 | goto restart; |
| 3030 | } |
| 3031 | } |
| 3032 | } |
| 3033 | |
| 3034 | /** |
| 3035 | * cgroup_save_control - save control masks of a subtree |
| 3036 | * @cgrp: root of the target subtree |
| 3037 | * |
| 3038 | * Save ->subtree_control and ->subtree_ss_mask to the respective old_ |
| 3039 | * prefixed fields for @cgrp's subtree including @cgrp itself. |
| 3040 | */ |
| 3041 | static void cgroup_save_control(struct cgroup *cgrp) |
| 3042 | { |
| 3043 | struct cgroup *dsct; |
| 3044 | struct cgroup_subsys_state *d_css; |
| 3045 | |
| 3046 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 3047 | dsct->old_subtree_control = dsct->subtree_control; |
| 3048 | dsct->old_subtree_ss_mask = dsct->subtree_ss_mask; |
| 3049 | } |
| 3050 | } |
| 3051 | |
| 3052 | /** |
| 3053 | * cgroup_propagate_control - refresh control masks of a subtree |
| 3054 | * @cgrp: root of the target subtree |
| 3055 | * |
| 3056 | * For @cgrp and its subtree, ensure ->subtree_ss_mask matches |
| 3057 | * ->subtree_control and propagate controller availability through the |
| 3058 | * subtree so that descendants don't have unavailable controllers enabled. |
| 3059 | */ |
| 3060 | static void cgroup_propagate_control(struct cgroup *cgrp) |
| 3061 | { |
| 3062 | struct cgroup *dsct; |
| 3063 | struct cgroup_subsys_state *d_css; |
| 3064 | |
| 3065 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 3066 | dsct->subtree_control &= cgroup_control(dsct); |
| 3067 | dsct->subtree_ss_mask = cgroup_calc_subtree_ss_mask(dsct, |
| 3068 | dsct->subtree_control); |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | /** |
| 3073 | * cgroup_restore_control - restore control masks of a subtree |
| 3074 | * @cgrp: root of the target subtree |
| 3075 | * |
| 3076 | * Restore ->subtree_control and ->subtree_ss_mask from the respective old_ |
| 3077 | * prefixed fields for @cgrp's subtree including @cgrp itself. |
| 3078 | */ |
| 3079 | static void cgroup_restore_control(struct cgroup *cgrp) |
| 3080 | { |
| 3081 | struct cgroup *dsct; |
| 3082 | struct cgroup_subsys_state *d_css; |
| 3083 | |
| 3084 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
| 3085 | dsct->subtree_control = dsct->old_subtree_control; |
| 3086 | dsct->subtree_ss_mask = dsct->old_subtree_ss_mask; |
| 3087 | } |
| 3088 | } |
| 3089 | |
| 3090 | /** |
| 3091 | * cgroup_apply_control_enable - enable or show csses according to control |
| 3092 | * @cgrp: root of the target subtree |
| 3093 | * |
| 3094 | * Walk @cgrp's subtree and create new csses or make the existing ones |
| 3095 | * visible. A css is created invisible if it's being implicitly enabled |
| 3096 | * through dependency. An invisible css is made visible when the userland |
| 3097 | * explicitly enables it. |
| 3098 | * |
| 3099 | * Returns 0 on success, -errno on failure. On failure, csses which have |
| 3100 | * been processed already aren't cleaned up. The caller is responsible for |
| 3101 | * cleaning up with cgroup_apply_control_disble(). |
| 3102 | */ |
| 3103 | static int cgroup_apply_control_enable(struct cgroup *cgrp) |
| 3104 | { |
| 3105 | struct cgroup *dsct; |
| 3106 | struct cgroup_subsys_state *d_css; |
| 3107 | struct cgroup_subsys *ss; |
| 3108 | int ssid, ret; |
| 3109 | |
| 3110 | cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { |
| 3111 | for_each_subsys(ss, ssid) { |
| 3112 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 3113 | |
| 3114 | WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); |
| 3115 | |
| 3116 | if (!(cgroup_ss_mask(dsct) & (1 << ss->id))) |
| 3117 | continue; |
| 3118 | |
| 3119 | if (!css) { |
| 3120 | css = css_create(dsct, ss); |
| 3121 | if (IS_ERR(css)) |
| 3122 | return PTR_ERR(css); |
| 3123 | } |
| 3124 | |
| 3125 | if (cgroup_control(dsct) & (1 << ss->id)) { |
| 3126 | ret = css_populate_dir(css, NULL); |
| 3127 | if (ret) |
| 3128 | return ret; |
| 3129 | } |
| 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | return 0; |
| 3134 | } |
| 3135 | |
| 3136 | /** |
| 3137 | * cgroup_apply_control_disable - kill or hide csses according to control |
| 3138 | * @cgrp: root of the target subtree |
| 3139 | * |
| 3140 | * Walk @cgrp's subtree and kill and hide csses so that they match |
| 3141 | * cgroup_ss_mask() and cgroup_visible_mask(). |
| 3142 | * |
| 3143 | * A css is hidden when the userland requests it to be disabled while other |
| 3144 | * subsystems are still depending on it. The css must not actively control |
| 3145 | * resources and be in the vanilla state if it's made visible again later. |
| 3146 | * Controllers which may be depended upon should provide ->css_reset() for |
| 3147 | * this purpose. |
| 3148 | */ |
| 3149 | static void cgroup_apply_control_disable(struct cgroup *cgrp) |
| 3150 | { |
| 3151 | struct cgroup *dsct; |
| 3152 | struct cgroup_subsys_state *d_css; |
| 3153 | struct cgroup_subsys *ss; |
| 3154 | int ssid; |
| 3155 | |
| 3156 | cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { |
| 3157 | for_each_subsys(ss, ssid) { |
| 3158 | struct cgroup_subsys_state *css = cgroup_css(dsct, ss); |
| 3159 | |
| 3160 | WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); |
| 3161 | |
| 3162 | if (!css) |
| 3163 | continue; |
| 3164 | |
| 3165 | if (!(cgroup_ss_mask(dsct) & (1 << ss->id))) { |
| 3166 | kill_css(css); |
| 3167 | } else if (!(cgroup_control(dsct) & (1 << ss->id))) { |
| 3168 | css_clear_dir(css, NULL); |
| 3169 | if (ss->css_reset) |
| 3170 | ss->css_reset(css); |
| 3171 | } |
| 3172 | } |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | /** |
| 3177 | * cgroup_apply_control - apply control mask updates to the subtree |
| 3178 | * @cgrp: root of the target subtree |
| 3179 | * |
| 3180 | * subsystems can be enabled and disabled in a subtree using the following |
| 3181 | * steps. |
| 3182 | * |
| 3183 | * 1. Call cgroup_save_control() to stash the current state. |
| 3184 | * 2. Update ->subtree_control masks in the subtree as desired. |
| 3185 | * 3. Call cgroup_apply_control() to apply the changes. |
| 3186 | * 4. Optionally perform other related operations. |
| 3187 | * 5. Call cgroup_finalize_control() to finish up. |
| 3188 | * |
| 3189 | * This function implements step 3 and propagates the mask changes |
| 3190 | * throughout @cgrp's subtree, updates csses accordingly and perform |
| 3191 | * process migrations. |
| 3192 | */ |
| 3193 | static int cgroup_apply_control(struct cgroup *cgrp) |
| 3194 | { |
| 3195 | int ret; |
| 3196 | |
| 3197 | cgroup_propagate_control(cgrp); |
| 3198 | |
| 3199 | ret = cgroup_apply_control_enable(cgrp); |
| 3200 | if (ret) |
| 3201 | return ret; |
| 3202 | |
| 3203 | /* |
| 3204 | * At this point, cgroup_e_css() results reflect the new csses |
| 3205 | * making the following cgroup_update_dfl_csses() properly update |
| 3206 | * css associations of all tasks in the subtree. |
| 3207 | */ |
| 3208 | ret = cgroup_update_dfl_csses(cgrp); |
| 3209 | if (ret) |
| 3210 | return ret; |
| 3211 | |
| 3212 | return 0; |
| 3213 | } |
| 3214 | |
| 3215 | /** |
| 3216 | * cgroup_finalize_control - finalize control mask update |
| 3217 | * @cgrp: root of the target subtree |
| 3218 | * @ret: the result of the update |
| 3219 | * |
| 3220 | * Finalize control mask update. See cgroup_apply_control() for more info. |
| 3221 | */ |
| 3222 | static void cgroup_finalize_control(struct cgroup *cgrp, int ret) |
| 3223 | { |
| 3224 | if (ret) { |
| 3225 | cgroup_restore_control(cgrp); |
| 3226 | cgroup_propagate_control(cgrp); |
| 3227 | } |
| 3228 | |
| 3229 | cgroup_apply_control_disable(cgrp); |
| 3230 | } |
| 3231 | |
| 3232 | /* change the enabled child controllers for a cgroup in the default hierarchy */ |
| 3233 | static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, |
| 3234 | char *buf, size_t nbytes, |
| 3235 | loff_t off) |
| 3236 | { |
| 3237 | u16 enable = 0, disable = 0; |
| 3238 | struct cgroup *cgrp, *child; |
| 3239 | struct cgroup_subsys *ss; |
| 3240 | char *tok; |
| 3241 | int ssid, ret; |
| 3242 | |
| 3243 | /* |
| 3244 | * Parse input - space separated list of subsystem names prefixed |
| 3245 | * with either + or -. |
| 3246 | */ |
| 3247 | buf = strstrip(buf); |
| 3248 | while ((tok = strsep(&buf, " "))) { |
| 3249 | if (tok[0] == '\0') |
| 3250 | continue; |
| 3251 | do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) { |
| 3252 | if (!cgroup_ssid_enabled(ssid) || |
| 3253 | strcmp(tok + 1, ss->name)) |
| 3254 | continue; |
| 3255 | |
| 3256 | if (*tok == '+') { |
| 3257 | enable |= 1 << ssid; |
| 3258 | disable &= ~(1 << ssid); |
| 3259 | } else if (*tok == '-') { |
| 3260 | disable |= 1 << ssid; |
| 3261 | enable &= ~(1 << ssid); |
| 3262 | } else { |
| 3263 | return -EINVAL; |
| 3264 | } |
| 3265 | break; |
| 3266 | } while_each_subsys_mask(); |
| 3267 | if (ssid == CGROUP_SUBSYS_COUNT) |
| 3268 | return -EINVAL; |
| 3269 | } |
| 3270 | |
| 3271 | cgrp = cgroup_kn_lock_live(of->kn, true); |
| 3272 | if (!cgrp) |
| 3273 | return -ENODEV; |
| 3274 | |
| 3275 | for_each_subsys(ss, ssid) { |
| 3276 | if (enable & (1 << ssid)) { |
| 3277 | if (cgrp->subtree_control & (1 << ssid)) { |
| 3278 | enable &= ~(1 << ssid); |
| 3279 | continue; |
| 3280 | } |
| 3281 | |
| 3282 | if (!(cgroup_control(cgrp) & (1 << ssid))) { |
| 3283 | ret = -ENOENT; |
| 3284 | goto out_unlock; |
| 3285 | } |
| 3286 | } else if (disable & (1 << ssid)) { |
| 3287 | if (!(cgrp->subtree_control & (1 << ssid))) { |
| 3288 | disable &= ~(1 << ssid); |
| 3289 | continue; |
| 3290 | } |
| 3291 | |
| 3292 | /* a child has it enabled? */ |
| 3293 | cgroup_for_each_live_child(child, cgrp) { |
| 3294 | if (child->subtree_control & (1 << ssid)) { |
| 3295 | ret = -EBUSY; |
| 3296 | goto out_unlock; |
| 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | } |
| 3301 | |
| 3302 | if (!enable && !disable) { |
| 3303 | ret = 0; |
| 3304 | goto out_unlock; |
| 3305 | } |
| 3306 | |
| 3307 | /* |
| 3308 | * Except for the root, subtree_control must be zero for a cgroup |
| 3309 | * with tasks so that child cgroups don't compete against tasks. |
| 3310 | */ |
| 3311 | if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) { |
| 3312 | ret = -EBUSY; |
| 3313 | goto out_unlock; |
| 3314 | } |
| 3315 | |
| 3316 | /* save and update control masks and prepare csses */ |
| 3317 | cgroup_save_control(cgrp); |
| 3318 | |
| 3319 | cgrp->subtree_control |= enable; |
| 3320 | cgrp->subtree_control &= ~disable; |
| 3321 | |
| 3322 | ret = cgroup_apply_control(cgrp); |
| 3323 | |
| 3324 | cgroup_finalize_control(cgrp, ret); |
| 3325 | |
| 3326 | kernfs_activate(cgrp->kn); |
| 3327 | ret = 0; |
| 3328 | out_unlock: |
| 3329 | cgroup_kn_unlock(of->kn); |
| 3330 | return ret ?: nbytes; |
| 3331 | } |
| 3332 | |
| 3333 | static int cgroup_events_show(struct seq_file *seq, void *v) |
| 3334 | { |
| 3335 | seq_printf(seq, "populated %d\n", |
| 3336 | cgroup_is_populated(seq_css(seq)->cgroup)); |
| 3337 | return 0; |
| 3338 | } |
| 3339 | |
| 3340 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 3341 | size_t nbytes, loff_t off) |
| 3342 | { |
| 3343 | struct cgroup *cgrp = of->kn->parent->priv; |
| 3344 | struct cftype *cft = of->kn->priv; |
| 3345 | struct cgroup_subsys_state *css; |
| 3346 | int ret; |
| 3347 | |
| 3348 | if (cft->write) |
| 3349 | return cft->write(of, buf, nbytes, off); |
| 3350 | |
| 3351 | /* |
| 3352 | * kernfs guarantees that a file isn't deleted with operations in |
| 3353 | * flight, which means that the matching css is and stays alive and |
| 3354 | * doesn't need to be pinned. The RCU locking is not necessary |
| 3355 | * either. It's just for the convenience of using cgroup_css(). |
| 3356 | */ |
| 3357 | rcu_read_lock(); |
| 3358 | css = cgroup_css(cgrp, cft->ss); |
| 3359 | rcu_read_unlock(); |
| 3360 | |
| 3361 | if (cft->write_u64) { |
| 3362 | unsigned long long v; |
| 3363 | ret = kstrtoull(buf, 0, &v); |
| 3364 | if (!ret) |
| 3365 | ret = cft->write_u64(css, cft, v); |
| 3366 | } else if (cft->write_s64) { |
| 3367 | long long v; |
| 3368 | ret = kstrtoll(buf, 0, &v); |
| 3369 | if (!ret) |
| 3370 | ret = cft->write_s64(css, cft, v); |
| 3371 | } else { |
| 3372 | ret = -EINVAL; |
| 3373 | } |
| 3374 | |
| 3375 | return ret ?: nbytes; |
| 3376 | } |
| 3377 | |
| 3378 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
| 3379 | { |
| 3380 | return seq_cft(seq)->seq_start(seq, ppos); |
| 3381 | } |
| 3382 | |
| 3383 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 3384 | { |
| 3385 | return seq_cft(seq)->seq_next(seq, v, ppos); |
| 3386 | } |
| 3387 | |
| 3388 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 3389 | { |
| 3390 | seq_cft(seq)->seq_stop(seq, v); |
| 3391 | } |
| 3392 | |
| 3393 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 3394 | { |
| 3395 | struct cftype *cft = seq_cft(m); |
| 3396 | struct cgroup_subsys_state *css = seq_css(m); |
| 3397 | |
| 3398 | if (cft->seq_show) |
| 3399 | return cft->seq_show(m, arg); |
| 3400 | |
| 3401 | if (cft->read_u64) |
| 3402 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 3403 | else if (cft->read_s64) |
| 3404 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 3405 | else |
| 3406 | return -EINVAL; |
| 3407 | return 0; |
| 3408 | } |
| 3409 | |
| 3410 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 3411 | .atomic_write_len = PAGE_SIZE, |
| 3412 | .write = cgroup_file_write, |
| 3413 | .seq_show = cgroup_seqfile_show, |
| 3414 | }; |
| 3415 | |
| 3416 | static struct kernfs_ops cgroup_kf_ops = { |
| 3417 | .atomic_write_len = PAGE_SIZE, |
| 3418 | .write = cgroup_file_write, |
| 3419 | .seq_start = cgroup_seqfile_start, |
| 3420 | .seq_next = cgroup_seqfile_next, |
| 3421 | .seq_stop = cgroup_seqfile_stop, |
| 3422 | .seq_show = cgroup_seqfile_show, |
| 3423 | }; |
| 3424 | |
| 3425 | /* |
| 3426 | * cgroup_rename - Only allow simple rename of directories in place. |
| 3427 | */ |
| 3428 | static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent, |
| 3429 | const char *new_name_str) |
| 3430 | { |
| 3431 | struct cgroup *cgrp = kn->priv; |
| 3432 | int ret; |
| 3433 | |
| 3434 | if (kernfs_type(kn) != KERNFS_DIR) |
| 3435 | return -ENOTDIR; |
| 3436 | if (kn->parent != new_parent) |
| 3437 | return -EIO; |
| 3438 | |
| 3439 | /* |
| 3440 | * This isn't a proper migration and its usefulness is very |
| 3441 | * limited. Disallow on the default hierarchy. |
| 3442 | */ |
| 3443 | if (cgroup_on_dfl(cgrp)) |
| 3444 | return -EPERM; |
| 3445 | |
| 3446 | /* |
| 3447 | * We're gonna grab cgroup_mutex which nests outside kernfs |
| 3448 | * active_ref. kernfs_rename() doesn't require active_ref |
| 3449 | * protection. Break them before grabbing cgroup_mutex. |
| 3450 | */ |
| 3451 | kernfs_break_active_protection(new_parent); |
| 3452 | kernfs_break_active_protection(kn); |
| 3453 | |
| 3454 | mutex_lock(&cgroup_mutex); |
| 3455 | |
| 3456 | ret = kernfs_rename(kn, new_parent, new_name_str); |
| 3457 | |
| 3458 | mutex_unlock(&cgroup_mutex); |
| 3459 | |
| 3460 | kernfs_unbreak_active_protection(kn); |
| 3461 | kernfs_unbreak_active_protection(new_parent); |
| 3462 | return ret; |
| 3463 | } |
| 3464 | |
| 3465 | /* set uid and gid of cgroup dirs and files to that of the creator */ |
| 3466 | static int cgroup_kn_set_ugid(struct kernfs_node *kn) |
| 3467 | { |
| 3468 | struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, |
| 3469 | .ia_uid = current_fsuid(), |
| 3470 | .ia_gid = current_fsgid(), }; |
| 3471 | |
| 3472 | if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && |
| 3473 | gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) |
| 3474 | return 0; |
| 3475 | |
| 3476 | return kernfs_setattr(kn, &iattr); |
| 3477 | } |
| 3478 | |
| 3479 | static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, |
| 3480 | struct cftype *cft) |
| 3481 | { |
| 3482 | char name[CGROUP_FILE_NAME_MAX]; |
| 3483 | struct kernfs_node *kn; |
| 3484 | struct lock_class_key *key = NULL; |
| 3485 | int ret; |
| 3486 | |
| 3487 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 3488 | key = &cft->lockdep_key; |
| 3489 | #endif |
| 3490 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
| 3491 | cgroup_file_mode(cft), 0, cft->kf_ops, cft, |
| 3492 | NULL, key); |
| 3493 | if (IS_ERR(kn)) |
| 3494 | return PTR_ERR(kn); |
| 3495 | |
| 3496 | ret = cgroup_kn_set_ugid(kn); |
| 3497 | if (ret) { |
| 3498 | kernfs_remove(kn); |
| 3499 | return ret; |
| 3500 | } |
| 3501 | |
| 3502 | if (cft->file_offset) { |
| 3503 | struct cgroup_file *cfile = (void *)css + cft->file_offset; |
| 3504 | |
| 3505 | spin_lock_irq(&cgroup_file_kn_lock); |
| 3506 | cfile->kn = kn; |
| 3507 | spin_unlock_irq(&cgroup_file_kn_lock); |
| 3508 | } |
| 3509 | |
| 3510 | return 0; |
| 3511 | } |
| 3512 | |
| 3513 | /** |
| 3514 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 3515 | * @css: the target css |
| 3516 | * @cgrp: the target cgroup (usually css->cgroup) |
| 3517 | * @cfts: array of cftypes to be added |
| 3518 | * @is_add: whether to add or remove |
| 3519 | * |
| 3520 | * Depending on @is_add, add or remove files defined by @cfts on @cgrp. |
| 3521 | * For removals, this function never fails. |
| 3522 | */ |
| 3523 | static int cgroup_addrm_files(struct cgroup_subsys_state *css, |
| 3524 | struct cgroup *cgrp, struct cftype cfts[], |
| 3525 | bool is_add) |
| 3526 | { |
| 3527 | struct cftype *cft, *cft_end = NULL; |
| 3528 | int ret = 0; |
| 3529 | |
| 3530 | lockdep_assert_held(&cgroup_mutex); |
| 3531 | |
| 3532 | restart: |
| 3533 | for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) { |
| 3534 | /* does cft->flags tell us to skip this file on @cgrp? */ |
| 3535 | if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) |
| 3536 | continue; |
| 3537 | if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp)) |
| 3538 | continue; |
| 3539 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp)) |
| 3540 | continue; |
| 3541 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp)) |
| 3542 | continue; |
| 3543 | |
| 3544 | if (is_add) { |
| 3545 | ret = cgroup_add_file(css, cgrp, cft); |
| 3546 | if (ret) { |
| 3547 | pr_warn("%s: failed to add %s, err=%d\n", |
| 3548 | __func__, cft->name, ret); |
| 3549 | cft_end = cft; |
| 3550 | is_add = false; |
| 3551 | goto restart; |
| 3552 | } |
| 3553 | } else { |
| 3554 | cgroup_rm_file(cgrp, cft); |
| 3555 | } |
| 3556 | } |
| 3557 | return ret; |
| 3558 | } |
| 3559 | |
| 3560 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
| 3561 | { |
| 3562 | LIST_HEAD(pending); |
| 3563 | struct cgroup_subsys *ss = cfts[0].ss; |
| 3564 | struct cgroup *root = &ss->root->cgrp; |
| 3565 | struct cgroup_subsys_state *css; |
| 3566 | int ret = 0; |
| 3567 | |
| 3568 | lockdep_assert_held(&cgroup_mutex); |
| 3569 | |
| 3570 | /* add/rm files for all cgroups created before */ |
| 3571 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
| 3572 | struct cgroup *cgrp = css->cgroup; |
| 3573 | |
| 3574 | if (!(css->flags & CSS_VISIBLE)) |
| 3575 | continue; |
| 3576 | |
| 3577 | ret = cgroup_addrm_files(css, cgrp, cfts, is_add); |
| 3578 | if (ret) |
| 3579 | break; |
| 3580 | } |
| 3581 | |
| 3582 | if (is_add && !ret) |
| 3583 | kernfs_activate(root->kn); |
| 3584 | return ret; |
| 3585 | } |
| 3586 | |
| 3587 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 3588 | { |
| 3589 | struct cftype *cft; |
| 3590 | |
| 3591 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 3592 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 3593 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 3594 | kfree(cft->kf_ops); |
| 3595 | cft->kf_ops = NULL; |
| 3596 | cft->ss = NULL; |
| 3597 | |
| 3598 | /* revert flags set by cgroup core while adding @cfts */ |
| 3599 | cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL); |
| 3600 | } |
| 3601 | } |
| 3602 | |
| 3603 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 3604 | { |
| 3605 | struct cftype *cft; |
| 3606 | |
| 3607 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 3608 | struct kernfs_ops *kf_ops; |
| 3609 | |
| 3610 | WARN_ON(cft->ss || cft->kf_ops); |
| 3611 | |
| 3612 | if (cft->seq_start) |
| 3613 | kf_ops = &cgroup_kf_ops; |
| 3614 | else |
| 3615 | kf_ops = &cgroup_kf_single_ops; |
| 3616 | |
| 3617 | /* |
| 3618 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 3619 | * make a copy of kf_ops to set its atomic_write_len. |
| 3620 | */ |
| 3621 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 3622 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 3623 | if (!kf_ops) { |
| 3624 | cgroup_exit_cftypes(cfts); |
| 3625 | return -ENOMEM; |
| 3626 | } |
| 3627 | kf_ops->atomic_write_len = cft->max_write_len; |
| 3628 | } |
| 3629 | |
| 3630 | cft->kf_ops = kf_ops; |
| 3631 | cft->ss = ss; |
| 3632 | } |
| 3633 | |
| 3634 | return 0; |
| 3635 | } |
| 3636 | |
| 3637 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 3638 | { |
| 3639 | lockdep_assert_held(&cgroup_mutex); |
| 3640 | |
| 3641 | if (!cfts || !cfts[0].ss) |
| 3642 | return -ENOENT; |
| 3643 | |
| 3644 | list_del(&cfts->node); |
| 3645 | cgroup_apply_cftypes(cfts, false); |
| 3646 | cgroup_exit_cftypes(cfts); |
| 3647 | return 0; |
| 3648 | } |
| 3649 | |
| 3650 | /** |
| 3651 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 3652 | * @cfts: zero-length name terminated array of cftypes |
| 3653 | * |
| 3654 | * Unregister @cfts. Files described by @cfts are removed from all |
| 3655 | * existing cgroups and all future cgroups won't have them either. This |
| 3656 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 3657 | * |
| 3658 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 3659 | * registered. |
| 3660 | */ |
| 3661 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 3662 | { |
| 3663 | int ret; |
| 3664 | |
| 3665 | mutex_lock(&cgroup_mutex); |
| 3666 | ret = cgroup_rm_cftypes_locked(cfts); |
| 3667 | mutex_unlock(&cgroup_mutex); |
| 3668 | return ret; |
| 3669 | } |
| 3670 | |
| 3671 | /** |
| 3672 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 3673 | * @ss: target cgroup subsystem |
| 3674 | * @cfts: zero-length name terminated array of cftypes |
| 3675 | * |
| 3676 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 3677 | * existing cgroups to which @ss is attached and all future cgroups will |
| 3678 | * have them too. This function can be called anytime whether @ss is |
| 3679 | * attached or not. |
| 3680 | * |
| 3681 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 3682 | * function currently returns 0 as long as @cfts registration is successful |
| 3683 | * even if some file creation attempts on existing cgroups fail. |
| 3684 | */ |
| 3685 | static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 3686 | { |
| 3687 | int ret; |
| 3688 | |
| 3689 | if (!cgroup_ssid_enabled(ss->id)) |
| 3690 | return 0; |
| 3691 | |
| 3692 | if (!cfts || cfts[0].name[0] == '\0') |
| 3693 | return 0; |
| 3694 | |
| 3695 | ret = cgroup_init_cftypes(ss, cfts); |
| 3696 | if (ret) |
| 3697 | return ret; |
| 3698 | |
| 3699 | mutex_lock(&cgroup_mutex); |
| 3700 | |
| 3701 | list_add_tail(&cfts->node, &ss->cfts); |
| 3702 | ret = cgroup_apply_cftypes(cfts, true); |
| 3703 | if (ret) |
| 3704 | cgroup_rm_cftypes_locked(cfts); |
| 3705 | |
| 3706 | mutex_unlock(&cgroup_mutex); |
| 3707 | return ret; |
| 3708 | } |
| 3709 | |
| 3710 | /** |
| 3711 | * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy |
| 3712 | * @ss: target cgroup subsystem |
| 3713 | * @cfts: zero-length name terminated array of cftypes |
| 3714 | * |
| 3715 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 3716 | * the default hierarchy. |
| 3717 | */ |
| 3718 | int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 3719 | { |
| 3720 | struct cftype *cft; |
| 3721 | |
| 3722 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
| 3723 | cft->flags |= __CFTYPE_ONLY_ON_DFL; |
| 3724 | return cgroup_add_cftypes(ss, cfts); |
| 3725 | } |
| 3726 | |
| 3727 | /** |
| 3728 | * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies |
| 3729 | * @ss: target cgroup subsystem |
| 3730 | * @cfts: zero-length name terminated array of cftypes |
| 3731 | * |
| 3732 | * Similar to cgroup_add_cftypes() but the added files are only used for |
| 3733 | * the legacy hierarchies. |
| 3734 | */ |
| 3735 | int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
| 3736 | { |
| 3737 | struct cftype *cft; |
| 3738 | |
| 3739 | for (cft = cfts; cft && cft->name[0] != '\0'; cft++) |
| 3740 | cft->flags |= __CFTYPE_NOT_ON_DFL; |
| 3741 | return cgroup_add_cftypes(ss, cfts); |
| 3742 | } |
| 3743 | |
| 3744 | /** |
| 3745 | * cgroup_file_notify - generate a file modified event for a cgroup_file |
| 3746 | * @cfile: target cgroup_file |
| 3747 | * |
| 3748 | * @cfile must have been obtained by setting cftype->file_offset. |
| 3749 | */ |
| 3750 | void cgroup_file_notify(struct cgroup_file *cfile) |
| 3751 | { |
| 3752 | unsigned long flags; |
| 3753 | |
| 3754 | spin_lock_irqsave(&cgroup_file_kn_lock, flags); |
| 3755 | if (cfile->kn) |
| 3756 | kernfs_notify(cfile->kn); |
| 3757 | spin_unlock_irqrestore(&cgroup_file_kn_lock, flags); |
| 3758 | } |
| 3759 | |
| 3760 | /** |
| 3761 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 3762 | * @cgrp: the cgroup in question |
| 3763 | * |
| 3764 | * Return the number of tasks in the cgroup. |
| 3765 | */ |
| 3766 | static int cgroup_task_count(const struct cgroup *cgrp) |
| 3767 | { |
| 3768 | int count = 0; |
| 3769 | struct cgrp_cset_link *link; |
| 3770 | |
| 3771 | spin_lock_bh(&css_set_lock); |
| 3772 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 3773 | count += atomic_read(&link->cset->refcount); |
| 3774 | spin_unlock_bh(&css_set_lock); |
| 3775 | return count; |
| 3776 | } |
| 3777 | |
| 3778 | /** |
| 3779 | * css_next_child - find the next child of a given css |
| 3780 | * @pos: the current position (%NULL to initiate traversal) |
| 3781 | * @parent: css whose children to walk |
| 3782 | * |
| 3783 | * This function returns the next child of @parent and should be called |
| 3784 | * under either cgroup_mutex or RCU read lock. The only requirement is |
| 3785 | * that @parent and @pos are accessible. The next sibling is guaranteed to |
| 3786 | * be returned regardless of their states. |
| 3787 | * |
| 3788 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 3789 | * css which finished ->css_online() is guaranteed to be visible in the |
| 3790 | * future iterations and will stay visible until the last reference is put. |
| 3791 | * A css which hasn't finished ->css_online() or already finished |
| 3792 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 3793 | * responsibility to synchronize against on/offlining. |
| 3794 | */ |
| 3795 | struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, |
| 3796 | struct cgroup_subsys_state *parent) |
| 3797 | { |
| 3798 | struct cgroup_subsys_state *next; |
| 3799 | |
| 3800 | cgroup_assert_mutex_or_rcu_locked(); |
| 3801 | |
| 3802 | /* |
| 3803 | * @pos could already have been unlinked from the sibling list. |
| 3804 | * Once a cgroup is removed, its ->sibling.next is no longer |
| 3805 | * updated when its next sibling changes. CSS_RELEASED is set when |
| 3806 | * @pos is taken off list, at which time its next pointer is valid, |
| 3807 | * and, as releases are serialized, the one pointed to by the next |
| 3808 | * pointer is guaranteed to not have started release yet. This |
| 3809 | * implies that if we observe !CSS_RELEASED on @pos in this RCU |
| 3810 | * critical section, the one pointed to by its next pointer is |
| 3811 | * guaranteed to not have finished its RCU grace period even if we |
| 3812 | * have dropped rcu_read_lock() inbetween iterations. |
| 3813 | * |
| 3814 | * If @pos has CSS_RELEASED set, its next pointer can't be |
| 3815 | * dereferenced; however, as each css is given a monotonically |
| 3816 | * increasing unique serial number and always appended to the |
| 3817 | * sibling list, the next one can be found by walking the parent's |
| 3818 | * children until the first css with higher serial number than |
| 3819 | * @pos's. While this path can be slower, it happens iff iteration |
| 3820 | * races against release and the race window is very small. |
| 3821 | */ |
| 3822 | if (!pos) { |
| 3823 | next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling); |
| 3824 | } else if (likely(!(pos->flags & CSS_RELEASED))) { |
| 3825 | next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling); |
| 3826 | } else { |
| 3827 | list_for_each_entry_rcu(next, &parent->children, sibling) |
| 3828 | if (next->serial_nr > pos->serial_nr) |
| 3829 | break; |
| 3830 | } |
| 3831 | |
| 3832 | /* |
| 3833 | * @next, if not pointing to the head, can be dereferenced and is |
| 3834 | * the next sibling. |
| 3835 | */ |
| 3836 | if (&next->sibling != &parent->children) |
| 3837 | return next; |
| 3838 | return NULL; |
| 3839 | } |
| 3840 | |
| 3841 | /** |
| 3842 | * css_next_descendant_pre - find the next descendant for pre-order walk |
| 3843 | * @pos: the current position (%NULL to initiate traversal) |
| 3844 | * @root: css whose descendants to walk |
| 3845 | * |
| 3846 | * To be used by css_for_each_descendant_pre(). Find the next descendant |
| 3847 | * to visit for pre-order traversal of @root's descendants. @root is |
| 3848 | * included in the iteration and the first node to be visited. |
| 3849 | * |
| 3850 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3851 | * doesn't require the whole traversal to be contained in a single critical |
| 3852 | * section. This function will return the correct next descendant as long |
| 3853 | * as both @pos and @root are accessible and @pos is a descendant of @root. |
| 3854 | * |
| 3855 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 3856 | * css which finished ->css_online() is guaranteed to be visible in the |
| 3857 | * future iterations and will stay visible until the last reference is put. |
| 3858 | * A css which hasn't finished ->css_online() or already finished |
| 3859 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 3860 | * responsibility to synchronize against on/offlining. |
| 3861 | */ |
| 3862 | struct cgroup_subsys_state * |
| 3863 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 3864 | struct cgroup_subsys_state *root) |
| 3865 | { |
| 3866 | struct cgroup_subsys_state *next; |
| 3867 | |
| 3868 | cgroup_assert_mutex_or_rcu_locked(); |
| 3869 | |
| 3870 | /* if first iteration, visit @root */ |
| 3871 | if (!pos) |
| 3872 | return root; |
| 3873 | |
| 3874 | /* visit the first child if exists */ |
| 3875 | next = css_next_child(NULL, pos); |
| 3876 | if (next) |
| 3877 | return next; |
| 3878 | |
| 3879 | /* no child, visit my or the closest ancestor's next sibling */ |
| 3880 | while (pos != root) { |
| 3881 | next = css_next_child(pos, pos->parent); |
| 3882 | if (next) |
| 3883 | return next; |
| 3884 | pos = pos->parent; |
| 3885 | } |
| 3886 | |
| 3887 | return NULL; |
| 3888 | } |
| 3889 | |
| 3890 | /** |
| 3891 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 3892 | * @pos: css of interest |
| 3893 | * |
| 3894 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 3895 | * is returned. This can be used during pre-order traversal to skip |
| 3896 | * subtree of @pos. |
| 3897 | * |
| 3898 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3899 | * doesn't require the whole traversal to be contained in a single critical |
| 3900 | * section. This function will return the correct rightmost descendant as |
| 3901 | * long as @pos is accessible. |
| 3902 | */ |
| 3903 | struct cgroup_subsys_state * |
| 3904 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
| 3905 | { |
| 3906 | struct cgroup_subsys_state *last, *tmp; |
| 3907 | |
| 3908 | cgroup_assert_mutex_or_rcu_locked(); |
| 3909 | |
| 3910 | do { |
| 3911 | last = pos; |
| 3912 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3913 | pos = NULL; |
| 3914 | css_for_each_child(tmp, last) |
| 3915 | pos = tmp; |
| 3916 | } while (pos); |
| 3917 | |
| 3918 | return last; |
| 3919 | } |
| 3920 | |
| 3921 | static struct cgroup_subsys_state * |
| 3922 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
| 3923 | { |
| 3924 | struct cgroup_subsys_state *last; |
| 3925 | |
| 3926 | do { |
| 3927 | last = pos; |
| 3928 | pos = css_next_child(NULL, pos); |
| 3929 | } while (pos); |
| 3930 | |
| 3931 | return last; |
| 3932 | } |
| 3933 | |
| 3934 | /** |
| 3935 | * css_next_descendant_post - find the next descendant for post-order walk |
| 3936 | * @pos: the current position (%NULL to initiate traversal) |
| 3937 | * @root: css whose descendants to walk |
| 3938 | * |
| 3939 | * To be used by css_for_each_descendant_post(). Find the next descendant |
| 3940 | * to visit for post-order traversal of @root's descendants. @root is |
| 3941 | * included in the iteration and the last node to be visited. |
| 3942 | * |
| 3943 | * While this function requires cgroup_mutex or RCU read locking, it |
| 3944 | * doesn't require the whole traversal to be contained in a single critical |
| 3945 | * section. This function will return the correct next descendant as long |
| 3946 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 3947 | * @cgroup. |
| 3948 | * |
| 3949 | * If a subsystem synchronizes ->css_online() and the start of iteration, a |
| 3950 | * css which finished ->css_online() is guaranteed to be visible in the |
| 3951 | * future iterations and will stay visible until the last reference is put. |
| 3952 | * A css which hasn't finished ->css_online() or already finished |
| 3953 | * ->css_offline() may show up during traversal. It's each subsystem's |
| 3954 | * responsibility to synchronize against on/offlining. |
| 3955 | */ |
| 3956 | struct cgroup_subsys_state * |
| 3957 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 3958 | struct cgroup_subsys_state *root) |
| 3959 | { |
| 3960 | struct cgroup_subsys_state *next; |
| 3961 | |
| 3962 | cgroup_assert_mutex_or_rcu_locked(); |
| 3963 | |
| 3964 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 3965 | if (!pos) |
| 3966 | return css_leftmost_descendant(root); |
| 3967 | |
| 3968 | /* if we visited @root, we're done */ |
| 3969 | if (pos == root) |
| 3970 | return NULL; |
| 3971 | |
| 3972 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
| 3973 | next = css_next_child(pos, pos->parent); |
| 3974 | if (next) |
| 3975 | return css_leftmost_descendant(next); |
| 3976 | |
| 3977 | /* no sibling left, visit parent */ |
| 3978 | return pos->parent; |
| 3979 | } |
| 3980 | |
| 3981 | /** |
| 3982 | * css_has_online_children - does a css have online children |
| 3983 | * @css: the target css |
| 3984 | * |
| 3985 | * Returns %true if @css has any online children; otherwise, %false. This |
| 3986 | * function can be called from any context but the caller is responsible |
| 3987 | * for synchronizing against on/offlining as necessary. |
| 3988 | */ |
| 3989 | bool css_has_online_children(struct cgroup_subsys_state *css) |
| 3990 | { |
| 3991 | struct cgroup_subsys_state *child; |
| 3992 | bool ret = false; |
| 3993 | |
| 3994 | rcu_read_lock(); |
| 3995 | css_for_each_child(child, css) { |
| 3996 | if (child->flags & CSS_ONLINE) { |
| 3997 | ret = true; |
| 3998 | break; |
| 3999 | } |
| 4000 | } |
| 4001 | rcu_read_unlock(); |
| 4002 | return ret; |
| 4003 | } |
| 4004 | |
| 4005 | /** |
| 4006 | * css_task_iter_advance_css_set - advance a task itererator to the next css_set |
| 4007 | * @it: the iterator to advance |
| 4008 | * |
| 4009 | * Advance @it to the next css_set to walk. |
| 4010 | */ |
| 4011 | static void css_task_iter_advance_css_set(struct css_task_iter *it) |
| 4012 | { |
| 4013 | struct list_head *l = it->cset_pos; |
| 4014 | struct cgrp_cset_link *link; |
| 4015 | struct css_set *cset; |
| 4016 | |
| 4017 | lockdep_assert_held(&css_set_lock); |
| 4018 | |
| 4019 | /* Advance to the next non-empty css_set */ |
| 4020 | do { |
| 4021 | l = l->next; |
| 4022 | if (l == it->cset_head) { |
| 4023 | it->cset_pos = NULL; |
| 4024 | it->task_pos = NULL; |
| 4025 | return; |
| 4026 | } |
| 4027 | |
| 4028 | if (it->ss) { |
| 4029 | cset = container_of(l, struct css_set, |
| 4030 | e_cset_node[it->ss->id]); |
| 4031 | } else { |
| 4032 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 4033 | cset = link->cset; |
| 4034 | } |
| 4035 | } while (!css_set_populated(cset)); |
| 4036 | |
| 4037 | it->cset_pos = l; |
| 4038 | |
| 4039 | if (!list_empty(&cset->tasks)) |
| 4040 | it->task_pos = cset->tasks.next; |
| 4041 | else |
| 4042 | it->task_pos = cset->mg_tasks.next; |
| 4043 | |
| 4044 | it->tasks_head = &cset->tasks; |
| 4045 | it->mg_tasks_head = &cset->mg_tasks; |
| 4046 | |
| 4047 | /* |
| 4048 | * We don't keep css_sets locked across iteration steps and thus |
| 4049 | * need to take steps to ensure that iteration can be resumed after |
| 4050 | * the lock is re-acquired. Iteration is performed at two levels - |
| 4051 | * css_sets and tasks in them. |
| 4052 | * |
| 4053 | * Once created, a css_set never leaves its cgroup lists, so a |
| 4054 | * pinned css_set is guaranteed to stay put and we can resume |
| 4055 | * iteration afterwards. |
| 4056 | * |
| 4057 | * Tasks may leave @cset across iteration steps. This is resolved |
| 4058 | * by registering each iterator with the css_set currently being |
| 4059 | * walked and making css_set_move_task() advance iterators whose |
| 4060 | * next task is leaving. |
| 4061 | */ |
| 4062 | if (it->cur_cset) { |
| 4063 | list_del(&it->iters_node); |
| 4064 | put_css_set_locked(it->cur_cset); |
| 4065 | } |
| 4066 | get_css_set(cset); |
| 4067 | it->cur_cset = cset; |
| 4068 | list_add(&it->iters_node, &cset->task_iters); |
| 4069 | } |
| 4070 | |
| 4071 | static void css_task_iter_advance(struct css_task_iter *it) |
| 4072 | { |
| 4073 | struct list_head *l = it->task_pos; |
| 4074 | |
| 4075 | lockdep_assert_held(&css_set_lock); |
| 4076 | WARN_ON_ONCE(!l); |
| 4077 | |
| 4078 | /* |
| 4079 | * Advance iterator to find next entry. cset->tasks is consumed |
| 4080 | * first and then ->mg_tasks. After ->mg_tasks, we move onto the |
| 4081 | * next cset. |
| 4082 | */ |
| 4083 | l = l->next; |
| 4084 | |
| 4085 | if (l == it->tasks_head) |
| 4086 | l = it->mg_tasks_head->next; |
| 4087 | |
| 4088 | if (l == it->mg_tasks_head) |
| 4089 | css_task_iter_advance_css_set(it); |
| 4090 | else |
| 4091 | it->task_pos = l; |
| 4092 | } |
| 4093 | |
| 4094 | /** |
| 4095 | * css_task_iter_start - initiate task iteration |
| 4096 | * @css: the css to walk tasks of |
| 4097 | * @it: the task iterator to use |
| 4098 | * |
| 4099 | * Initiate iteration through the tasks of @css. The caller can call |
| 4100 | * css_task_iter_next() to walk through the tasks until the function |
| 4101 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 4102 | * called. |
| 4103 | */ |
| 4104 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 4105 | struct css_task_iter *it) |
| 4106 | { |
| 4107 | /* no one should try to iterate before mounting cgroups */ |
| 4108 | WARN_ON_ONCE(!use_task_css_set_links); |
| 4109 | |
| 4110 | memset(it, 0, sizeof(*it)); |
| 4111 | |
| 4112 | spin_lock_bh(&css_set_lock); |
| 4113 | |
| 4114 | it->ss = css->ss; |
| 4115 | |
| 4116 | if (it->ss) |
| 4117 | it->cset_pos = &css->cgroup->e_csets[css->ss->id]; |
| 4118 | else |
| 4119 | it->cset_pos = &css->cgroup->cset_links; |
| 4120 | |
| 4121 | it->cset_head = it->cset_pos; |
| 4122 | |
| 4123 | css_task_iter_advance_css_set(it); |
| 4124 | |
| 4125 | spin_unlock_bh(&css_set_lock); |
| 4126 | } |
| 4127 | |
| 4128 | /** |
| 4129 | * css_task_iter_next - return the next task for the iterator |
| 4130 | * @it: the task iterator being iterated |
| 4131 | * |
| 4132 | * The "next" function for task iteration. @it should have been |
| 4133 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 4134 | * reaches the end. |
| 4135 | */ |
| 4136 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
| 4137 | { |
| 4138 | if (it->cur_task) { |
| 4139 | put_task_struct(it->cur_task); |
| 4140 | it->cur_task = NULL; |
| 4141 | } |
| 4142 | |
| 4143 | spin_lock_bh(&css_set_lock); |
| 4144 | |
| 4145 | if (it->task_pos) { |
| 4146 | it->cur_task = list_entry(it->task_pos, struct task_struct, |
| 4147 | cg_list); |
| 4148 | get_task_struct(it->cur_task); |
| 4149 | css_task_iter_advance(it); |
| 4150 | } |
| 4151 | |
| 4152 | spin_unlock_bh(&css_set_lock); |
| 4153 | |
| 4154 | return it->cur_task; |
| 4155 | } |
| 4156 | |
| 4157 | /** |
| 4158 | * css_task_iter_end - finish task iteration |
| 4159 | * @it: the task iterator to finish |
| 4160 | * |
| 4161 | * Finish task iteration started by css_task_iter_start(). |
| 4162 | */ |
| 4163 | void css_task_iter_end(struct css_task_iter *it) |
| 4164 | { |
| 4165 | if (it->cur_cset) { |
| 4166 | spin_lock_bh(&css_set_lock); |
| 4167 | list_del(&it->iters_node); |
| 4168 | put_css_set_locked(it->cur_cset); |
| 4169 | spin_unlock_bh(&css_set_lock); |
| 4170 | } |
| 4171 | |
| 4172 | if (it->cur_task) |
| 4173 | put_task_struct(it->cur_task); |
| 4174 | } |
| 4175 | |
| 4176 | /** |
| 4177 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 4178 | * @to: cgroup to which the tasks will be moved |
| 4179 | * @from: cgroup in which the tasks currently reside |
| 4180 | * |
| 4181 | * Locking rules between cgroup_post_fork() and the migration path |
| 4182 | * guarantee that, if a task is forking while being migrated, the new child |
| 4183 | * is guaranteed to be either visible in the source cgroup after the |
| 4184 | * parent's migration is complete or put into the target cgroup. No task |
| 4185 | * can slip out of migration through forking. |
| 4186 | */ |
| 4187 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 4188 | { |
| 4189 | LIST_HEAD(preloaded_csets); |
| 4190 | struct cgrp_cset_link *link; |
| 4191 | struct css_task_iter it; |
| 4192 | struct task_struct *task; |
| 4193 | int ret; |
| 4194 | |
| 4195 | mutex_lock(&cgroup_mutex); |
| 4196 | |
| 4197 | /* all tasks in @from are being moved, all csets are source */ |
| 4198 | spin_lock_bh(&css_set_lock); |
| 4199 | list_for_each_entry(link, &from->cset_links, cset_link) |
| 4200 | cgroup_migrate_add_src(link->cset, to, &preloaded_csets); |
| 4201 | spin_unlock_bh(&css_set_lock); |
| 4202 | |
| 4203 | ret = cgroup_migrate_prepare_dst(to, &preloaded_csets); |
| 4204 | if (ret) |
| 4205 | goto out_err; |
| 4206 | |
| 4207 | /* |
| 4208 | * Migrate tasks one-by-one until @from is empty. This fails iff |
| 4209 | * ->can_attach() fails. |
| 4210 | */ |
| 4211 | do { |
| 4212 | css_task_iter_start(&from->self, &it); |
| 4213 | task = css_task_iter_next(&it); |
| 4214 | if (task) |
| 4215 | get_task_struct(task); |
| 4216 | css_task_iter_end(&it); |
| 4217 | |
| 4218 | if (task) { |
| 4219 | ret = cgroup_migrate(task, false, to); |
| 4220 | put_task_struct(task); |
| 4221 | } |
| 4222 | } while (task && !ret); |
| 4223 | out_err: |
| 4224 | cgroup_migrate_finish(&preloaded_csets); |
| 4225 | mutex_unlock(&cgroup_mutex); |
| 4226 | return ret; |
| 4227 | } |
| 4228 | |
| 4229 | /* |
| 4230 | * Stuff for reading the 'tasks'/'procs' files. |
| 4231 | * |
| 4232 | * Reading this file can return large amounts of data if a cgroup has |
| 4233 | * *lots* of attached tasks. So it may need several calls to read(), |
| 4234 | * but we cannot guarantee that the information we produce is correct |
| 4235 | * unless we produce it entirely atomically. |
| 4236 | * |
| 4237 | */ |
| 4238 | |
| 4239 | /* which pidlist file are we talking about? */ |
| 4240 | enum cgroup_filetype { |
| 4241 | CGROUP_FILE_PROCS, |
| 4242 | CGROUP_FILE_TASKS, |
| 4243 | }; |
| 4244 | |
| 4245 | /* |
| 4246 | * A pidlist is a list of pids that virtually represents the contents of one |
| 4247 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 4248 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 4249 | * to the cgroup. |
| 4250 | */ |
| 4251 | struct cgroup_pidlist { |
| 4252 | /* |
| 4253 | * used to find which pidlist is wanted. doesn't change as long as |
| 4254 | * this particular list stays in the list. |
| 4255 | */ |
| 4256 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 4257 | /* array of xids */ |
| 4258 | pid_t *list; |
| 4259 | /* how many elements the above list has */ |
| 4260 | int length; |
| 4261 | /* each of these stored in a list by its cgroup */ |
| 4262 | struct list_head links; |
| 4263 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 4264 | struct cgroup *owner; |
| 4265 | /* for delayed destruction */ |
| 4266 | struct delayed_work destroy_dwork; |
| 4267 | }; |
| 4268 | |
| 4269 | /* |
| 4270 | * The following two functions "fix" the issue where there are more pids |
| 4271 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 4272 | * TODO: replace with a kernel-wide solution to this problem |
| 4273 | */ |
| 4274 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 4275 | static void *pidlist_allocate(int count) |
| 4276 | { |
| 4277 | if (PIDLIST_TOO_LARGE(count)) |
| 4278 | return vmalloc(count * sizeof(pid_t)); |
| 4279 | else |
| 4280 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 4281 | } |
| 4282 | |
| 4283 | static void pidlist_free(void *p) |
| 4284 | { |
| 4285 | kvfree(p); |
| 4286 | } |
| 4287 | |
| 4288 | /* |
| 4289 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 4290 | * should be left afterwards. |
| 4291 | */ |
| 4292 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 4293 | { |
| 4294 | struct cgroup_pidlist *l, *tmp_l; |
| 4295 | |
| 4296 | mutex_lock(&cgrp->pidlist_mutex); |
| 4297 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 4298 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 4299 | mutex_unlock(&cgrp->pidlist_mutex); |
| 4300 | |
| 4301 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 4302 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 4303 | } |
| 4304 | |
| 4305 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 4306 | { |
| 4307 | struct delayed_work *dwork = to_delayed_work(work); |
| 4308 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 4309 | destroy_dwork); |
| 4310 | struct cgroup_pidlist *tofree = NULL; |
| 4311 | |
| 4312 | mutex_lock(&l->owner->pidlist_mutex); |
| 4313 | |
| 4314 | /* |
| 4315 | * Destroy iff we didn't get queued again. The state won't change |
| 4316 | * as destroy_dwork can only be queued while locked. |
| 4317 | */ |
| 4318 | if (!delayed_work_pending(dwork)) { |
| 4319 | list_del(&l->links); |
| 4320 | pidlist_free(l->list); |
| 4321 | put_pid_ns(l->key.ns); |
| 4322 | tofree = l; |
| 4323 | } |
| 4324 | |
| 4325 | mutex_unlock(&l->owner->pidlist_mutex); |
| 4326 | kfree(tofree); |
| 4327 | } |
| 4328 | |
| 4329 | /* |
| 4330 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
| 4331 | * Returns the number of unique elements. |
| 4332 | */ |
| 4333 | static int pidlist_uniq(pid_t *list, int length) |
| 4334 | { |
| 4335 | int src, dest = 1; |
| 4336 | |
| 4337 | /* |
| 4338 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 4339 | * edge cases first; no work needs to be done for either |
| 4340 | */ |
| 4341 | if (length == 0 || length == 1) |
| 4342 | return length; |
| 4343 | /* src and dest walk down the list; dest counts unique elements */ |
| 4344 | for (src = 1; src < length; src++) { |
| 4345 | /* find next unique element */ |
| 4346 | while (list[src] == list[src-1]) { |
| 4347 | src++; |
| 4348 | if (src == length) |
| 4349 | goto after; |
| 4350 | } |
| 4351 | /* dest always points to where the next unique element goes */ |
| 4352 | list[dest] = list[src]; |
| 4353 | dest++; |
| 4354 | } |
| 4355 | after: |
| 4356 | return dest; |
| 4357 | } |
| 4358 | |
| 4359 | /* |
| 4360 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 4361 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 4362 | * different per namespace, each namespace needs differently sorted list, |
| 4363 | * making it impossible to use, for example, single rbtree of member tasks |
| 4364 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 4365 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 4366 | * pidlists keyed by cgroup and namespace. |
| 4367 | * |
| 4368 | * All this extra complexity was caused by the original implementation |
| 4369 | * committing to an entirely unnecessary property. In the long term, we |
| 4370 | * want to do away with it. Explicitly scramble sort order if on the |
| 4371 | * default hierarchy so that no such expectation exists in the new |
| 4372 | * interface. |
| 4373 | * |
| 4374 | * Scrambling is done by swapping every two consecutive bits, which is |
| 4375 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 4376 | */ |
| 4377 | static pid_t pid_fry(pid_t pid) |
| 4378 | { |
| 4379 | unsigned a = pid & 0x55555555; |
| 4380 | unsigned b = pid & 0xAAAAAAAA; |
| 4381 | |
| 4382 | return (a << 1) | (b >> 1); |
| 4383 | } |
| 4384 | |
| 4385 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 4386 | { |
| 4387 | if (cgroup_on_dfl(cgrp)) |
| 4388 | return pid_fry(pid); |
| 4389 | else |
| 4390 | return pid; |
| 4391 | } |
| 4392 | |
| 4393 | static int cmppid(const void *a, const void *b) |
| 4394 | { |
| 4395 | return *(pid_t *)a - *(pid_t *)b; |
| 4396 | } |
| 4397 | |
| 4398 | static int fried_cmppid(const void *a, const void *b) |
| 4399 | { |
| 4400 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 4401 | } |
| 4402 | |
| 4403 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 4404 | enum cgroup_filetype type) |
| 4405 | { |
| 4406 | struct cgroup_pidlist *l; |
| 4407 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 4408 | struct pid_namespace *ns = task_active_pid_ns(current); |
| 4409 | |
| 4410 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 4411 | |
| 4412 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 4413 | if (l->key.type == type && l->key.ns == ns) |
| 4414 | return l; |
| 4415 | return NULL; |
| 4416 | } |
| 4417 | |
| 4418 | /* |
| 4419 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 4420 | * returns with the lock on that pidlist already held, and takes care |
| 4421 | * of the use count, or returns NULL with no locks held if we're out of |
| 4422 | * memory. |
| 4423 | */ |
| 4424 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 4425 | enum cgroup_filetype type) |
| 4426 | { |
| 4427 | struct cgroup_pidlist *l; |
| 4428 | |
| 4429 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 4430 | |
| 4431 | l = cgroup_pidlist_find(cgrp, type); |
| 4432 | if (l) |
| 4433 | return l; |
| 4434 | |
| 4435 | /* entry not found; create a new one */ |
| 4436 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
| 4437 | if (!l) |
| 4438 | return l; |
| 4439 | |
| 4440 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
| 4441 | l->key.type = type; |
| 4442 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 4443 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
| 4444 | l->owner = cgrp; |
| 4445 | list_add(&l->links, &cgrp->pidlists); |
| 4446 | return l; |
| 4447 | } |
| 4448 | |
| 4449 | /* |
| 4450 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 4451 | */ |
| 4452 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 4453 | struct cgroup_pidlist **lp) |
| 4454 | { |
| 4455 | pid_t *array; |
| 4456 | int length; |
| 4457 | int pid, n = 0; /* used for populating the array */ |
| 4458 | struct css_task_iter it; |
| 4459 | struct task_struct *tsk; |
| 4460 | struct cgroup_pidlist *l; |
| 4461 | |
| 4462 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 4463 | |
| 4464 | /* |
| 4465 | * If cgroup gets more users after we read count, we won't have |
| 4466 | * enough space - tough. This race is indistinguishable to the |
| 4467 | * caller from the case that the additional cgroup users didn't |
| 4468 | * show up until sometime later on. |
| 4469 | */ |
| 4470 | length = cgroup_task_count(cgrp); |
| 4471 | array = pidlist_allocate(length); |
| 4472 | if (!array) |
| 4473 | return -ENOMEM; |
| 4474 | /* now, populate the array */ |
| 4475 | css_task_iter_start(&cgrp->self, &it); |
| 4476 | while ((tsk = css_task_iter_next(&it))) { |
| 4477 | if (unlikely(n == length)) |
| 4478 | break; |
| 4479 | /* get tgid or pid for procs or tasks file respectively */ |
| 4480 | if (type == CGROUP_FILE_PROCS) |
| 4481 | pid = task_tgid_vnr(tsk); |
| 4482 | else |
| 4483 | pid = task_pid_vnr(tsk); |
| 4484 | if (pid > 0) /* make sure to only use valid results */ |
| 4485 | array[n++] = pid; |
| 4486 | } |
| 4487 | css_task_iter_end(&it); |
| 4488 | length = n; |
| 4489 | /* now sort & (if procs) strip out duplicates */ |
| 4490 | if (cgroup_on_dfl(cgrp)) |
| 4491 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 4492 | else |
| 4493 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
| 4494 | if (type == CGROUP_FILE_PROCS) |
| 4495 | length = pidlist_uniq(array, length); |
| 4496 | |
| 4497 | l = cgroup_pidlist_find_create(cgrp, type); |
| 4498 | if (!l) { |
| 4499 | pidlist_free(array); |
| 4500 | return -ENOMEM; |
| 4501 | } |
| 4502 | |
| 4503 | /* store array, freeing old if necessary */ |
| 4504 | pidlist_free(l->list); |
| 4505 | l->list = array; |
| 4506 | l->length = length; |
| 4507 | *lp = l; |
| 4508 | return 0; |
| 4509 | } |
| 4510 | |
| 4511 | /** |
| 4512 | * cgroupstats_build - build and fill cgroupstats |
| 4513 | * @stats: cgroupstats to fill information into |
| 4514 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 4515 | * been requested. |
| 4516 | * |
| 4517 | * Build and fill cgroupstats so that taskstats can export it to user |
| 4518 | * space. |
| 4519 | */ |
| 4520 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 4521 | { |
| 4522 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
| 4523 | struct cgroup *cgrp; |
| 4524 | struct css_task_iter it; |
| 4525 | struct task_struct *tsk; |
| 4526 | |
| 4527 | /* it should be kernfs_node belonging to cgroupfs and is a directory */ |
| 4528 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 4529 | kernfs_type(kn) != KERNFS_DIR) |
| 4530 | return -EINVAL; |
| 4531 | |
| 4532 | mutex_lock(&cgroup_mutex); |
| 4533 | |
| 4534 | /* |
| 4535 | * We aren't being called from kernfs and there's no guarantee on |
| 4536 | * @kn->priv's validity. For this and css_tryget_online_from_dir(), |
| 4537 | * @kn->priv is RCU safe. Let's do the RCU dancing. |
| 4538 | */ |
| 4539 | rcu_read_lock(); |
| 4540 | cgrp = rcu_dereference(kn->priv); |
| 4541 | if (!cgrp || cgroup_is_dead(cgrp)) { |
| 4542 | rcu_read_unlock(); |
| 4543 | mutex_unlock(&cgroup_mutex); |
| 4544 | return -ENOENT; |
| 4545 | } |
| 4546 | rcu_read_unlock(); |
| 4547 | |
| 4548 | css_task_iter_start(&cgrp->self, &it); |
| 4549 | while ((tsk = css_task_iter_next(&it))) { |
| 4550 | switch (tsk->state) { |
| 4551 | case TASK_RUNNING: |
| 4552 | stats->nr_running++; |
| 4553 | break; |
| 4554 | case TASK_INTERRUPTIBLE: |
| 4555 | stats->nr_sleeping++; |
| 4556 | break; |
| 4557 | case TASK_UNINTERRUPTIBLE: |
| 4558 | stats->nr_uninterruptible++; |
| 4559 | break; |
| 4560 | case TASK_STOPPED: |
| 4561 | stats->nr_stopped++; |
| 4562 | break; |
| 4563 | default: |
| 4564 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 4565 | stats->nr_io_wait++; |
| 4566 | break; |
| 4567 | } |
| 4568 | } |
| 4569 | css_task_iter_end(&it); |
| 4570 | |
| 4571 | mutex_unlock(&cgroup_mutex); |
| 4572 | return 0; |
| 4573 | } |
| 4574 | |
| 4575 | |
| 4576 | /* |
| 4577 | * seq_file methods for the tasks/procs files. The seq_file position is the |
| 4578 | * next pid to display; the seq_file iterator is a pointer to the pid |
| 4579 | * in the cgroup->l->list array. |
| 4580 | */ |
| 4581 | |
| 4582 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
| 4583 | { |
| 4584 | /* |
| 4585 | * Initially we receive a position value that corresponds to |
| 4586 | * one more than the last pid shown (or 0 on the first call or |
| 4587 | * after a seek to the start). Use a binary-search to find the |
| 4588 | * next pid to display, if any |
| 4589 | */ |
| 4590 | struct kernfs_open_file *of = s->private; |
| 4591 | struct cgroup *cgrp = seq_css(s)->cgroup; |
| 4592 | struct cgroup_pidlist *l; |
| 4593 | enum cgroup_filetype type = seq_cft(s)->private; |
| 4594 | int index = 0, pid = *pos; |
| 4595 | int *iter, ret; |
| 4596 | |
| 4597 | mutex_lock(&cgrp->pidlist_mutex); |
| 4598 | |
| 4599 | /* |
| 4600 | * !NULL @of->priv indicates that this isn't the first start() |
| 4601 | * after open. If the matching pidlist is around, we can use that. |
| 4602 | * Look for it. Note that @of->priv can't be used directly. It |
| 4603 | * could already have been destroyed. |
| 4604 | */ |
| 4605 | if (of->priv) |
| 4606 | of->priv = cgroup_pidlist_find(cgrp, type); |
| 4607 | |
| 4608 | /* |
| 4609 | * Either this is the first start() after open or the matching |
| 4610 | * pidlist has been destroyed inbetween. Create a new one. |
| 4611 | */ |
| 4612 | if (!of->priv) { |
| 4613 | ret = pidlist_array_load(cgrp, type, |
| 4614 | (struct cgroup_pidlist **)&of->priv); |
| 4615 | if (ret) |
| 4616 | return ERR_PTR(ret); |
| 4617 | } |
| 4618 | l = of->priv; |
| 4619 | |
| 4620 | if (pid) { |
| 4621 | int end = l->length; |
| 4622 | |
| 4623 | while (index < end) { |
| 4624 | int mid = (index + end) / 2; |
| 4625 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
| 4626 | index = mid; |
| 4627 | break; |
| 4628 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
| 4629 | index = mid + 1; |
| 4630 | else |
| 4631 | end = mid; |
| 4632 | } |
| 4633 | } |
| 4634 | /* If we're off the end of the array, we're done */ |
| 4635 | if (index >= l->length) |
| 4636 | return NULL; |
| 4637 | /* Update the abstract position to be the actual pid that we found */ |
| 4638 | iter = l->list + index; |
| 4639 | *pos = cgroup_pid_fry(cgrp, *iter); |
| 4640 | return iter; |
| 4641 | } |
| 4642 | |
| 4643 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
| 4644 | { |
| 4645 | struct kernfs_open_file *of = s->private; |
| 4646 | struct cgroup_pidlist *l = of->priv; |
| 4647 | |
| 4648 | if (l) |
| 4649 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
| 4650 | CGROUP_PIDLIST_DESTROY_DELAY); |
| 4651 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
| 4652 | } |
| 4653 | |
| 4654 | static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos) |
| 4655 | { |
| 4656 | struct kernfs_open_file *of = s->private; |
| 4657 | struct cgroup_pidlist *l = of->priv; |
| 4658 | pid_t *p = v; |
| 4659 | pid_t *end = l->list + l->length; |
| 4660 | /* |
| 4661 | * Advance to the next pid in the array. If this goes off the |
| 4662 | * end, we're done |
| 4663 | */ |
| 4664 | p++; |
| 4665 | if (p >= end) { |
| 4666 | return NULL; |
| 4667 | } else { |
| 4668 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
| 4669 | return p; |
| 4670 | } |
| 4671 | } |
| 4672 | |
| 4673 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
| 4674 | { |
| 4675 | seq_printf(s, "%d\n", *(int *)v); |
| 4676 | |
| 4677 | return 0; |
| 4678 | } |
| 4679 | |
| 4680 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 4681 | struct cftype *cft) |
| 4682 | { |
| 4683 | return notify_on_release(css->cgroup); |
| 4684 | } |
| 4685 | |
| 4686 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 4687 | struct cftype *cft, u64 val) |
| 4688 | { |
| 4689 | if (val) |
| 4690 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
| 4691 | else |
| 4692 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
| 4693 | return 0; |
| 4694 | } |
| 4695 | |
| 4696 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 4697 | struct cftype *cft) |
| 4698 | { |
| 4699 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
| 4700 | } |
| 4701 | |
| 4702 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 4703 | struct cftype *cft, u64 val) |
| 4704 | { |
| 4705 | if (val) |
| 4706 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
| 4707 | else |
| 4708 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
| 4709 | return 0; |
| 4710 | } |
| 4711 | |
| 4712 | /* cgroup core interface files for the default hierarchy */ |
| 4713 | static struct cftype cgroup_dfl_base_files[] = { |
| 4714 | { |
| 4715 | .name = "cgroup.procs", |
| 4716 | .file_offset = offsetof(struct cgroup, procs_file), |
| 4717 | .seq_start = cgroup_pidlist_start, |
| 4718 | .seq_next = cgroup_pidlist_next, |
| 4719 | .seq_stop = cgroup_pidlist_stop, |
| 4720 | .seq_show = cgroup_pidlist_show, |
| 4721 | .private = CGROUP_FILE_PROCS, |
| 4722 | .write = cgroup_procs_write, |
| 4723 | }, |
| 4724 | { |
| 4725 | .name = "cgroup.controllers", |
| 4726 | .seq_show = cgroup_controllers_show, |
| 4727 | }, |
| 4728 | { |
| 4729 | .name = "cgroup.subtree_control", |
| 4730 | .seq_show = cgroup_subtree_control_show, |
| 4731 | .write = cgroup_subtree_control_write, |
| 4732 | }, |
| 4733 | { |
| 4734 | .name = "cgroup.events", |
| 4735 | .flags = CFTYPE_NOT_ON_ROOT, |
| 4736 | .file_offset = offsetof(struct cgroup, events_file), |
| 4737 | .seq_show = cgroup_events_show, |
| 4738 | }, |
| 4739 | { } /* terminate */ |
| 4740 | }; |
| 4741 | |
| 4742 | /* cgroup core interface files for the legacy hierarchies */ |
| 4743 | static struct cftype cgroup_legacy_base_files[] = { |
| 4744 | { |
| 4745 | .name = "cgroup.procs", |
| 4746 | .seq_start = cgroup_pidlist_start, |
| 4747 | .seq_next = cgroup_pidlist_next, |
| 4748 | .seq_stop = cgroup_pidlist_stop, |
| 4749 | .seq_show = cgroup_pidlist_show, |
| 4750 | .private = CGROUP_FILE_PROCS, |
| 4751 | .write = cgroup_procs_write, |
| 4752 | }, |
| 4753 | { |
| 4754 | .name = "cgroup.clone_children", |
| 4755 | .read_u64 = cgroup_clone_children_read, |
| 4756 | .write_u64 = cgroup_clone_children_write, |
| 4757 | }, |
| 4758 | { |
| 4759 | .name = "cgroup.sane_behavior", |
| 4760 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4761 | .seq_show = cgroup_sane_behavior_show, |
| 4762 | }, |
| 4763 | { |
| 4764 | .name = "tasks", |
| 4765 | .seq_start = cgroup_pidlist_start, |
| 4766 | .seq_next = cgroup_pidlist_next, |
| 4767 | .seq_stop = cgroup_pidlist_stop, |
| 4768 | .seq_show = cgroup_pidlist_show, |
| 4769 | .private = CGROUP_FILE_TASKS, |
| 4770 | .write = cgroup_tasks_write, |
| 4771 | }, |
| 4772 | { |
| 4773 | .name = "notify_on_release", |
| 4774 | .read_u64 = cgroup_read_notify_on_release, |
| 4775 | .write_u64 = cgroup_write_notify_on_release, |
| 4776 | }, |
| 4777 | { |
| 4778 | .name = "release_agent", |
| 4779 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4780 | .seq_show = cgroup_release_agent_show, |
| 4781 | .write = cgroup_release_agent_write, |
| 4782 | .max_write_len = PATH_MAX - 1, |
| 4783 | }, |
| 4784 | { } /* terminate */ |
| 4785 | }; |
| 4786 | |
| 4787 | /* |
| 4788 | * css destruction is four-stage process. |
| 4789 | * |
| 4790 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 4791 | * Implemented in kill_css(). |
| 4792 | * |
| 4793 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
| 4794 | * and thus css_tryget_online() is guaranteed to fail, the css can be |
| 4795 | * offlined by invoking offline_css(). After offlining, the base ref is |
| 4796 | * put. Implemented in css_killed_work_fn(). |
| 4797 | * |
| 4798 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 4799 | * accessors are inside RCU read sections. css_release() schedules the |
| 4800 | * RCU callback. |
| 4801 | * |
| 4802 | * 4. After the grace period, the css can be freed. Implemented in |
| 4803 | * css_free_work_fn(). |
| 4804 | * |
| 4805 | * It is actually hairier because both step 2 and 4 require process context |
| 4806 | * and thus involve punting to css->destroy_work adding two additional |
| 4807 | * steps to the already complex sequence. |
| 4808 | */ |
| 4809 | static void css_free_work_fn(struct work_struct *work) |
| 4810 | { |
| 4811 | struct cgroup_subsys_state *css = |
| 4812 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 4813 | struct cgroup_subsys *ss = css->ss; |
| 4814 | struct cgroup *cgrp = css->cgroup; |
| 4815 | |
| 4816 | percpu_ref_exit(&css->refcnt); |
| 4817 | |
| 4818 | if (ss) { |
| 4819 | /* css free path */ |
| 4820 | struct cgroup_subsys_state *parent = css->parent; |
| 4821 | int id = css->id; |
| 4822 | |
| 4823 | ss->css_free(css); |
| 4824 | cgroup_idr_remove(&ss->css_idr, id); |
| 4825 | cgroup_put(cgrp); |
| 4826 | |
| 4827 | if (parent) |
| 4828 | css_put(parent); |
| 4829 | } else { |
| 4830 | /* cgroup free path */ |
| 4831 | atomic_dec(&cgrp->root->nr_cgrps); |
| 4832 | cgroup_pidlist_destroy_all(cgrp); |
| 4833 | cancel_work_sync(&cgrp->release_agent_work); |
| 4834 | |
| 4835 | if (cgroup_parent(cgrp)) { |
| 4836 | /* |
| 4837 | * We get a ref to the parent, and put the ref when |
| 4838 | * this cgroup is being freed, so it's guaranteed |
| 4839 | * that the parent won't be destroyed before its |
| 4840 | * children. |
| 4841 | */ |
| 4842 | cgroup_put(cgroup_parent(cgrp)); |
| 4843 | kernfs_put(cgrp->kn); |
| 4844 | kfree(cgrp); |
| 4845 | } else { |
| 4846 | /* |
| 4847 | * This is root cgroup's refcnt reaching zero, |
| 4848 | * which indicates that the root should be |
| 4849 | * released. |
| 4850 | */ |
| 4851 | cgroup_destroy_root(cgrp->root); |
| 4852 | } |
| 4853 | } |
| 4854 | } |
| 4855 | |
| 4856 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 4857 | { |
| 4858 | struct cgroup_subsys_state *css = |
| 4859 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 4860 | |
| 4861 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
| 4862 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
| 4863 | } |
| 4864 | |
| 4865 | static void css_release_work_fn(struct work_struct *work) |
| 4866 | { |
| 4867 | struct cgroup_subsys_state *css = |
| 4868 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 4869 | struct cgroup_subsys *ss = css->ss; |
| 4870 | struct cgroup *cgrp = css->cgroup; |
| 4871 | |
| 4872 | mutex_lock(&cgroup_mutex); |
| 4873 | |
| 4874 | css->flags |= CSS_RELEASED; |
| 4875 | list_del_rcu(&css->sibling); |
| 4876 | |
| 4877 | if (ss) { |
| 4878 | /* css release path */ |
| 4879 | cgroup_idr_replace(&ss->css_idr, NULL, css->id); |
| 4880 | if (ss->css_released) |
| 4881 | ss->css_released(css); |
| 4882 | } else { |
| 4883 | /* cgroup release path */ |
| 4884 | cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 4885 | cgrp->id = -1; |
| 4886 | |
| 4887 | /* |
| 4888 | * There are two control paths which try to determine |
| 4889 | * cgroup from dentry without going through kernfs - |
| 4890 | * cgroupstats_build() and css_tryget_online_from_dir(). |
| 4891 | * Those are supported by RCU protecting clearing of |
| 4892 | * cgrp->kn->priv backpointer. |
| 4893 | */ |
| 4894 | if (cgrp->kn) |
| 4895 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, |
| 4896 | NULL); |
| 4897 | } |
| 4898 | |
| 4899 | mutex_unlock(&cgroup_mutex); |
| 4900 | |
| 4901 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
| 4902 | } |
| 4903 | |
| 4904 | static void css_release(struct percpu_ref *ref) |
| 4905 | { |
| 4906 | struct cgroup_subsys_state *css = |
| 4907 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4908 | |
| 4909 | INIT_WORK(&css->destroy_work, css_release_work_fn); |
| 4910 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
| 4911 | } |
| 4912 | |
| 4913 | static void init_and_link_css(struct cgroup_subsys_state *css, |
| 4914 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
| 4915 | { |
| 4916 | lockdep_assert_held(&cgroup_mutex); |
| 4917 | |
| 4918 | cgroup_get(cgrp); |
| 4919 | |
| 4920 | memset(css, 0, sizeof(*css)); |
| 4921 | css->cgroup = cgrp; |
| 4922 | css->ss = ss; |
| 4923 | INIT_LIST_HEAD(&css->sibling); |
| 4924 | INIT_LIST_HEAD(&css->children); |
| 4925 | css->serial_nr = css_serial_nr_next++; |
| 4926 | atomic_set(&css->online_cnt, 0); |
| 4927 | |
| 4928 | if (cgroup_parent(cgrp)) { |
| 4929 | css->parent = cgroup_css(cgroup_parent(cgrp), ss); |
| 4930 | css_get(css->parent); |
| 4931 | } |
| 4932 | |
| 4933 | BUG_ON(cgroup_css(cgrp, ss)); |
| 4934 | } |
| 4935 | |
| 4936 | /* invoke ->css_online() on a new CSS and mark it online if successful */ |
| 4937 | static int online_css(struct cgroup_subsys_state *css) |
| 4938 | { |
| 4939 | struct cgroup_subsys *ss = css->ss; |
| 4940 | int ret = 0; |
| 4941 | |
| 4942 | lockdep_assert_held(&cgroup_mutex); |
| 4943 | |
| 4944 | if (ss->css_online) |
| 4945 | ret = ss->css_online(css); |
| 4946 | if (!ret) { |
| 4947 | css->flags |= CSS_ONLINE; |
| 4948 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
| 4949 | |
| 4950 | atomic_inc(&css->online_cnt); |
| 4951 | if (css->parent) |
| 4952 | atomic_inc(&css->parent->online_cnt); |
| 4953 | } |
| 4954 | return ret; |
| 4955 | } |
| 4956 | |
| 4957 | /* if the CSS is online, invoke ->css_offline() on it and mark it offline */ |
| 4958 | static void offline_css(struct cgroup_subsys_state *css) |
| 4959 | { |
| 4960 | struct cgroup_subsys *ss = css->ss; |
| 4961 | |
| 4962 | lockdep_assert_held(&cgroup_mutex); |
| 4963 | |
| 4964 | if (!(css->flags & CSS_ONLINE)) |
| 4965 | return; |
| 4966 | |
| 4967 | if (ss->css_reset) |
| 4968 | ss->css_reset(css); |
| 4969 | |
| 4970 | if (ss->css_offline) |
| 4971 | ss->css_offline(css); |
| 4972 | |
| 4973 | css->flags &= ~CSS_ONLINE; |
| 4974 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); |
| 4975 | |
| 4976 | wake_up_all(&css->cgroup->offline_waitq); |
| 4977 | } |
| 4978 | |
| 4979 | /** |
| 4980 | * css_create - create a cgroup_subsys_state |
| 4981 | * @cgrp: the cgroup new css will be associated with |
| 4982 | * @ss: the subsys of new css |
| 4983 | * |
| 4984 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
| 4985 | * css is online and installed in @cgrp. This function doesn't create the |
| 4986 | * interface files. Returns 0 on success, -errno on failure. |
| 4987 | */ |
| 4988 | static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, |
| 4989 | struct cgroup_subsys *ss) |
| 4990 | { |
| 4991 | struct cgroup *parent = cgroup_parent(cgrp); |
| 4992 | struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss); |
| 4993 | struct cgroup_subsys_state *css; |
| 4994 | int err; |
| 4995 | |
| 4996 | lockdep_assert_held(&cgroup_mutex); |
| 4997 | |
| 4998 | css = ss->css_alloc(parent_css); |
| 4999 | if (IS_ERR(css)) |
| 5000 | return css; |
| 5001 | |
| 5002 | init_and_link_css(css, ss, cgrp); |
| 5003 | |
| 5004 | err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL); |
| 5005 | if (err) |
| 5006 | goto err_free_css; |
| 5007 | |
| 5008 | err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL); |
| 5009 | if (err < 0) |
| 5010 | goto err_free_percpu_ref; |
| 5011 | css->id = err; |
| 5012 | |
| 5013 | /* @css is ready to be brought online now, make it visible */ |
| 5014 | list_add_tail_rcu(&css->sibling, &parent_css->children); |
| 5015 | cgroup_idr_replace(&ss->css_idr, css, css->id); |
| 5016 | |
| 5017 | err = online_css(css); |
| 5018 | if (err) |
| 5019 | goto err_list_del; |
| 5020 | |
| 5021 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 5022 | cgroup_parent(parent)) { |
| 5023 | pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n", |
| 5024 | current->comm, current->pid, ss->name); |
| 5025 | if (!strcmp(ss->name, "memory")) |
| 5026 | pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n"); |
| 5027 | ss->warned_broken_hierarchy = true; |
| 5028 | } |
| 5029 | |
| 5030 | return css; |
| 5031 | |
| 5032 | err_list_del: |
| 5033 | list_del_rcu(&css->sibling); |
| 5034 | cgroup_idr_remove(&ss->css_idr, css->id); |
| 5035 | err_free_percpu_ref: |
| 5036 | percpu_ref_exit(&css->refcnt); |
| 5037 | err_free_css: |
| 5038 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
| 5039 | return ERR_PTR(err); |
| 5040 | } |
| 5041 | |
| 5042 | static struct cgroup *cgroup_create(struct cgroup *parent) |
| 5043 | { |
| 5044 | struct cgroup_root *root = parent->root; |
| 5045 | struct cgroup *cgrp, *tcgrp; |
| 5046 | int level = parent->level + 1; |
| 5047 | int ret; |
| 5048 | |
| 5049 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
| 5050 | cgrp = kzalloc(sizeof(*cgrp) + |
| 5051 | sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL); |
| 5052 | if (!cgrp) |
| 5053 | return ERR_PTR(-ENOMEM); |
| 5054 | |
| 5055 | ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL); |
| 5056 | if (ret) |
| 5057 | goto out_free_cgrp; |
| 5058 | |
| 5059 | /* |
| 5060 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 5061 | * a half-baked cgroup. |
| 5062 | */ |
| 5063 | cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL); |
| 5064 | if (cgrp->id < 0) { |
| 5065 | ret = -ENOMEM; |
| 5066 | goto out_cancel_ref; |
| 5067 | } |
| 5068 | |
| 5069 | init_cgroup_housekeeping(cgrp); |
| 5070 | |
| 5071 | cgrp->self.parent = &parent->self; |
| 5072 | cgrp->root = root; |
| 5073 | cgrp->level = level; |
| 5074 | |
| 5075 | for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) |
| 5076 | cgrp->ancestor_ids[tcgrp->level] = tcgrp->id; |
| 5077 | |
| 5078 | if (notify_on_release(parent)) |
| 5079 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 5080 | |
| 5081 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 5082 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
| 5083 | |
| 5084 | cgrp->self.serial_nr = css_serial_nr_next++; |
| 5085 | |
| 5086 | /* allocation complete, commit to creation */ |
| 5087 | list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children); |
| 5088 | atomic_inc(&root->nr_cgrps); |
| 5089 | cgroup_get(parent); |
| 5090 | |
| 5091 | /* |
| 5092 | * @cgrp is now fully operational. If something fails after this |
| 5093 | * point, it'll be released via the normal destruction path. |
| 5094 | */ |
| 5095 | cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 5096 | |
| 5097 | /* |
| 5098 | * On the default hierarchy, a child doesn't automatically inherit |
| 5099 | * subtree_control from the parent. Each is configured manually. |
| 5100 | */ |
| 5101 | if (!cgroup_on_dfl(cgrp)) |
| 5102 | cgrp->subtree_control = cgroup_control(cgrp); |
| 5103 | |
| 5104 | cgroup_propagate_control(cgrp); |
| 5105 | |
| 5106 | /* @cgrp doesn't have dir yet so the following will only create csses */ |
| 5107 | ret = cgroup_apply_control_enable(cgrp); |
| 5108 | if (ret) |
| 5109 | goto out_destroy; |
| 5110 | |
| 5111 | return cgrp; |
| 5112 | |
| 5113 | out_cancel_ref: |
| 5114 | percpu_ref_exit(&cgrp->self.refcnt); |
| 5115 | out_free_cgrp: |
| 5116 | kfree(cgrp); |
| 5117 | return ERR_PTR(ret); |
| 5118 | out_destroy: |
| 5119 | cgroup_destroy_locked(cgrp); |
| 5120 | return ERR_PTR(ret); |
| 5121 | } |
| 5122 | |
| 5123 | static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, |
| 5124 | umode_t mode) |
| 5125 | { |
| 5126 | struct cgroup *parent, *cgrp; |
| 5127 | struct kernfs_node *kn; |
| 5128 | int ret; |
| 5129 | |
| 5130 | /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */ |
| 5131 | if (strchr(name, '\n')) |
| 5132 | return -EINVAL; |
| 5133 | |
| 5134 | parent = cgroup_kn_lock_live(parent_kn, false); |
| 5135 | if (!parent) |
| 5136 | return -ENODEV; |
| 5137 | |
| 5138 | cgrp = cgroup_create(parent); |
| 5139 | if (IS_ERR(cgrp)) { |
| 5140 | ret = PTR_ERR(cgrp); |
| 5141 | goto out_unlock; |
| 5142 | } |
| 5143 | |
| 5144 | /* create the directory */ |
| 5145 | kn = kernfs_create_dir(parent->kn, name, mode, cgrp); |
| 5146 | if (IS_ERR(kn)) { |
| 5147 | ret = PTR_ERR(kn); |
| 5148 | goto out_destroy; |
| 5149 | } |
| 5150 | cgrp->kn = kn; |
| 5151 | |
| 5152 | /* |
| 5153 | * This extra ref will be put in cgroup_free_fn() and guarantees |
| 5154 | * that @cgrp->kn is always accessible. |
| 5155 | */ |
| 5156 | kernfs_get(kn); |
| 5157 | |
| 5158 | ret = cgroup_kn_set_ugid(kn); |
| 5159 | if (ret) |
| 5160 | goto out_destroy; |
| 5161 | |
| 5162 | ret = css_populate_dir(&cgrp->self, NULL); |
| 5163 | if (ret) |
| 5164 | goto out_destroy; |
| 5165 | |
| 5166 | ret = cgroup_apply_control_enable(cgrp); |
| 5167 | if (ret) |
| 5168 | goto out_destroy; |
| 5169 | |
| 5170 | /* let's create and online css's */ |
| 5171 | kernfs_activate(kn); |
| 5172 | |
| 5173 | ret = 0; |
| 5174 | goto out_unlock; |
| 5175 | |
| 5176 | out_destroy: |
| 5177 | cgroup_destroy_locked(cgrp); |
| 5178 | out_unlock: |
| 5179 | cgroup_kn_unlock(parent_kn); |
| 5180 | return ret; |
| 5181 | } |
| 5182 | |
| 5183 | /* |
| 5184 | * This is called when the refcnt of a css is confirmed to be killed. |
| 5185 | * css_tryget_online() is now guaranteed to fail. Tell the subsystem to |
| 5186 | * initate destruction and put the css ref from kill_css(). |
| 5187 | */ |
| 5188 | static void css_killed_work_fn(struct work_struct *work) |
| 5189 | { |
| 5190 | struct cgroup_subsys_state *css = |
| 5191 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 5192 | |
| 5193 | mutex_lock(&cgroup_mutex); |
| 5194 | |
| 5195 | do { |
| 5196 | offline_css(css); |
| 5197 | css_put(css); |
| 5198 | /* @css can't go away while we're holding cgroup_mutex */ |
| 5199 | css = css->parent; |
| 5200 | } while (css && atomic_dec_and_test(&css->online_cnt)); |
| 5201 | |
| 5202 | mutex_unlock(&cgroup_mutex); |
| 5203 | } |
| 5204 | |
| 5205 | /* css kill confirmation processing requires process context, bounce */ |
| 5206 | static void css_killed_ref_fn(struct percpu_ref *ref) |
| 5207 | { |
| 5208 | struct cgroup_subsys_state *css = |
| 5209 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 5210 | |
| 5211 | if (atomic_dec_and_test(&css->online_cnt)) { |
| 5212 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
| 5213 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
| 5214 | } |
| 5215 | } |
| 5216 | |
| 5217 | /** |
| 5218 | * kill_css - destroy a css |
| 5219 | * @css: css to destroy |
| 5220 | * |
| 5221 | * This function initiates destruction of @css by removing cgroup interface |
| 5222 | * files and putting its base reference. ->css_offline() will be invoked |
| 5223 | * asynchronously once css_tryget_online() is guaranteed to fail and when |
| 5224 | * the reference count reaches zero, @css will be released. |
| 5225 | */ |
| 5226 | static void kill_css(struct cgroup_subsys_state *css) |
| 5227 | { |
| 5228 | lockdep_assert_held(&cgroup_mutex); |
| 5229 | |
| 5230 | /* |
| 5231 | * This must happen before css is disassociated with its cgroup. |
| 5232 | * See seq_css() for details. |
| 5233 | */ |
| 5234 | css_clear_dir(css, NULL); |
| 5235 | |
| 5236 | /* |
| 5237 | * Killing would put the base ref, but we need to keep it alive |
| 5238 | * until after ->css_offline(). |
| 5239 | */ |
| 5240 | css_get(css); |
| 5241 | |
| 5242 | /* |
| 5243 | * cgroup core guarantees that, by the time ->css_offline() is |
| 5244 | * invoked, no new css reference will be given out via |
| 5245 | * css_tryget_online(). We can't simply call percpu_ref_kill() and |
| 5246 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 5247 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 5248 | * |
| 5249 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 5250 | * css is confirmed to be seen as killed on all CPUs. |
| 5251 | */ |
| 5252 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
| 5253 | } |
| 5254 | |
| 5255 | /** |
| 5256 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 5257 | * @cgrp: cgroup to be destroyed |
| 5258 | * |
| 5259 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 5260 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 5261 | * guarantee that css_tryget_online() won't succeed by the time |
| 5262 | * ->css_offline() is invoked. To satisfy all the requirements, |
| 5263 | * destruction is implemented in the following two steps. |
| 5264 | * |
| 5265 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 5266 | * userland visible parts and start killing the percpu refcnts of |
| 5267 | * css's. Set up so that the next stage will be kicked off once all |
| 5268 | * the percpu refcnts are confirmed to be killed. |
| 5269 | * |
| 5270 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 5271 | * rest of destruction. Once all cgroup references are gone, the |
| 5272 | * cgroup is RCU-freed. |
| 5273 | * |
| 5274 | * This function implements s1. After this step, @cgrp is gone as far as |
| 5275 | * the userland is concerned and a new cgroup with the same name may be |
| 5276 | * created. As cgroup doesn't care about the names internally, this |
| 5277 | * doesn't cause any problem. |
| 5278 | */ |
| 5279 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 5280 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
| 5281 | { |
| 5282 | struct cgroup_subsys_state *css; |
| 5283 | int ssid; |
| 5284 | |
| 5285 | lockdep_assert_held(&cgroup_mutex); |
| 5286 | |
| 5287 | /* |
| 5288 | * Only migration can raise populated from zero and we're already |
| 5289 | * holding cgroup_mutex. |
| 5290 | */ |
| 5291 | if (cgroup_is_populated(cgrp)) |
| 5292 | return -EBUSY; |
| 5293 | |
| 5294 | /* |
| 5295 | * Make sure there's no live children. We can't test emptiness of |
| 5296 | * ->self.children as dead children linger on it while being |
| 5297 | * drained; otherwise, "rmdir parent/child parent" may fail. |
| 5298 | */ |
| 5299 | if (css_has_online_children(&cgrp->self)) |
| 5300 | return -EBUSY; |
| 5301 | |
| 5302 | /* |
| 5303 | * Mark @cgrp dead. This prevents further task migration and child |
| 5304 | * creation by disabling cgroup_lock_live_group(). |
| 5305 | */ |
| 5306 | cgrp->self.flags &= ~CSS_ONLINE; |
| 5307 | |
| 5308 | /* initiate massacre of all css's */ |
| 5309 | for_each_css(css, ssid, cgrp) |
| 5310 | kill_css(css); |
| 5311 | |
| 5312 | /* |
| 5313 | * Remove @cgrp directory along with the base files. @cgrp has an |
| 5314 | * extra ref on its kn. |
| 5315 | */ |
| 5316 | kernfs_remove(cgrp->kn); |
| 5317 | |
| 5318 | check_for_release(cgroup_parent(cgrp)); |
| 5319 | |
| 5320 | /* put the base reference */ |
| 5321 | percpu_ref_kill(&cgrp->self.refcnt); |
| 5322 | |
| 5323 | return 0; |
| 5324 | }; |
| 5325 | |
| 5326 | static int cgroup_rmdir(struct kernfs_node *kn) |
| 5327 | { |
| 5328 | struct cgroup *cgrp; |
| 5329 | int ret = 0; |
| 5330 | |
| 5331 | cgrp = cgroup_kn_lock_live(kn, false); |
| 5332 | if (!cgrp) |
| 5333 | return 0; |
| 5334 | |
| 5335 | ret = cgroup_destroy_locked(cgrp); |
| 5336 | |
| 5337 | cgroup_kn_unlock(kn); |
| 5338 | return ret; |
| 5339 | } |
| 5340 | |
| 5341 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
| 5342 | .remount_fs = cgroup_remount, |
| 5343 | .show_options = cgroup_show_options, |
| 5344 | .mkdir = cgroup_mkdir, |
| 5345 | .rmdir = cgroup_rmdir, |
| 5346 | .rename = cgroup_rename, |
| 5347 | }; |
| 5348 | |
| 5349 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) |
| 5350 | { |
| 5351 | struct cgroup_subsys_state *css; |
| 5352 | |
| 5353 | pr_debug("Initializing cgroup subsys %s\n", ss->name); |
| 5354 | |
| 5355 | mutex_lock(&cgroup_mutex); |
| 5356 | |
| 5357 | idr_init(&ss->css_idr); |
| 5358 | INIT_LIST_HEAD(&ss->cfts); |
| 5359 | |
| 5360 | /* Create the root cgroup state for this subsystem */ |
| 5361 | ss->root = &cgrp_dfl_root; |
| 5362 | css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss)); |
| 5363 | /* We don't handle early failures gracefully */ |
| 5364 | BUG_ON(IS_ERR(css)); |
| 5365 | init_and_link_css(css, ss, &cgrp_dfl_root.cgrp); |
| 5366 | |
| 5367 | /* |
| 5368 | * Root csses are never destroyed and we can't initialize |
| 5369 | * percpu_ref during early init. Disable refcnting. |
| 5370 | */ |
| 5371 | css->flags |= CSS_NO_REF; |
| 5372 | |
| 5373 | if (early) { |
| 5374 | /* allocation can't be done safely during early init */ |
| 5375 | css->id = 1; |
| 5376 | } else { |
| 5377 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL); |
| 5378 | BUG_ON(css->id < 0); |
| 5379 | } |
| 5380 | |
| 5381 | /* Update the init_css_set to contain a subsys |
| 5382 | * pointer to this state - since the subsystem is |
| 5383 | * newly registered, all tasks and hence the |
| 5384 | * init_css_set is in the subsystem's root cgroup. */ |
| 5385 | init_css_set.subsys[ss->id] = css; |
| 5386 | |
| 5387 | have_fork_callback |= (bool)ss->fork << ss->id; |
| 5388 | have_exit_callback |= (bool)ss->exit << ss->id; |
| 5389 | have_free_callback |= (bool)ss->free << ss->id; |
| 5390 | have_canfork_callback |= (bool)ss->can_fork << ss->id; |
| 5391 | |
| 5392 | /* At system boot, before all subsystems have been |
| 5393 | * registered, no tasks have been forked, so we don't |
| 5394 | * need to invoke fork callbacks here. */ |
| 5395 | BUG_ON(!list_empty(&init_task.tasks)); |
| 5396 | |
| 5397 | BUG_ON(online_css(css)); |
| 5398 | |
| 5399 | mutex_unlock(&cgroup_mutex); |
| 5400 | } |
| 5401 | |
| 5402 | /** |
| 5403 | * cgroup_init_early - cgroup initialization at system boot |
| 5404 | * |
| 5405 | * Initialize cgroups at system boot, and initialize any |
| 5406 | * subsystems that request early init. |
| 5407 | */ |
| 5408 | int __init cgroup_init_early(void) |
| 5409 | { |
| 5410 | static struct cgroup_sb_opts __initdata opts; |
| 5411 | struct cgroup_subsys *ss; |
| 5412 | int i; |
| 5413 | |
| 5414 | init_cgroup_root(&cgrp_dfl_root, &opts); |
| 5415 | cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF; |
| 5416 | |
| 5417 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
| 5418 | |
| 5419 | for_each_subsys(ss, i) { |
| 5420 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
| 5421 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n", |
| 5422 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
| 5423 | ss->id, ss->name); |
| 5424 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 5425 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
| 5426 | |
| 5427 | ss->id = i; |
| 5428 | ss->name = cgroup_subsys_name[i]; |
| 5429 | if (!ss->legacy_name) |
| 5430 | ss->legacy_name = cgroup_subsys_name[i]; |
| 5431 | |
| 5432 | if (ss->early_init) |
| 5433 | cgroup_init_subsys(ss, true); |
| 5434 | } |
| 5435 | return 0; |
| 5436 | } |
| 5437 | |
| 5438 | static u16 cgroup_disable_mask __initdata; |
| 5439 | |
| 5440 | /** |
| 5441 | * cgroup_init - cgroup initialization |
| 5442 | * |
| 5443 | * Register cgroup filesystem and /proc file, and initialize |
| 5444 | * any subsystems that didn't request early init. |
| 5445 | */ |
| 5446 | int __init cgroup_init(void) |
| 5447 | { |
| 5448 | struct cgroup_subsys *ss; |
| 5449 | int ssid; |
| 5450 | |
| 5451 | BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16); |
| 5452 | BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem)); |
| 5453 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files)); |
| 5454 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files)); |
| 5455 | |
| 5456 | mutex_lock(&cgroup_mutex); |
| 5457 | |
| 5458 | /* |
| 5459 | * Add init_css_set to the hash table so that dfl_root can link to |
| 5460 | * it during init. |
| 5461 | */ |
| 5462 | hash_add(css_set_table, &init_css_set.hlist, |
| 5463 | css_set_hash(init_css_set.subsys)); |
| 5464 | |
| 5465 | BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0)); |
| 5466 | |
| 5467 | mutex_unlock(&cgroup_mutex); |
| 5468 | |
| 5469 | for_each_subsys(ss, ssid) { |
| 5470 | if (ss->early_init) { |
| 5471 | struct cgroup_subsys_state *css = |
| 5472 | init_css_set.subsys[ss->id]; |
| 5473 | |
| 5474 | css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, |
| 5475 | GFP_KERNEL); |
| 5476 | BUG_ON(css->id < 0); |
| 5477 | } else { |
| 5478 | cgroup_init_subsys(ss, false); |
| 5479 | } |
| 5480 | |
| 5481 | list_add_tail(&init_css_set.e_cset_node[ssid], |
| 5482 | &cgrp_dfl_root.cgrp.e_csets[ssid]); |
| 5483 | |
| 5484 | /* |
| 5485 | * Setting dfl_root subsys_mask needs to consider the |
| 5486 | * disabled flag and cftype registration needs kmalloc, |
| 5487 | * both of which aren't available during early_init. |
| 5488 | */ |
| 5489 | if (cgroup_disable_mask & (1 << ssid)) { |
| 5490 | static_branch_disable(cgroup_subsys_enabled_key[ssid]); |
| 5491 | printk(KERN_INFO "Disabling %s control group subsystem\n", |
| 5492 | ss->name); |
| 5493 | continue; |
| 5494 | } |
| 5495 | |
| 5496 | if (cgroup_ssid_no_v1(ssid)) |
| 5497 | printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n", |
| 5498 | ss->name); |
| 5499 | |
| 5500 | cgrp_dfl_root.subsys_mask |= 1 << ss->id; |
| 5501 | |
| 5502 | if (!ss->dfl_cftypes) |
| 5503 | cgrp_dfl_inhibit_ss_mask |= 1 << ss->id; |
| 5504 | |
| 5505 | if (ss->dfl_cftypes == ss->legacy_cftypes) { |
| 5506 | WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes)); |
| 5507 | } else { |
| 5508 | WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes)); |
| 5509 | WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes)); |
| 5510 | } |
| 5511 | |
| 5512 | if (ss->bind) |
| 5513 | ss->bind(init_css_set.subsys[ssid]); |
| 5514 | } |
| 5515 | |
| 5516 | /* init_css_set.subsys[] has been updated, re-hash */ |
| 5517 | hash_del(&init_css_set.hlist); |
| 5518 | hash_add(css_set_table, &init_css_set.hlist, |
| 5519 | css_set_hash(init_css_set.subsys)); |
| 5520 | |
| 5521 | WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup")); |
| 5522 | WARN_ON(register_filesystem(&cgroup_fs_type)); |
| 5523 | WARN_ON(register_filesystem(&cgroup2_fs_type)); |
| 5524 | WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations)); |
| 5525 | |
| 5526 | return 0; |
| 5527 | } |
| 5528 | |
| 5529 | static int __init cgroup_wq_init(void) |
| 5530 | { |
| 5531 | /* |
| 5532 | * There isn't much point in executing destruction path in |
| 5533 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
| 5534 | * Use 1 for @max_active. |
| 5535 | * |
| 5536 | * We would prefer to do this in cgroup_init() above, but that |
| 5537 | * is called before init_workqueues(): so leave this until after. |
| 5538 | */ |
| 5539 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
| 5540 | BUG_ON(!cgroup_destroy_wq); |
| 5541 | |
| 5542 | /* |
| 5543 | * Used to destroy pidlists and separate to serve as flush domain. |
| 5544 | * Cap @max_active to 1 too. |
| 5545 | */ |
| 5546 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 5547 | 0, 1); |
| 5548 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 5549 | |
| 5550 | return 0; |
| 5551 | } |
| 5552 | core_initcall(cgroup_wq_init); |
| 5553 | |
| 5554 | /* |
| 5555 | * proc_cgroup_show() |
| 5556 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 5557 | * - Used for /proc/<pid>/cgroup. |
| 5558 | */ |
| 5559 | int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, |
| 5560 | struct pid *pid, struct task_struct *tsk) |
| 5561 | { |
| 5562 | char *buf, *path; |
| 5563 | int retval; |
| 5564 | struct cgroup_root *root; |
| 5565 | |
| 5566 | retval = -ENOMEM; |
| 5567 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5568 | if (!buf) |
| 5569 | goto out; |
| 5570 | |
| 5571 | mutex_lock(&cgroup_mutex); |
| 5572 | spin_lock_bh(&css_set_lock); |
| 5573 | |
| 5574 | for_each_root(root) { |
| 5575 | struct cgroup_subsys *ss; |
| 5576 | struct cgroup *cgrp; |
| 5577 | int ssid, count = 0; |
| 5578 | |
| 5579 | if (root == &cgrp_dfl_root && !cgrp_dfl_visible) |
| 5580 | continue; |
| 5581 | |
| 5582 | seq_printf(m, "%d:", root->hierarchy_id); |
| 5583 | if (root != &cgrp_dfl_root) |
| 5584 | for_each_subsys(ss, ssid) |
| 5585 | if (root->subsys_mask & (1 << ssid)) |
| 5586 | seq_printf(m, "%s%s", count++ ? "," : "", |
| 5587 | ss->legacy_name); |
| 5588 | if (strlen(root->name)) |
| 5589 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 5590 | root->name); |
| 5591 | seq_putc(m, ':'); |
| 5592 | |
| 5593 | cgrp = task_cgroup_from_root(tsk, root); |
| 5594 | |
| 5595 | /* |
| 5596 | * On traditional hierarchies, all zombie tasks show up as |
| 5597 | * belonging to the root cgroup. On the default hierarchy, |
| 5598 | * while a zombie doesn't show up in "cgroup.procs" and |
| 5599 | * thus can't be migrated, its /proc/PID/cgroup keeps |
| 5600 | * reporting the cgroup it belonged to before exiting. If |
| 5601 | * the cgroup is removed before the zombie is reaped, |
| 5602 | * " (deleted)" is appended to the cgroup path. |
| 5603 | */ |
| 5604 | if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) { |
| 5605 | path = cgroup_path(cgrp, buf, PATH_MAX); |
| 5606 | if (!path) { |
| 5607 | retval = -ENAMETOOLONG; |
| 5608 | goto out_unlock; |
| 5609 | } |
| 5610 | } else { |
| 5611 | path = "/"; |
| 5612 | } |
| 5613 | |
| 5614 | seq_puts(m, path); |
| 5615 | |
| 5616 | if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp)) |
| 5617 | seq_puts(m, " (deleted)\n"); |
| 5618 | else |
| 5619 | seq_putc(m, '\n'); |
| 5620 | } |
| 5621 | |
| 5622 | retval = 0; |
| 5623 | out_unlock: |
| 5624 | spin_unlock_bh(&css_set_lock); |
| 5625 | mutex_unlock(&cgroup_mutex); |
| 5626 | kfree(buf); |
| 5627 | out: |
| 5628 | return retval; |
| 5629 | } |
| 5630 | |
| 5631 | /* Display information about each subsystem and each hierarchy */ |
| 5632 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 5633 | { |
| 5634 | struct cgroup_subsys *ss; |
| 5635 | int i; |
| 5636 | |
| 5637 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
| 5638 | /* |
| 5639 | * ideally we don't want subsystems moving around while we do this. |
| 5640 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 5641 | * subsys/hierarchy state. |
| 5642 | */ |
| 5643 | mutex_lock(&cgroup_mutex); |
| 5644 | |
| 5645 | for_each_subsys(ss, i) |
| 5646 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 5647 | ss->legacy_name, ss->root->hierarchy_id, |
| 5648 | atomic_read(&ss->root->nr_cgrps), |
| 5649 | cgroup_ssid_enabled(i)); |
| 5650 | |
| 5651 | mutex_unlock(&cgroup_mutex); |
| 5652 | return 0; |
| 5653 | } |
| 5654 | |
| 5655 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 5656 | { |
| 5657 | return single_open(file, proc_cgroupstats_show, NULL); |
| 5658 | } |
| 5659 | |
| 5660 | static const struct file_operations proc_cgroupstats_operations = { |
| 5661 | .open = cgroupstats_open, |
| 5662 | .read = seq_read, |
| 5663 | .llseek = seq_lseek, |
| 5664 | .release = single_release, |
| 5665 | }; |
| 5666 | |
| 5667 | /** |
| 5668 | * cgroup_fork - initialize cgroup related fields during copy_process() |
| 5669 | * @child: pointer to task_struct of forking parent process. |
| 5670 | * |
| 5671 | * A task is associated with the init_css_set until cgroup_post_fork() |
| 5672 | * attaches it to the parent's css_set. Empty cg_list indicates that |
| 5673 | * @child isn't holding reference to its css_set. |
| 5674 | */ |
| 5675 | void cgroup_fork(struct task_struct *child) |
| 5676 | { |
| 5677 | RCU_INIT_POINTER(child->cgroups, &init_css_set); |
| 5678 | INIT_LIST_HEAD(&child->cg_list); |
| 5679 | } |
| 5680 | |
| 5681 | /** |
| 5682 | * cgroup_can_fork - called on a new task before the process is exposed |
| 5683 | * @child: the task in question. |
| 5684 | * |
| 5685 | * This calls the subsystem can_fork() callbacks. If the can_fork() callback |
| 5686 | * returns an error, the fork aborts with that error code. This allows for |
| 5687 | * a cgroup subsystem to conditionally allow or deny new forks. |
| 5688 | */ |
| 5689 | int cgroup_can_fork(struct task_struct *child) |
| 5690 | { |
| 5691 | struct cgroup_subsys *ss; |
| 5692 | int i, j, ret; |
| 5693 | |
| 5694 | do_each_subsys_mask(ss, i, have_canfork_callback) { |
| 5695 | ret = ss->can_fork(child); |
| 5696 | if (ret) |
| 5697 | goto out_revert; |
| 5698 | } while_each_subsys_mask(); |
| 5699 | |
| 5700 | return 0; |
| 5701 | |
| 5702 | out_revert: |
| 5703 | for_each_subsys(ss, j) { |
| 5704 | if (j >= i) |
| 5705 | break; |
| 5706 | if (ss->cancel_fork) |
| 5707 | ss->cancel_fork(child); |
| 5708 | } |
| 5709 | |
| 5710 | return ret; |
| 5711 | } |
| 5712 | |
| 5713 | /** |
| 5714 | * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork() |
| 5715 | * @child: the task in question |
| 5716 | * |
| 5717 | * This calls the cancel_fork() callbacks if a fork failed *after* |
| 5718 | * cgroup_can_fork() succeded. |
| 5719 | */ |
| 5720 | void cgroup_cancel_fork(struct task_struct *child) |
| 5721 | { |
| 5722 | struct cgroup_subsys *ss; |
| 5723 | int i; |
| 5724 | |
| 5725 | for_each_subsys(ss, i) |
| 5726 | if (ss->cancel_fork) |
| 5727 | ss->cancel_fork(child); |
| 5728 | } |
| 5729 | |
| 5730 | /** |
| 5731 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5732 | * @child: the task in question |
| 5733 | * |
| 5734 | * Adds the task to the list running through its css_set if necessary and |
| 5735 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5736 | * visible on the task list in case we race with the first call to |
| 5737 | * cgroup_task_iter_start() - to guarantee that the new task ends up on its |
| 5738 | * list. |
| 5739 | */ |
| 5740 | void cgroup_post_fork(struct task_struct *child) |
| 5741 | { |
| 5742 | struct cgroup_subsys *ss; |
| 5743 | int i; |
| 5744 | |
| 5745 | /* |
| 5746 | * This may race against cgroup_enable_task_cg_lists(). As that |
| 5747 | * function sets use_task_css_set_links before grabbing |
| 5748 | * tasklist_lock and we just went through tasklist_lock to add |
| 5749 | * @child, it's guaranteed that either we see the set |
| 5750 | * use_task_css_set_links or cgroup_enable_task_cg_lists() sees |
| 5751 | * @child during its iteration. |
| 5752 | * |
| 5753 | * If we won the race, @child is associated with %current's |
| 5754 | * css_set. Grabbing css_set_lock guarantees both that the |
| 5755 | * association is stable, and, on completion of the parent's |
| 5756 | * migration, @child is visible in the source of migration or |
| 5757 | * already in the destination cgroup. This guarantee is necessary |
| 5758 | * when implementing operations which need to migrate all tasks of |
| 5759 | * a cgroup to another. |
| 5760 | * |
| 5761 | * Note that if we lose to cgroup_enable_task_cg_lists(), @child |
| 5762 | * will remain in init_css_set. This is safe because all tasks are |
| 5763 | * in the init_css_set before cg_links is enabled and there's no |
| 5764 | * operation which transfers all tasks out of init_css_set. |
| 5765 | */ |
| 5766 | if (use_task_css_set_links) { |
| 5767 | struct css_set *cset; |
| 5768 | |
| 5769 | spin_lock_bh(&css_set_lock); |
| 5770 | cset = task_css_set(current); |
| 5771 | if (list_empty(&child->cg_list)) { |
| 5772 | get_css_set(cset); |
| 5773 | css_set_move_task(child, NULL, cset, false); |
| 5774 | } |
| 5775 | spin_unlock_bh(&css_set_lock); |
| 5776 | } |
| 5777 | |
| 5778 | /* |
| 5779 | * Call ss->fork(). This must happen after @child is linked on |
| 5780 | * css_set; otherwise, @child might change state between ->fork() |
| 5781 | * and addition to css_set. |
| 5782 | */ |
| 5783 | do_each_subsys_mask(ss, i, have_fork_callback) { |
| 5784 | ss->fork(child); |
| 5785 | } while_each_subsys_mask(); |
| 5786 | } |
| 5787 | |
| 5788 | /** |
| 5789 | * cgroup_exit - detach cgroup from exiting task |
| 5790 | * @tsk: pointer to task_struct of exiting process |
| 5791 | * |
| 5792 | * Description: Detach cgroup from @tsk and release it. |
| 5793 | * |
| 5794 | * Note that cgroups marked notify_on_release force every task in |
| 5795 | * them to take the global cgroup_mutex mutex when exiting. |
| 5796 | * This could impact scaling on very large systems. Be reluctant to |
| 5797 | * use notify_on_release cgroups where very high task exit scaling |
| 5798 | * is required on large systems. |
| 5799 | * |
| 5800 | * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We |
| 5801 | * call cgroup_exit() while the task is still competent to handle |
| 5802 | * notify_on_release(), then leave the task attached to the root cgroup in |
| 5803 | * each hierarchy for the remainder of its exit. No need to bother with |
| 5804 | * init_css_set refcnting. init_css_set never goes away and we can't race |
| 5805 | * with migration path - PF_EXITING is visible to migration path. |
| 5806 | */ |
| 5807 | void cgroup_exit(struct task_struct *tsk) |
| 5808 | { |
| 5809 | struct cgroup_subsys *ss; |
| 5810 | struct css_set *cset; |
| 5811 | int i; |
| 5812 | |
| 5813 | /* |
| 5814 | * Unlink from @tsk from its css_set. As migration path can't race |
| 5815 | * with us, we can check css_set and cg_list without synchronization. |
| 5816 | */ |
| 5817 | cset = task_css_set(tsk); |
| 5818 | |
| 5819 | if (!list_empty(&tsk->cg_list)) { |
| 5820 | spin_lock_bh(&css_set_lock); |
| 5821 | css_set_move_task(tsk, cset, NULL, false); |
| 5822 | spin_unlock_bh(&css_set_lock); |
| 5823 | } else { |
| 5824 | get_css_set(cset); |
| 5825 | } |
| 5826 | |
| 5827 | /* see cgroup_post_fork() for details */ |
| 5828 | do_each_subsys_mask(ss, i, have_exit_callback) { |
| 5829 | ss->exit(tsk); |
| 5830 | } while_each_subsys_mask(); |
| 5831 | } |
| 5832 | |
| 5833 | void cgroup_free(struct task_struct *task) |
| 5834 | { |
| 5835 | struct css_set *cset = task_css_set(task); |
| 5836 | struct cgroup_subsys *ss; |
| 5837 | int ssid; |
| 5838 | |
| 5839 | do_each_subsys_mask(ss, ssid, have_free_callback) { |
| 5840 | ss->free(task); |
| 5841 | } while_each_subsys_mask(); |
| 5842 | |
| 5843 | put_css_set(cset); |
| 5844 | } |
| 5845 | |
| 5846 | static void check_for_release(struct cgroup *cgrp) |
| 5847 | { |
| 5848 | if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) && |
| 5849 | !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp)) |
| 5850 | schedule_work(&cgrp->release_agent_work); |
| 5851 | } |
| 5852 | |
| 5853 | /* |
| 5854 | * Notify userspace when a cgroup is released, by running the |
| 5855 | * configured release agent with the name of the cgroup (path |
| 5856 | * relative to the root of cgroup file system) as the argument. |
| 5857 | * |
| 5858 | * Most likely, this user command will try to rmdir this cgroup. |
| 5859 | * |
| 5860 | * This races with the possibility that some other task will be |
| 5861 | * attached to this cgroup before it is removed, or that some other |
| 5862 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5863 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5864 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5865 | * to continue to serve a useful existence. Next time it's released, |
| 5866 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5867 | * |
| 5868 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5869 | * means only wait until the task is successfully execve()'d. The |
| 5870 | * separate release agent task is forked by call_usermodehelper(), |
| 5871 | * then control in this thread returns here, without waiting for the |
| 5872 | * release agent task. We don't bother to wait because the caller of |
| 5873 | * this routine has no use for the exit status of the release agent |
| 5874 | * task, so no sense holding our caller up for that. |
| 5875 | */ |
| 5876 | static void cgroup_release_agent(struct work_struct *work) |
| 5877 | { |
| 5878 | struct cgroup *cgrp = |
| 5879 | container_of(work, struct cgroup, release_agent_work); |
| 5880 | char *pathbuf = NULL, *agentbuf = NULL, *path; |
| 5881 | char *argv[3], *envp[3]; |
| 5882 | |
| 5883 | mutex_lock(&cgroup_mutex); |
| 5884 | |
| 5885 | pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 5886 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5887 | if (!pathbuf || !agentbuf) |
| 5888 | goto out; |
| 5889 | |
| 5890 | path = cgroup_path(cgrp, pathbuf, PATH_MAX); |
| 5891 | if (!path) |
| 5892 | goto out; |
| 5893 | |
| 5894 | argv[0] = agentbuf; |
| 5895 | argv[1] = path; |
| 5896 | argv[2] = NULL; |
| 5897 | |
| 5898 | /* minimal command environment */ |
| 5899 | envp[0] = "HOME=/"; |
| 5900 | envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5901 | envp[2] = NULL; |
| 5902 | |
| 5903 | mutex_unlock(&cgroup_mutex); |
| 5904 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
| 5905 | goto out_free; |
| 5906 | out: |
| 5907 | mutex_unlock(&cgroup_mutex); |
| 5908 | out_free: |
| 5909 | kfree(agentbuf); |
| 5910 | kfree(pathbuf); |
| 5911 | } |
| 5912 | |
| 5913 | static int __init cgroup_disable(char *str) |
| 5914 | { |
| 5915 | struct cgroup_subsys *ss; |
| 5916 | char *token; |
| 5917 | int i; |
| 5918 | |
| 5919 | while ((token = strsep(&str, ",")) != NULL) { |
| 5920 | if (!*token) |
| 5921 | continue; |
| 5922 | |
| 5923 | for_each_subsys(ss, i) { |
| 5924 | if (strcmp(token, ss->name) && |
| 5925 | strcmp(token, ss->legacy_name)) |
| 5926 | continue; |
| 5927 | cgroup_disable_mask |= 1 << i; |
| 5928 | } |
| 5929 | } |
| 5930 | return 1; |
| 5931 | } |
| 5932 | __setup("cgroup_disable=", cgroup_disable); |
| 5933 | |
| 5934 | static int __init cgroup_no_v1(char *str) |
| 5935 | { |
| 5936 | struct cgroup_subsys *ss; |
| 5937 | char *token; |
| 5938 | int i; |
| 5939 | |
| 5940 | while ((token = strsep(&str, ",")) != NULL) { |
| 5941 | if (!*token) |
| 5942 | continue; |
| 5943 | |
| 5944 | if (!strcmp(token, "all")) { |
| 5945 | cgroup_no_v1_mask = U16_MAX; |
| 5946 | break; |
| 5947 | } |
| 5948 | |
| 5949 | for_each_subsys(ss, i) { |
| 5950 | if (strcmp(token, ss->name) && |
| 5951 | strcmp(token, ss->legacy_name)) |
| 5952 | continue; |
| 5953 | |
| 5954 | cgroup_no_v1_mask |= 1 << i; |
| 5955 | } |
| 5956 | } |
| 5957 | return 1; |
| 5958 | } |
| 5959 | __setup("cgroup_no_v1=", cgroup_no_v1); |
| 5960 | |
| 5961 | /** |
| 5962 | * css_tryget_online_from_dir - get corresponding css from a cgroup dentry |
| 5963 | * @dentry: directory dentry of interest |
| 5964 | * @ss: subsystem of interest |
| 5965 | * |
| 5966 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 5967 | * to get the corresponding css and return it. If such css doesn't exist |
| 5968 | * or can't be pinned, an ERR_PTR value is returned. |
| 5969 | */ |
| 5970 | struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, |
| 5971 | struct cgroup_subsys *ss) |
| 5972 | { |
| 5973 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
| 5974 | struct file_system_type *s_type = dentry->d_sb->s_type; |
| 5975 | struct cgroup_subsys_state *css = NULL; |
| 5976 | struct cgroup *cgrp; |
| 5977 | |
| 5978 | /* is @dentry a cgroup dir? */ |
| 5979 | if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) || |
| 5980 | !kn || kernfs_type(kn) != KERNFS_DIR) |
| 5981 | return ERR_PTR(-EBADF); |
| 5982 | |
| 5983 | rcu_read_lock(); |
| 5984 | |
| 5985 | /* |
| 5986 | * This path doesn't originate from kernfs and @kn could already |
| 5987 | * have been or be removed at any point. @kn->priv is RCU |
| 5988 | * protected for this access. See css_release_work_fn() for details. |
| 5989 | */ |
| 5990 | cgrp = rcu_dereference(kn->priv); |
| 5991 | if (cgrp) |
| 5992 | css = cgroup_css(cgrp, ss); |
| 5993 | |
| 5994 | if (!css || !css_tryget_online(css)) |
| 5995 | css = ERR_PTR(-ENOENT); |
| 5996 | |
| 5997 | rcu_read_unlock(); |
| 5998 | return css; |
| 5999 | } |
| 6000 | |
| 6001 | /** |
| 6002 | * css_from_id - lookup css by id |
| 6003 | * @id: the cgroup id |
| 6004 | * @ss: cgroup subsys to be looked into |
| 6005 | * |
| 6006 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 6007 | * Should be called under rcu_read_lock(). |
| 6008 | */ |
| 6009 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 6010 | { |
| 6011 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 6012 | return id > 0 ? idr_find(&ss->css_idr, id) : NULL; |
| 6013 | } |
| 6014 | |
| 6015 | /** |
| 6016 | * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path |
| 6017 | * @path: path on the default hierarchy |
| 6018 | * |
| 6019 | * Find the cgroup at @path on the default hierarchy, increment its |
| 6020 | * reference count and return it. Returns pointer to the found cgroup on |
| 6021 | * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR) |
| 6022 | * if @path points to a non-directory. |
| 6023 | */ |
| 6024 | struct cgroup *cgroup_get_from_path(const char *path) |
| 6025 | { |
| 6026 | struct kernfs_node *kn; |
| 6027 | struct cgroup *cgrp; |
| 6028 | |
| 6029 | mutex_lock(&cgroup_mutex); |
| 6030 | |
| 6031 | kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path); |
| 6032 | if (kn) { |
| 6033 | if (kernfs_type(kn) == KERNFS_DIR) { |
| 6034 | cgrp = kn->priv; |
| 6035 | cgroup_get(cgrp); |
| 6036 | } else { |
| 6037 | cgrp = ERR_PTR(-ENOTDIR); |
| 6038 | } |
| 6039 | kernfs_put(kn); |
| 6040 | } else { |
| 6041 | cgrp = ERR_PTR(-ENOENT); |
| 6042 | } |
| 6043 | |
| 6044 | mutex_unlock(&cgroup_mutex); |
| 6045 | return cgrp; |
| 6046 | } |
| 6047 | EXPORT_SYMBOL_GPL(cgroup_get_from_path); |
| 6048 | |
| 6049 | /* |
| 6050 | * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data |
| 6051 | * definition in cgroup-defs.h. |
| 6052 | */ |
| 6053 | #ifdef CONFIG_SOCK_CGROUP_DATA |
| 6054 | |
| 6055 | #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID) |
| 6056 | |
| 6057 | DEFINE_SPINLOCK(cgroup_sk_update_lock); |
| 6058 | static bool cgroup_sk_alloc_disabled __read_mostly; |
| 6059 | |
| 6060 | void cgroup_sk_alloc_disable(void) |
| 6061 | { |
| 6062 | if (cgroup_sk_alloc_disabled) |
| 6063 | return; |
| 6064 | pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n"); |
| 6065 | cgroup_sk_alloc_disabled = true; |
| 6066 | } |
| 6067 | |
| 6068 | #else |
| 6069 | |
| 6070 | #define cgroup_sk_alloc_disabled false |
| 6071 | |
| 6072 | #endif |
| 6073 | |
| 6074 | void cgroup_sk_alloc(struct sock_cgroup_data *skcd) |
| 6075 | { |
| 6076 | if (cgroup_sk_alloc_disabled) |
| 6077 | return; |
| 6078 | |
| 6079 | rcu_read_lock(); |
| 6080 | |
| 6081 | while (true) { |
| 6082 | struct css_set *cset; |
| 6083 | |
| 6084 | cset = task_css_set(current); |
| 6085 | if (likely(cgroup_tryget(cset->dfl_cgrp))) { |
| 6086 | skcd->val = (unsigned long)cset->dfl_cgrp; |
| 6087 | break; |
| 6088 | } |
| 6089 | cpu_relax(); |
| 6090 | } |
| 6091 | |
| 6092 | rcu_read_unlock(); |
| 6093 | } |
| 6094 | |
| 6095 | void cgroup_sk_free(struct sock_cgroup_data *skcd) |
| 6096 | { |
| 6097 | cgroup_put(sock_cgroup_ptr(skcd)); |
| 6098 | } |
| 6099 | |
| 6100 | #endif /* CONFIG_SOCK_CGROUP_DATA */ |
| 6101 | |
| 6102 | #ifdef CONFIG_CGROUP_DEBUG |
| 6103 | static struct cgroup_subsys_state * |
| 6104 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
| 6105 | { |
| 6106 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 6107 | |
| 6108 | if (!css) |
| 6109 | return ERR_PTR(-ENOMEM); |
| 6110 | |
| 6111 | return css; |
| 6112 | } |
| 6113 | |
| 6114 | static void debug_css_free(struct cgroup_subsys_state *css) |
| 6115 | { |
| 6116 | kfree(css); |
| 6117 | } |
| 6118 | |
| 6119 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 6120 | struct cftype *cft) |
| 6121 | { |
| 6122 | return cgroup_task_count(css->cgroup); |
| 6123 | } |
| 6124 | |
| 6125 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 6126 | struct cftype *cft) |
| 6127 | { |
| 6128 | return (u64)(unsigned long)current->cgroups; |
| 6129 | } |
| 6130 | |
| 6131 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
| 6132 | struct cftype *cft) |
| 6133 | { |
| 6134 | u64 count; |
| 6135 | |
| 6136 | rcu_read_lock(); |
| 6137 | count = atomic_read(&task_css_set(current)->refcount); |
| 6138 | rcu_read_unlock(); |
| 6139 | return count; |
| 6140 | } |
| 6141 | |
| 6142 | static int current_css_set_cg_links_read(struct seq_file *seq, void *v) |
| 6143 | { |
| 6144 | struct cgrp_cset_link *link; |
| 6145 | struct css_set *cset; |
| 6146 | char *name_buf; |
| 6147 | |
| 6148 | name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL); |
| 6149 | if (!name_buf) |
| 6150 | return -ENOMEM; |
| 6151 | |
| 6152 | spin_lock_bh(&css_set_lock); |
| 6153 | rcu_read_lock(); |
| 6154 | cset = rcu_dereference(current->cgroups); |
| 6155 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
| 6156 | struct cgroup *c = link->cgrp; |
| 6157 | |
| 6158 | cgroup_name(c, name_buf, NAME_MAX + 1); |
| 6159 | seq_printf(seq, "Root %d group %s\n", |
| 6160 | c->root->hierarchy_id, name_buf); |
| 6161 | } |
| 6162 | rcu_read_unlock(); |
| 6163 | spin_unlock_bh(&css_set_lock); |
| 6164 | kfree(name_buf); |
| 6165 | return 0; |
| 6166 | } |
| 6167 | |
| 6168 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
| 6169 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
| 6170 | { |
| 6171 | struct cgroup_subsys_state *css = seq_css(seq); |
| 6172 | struct cgrp_cset_link *link; |
| 6173 | |
| 6174 | spin_lock_bh(&css_set_lock); |
| 6175 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
| 6176 | struct css_set *cset = link->cset; |
| 6177 | struct task_struct *task; |
| 6178 | int count = 0; |
| 6179 | |
| 6180 | seq_printf(seq, "css_set %p\n", cset); |
| 6181 | |
| 6182 | list_for_each_entry(task, &cset->tasks, cg_list) { |
| 6183 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 6184 | goto overflow; |
| 6185 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
| 6186 | } |
| 6187 | |
| 6188 | list_for_each_entry(task, &cset->mg_tasks, cg_list) { |
| 6189 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 6190 | goto overflow; |
| 6191 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
| 6192 | } |
| 6193 | continue; |
| 6194 | overflow: |
| 6195 | seq_puts(seq, " ...\n"); |
| 6196 | } |
| 6197 | spin_unlock_bh(&css_set_lock); |
| 6198 | return 0; |
| 6199 | } |
| 6200 | |
| 6201 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
| 6202 | { |
| 6203 | return (!cgroup_is_populated(css->cgroup) && |
| 6204 | !css_has_online_children(&css->cgroup->self)); |
| 6205 | } |
| 6206 | |
| 6207 | static struct cftype debug_files[] = { |
| 6208 | { |
| 6209 | .name = "taskcount", |
| 6210 | .read_u64 = debug_taskcount_read, |
| 6211 | }, |
| 6212 | |
| 6213 | { |
| 6214 | .name = "current_css_set", |
| 6215 | .read_u64 = current_css_set_read, |
| 6216 | }, |
| 6217 | |
| 6218 | { |
| 6219 | .name = "current_css_set_refcount", |
| 6220 | .read_u64 = current_css_set_refcount_read, |
| 6221 | }, |
| 6222 | |
| 6223 | { |
| 6224 | .name = "current_css_set_cg_links", |
| 6225 | .seq_show = current_css_set_cg_links_read, |
| 6226 | }, |
| 6227 | |
| 6228 | { |
| 6229 | .name = "cgroup_css_links", |
| 6230 | .seq_show = cgroup_css_links_read, |
| 6231 | }, |
| 6232 | |
| 6233 | { |
| 6234 | .name = "releasable", |
| 6235 | .read_u64 = releasable_read, |
| 6236 | }, |
| 6237 | |
| 6238 | { } /* terminate */ |
| 6239 | }; |
| 6240 | |
| 6241 | struct cgroup_subsys debug_cgrp_subsys = { |
| 6242 | .css_alloc = debug_css_alloc, |
| 6243 | .css_free = debug_css_free, |
| 6244 | .legacy_cftypes = debug_files, |
| 6245 | }; |
| 6246 | #endif /* CONFIG_CGROUP_DEBUG */ |