Merge branch 'for-4.21' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup
[linux/fpc-iii.git] / kernel / cgroup / cgroup.c
blobf31bd61c946645a30c9f43814bec588c9034677c
1 /*
2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
11 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
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 * ---------------------------------------------------
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.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include "cgroup-internal.h"
33 #include <linux/cred.h>
34 #include <linux/errno.h>
35 #include <linux/init_task.h>
36 #include <linux/kernel.h>
37 #include <linux/magic.h>
38 #include <linux/mutex.h>
39 #include <linux/mount.h>
40 #include <linux/pagemap.h>
41 #include <linux/proc_fs.h>
42 #include <linux/rcupdate.h>
43 #include <linux/sched.h>
44 #include <linux/sched/task.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/percpu-rwsem.h>
48 #include <linux/string.h>
49 #include <linux/hashtable.h>
50 #include <linux/idr.h>
51 #include <linux/kthread.h>
52 #include <linux/atomic.h>
53 #include <linux/cpuset.h>
54 #include <linux/proc_ns.h>
55 #include <linux/nsproxy.h>
56 #include <linux/file.h>
57 #include <linux/sched/cputime.h>
58 #include <linux/psi.h>
59 #include <net/sock.h>
61 #define CREATE_TRACE_POINTS
62 #include <trace/events/cgroup.h>
64 #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
65 MAX_CFTYPE_NAME + 2)
66 /* let's not notify more than 100 times per second */
67 #define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100)
70 * cgroup_mutex is the master lock. Any modification to cgroup or its
71 * hierarchy must be performed while holding it.
73 * css_set_lock protects task->cgroups pointer, the list of css_set
74 * objects, and the chain of tasks off each css_set.
76 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
77 * cgroup.h can use them for lockdep annotations.
79 DEFINE_MUTEX(cgroup_mutex);
80 DEFINE_SPINLOCK(css_set_lock);
82 #ifdef CONFIG_PROVE_RCU
83 EXPORT_SYMBOL_GPL(cgroup_mutex);
84 EXPORT_SYMBOL_GPL(css_set_lock);
85 #endif
87 DEFINE_SPINLOCK(trace_cgroup_path_lock);
88 char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
89 bool cgroup_debug __read_mostly;
92 * Protects cgroup_idr and css_idr so that IDs can be released without
93 * grabbing cgroup_mutex.
95 static DEFINE_SPINLOCK(cgroup_idr_lock);
98 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
99 * against file removal/re-creation across css hiding.
101 static DEFINE_SPINLOCK(cgroup_file_kn_lock);
103 struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
105 #define cgroup_assert_mutex_or_rcu_locked() \
106 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
107 !lockdep_is_held(&cgroup_mutex), \
108 "cgroup_mutex or RCU read lock required");
111 * cgroup destruction makes heavy use of work items and there can be a lot
112 * of concurrent destructions. Use a separate workqueue so that cgroup
113 * destruction work items don't end up filling up max_active of system_wq
114 * which may lead to deadlock.
116 static struct workqueue_struct *cgroup_destroy_wq;
118 /* generate an array of cgroup subsystem pointers */
119 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
120 struct cgroup_subsys *cgroup_subsys[] = {
121 #include <linux/cgroup_subsys.h>
123 #undef SUBSYS
125 /* array of cgroup subsystem names */
126 #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
127 static const char *cgroup_subsys_name[] = {
128 #include <linux/cgroup_subsys.h>
130 #undef SUBSYS
132 /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
133 #define SUBSYS(_x) \
134 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
135 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
136 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
137 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
138 #include <linux/cgroup_subsys.h>
139 #undef SUBSYS
141 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
142 static struct static_key_true *cgroup_subsys_enabled_key[] = {
143 #include <linux/cgroup_subsys.h>
145 #undef SUBSYS
147 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
148 static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
149 #include <linux/cgroup_subsys.h>
151 #undef SUBSYS
153 static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu);
156 * The default hierarchy, reserved for the subsystems that are otherwise
157 * unattached - it never has more than a single cgroup, and all tasks are
158 * part of that cgroup.
160 struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu };
161 EXPORT_SYMBOL_GPL(cgrp_dfl_root);
164 * The default hierarchy always exists but is hidden until mounted for the
165 * first time. This is for backward compatibility.
167 static bool cgrp_dfl_visible;
169 /* some controllers are not supported in the default hierarchy */
170 static u16 cgrp_dfl_inhibit_ss_mask;
172 /* some controllers are implicitly enabled on the default hierarchy */
173 static u16 cgrp_dfl_implicit_ss_mask;
175 /* some controllers can be threaded on the default hierarchy */
176 static u16 cgrp_dfl_threaded_ss_mask;
178 /* The list of hierarchy roots */
179 LIST_HEAD(cgroup_roots);
180 static int cgroup_root_count;
182 /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
183 static DEFINE_IDR(cgroup_hierarchy_idr);
186 * Assign a monotonically increasing serial number to csses. It guarantees
187 * cgroups with bigger numbers are newer than those with smaller numbers.
188 * Also, as csses are always appended to the parent's ->children list, it
189 * guarantees that sibling csses are always sorted in the ascending serial
190 * number order on the list. Protected by cgroup_mutex.
192 static u64 css_serial_nr_next = 1;
195 * These bitmasks identify subsystems with specific features to avoid
196 * having to do iterative checks repeatedly.
198 static u16 have_fork_callback __read_mostly;
199 static u16 have_exit_callback __read_mostly;
200 static u16 have_free_callback __read_mostly;
201 static u16 have_canfork_callback __read_mostly;
203 /* cgroup namespace for init task */
204 struct cgroup_namespace init_cgroup_ns = {
205 .count = REFCOUNT_INIT(2),
206 .user_ns = &init_user_ns,
207 .ns.ops = &cgroupns_operations,
208 .ns.inum = PROC_CGROUP_INIT_INO,
209 .root_cset = &init_css_set,
212 static struct file_system_type cgroup2_fs_type;
213 static struct cftype cgroup_base_files[];
215 static int cgroup_apply_control(struct cgroup *cgrp);
216 static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
217 static void css_task_iter_advance(struct css_task_iter *it);
218 static int cgroup_destroy_locked(struct cgroup *cgrp);
219 static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
220 struct cgroup_subsys *ss);
221 static void css_release(struct percpu_ref *ref);
222 static void kill_css(struct cgroup_subsys_state *css);
223 static int cgroup_addrm_files(struct cgroup_subsys_state *css,
224 struct cgroup *cgrp, struct cftype cfts[],
225 bool is_add);
228 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
229 * @ssid: subsys ID of interest
231 * cgroup_subsys_enabled() can only be used with literal subsys names which
232 * is fine for individual subsystems but unsuitable for cgroup core. This
233 * is slower static_key_enabled() based test indexed by @ssid.
235 bool cgroup_ssid_enabled(int ssid)
237 if (CGROUP_SUBSYS_COUNT == 0)
238 return false;
240 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
244 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
245 * @cgrp: the cgroup of interest
247 * The default hierarchy is the v2 interface of cgroup and this function
248 * can be used to test whether a cgroup is on the default hierarchy for
249 * cases where a subsystem should behave differnetly depending on the
250 * interface version.
252 * The set of behaviors which change on the default hierarchy are still
253 * being determined and the mount option is prefixed with __DEVEL__.
255 * List of changed behaviors:
257 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
258 * and "name" are disallowed.
260 * - When mounting an existing superblock, mount options should match.
262 * - Remount is disallowed.
264 * - rename(2) is disallowed.
266 * - "tasks" is removed. Everything should be at process granularity. Use
267 * "cgroup.procs" instead.
269 * - "cgroup.procs" is not sorted. pids will be unique unless they got
270 * recycled inbetween reads.
272 * - "release_agent" and "notify_on_release" are removed. Replacement
273 * notification mechanism will be implemented.
275 * - "cgroup.clone_children" is removed.
277 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
278 * and its descendants contain no task; otherwise, 1. The file also
279 * generates kernfs notification which can be monitored through poll and
280 * [di]notify when the value of the file changes.
282 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
283 * take masks of ancestors with non-empty cpus/mems, instead of being
284 * moved to an ancestor.
286 * - cpuset: a task can be moved into an empty cpuset, and again it takes
287 * masks of ancestors.
289 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
290 * is not created.
292 * - blkcg: blk-throttle becomes properly hierarchical.
294 * - debug: disallowed on the default hierarchy.
296 bool cgroup_on_dfl(const struct cgroup *cgrp)
298 return cgrp->root == &cgrp_dfl_root;
301 /* IDR wrappers which synchronize using cgroup_idr_lock */
302 static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
303 gfp_t gfp_mask)
305 int ret;
307 idr_preload(gfp_mask);
308 spin_lock_bh(&cgroup_idr_lock);
309 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
310 spin_unlock_bh(&cgroup_idr_lock);
311 idr_preload_end();
312 return ret;
315 static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
317 void *ret;
319 spin_lock_bh(&cgroup_idr_lock);
320 ret = idr_replace(idr, ptr, id);
321 spin_unlock_bh(&cgroup_idr_lock);
322 return ret;
325 static void cgroup_idr_remove(struct idr *idr, int id)
327 spin_lock_bh(&cgroup_idr_lock);
328 idr_remove(idr, id);
329 spin_unlock_bh(&cgroup_idr_lock);
332 static bool cgroup_has_tasks(struct cgroup *cgrp)
334 return cgrp->nr_populated_csets;
337 bool cgroup_is_threaded(struct cgroup *cgrp)
339 return cgrp->dom_cgrp != cgrp;
342 /* can @cgrp host both domain and threaded children? */
343 static bool cgroup_is_mixable(struct cgroup *cgrp)
346 * Root isn't under domain level resource control exempting it from
347 * the no-internal-process constraint, so it can serve as a thread
348 * root and a parent of resource domains at the same time.
350 return !cgroup_parent(cgrp);
353 /* can @cgrp become a thread root? should always be true for a thread root */
354 static bool cgroup_can_be_thread_root(struct cgroup *cgrp)
356 /* mixables don't care */
357 if (cgroup_is_mixable(cgrp))
358 return true;
360 /* domain roots can't be nested under threaded */
361 if (cgroup_is_threaded(cgrp))
362 return false;
364 /* can only have either domain or threaded children */
365 if (cgrp->nr_populated_domain_children)
366 return false;
368 /* and no domain controllers can be enabled */
369 if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
370 return false;
372 return true;
375 /* is @cgrp root of a threaded subtree? */
376 bool cgroup_is_thread_root(struct cgroup *cgrp)
378 /* thread root should be a domain */
379 if (cgroup_is_threaded(cgrp))
380 return false;
382 /* a domain w/ threaded children is a thread root */
383 if (cgrp->nr_threaded_children)
384 return true;
387 * A domain which has tasks and explicit threaded controllers
388 * enabled is a thread root.
390 if (cgroup_has_tasks(cgrp) &&
391 (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask))
392 return true;
394 return false;
397 /* a domain which isn't connected to the root w/o brekage can't be used */
398 static bool cgroup_is_valid_domain(struct cgroup *cgrp)
400 /* the cgroup itself can be a thread root */
401 if (cgroup_is_threaded(cgrp))
402 return false;
404 /* but the ancestors can't be unless mixable */
405 while ((cgrp = cgroup_parent(cgrp))) {
406 if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp))
407 return false;
408 if (cgroup_is_threaded(cgrp))
409 return false;
412 return true;
415 /* subsystems visibly enabled on a cgroup */
416 static u16 cgroup_control(struct cgroup *cgrp)
418 struct cgroup *parent = cgroup_parent(cgrp);
419 u16 root_ss_mask = cgrp->root->subsys_mask;
421 if (parent) {
422 u16 ss_mask = parent->subtree_control;
424 /* threaded cgroups can only have threaded controllers */
425 if (cgroup_is_threaded(cgrp))
426 ss_mask &= cgrp_dfl_threaded_ss_mask;
427 return ss_mask;
430 if (cgroup_on_dfl(cgrp))
431 root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
432 cgrp_dfl_implicit_ss_mask);
433 return root_ss_mask;
436 /* subsystems enabled on a cgroup */
437 static u16 cgroup_ss_mask(struct cgroup *cgrp)
439 struct cgroup *parent = cgroup_parent(cgrp);
441 if (parent) {
442 u16 ss_mask = parent->subtree_ss_mask;
444 /* threaded cgroups can only have threaded controllers */
445 if (cgroup_is_threaded(cgrp))
446 ss_mask &= cgrp_dfl_threaded_ss_mask;
447 return ss_mask;
450 return cgrp->root->subsys_mask;
454 * cgroup_css - obtain a cgroup's css for the specified subsystem
455 * @cgrp: the cgroup of interest
456 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
458 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
459 * function must be called either under cgroup_mutex or rcu_read_lock() and
460 * the caller is responsible for pinning the returned css if it wants to
461 * keep accessing it outside the said locks. This function may return
462 * %NULL if @cgrp doesn't have @subsys_id enabled.
464 static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
465 struct cgroup_subsys *ss)
467 if (ss)
468 return rcu_dereference_check(cgrp->subsys[ss->id],
469 lockdep_is_held(&cgroup_mutex));
470 else
471 return &cgrp->self;
475 * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem
476 * @cgrp: the cgroup of interest
477 * @ss: the subsystem of interest
479 * Find and get @cgrp's css assocaited with @ss. If the css doesn't exist
480 * or is offline, %NULL is returned.
482 static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp,
483 struct cgroup_subsys *ss)
485 struct cgroup_subsys_state *css;
487 rcu_read_lock();
488 css = cgroup_css(cgrp, ss);
489 if (!css || !css_tryget_online(css))
490 css = NULL;
491 rcu_read_unlock();
493 return css;
497 * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss
498 * @cgrp: the cgroup of interest
499 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
501 * Similar to cgroup_css() but returns the effective css, which is defined
502 * as the matching css of the nearest ancestor including self which has @ss
503 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
504 * function is guaranteed to return non-NULL css.
506 static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp,
507 struct cgroup_subsys *ss)
509 lockdep_assert_held(&cgroup_mutex);
511 if (!ss)
512 return &cgrp->self;
515 * This function is used while updating css associations and thus
516 * can't test the csses directly. Test ss_mask.
518 while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
519 cgrp = cgroup_parent(cgrp);
520 if (!cgrp)
521 return NULL;
524 return cgroup_css(cgrp, ss);
528 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
529 * @cgrp: the cgroup of interest
530 * @ss: the subsystem of interest
532 * Find and get the effective css of @cgrp for @ss. The effective css is
533 * defined as the matching css of the nearest ancestor including self which
534 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
535 * the root css is returned, so this function always returns a valid css.
537 * The returned css is not guaranteed to be online, and therefore it is the
538 * callers responsiblity to tryget a reference for it.
540 struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
541 struct cgroup_subsys *ss)
543 struct cgroup_subsys_state *css;
545 do {
546 css = cgroup_css(cgrp, ss);
548 if (css)
549 return css;
550 cgrp = cgroup_parent(cgrp);
551 } while (cgrp);
553 return init_css_set.subsys[ss->id];
557 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
558 * @cgrp: the cgroup of interest
559 * @ss: the subsystem of interest
561 * Find and get the effective css of @cgrp for @ss. The effective css is
562 * defined as the matching css of the nearest ancestor including self which
563 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
564 * the root css is returned, so this function always returns a valid css.
565 * The returned css must be put using css_put().
567 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
568 struct cgroup_subsys *ss)
570 struct cgroup_subsys_state *css;
572 rcu_read_lock();
574 do {
575 css = cgroup_css(cgrp, ss);
577 if (css && css_tryget_online(css))
578 goto out_unlock;
579 cgrp = cgroup_parent(cgrp);
580 } while (cgrp);
582 css = init_css_set.subsys[ss->id];
583 css_get(css);
584 out_unlock:
585 rcu_read_unlock();
586 return css;
589 static void cgroup_get_live(struct cgroup *cgrp)
591 WARN_ON_ONCE(cgroup_is_dead(cgrp));
592 css_get(&cgrp->self);
595 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
597 struct cgroup *cgrp = of->kn->parent->priv;
598 struct cftype *cft = of_cft(of);
601 * This is open and unprotected implementation of cgroup_css().
602 * seq_css() is only called from a kernfs file operation which has
603 * an active reference on the file. Because all the subsystem
604 * files are drained before a css is disassociated with a cgroup,
605 * the matching css from the cgroup's subsys table is guaranteed to
606 * be and stay valid until the enclosing operation is complete.
608 if (cft->ss)
609 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
610 else
611 return &cgrp->self;
613 EXPORT_SYMBOL_GPL(of_css);
616 * for_each_css - iterate all css's of a cgroup
617 * @css: the iteration cursor
618 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
619 * @cgrp: the target cgroup to iterate css's of
621 * Should be called under cgroup_[tree_]mutex.
623 #define for_each_css(css, ssid, cgrp) \
624 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
625 if (!((css) = rcu_dereference_check( \
626 (cgrp)->subsys[(ssid)], \
627 lockdep_is_held(&cgroup_mutex)))) { } \
628 else
631 * for_each_e_css - iterate all effective css's of a cgroup
632 * @css: the iteration cursor
633 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
634 * @cgrp: the target cgroup to iterate css's of
636 * Should be called under cgroup_[tree_]mutex.
638 #define for_each_e_css(css, ssid, cgrp) \
639 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
640 if (!((css) = cgroup_e_css_by_mask(cgrp, \
641 cgroup_subsys[(ssid)]))) \
643 else
646 * do_each_subsys_mask - filter for_each_subsys with a bitmask
647 * @ss: the iteration cursor
648 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
649 * @ss_mask: the bitmask
651 * The block will only run for cases where the ssid-th bit (1 << ssid) of
652 * @ss_mask is set.
654 #define do_each_subsys_mask(ss, ssid, ss_mask) do { \
655 unsigned long __ss_mask = (ss_mask); \
656 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
657 (ssid) = 0; \
658 break; \
660 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
661 (ss) = cgroup_subsys[ssid]; \
664 #define while_each_subsys_mask() \
667 } while (false)
669 /* iterate over child cgrps, lock should be held throughout iteration */
670 #define cgroup_for_each_live_child(child, cgrp) \
671 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
672 if (({ lockdep_assert_held(&cgroup_mutex); \
673 cgroup_is_dead(child); })) \
675 else
677 /* walk live descendants in preorder */
678 #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
679 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
680 if (({ lockdep_assert_held(&cgroup_mutex); \
681 (dsct) = (d_css)->cgroup; \
682 cgroup_is_dead(dsct); })) \
684 else
686 /* walk live descendants in postorder */
687 #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
688 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
689 if (({ lockdep_assert_held(&cgroup_mutex); \
690 (dsct) = (d_css)->cgroup; \
691 cgroup_is_dead(dsct); })) \
693 else
696 * The default css_set - used by init and its children prior to any
697 * hierarchies being mounted. It contains a pointer to the root state
698 * for each subsystem. Also used to anchor the list of css_sets. Not
699 * reference-counted, to improve performance when child cgroups
700 * haven't been created.
702 struct css_set init_css_set = {
703 .refcount = REFCOUNT_INIT(1),
704 .dom_cset = &init_css_set,
705 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
706 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
707 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
708 .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets),
709 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
710 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
711 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
714 * The following field is re-initialized when this cset gets linked
715 * in cgroup_init(). However, let's initialize the field
716 * statically too so that the default cgroup can be accessed safely
717 * early during boot.
719 .dfl_cgrp = &cgrp_dfl_root.cgrp,
722 static int css_set_count = 1; /* 1 for init_css_set */
724 static bool css_set_threaded(struct css_set *cset)
726 return cset->dom_cset != cset;
730 * css_set_populated - does a css_set contain any tasks?
731 * @cset: target css_set
733 * css_set_populated() should be the same as !!cset->nr_tasks at steady
734 * state. However, css_set_populated() can be called while a task is being
735 * added to or removed from the linked list before the nr_tasks is
736 * properly updated. Hence, we can't just look at ->nr_tasks here.
738 static bool css_set_populated(struct css_set *cset)
740 lockdep_assert_held(&css_set_lock);
742 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
746 * cgroup_update_populated - update the populated count of a cgroup
747 * @cgrp: the target cgroup
748 * @populated: inc or dec populated count
750 * One of the css_sets associated with @cgrp is either getting its first
751 * task or losing the last. Update @cgrp->nr_populated_* accordingly. The
752 * count is propagated towards root so that a given cgroup's
753 * nr_populated_children is zero iff none of its descendants contain any
754 * tasks.
756 * @cgrp's interface file "cgroup.populated" is zero if both
757 * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and
758 * 1 otherwise. When the sum changes from or to zero, userland is notified
759 * that the content of the interface file has changed. This can be used to
760 * detect when @cgrp and its descendants become populated or empty.
762 static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
764 struct cgroup *child = NULL;
765 int adj = populated ? 1 : -1;
767 lockdep_assert_held(&css_set_lock);
769 do {
770 bool was_populated = cgroup_is_populated(cgrp);
772 if (!child) {
773 cgrp->nr_populated_csets += adj;
774 } else {
775 if (cgroup_is_threaded(child))
776 cgrp->nr_populated_threaded_children += adj;
777 else
778 cgrp->nr_populated_domain_children += adj;
781 if (was_populated == cgroup_is_populated(cgrp))
782 break;
784 cgroup1_check_for_release(cgrp);
785 cgroup_file_notify(&cgrp->events_file);
787 child = cgrp;
788 cgrp = cgroup_parent(cgrp);
789 } while (cgrp);
793 * css_set_update_populated - update populated state of a css_set
794 * @cset: target css_set
795 * @populated: whether @cset is populated or depopulated
797 * @cset is either getting the first task or losing the last. Update the
798 * populated counters of all associated cgroups accordingly.
800 static void css_set_update_populated(struct css_set *cset, bool populated)
802 struct cgrp_cset_link *link;
804 lockdep_assert_held(&css_set_lock);
806 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
807 cgroup_update_populated(link->cgrp, populated);
811 * css_set_move_task - move a task from one css_set to another
812 * @task: task being moved
813 * @from_cset: css_set @task currently belongs to (may be NULL)
814 * @to_cset: new css_set @task is being moved to (may be NULL)
815 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
817 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
818 * css_set, @from_cset can be NULL. If @task is being disassociated
819 * instead of moved, @to_cset can be NULL.
821 * This function automatically handles populated counter updates and
822 * css_task_iter adjustments but the caller is responsible for managing
823 * @from_cset and @to_cset's reference counts.
825 static void css_set_move_task(struct task_struct *task,
826 struct css_set *from_cset, struct css_set *to_cset,
827 bool use_mg_tasks)
829 lockdep_assert_held(&css_set_lock);
831 if (to_cset && !css_set_populated(to_cset))
832 css_set_update_populated(to_cset, true);
834 if (from_cset) {
835 struct css_task_iter *it, *pos;
837 WARN_ON_ONCE(list_empty(&task->cg_list));
840 * @task is leaving, advance task iterators which are
841 * pointing to it so that they can resume at the next
842 * position. Advancing an iterator might remove it from
843 * the list, use safe walk. See css_task_iter_advance*()
844 * for details.
846 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
847 iters_node)
848 if (it->task_pos == &task->cg_list)
849 css_task_iter_advance(it);
851 list_del_init(&task->cg_list);
852 if (!css_set_populated(from_cset))
853 css_set_update_populated(from_cset, false);
854 } else {
855 WARN_ON_ONCE(!list_empty(&task->cg_list));
858 if (to_cset) {
860 * We are synchronized through cgroup_threadgroup_rwsem
861 * against PF_EXITING setting such that we can't race
862 * against cgroup_exit() changing the css_set to
863 * init_css_set and dropping the old one.
865 WARN_ON_ONCE(task->flags & PF_EXITING);
867 cgroup_move_task(task, to_cset);
868 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
869 &to_cset->tasks);
874 * hash table for cgroup groups. This improves the performance to find
875 * an existing css_set. This hash doesn't (currently) take into
876 * account cgroups in empty hierarchies.
878 #define CSS_SET_HASH_BITS 7
879 static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
881 static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
883 unsigned long key = 0UL;
884 struct cgroup_subsys *ss;
885 int i;
887 for_each_subsys(ss, i)
888 key += (unsigned long)css[i];
889 key = (key >> 16) ^ key;
891 return key;
894 void put_css_set_locked(struct css_set *cset)
896 struct cgrp_cset_link *link, *tmp_link;
897 struct cgroup_subsys *ss;
898 int ssid;
900 lockdep_assert_held(&css_set_lock);
902 if (!refcount_dec_and_test(&cset->refcount))
903 return;
905 WARN_ON_ONCE(!list_empty(&cset->threaded_csets));
907 /* This css_set is dead. unlink it and release cgroup and css refs */
908 for_each_subsys(ss, ssid) {
909 list_del(&cset->e_cset_node[ssid]);
910 css_put(cset->subsys[ssid]);
912 hash_del(&cset->hlist);
913 css_set_count--;
915 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
916 list_del(&link->cset_link);
917 list_del(&link->cgrp_link);
918 if (cgroup_parent(link->cgrp))
919 cgroup_put(link->cgrp);
920 kfree(link);
923 if (css_set_threaded(cset)) {
924 list_del(&cset->threaded_csets_node);
925 put_css_set_locked(cset->dom_cset);
928 kfree_rcu(cset, rcu_head);
932 * compare_css_sets - helper function for find_existing_css_set().
933 * @cset: candidate css_set being tested
934 * @old_cset: existing css_set for a task
935 * @new_cgrp: cgroup that's being entered by the task
936 * @template: desired set of css pointers in css_set (pre-calculated)
938 * Returns true if "cset" matches "old_cset" except for the hierarchy
939 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
941 static bool compare_css_sets(struct css_set *cset,
942 struct css_set *old_cset,
943 struct cgroup *new_cgrp,
944 struct cgroup_subsys_state *template[])
946 struct cgroup *new_dfl_cgrp;
947 struct list_head *l1, *l2;
950 * On the default hierarchy, there can be csets which are
951 * associated with the same set of cgroups but different csses.
952 * Let's first ensure that csses match.
954 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
955 return false;
958 /* @cset's domain should match the default cgroup's */
959 if (cgroup_on_dfl(new_cgrp))
960 new_dfl_cgrp = new_cgrp;
961 else
962 new_dfl_cgrp = old_cset->dfl_cgrp;
964 if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp)
965 return false;
968 * Compare cgroup pointers in order to distinguish between
969 * different cgroups in hierarchies. As different cgroups may
970 * share the same effective css, this comparison is always
971 * necessary.
973 l1 = &cset->cgrp_links;
974 l2 = &old_cset->cgrp_links;
975 while (1) {
976 struct cgrp_cset_link *link1, *link2;
977 struct cgroup *cgrp1, *cgrp2;
979 l1 = l1->next;
980 l2 = l2->next;
981 /* See if we reached the end - both lists are equal length. */
982 if (l1 == &cset->cgrp_links) {
983 BUG_ON(l2 != &old_cset->cgrp_links);
984 break;
985 } else {
986 BUG_ON(l2 == &old_cset->cgrp_links);
988 /* Locate the cgroups associated with these links. */
989 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
990 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
991 cgrp1 = link1->cgrp;
992 cgrp2 = link2->cgrp;
993 /* Hierarchies should be linked in the same order. */
994 BUG_ON(cgrp1->root != cgrp2->root);
997 * If this hierarchy is the hierarchy of the cgroup
998 * that's changing, then we need to check that this
999 * css_set points to the new cgroup; if it's any other
1000 * hierarchy, then this css_set should point to the
1001 * same cgroup as the old css_set.
1003 if (cgrp1->root == new_cgrp->root) {
1004 if (cgrp1 != new_cgrp)
1005 return false;
1006 } else {
1007 if (cgrp1 != cgrp2)
1008 return false;
1011 return true;
1015 * find_existing_css_set - init css array and find the matching css_set
1016 * @old_cset: the css_set that we're using before the cgroup transition
1017 * @cgrp: the cgroup that we're moving into
1018 * @template: out param for the new set of csses, should be clear on entry
1020 static struct css_set *find_existing_css_set(struct css_set *old_cset,
1021 struct cgroup *cgrp,
1022 struct cgroup_subsys_state *template[])
1024 struct cgroup_root *root = cgrp->root;
1025 struct cgroup_subsys *ss;
1026 struct css_set *cset;
1027 unsigned long key;
1028 int i;
1031 * Build the set of subsystem state objects that we want to see in the
1032 * new css_set. while subsystems can change globally, the entries here
1033 * won't change, so no need for locking.
1035 for_each_subsys(ss, i) {
1036 if (root->subsys_mask & (1UL << i)) {
1038 * @ss is in this hierarchy, so we want the
1039 * effective css from @cgrp.
1041 template[i] = cgroup_e_css_by_mask(cgrp, ss);
1042 } else {
1044 * @ss is not in this hierarchy, so we don't want
1045 * to change the css.
1047 template[i] = old_cset->subsys[i];
1051 key = css_set_hash(template);
1052 hash_for_each_possible(css_set_table, cset, hlist, key) {
1053 if (!compare_css_sets(cset, old_cset, cgrp, template))
1054 continue;
1056 /* This css_set matches what we need */
1057 return cset;
1060 /* No existing cgroup group matched */
1061 return NULL;
1064 static void free_cgrp_cset_links(struct list_head *links_to_free)
1066 struct cgrp_cset_link *link, *tmp_link;
1068 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
1069 list_del(&link->cset_link);
1070 kfree(link);
1075 * allocate_cgrp_cset_links - allocate cgrp_cset_links
1076 * @count: the number of links to allocate
1077 * @tmp_links: list_head the allocated links are put on
1079 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
1080 * through ->cset_link. Returns 0 on success or -errno.
1082 static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
1084 struct cgrp_cset_link *link;
1085 int i;
1087 INIT_LIST_HEAD(tmp_links);
1089 for (i = 0; i < count; i++) {
1090 link = kzalloc(sizeof(*link), GFP_KERNEL);
1091 if (!link) {
1092 free_cgrp_cset_links(tmp_links);
1093 return -ENOMEM;
1095 list_add(&link->cset_link, tmp_links);
1097 return 0;
1101 * link_css_set - a helper function to link a css_set to a cgroup
1102 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
1103 * @cset: the css_set to be linked
1104 * @cgrp: the destination cgroup
1106 static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
1107 struct cgroup *cgrp)
1109 struct cgrp_cset_link *link;
1111 BUG_ON(list_empty(tmp_links));
1113 if (cgroup_on_dfl(cgrp))
1114 cset->dfl_cgrp = cgrp;
1116 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
1117 link->cset = cset;
1118 link->cgrp = cgrp;
1121 * Always add links to the tail of the lists so that the lists are
1122 * in choronological order.
1124 list_move_tail(&link->cset_link, &cgrp->cset_links);
1125 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
1127 if (cgroup_parent(cgrp))
1128 cgroup_get_live(cgrp);
1132 * find_css_set - return a new css_set with one cgroup updated
1133 * @old_cset: the baseline css_set
1134 * @cgrp: the cgroup to be updated
1136 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1137 * substituted into the appropriate hierarchy.
1139 static struct css_set *find_css_set(struct css_set *old_cset,
1140 struct cgroup *cgrp)
1142 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
1143 struct css_set *cset;
1144 struct list_head tmp_links;
1145 struct cgrp_cset_link *link;
1146 struct cgroup_subsys *ss;
1147 unsigned long key;
1148 int ssid;
1150 lockdep_assert_held(&cgroup_mutex);
1152 /* First see if we already have a cgroup group that matches
1153 * the desired set */
1154 spin_lock_irq(&css_set_lock);
1155 cset = find_existing_css_set(old_cset, cgrp, template);
1156 if (cset)
1157 get_css_set(cset);
1158 spin_unlock_irq(&css_set_lock);
1160 if (cset)
1161 return cset;
1163 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
1164 if (!cset)
1165 return NULL;
1167 /* Allocate all the cgrp_cset_link objects that we'll need */
1168 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
1169 kfree(cset);
1170 return NULL;
1173 refcount_set(&cset->refcount, 1);
1174 cset->dom_cset = cset;
1175 INIT_LIST_HEAD(&cset->tasks);
1176 INIT_LIST_HEAD(&cset->mg_tasks);
1177 INIT_LIST_HEAD(&cset->task_iters);
1178 INIT_LIST_HEAD(&cset->threaded_csets);
1179 INIT_HLIST_NODE(&cset->hlist);
1180 INIT_LIST_HEAD(&cset->cgrp_links);
1181 INIT_LIST_HEAD(&cset->mg_preload_node);
1182 INIT_LIST_HEAD(&cset->mg_node);
1184 /* Copy the set of subsystem state objects generated in
1185 * find_existing_css_set() */
1186 memcpy(cset->subsys, template, sizeof(cset->subsys));
1188 spin_lock_irq(&css_set_lock);
1189 /* Add reference counts and links from the new css_set. */
1190 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
1191 struct cgroup *c = link->cgrp;
1193 if (c->root == cgrp->root)
1194 c = cgrp;
1195 link_css_set(&tmp_links, cset, c);
1198 BUG_ON(!list_empty(&tmp_links));
1200 css_set_count++;
1202 /* Add @cset to the hash table */
1203 key = css_set_hash(cset->subsys);
1204 hash_add(css_set_table, &cset->hlist, key);
1206 for_each_subsys(ss, ssid) {
1207 struct cgroup_subsys_state *css = cset->subsys[ssid];
1209 list_add_tail(&cset->e_cset_node[ssid],
1210 &css->cgroup->e_csets[ssid]);
1211 css_get(css);
1214 spin_unlock_irq(&css_set_lock);
1217 * If @cset should be threaded, look up the matching dom_cset and
1218 * link them up. We first fully initialize @cset then look for the
1219 * dom_cset. It's simpler this way and safe as @cset is guaranteed
1220 * to stay empty until we return.
1222 if (cgroup_is_threaded(cset->dfl_cgrp)) {
1223 struct css_set *dcset;
1225 dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp);
1226 if (!dcset) {
1227 put_css_set(cset);
1228 return NULL;
1231 spin_lock_irq(&css_set_lock);
1232 cset->dom_cset = dcset;
1233 list_add_tail(&cset->threaded_csets_node,
1234 &dcset->threaded_csets);
1235 spin_unlock_irq(&css_set_lock);
1238 return cset;
1241 struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
1243 struct cgroup *root_cgrp = kf_root->kn->priv;
1245 return root_cgrp->root;
1248 static int cgroup_init_root_id(struct cgroup_root *root)
1250 int id;
1252 lockdep_assert_held(&cgroup_mutex);
1254 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
1255 if (id < 0)
1256 return id;
1258 root->hierarchy_id = id;
1259 return 0;
1262 static void cgroup_exit_root_id(struct cgroup_root *root)
1264 lockdep_assert_held(&cgroup_mutex);
1266 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1269 void cgroup_free_root(struct cgroup_root *root)
1271 if (root) {
1272 idr_destroy(&root->cgroup_idr);
1273 kfree(root);
1277 static void cgroup_destroy_root(struct cgroup_root *root)
1279 struct cgroup *cgrp = &root->cgrp;
1280 struct cgrp_cset_link *link, *tmp_link;
1282 trace_cgroup_destroy_root(root);
1284 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
1286 BUG_ON(atomic_read(&root->nr_cgrps));
1287 BUG_ON(!list_empty(&cgrp->self.children));
1289 /* Rebind all subsystems back to the default hierarchy */
1290 WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
1293 * Release all the links from cset_links to this hierarchy's
1294 * root cgroup
1296 spin_lock_irq(&css_set_lock);
1298 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1299 list_del(&link->cset_link);
1300 list_del(&link->cgrp_link);
1301 kfree(link);
1304 spin_unlock_irq(&css_set_lock);
1306 if (!list_empty(&root->root_list)) {
1307 list_del(&root->root_list);
1308 cgroup_root_count--;
1311 cgroup_exit_root_id(root);
1313 mutex_unlock(&cgroup_mutex);
1315 kernfs_destroy_root(root->kf_root);
1316 cgroup_free_root(root);
1320 * look up cgroup associated with current task's cgroup namespace on the
1321 * specified hierarchy
1323 static struct cgroup *
1324 current_cgns_cgroup_from_root(struct cgroup_root *root)
1326 struct cgroup *res = NULL;
1327 struct css_set *cset;
1329 lockdep_assert_held(&css_set_lock);
1331 rcu_read_lock();
1333 cset = current->nsproxy->cgroup_ns->root_cset;
1334 if (cset == &init_css_set) {
1335 res = &root->cgrp;
1336 } else {
1337 struct cgrp_cset_link *link;
1339 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1340 struct cgroup *c = link->cgrp;
1342 if (c->root == root) {
1343 res = c;
1344 break;
1348 rcu_read_unlock();
1350 BUG_ON(!res);
1351 return res;
1354 /* look up cgroup associated with given css_set on the specified hierarchy */
1355 static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
1356 struct cgroup_root *root)
1358 struct cgroup *res = NULL;
1360 lockdep_assert_held(&cgroup_mutex);
1361 lockdep_assert_held(&css_set_lock);
1363 if (cset == &init_css_set) {
1364 res = &root->cgrp;
1365 } else if (root == &cgrp_dfl_root) {
1366 res = cset->dfl_cgrp;
1367 } else {
1368 struct cgrp_cset_link *link;
1370 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1371 struct cgroup *c = link->cgrp;
1373 if (c->root == root) {
1374 res = c;
1375 break;
1380 BUG_ON(!res);
1381 return res;
1385 * Return the cgroup for "task" from the given hierarchy. Must be
1386 * called with cgroup_mutex and css_set_lock held.
1388 struct cgroup *task_cgroup_from_root(struct task_struct *task,
1389 struct cgroup_root *root)
1392 * No need to lock the task - since we hold cgroup_mutex the
1393 * task can't change groups, so the only thing that can happen
1394 * is that it exits and its css is set back to init_css_set.
1396 return cset_cgroup_from_root(task_css_set(task), root);
1400 * A task must hold cgroup_mutex to modify cgroups.
1402 * Any task can increment and decrement the count field without lock.
1403 * So in general, code holding cgroup_mutex can't rely on the count
1404 * field not changing. However, if the count goes to zero, then only
1405 * cgroup_attach_task() can increment it again. Because a count of zero
1406 * means that no tasks are currently attached, therefore there is no
1407 * way a task attached to that cgroup can fork (the other way to
1408 * increment the count). So code holding cgroup_mutex can safely
1409 * assume that if the count is zero, it will stay zero. Similarly, if
1410 * a task holds cgroup_mutex on a cgroup with zero count, it
1411 * knows that the cgroup won't be removed, as cgroup_rmdir()
1412 * needs that mutex.
1414 * A cgroup can only be deleted if both its 'count' of using tasks
1415 * is zero, and its list of 'children' cgroups is empty. Since all
1416 * tasks in the system use _some_ cgroup, and since there is always at
1417 * least one task in the system (init, pid == 1), therefore, root cgroup
1418 * always has either children cgroups and/or using tasks. So we don't
1419 * need a special hack to ensure that root cgroup cannot be deleted.
1421 * P.S. One more locking exception. RCU is used to guard the
1422 * update of a tasks cgroup pointer by cgroup_attach_task()
1425 static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
1427 static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1428 char *buf)
1430 struct cgroup_subsys *ss = cft->ss;
1432 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1433 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
1434 const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : "";
1436 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s",
1437 dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1438 cft->name);
1439 } else {
1440 strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1442 return buf;
1446 * cgroup_file_mode - deduce file mode of a control file
1447 * @cft: the control file in question
1449 * S_IRUGO for read, S_IWUSR for write.
1451 static umode_t cgroup_file_mode(const struct cftype *cft)
1453 umode_t mode = 0;
1455 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1456 mode |= S_IRUGO;
1458 if (cft->write_u64 || cft->write_s64 || cft->write) {
1459 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1460 mode |= S_IWUGO;
1461 else
1462 mode |= S_IWUSR;
1465 return mode;
1469 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
1470 * @subtree_control: the new subtree_control mask to consider
1471 * @this_ss_mask: available subsystems
1473 * On the default hierarchy, a subsystem may request other subsystems to be
1474 * enabled together through its ->depends_on mask. In such cases, more
1475 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1477 * This function calculates which subsystems need to be enabled if
1478 * @subtree_control is to be applied while restricted to @this_ss_mask.
1480 static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
1482 u16 cur_ss_mask = subtree_control;
1483 struct cgroup_subsys *ss;
1484 int ssid;
1486 lockdep_assert_held(&cgroup_mutex);
1488 cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
1490 while (true) {
1491 u16 new_ss_mask = cur_ss_mask;
1493 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
1494 new_ss_mask |= ss->depends_on;
1495 } while_each_subsys_mask();
1498 * Mask out subsystems which aren't available. This can
1499 * happen only if some depended-upon subsystems were bound
1500 * to non-default hierarchies.
1502 new_ss_mask &= this_ss_mask;
1504 if (new_ss_mask == cur_ss_mask)
1505 break;
1506 cur_ss_mask = new_ss_mask;
1509 return cur_ss_mask;
1513 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1514 * @kn: the kernfs_node being serviced
1516 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1517 * the method finishes if locking succeeded. Note that once this function
1518 * returns the cgroup returned by cgroup_kn_lock_live() may become
1519 * inaccessible any time. If the caller intends to continue to access the
1520 * cgroup, it should pin it before invoking this function.
1522 void cgroup_kn_unlock(struct kernfs_node *kn)
1524 struct cgroup *cgrp;
1526 if (kernfs_type(kn) == KERNFS_DIR)
1527 cgrp = kn->priv;
1528 else
1529 cgrp = kn->parent->priv;
1531 mutex_unlock(&cgroup_mutex);
1533 kernfs_unbreak_active_protection(kn);
1534 cgroup_put(cgrp);
1538 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1539 * @kn: the kernfs_node being serviced
1540 * @drain_offline: perform offline draining on the cgroup
1542 * This helper is to be used by a cgroup kernfs method currently servicing
1543 * @kn. It breaks the active protection, performs cgroup locking and
1544 * verifies that the associated cgroup is alive. Returns the cgroup if
1545 * alive; otherwise, %NULL. A successful return should be undone by a
1546 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1547 * cgroup is drained of offlining csses before return.
1549 * Any cgroup kernfs method implementation which requires locking the
1550 * associated cgroup should use this helper. It avoids nesting cgroup
1551 * locking under kernfs active protection and allows all kernfs operations
1552 * including self-removal.
1554 struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
1556 struct cgroup *cgrp;
1558 if (kernfs_type(kn) == KERNFS_DIR)
1559 cgrp = kn->priv;
1560 else
1561 cgrp = kn->parent->priv;
1564 * We're gonna grab cgroup_mutex which nests outside kernfs
1565 * active_ref. cgroup liveliness check alone provides enough
1566 * protection against removal. Ensure @cgrp stays accessible and
1567 * break the active_ref protection.
1569 if (!cgroup_tryget(cgrp))
1570 return NULL;
1571 kernfs_break_active_protection(kn);
1573 if (drain_offline)
1574 cgroup_lock_and_drain_offline(cgrp);
1575 else
1576 mutex_lock(&cgroup_mutex);
1578 if (!cgroup_is_dead(cgrp))
1579 return cgrp;
1581 cgroup_kn_unlock(kn);
1582 return NULL;
1585 static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
1587 char name[CGROUP_FILE_NAME_MAX];
1589 lockdep_assert_held(&cgroup_mutex);
1591 if (cft->file_offset) {
1592 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1593 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1595 spin_lock_irq(&cgroup_file_kn_lock);
1596 cfile->kn = NULL;
1597 spin_unlock_irq(&cgroup_file_kn_lock);
1599 del_timer_sync(&cfile->notify_timer);
1602 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
1606 * css_clear_dir - remove subsys files in a cgroup directory
1607 * @css: taget css
1609 static void css_clear_dir(struct cgroup_subsys_state *css)
1611 struct cgroup *cgrp = css->cgroup;
1612 struct cftype *cfts;
1614 if (!(css->flags & CSS_VISIBLE))
1615 return;
1617 css->flags &= ~CSS_VISIBLE;
1619 if (!css->ss) {
1620 if (cgroup_on_dfl(cgrp))
1621 cfts = cgroup_base_files;
1622 else
1623 cfts = cgroup1_base_files;
1625 cgroup_addrm_files(css, cgrp, cfts, false);
1626 } else {
1627 list_for_each_entry(cfts, &css->ss->cfts, node)
1628 cgroup_addrm_files(css, cgrp, cfts, false);
1633 * css_populate_dir - create subsys files in a cgroup directory
1634 * @css: target css
1636 * On failure, no file is added.
1638 static int css_populate_dir(struct cgroup_subsys_state *css)
1640 struct cgroup *cgrp = css->cgroup;
1641 struct cftype *cfts, *failed_cfts;
1642 int ret;
1644 if ((css->flags & CSS_VISIBLE) || !cgrp->kn)
1645 return 0;
1647 if (!css->ss) {
1648 if (cgroup_on_dfl(cgrp))
1649 cfts = cgroup_base_files;
1650 else
1651 cfts = cgroup1_base_files;
1653 ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1654 if (ret < 0)
1655 return ret;
1656 } else {
1657 list_for_each_entry(cfts, &css->ss->cfts, node) {
1658 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1659 if (ret < 0) {
1660 failed_cfts = cfts;
1661 goto err;
1666 css->flags |= CSS_VISIBLE;
1668 return 0;
1669 err:
1670 list_for_each_entry(cfts, &css->ss->cfts, node) {
1671 if (cfts == failed_cfts)
1672 break;
1673 cgroup_addrm_files(css, cgrp, cfts, false);
1675 return ret;
1678 int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
1680 struct cgroup *dcgrp = &dst_root->cgrp;
1681 struct cgroup_subsys *ss;
1682 int ssid, i, ret;
1684 lockdep_assert_held(&cgroup_mutex);
1686 do_each_subsys_mask(ss, ssid, ss_mask) {
1688 * If @ss has non-root csses attached to it, can't move.
1689 * If @ss is an implicit controller, it is exempt from this
1690 * rule and can be stolen.
1692 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
1693 !ss->implicit_on_dfl)
1694 return -EBUSY;
1696 /* can't move between two non-dummy roots either */
1697 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
1698 return -EBUSY;
1699 } while_each_subsys_mask();
1701 do_each_subsys_mask(ss, ssid, ss_mask) {
1702 struct cgroup_root *src_root = ss->root;
1703 struct cgroup *scgrp = &src_root->cgrp;
1704 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
1705 struct css_set *cset;
1707 WARN_ON(!css || cgroup_css(dcgrp, ss));
1709 /* disable from the source */
1710 src_root->subsys_mask &= ~(1 << ssid);
1711 WARN_ON(cgroup_apply_control(scgrp));
1712 cgroup_finalize_control(scgrp, 0);
1714 /* rebind */
1715 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1716 rcu_assign_pointer(dcgrp->subsys[ssid], css);
1717 ss->root = dst_root;
1718 css->cgroup = dcgrp;
1720 spin_lock_irq(&css_set_lock);
1721 hash_for_each(css_set_table, i, cset, hlist)
1722 list_move_tail(&cset->e_cset_node[ss->id],
1723 &dcgrp->e_csets[ss->id]);
1724 spin_unlock_irq(&css_set_lock);
1726 /* default hierarchy doesn't enable controllers by default */
1727 dst_root->subsys_mask |= 1 << ssid;
1728 if (dst_root == &cgrp_dfl_root) {
1729 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1730 } else {
1731 dcgrp->subtree_control |= 1 << ssid;
1732 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
1735 ret = cgroup_apply_control(dcgrp);
1736 if (ret)
1737 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1738 ss->name, ret);
1740 if (ss->bind)
1741 ss->bind(css);
1742 } while_each_subsys_mask();
1744 kernfs_activate(dcgrp->kn);
1745 return 0;
1748 int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
1749 struct kernfs_root *kf_root)
1751 int len = 0;
1752 char *buf = NULL;
1753 struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
1754 struct cgroup *ns_cgroup;
1756 buf = kmalloc(PATH_MAX, GFP_KERNEL);
1757 if (!buf)
1758 return -ENOMEM;
1760 spin_lock_irq(&css_set_lock);
1761 ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
1762 len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
1763 spin_unlock_irq(&css_set_lock);
1765 if (len >= PATH_MAX)
1766 len = -ERANGE;
1767 else if (len > 0) {
1768 seq_escape(sf, buf, " \t\n\\");
1769 len = 0;
1771 kfree(buf);
1772 return len;
1775 static int parse_cgroup_root_flags(char *data, unsigned int *root_flags)
1777 char *token;
1779 *root_flags = 0;
1781 if (!data || *data == '\0')
1782 return 0;
1784 while ((token = strsep(&data, ",")) != NULL) {
1785 if (!strcmp(token, "nsdelegate")) {
1786 *root_flags |= CGRP_ROOT_NS_DELEGATE;
1787 continue;
1790 pr_err("cgroup2: unknown option \"%s\"\n", token);
1791 return -EINVAL;
1794 return 0;
1797 static void apply_cgroup_root_flags(unsigned int root_flags)
1799 if (current->nsproxy->cgroup_ns == &init_cgroup_ns) {
1800 if (root_flags & CGRP_ROOT_NS_DELEGATE)
1801 cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE;
1802 else
1803 cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE;
1807 static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
1809 if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE)
1810 seq_puts(seq, ",nsdelegate");
1811 return 0;
1814 static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
1816 unsigned int root_flags;
1817 int ret;
1819 ret = parse_cgroup_root_flags(data, &root_flags);
1820 if (ret)
1821 return ret;
1823 apply_cgroup_root_flags(root_flags);
1824 return 0;
1828 * To reduce the fork() overhead for systems that are not actually using
1829 * their cgroups capability, we don't maintain the lists running through
1830 * each css_set to its tasks until we see the list actually used - in other
1831 * words after the first mount.
1833 static bool use_task_css_set_links __read_mostly;
1835 static void cgroup_enable_task_cg_lists(void)
1837 struct task_struct *p, *g;
1840 * We need tasklist_lock because RCU is not safe against
1841 * while_each_thread(). Besides, a forking task that has passed
1842 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1843 * is not guaranteed to have its child immediately visible in the
1844 * tasklist if we walk through it with RCU.
1846 read_lock(&tasklist_lock);
1847 spin_lock_irq(&css_set_lock);
1849 if (use_task_css_set_links)
1850 goto out_unlock;
1852 use_task_css_set_links = true;
1854 do_each_thread(g, p) {
1855 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1856 task_css_set(p) != &init_css_set);
1859 * We should check if the process is exiting, otherwise
1860 * it will race with cgroup_exit() in that the list
1861 * entry won't be deleted though the process has exited.
1862 * Do it while holding siglock so that we don't end up
1863 * racing against cgroup_exit().
1865 * Interrupts were already disabled while acquiring
1866 * the css_set_lock, so we do not need to disable it
1867 * again when acquiring the sighand->siglock here.
1869 spin_lock(&p->sighand->siglock);
1870 if (!(p->flags & PF_EXITING)) {
1871 struct css_set *cset = task_css_set(p);
1873 if (!css_set_populated(cset))
1874 css_set_update_populated(cset, true);
1875 list_add_tail(&p->cg_list, &cset->tasks);
1876 get_css_set(cset);
1877 cset->nr_tasks++;
1879 spin_unlock(&p->sighand->siglock);
1880 } while_each_thread(g, p);
1881 out_unlock:
1882 spin_unlock_irq(&css_set_lock);
1883 read_unlock(&tasklist_lock);
1886 static void init_cgroup_housekeeping(struct cgroup *cgrp)
1888 struct cgroup_subsys *ss;
1889 int ssid;
1891 INIT_LIST_HEAD(&cgrp->self.sibling);
1892 INIT_LIST_HEAD(&cgrp->self.children);
1893 INIT_LIST_HEAD(&cgrp->cset_links);
1894 INIT_LIST_HEAD(&cgrp->pidlists);
1895 mutex_init(&cgrp->pidlist_mutex);
1896 cgrp->self.cgroup = cgrp;
1897 cgrp->self.flags |= CSS_ONLINE;
1898 cgrp->dom_cgrp = cgrp;
1899 cgrp->max_descendants = INT_MAX;
1900 cgrp->max_depth = INT_MAX;
1901 INIT_LIST_HEAD(&cgrp->rstat_css_list);
1902 prev_cputime_init(&cgrp->prev_cputime);
1904 for_each_subsys(ss, ssid)
1905 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
1907 init_waitqueue_head(&cgrp->offline_waitq);
1908 INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
1911 void init_cgroup_root(struct cgroup_root *root, struct cgroup_sb_opts *opts)
1913 struct cgroup *cgrp = &root->cgrp;
1915 INIT_LIST_HEAD(&root->root_list);
1916 atomic_set(&root->nr_cgrps, 1);
1917 cgrp->root = root;
1918 init_cgroup_housekeeping(cgrp);
1919 idr_init(&root->cgroup_idr);
1921 root->flags = opts->flags;
1922 if (opts->release_agent)
1923 strscpy(root->release_agent_path, opts->release_agent, PATH_MAX);
1924 if (opts->name)
1925 strscpy(root->name, opts->name, MAX_CGROUP_ROOT_NAMELEN);
1926 if (opts->cpuset_clone_children)
1927 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
1930 int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask, int ref_flags)
1932 LIST_HEAD(tmp_links);
1933 struct cgroup *root_cgrp = &root->cgrp;
1934 struct kernfs_syscall_ops *kf_sops;
1935 struct css_set *cset;
1936 int i, ret;
1938 lockdep_assert_held(&cgroup_mutex);
1940 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
1941 if (ret < 0)
1942 goto out;
1943 root_cgrp->id = ret;
1944 root_cgrp->ancestor_ids[0] = ret;
1946 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release,
1947 ref_flags, GFP_KERNEL);
1948 if (ret)
1949 goto out;
1952 * We're accessing css_set_count without locking css_set_lock here,
1953 * but that's OK - it can only be increased by someone holding
1954 * cgroup_lock, and that's us. Later rebinding may disable
1955 * controllers on the default hierarchy and thus create new csets,
1956 * which can't be more than the existing ones. Allocate 2x.
1958 ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
1959 if (ret)
1960 goto cancel_ref;
1962 ret = cgroup_init_root_id(root);
1963 if (ret)
1964 goto cancel_ref;
1966 kf_sops = root == &cgrp_dfl_root ?
1967 &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
1969 root->kf_root = kernfs_create_root(kf_sops,
1970 KERNFS_ROOT_CREATE_DEACTIVATED |
1971 KERNFS_ROOT_SUPPORT_EXPORTOP,
1972 root_cgrp);
1973 if (IS_ERR(root->kf_root)) {
1974 ret = PTR_ERR(root->kf_root);
1975 goto exit_root_id;
1977 root_cgrp->kn = root->kf_root->kn;
1979 ret = css_populate_dir(&root_cgrp->self);
1980 if (ret)
1981 goto destroy_root;
1983 ret = rebind_subsystems(root, ss_mask);
1984 if (ret)
1985 goto destroy_root;
1987 ret = cgroup_bpf_inherit(root_cgrp);
1988 WARN_ON_ONCE(ret);
1990 trace_cgroup_setup_root(root);
1993 * There must be no failure case after here, since rebinding takes
1994 * care of subsystems' refcounts, which are explicitly dropped in
1995 * the failure exit path.
1997 list_add(&root->root_list, &cgroup_roots);
1998 cgroup_root_count++;
2001 * Link the root cgroup in this hierarchy into all the css_set
2002 * objects.
2004 spin_lock_irq(&css_set_lock);
2005 hash_for_each(css_set_table, i, cset, hlist) {
2006 link_css_set(&tmp_links, cset, root_cgrp);
2007 if (css_set_populated(cset))
2008 cgroup_update_populated(root_cgrp, true);
2010 spin_unlock_irq(&css_set_lock);
2012 BUG_ON(!list_empty(&root_cgrp->self.children));
2013 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
2015 kernfs_activate(root_cgrp->kn);
2016 ret = 0;
2017 goto out;
2019 destroy_root:
2020 kernfs_destroy_root(root->kf_root);
2021 root->kf_root = NULL;
2022 exit_root_id:
2023 cgroup_exit_root_id(root);
2024 cancel_ref:
2025 percpu_ref_exit(&root_cgrp->self.refcnt);
2026 out:
2027 free_cgrp_cset_links(&tmp_links);
2028 return ret;
2031 struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
2032 struct cgroup_root *root, unsigned long magic,
2033 struct cgroup_namespace *ns)
2035 struct dentry *dentry;
2036 bool new_sb;
2038 dentry = kernfs_mount(fs_type, flags, root->kf_root, magic, &new_sb);
2041 * In non-init cgroup namespace, instead of root cgroup's dentry,
2042 * we return the dentry corresponding to the cgroupns->root_cgrp.
2044 if (!IS_ERR(dentry) && ns != &init_cgroup_ns) {
2045 struct dentry *nsdentry;
2046 struct cgroup *cgrp;
2048 mutex_lock(&cgroup_mutex);
2049 spin_lock_irq(&css_set_lock);
2051 cgrp = cset_cgroup_from_root(ns->root_cset, root);
2053 spin_unlock_irq(&css_set_lock);
2054 mutex_unlock(&cgroup_mutex);
2056 nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb);
2057 dput(dentry);
2058 dentry = nsdentry;
2061 if (IS_ERR(dentry) || !new_sb)
2062 cgroup_put(&root->cgrp);
2064 return dentry;
2067 static struct dentry *cgroup_mount(struct file_system_type *fs_type,
2068 int flags, const char *unused_dev_name,
2069 void *data)
2071 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
2072 struct dentry *dentry;
2073 int ret;
2075 get_cgroup_ns(ns);
2077 /* Check if the caller has permission to mount. */
2078 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) {
2079 put_cgroup_ns(ns);
2080 return ERR_PTR(-EPERM);
2084 * The first time anyone tries to mount a cgroup, enable the list
2085 * linking each css_set to its tasks and fix up all existing tasks.
2087 if (!use_task_css_set_links)
2088 cgroup_enable_task_cg_lists();
2090 if (fs_type == &cgroup2_fs_type) {
2091 unsigned int root_flags;
2093 ret = parse_cgroup_root_flags(data, &root_flags);
2094 if (ret) {
2095 put_cgroup_ns(ns);
2096 return ERR_PTR(ret);
2099 cgrp_dfl_visible = true;
2100 cgroup_get_live(&cgrp_dfl_root.cgrp);
2102 dentry = cgroup_do_mount(&cgroup2_fs_type, flags, &cgrp_dfl_root,
2103 CGROUP2_SUPER_MAGIC, ns);
2104 if (!IS_ERR(dentry))
2105 apply_cgroup_root_flags(root_flags);
2106 } else {
2107 dentry = cgroup1_mount(&cgroup_fs_type, flags, data,
2108 CGROUP_SUPER_MAGIC, ns);
2111 put_cgroup_ns(ns);
2112 return dentry;
2115 static void cgroup_kill_sb(struct super_block *sb)
2117 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
2118 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
2121 * If @root doesn't have any mounts or children, start killing it.
2122 * This prevents new mounts by disabling percpu_ref_tryget_live().
2123 * cgroup_mount() may wait for @root's release.
2125 * And don't kill the default root.
2127 if (!list_empty(&root->cgrp.self.children) ||
2128 root == &cgrp_dfl_root)
2129 cgroup_put(&root->cgrp);
2130 else
2131 percpu_ref_kill(&root->cgrp.self.refcnt);
2133 kernfs_kill_sb(sb);
2136 struct file_system_type cgroup_fs_type = {
2137 .name = "cgroup",
2138 .mount = cgroup_mount,
2139 .kill_sb = cgroup_kill_sb,
2140 .fs_flags = FS_USERNS_MOUNT,
2143 static struct file_system_type cgroup2_fs_type = {
2144 .name = "cgroup2",
2145 .mount = cgroup_mount,
2146 .kill_sb = cgroup_kill_sb,
2147 .fs_flags = FS_USERNS_MOUNT,
2150 int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
2151 struct cgroup_namespace *ns)
2153 struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
2155 return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
2158 int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
2159 struct cgroup_namespace *ns)
2161 int ret;
2163 mutex_lock(&cgroup_mutex);
2164 spin_lock_irq(&css_set_lock);
2166 ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
2168 spin_unlock_irq(&css_set_lock);
2169 mutex_unlock(&cgroup_mutex);
2171 return ret;
2173 EXPORT_SYMBOL_GPL(cgroup_path_ns);
2176 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
2177 * @task: target task
2178 * @buf: the buffer to write the path into
2179 * @buflen: the length of the buffer
2181 * Determine @task's cgroup on the first (the one with the lowest non-zero
2182 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2183 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2184 * cgroup controller callbacks.
2186 * Return value is the same as kernfs_path().
2188 int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
2190 struct cgroup_root *root;
2191 struct cgroup *cgrp;
2192 int hierarchy_id = 1;
2193 int ret;
2195 mutex_lock(&cgroup_mutex);
2196 spin_lock_irq(&css_set_lock);
2198 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
2200 if (root) {
2201 cgrp = task_cgroup_from_root(task, root);
2202 ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
2203 } else {
2204 /* if no hierarchy exists, everyone is in "/" */
2205 ret = strlcpy(buf, "/", buflen);
2208 spin_unlock_irq(&css_set_lock);
2209 mutex_unlock(&cgroup_mutex);
2210 return ret;
2212 EXPORT_SYMBOL_GPL(task_cgroup_path);
2215 * cgroup_migrate_add_task - add a migration target task to a migration context
2216 * @task: target task
2217 * @mgctx: target migration context
2219 * Add @task, which is a migration target, to @mgctx->tset. This function
2220 * becomes noop if @task doesn't need to be migrated. @task's css_set
2221 * should have been added as a migration source and @task->cg_list will be
2222 * moved from the css_set's tasks list to mg_tasks one.
2224 static void cgroup_migrate_add_task(struct task_struct *task,
2225 struct cgroup_mgctx *mgctx)
2227 struct css_set *cset;
2229 lockdep_assert_held(&css_set_lock);
2231 /* @task either already exited or can't exit until the end */
2232 if (task->flags & PF_EXITING)
2233 return;
2235 /* leave @task alone if post_fork() hasn't linked it yet */
2236 if (list_empty(&task->cg_list))
2237 return;
2239 cset = task_css_set(task);
2240 if (!cset->mg_src_cgrp)
2241 return;
2243 mgctx->tset.nr_tasks++;
2245 list_move_tail(&task->cg_list, &cset->mg_tasks);
2246 if (list_empty(&cset->mg_node))
2247 list_add_tail(&cset->mg_node,
2248 &mgctx->tset.src_csets);
2249 if (list_empty(&cset->mg_dst_cset->mg_node))
2250 list_add_tail(&cset->mg_dst_cset->mg_node,
2251 &mgctx->tset.dst_csets);
2255 * cgroup_taskset_first - reset taskset and return the first task
2256 * @tset: taskset of interest
2257 * @dst_cssp: output variable for the destination css
2259 * @tset iteration is initialized and the first task is returned.
2261 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
2262 struct cgroup_subsys_state **dst_cssp)
2264 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
2265 tset->cur_task = NULL;
2267 return cgroup_taskset_next(tset, dst_cssp);
2271 * cgroup_taskset_next - iterate to the next task in taskset
2272 * @tset: taskset of interest
2273 * @dst_cssp: output variable for the destination css
2275 * Return the next task in @tset. Iteration must have been initialized
2276 * with cgroup_taskset_first().
2278 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
2279 struct cgroup_subsys_state **dst_cssp)
2281 struct css_set *cset = tset->cur_cset;
2282 struct task_struct *task = tset->cur_task;
2284 while (&cset->mg_node != tset->csets) {
2285 if (!task)
2286 task = list_first_entry(&cset->mg_tasks,
2287 struct task_struct, cg_list);
2288 else
2289 task = list_next_entry(task, cg_list);
2291 if (&task->cg_list != &cset->mg_tasks) {
2292 tset->cur_cset = cset;
2293 tset->cur_task = task;
2296 * This function may be called both before and
2297 * after cgroup_taskset_migrate(). The two cases
2298 * can be distinguished by looking at whether @cset
2299 * has its ->mg_dst_cset set.
2301 if (cset->mg_dst_cset)
2302 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2303 else
2304 *dst_cssp = cset->subsys[tset->ssid];
2306 return task;
2309 cset = list_next_entry(cset, mg_node);
2310 task = NULL;
2313 return NULL;
2317 * cgroup_taskset_migrate - migrate a taskset
2318 * @mgctx: migration context
2320 * Migrate tasks in @mgctx as setup by migration preparation functions.
2321 * This function fails iff one of the ->can_attach callbacks fails and
2322 * guarantees that either all or none of the tasks in @mgctx are migrated.
2323 * @mgctx is consumed regardless of success.
2325 static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
2327 struct cgroup_taskset *tset = &mgctx->tset;
2328 struct cgroup_subsys *ss;
2329 struct task_struct *task, *tmp_task;
2330 struct css_set *cset, *tmp_cset;
2331 int ssid, failed_ssid, ret;
2333 /* check that we can legitimately attach to the cgroup */
2334 if (tset->nr_tasks) {
2335 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2336 if (ss->can_attach) {
2337 tset->ssid = ssid;
2338 ret = ss->can_attach(tset);
2339 if (ret) {
2340 failed_ssid = ssid;
2341 goto out_cancel_attach;
2344 } while_each_subsys_mask();
2348 * Now that we're guaranteed success, proceed to move all tasks to
2349 * the new cgroup. There are no failure cases after here, so this
2350 * is the commit point.
2352 spin_lock_irq(&css_set_lock);
2353 list_for_each_entry(cset, &tset->src_csets, mg_node) {
2354 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2355 struct css_set *from_cset = task_css_set(task);
2356 struct css_set *to_cset = cset->mg_dst_cset;
2358 get_css_set(to_cset);
2359 to_cset->nr_tasks++;
2360 css_set_move_task(task, from_cset, to_cset, true);
2361 put_css_set_locked(from_cset);
2362 from_cset->nr_tasks--;
2365 spin_unlock_irq(&css_set_lock);
2368 * Migration is committed, all target tasks are now on dst_csets.
2369 * Nothing is sensitive to fork() after this point. Notify
2370 * controllers that migration is complete.
2372 tset->csets = &tset->dst_csets;
2374 if (tset->nr_tasks) {
2375 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2376 if (ss->attach) {
2377 tset->ssid = ssid;
2378 ss->attach(tset);
2380 } while_each_subsys_mask();
2383 ret = 0;
2384 goto out_release_tset;
2386 out_cancel_attach:
2387 if (tset->nr_tasks) {
2388 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2389 if (ssid == failed_ssid)
2390 break;
2391 if (ss->cancel_attach) {
2392 tset->ssid = ssid;
2393 ss->cancel_attach(tset);
2395 } while_each_subsys_mask();
2397 out_release_tset:
2398 spin_lock_irq(&css_set_lock);
2399 list_splice_init(&tset->dst_csets, &tset->src_csets);
2400 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2401 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2402 list_del_init(&cset->mg_node);
2404 spin_unlock_irq(&css_set_lock);
2407 * Re-initialize the cgroup_taskset structure in case it is reused
2408 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
2409 * iteration.
2411 tset->nr_tasks = 0;
2412 tset->csets = &tset->src_csets;
2413 return ret;
2417 * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
2418 * @dst_cgrp: destination cgroup to test
2420 * On the default hierarchy, except for the mixable, (possible) thread root
2421 * and threaded cgroups, subtree_control must be zero for migration
2422 * destination cgroups with tasks so that child cgroups don't compete
2423 * against tasks.
2425 int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp)
2427 /* v1 doesn't have any restriction */
2428 if (!cgroup_on_dfl(dst_cgrp))
2429 return 0;
2431 /* verify @dst_cgrp can host resources */
2432 if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp))
2433 return -EOPNOTSUPP;
2435 /* mixables don't care */
2436 if (cgroup_is_mixable(dst_cgrp))
2437 return 0;
2440 * If @dst_cgrp is already or can become a thread root or is
2441 * threaded, it doesn't matter.
2443 if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp))
2444 return 0;
2446 /* apply no-internal-process constraint */
2447 if (dst_cgrp->subtree_control)
2448 return -EBUSY;
2450 return 0;
2454 * cgroup_migrate_finish - cleanup after attach
2455 * @mgctx: migration context
2457 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2458 * those functions for details.
2460 void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
2462 LIST_HEAD(preloaded);
2463 struct css_set *cset, *tmp_cset;
2465 lockdep_assert_held(&cgroup_mutex);
2467 spin_lock_irq(&css_set_lock);
2469 list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded);
2470 list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded);
2472 list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) {
2473 cset->mg_src_cgrp = NULL;
2474 cset->mg_dst_cgrp = NULL;
2475 cset->mg_dst_cset = NULL;
2476 list_del_init(&cset->mg_preload_node);
2477 put_css_set_locked(cset);
2480 spin_unlock_irq(&css_set_lock);
2484 * cgroup_migrate_add_src - add a migration source css_set
2485 * @src_cset: the source css_set to add
2486 * @dst_cgrp: the destination cgroup
2487 * @mgctx: migration context
2489 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2490 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
2491 * up by cgroup_migrate_finish().
2493 * This function may be called without holding cgroup_threadgroup_rwsem
2494 * even if the target is a process. Threads may be created and destroyed
2495 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2496 * into play and the preloaded css_sets are guaranteed to cover all
2497 * migrations.
2499 void cgroup_migrate_add_src(struct css_set *src_cset,
2500 struct cgroup *dst_cgrp,
2501 struct cgroup_mgctx *mgctx)
2503 struct cgroup *src_cgrp;
2505 lockdep_assert_held(&cgroup_mutex);
2506 lockdep_assert_held(&css_set_lock);
2509 * If ->dead, @src_set is associated with one or more dead cgroups
2510 * and doesn't contain any migratable tasks. Ignore it early so
2511 * that the rest of migration path doesn't get confused by it.
2513 if (src_cset->dead)
2514 return;
2516 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2518 if (!list_empty(&src_cset->mg_preload_node))
2519 return;
2521 WARN_ON(src_cset->mg_src_cgrp);
2522 WARN_ON(src_cset->mg_dst_cgrp);
2523 WARN_ON(!list_empty(&src_cset->mg_tasks));
2524 WARN_ON(!list_empty(&src_cset->mg_node));
2526 src_cset->mg_src_cgrp = src_cgrp;
2527 src_cset->mg_dst_cgrp = dst_cgrp;
2528 get_css_set(src_cset);
2529 list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets);
2533 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
2534 * @mgctx: migration context
2536 * Tasks are about to be moved and all the source css_sets have been
2537 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2538 * pins all destination css_sets, links each to its source, and append them
2539 * to @mgctx->preloaded_dst_csets.
2541 * This function must be called after cgroup_migrate_add_src() has been
2542 * called on each migration source css_set. After migration is performed
2543 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2544 * @mgctx.
2546 int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx)
2548 struct css_set *src_cset, *tmp_cset;
2550 lockdep_assert_held(&cgroup_mutex);
2552 /* look up the dst cset for each src cset and link it to src */
2553 list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
2554 mg_preload_node) {
2555 struct css_set *dst_cset;
2556 struct cgroup_subsys *ss;
2557 int ssid;
2559 dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
2560 if (!dst_cset)
2561 goto err;
2563 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
2566 * If src cset equals dst, it's noop. Drop the src.
2567 * cgroup_migrate() will skip the cset too. Note that we
2568 * can't handle src == dst as some nodes are used by both.
2570 if (src_cset == dst_cset) {
2571 src_cset->mg_src_cgrp = NULL;
2572 src_cset->mg_dst_cgrp = NULL;
2573 list_del_init(&src_cset->mg_preload_node);
2574 put_css_set(src_cset);
2575 put_css_set(dst_cset);
2576 continue;
2579 src_cset->mg_dst_cset = dst_cset;
2581 if (list_empty(&dst_cset->mg_preload_node))
2582 list_add_tail(&dst_cset->mg_preload_node,
2583 &mgctx->preloaded_dst_csets);
2584 else
2585 put_css_set(dst_cset);
2587 for_each_subsys(ss, ssid)
2588 if (src_cset->subsys[ssid] != dst_cset->subsys[ssid])
2589 mgctx->ss_mask |= 1 << ssid;
2592 return 0;
2593 err:
2594 cgroup_migrate_finish(mgctx);
2595 return -ENOMEM;
2599 * cgroup_migrate - migrate a process or task to a cgroup
2600 * @leader: the leader of the process or the task to migrate
2601 * @threadgroup: whether @leader points to the whole process or a single task
2602 * @mgctx: migration context
2604 * Migrate a process or task denoted by @leader. If migrating a process,
2605 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2606 * responsible for invoking cgroup_migrate_add_src() and
2607 * cgroup_migrate_prepare_dst() on the targets before invoking this
2608 * function and following up with cgroup_migrate_finish().
2610 * As long as a controller's ->can_attach() doesn't fail, this function is
2611 * guaranteed to succeed. This means that, excluding ->can_attach()
2612 * failure, when migrating multiple targets, the success or failure can be
2613 * decided for all targets by invoking group_migrate_prepare_dst() before
2614 * actually starting migrating.
2616 int cgroup_migrate(struct task_struct *leader, bool threadgroup,
2617 struct cgroup_mgctx *mgctx)
2619 struct task_struct *task;
2622 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2623 * already PF_EXITING could be freed from underneath us unless we
2624 * take an rcu_read_lock.
2626 spin_lock_irq(&css_set_lock);
2627 rcu_read_lock();
2628 task = leader;
2629 do {
2630 cgroup_migrate_add_task(task, mgctx);
2631 if (!threadgroup)
2632 break;
2633 } while_each_thread(leader, task);
2634 rcu_read_unlock();
2635 spin_unlock_irq(&css_set_lock);
2637 return cgroup_migrate_execute(mgctx);
2641 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2642 * @dst_cgrp: the cgroup to attach to
2643 * @leader: the task or the leader of the threadgroup to be attached
2644 * @threadgroup: attach the whole threadgroup?
2646 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
2648 int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
2649 bool threadgroup)
2651 DEFINE_CGROUP_MGCTX(mgctx);
2652 struct task_struct *task;
2653 int ret;
2655 ret = cgroup_migrate_vet_dst(dst_cgrp);
2656 if (ret)
2657 return ret;
2659 /* look up all src csets */
2660 spin_lock_irq(&css_set_lock);
2661 rcu_read_lock();
2662 task = leader;
2663 do {
2664 cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
2665 if (!threadgroup)
2666 break;
2667 } while_each_thread(leader, task);
2668 rcu_read_unlock();
2669 spin_unlock_irq(&css_set_lock);
2671 /* prepare dst csets and commit */
2672 ret = cgroup_migrate_prepare_dst(&mgctx);
2673 if (!ret)
2674 ret = cgroup_migrate(leader, threadgroup, &mgctx);
2676 cgroup_migrate_finish(&mgctx);
2678 if (!ret)
2679 TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup);
2681 return ret;
2684 struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup)
2685 __acquires(&cgroup_threadgroup_rwsem)
2687 struct task_struct *tsk;
2688 pid_t pid;
2690 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2691 return ERR_PTR(-EINVAL);
2693 percpu_down_write(&cgroup_threadgroup_rwsem);
2695 rcu_read_lock();
2696 if (pid) {
2697 tsk = find_task_by_vpid(pid);
2698 if (!tsk) {
2699 tsk = ERR_PTR(-ESRCH);
2700 goto out_unlock_threadgroup;
2702 } else {
2703 tsk = current;
2706 if (threadgroup)
2707 tsk = tsk->group_leader;
2710 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2711 * If userland migrates such a kthread to a non-root cgroup, it can
2712 * become trapped in a cpuset, or RT kthread may be born in a
2713 * cgroup with no rt_runtime allocated. Just say no.
2715 if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) {
2716 tsk = ERR_PTR(-EINVAL);
2717 goto out_unlock_threadgroup;
2720 get_task_struct(tsk);
2721 goto out_unlock_rcu;
2723 out_unlock_threadgroup:
2724 percpu_up_write(&cgroup_threadgroup_rwsem);
2725 out_unlock_rcu:
2726 rcu_read_unlock();
2727 return tsk;
2730 void cgroup_procs_write_finish(struct task_struct *task)
2731 __releases(&cgroup_threadgroup_rwsem)
2733 struct cgroup_subsys *ss;
2734 int ssid;
2736 /* release reference from cgroup_procs_write_start() */
2737 put_task_struct(task);
2739 percpu_up_write(&cgroup_threadgroup_rwsem);
2740 for_each_subsys(ss, ssid)
2741 if (ss->post_attach)
2742 ss->post_attach();
2745 static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
2747 struct cgroup_subsys *ss;
2748 bool printed = false;
2749 int ssid;
2751 do_each_subsys_mask(ss, ssid, ss_mask) {
2752 if (printed)
2753 seq_putc(seq, ' ');
2754 seq_printf(seq, "%s", ss->name);
2755 printed = true;
2756 } while_each_subsys_mask();
2757 if (printed)
2758 seq_putc(seq, '\n');
2761 /* show controllers which are enabled from the parent */
2762 static int cgroup_controllers_show(struct seq_file *seq, void *v)
2764 struct cgroup *cgrp = seq_css(seq)->cgroup;
2766 cgroup_print_ss_mask(seq, cgroup_control(cgrp));
2767 return 0;
2770 /* show controllers which are enabled for a given cgroup's children */
2771 static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2773 struct cgroup *cgrp = seq_css(seq)->cgroup;
2775 cgroup_print_ss_mask(seq, cgrp->subtree_control);
2776 return 0;
2780 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2781 * @cgrp: root of the subtree to update csses for
2783 * @cgrp's control masks have changed and its subtree's css associations
2784 * need to be updated accordingly. This function looks up all css_sets
2785 * which are attached to the subtree, creates the matching updated css_sets
2786 * and migrates the tasks to the new ones.
2788 static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2790 DEFINE_CGROUP_MGCTX(mgctx);
2791 struct cgroup_subsys_state *d_css;
2792 struct cgroup *dsct;
2793 struct css_set *src_cset;
2794 int ret;
2796 lockdep_assert_held(&cgroup_mutex);
2798 percpu_down_write(&cgroup_threadgroup_rwsem);
2800 /* look up all csses currently attached to @cgrp's subtree */
2801 spin_lock_irq(&css_set_lock);
2802 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2803 struct cgrp_cset_link *link;
2805 list_for_each_entry(link, &dsct->cset_links, cset_link)
2806 cgroup_migrate_add_src(link->cset, dsct, &mgctx);
2808 spin_unlock_irq(&css_set_lock);
2810 /* NULL dst indicates self on default hierarchy */
2811 ret = cgroup_migrate_prepare_dst(&mgctx);
2812 if (ret)
2813 goto out_finish;
2815 spin_lock_irq(&css_set_lock);
2816 list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) {
2817 struct task_struct *task, *ntask;
2819 /* all tasks in src_csets need to be migrated */
2820 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
2821 cgroup_migrate_add_task(task, &mgctx);
2823 spin_unlock_irq(&css_set_lock);
2825 ret = cgroup_migrate_execute(&mgctx);
2826 out_finish:
2827 cgroup_migrate_finish(&mgctx);
2828 percpu_up_write(&cgroup_threadgroup_rwsem);
2829 return ret;
2833 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
2834 * @cgrp: root of the target subtree
2836 * Because css offlining is asynchronous, userland may try to re-enable a
2837 * controller while the previous css is still around. This function grabs
2838 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
2840 void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
2841 __acquires(&cgroup_mutex)
2843 struct cgroup *dsct;
2844 struct cgroup_subsys_state *d_css;
2845 struct cgroup_subsys *ss;
2846 int ssid;
2848 restart:
2849 mutex_lock(&cgroup_mutex);
2851 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2852 for_each_subsys(ss, ssid) {
2853 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2854 DEFINE_WAIT(wait);
2856 if (!css || !percpu_ref_is_dying(&css->refcnt))
2857 continue;
2859 cgroup_get_live(dsct);
2860 prepare_to_wait(&dsct->offline_waitq, &wait,
2861 TASK_UNINTERRUPTIBLE);
2863 mutex_unlock(&cgroup_mutex);
2864 schedule();
2865 finish_wait(&dsct->offline_waitq, &wait);
2867 cgroup_put(dsct);
2868 goto restart;
2874 * cgroup_save_control - save control masks and dom_cgrp of a subtree
2875 * @cgrp: root of the target subtree
2877 * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the
2878 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
2879 * itself.
2881 static void cgroup_save_control(struct cgroup *cgrp)
2883 struct cgroup *dsct;
2884 struct cgroup_subsys_state *d_css;
2886 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2887 dsct->old_subtree_control = dsct->subtree_control;
2888 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
2889 dsct->old_dom_cgrp = dsct->dom_cgrp;
2894 * cgroup_propagate_control - refresh control masks of a subtree
2895 * @cgrp: root of the target subtree
2897 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2898 * ->subtree_control and propagate controller availability through the
2899 * subtree so that descendants don't have unavailable controllers enabled.
2901 static void cgroup_propagate_control(struct cgroup *cgrp)
2903 struct cgroup *dsct;
2904 struct cgroup_subsys_state *d_css;
2906 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2907 dsct->subtree_control &= cgroup_control(dsct);
2908 dsct->subtree_ss_mask =
2909 cgroup_calc_subtree_ss_mask(dsct->subtree_control,
2910 cgroup_ss_mask(dsct));
2915 * cgroup_restore_control - restore control masks and dom_cgrp of a subtree
2916 * @cgrp: root of the target subtree
2918 * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the
2919 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
2920 * itself.
2922 static void cgroup_restore_control(struct cgroup *cgrp)
2924 struct cgroup *dsct;
2925 struct cgroup_subsys_state *d_css;
2927 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2928 dsct->subtree_control = dsct->old_subtree_control;
2929 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
2930 dsct->dom_cgrp = dsct->old_dom_cgrp;
2934 static bool css_visible(struct cgroup_subsys_state *css)
2936 struct cgroup_subsys *ss = css->ss;
2937 struct cgroup *cgrp = css->cgroup;
2939 if (cgroup_control(cgrp) & (1 << ss->id))
2940 return true;
2941 if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
2942 return false;
2943 return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
2947 * cgroup_apply_control_enable - enable or show csses according to control
2948 * @cgrp: root of the target subtree
2950 * Walk @cgrp's subtree and create new csses or make the existing ones
2951 * visible. A css is created invisible if it's being implicitly enabled
2952 * through dependency. An invisible css is made visible when the userland
2953 * explicitly enables it.
2955 * Returns 0 on success, -errno on failure. On failure, csses which have
2956 * been processed already aren't cleaned up. The caller is responsible for
2957 * cleaning up with cgroup_apply_control_disable().
2959 static int cgroup_apply_control_enable(struct cgroup *cgrp)
2961 struct cgroup *dsct;
2962 struct cgroup_subsys_state *d_css;
2963 struct cgroup_subsys *ss;
2964 int ssid, ret;
2966 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2967 for_each_subsys(ss, ssid) {
2968 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2970 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
2972 if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
2973 continue;
2975 if (!css) {
2976 css = css_create(dsct, ss);
2977 if (IS_ERR(css))
2978 return PTR_ERR(css);
2981 if (css_visible(css)) {
2982 ret = css_populate_dir(css);
2983 if (ret)
2984 return ret;
2989 return 0;
2993 * cgroup_apply_control_disable - kill or hide csses according to control
2994 * @cgrp: root of the target subtree
2996 * Walk @cgrp's subtree and kill and hide csses so that they match
2997 * cgroup_ss_mask() and cgroup_visible_mask().
2999 * A css is hidden when the userland requests it to be disabled while other
3000 * subsystems are still depending on it. The css must not actively control
3001 * resources and be in the vanilla state if it's made visible again later.
3002 * Controllers which may be depended upon should provide ->css_reset() for
3003 * this purpose.
3005 static void cgroup_apply_control_disable(struct cgroup *cgrp)
3007 struct cgroup *dsct;
3008 struct cgroup_subsys_state *d_css;
3009 struct cgroup_subsys *ss;
3010 int ssid;
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);
3016 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
3018 if (!css)
3019 continue;
3021 if (css->parent &&
3022 !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
3023 kill_css(css);
3024 } else if (!css_visible(css)) {
3025 css_clear_dir(css);
3026 if (ss->css_reset)
3027 ss->css_reset(css);
3034 * cgroup_apply_control - apply control mask updates to the subtree
3035 * @cgrp: root of the target subtree
3037 * subsystems can be enabled and disabled in a subtree using the following
3038 * steps.
3040 * 1. Call cgroup_save_control() to stash the current state.
3041 * 2. Update ->subtree_control masks in the subtree as desired.
3042 * 3. Call cgroup_apply_control() to apply the changes.
3043 * 4. Optionally perform other related operations.
3044 * 5. Call cgroup_finalize_control() to finish up.
3046 * This function implements step 3 and propagates the mask changes
3047 * throughout @cgrp's subtree, updates csses accordingly and perform
3048 * process migrations.
3050 static int cgroup_apply_control(struct cgroup *cgrp)
3052 int ret;
3054 cgroup_propagate_control(cgrp);
3056 ret = cgroup_apply_control_enable(cgrp);
3057 if (ret)
3058 return ret;
3061 * At this point, cgroup_e_css_by_mask() results reflect the new csses
3062 * making the following cgroup_update_dfl_csses() properly update
3063 * css associations of all tasks in the subtree.
3065 ret = cgroup_update_dfl_csses(cgrp);
3066 if (ret)
3067 return ret;
3069 return 0;
3073 * cgroup_finalize_control - finalize control mask update
3074 * @cgrp: root of the target subtree
3075 * @ret: the result of the update
3077 * Finalize control mask update. See cgroup_apply_control() for more info.
3079 static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
3081 if (ret) {
3082 cgroup_restore_control(cgrp);
3083 cgroup_propagate_control(cgrp);
3086 cgroup_apply_control_disable(cgrp);
3089 static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable)
3091 u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask;
3093 /* if nothing is getting enabled, nothing to worry about */
3094 if (!enable)
3095 return 0;
3097 /* can @cgrp host any resources? */
3098 if (!cgroup_is_valid_domain(cgrp->dom_cgrp))
3099 return -EOPNOTSUPP;
3101 /* mixables don't care */
3102 if (cgroup_is_mixable(cgrp))
3103 return 0;
3105 if (domain_enable) {
3106 /* can't enable domain controllers inside a thread subtree */
3107 if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3108 return -EOPNOTSUPP;
3109 } else {
3111 * Threaded controllers can handle internal competitions
3112 * and are always allowed inside a (prospective) thread
3113 * subtree.
3115 if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp))
3116 return 0;
3120 * Controllers can't be enabled for a cgroup with tasks to avoid
3121 * child cgroups competing against tasks.
3123 if (cgroup_has_tasks(cgrp))
3124 return -EBUSY;
3126 return 0;
3129 /* change the enabled child controllers for a cgroup in the default hierarchy */
3130 static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
3131 char *buf, size_t nbytes,
3132 loff_t off)
3134 u16 enable = 0, disable = 0;
3135 struct cgroup *cgrp, *child;
3136 struct cgroup_subsys *ss;
3137 char *tok;
3138 int ssid, ret;
3141 * Parse input - space separated list of subsystem names prefixed
3142 * with either + or -.
3144 buf = strstrip(buf);
3145 while ((tok = strsep(&buf, " "))) {
3146 if (tok[0] == '\0')
3147 continue;
3148 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
3149 if (!cgroup_ssid_enabled(ssid) ||
3150 strcmp(tok + 1, ss->name))
3151 continue;
3153 if (*tok == '+') {
3154 enable |= 1 << ssid;
3155 disable &= ~(1 << ssid);
3156 } else if (*tok == '-') {
3157 disable |= 1 << ssid;
3158 enable &= ~(1 << ssid);
3159 } else {
3160 return -EINVAL;
3162 break;
3163 } while_each_subsys_mask();
3164 if (ssid == CGROUP_SUBSYS_COUNT)
3165 return -EINVAL;
3168 cgrp = cgroup_kn_lock_live(of->kn, true);
3169 if (!cgrp)
3170 return -ENODEV;
3172 for_each_subsys(ss, ssid) {
3173 if (enable & (1 << ssid)) {
3174 if (cgrp->subtree_control & (1 << ssid)) {
3175 enable &= ~(1 << ssid);
3176 continue;
3179 if (!(cgroup_control(cgrp) & (1 << ssid))) {
3180 ret = -ENOENT;
3181 goto out_unlock;
3183 } else if (disable & (1 << ssid)) {
3184 if (!(cgrp->subtree_control & (1 << ssid))) {
3185 disable &= ~(1 << ssid);
3186 continue;
3189 /* a child has it enabled? */
3190 cgroup_for_each_live_child(child, cgrp) {
3191 if (child->subtree_control & (1 << ssid)) {
3192 ret = -EBUSY;
3193 goto out_unlock;
3199 if (!enable && !disable) {
3200 ret = 0;
3201 goto out_unlock;
3204 ret = cgroup_vet_subtree_control_enable(cgrp, enable);
3205 if (ret)
3206 goto out_unlock;
3208 /* save and update control masks and prepare csses */
3209 cgroup_save_control(cgrp);
3211 cgrp->subtree_control |= enable;
3212 cgrp->subtree_control &= ~disable;
3214 ret = cgroup_apply_control(cgrp);
3215 cgroup_finalize_control(cgrp, ret);
3216 if (ret)
3217 goto out_unlock;
3219 kernfs_activate(cgrp->kn);
3220 out_unlock:
3221 cgroup_kn_unlock(of->kn);
3222 return ret ?: nbytes;
3226 * cgroup_enable_threaded - make @cgrp threaded
3227 * @cgrp: the target cgroup
3229 * Called when "threaded" is written to the cgroup.type interface file and
3230 * tries to make @cgrp threaded and join the parent's resource domain.
3231 * This function is never called on the root cgroup as cgroup.type doesn't
3232 * exist on it.
3234 static int cgroup_enable_threaded(struct cgroup *cgrp)
3236 struct cgroup *parent = cgroup_parent(cgrp);
3237 struct cgroup *dom_cgrp = parent->dom_cgrp;
3238 struct cgroup *dsct;
3239 struct cgroup_subsys_state *d_css;
3240 int ret;
3242 lockdep_assert_held(&cgroup_mutex);
3244 /* noop if already threaded */
3245 if (cgroup_is_threaded(cgrp))
3246 return 0;
3249 * If @cgroup is populated or has domain controllers enabled, it
3250 * can't be switched. While the below cgroup_can_be_thread_root()
3251 * test can catch the same conditions, that's only when @parent is
3252 * not mixable, so let's check it explicitly.
3254 if (cgroup_is_populated(cgrp) ||
3255 cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
3256 return -EOPNOTSUPP;
3258 /* we're joining the parent's domain, ensure its validity */
3259 if (!cgroup_is_valid_domain(dom_cgrp) ||
3260 !cgroup_can_be_thread_root(dom_cgrp))
3261 return -EOPNOTSUPP;
3264 * The following shouldn't cause actual migrations and should
3265 * always succeed.
3267 cgroup_save_control(cgrp);
3269 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp)
3270 if (dsct == cgrp || cgroup_is_threaded(dsct))
3271 dsct->dom_cgrp = dom_cgrp;
3273 ret = cgroup_apply_control(cgrp);
3274 if (!ret)
3275 parent->nr_threaded_children++;
3277 cgroup_finalize_control(cgrp, ret);
3278 return ret;
3281 static int cgroup_type_show(struct seq_file *seq, void *v)
3283 struct cgroup *cgrp = seq_css(seq)->cgroup;
3285 if (cgroup_is_threaded(cgrp))
3286 seq_puts(seq, "threaded\n");
3287 else if (!cgroup_is_valid_domain(cgrp))
3288 seq_puts(seq, "domain invalid\n");
3289 else if (cgroup_is_thread_root(cgrp))
3290 seq_puts(seq, "domain threaded\n");
3291 else
3292 seq_puts(seq, "domain\n");
3294 return 0;
3297 static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf,
3298 size_t nbytes, loff_t off)
3300 struct cgroup *cgrp;
3301 int ret;
3303 /* only switching to threaded mode is supported */
3304 if (strcmp(strstrip(buf), "threaded"))
3305 return -EINVAL;
3307 cgrp = cgroup_kn_lock_live(of->kn, false);
3308 if (!cgrp)
3309 return -ENOENT;
3311 /* threaded can only be enabled */
3312 ret = cgroup_enable_threaded(cgrp);
3314 cgroup_kn_unlock(of->kn);
3315 return ret ?: nbytes;
3318 static int cgroup_max_descendants_show(struct seq_file *seq, void *v)
3320 struct cgroup *cgrp = seq_css(seq)->cgroup;
3321 int descendants = READ_ONCE(cgrp->max_descendants);
3323 if (descendants == INT_MAX)
3324 seq_puts(seq, "max\n");
3325 else
3326 seq_printf(seq, "%d\n", descendants);
3328 return 0;
3331 static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of,
3332 char *buf, size_t nbytes, loff_t off)
3334 struct cgroup *cgrp;
3335 int descendants;
3336 ssize_t ret;
3338 buf = strstrip(buf);
3339 if (!strcmp(buf, "max")) {
3340 descendants = INT_MAX;
3341 } else {
3342 ret = kstrtoint(buf, 0, &descendants);
3343 if (ret)
3344 return ret;
3347 if (descendants < 0)
3348 return -ERANGE;
3350 cgrp = cgroup_kn_lock_live(of->kn, false);
3351 if (!cgrp)
3352 return -ENOENT;
3354 cgrp->max_descendants = descendants;
3356 cgroup_kn_unlock(of->kn);
3358 return nbytes;
3361 static int cgroup_max_depth_show(struct seq_file *seq, void *v)
3363 struct cgroup *cgrp = seq_css(seq)->cgroup;
3364 int depth = READ_ONCE(cgrp->max_depth);
3366 if (depth == INT_MAX)
3367 seq_puts(seq, "max\n");
3368 else
3369 seq_printf(seq, "%d\n", depth);
3371 return 0;
3374 static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of,
3375 char *buf, size_t nbytes, loff_t off)
3377 struct cgroup *cgrp;
3378 ssize_t ret;
3379 int depth;
3381 buf = strstrip(buf);
3382 if (!strcmp(buf, "max")) {
3383 depth = INT_MAX;
3384 } else {
3385 ret = kstrtoint(buf, 0, &depth);
3386 if (ret)
3387 return ret;
3390 if (depth < 0)
3391 return -ERANGE;
3393 cgrp = cgroup_kn_lock_live(of->kn, false);
3394 if (!cgrp)
3395 return -ENOENT;
3397 cgrp->max_depth = depth;
3399 cgroup_kn_unlock(of->kn);
3401 return nbytes;
3404 static int cgroup_events_show(struct seq_file *seq, void *v)
3406 seq_printf(seq, "populated %d\n",
3407 cgroup_is_populated(seq_css(seq)->cgroup));
3408 return 0;
3411 static int cgroup_stat_show(struct seq_file *seq, void *v)
3413 struct cgroup *cgroup = seq_css(seq)->cgroup;
3415 seq_printf(seq, "nr_descendants %d\n",
3416 cgroup->nr_descendants);
3417 seq_printf(seq, "nr_dying_descendants %d\n",
3418 cgroup->nr_dying_descendants);
3420 return 0;
3423 static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq,
3424 struct cgroup *cgrp, int ssid)
3426 struct cgroup_subsys *ss = cgroup_subsys[ssid];
3427 struct cgroup_subsys_state *css;
3428 int ret;
3430 if (!ss->css_extra_stat_show)
3431 return 0;
3433 css = cgroup_tryget_css(cgrp, ss);
3434 if (!css)
3435 return 0;
3437 ret = ss->css_extra_stat_show(seq, css);
3438 css_put(css);
3439 return ret;
3442 static int cpu_stat_show(struct seq_file *seq, void *v)
3444 struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup;
3445 int ret = 0;
3447 cgroup_base_stat_cputime_show(seq);
3448 #ifdef CONFIG_CGROUP_SCHED
3449 ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id);
3450 #endif
3451 return ret;
3454 #ifdef CONFIG_PSI
3455 static int cgroup_io_pressure_show(struct seq_file *seq, void *v)
3457 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_IO);
3459 static int cgroup_memory_pressure_show(struct seq_file *seq, void *v)
3461 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_MEM);
3463 static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v)
3465 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_CPU);
3467 #endif
3469 static int cgroup_file_open(struct kernfs_open_file *of)
3471 struct cftype *cft = of->kn->priv;
3473 if (cft->open)
3474 return cft->open(of);
3475 return 0;
3478 static void cgroup_file_release(struct kernfs_open_file *of)
3480 struct cftype *cft = of->kn->priv;
3482 if (cft->release)
3483 cft->release(of);
3486 static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
3487 size_t nbytes, loff_t off)
3489 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
3490 struct cgroup *cgrp = of->kn->parent->priv;
3491 struct cftype *cft = of->kn->priv;
3492 struct cgroup_subsys_state *css;
3493 int ret;
3496 * If namespaces are delegation boundaries, disallow writes to
3497 * files in an non-init namespace root from inside the namespace
3498 * except for the files explicitly marked delegatable -
3499 * cgroup.procs and cgroup.subtree_control.
3501 if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) &&
3502 !(cft->flags & CFTYPE_NS_DELEGATABLE) &&
3503 ns != &init_cgroup_ns && ns->root_cset->dfl_cgrp == cgrp)
3504 return -EPERM;
3506 if (cft->write)
3507 return cft->write(of, buf, nbytes, off);
3510 * kernfs guarantees that a file isn't deleted with operations in
3511 * flight, which means that the matching css is and stays alive and
3512 * doesn't need to be pinned. The RCU locking is not necessary
3513 * either. It's just for the convenience of using cgroup_css().
3515 rcu_read_lock();
3516 css = cgroup_css(cgrp, cft->ss);
3517 rcu_read_unlock();
3519 if (cft->write_u64) {
3520 unsigned long long v;
3521 ret = kstrtoull(buf, 0, &v);
3522 if (!ret)
3523 ret = cft->write_u64(css, cft, v);
3524 } else if (cft->write_s64) {
3525 long long v;
3526 ret = kstrtoll(buf, 0, &v);
3527 if (!ret)
3528 ret = cft->write_s64(css, cft, v);
3529 } else {
3530 ret = -EINVAL;
3533 return ret ?: nbytes;
3536 static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
3538 return seq_cft(seq)->seq_start(seq, ppos);
3541 static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
3543 return seq_cft(seq)->seq_next(seq, v, ppos);
3546 static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
3548 if (seq_cft(seq)->seq_stop)
3549 seq_cft(seq)->seq_stop(seq, v);
3552 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3554 struct cftype *cft = seq_cft(m);
3555 struct cgroup_subsys_state *css = seq_css(m);
3557 if (cft->seq_show)
3558 return cft->seq_show(m, arg);
3560 if (cft->read_u64)
3561 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3562 else if (cft->read_s64)
3563 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3564 else
3565 return -EINVAL;
3566 return 0;
3569 static struct kernfs_ops cgroup_kf_single_ops = {
3570 .atomic_write_len = PAGE_SIZE,
3571 .open = cgroup_file_open,
3572 .release = cgroup_file_release,
3573 .write = cgroup_file_write,
3574 .seq_show = cgroup_seqfile_show,
3577 static struct kernfs_ops cgroup_kf_ops = {
3578 .atomic_write_len = PAGE_SIZE,
3579 .open = cgroup_file_open,
3580 .release = cgroup_file_release,
3581 .write = cgroup_file_write,
3582 .seq_start = cgroup_seqfile_start,
3583 .seq_next = cgroup_seqfile_next,
3584 .seq_stop = cgroup_seqfile_stop,
3585 .seq_show = cgroup_seqfile_show,
3588 /* set uid and gid of cgroup dirs and files to that of the creator */
3589 static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3591 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3592 .ia_uid = current_fsuid(),
3593 .ia_gid = current_fsgid(), };
3595 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3596 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3597 return 0;
3599 return kernfs_setattr(kn, &iattr);
3602 static void cgroup_file_notify_timer(struct timer_list *timer)
3604 cgroup_file_notify(container_of(timer, struct cgroup_file,
3605 notify_timer));
3608 static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3609 struct cftype *cft)
3611 char name[CGROUP_FILE_NAME_MAX];
3612 struct kernfs_node *kn;
3613 struct lock_class_key *key = NULL;
3614 int ret;
3616 #ifdef CONFIG_DEBUG_LOCK_ALLOC
3617 key = &cft->lockdep_key;
3618 #endif
3619 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3620 cgroup_file_mode(cft),
3621 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
3622 0, cft->kf_ops, cft,
3623 NULL, key);
3624 if (IS_ERR(kn))
3625 return PTR_ERR(kn);
3627 ret = cgroup_kn_set_ugid(kn);
3628 if (ret) {
3629 kernfs_remove(kn);
3630 return ret;
3633 if (cft->file_offset) {
3634 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3636 timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
3638 spin_lock_irq(&cgroup_file_kn_lock);
3639 cfile->kn = kn;
3640 spin_unlock_irq(&cgroup_file_kn_lock);
3643 return 0;
3647 * cgroup_addrm_files - add or remove files to a cgroup directory
3648 * @css: the target css
3649 * @cgrp: the target cgroup (usually css->cgroup)
3650 * @cfts: array of cftypes to be added
3651 * @is_add: whether to add or remove
3653 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
3654 * For removals, this function never fails.
3656 static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3657 struct cgroup *cgrp, struct cftype cfts[],
3658 bool is_add)
3660 struct cftype *cft, *cft_end = NULL;
3661 int ret = 0;
3663 lockdep_assert_held(&cgroup_mutex);
3665 restart:
3666 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
3667 /* does cft->flags tell us to skip this file on @cgrp? */
3668 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
3669 continue;
3670 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
3671 continue;
3672 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
3673 continue;
3674 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
3675 continue;
3676 if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug)
3677 continue;
3678 if (is_add) {
3679 ret = cgroup_add_file(css, cgrp, cft);
3680 if (ret) {
3681 pr_warn("%s: failed to add %s, err=%d\n",
3682 __func__, cft->name, ret);
3683 cft_end = cft;
3684 is_add = false;
3685 goto restart;
3687 } else {
3688 cgroup_rm_file(cgrp, cft);
3691 return ret;
3694 static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
3696 struct cgroup_subsys *ss = cfts[0].ss;
3697 struct cgroup *root = &ss->root->cgrp;
3698 struct cgroup_subsys_state *css;
3699 int ret = 0;
3701 lockdep_assert_held(&cgroup_mutex);
3703 /* add/rm files for all cgroups created before */
3704 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
3705 struct cgroup *cgrp = css->cgroup;
3707 if (!(css->flags & CSS_VISIBLE))
3708 continue;
3710 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
3711 if (ret)
3712 break;
3715 if (is_add && !ret)
3716 kernfs_activate(root->kn);
3717 return ret;
3720 static void cgroup_exit_cftypes(struct cftype *cfts)
3722 struct cftype *cft;
3724 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3725 /* free copy for custom atomic_write_len, see init_cftypes() */
3726 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3727 kfree(cft->kf_ops);
3728 cft->kf_ops = NULL;
3729 cft->ss = NULL;
3731 /* revert flags set by cgroup core while adding @cfts */
3732 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
3736 static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3738 struct cftype *cft;
3740 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3741 struct kernfs_ops *kf_ops;
3743 WARN_ON(cft->ss || cft->kf_ops);
3745 if (cft->seq_start)
3746 kf_ops = &cgroup_kf_ops;
3747 else
3748 kf_ops = &cgroup_kf_single_ops;
3751 * Ugh... if @cft wants a custom max_write_len, we need to
3752 * make a copy of kf_ops to set its atomic_write_len.
3754 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3755 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3756 if (!kf_ops) {
3757 cgroup_exit_cftypes(cfts);
3758 return -ENOMEM;
3760 kf_ops->atomic_write_len = cft->max_write_len;
3763 cft->kf_ops = kf_ops;
3764 cft->ss = ss;
3767 return 0;
3770 static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3772 lockdep_assert_held(&cgroup_mutex);
3774 if (!cfts || !cfts[0].ss)
3775 return -ENOENT;
3777 list_del(&cfts->node);
3778 cgroup_apply_cftypes(cfts, false);
3779 cgroup_exit_cftypes(cfts);
3780 return 0;
3784 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3785 * @cfts: zero-length name terminated array of cftypes
3787 * Unregister @cfts. Files described by @cfts are removed from all
3788 * existing cgroups and all future cgroups won't have them either. This
3789 * function can be called anytime whether @cfts' subsys is attached or not.
3791 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3792 * registered.
3794 int cgroup_rm_cftypes(struct cftype *cfts)
3796 int ret;
3798 mutex_lock(&cgroup_mutex);
3799 ret = cgroup_rm_cftypes_locked(cfts);
3800 mutex_unlock(&cgroup_mutex);
3801 return ret;
3805 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3806 * @ss: target cgroup subsystem
3807 * @cfts: zero-length name terminated array of cftypes
3809 * Register @cfts to @ss. Files described by @cfts are created for all
3810 * existing cgroups to which @ss is attached and all future cgroups will
3811 * have them too. This function can be called anytime whether @ss is
3812 * attached or not.
3814 * Returns 0 on successful registration, -errno on failure. Note that this
3815 * function currently returns 0 as long as @cfts registration is successful
3816 * even if some file creation attempts on existing cgroups fail.
3818 static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3820 int ret;
3822 if (!cgroup_ssid_enabled(ss->id))
3823 return 0;
3825 if (!cfts || cfts[0].name[0] == '\0')
3826 return 0;
3828 ret = cgroup_init_cftypes(ss, cfts);
3829 if (ret)
3830 return ret;
3832 mutex_lock(&cgroup_mutex);
3834 list_add_tail(&cfts->node, &ss->cfts);
3835 ret = cgroup_apply_cftypes(cfts, true);
3836 if (ret)
3837 cgroup_rm_cftypes_locked(cfts);
3839 mutex_unlock(&cgroup_mutex);
3840 return ret;
3844 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3845 * @ss: target cgroup subsystem
3846 * @cfts: zero-length name terminated array of cftypes
3848 * Similar to cgroup_add_cftypes() but the added files are only used for
3849 * the default hierarchy.
3851 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3853 struct cftype *cft;
3855 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3856 cft->flags |= __CFTYPE_ONLY_ON_DFL;
3857 return cgroup_add_cftypes(ss, cfts);
3861 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3862 * @ss: target cgroup subsystem
3863 * @cfts: zero-length name terminated array of cftypes
3865 * Similar to cgroup_add_cftypes() but the added files are only used for
3866 * the legacy hierarchies.
3868 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3870 struct cftype *cft;
3872 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3873 cft->flags |= __CFTYPE_NOT_ON_DFL;
3874 return cgroup_add_cftypes(ss, cfts);
3878 * cgroup_file_notify - generate a file modified event for a cgroup_file
3879 * @cfile: target cgroup_file
3881 * @cfile must have been obtained by setting cftype->file_offset.
3883 void cgroup_file_notify(struct cgroup_file *cfile)
3885 unsigned long flags;
3887 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
3888 if (cfile->kn) {
3889 unsigned long last = cfile->notified_at;
3890 unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
3892 if (time_in_range(jiffies, last, next)) {
3893 timer_reduce(&cfile->notify_timer, next);
3894 } else {
3895 kernfs_notify(cfile->kn);
3896 cfile->notified_at = jiffies;
3899 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3903 * css_next_child - find the next child of a given css
3904 * @pos: the current position (%NULL to initiate traversal)
3905 * @parent: css whose children to walk
3907 * This function returns the next child of @parent and should be called
3908 * under either cgroup_mutex or RCU read lock. The only requirement is
3909 * that @parent and @pos are accessible. The next sibling is guaranteed to
3910 * be returned regardless of their states.
3912 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3913 * css which finished ->css_online() is guaranteed to be visible in the
3914 * future iterations and will stay visible until the last reference is put.
3915 * A css which hasn't finished ->css_online() or already finished
3916 * ->css_offline() may show up during traversal. It's each subsystem's
3917 * responsibility to synchronize against on/offlining.
3919 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3920 struct cgroup_subsys_state *parent)
3922 struct cgroup_subsys_state *next;
3924 cgroup_assert_mutex_or_rcu_locked();
3927 * @pos could already have been unlinked from the sibling list.
3928 * Once a cgroup is removed, its ->sibling.next is no longer
3929 * updated when its next sibling changes. CSS_RELEASED is set when
3930 * @pos is taken off list, at which time its next pointer is valid,
3931 * and, as releases are serialized, the one pointed to by the next
3932 * pointer is guaranteed to not have started release yet. This
3933 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3934 * critical section, the one pointed to by its next pointer is
3935 * guaranteed to not have finished its RCU grace period even if we
3936 * have dropped rcu_read_lock() inbetween iterations.
3938 * If @pos has CSS_RELEASED set, its next pointer can't be
3939 * dereferenced; however, as each css is given a monotonically
3940 * increasing unique serial number and always appended to the
3941 * sibling list, the next one can be found by walking the parent's
3942 * children until the first css with higher serial number than
3943 * @pos's. While this path can be slower, it happens iff iteration
3944 * races against release and the race window is very small.
3946 if (!pos) {
3947 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3948 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3949 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
3950 } else {
3951 list_for_each_entry_rcu(next, &parent->children, sibling)
3952 if (next->serial_nr > pos->serial_nr)
3953 break;
3957 * @next, if not pointing to the head, can be dereferenced and is
3958 * the next sibling.
3960 if (&next->sibling != &parent->children)
3961 return next;
3962 return NULL;
3966 * css_next_descendant_pre - find the next descendant for pre-order walk
3967 * @pos: the current position (%NULL to initiate traversal)
3968 * @root: css whose descendants to walk
3970 * To be used by css_for_each_descendant_pre(). Find the next descendant
3971 * to visit for pre-order traversal of @root's descendants. @root is
3972 * included in the iteration and the first node to be visited.
3974 * While this function requires cgroup_mutex or RCU read locking, it
3975 * doesn't require the whole traversal to be contained in a single critical
3976 * section. This function will return the correct next descendant as long
3977 * as both @pos and @root are accessible and @pos is a descendant of @root.
3979 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3980 * css which finished ->css_online() is guaranteed to be visible in the
3981 * future iterations and will stay visible until the last reference is put.
3982 * A css which hasn't finished ->css_online() or already finished
3983 * ->css_offline() may show up during traversal. It's each subsystem's
3984 * responsibility to synchronize against on/offlining.
3986 struct cgroup_subsys_state *
3987 css_next_descendant_pre(struct cgroup_subsys_state *pos,
3988 struct cgroup_subsys_state *root)
3990 struct cgroup_subsys_state *next;
3992 cgroup_assert_mutex_or_rcu_locked();
3994 /* if first iteration, visit @root */
3995 if (!pos)
3996 return root;
3998 /* visit the first child if exists */
3999 next = css_next_child(NULL, pos);
4000 if (next)
4001 return next;
4003 /* no child, visit my or the closest ancestor's next sibling */
4004 while (pos != root) {
4005 next = css_next_child(pos, pos->parent);
4006 if (next)
4007 return next;
4008 pos = pos->parent;
4011 return NULL;
4015 * css_rightmost_descendant - return the rightmost descendant of a css
4016 * @pos: css of interest
4018 * Return the rightmost descendant of @pos. If there's no descendant, @pos
4019 * is returned. This can be used during pre-order traversal to skip
4020 * subtree of @pos.
4022 * While this function requires cgroup_mutex or RCU read locking, it
4023 * doesn't require the whole traversal to be contained in a single critical
4024 * section. This function will return the correct rightmost descendant as
4025 * long as @pos is accessible.
4027 struct cgroup_subsys_state *
4028 css_rightmost_descendant(struct cgroup_subsys_state *pos)
4030 struct cgroup_subsys_state *last, *tmp;
4032 cgroup_assert_mutex_or_rcu_locked();
4034 do {
4035 last = pos;
4036 /* ->prev isn't RCU safe, walk ->next till the end */
4037 pos = NULL;
4038 css_for_each_child(tmp, last)
4039 pos = tmp;
4040 } while (pos);
4042 return last;
4045 static struct cgroup_subsys_state *
4046 css_leftmost_descendant(struct cgroup_subsys_state *pos)
4048 struct cgroup_subsys_state *last;
4050 do {
4051 last = pos;
4052 pos = css_next_child(NULL, pos);
4053 } while (pos);
4055 return last;
4059 * css_next_descendant_post - find the next descendant for post-order walk
4060 * @pos: the current position (%NULL to initiate traversal)
4061 * @root: css whose descendants to walk
4063 * To be used by css_for_each_descendant_post(). Find the next descendant
4064 * to visit for post-order traversal of @root's descendants. @root is
4065 * included in the iteration and the last node to be visited.
4067 * While this function requires cgroup_mutex or RCU read locking, it
4068 * doesn't require the whole traversal to be contained in a single critical
4069 * section. This function will return the correct next descendant as long
4070 * as both @pos and @cgroup are accessible and @pos is a descendant of
4071 * @cgroup.
4073 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4074 * css which finished ->css_online() is guaranteed to be visible in the
4075 * future iterations and will stay visible until the last reference is put.
4076 * A css which hasn't finished ->css_online() or already finished
4077 * ->css_offline() may show up during traversal. It's each subsystem's
4078 * responsibility to synchronize against on/offlining.
4080 struct cgroup_subsys_state *
4081 css_next_descendant_post(struct cgroup_subsys_state *pos,
4082 struct cgroup_subsys_state *root)
4084 struct cgroup_subsys_state *next;
4086 cgroup_assert_mutex_or_rcu_locked();
4088 /* if first iteration, visit leftmost descendant which may be @root */
4089 if (!pos)
4090 return css_leftmost_descendant(root);
4092 /* if we visited @root, we're done */
4093 if (pos == root)
4094 return NULL;
4096 /* if there's an unvisited sibling, visit its leftmost descendant */
4097 next = css_next_child(pos, pos->parent);
4098 if (next)
4099 return css_leftmost_descendant(next);
4101 /* no sibling left, visit parent */
4102 return pos->parent;
4106 * css_has_online_children - does a css have online children
4107 * @css: the target css
4109 * Returns %true if @css has any online children; otherwise, %false. This
4110 * function can be called from any context but the caller is responsible
4111 * for synchronizing against on/offlining as necessary.
4113 bool css_has_online_children(struct cgroup_subsys_state *css)
4115 struct cgroup_subsys_state *child;
4116 bool ret = false;
4118 rcu_read_lock();
4119 css_for_each_child(child, css) {
4120 if (child->flags & CSS_ONLINE) {
4121 ret = true;
4122 break;
4125 rcu_read_unlock();
4126 return ret;
4129 static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it)
4131 struct list_head *l;
4132 struct cgrp_cset_link *link;
4133 struct css_set *cset;
4135 lockdep_assert_held(&css_set_lock);
4137 /* find the next threaded cset */
4138 if (it->tcset_pos) {
4139 l = it->tcset_pos->next;
4141 if (l != it->tcset_head) {
4142 it->tcset_pos = l;
4143 return container_of(l, struct css_set,
4144 threaded_csets_node);
4147 it->tcset_pos = NULL;
4150 /* find the next cset */
4151 l = it->cset_pos;
4152 l = l->next;
4153 if (l == it->cset_head) {
4154 it->cset_pos = NULL;
4155 return NULL;
4158 if (it->ss) {
4159 cset = container_of(l, struct css_set, e_cset_node[it->ss->id]);
4160 } else {
4161 link = list_entry(l, struct cgrp_cset_link, cset_link);
4162 cset = link->cset;
4165 it->cset_pos = l;
4167 /* initialize threaded css_set walking */
4168 if (it->flags & CSS_TASK_ITER_THREADED) {
4169 if (it->cur_dcset)
4170 put_css_set_locked(it->cur_dcset);
4171 it->cur_dcset = cset;
4172 get_css_set(cset);
4174 it->tcset_head = &cset->threaded_csets;
4175 it->tcset_pos = &cset->threaded_csets;
4178 return cset;
4182 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
4183 * @it: the iterator to advance
4185 * Advance @it to the next css_set to walk.
4187 static void css_task_iter_advance_css_set(struct css_task_iter *it)
4189 struct css_set *cset;
4191 lockdep_assert_held(&css_set_lock);
4193 /* Advance to the next non-empty css_set */
4194 do {
4195 cset = css_task_iter_next_css_set(it);
4196 if (!cset) {
4197 it->task_pos = NULL;
4198 return;
4200 } while (!css_set_populated(cset));
4202 if (!list_empty(&cset->tasks))
4203 it->task_pos = cset->tasks.next;
4204 else
4205 it->task_pos = cset->mg_tasks.next;
4207 it->tasks_head = &cset->tasks;
4208 it->mg_tasks_head = &cset->mg_tasks;
4211 * We don't keep css_sets locked across iteration steps and thus
4212 * need to take steps to ensure that iteration can be resumed after
4213 * the lock is re-acquired. Iteration is performed at two levels -
4214 * css_sets and tasks in them.
4216 * Once created, a css_set never leaves its cgroup lists, so a
4217 * pinned css_set is guaranteed to stay put and we can resume
4218 * iteration afterwards.
4220 * Tasks may leave @cset across iteration steps. This is resolved
4221 * by registering each iterator with the css_set currently being
4222 * walked and making css_set_move_task() advance iterators whose
4223 * next task is leaving.
4225 if (it->cur_cset) {
4226 list_del(&it->iters_node);
4227 put_css_set_locked(it->cur_cset);
4229 get_css_set(cset);
4230 it->cur_cset = cset;
4231 list_add(&it->iters_node, &cset->task_iters);
4234 static void css_task_iter_advance(struct css_task_iter *it)
4236 struct list_head *next;
4238 lockdep_assert_held(&css_set_lock);
4239 repeat:
4240 if (it->task_pos) {
4242 * Advance iterator to find next entry. cset->tasks is
4243 * consumed first and then ->mg_tasks. After ->mg_tasks,
4244 * we move onto the next cset.
4246 next = it->task_pos->next;
4248 if (next == it->tasks_head)
4249 next = it->mg_tasks_head->next;
4251 if (next == it->mg_tasks_head)
4252 css_task_iter_advance_css_set(it);
4253 else
4254 it->task_pos = next;
4255 } else {
4256 /* called from start, proceed to the first cset */
4257 css_task_iter_advance_css_set(it);
4260 /* if PROCS, skip over tasks which aren't group leaders */
4261 if ((it->flags & CSS_TASK_ITER_PROCS) && it->task_pos &&
4262 !thread_group_leader(list_entry(it->task_pos, struct task_struct,
4263 cg_list)))
4264 goto repeat;
4268 * css_task_iter_start - initiate task iteration
4269 * @css: the css to walk tasks of
4270 * @flags: CSS_TASK_ITER_* flags
4271 * @it: the task iterator to use
4273 * Initiate iteration through the tasks of @css. The caller can call
4274 * css_task_iter_next() to walk through the tasks until the function
4275 * returns NULL. On completion of iteration, css_task_iter_end() must be
4276 * called.
4278 void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
4279 struct css_task_iter *it)
4281 /* no one should try to iterate before mounting cgroups */
4282 WARN_ON_ONCE(!use_task_css_set_links);
4284 memset(it, 0, sizeof(*it));
4286 spin_lock_irq(&css_set_lock);
4288 it->ss = css->ss;
4289 it->flags = flags;
4291 if (it->ss)
4292 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
4293 else
4294 it->cset_pos = &css->cgroup->cset_links;
4296 it->cset_head = it->cset_pos;
4298 css_task_iter_advance(it);
4300 spin_unlock_irq(&css_set_lock);
4304 * css_task_iter_next - return the next task for the iterator
4305 * @it: the task iterator being iterated
4307 * The "next" function for task iteration. @it should have been
4308 * initialized via css_task_iter_start(). Returns NULL when the iteration
4309 * reaches the end.
4311 struct task_struct *css_task_iter_next(struct css_task_iter *it)
4313 if (it->cur_task) {
4314 put_task_struct(it->cur_task);
4315 it->cur_task = NULL;
4318 spin_lock_irq(&css_set_lock);
4320 if (it->task_pos) {
4321 it->cur_task = list_entry(it->task_pos, struct task_struct,
4322 cg_list);
4323 get_task_struct(it->cur_task);
4324 css_task_iter_advance(it);
4327 spin_unlock_irq(&css_set_lock);
4329 return it->cur_task;
4333 * css_task_iter_end - finish task iteration
4334 * @it: the task iterator to finish
4336 * Finish task iteration started by css_task_iter_start().
4338 void css_task_iter_end(struct css_task_iter *it)
4340 if (it->cur_cset) {
4341 spin_lock_irq(&css_set_lock);
4342 list_del(&it->iters_node);
4343 put_css_set_locked(it->cur_cset);
4344 spin_unlock_irq(&css_set_lock);
4347 if (it->cur_dcset)
4348 put_css_set(it->cur_dcset);
4350 if (it->cur_task)
4351 put_task_struct(it->cur_task);
4354 static void cgroup_procs_release(struct kernfs_open_file *of)
4356 if (of->priv) {
4357 css_task_iter_end(of->priv);
4358 kfree(of->priv);
4362 static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
4364 struct kernfs_open_file *of = s->private;
4365 struct css_task_iter *it = of->priv;
4367 return css_task_iter_next(it);
4370 static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos,
4371 unsigned int iter_flags)
4373 struct kernfs_open_file *of = s->private;
4374 struct cgroup *cgrp = seq_css(s)->cgroup;
4375 struct css_task_iter *it = of->priv;
4378 * When a seq_file is seeked, it's always traversed sequentially
4379 * from position 0, so we can simply keep iterating on !0 *pos.
4381 if (!it) {
4382 if (WARN_ON_ONCE((*pos)++))
4383 return ERR_PTR(-EINVAL);
4385 it = kzalloc(sizeof(*it), GFP_KERNEL);
4386 if (!it)
4387 return ERR_PTR(-ENOMEM);
4388 of->priv = it;
4389 css_task_iter_start(&cgrp->self, iter_flags, it);
4390 } else if (!(*pos)++) {
4391 css_task_iter_end(it);
4392 css_task_iter_start(&cgrp->self, iter_flags, it);
4395 return cgroup_procs_next(s, NULL, NULL);
4398 static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
4400 struct cgroup *cgrp = seq_css(s)->cgroup;
4403 * All processes of a threaded subtree belong to the domain cgroup
4404 * of the subtree. Only threads can be distributed across the
4405 * subtree. Reject reads on cgroup.procs in the subtree proper.
4406 * They're always empty anyway.
4408 if (cgroup_is_threaded(cgrp))
4409 return ERR_PTR(-EOPNOTSUPP);
4411 return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS |
4412 CSS_TASK_ITER_THREADED);
4415 static int cgroup_procs_show(struct seq_file *s, void *v)
4417 seq_printf(s, "%d\n", task_pid_vnr(v));
4418 return 0;
4421 static int cgroup_procs_write_permission(struct cgroup *src_cgrp,
4422 struct cgroup *dst_cgrp,
4423 struct super_block *sb)
4425 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
4426 struct cgroup *com_cgrp = src_cgrp;
4427 struct inode *inode;
4428 int ret;
4430 lockdep_assert_held(&cgroup_mutex);
4432 /* find the common ancestor */
4433 while (!cgroup_is_descendant(dst_cgrp, com_cgrp))
4434 com_cgrp = cgroup_parent(com_cgrp);
4436 /* %current should be authorized to migrate to the common ancestor */
4437 inode = kernfs_get_inode(sb, com_cgrp->procs_file.kn);
4438 if (!inode)
4439 return -ENOMEM;
4441 ret = inode_permission(inode, MAY_WRITE);
4442 iput(inode);
4443 if (ret)
4444 return ret;
4447 * If namespaces are delegation boundaries, %current must be able
4448 * to see both source and destination cgroups from its namespace.
4450 if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) &&
4451 (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) ||
4452 !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp)))
4453 return -ENOENT;
4455 return 0;
4458 static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
4459 char *buf, size_t nbytes, loff_t off)
4461 struct cgroup *src_cgrp, *dst_cgrp;
4462 struct task_struct *task;
4463 ssize_t ret;
4465 dst_cgrp = cgroup_kn_lock_live(of->kn, false);
4466 if (!dst_cgrp)
4467 return -ENODEV;
4469 task = cgroup_procs_write_start(buf, true);
4470 ret = PTR_ERR_OR_ZERO(task);
4471 if (ret)
4472 goto out_unlock;
4474 /* find the source cgroup */
4475 spin_lock_irq(&css_set_lock);
4476 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
4477 spin_unlock_irq(&css_set_lock);
4479 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp,
4480 of->file->f_path.dentry->d_sb);
4481 if (ret)
4482 goto out_finish;
4484 ret = cgroup_attach_task(dst_cgrp, task, true);
4486 out_finish:
4487 cgroup_procs_write_finish(task);
4488 out_unlock:
4489 cgroup_kn_unlock(of->kn);
4491 return ret ?: nbytes;
4494 static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
4496 return __cgroup_procs_start(s, pos, 0);
4499 static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
4500 char *buf, size_t nbytes, loff_t off)
4502 struct cgroup *src_cgrp, *dst_cgrp;
4503 struct task_struct *task;
4504 ssize_t ret;
4506 buf = strstrip(buf);
4508 dst_cgrp = cgroup_kn_lock_live(of->kn, false);
4509 if (!dst_cgrp)
4510 return -ENODEV;
4512 task = cgroup_procs_write_start(buf, false);
4513 ret = PTR_ERR_OR_ZERO(task);
4514 if (ret)
4515 goto out_unlock;
4517 /* find the source cgroup */
4518 spin_lock_irq(&css_set_lock);
4519 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
4520 spin_unlock_irq(&css_set_lock);
4522 /* thread migrations follow the cgroup.procs delegation rule */
4523 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp,
4524 of->file->f_path.dentry->d_sb);
4525 if (ret)
4526 goto out_finish;
4528 /* and must be contained in the same domain */
4529 ret = -EOPNOTSUPP;
4530 if (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp)
4531 goto out_finish;
4533 ret = cgroup_attach_task(dst_cgrp, task, false);
4535 out_finish:
4536 cgroup_procs_write_finish(task);
4537 out_unlock:
4538 cgroup_kn_unlock(of->kn);
4540 return ret ?: nbytes;
4543 /* cgroup core interface files for the default hierarchy */
4544 static struct cftype cgroup_base_files[] = {
4546 .name = "cgroup.type",
4547 .flags = CFTYPE_NOT_ON_ROOT,
4548 .seq_show = cgroup_type_show,
4549 .write = cgroup_type_write,
4552 .name = "cgroup.procs",
4553 .flags = CFTYPE_NS_DELEGATABLE,
4554 .file_offset = offsetof(struct cgroup, procs_file),
4555 .release = cgroup_procs_release,
4556 .seq_start = cgroup_procs_start,
4557 .seq_next = cgroup_procs_next,
4558 .seq_show = cgroup_procs_show,
4559 .write = cgroup_procs_write,
4562 .name = "cgroup.threads",
4563 .flags = CFTYPE_NS_DELEGATABLE,
4564 .release = cgroup_procs_release,
4565 .seq_start = cgroup_threads_start,
4566 .seq_next = cgroup_procs_next,
4567 .seq_show = cgroup_procs_show,
4568 .write = cgroup_threads_write,
4571 .name = "cgroup.controllers",
4572 .seq_show = cgroup_controllers_show,
4575 .name = "cgroup.subtree_control",
4576 .flags = CFTYPE_NS_DELEGATABLE,
4577 .seq_show = cgroup_subtree_control_show,
4578 .write = cgroup_subtree_control_write,
4581 .name = "cgroup.events",
4582 .flags = CFTYPE_NOT_ON_ROOT,
4583 .file_offset = offsetof(struct cgroup, events_file),
4584 .seq_show = cgroup_events_show,
4587 .name = "cgroup.max.descendants",
4588 .seq_show = cgroup_max_descendants_show,
4589 .write = cgroup_max_descendants_write,
4592 .name = "cgroup.max.depth",
4593 .seq_show = cgroup_max_depth_show,
4594 .write = cgroup_max_depth_write,
4597 .name = "cgroup.stat",
4598 .seq_show = cgroup_stat_show,
4601 .name = "cpu.stat",
4602 .flags = CFTYPE_NOT_ON_ROOT,
4603 .seq_show = cpu_stat_show,
4605 #ifdef CONFIG_PSI
4607 .name = "io.pressure",
4608 .flags = CFTYPE_NOT_ON_ROOT,
4609 .seq_show = cgroup_io_pressure_show,
4612 .name = "memory.pressure",
4613 .flags = CFTYPE_NOT_ON_ROOT,
4614 .seq_show = cgroup_memory_pressure_show,
4617 .name = "cpu.pressure",
4618 .flags = CFTYPE_NOT_ON_ROOT,
4619 .seq_show = cgroup_cpu_pressure_show,
4621 #endif
4622 { } /* terminate */
4626 * css destruction is four-stage process.
4628 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4629 * Implemented in kill_css().
4631 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4632 * and thus css_tryget_online() is guaranteed to fail, the css can be
4633 * offlined by invoking offline_css(). After offlining, the base ref is
4634 * put. Implemented in css_killed_work_fn().
4636 * 3. When the percpu_ref reaches zero, the only possible remaining
4637 * accessors are inside RCU read sections. css_release() schedules the
4638 * RCU callback.
4640 * 4. After the grace period, the css can be freed. Implemented in
4641 * css_free_work_fn().
4643 * It is actually hairier because both step 2 and 4 require process context
4644 * and thus involve punting to css->destroy_work adding two additional
4645 * steps to the already complex sequence.
4647 static void css_free_rwork_fn(struct work_struct *work)
4649 struct cgroup_subsys_state *css = container_of(to_rcu_work(work),
4650 struct cgroup_subsys_state, destroy_rwork);
4651 struct cgroup_subsys *ss = css->ss;
4652 struct cgroup *cgrp = css->cgroup;
4654 percpu_ref_exit(&css->refcnt);
4656 if (ss) {
4657 /* css free path */
4658 struct cgroup_subsys_state *parent = css->parent;
4659 int id = css->id;
4661 ss->css_free(css);
4662 cgroup_idr_remove(&ss->css_idr, id);
4663 cgroup_put(cgrp);
4665 if (parent)
4666 css_put(parent);
4667 } else {
4668 /* cgroup free path */
4669 atomic_dec(&cgrp->root->nr_cgrps);
4670 cgroup1_pidlist_destroy_all(cgrp);
4671 cancel_work_sync(&cgrp->release_agent_work);
4673 if (cgroup_parent(cgrp)) {
4675 * We get a ref to the parent, and put the ref when
4676 * this cgroup is being freed, so it's guaranteed
4677 * that the parent won't be destroyed before its
4678 * children.
4680 cgroup_put(cgroup_parent(cgrp));
4681 kernfs_put(cgrp->kn);
4682 psi_cgroup_free(cgrp);
4683 if (cgroup_on_dfl(cgrp))
4684 cgroup_rstat_exit(cgrp);
4685 kfree(cgrp);
4686 } else {
4688 * This is root cgroup's refcnt reaching zero,
4689 * which indicates that the root should be
4690 * released.
4692 cgroup_destroy_root(cgrp->root);
4697 static void css_release_work_fn(struct work_struct *work)
4699 struct cgroup_subsys_state *css =
4700 container_of(work, struct cgroup_subsys_state, destroy_work);
4701 struct cgroup_subsys *ss = css->ss;
4702 struct cgroup *cgrp = css->cgroup;
4704 mutex_lock(&cgroup_mutex);
4706 css->flags |= CSS_RELEASED;
4707 list_del_rcu(&css->sibling);
4709 if (ss) {
4710 /* css release path */
4711 if (!list_empty(&css->rstat_css_node)) {
4712 cgroup_rstat_flush(cgrp);
4713 list_del_rcu(&css->rstat_css_node);
4716 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
4717 if (ss->css_released)
4718 ss->css_released(css);
4719 } else {
4720 struct cgroup *tcgrp;
4722 /* cgroup release path */
4723 TRACE_CGROUP_PATH(release, cgrp);
4725 if (cgroup_on_dfl(cgrp))
4726 cgroup_rstat_flush(cgrp);
4728 for (tcgrp = cgroup_parent(cgrp); tcgrp;
4729 tcgrp = cgroup_parent(tcgrp))
4730 tcgrp->nr_dying_descendants--;
4732 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4733 cgrp->id = -1;
4736 * There are two control paths which try to determine
4737 * cgroup from dentry without going through kernfs -
4738 * cgroupstats_build() and css_tryget_online_from_dir().
4739 * Those are supported by RCU protecting clearing of
4740 * cgrp->kn->priv backpointer.
4742 if (cgrp->kn)
4743 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
4744 NULL);
4746 cgroup_bpf_put(cgrp);
4749 mutex_unlock(&cgroup_mutex);
4751 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
4752 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
4755 static void css_release(struct percpu_ref *ref)
4757 struct cgroup_subsys_state *css =
4758 container_of(ref, struct cgroup_subsys_state, refcnt);
4760 INIT_WORK(&css->destroy_work, css_release_work_fn);
4761 queue_work(cgroup_destroy_wq, &css->destroy_work);
4764 static void init_and_link_css(struct cgroup_subsys_state *css,
4765 struct cgroup_subsys *ss, struct cgroup *cgrp)
4767 lockdep_assert_held(&cgroup_mutex);
4769 cgroup_get_live(cgrp);
4771 memset(css, 0, sizeof(*css));
4772 css->cgroup = cgrp;
4773 css->ss = ss;
4774 css->id = -1;
4775 INIT_LIST_HEAD(&css->sibling);
4776 INIT_LIST_HEAD(&css->children);
4777 INIT_LIST_HEAD(&css->rstat_css_node);
4778 css->serial_nr = css_serial_nr_next++;
4779 atomic_set(&css->online_cnt, 0);
4781 if (cgroup_parent(cgrp)) {
4782 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
4783 css_get(css->parent);
4786 if (cgroup_on_dfl(cgrp) && ss->css_rstat_flush)
4787 list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list);
4789 BUG_ON(cgroup_css(cgrp, ss));
4792 /* invoke ->css_online() on a new CSS and mark it online if successful */
4793 static int online_css(struct cgroup_subsys_state *css)
4795 struct cgroup_subsys *ss = css->ss;
4796 int ret = 0;
4798 lockdep_assert_held(&cgroup_mutex);
4800 if (ss->css_online)
4801 ret = ss->css_online(css);
4802 if (!ret) {
4803 css->flags |= CSS_ONLINE;
4804 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
4806 atomic_inc(&css->online_cnt);
4807 if (css->parent)
4808 atomic_inc(&css->parent->online_cnt);
4810 return ret;
4813 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4814 static void offline_css(struct cgroup_subsys_state *css)
4816 struct cgroup_subsys *ss = css->ss;
4818 lockdep_assert_held(&cgroup_mutex);
4820 if (!(css->flags & CSS_ONLINE))
4821 return;
4823 if (ss->css_offline)
4824 ss->css_offline(css);
4826 css->flags &= ~CSS_ONLINE;
4827 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
4829 wake_up_all(&css->cgroup->offline_waitq);
4833 * css_create - create a cgroup_subsys_state
4834 * @cgrp: the cgroup new css will be associated with
4835 * @ss: the subsys of new css
4837 * Create a new css associated with @cgrp - @ss pair. On success, the new
4838 * css is online and installed in @cgrp. This function doesn't create the
4839 * interface files. Returns 0 on success, -errno on failure.
4841 static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
4842 struct cgroup_subsys *ss)
4844 struct cgroup *parent = cgroup_parent(cgrp);
4845 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
4846 struct cgroup_subsys_state *css;
4847 int err;
4849 lockdep_assert_held(&cgroup_mutex);
4851 css = ss->css_alloc(parent_css);
4852 if (!css)
4853 css = ERR_PTR(-ENOMEM);
4854 if (IS_ERR(css))
4855 return css;
4857 init_and_link_css(css, ss, cgrp);
4859 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
4860 if (err)
4861 goto err_free_css;
4863 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
4864 if (err < 0)
4865 goto err_free_css;
4866 css->id = err;
4868 /* @css is ready to be brought online now, make it visible */
4869 list_add_tail_rcu(&css->sibling, &parent_css->children);
4870 cgroup_idr_replace(&ss->css_idr, css, css->id);
4872 err = online_css(css);
4873 if (err)
4874 goto err_list_del;
4876 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4877 cgroup_parent(parent)) {
4878 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4879 current->comm, current->pid, ss->name);
4880 if (!strcmp(ss->name, "memory"))
4881 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
4882 ss->warned_broken_hierarchy = true;
4885 return css;
4887 err_list_del:
4888 list_del_rcu(&css->sibling);
4889 err_free_css:
4890 list_del_rcu(&css->rstat_css_node);
4891 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
4892 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
4893 return ERR_PTR(err);
4897 * The returned cgroup is fully initialized including its control mask, but
4898 * it isn't associated with its kernfs_node and doesn't have the control
4899 * mask applied.
4901 static struct cgroup *cgroup_create(struct cgroup *parent)
4903 struct cgroup_root *root = parent->root;
4904 struct cgroup *cgrp, *tcgrp;
4905 int level = parent->level + 1;
4906 int ret;
4908 /* allocate the cgroup and its ID, 0 is reserved for the root */
4909 cgrp = kzalloc(struct_size(cgrp, ancestor_ids, (level + 1)),
4910 GFP_KERNEL);
4911 if (!cgrp)
4912 return ERR_PTR(-ENOMEM);
4914 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
4915 if (ret)
4916 goto out_free_cgrp;
4918 if (cgroup_on_dfl(parent)) {
4919 ret = cgroup_rstat_init(cgrp);
4920 if (ret)
4921 goto out_cancel_ref;
4925 * Temporarily set the pointer to NULL, so idr_find() won't return
4926 * a half-baked cgroup.
4928 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
4929 if (cgrp->id < 0) {
4930 ret = -ENOMEM;
4931 goto out_stat_exit;
4934 init_cgroup_housekeeping(cgrp);
4936 cgrp->self.parent = &parent->self;
4937 cgrp->root = root;
4938 cgrp->level = level;
4940 ret = psi_cgroup_alloc(cgrp);
4941 if (ret)
4942 goto out_idr_free;
4944 ret = cgroup_bpf_inherit(cgrp);
4945 if (ret)
4946 goto out_psi_free;
4948 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
4949 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
4951 if (tcgrp != cgrp)
4952 tcgrp->nr_descendants++;
4955 if (notify_on_release(parent))
4956 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4958 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4959 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
4961 cgrp->self.serial_nr = css_serial_nr_next++;
4963 /* allocation complete, commit to creation */
4964 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
4965 atomic_inc(&root->nr_cgrps);
4966 cgroup_get_live(parent);
4969 * @cgrp is now fully operational. If something fails after this
4970 * point, it'll be released via the normal destruction path.
4972 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4975 * On the default hierarchy, a child doesn't automatically inherit
4976 * subtree_control from the parent. Each is configured manually.
4978 if (!cgroup_on_dfl(cgrp))
4979 cgrp->subtree_control = cgroup_control(cgrp);
4981 cgroup_propagate_control(cgrp);
4983 return cgrp;
4985 out_psi_free:
4986 psi_cgroup_free(cgrp);
4987 out_idr_free:
4988 cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
4989 out_stat_exit:
4990 if (cgroup_on_dfl(parent))
4991 cgroup_rstat_exit(cgrp);
4992 out_cancel_ref:
4993 percpu_ref_exit(&cgrp->self.refcnt);
4994 out_free_cgrp:
4995 kfree(cgrp);
4996 return ERR_PTR(ret);
4999 static bool cgroup_check_hierarchy_limits(struct cgroup *parent)
5001 struct cgroup *cgroup;
5002 int ret = false;
5003 int level = 1;
5005 lockdep_assert_held(&cgroup_mutex);
5007 for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) {
5008 if (cgroup->nr_descendants >= cgroup->max_descendants)
5009 goto fail;
5011 if (level > cgroup->max_depth)
5012 goto fail;
5014 level++;
5017 ret = true;
5018 fail:
5019 return ret;
5022 int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
5024 struct cgroup *parent, *cgrp;
5025 struct kernfs_node *kn;
5026 int ret;
5028 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
5029 if (strchr(name, '\n'))
5030 return -EINVAL;
5032 parent = cgroup_kn_lock_live(parent_kn, false);
5033 if (!parent)
5034 return -ENODEV;
5036 if (!cgroup_check_hierarchy_limits(parent)) {
5037 ret = -EAGAIN;
5038 goto out_unlock;
5041 cgrp = cgroup_create(parent);
5042 if (IS_ERR(cgrp)) {
5043 ret = PTR_ERR(cgrp);
5044 goto out_unlock;
5047 /* create the directory */
5048 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
5049 if (IS_ERR(kn)) {
5050 ret = PTR_ERR(kn);
5051 goto out_destroy;
5053 cgrp->kn = kn;
5056 * This extra ref will be put in cgroup_free_fn() and guarantees
5057 * that @cgrp->kn is always accessible.
5059 kernfs_get(kn);
5061 ret = cgroup_kn_set_ugid(kn);
5062 if (ret)
5063 goto out_destroy;
5065 ret = css_populate_dir(&cgrp->self);
5066 if (ret)
5067 goto out_destroy;
5069 ret = cgroup_apply_control_enable(cgrp);
5070 if (ret)
5071 goto out_destroy;
5073 TRACE_CGROUP_PATH(mkdir, cgrp);
5075 /* let's create and online css's */
5076 kernfs_activate(kn);
5078 ret = 0;
5079 goto out_unlock;
5081 out_destroy:
5082 cgroup_destroy_locked(cgrp);
5083 out_unlock:
5084 cgroup_kn_unlock(parent_kn);
5085 return ret;
5089 * This is called when the refcnt of a css is confirmed to be killed.
5090 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5091 * initate destruction and put the css ref from kill_css().
5093 static void css_killed_work_fn(struct work_struct *work)
5095 struct cgroup_subsys_state *css =
5096 container_of(work, struct cgroup_subsys_state, destroy_work);
5098 mutex_lock(&cgroup_mutex);
5100 do {
5101 offline_css(css);
5102 css_put(css);
5103 /* @css can't go away while we're holding cgroup_mutex */
5104 css = css->parent;
5105 } while (css && atomic_dec_and_test(&css->online_cnt));
5107 mutex_unlock(&cgroup_mutex);
5110 /* css kill confirmation processing requires process context, bounce */
5111 static void css_killed_ref_fn(struct percpu_ref *ref)
5113 struct cgroup_subsys_state *css =
5114 container_of(ref, struct cgroup_subsys_state, refcnt);
5116 if (atomic_dec_and_test(&css->online_cnt)) {
5117 INIT_WORK(&css->destroy_work, css_killed_work_fn);
5118 queue_work(cgroup_destroy_wq, &css->destroy_work);
5123 * kill_css - destroy a css
5124 * @css: css to destroy
5126 * This function initiates destruction of @css by removing cgroup interface
5127 * files and putting its base reference. ->css_offline() will be invoked
5128 * asynchronously once css_tryget_online() is guaranteed to fail and when
5129 * the reference count reaches zero, @css will be released.
5131 static void kill_css(struct cgroup_subsys_state *css)
5133 lockdep_assert_held(&cgroup_mutex);
5135 if (css->flags & CSS_DYING)
5136 return;
5138 css->flags |= CSS_DYING;
5141 * This must happen before css is disassociated with its cgroup.
5142 * See seq_css() for details.
5144 css_clear_dir(css);
5147 * Killing would put the base ref, but we need to keep it alive
5148 * until after ->css_offline().
5150 css_get(css);
5153 * cgroup core guarantees that, by the time ->css_offline() is
5154 * invoked, no new css reference will be given out via
5155 * css_tryget_online(). We can't simply call percpu_ref_kill() and
5156 * proceed to offlining css's because percpu_ref_kill() doesn't
5157 * guarantee that the ref is seen as killed on all CPUs on return.
5159 * Use percpu_ref_kill_and_confirm() to get notifications as each
5160 * css is confirmed to be seen as killed on all CPUs.
5162 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
5166 * cgroup_destroy_locked - the first stage of cgroup destruction
5167 * @cgrp: cgroup to be destroyed
5169 * css's make use of percpu refcnts whose killing latency shouldn't be
5170 * exposed to userland and are RCU protected. Also, cgroup core needs to
5171 * guarantee that css_tryget_online() won't succeed by the time
5172 * ->css_offline() is invoked. To satisfy all the requirements,
5173 * destruction is implemented in the following two steps.
5175 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5176 * userland visible parts and start killing the percpu refcnts of
5177 * css's. Set up so that the next stage will be kicked off once all
5178 * the percpu refcnts are confirmed to be killed.
5180 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5181 * rest of destruction. Once all cgroup references are gone, the
5182 * cgroup is RCU-freed.
5184 * This function implements s1. After this step, @cgrp is gone as far as
5185 * the userland is concerned and a new cgroup with the same name may be
5186 * created. As cgroup doesn't care about the names internally, this
5187 * doesn't cause any problem.
5189 static int cgroup_destroy_locked(struct cgroup *cgrp)
5190 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
5192 struct cgroup *tcgrp, *parent = cgroup_parent(cgrp);
5193 struct cgroup_subsys_state *css;
5194 struct cgrp_cset_link *link;
5195 int ssid;
5197 lockdep_assert_held(&cgroup_mutex);
5200 * Only migration can raise populated from zero and we're already
5201 * holding cgroup_mutex.
5203 if (cgroup_is_populated(cgrp))
5204 return -EBUSY;
5207 * Make sure there's no live children. We can't test emptiness of
5208 * ->self.children as dead children linger on it while being
5209 * drained; otherwise, "rmdir parent/child parent" may fail.
5211 if (css_has_online_children(&cgrp->self))
5212 return -EBUSY;
5215 * Mark @cgrp and the associated csets dead. The former prevents
5216 * further task migration and child creation by disabling
5217 * cgroup_lock_live_group(). The latter makes the csets ignored by
5218 * the migration path.
5220 cgrp->self.flags &= ~CSS_ONLINE;
5222 spin_lock_irq(&css_set_lock);
5223 list_for_each_entry(link, &cgrp->cset_links, cset_link)
5224 link->cset->dead = true;
5225 spin_unlock_irq(&css_set_lock);
5227 /* initiate massacre of all css's */
5228 for_each_css(css, ssid, cgrp)
5229 kill_css(css);
5231 /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
5232 css_clear_dir(&cgrp->self);
5233 kernfs_remove(cgrp->kn);
5235 if (parent && cgroup_is_threaded(cgrp))
5236 parent->nr_threaded_children--;
5238 for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) {
5239 tcgrp->nr_descendants--;
5240 tcgrp->nr_dying_descendants++;
5243 cgroup1_check_for_release(parent);
5245 /* put the base reference */
5246 percpu_ref_kill(&cgrp->self.refcnt);
5248 return 0;
5251 int cgroup_rmdir(struct kernfs_node *kn)
5253 struct cgroup *cgrp;
5254 int ret = 0;
5256 cgrp = cgroup_kn_lock_live(kn, false);
5257 if (!cgrp)
5258 return 0;
5260 ret = cgroup_destroy_locked(cgrp);
5261 if (!ret)
5262 TRACE_CGROUP_PATH(rmdir, cgrp);
5264 cgroup_kn_unlock(kn);
5265 return ret;
5268 static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
5269 .show_options = cgroup_show_options,
5270 .remount_fs = cgroup_remount,
5271 .mkdir = cgroup_mkdir,
5272 .rmdir = cgroup_rmdir,
5273 .show_path = cgroup_show_path,
5276 static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
5278 struct cgroup_subsys_state *css;
5280 pr_debug("Initializing cgroup subsys %s\n", ss->name);
5282 mutex_lock(&cgroup_mutex);
5284 idr_init(&ss->css_idr);
5285 INIT_LIST_HEAD(&ss->cfts);
5287 /* Create the root cgroup state for this subsystem */
5288 ss->root = &cgrp_dfl_root;
5289 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
5290 /* We don't handle early failures gracefully */
5291 BUG_ON(IS_ERR(css));
5292 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
5295 * Root csses are never destroyed and we can't initialize
5296 * percpu_ref during early init. Disable refcnting.
5298 css->flags |= CSS_NO_REF;
5300 if (early) {
5301 /* allocation can't be done safely during early init */
5302 css->id = 1;
5303 } else {
5304 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
5305 BUG_ON(css->id < 0);
5308 /* Update the init_css_set to contain a subsys
5309 * pointer to this state - since the subsystem is
5310 * newly registered, all tasks and hence the
5311 * init_css_set is in the subsystem's root cgroup. */
5312 init_css_set.subsys[ss->id] = css;
5314 have_fork_callback |= (bool)ss->fork << ss->id;
5315 have_exit_callback |= (bool)ss->exit << ss->id;
5316 have_free_callback |= (bool)ss->free << ss->id;
5317 have_canfork_callback |= (bool)ss->can_fork << ss->id;
5319 /* At system boot, before all subsystems have been
5320 * registered, no tasks have been forked, so we don't
5321 * need to invoke fork callbacks here. */
5322 BUG_ON(!list_empty(&init_task.tasks));
5324 BUG_ON(online_css(css));
5326 mutex_unlock(&cgroup_mutex);
5330 * cgroup_init_early - cgroup initialization at system boot
5332 * Initialize cgroups at system boot, and initialize any
5333 * subsystems that request early init.
5335 int __init cgroup_init_early(void)
5337 static struct cgroup_sb_opts __initdata opts;
5338 struct cgroup_subsys *ss;
5339 int i;
5341 init_cgroup_root(&cgrp_dfl_root, &opts);
5342 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
5344 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
5346 for_each_subsys(ss, i) {
5347 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
5348 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
5349 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
5350 ss->id, ss->name);
5351 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
5352 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
5354 ss->id = i;
5355 ss->name = cgroup_subsys_name[i];
5356 if (!ss->legacy_name)
5357 ss->legacy_name = cgroup_subsys_name[i];
5359 if (ss->early_init)
5360 cgroup_init_subsys(ss, true);
5362 return 0;
5365 static u16 cgroup_disable_mask __initdata;
5368 * cgroup_init - cgroup initialization
5370 * Register cgroup filesystem and /proc file, and initialize
5371 * any subsystems that didn't request early init.
5373 int __init cgroup_init(void)
5375 struct cgroup_subsys *ss;
5376 int ssid;
5378 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
5379 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
5380 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
5381 BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
5383 cgroup_rstat_boot();
5386 * The latency of the synchronize_rcu() is too high for cgroups,
5387 * avoid it at the cost of forcing all readers into the slow path.
5389 rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss);
5391 get_user_ns(init_cgroup_ns.user_ns);
5393 mutex_lock(&cgroup_mutex);
5396 * Add init_css_set to the hash table so that dfl_root can link to
5397 * it during init.
5399 hash_add(css_set_table, &init_css_set.hlist,
5400 css_set_hash(init_css_set.subsys));
5402 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0, 0));
5404 mutex_unlock(&cgroup_mutex);
5406 for_each_subsys(ss, ssid) {
5407 if (ss->early_init) {
5408 struct cgroup_subsys_state *css =
5409 init_css_set.subsys[ss->id];
5411 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
5412 GFP_KERNEL);
5413 BUG_ON(css->id < 0);
5414 } else {
5415 cgroup_init_subsys(ss, false);
5418 list_add_tail(&init_css_set.e_cset_node[ssid],
5419 &cgrp_dfl_root.cgrp.e_csets[ssid]);
5422 * Setting dfl_root subsys_mask needs to consider the
5423 * disabled flag and cftype registration needs kmalloc,
5424 * both of which aren't available during early_init.
5426 if (cgroup_disable_mask & (1 << ssid)) {
5427 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
5428 printk(KERN_INFO "Disabling %s control group subsystem\n",
5429 ss->name);
5430 continue;
5433 if (cgroup1_ssid_disabled(ssid))
5434 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
5435 ss->name);
5437 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
5439 /* implicit controllers must be threaded too */
5440 WARN_ON(ss->implicit_on_dfl && !ss->threaded);
5442 if (ss->implicit_on_dfl)
5443 cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
5444 else if (!ss->dfl_cftypes)
5445 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
5447 if (ss->threaded)
5448 cgrp_dfl_threaded_ss_mask |= 1 << ss->id;
5450 if (ss->dfl_cftypes == ss->legacy_cftypes) {
5451 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
5452 } else {
5453 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
5454 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
5457 if (ss->bind)
5458 ss->bind(init_css_set.subsys[ssid]);
5460 mutex_lock(&cgroup_mutex);
5461 css_populate_dir(init_css_set.subsys[ssid]);
5462 mutex_unlock(&cgroup_mutex);
5465 /* init_css_set.subsys[] has been updated, re-hash */
5466 hash_del(&init_css_set.hlist);
5467 hash_add(css_set_table, &init_css_set.hlist,
5468 css_set_hash(init_css_set.subsys));
5470 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
5471 WARN_ON(register_filesystem(&cgroup_fs_type));
5472 WARN_ON(register_filesystem(&cgroup2_fs_type));
5473 WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show));
5475 return 0;
5478 static int __init cgroup_wq_init(void)
5481 * There isn't much point in executing destruction path in
5482 * parallel. Good chunk is serialized with cgroup_mutex anyway.
5483 * Use 1 for @max_active.
5485 * We would prefer to do this in cgroup_init() above, but that
5486 * is called before init_workqueues(): so leave this until after.
5488 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
5489 BUG_ON(!cgroup_destroy_wq);
5490 return 0;
5492 core_initcall(cgroup_wq_init);
5494 void cgroup_path_from_kernfs_id(const union kernfs_node_id *id,
5495 char *buf, size_t buflen)
5497 struct kernfs_node *kn;
5499 kn = kernfs_get_node_by_id(cgrp_dfl_root.kf_root, id);
5500 if (!kn)
5501 return;
5502 kernfs_path(kn, buf, buflen);
5503 kernfs_put(kn);
5507 * proc_cgroup_show()
5508 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5509 * - Used for /proc/<pid>/cgroup.
5511 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
5512 struct pid *pid, struct task_struct *tsk)
5514 char *buf;
5515 int retval;
5516 struct cgroup_root *root;
5518 retval = -ENOMEM;
5519 buf = kmalloc(PATH_MAX, GFP_KERNEL);
5520 if (!buf)
5521 goto out;
5523 mutex_lock(&cgroup_mutex);
5524 spin_lock_irq(&css_set_lock);
5526 for_each_root(root) {
5527 struct cgroup_subsys *ss;
5528 struct cgroup *cgrp;
5529 int ssid, count = 0;
5531 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
5532 continue;
5534 seq_printf(m, "%d:", root->hierarchy_id);
5535 if (root != &cgrp_dfl_root)
5536 for_each_subsys(ss, ssid)
5537 if (root->subsys_mask & (1 << ssid))
5538 seq_printf(m, "%s%s", count++ ? "," : "",
5539 ss->legacy_name);
5540 if (strlen(root->name))
5541 seq_printf(m, "%sname=%s", count ? "," : "",
5542 root->name);
5543 seq_putc(m, ':');
5545 cgrp = task_cgroup_from_root(tsk, root);
5548 * On traditional hierarchies, all zombie tasks show up as
5549 * belonging to the root cgroup. On the default hierarchy,
5550 * while a zombie doesn't show up in "cgroup.procs" and
5551 * thus can't be migrated, its /proc/PID/cgroup keeps
5552 * reporting the cgroup it belonged to before exiting. If
5553 * the cgroup is removed before the zombie is reaped,
5554 * " (deleted)" is appended to the cgroup path.
5556 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
5557 retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
5558 current->nsproxy->cgroup_ns);
5559 if (retval >= PATH_MAX)
5560 retval = -ENAMETOOLONG;
5561 if (retval < 0)
5562 goto out_unlock;
5564 seq_puts(m, buf);
5565 } else {
5566 seq_puts(m, "/");
5569 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
5570 seq_puts(m, " (deleted)\n");
5571 else
5572 seq_putc(m, '\n');
5575 retval = 0;
5576 out_unlock:
5577 spin_unlock_irq(&css_set_lock);
5578 mutex_unlock(&cgroup_mutex);
5579 kfree(buf);
5580 out:
5581 return retval;
5585 * cgroup_fork - initialize cgroup related fields during copy_process()
5586 * @child: pointer to task_struct of forking parent process.
5588 * A task is associated with the init_css_set until cgroup_post_fork()
5589 * attaches it to the parent's css_set. Empty cg_list indicates that
5590 * @child isn't holding reference to its css_set.
5592 void cgroup_fork(struct task_struct *child)
5594 RCU_INIT_POINTER(child->cgroups, &init_css_set);
5595 INIT_LIST_HEAD(&child->cg_list);
5599 * cgroup_can_fork - called on a new task before the process is exposed
5600 * @child: the task in question.
5602 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5603 * returns an error, the fork aborts with that error code. This allows for
5604 * a cgroup subsystem to conditionally allow or deny new forks.
5606 int cgroup_can_fork(struct task_struct *child)
5608 struct cgroup_subsys *ss;
5609 int i, j, ret;
5611 do_each_subsys_mask(ss, i, have_canfork_callback) {
5612 ret = ss->can_fork(child);
5613 if (ret)
5614 goto out_revert;
5615 } while_each_subsys_mask();
5617 return 0;
5619 out_revert:
5620 for_each_subsys(ss, j) {
5621 if (j >= i)
5622 break;
5623 if (ss->cancel_fork)
5624 ss->cancel_fork(child);
5627 return ret;
5631 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5632 * @child: the task in question
5634 * This calls the cancel_fork() callbacks if a fork failed *after*
5635 * cgroup_can_fork() succeded.
5637 void cgroup_cancel_fork(struct task_struct *child)
5639 struct cgroup_subsys *ss;
5640 int i;
5642 for_each_subsys(ss, i)
5643 if (ss->cancel_fork)
5644 ss->cancel_fork(child);
5648 * cgroup_post_fork - called on a new task after adding it to the task list
5649 * @child: the task in question
5651 * Adds the task to the list running through its css_set if necessary and
5652 * call the subsystem fork() callbacks. Has to be after the task is
5653 * visible on the task list in case we race with the first call to
5654 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5655 * list.
5657 void cgroup_post_fork(struct task_struct *child)
5659 struct cgroup_subsys *ss;
5660 int i;
5663 * This may race against cgroup_enable_task_cg_lists(). As that
5664 * function sets use_task_css_set_links before grabbing
5665 * tasklist_lock and we just went through tasklist_lock to add
5666 * @child, it's guaranteed that either we see the set
5667 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5668 * @child during its iteration.
5670 * If we won the race, @child is associated with %current's
5671 * css_set. Grabbing css_set_lock guarantees both that the
5672 * association is stable, and, on completion of the parent's
5673 * migration, @child is visible in the source of migration or
5674 * already in the destination cgroup. This guarantee is necessary
5675 * when implementing operations which need to migrate all tasks of
5676 * a cgroup to another.
5678 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
5679 * will remain in init_css_set. This is safe because all tasks are
5680 * in the init_css_set before cg_links is enabled and there's no
5681 * operation which transfers all tasks out of init_css_set.
5683 if (use_task_css_set_links) {
5684 struct css_set *cset;
5686 spin_lock_irq(&css_set_lock);
5687 cset = task_css_set(current);
5688 if (list_empty(&child->cg_list)) {
5689 get_css_set(cset);
5690 cset->nr_tasks++;
5691 css_set_move_task(child, NULL, cset, false);
5693 spin_unlock_irq(&css_set_lock);
5697 * Call ss->fork(). This must happen after @child is linked on
5698 * css_set; otherwise, @child might change state between ->fork()
5699 * and addition to css_set.
5701 do_each_subsys_mask(ss, i, have_fork_callback) {
5702 ss->fork(child);
5703 } while_each_subsys_mask();
5707 * cgroup_exit - detach cgroup from exiting task
5708 * @tsk: pointer to task_struct of exiting process
5710 * Description: Detach cgroup from @tsk and release it.
5712 * Note that cgroups marked notify_on_release force every task in
5713 * them to take the global cgroup_mutex mutex when exiting.
5714 * This could impact scaling on very large systems. Be reluctant to
5715 * use notify_on_release cgroups where very high task exit scaling
5716 * is required on large systems.
5718 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5719 * call cgroup_exit() while the task is still competent to handle
5720 * notify_on_release(), then leave the task attached to the root cgroup in
5721 * each hierarchy for the remainder of its exit. No need to bother with
5722 * init_css_set refcnting. init_css_set never goes away and we can't race
5723 * with migration path - PF_EXITING is visible to migration path.
5725 void cgroup_exit(struct task_struct *tsk)
5727 struct cgroup_subsys *ss;
5728 struct css_set *cset;
5729 int i;
5732 * Unlink from @tsk from its css_set. As migration path can't race
5733 * with us, we can check css_set and cg_list without synchronization.
5735 cset = task_css_set(tsk);
5737 if (!list_empty(&tsk->cg_list)) {
5738 spin_lock_irq(&css_set_lock);
5739 css_set_move_task(tsk, cset, NULL, false);
5740 cset->nr_tasks--;
5741 spin_unlock_irq(&css_set_lock);
5742 } else {
5743 get_css_set(cset);
5746 /* see cgroup_post_fork() for details */
5747 do_each_subsys_mask(ss, i, have_exit_callback) {
5748 ss->exit(tsk);
5749 } while_each_subsys_mask();
5752 void cgroup_free(struct task_struct *task)
5754 struct css_set *cset = task_css_set(task);
5755 struct cgroup_subsys *ss;
5756 int ssid;
5758 do_each_subsys_mask(ss, ssid, have_free_callback) {
5759 ss->free(task);
5760 } while_each_subsys_mask();
5762 put_css_set(cset);
5765 static int __init cgroup_disable(char *str)
5767 struct cgroup_subsys *ss;
5768 char *token;
5769 int i;
5771 while ((token = strsep(&str, ",")) != NULL) {
5772 if (!*token)
5773 continue;
5775 for_each_subsys(ss, i) {
5776 if (strcmp(token, ss->name) &&
5777 strcmp(token, ss->legacy_name))
5778 continue;
5779 cgroup_disable_mask |= 1 << i;
5782 return 1;
5784 __setup("cgroup_disable=", cgroup_disable);
5786 void __init __weak enable_debug_cgroup(void) { }
5788 static int __init enable_cgroup_debug(char *str)
5790 cgroup_debug = true;
5791 enable_debug_cgroup();
5792 return 1;
5794 __setup("cgroup_debug", enable_cgroup_debug);
5797 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
5798 * @dentry: directory dentry of interest
5799 * @ss: subsystem of interest
5801 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5802 * to get the corresponding css and return it. If such css doesn't exist
5803 * or can't be pinned, an ERR_PTR value is returned.
5805 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
5806 struct cgroup_subsys *ss)
5808 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
5809 struct file_system_type *s_type = dentry->d_sb->s_type;
5810 struct cgroup_subsys_state *css = NULL;
5811 struct cgroup *cgrp;
5813 /* is @dentry a cgroup dir? */
5814 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
5815 !kn || kernfs_type(kn) != KERNFS_DIR)
5816 return ERR_PTR(-EBADF);
5818 rcu_read_lock();
5821 * This path doesn't originate from kernfs and @kn could already
5822 * have been or be removed at any point. @kn->priv is RCU
5823 * protected for this access. See css_release_work_fn() for details.
5825 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
5826 if (cgrp)
5827 css = cgroup_css(cgrp, ss);
5829 if (!css || !css_tryget_online(css))
5830 css = ERR_PTR(-ENOENT);
5832 rcu_read_unlock();
5833 return css;
5837 * css_from_id - lookup css by id
5838 * @id: the cgroup id
5839 * @ss: cgroup subsys to be looked into
5841 * Returns the css if there's valid one with @id, otherwise returns NULL.
5842 * Should be called under rcu_read_lock().
5844 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5846 WARN_ON_ONCE(!rcu_read_lock_held());
5847 return idr_find(&ss->css_idr, id);
5851 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5852 * @path: path on the default hierarchy
5854 * Find the cgroup at @path on the default hierarchy, increment its
5855 * reference count and return it. Returns pointer to the found cgroup on
5856 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5857 * if @path points to a non-directory.
5859 struct cgroup *cgroup_get_from_path(const char *path)
5861 struct kernfs_node *kn;
5862 struct cgroup *cgrp;
5864 mutex_lock(&cgroup_mutex);
5866 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
5867 if (kn) {
5868 if (kernfs_type(kn) == KERNFS_DIR) {
5869 cgrp = kn->priv;
5870 cgroup_get_live(cgrp);
5871 } else {
5872 cgrp = ERR_PTR(-ENOTDIR);
5874 kernfs_put(kn);
5875 } else {
5876 cgrp = ERR_PTR(-ENOENT);
5879 mutex_unlock(&cgroup_mutex);
5880 return cgrp;
5882 EXPORT_SYMBOL_GPL(cgroup_get_from_path);
5885 * cgroup_get_from_fd - get a cgroup pointer from a fd
5886 * @fd: fd obtained by open(cgroup2_dir)
5888 * Find the cgroup from a fd which should be obtained
5889 * by opening a cgroup directory. Returns a pointer to the
5890 * cgroup on success. ERR_PTR is returned if the cgroup
5891 * cannot be found.
5893 struct cgroup *cgroup_get_from_fd(int fd)
5895 struct cgroup_subsys_state *css;
5896 struct cgroup *cgrp;
5897 struct file *f;
5899 f = fget_raw(fd);
5900 if (!f)
5901 return ERR_PTR(-EBADF);
5903 css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
5904 fput(f);
5905 if (IS_ERR(css))
5906 return ERR_CAST(css);
5908 cgrp = css->cgroup;
5909 if (!cgroup_on_dfl(cgrp)) {
5910 cgroup_put(cgrp);
5911 return ERR_PTR(-EBADF);
5914 return cgrp;
5916 EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
5919 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5920 * definition in cgroup-defs.h.
5922 #ifdef CONFIG_SOCK_CGROUP_DATA
5924 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5926 DEFINE_SPINLOCK(cgroup_sk_update_lock);
5927 static bool cgroup_sk_alloc_disabled __read_mostly;
5929 void cgroup_sk_alloc_disable(void)
5931 if (cgroup_sk_alloc_disabled)
5932 return;
5933 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5934 cgroup_sk_alloc_disabled = true;
5937 #else
5939 #define cgroup_sk_alloc_disabled false
5941 #endif
5943 void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
5945 if (cgroup_sk_alloc_disabled)
5946 return;
5948 /* Socket clone path */
5949 if (skcd->val) {
5951 * We might be cloning a socket which is left in an empty
5952 * cgroup and the cgroup might have already been rmdir'd.
5953 * Don't use cgroup_get_live().
5955 cgroup_get(sock_cgroup_ptr(skcd));
5956 return;
5959 rcu_read_lock();
5961 while (true) {
5962 struct css_set *cset;
5964 cset = task_css_set(current);
5965 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
5966 skcd->val = (unsigned long)cset->dfl_cgrp;
5967 break;
5969 cpu_relax();
5972 rcu_read_unlock();
5975 void cgroup_sk_free(struct sock_cgroup_data *skcd)
5977 cgroup_put(sock_cgroup_ptr(skcd));
5980 #endif /* CONFIG_SOCK_CGROUP_DATA */
5982 #ifdef CONFIG_CGROUP_BPF
5983 int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
5984 enum bpf_attach_type type, u32 flags)
5986 int ret;
5988 mutex_lock(&cgroup_mutex);
5989 ret = __cgroup_bpf_attach(cgrp, prog, type, flags);
5990 mutex_unlock(&cgroup_mutex);
5991 return ret;
5993 int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
5994 enum bpf_attach_type type, u32 flags)
5996 int ret;
5998 mutex_lock(&cgroup_mutex);
5999 ret = __cgroup_bpf_detach(cgrp, prog, type, flags);
6000 mutex_unlock(&cgroup_mutex);
6001 return ret;
6003 int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
6004 union bpf_attr __user *uattr)
6006 int ret;
6008 mutex_lock(&cgroup_mutex);
6009 ret = __cgroup_bpf_query(cgrp, attr, uattr);
6010 mutex_unlock(&cgroup_mutex);
6011 return ret;
6013 #endif /* CONFIG_CGROUP_BPF */
6015 #ifdef CONFIG_SYSFS
6016 static ssize_t show_delegatable_files(struct cftype *files, char *buf,
6017 ssize_t size, const char *prefix)
6019 struct cftype *cft;
6020 ssize_t ret = 0;
6022 for (cft = files; cft && cft->name[0] != '\0'; cft++) {
6023 if (!(cft->flags & CFTYPE_NS_DELEGATABLE))
6024 continue;
6026 if (prefix)
6027 ret += snprintf(buf + ret, size - ret, "%s.", prefix);
6029 ret += snprintf(buf + ret, size - ret, "%s\n", cft->name);
6031 if (WARN_ON(ret >= size))
6032 break;
6035 return ret;
6038 static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr,
6039 char *buf)
6041 struct cgroup_subsys *ss;
6042 int ssid;
6043 ssize_t ret = 0;
6045 ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret,
6046 NULL);
6048 for_each_subsys(ss, ssid)
6049 ret += show_delegatable_files(ss->dfl_cftypes, buf + ret,
6050 PAGE_SIZE - ret,
6051 cgroup_subsys_name[ssid]);
6053 return ret;
6055 static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate);
6057 static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr,
6058 char *buf)
6060 return snprintf(buf, PAGE_SIZE, "nsdelegate\n");
6062 static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features);
6064 static struct attribute *cgroup_sysfs_attrs[] = {
6065 &cgroup_delegate_attr.attr,
6066 &cgroup_features_attr.attr,
6067 NULL,
6070 static const struct attribute_group cgroup_sysfs_attr_group = {
6071 .attrs = cgroup_sysfs_attrs,
6072 .name = "cgroup",
6075 static int __init cgroup_sysfs_init(void)
6077 return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group);
6079 subsys_initcall(cgroup_sysfs_init);
6080 #endif /* CONFIG_SYSFS */