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/psi.h>
60 #define CREATE_TRACE_POINTS
61 #include <trace/events/cgroup.h>
63 #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
70 * css_set_lock protects task->cgroups pointer, the list of css_set
71 * objects, and the chain of tasks off each css_set.
73 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
74 * cgroup.h can use them for lockdep annotations.
76 DEFINE_MUTEX(cgroup_mutex
);
77 DEFINE_SPINLOCK(css_set_lock
);
79 #ifdef CONFIG_PROVE_RCU
80 EXPORT_SYMBOL_GPL(cgroup_mutex
);
81 EXPORT_SYMBOL_GPL(css_set_lock
);
85 * Protects cgroup_idr and css_idr so that IDs can be released without
86 * grabbing cgroup_mutex.
88 static DEFINE_SPINLOCK(cgroup_idr_lock
);
91 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
92 * against file removal/re-creation across css hiding.
94 static DEFINE_SPINLOCK(cgroup_file_kn_lock
);
96 struct percpu_rw_semaphore cgroup_threadgroup_rwsem
;
98 #define cgroup_assert_mutex_or_rcu_locked() \
99 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
100 !lockdep_is_held(&cgroup_mutex), \
101 "cgroup_mutex or RCU read lock required");
104 * cgroup destruction makes heavy use of work items and there can be a lot
105 * of concurrent destructions. Use a separate workqueue so that cgroup
106 * destruction work items don't end up filling up max_active of system_wq
107 * which may lead to deadlock.
109 static struct workqueue_struct
*cgroup_destroy_wq
;
111 /* generate an array of cgroup subsystem pointers */
112 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
113 struct cgroup_subsys
*cgroup_subsys
[] = {
114 #include <linux/cgroup_subsys.h>
118 /* array of cgroup subsystem names */
119 #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
120 static const char *cgroup_subsys_name
[] = {
121 #include <linux/cgroup_subsys.h>
125 /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
127 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
128 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
129 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
130 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
131 #include <linux/cgroup_subsys.h>
134 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
135 static struct static_key_true
*cgroup_subsys_enabled_key
[] = {
136 #include <linux/cgroup_subsys.h>
140 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
141 static struct static_key_true
*cgroup_subsys_on_dfl_key
[] = {
142 #include <linux/cgroup_subsys.h>
147 * The default hierarchy, reserved for the subsystems that are otherwise
148 * unattached - it never has more than a single cgroup, and all tasks are
149 * part of that cgroup.
151 struct cgroup_root cgrp_dfl_root
;
152 EXPORT_SYMBOL_GPL(cgrp_dfl_root
);
155 * The default hierarchy always exists but is hidden until mounted for the
156 * first time. This is for backward compatibility.
158 static bool cgrp_dfl_visible
;
160 /* some controllers are not supported in the default hierarchy */
161 static u16 cgrp_dfl_inhibit_ss_mask
;
163 /* some controllers are implicitly enabled on the default hierarchy */
164 static u16 cgrp_dfl_implicit_ss_mask
;
166 /* some controllers can be threaded on the default hierarchy */
167 static u16 cgrp_dfl_threaded_ss_mask
;
169 /* The list of hierarchy roots */
170 LIST_HEAD(cgroup_roots
);
171 static int cgroup_root_count
;
173 /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
174 static DEFINE_IDR(cgroup_hierarchy_idr
);
177 * Assign a monotonically increasing serial number to csses. It guarantees
178 * cgroups with bigger numbers are newer than those with smaller numbers.
179 * Also, as csses are always appended to the parent's ->children list, it
180 * guarantees that sibling csses are always sorted in the ascending serial
181 * number order on the list. Protected by cgroup_mutex.
183 static u64 css_serial_nr_next
= 1;
186 * These bitmasks identify subsystems with specific features to avoid
187 * having to do iterative checks repeatedly.
189 static u16 have_fork_callback __read_mostly
;
190 static u16 have_exit_callback __read_mostly
;
191 static u16 have_release_callback __read_mostly
;
192 static u16 have_canfork_callback __read_mostly
;
194 /* cgroup namespace for init task */
195 struct cgroup_namespace init_cgroup_ns
= {
196 .count
= REFCOUNT_INIT(2),
197 .user_ns
= &init_user_ns
,
198 .ns
.ops
= &cgroupns_operations
,
199 .ns
.inum
= PROC_CGROUP_INIT_INO
,
200 .root_cset
= &init_css_set
,
203 static struct file_system_type cgroup2_fs_type
;
204 static struct cftype cgroup_base_files
[];
206 static int cgroup_apply_control(struct cgroup
*cgrp
);
207 static void cgroup_finalize_control(struct cgroup
*cgrp
, int ret
);
208 static void css_task_iter_advance(struct css_task_iter
*it
);
209 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
210 static struct cgroup_subsys_state
*css_create(struct cgroup
*cgrp
,
211 struct cgroup_subsys
*ss
);
212 static void css_release(struct percpu_ref
*ref
);
213 static void kill_css(struct cgroup_subsys_state
*css
);
214 static int cgroup_addrm_files(struct cgroup_subsys_state
*css
,
215 struct cgroup
*cgrp
, struct cftype cfts
[],
219 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
220 * @ssid: subsys ID of interest
222 * cgroup_subsys_enabled() can only be used with literal subsys names which
223 * is fine for individual subsystems but unsuitable for cgroup core. This
224 * is slower static_key_enabled() based test indexed by @ssid.
226 bool cgroup_ssid_enabled(int ssid
)
228 if (CGROUP_SUBSYS_COUNT
== 0)
231 return static_key_enabled(cgroup_subsys_enabled_key
[ssid
]);
235 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
236 * @cgrp: the cgroup of interest
238 * The default hierarchy is the v2 interface of cgroup and this function
239 * can be used to test whether a cgroup is on the default hierarchy for
240 * cases where a subsystem should behave differnetly depending on the
243 * The set of behaviors which change on the default hierarchy are still
244 * being determined and the mount option is prefixed with __DEVEL__.
246 * List of changed behaviors:
248 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
249 * and "name" are disallowed.
251 * - When mounting an existing superblock, mount options should match.
253 * - Remount is disallowed.
255 * - rename(2) is disallowed.
257 * - "tasks" is removed. Everything should be at process granularity. Use
258 * "cgroup.procs" instead.
260 * - "cgroup.procs" is not sorted. pids will be unique unless they got
261 * recycled inbetween reads.
263 * - "release_agent" and "notify_on_release" are removed. Replacement
264 * notification mechanism will be implemented.
266 * - "cgroup.clone_children" is removed.
268 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
269 * and its descendants contain no task; otherwise, 1. The file also
270 * generates kernfs notification which can be monitored through poll and
271 * [di]notify when the value of the file changes.
273 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
274 * take masks of ancestors with non-empty cpus/mems, instead of being
275 * moved to an ancestor.
277 * - cpuset: a task can be moved into an empty cpuset, and again it takes
278 * masks of ancestors.
280 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
283 * - blkcg: blk-throttle becomes properly hierarchical.
285 * - debug: disallowed on the default hierarchy.
287 bool cgroup_on_dfl(const struct cgroup
*cgrp
)
289 return cgrp
->root
== &cgrp_dfl_root
;
292 /* IDR wrappers which synchronize using cgroup_idr_lock */
293 static int cgroup_idr_alloc(struct idr
*idr
, void *ptr
, int start
, int end
,
298 idr_preload(gfp_mask
);
299 spin_lock_bh(&cgroup_idr_lock
);
300 ret
= idr_alloc(idr
, ptr
, start
, end
, gfp_mask
& ~__GFP_DIRECT_RECLAIM
);
301 spin_unlock_bh(&cgroup_idr_lock
);
306 static void *cgroup_idr_replace(struct idr
*idr
, void *ptr
, int id
)
310 spin_lock_bh(&cgroup_idr_lock
);
311 ret
= idr_replace(idr
, ptr
, id
);
312 spin_unlock_bh(&cgroup_idr_lock
);
316 static void cgroup_idr_remove(struct idr
*idr
, int id
)
318 spin_lock_bh(&cgroup_idr_lock
);
320 spin_unlock_bh(&cgroup_idr_lock
);
323 static bool cgroup_has_tasks(struct cgroup
*cgrp
)
325 return cgrp
->nr_populated_csets
;
328 bool cgroup_is_threaded(struct cgroup
*cgrp
)
330 return cgrp
->dom_cgrp
!= cgrp
;
333 /* can @cgrp host both domain and threaded children? */
334 static bool cgroup_is_mixable(struct cgroup
*cgrp
)
337 * Root isn't under domain level resource control exempting it from
338 * the no-internal-process constraint, so it can serve as a thread
339 * root and a parent of resource domains at the same time.
341 return !cgroup_parent(cgrp
);
344 /* can @cgrp become a thread root? should always be true for a thread root */
345 static bool cgroup_can_be_thread_root(struct cgroup
*cgrp
)
347 /* mixables don't care */
348 if (cgroup_is_mixable(cgrp
))
351 /* domain roots can't be nested under threaded */
352 if (cgroup_is_threaded(cgrp
))
355 /* can only have either domain or threaded children */
356 if (cgrp
->nr_populated_domain_children
)
359 /* and no domain controllers can be enabled */
360 if (cgrp
->subtree_control
& ~cgrp_dfl_threaded_ss_mask
)
366 /* is @cgrp root of a threaded subtree? */
367 bool cgroup_is_thread_root(struct cgroup
*cgrp
)
369 /* thread root should be a domain */
370 if (cgroup_is_threaded(cgrp
))
373 /* a domain w/ threaded children is a thread root */
374 if (cgrp
->nr_threaded_children
)
378 * A domain which has tasks and explicit threaded controllers
379 * enabled is a thread root.
381 if (cgroup_has_tasks(cgrp
) &&
382 (cgrp
->subtree_control
& cgrp_dfl_threaded_ss_mask
))
388 /* a domain which isn't connected to the root w/o brekage can't be used */
389 static bool cgroup_is_valid_domain(struct cgroup
*cgrp
)
391 /* the cgroup itself can be a thread root */
392 if (cgroup_is_threaded(cgrp
))
395 /* but the ancestors can't be unless mixable */
396 while ((cgrp
= cgroup_parent(cgrp
))) {
397 if (!cgroup_is_mixable(cgrp
) && cgroup_is_thread_root(cgrp
))
399 if (cgroup_is_threaded(cgrp
))
406 /* subsystems visibly enabled on a cgroup */
407 static u16
cgroup_control(struct cgroup
*cgrp
)
409 struct cgroup
*parent
= cgroup_parent(cgrp
);
410 u16 root_ss_mask
= cgrp
->root
->subsys_mask
;
413 u16 ss_mask
= parent
->subtree_control
;
415 /* threaded cgroups can only have threaded controllers */
416 if (cgroup_is_threaded(cgrp
))
417 ss_mask
&= cgrp_dfl_threaded_ss_mask
;
421 if (cgroup_on_dfl(cgrp
))
422 root_ss_mask
&= ~(cgrp_dfl_inhibit_ss_mask
|
423 cgrp_dfl_implicit_ss_mask
);
427 /* subsystems enabled on a cgroup */
428 static u16
cgroup_ss_mask(struct cgroup
*cgrp
)
430 struct cgroup
*parent
= cgroup_parent(cgrp
);
433 u16 ss_mask
= parent
->subtree_ss_mask
;
435 /* threaded cgroups can only have threaded controllers */
436 if (cgroup_is_threaded(cgrp
))
437 ss_mask
&= cgrp_dfl_threaded_ss_mask
;
441 return cgrp
->root
->subsys_mask
;
445 * cgroup_css - obtain a cgroup's css for the specified subsystem
446 * @cgrp: the cgroup of interest
447 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
449 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
450 * function must be called either under cgroup_mutex or rcu_read_lock() and
451 * the caller is responsible for pinning the returned css if it wants to
452 * keep accessing it outside the said locks. This function may return
453 * %NULL if @cgrp doesn't have @subsys_id enabled.
455 static struct cgroup_subsys_state
*cgroup_css(struct cgroup
*cgrp
,
456 struct cgroup_subsys
*ss
)
459 return rcu_dereference_check(cgrp
->subsys
[ss
->id
],
460 lockdep_is_held(&cgroup_mutex
));
466 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
467 * @cgrp: the cgroup of interest
468 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
470 * Similar to cgroup_css() but returns the effective css, which is defined
471 * as the matching css of the nearest ancestor including self which has @ss
472 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
473 * function is guaranteed to return non-NULL css.
475 static struct cgroup_subsys_state
*cgroup_e_css(struct cgroup
*cgrp
,
476 struct cgroup_subsys
*ss
)
478 lockdep_assert_held(&cgroup_mutex
);
484 * This function is used while updating css associations and thus
485 * can't test the csses directly. Test ss_mask.
487 while (!(cgroup_ss_mask(cgrp
) & (1 << ss
->id
))) {
488 cgrp
= cgroup_parent(cgrp
);
493 return cgroup_css(cgrp
, ss
);
497 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
498 * @cgrp: the cgroup of interest
499 * @ss: the subsystem of interest
501 * Find and get the effective css of @cgrp for @ss. The effective css is
502 * defined as the matching css of the nearest ancestor including self which
503 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
504 * the root css is returned, so this function always returns a valid css.
505 * The returned css must be put using css_put().
507 struct cgroup_subsys_state
*cgroup_get_e_css(struct cgroup
*cgrp
,
508 struct cgroup_subsys
*ss
)
510 struct cgroup_subsys_state
*css
;
515 css
= cgroup_css(cgrp
, ss
);
517 if (css
&& css_tryget_online(css
))
519 cgrp
= cgroup_parent(cgrp
);
522 css
= init_css_set
.subsys
[ss
->id
];
529 static void cgroup_get_live(struct cgroup
*cgrp
)
531 WARN_ON_ONCE(cgroup_is_dead(cgrp
));
532 css_get(&cgrp
->self
);
535 struct cgroup_subsys_state
*of_css(struct kernfs_open_file
*of
)
537 struct cgroup
*cgrp
= of
->kn
->parent
->priv
;
538 struct cftype
*cft
= of_cft(of
);
541 * This is open and unprotected implementation of cgroup_css().
542 * seq_css() is only called from a kernfs file operation which has
543 * an active reference on the file. Because all the subsystem
544 * files are drained before a css is disassociated with a cgroup,
545 * the matching css from the cgroup's subsys table is guaranteed to
546 * be and stay valid until the enclosing operation is complete.
549 return rcu_dereference_raw(cgrp
->subsys
[cft
->ss
->id
]);
553 EXPORT_SYMBOL_GPL(of_css
);
556 * for_each_css - iterate all css's of a cgroup
557 * @css: the iteration cursor
558 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
559 * @cgrp: the target cgroup to iterate css's of
561 * Should be called under cgroup_[tree_]mutex.
563 #define for_each_css(css, ssid, cgrp) \
564 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
565 if (!((css) = rcu_dereference_check( \
566 (cgrp)->subsys[(ssid)], \
567 lockdep_is_held(&cgroup_mutex)))) { } \
571 * for_each_e_css - iterate all effective css's of a cgroup
572 * @css: the iteration cursor
573 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
574 * @cgrp: the target cgroup to iterate css's of
576 * Should be called under cgroup_[tree_]mutex.
578 #define for_each_e_css(css, ssid, cgrp) \
579 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
580 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
585 * do_each_subsys_mask - filter for_each_subsys with a bitmask
586 * @ss: the iteration cursor
587 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
588 * @ss_mask: the bitmask
590 * The block will only run for cases where the ssid-th bit (1 << ssid) of
593 #define do_each_subsys_mask(ss, ssid, ss_mask) do { \
594 unsigned long __ss_mask = (ss_mask); \
595 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
599 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
600 (ss) = cgroup_subsys[ssid]; \
603 #define while_each_subsys_mask() \
608 /* iterate over child cgrps, lock should be held throughout iteration */
609 #define cgroup_for_each_live_child(child, cgrp) \
610 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
611 if (({ lockdep_assert_held(&cgroup_mutex); \
612 cgroup_is_dead(child); })) \
616 /* walk live descendants in preorder */
617 #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
618 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
619 if (({ lockdep_assert_held(&cgroup_mutex); \
620 (dsct) = (d_css)->cgroup; \
621 cgroup_is_dead(dsct); })) \
625 /* walk live descendants in postorder */
626 #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
627 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
628 if (({ lockdep_assert_held(&cgroup_mutex); \
629 (dsct) = (d_css)->cgroup; \
630 cgroup_is_dead(dsct); })) \
635 * The default css_set - used by init and its children prior to any
636 * hierarchies being mounted. It contains a pointer to the root state
637 * for each subsystem. Also used to anchor the list of css_sets. Not
638 * reference-counted, to improve performance when child cgroups
639 * haven't been created.
641 struct css_set init_css_set
= {
642 .refcount
= REFCOUNT_INIT(1),
643 .dom_cset
= &init_css_set
,
644 .tasks
= LIST_HEAD_INIT(init_css_set
.tasks
),
645 .mg_tasks
= LIST_HEAD_INIT(init_css_set
.mg_tasks
),
646 .task_iters
= LIST_HEAD_INIT(init_css_set
.task_iters
),
647 .threaded_csets
= LIST_HEAD_INIT(init_css_set
.threaded_csets
),
648 .cgrp_links
= LIST_HEAD_INIT(init_css_set
.cgrp_links
),
649 .mg_preload_node
= LIST_HEAD_INIT(init_css_set
.mg_preload_node
),
650 .mg_node
= LIST_HEAD_INIT(init_css_set
.mg_node
),
653 static int css_set_count
= 1; /* 1 for init_css_set */
655 static bool css_set_threaded(struct css_set
*cset
)
657 return cset
->dom_cset
!= cset
;
661 * css_set_populated - does a css_set contain any tasks?
662 * @cset: target css_set
664 * css_set_populated() should be the same as !!cset->nr_tasks at steady
665 * state. However, css_set_populated() can be called while a task is being
666 * added to or removed from the linked list before the nr_tasks is
667 * properly updated. Hence, we can't just look at ->nr_tasks here.
669 static bool css_set_populated(struct css_set
*cset
)
671 lockdep_assert_held(&css_set_lock
);
673 return !list_empty(&cset
->tasks
) || !list_empty(&cset
->mg_tasks
);
677 * cgroup_update_populated - update the populated count of a cgroup
678 * @cgrp: the target cgroup
679 * @populated: inc or dec populated count
681 * One of the css_sets associated with @cgrp is either getting its first
682 * task or losing the last. Update @cgrp->nr_populated_* accordingly. The
683 * count is propagated towards root so that a given cgroup's
684 * nr_populated_children is zero iff none of its descendants contain any
687 * @cgrp's interface file "cgroup.populated" is zero if both
688 * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and
689 * 1 otherwise. When the sum changes from or to zero, userland is notified
690 * that the content of the interface file has changed. This can be used to
691 * detect when @cgrp and its descendants become populated or empty.
693 static void cgroup_update_populated(struct cgroup
*cgrp
, bool populated
)
695 struct cgroup
*child
= NULL
;
696 int adj
= populated
? 1 : -1;
698 lockdep_assert_held(&css_set_lock
);
701 bool was_populated
= cgroup_is_populated(cgrp
);
704 cgrp
->nr_populated_csets
+= adj
;
706 if (cgroup_is_threaded(child
))
707 cgrp
->nr_populated_threaded_children
+= adj
;
709 cgrp
->nr_populated_domain_children
+= adj
;
712 if (was_populated
== cgroup_is_populated(cgrp
))
715 cgroup1_check_for_release(cgrp
);
716 cgroup_file_notify(&cgrp
->events_file
);
719 cgrp
= cgroup_parent(cgrp
);
724 * css_set_update_populated - update populated state of a css_set
725 * @cset: target css_set
726 * @populated: whether @cset is populated or depopulated
728 * @cset is either getting the first task or losing the last. Update the
729 * populated counters of all associated cgroups accordingly.
731 static void css_set_update_populated(struct css_set
*cset
, bool populated
)
733 struct cgrp_cset_link
*link
;
735 lockdep_assert_held(&css_set_lock
);
737 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
)
738 cgroup_update_populated(link
->cgrp
, populated
);
742 * css_set_move_task - move a task from one css_set to another
743 * @task: task being moved
744 * @from_cset: css_set @task currently belongs to (may be NULL)
745 * @to_cset: new css_set @task is being moved to (may be NULL)
746 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
748 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
749 * css_set, @from_cset can be NULL. If @task is being disassociated
750 * instead of moved, @to_cset can be NULL.
752 * This function automatically handles populated counter updates and
753 * css_task_iter adjustments but the caller is responsible for managing
754 * @from_cset and @to_cset's reference counts.
756 static void css_set_move_task(struct task_struct
*task
,
757 struct css_set
*from_cset
, struct css_set
*to_cset
,
760 lockdep_assert_held(&css_set_lock
);
762 if (to_cset
&& !css_set_populated(to_cset
))
763 css_set_update_populated(to_cset
, true);
766 struct css_task_iter
*it
, *pos
;
768 WARN_ON_ONCE(list_empty(&task
->cg_list
));
771 * @task is leaving, advance task iterators which are
772 * pointing to it so that they can resume at the next
773 * position. Advancing an iterator might remove it from
774 * the list, use safe walk. See css_task_iter_advance*()
777 list_for_each_entry_safe(it
, pos
, &from_cset
->task_iters
,
779 if (it
->task_pos
== &task
->cg_list
)
780 css_task_iter_advance(it
);
782 list_del_init(&task
->cg_list
);
783 if (!css_set_populated(from_cset
))
784 css_set_update_populated(from_cset
, false);
786 WARN_ON_ONCE(!list_empty(&task
->cg_list
));
791 * We are synchronized through cgroup_threadgroup_rwsem
792 * against PF_EXITING setting such that we can't race
793 * against cgroup_exit() changing the css_set to
794 * init_css_set and dropping the old one.
796 WARN_ON_ONCE(task
->flags
& PF_EXITING
);
798 cgroup_move_task(task
, to_cset
);
799 list_add_tail(&task
->cg_list
, use_mg_tasks
? &to_cset
->mg_tasks
:
805 * hash table for cgroup groups. This improves the performance to find
806 * an existing css_set. This hash doesn't (currently) take into
807 * account cgroups in empty hierarchies.
809 #define CSS_SET_HASH_BITS 7
810 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
812 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
814 unsigned long key
= 0UL;
815 struct cgroup_subsys
*ss
;
818 for_each_subsys(ss
, i
)
819 key
+= (unsigned long)css
[i
];
820 key
= (key
>> 16) ^ key
;
825 void put_css_set_locked(struct css_set
*cset
)
827 struct cgrp_cset_link
*link
, *tmp_link
;
828 struct cgroup_subsys
*ss
;
831 lockdep_assert_held(&css_set_lock
);
833 if (!refcount_dec_and_test(&cset
->refcount
))
836 WARN_ON_ONCE(!list_empty(&cset
->threaded_csets
));
838 /* This css_set is dead. unlink it and release cgroup and css refs */
839 for_each_subsys(ss
, ssid
) {
840 list_del(&cset
->e_cset_node
[ssid
]);
841 css_put(cset
->subsys
[ssid
]);
843 hash_del(&cset
->hlist
);
846 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
847 list_del(&link
->cset_link
);
848 list_del(&link
->cgrp_link
);
849 if (cgroup_parent(link
->cgrp
))
850 cgroup_put(link
->cgrp
);
854 if (css_set_threaded(cset
)) {
855 list_del(&cset
->threaded_csets_node
);
856 put_css_set_locked(cset
->dom_cset
);
859 kfree_rcu(cset
, rcu_head
);
863 * compare_css_sets - helper function for find_existing_css_set().
864 * @cset: candidate css_set being tested
865 * @old_cset: existing css_set for a task
866 * @new_cgrp: cgroup that's being entered by the task
867 * @template: desired set of css pointers in css_set (pre-calculated)
869 * Returns true if "cset" matches "old_cset" except for the hierarchy
870 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
872 static bool compare_css_sets(struct css_set
*cset
,
873 struct css_set
*old_cset
,
874 struct cgroup
*new_cgrp
,
875 struct cgroup_subsys_state
*template[])
877 struct cgroup
*new_dfl_cgrp
;
878 struct list_head
*l1
, *l2
;
881 * On the default hierarchy, there can be csets which are
882 * associated with the same set of cgroups but different csses.
883 * Let's first ensure that csses match.
885 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
)))
889 /* @cset's domain should match the default cgroup's */
890 if (cgroup_on_dfl(new_cgrp
))
891 new_dfl_cgrp
= new_cgrp
;
893 new_dfl_cgrp
= old_cset
->dfl_cgrp
;
895 if (new_dfl_cgrp
->dom_cgrp
!= cset
->dom_cset
->dfl_cgrp
)
899 * Compare cgroup pointers in order to distinguish between
900 * different cgroups in hierarchies. As different cgroups may
901 * share the same effective css, this comparison is always
904 l1
= &cset
->cgrp_links
;
905 l2
= &old_cset
->cgrp_links
;
907 struct cgrp_cset_link
*link1
, *link2
;
908 struct cgroup
*cgrp1
, *cgrp2
;
912 /* See if we reached the end - both lists are equal length. */
913 if (l1
== &cset
->cgrp_links
) {
914 BUG_ON(l2
!= &old_cset
->cgrp_links
);
917 BUG_ON(l2
== &old_cset
->cgrp_links
);
919 /* Locate the cgroups associated with these links. */
920 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
921 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
924 /* Hierarchies should be linked in the same order. */
925 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
928 * If this hierarchy is the hierarchy of the cgroup
929 * that's changing, then we need to check that this
930 * css_set points to the new cgroup; if it's any other
931 * hierarchy, then this css_set should point to the
932 * same cgroup as the old css_set.
934 if (cgrp1
->root
== new_cgrp
->root
) {
935 if (cgrp1
!= new_cgrp
)
946 * find_existing_css_set - init css array and find the matching css_set
947 * @old_cset: the css_set that we're using before the cgroup transition
948 * @cgrp: the cgroup that we're moving into
949 * @template: out param for the new set of csses, should be clear on entry
951 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
953 struct cgroup_subsys_state
*template[])
955 struct cgroup_root
*root
= cgrp
->root
;
956 struct cgroup_subsys
*ss
;
957 struct css_set
*cset
;
962 * Build the set of subsystem state objects that we want to see in the
963 * new css_set. while subsystems can change globally, the entries here
964 * won't change, so no need for locking.
966 for_each_subsys(ss
, i
) {
967 if (root
->subsys_mask
& (1UL << i
)) {
969 * @ss is in this hierarchy, so we want the
970 * effective css from @cgrp.
972 template[i
] = cgroup_e_css(cgrp
, ss
);
975 * @ss is not in this hierarchy, so we don't want
978 template[i
] = old_cset
->subsys
[i
];
982 key
= css_set_hash(template);
983 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
984 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
987 /* This css_set matches what we need */
991 /* No existing cgroup group matched */
995 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
997 struct cgrp_cset_link
*link
, *tmp_link
;
999 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
1000 list_del(&link
->cset_link
);
1006 * allocate_cgrp_cset_links - allocate cgrp_cset_links
1007 * @count: the number of links to allocate
1008 * @tmp_links: list_head the allocated links are put on
1010 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
1011 * through ->cset_link. Returns 0 on success or -errno.
1013 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
1015 struct cgrp_cset_link
*link
;
1018 INIT_LIST_HEAD(tmp_links
);
1020 for (i
= 0; i
< count
; i
++) {
1021 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
1023 free_cgrp_cset_links(tmp_links
);
1026 list_add(&link
->cset_link
, tmp_links
);
1032 * link_css_set - a helper function to link a css_set to a cgroup
1033 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
1034 * @cset: the css_set to be linked
1035 * @cgrp: the destination cgroup
1037 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
1038 struct cgroup
*cgrp
)
1040 struct cgrp_cset_link
*link
;
1042 BUG_ON(list_empty(tmp_links
));
1044 if (cgroup_on_dfl(cgrp
))
1045 cset
->dfl_cgrp
= cgrp
;
1047 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
1052 * Always add links to the tail of the lists so that the lists are
1053 * in choronological order.
1055 list_move_tail(&link
->cset_link
, &cgrp
->cset_links
);
1056 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
1058 if (cgroup_parent(cgrp
))
1059 cgroup_get_live(cgrp
);
1063 * find_css_set - return a new css_set with one cgroup updated
1064 * @old_cset: the baseline css_set
1065 * @cgrp: the cgroup to be updated
1067 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1068 * substituted into the appropriate hierarchy.
1070 static struct css_set
*find_css_set(struct css_set
*old_cset
,
1071 struct cgroup
*cgrp
)
1073 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
1074 struct css_set
*cset
;
1075 struct list_head tmp_links
;
1076 struct cgrp_cset_link
*link
;
1077 struct cgroup_subsys
*ss
;
1081 lockdep_assert_held(&cgroup_mutex
);
1083 /* First see if we already have a cgroup group that matches
1084 * the desired set */
1085 spin_lock_irq(&css_set_lock
);
1086 cset
= find_existing_css_set(old_cset
, cgrp
, template);
1089 spin_unlock_irq(&css_set_lock
);
1094 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
1098 /* Allocate all the cgrp_cset_link objects that we'll need */
1099 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
1104 refcount_set(&cset
->refcount
, 1);
1105 cset
->dom_cset
= cset
;
1106 INIT_LIST_HEAD(&cset
->tasks
);
1107 INIT_LIST_HEAD(&cset
->mg_tasks
);
1108 INIT_LIST_HEAD(&cset
->task_iters
);
1109 INIT_LIST_HEAD(&cset
->threaded_csets
);
1110 INIT_HLIST_NODE(&cset
->hlist
);
1111 INIT_LIST_HEAD(&cset
->cgrp_links
);
1112 INIT_LIST_HEAD(&cset
->mg_preload_node
);
1113 INIT_LIST_HEAD(&cset
->mg_node
);
1115 /* Copy the set of subsystem state objects generated in
1116 * find_existing_css_set() */
1117 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
1119 spin_lock_irq(&css_set_lock
);
1120 /* Add reference counts and links from the new css_set. */
1121 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
1122 struct cgroup
*c
= link
->cgrp
;
1124 if (c
->root
== cgrp
->root
)
1126 link_css_set(&tmp_links
, cset
, c
);
1129 BUG_ON(!list_empty(&tmp_links
));
1133 /* Add @cset to the hash table */
1134 key
= css_set_hash(cset
->subsys
);
1135 hash_add(css_set_table
, &cset
->hlist
, key
);
1137 for_each_subsys(ss
, ssid
) {
1138 struct cgroup_subsys_state
*css
= cset
->subsys
[ssid
];
1140 list_add_tail(&cset
->e_cset_node
[ssid
],
1141 &css
->cgroup
->e_csets
[ssid
]);
1145 spin_unlock_irq(&css_set_lock
);
1148 * If @cset should be threaded, look up the matching dom_cset and
1149 * link them up. We first fully initialize @cset then look for the
1150 * dom_cset. It's simpler this way and safe as @cset is guaranteed
1151 * to stay empty until we return.
1153 if (cgroup_is_threaded(cset
->dfl_cgrp
)) {
1154 struct css_set
*dcset
;
1156 dcset
= find_css_set(cset
, cset
->dfl_cgrp
->dom_cgrp
);
1162 spin_lock_irq(&css_set_lock
);
1163 cset
->dom_cset
= dcset
;
1164 list_add_tail(&cset
->threaded_csets_node
,
1165 &dcset
->threaded_csets
);
1166 spin_unlock_irq(&css_set_lock
);
1172 struct cgroup_root
*cgroup_root_from_kf(struct kernfs_root
*kf_root
)
1174 struct cgroup
*root_cgrp
= kf_root
->kn
->priv
;
1176 return root_cgrp
->root
;
1179 static int cgroup_init_root_id(struct cgroup_root
*root
)
1183 lockdep_assert_held(&cgroup_mutex
);
1185 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, 0, 0, GFP_KERNEL
);
1189 root
->hierarchy_id
= id
;
1193 static void cgroup_exit_root_id(struct cgroup_root
*root
)
1195 lockdep_assert_held(&cgroup_mutex
);
1197 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1200 void cgroup_free_root(struct cgroup_root
*root
)
1203 idr_destroy(&root
->cgroup_idr
);
1208 static void cgroup_destroy_root(struct cgroup_root
*root
)
1210 struct cgroup
*cgrp
= &root
->cgrp
;
1211 struct cgrp_cset_link
*link
, *tmp_link
;
1213 trace_cgroup_destroy_root(root
);
1215 cgroup_lock_and_drain_offline(&cgrp_dfl_root
.cgrp
);
1217 BUG_ON(atomic_read(&root
->nr_cgrps
));
1218 BUG_ON(!list_empty(&cgrp
->self
.children
));
1220 /* Rebind all subsystems back to the default hierarchy */
1221 WARN_ON(rebind_subsystems(&cgrp_dfl_root
, root
->subsys_mask
));
1224 * Release all the links from cset_links to this hierarchy's
1227 spin_lock_irq(&css_set_lock
);
1229 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1230 list_del(&link
->cset_link
);
1231 list_del(&link
->cgrp_link
);
1235 spin_unlock_irq(&css_set_lock
);
1237 if (!list_empty(&root
->root_list
)) {
1238 list_del(&root
->root_list
);
1239 cgroup_root_count
--;
1242 cgroup_exit_root_id(root
);
1244 mutex_unlock(&cgroup_mutex
);
1246 kernfs_destroy_root(root
->kf_root
);
1247 cgroup_free_root(root
);
1251 * look up cgroup associated with current task's cgroup namespace on the
1252 * specified hierarchy
1254 static struct cgroup
*
1255 current_cgns_cgroup_from_root(struct cgroup_root
*root
)
1257 struct cgroup
*res
= NULL
;
1258 struct css_set
*cset
;
1260 lockdep_assert_held(&css_set_lock
);
1264 cset
= current
->nsproxy
->cgroup_ns
->root_cset
;
1265 if (cset
== &init_css_set
) {
1268 struct cgrp_cset_link
*link
;
1270 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
1271 struct cgroup
*c
= link
->cgrp
;
1273 if (c
->root
== root
) {
1285 /* look up cgroup associated with given css_set on the specified hierarchy */
1286 static struct cgroup
*cset_cgroup_from_root(struct css_set
*cset
,
1287 struct cgroup_root
*root
)
1289 struct cgroup
*res
= NULL
;
1291 lockdep_assert_held(&cgroup_mutex
);
1292 lockdep_assert_held(&css_set_lock
);
1294 if (cset
== &init_css_set
) {
1296 } else if (root
== &cgrp_dfl_root
) {
1297 res
= cset
->dfl_cgrp
;
1299 struct cgrp_cset_link
*link
;
1301 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
1302 struct cgroup
*c
= link
->cgrp
;
1304 if (c
->root
== root
) {
1316 * Return the cgroup for "task" from the given hierarchy. Must be
1317 * called with cgroup_mutex and css_set_lock held.
1319 struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
1320 struct cgroup_root
*root
)
1323 * No need to lock the task - since we hold cgroup_mutex the
1324 * task can't change groups, so the only thing that can happen
1325 * is that it exits and its css is set back to init_css_set.
1327 return cset_cgroup_from_root(task_css_set(task
), root
);
1331 * A task must hold cgroup_mutex to modify cgroups.
1333 * Any task can increment and decrement the count field without lock.
1334 * So in general, code holding cgroup_mutex can't rely on the count
1335 * field not changing. However, if the count goes to zero, then only
1336 * cgroup_attach_task() can increment it again. Because a count of zero
1337 * means that no tasks are currently attached, therefore there is no
1338 * way a task attached to that cgroup can fork (the other way to
1339 * increment the count). So code holding cgroup_mutex can safely
1340 * assume that if the count is zero, it will stay zero. Similarly, if
1341 * a task holds cgroup_mutex on a cgroup with zero count, it
1342 * knows that the cgroup won't be removed, as cgroup_rmdir()
1345 * A cgroup can only be deleted if both its 'count' of using tasks
1346 * is zero, and its list of 'children' cgroups is empty. Since all
1347 * tasks in the system use _some_ cgroup, and since there is always at
1348 * least one task in the system (init, pid == 1), therefore, root cgroup
1349 * always has either children cgroups and/or using tasks. So we don't
1350 * need a special hack to ensure that root cgroup cannot be deleted.
1352 * P.S. One more locking exception. RCU is used to guard the
1353 * update of a tasks cgroup pointer by cgroup_attach_task()
1356 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
;
1358 static char *cgroup_file_name(struct cgroup
*cgrp
, const struct cftype
*cft
,
1361 struct cgroup_subsys
*ss
= cft
->ss
;
1363 if (cft
->ss
&& !(cft
->flags
& CFTYPE_NO_PREFIX
) &&
1364 !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
))
1365 snprintf(buf
, CGROUP_FILE_NAME_MAX
, "%s.%s",
1366 cgroup_on_dfl(cgrp
) ? ss
->name
: ss
->legacy_name
,
1369 strncpy(buf
, cft
->name
, CGROUP_FILE_NAME_MAX
);
1374 * cgroup_file_mode - deduce file mode of a control file
1375 * @cft: the control file in question
1377 * S_IRUGO for read, S_IWUSR for write.
1379 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
1383 if (cft
->read_u64
|| cft
->read_s64
|| cft
->seq_show
)
1386 if (cft
->write_u64
|| cft
->write_s64
|| cft
->write
) {
1387 if (cft
->flags
& CFTYPE_WORLD_WRITABLE
)
1397 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
1398 * @subtree_control: the new subtree_control mask to consider
1399 * @this_ss_mask: available subsystems
1401 * On the default hierarchy, a subsystem may request other subsystems to be
1402 * enabled together through its ->depends_on mask. In such cases, more
1403 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1405 * This function calculates which subsystems need to be enabled if
1406 * @subtree_control is to be applied while restricted to @this_ss_mask.
1408 static u16
cgroup_calc_subtree_ss_mask(u16 subtree_control
, u16 this_ss_mask
)
1410 u16 cur_ss_mask
= subtree_control
;
1411 struct cgroup_subsys
*ss
;
1414 lockdep_assert_held(&cgroup_mutex
);
1416 cur_ss_mask
|= cgrp_dfl_implicit_ss_mask
;
1419 u16 new_ss_mask
= cur_ss_mask
;
1421 do_each_subsys_mask(ss
, ssid
, cur_ss_mask
) {
1422 new_ss_mask
|= ss
->depends_on
;
1423 } while_each_subsys_mask();
1426 * Mask out subsystems which aren't available. This can
1427 * happen only if some depended-upon subsystems were bound
1428 * to non-default hierarchies.
1430 new_ss_mask
&= this_ss_mask
;
1432 if (new_ss_mask
== cur_ss_mask
)
1434 cur_ss_mask
= new_ss_mask
;
1441 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1442 * @kn: the kernfs_node being serviced
1444 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1445 * the method finishes if locking succeeded. Note that once this function
1446 * returns the cgroup returned by cgroup_kn_lock_live() may become
1447 * inaccessible any time. If the caller intends to continue to access the
1448 * cgroup, it should pin it before invoking this function.
1450 void cgroup_kn_unlock(struct kernfs_node
*kn
)
1452 struct cgroup
*cgrp
;
1454 if (kernfs_type(kn
) == KERNFS_DIR
)
1457 cgrp
= kn
->parent
->priv
;
1459 mutex_unlock(&cgroup_mutex
);
1461 kernfs_unbreak_active_protection(kn
);
1466 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1467 * @kn: the kernfs_node being serviced
1468 * @drain_offline: perform offline draining on the cgroup
1470 * This helper is to be used by a cgroup kernfs method currently servicing
1471 * @kn. It breaks the active protection, performs cgroup locking and
1472 * verifies that the associated cgroup is alive. Returns the cgroup if
1473 * alive; otherwise, %NULL. A successful return should be undone by a
1474 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1475 * cgroup is drained of offlining csses before return.
1477 * Any cgroup kernfs method implementation which requires locking the
1478 * associated cgroup should use this helper. It avoids nesting cgroup
1479 * locking under kernfs active protection and allows all kernfs operations
1480 * including self-removal.
1482 struct cgroup
*cgroup_kn_lock_live(struct kernfs_node
*kn
, bool drain_offline
)
1484 struct cgroup
*cgrp
;
1486 if (kernfs_type(kn
) == KERNFS_DIR
)
1489 cgrp
= kn
->parent
->priv
;
1492 * We're gonna grab cgroup_mutex which nests outside kernfs
1493 * active_ref. cgroup liveliness check alone provides enough
1494 * protection against removal. Ensure @cgrp stays accessible and
1495 * break the active_ref protection.
1497 if (!cgroup_tryget(cgrp
))
1499 kernfs_break_active_protection(kn
);
1502 cgroup_lock_and_drain_offline(cgrp
);
1504 mutex_lock(&cgroup_mutex
);
1506 if (!cgroup_is_dead(cgrp
))
1509 cgroup_kn_unlock(kn
);
1513 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
1515 char name
[CGROUP_FILE_NAME_MAX
];
1517 lockdep_assert_held(&cgroup_mutex
);
1519 if (cft
->file_offset
) {
1520 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, cft
->ss
);
1521 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
1523 spin_lock_irq(&cgroup_file_kn_lock
);
1525 spin_unlock_irq(&cgroup_file_kn_lock
);
1528 kernfs_remove_by_name(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
));
1532 * css_clear_dir - remove subsys files in a cgroup directory
1535 static void css_clear_dir(struct cgroup_subsys_state
*css
)
1537 struct cgroup
*cgrp
= css
->cgroup
;
1538 struct cftype
*cfts
;
1540 if (!(css
->flags
& CSS_VISIBLE
))
1543 css
->flags
&= ~CSS_VISIBLE
;
1545 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
)
1546 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1550 * css_populate_dir - create subsys files in a cgroup directory
1553 * On failure, no file is added.
1555 static int css_populate_dir(struct cgroup_subsys_state
*css
)
1557 struct cgroup
*cgrp
= css
->cgroup
;
1558 struct cftype
*cfts
, *failed_cfts
;
1561 if ((css
->flags
& CSS_VISIBLE
) || !cgrp
->kn
)
1565 if (cgroup_on_dfl(cgrp
))
1566 cfts
= cgroup_base_files
;
1568 cfts
= cgroup1_base_files
;
1570 return cgroup_addrm_files(&cgrp
->self
, cgrp
, cfts
, true);
1573 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1574 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, true);
1581 css
->flags
|= CSS_VISIBLE
;
1585 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1586 if (cfts
== failed_cfts
)
1588 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1593 int rebind_subsystems(struct cgroup_root
*dst_root
, u16 ss_mask
)
1595 struct cgroup
*dcgrp
= &dst_root
->cgrp
;
1596 struct cgroup_subsys
*ss
;
1599 lockdep_assert_held(&cgroup_mutex
);
1601 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
1603 * If @ss has non-root csses attached to it, can't move.
1604 * If @ss is an implicit controller, it is exempt from this
1605 * rule and can be stolen.
1607 if (css_next_child(NULL
, cgroup_css(&ss
->root
->cgrp
, ss
)) &&
1608 !ss
->implicit_on_dfl
)
1611 /* can't move between two non-dummy roots either */
1612 if (ss
->root
!= &cgrp_dfl_root
&& dst_root
!= &cgrp_dfl_root
)
1614 } while_each_subsys_mask();
1616 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
1617 struct cgroup_root
*src_root
= ss
->root
;
1618 struct cgroup
*scgrp
= &src_root
->cgrp
;
1619 struct cgroup_subsys_state
*css
= cgroup_css(scgrp
, ss
);
1620 struct css_set
*cset
;
1622 WARN_ON(!css
|| cgroup_css(dcgrp
, ss
));
1624 /* disable from the source */
1625 src_root
->subsys_mask
&= ~(1 << ssid
);
1626 WARN_ON(cgroup_apply_control(scgrp
));
1627 cgroup_finalize_control(scgrp
, 0);
1630 RCU_INIT_POINTER(scgrp
->subsys
[ssid
], NULL
);
1631 rcu_assign_pointer(dcgrp
->subsys
[ssid
], css
);
1632 ss
->root
= dst_root
;
1633 css
->cgroup
= dcgrp
;
1635 spin_lock_irq(&css_set_lock
);
1636 hash_for_each(css_set_table
, i
, cset
, hlist
)
1637 list_move_tail(&cset
->e_cset_node
[ss
->id
],
1638 &dcgrp
->e_csets
[ss
->id
]);
1639 spin_unlock_irq(&css_set_lock
);
1641 /* default hierarchy doesn't enable controllers by default */
1642 dst_root
->subsys_mask
|= 1 << ssid
;
1643 if (dst_root
== &cgrp_dfl_root
) {
1644 static_branch_enable(cgroup_subsys_on_dfl_key
[ssid
]);
1646 dcgrp
->subtree_control
|= 1 << ssid
;
1647 static_branch_disable(cgroup_subsys_on_dfl_key
[ssid
]);
1650 ret
= cgroup_apply_control(dcgrp
);
1652 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1657 } while_each_subsys_mask();
1659 kernfs_activate(dcgrp
->kn
);
1663 int cgroup_show_path(struct seq_file
*sf
, struct kernfs_node
*kf_node
,
1664 struct kernfs_root
*kf_root
)
1668 struct cgroup_root
*kf_cgroot
= cgroup_root_from_kf(kf_root
);
1669 struct cgroup
*ns_cgroup
;
1671 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1675 spin_lock_irq(&css_set_lock
);
1676 ns_cgroup
= current_cgns_cgroup_from_root(kf_cgroot
);
1677 len
= kernfs_path_from_node(kf_node
, ns_cgroup
->kn
, buf
, PATH_MAX
);
1678 spin_unlock_irq(&css_set_lock
);
1680 if (len
>= PATH_MAX
)
1683 seq_escape(sf
, buf
, " \t\n\\");
1690 static int parse_cgroup_root_flags(char *data
, unsigned int *root_flags
)
1696 if (!data
|| *data
== '\0')
1699 while ((token
= strsep(&data
, ",")) != NULL
) {
1700 if (!strcmp(token
, "nsdelegate")) {
1701 *root_flags
|= CGRP_ROOT_NS_DELEGATE
;
1705 pr_err("cgroup2: unknown option \"%s\"\n", token
);
1712 static void apply_cgroup_root_flags(unsigned int root_flags
)
1714 if (current
->nsproxy
->cgroup_ns
== &init_cgroup_ns
) {
1715 if (root_flags
& CGRP_ROOT_NS_DELEGATE
)
1716 cgrp_dfl_root
.flags
|= CGRP_ROOT_NS_DELEGATE
;
1718 cgrp_dfl_root
.flags
&= ~CGRP_ROOT_NS_DELEGATE
;
1722 static int cgroup_show_options(struct seq_file
*seq
, struct kernfs_root
*kf_root
)
1724 if (cgrp_dfl_root
.flags
& CGRP_ROOT_NS_DELEGATE
)
1725 seq_puts(seq
, ",nsdelegate");
1729 static int cgroup_remount(struct kernfs_root
*kf_root
, int *flags
, char *data
)
1731 unsigned int root_flags
;
1734 ret
= parse_cgroup_root_flags(data
, &root_flags
);
1738 apply_cgroup_root_flags(root_flags
);
1743 * To reduce the fork() overhead for systems that are not actually using
1744 * their cgroups capability, we don't maintain the lists running through
1745 * each css_set to its tasks until we see the list actually used - in other
1746 * words after the first mount.
1748 static bool use_task_css_set_links __read_mostly
;
1750 static void cgroup_enable_task_cg_lists(void)
1752 struct task_struct
*p
, *g
;
1754 spin_lock_irq(&css_set_lock
);
1756 if (use_task_css_set_links
)
1759 use_task_css_set_links
= true;
1762 * We need tasklist_lock because RCU is not safe against
1763 * while_each_thread(). Besides, a forking task that has passed
1764 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1765 * is not guaranteed to have its child immediately visible in the
1766 * tasklist if we walk through it with RCU.
1768 read_lock(&tasklist_lock
);
1769 do_each_thread(g
, p
) {
1770 WARN_ON_ONCE(!list_empty(&p
->cg_list
) ||
1771 task_css_set(p
) != &init_css_set
);
1774 * We should check if the process is exiting, otherwise
1775 * it will race with cgroup_exit() in that the list
1776 * entry won't be deleted though the process has exited.
1777 * Do it while holding siglock so that we don't end up
1778 * racing against cgroup_exit().
1780 * Interrupts were already disabled while acquiring
1781 * the css_set_lock, so we do not need to disable it
1782 * again when acquiring the sighand->siglock here.
1784 spin_lock(&p
->sighand
->siglock
);
1785 if (!(p
->flags
& PF_EXITING
)) {
1786 struct css_set
*cset
= task_css_set(p
);
1788 if (!css_set_populated(cset
))
1789 css_set_update_populated(cset
, true);
1790 list_add_tail(&p
->cg_list
, &cset
->tasks
);
1794 spin_unlock(&p
->sighand
->siglock
);
1795 } while_each_thread(g
, p
);
1796 read_unlock(&tasklist_lock
);
1798 spin_unlock_irq(&css_set_lock
);
1801 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1803 struct cgroup_subsys
*ss
;
1806 INIT_LIST_HEAD(&cgrp
->self
.sibling
);
1807 INIT_LIST_HEAD(&cgrp
->self
.children
);
1808 INIT_LIST_HEAD(&cgrp
->cset_links
);
1809 INIT_LIST_HEAD(&cgrp
->pidlists
);
1810 mutex_init(&cgrp
->pidlist_mutex
);
1811 cgrp
->self
.cgroup
= cgrp
;
1812 cgrp
->self
.flags
|= CSS_ONLINE
;
1813 cgrp
->dom_cgrp
= cgrp
;
1814 cgrp
->max_descendants
= INT_MAX
;
1815 cgrp
->max_depth
= INT_MAX
;
1817 for_each_subsys(ss
, ssid
)
1818 INIT_LIST_HEAD(&cgrp
->e_csets
[ssid
]);
1820 init_waitqueue_head(&cgrp
->offline_waitq
);
1821 INIT_WORK(&cgrp
->release_agent_work
, cgroup1_release_agent
);
1824 void init_cgroup_root(struct cgroup_root
*root
, struct cgroup_sb_opts
*opts
)
1826 struct cgroup
*cgrp
= &root
->cgrp
;
1828 INIT_LIST_HEAD(&root
->root_list
);
1829 atomic_set(&root
->nr_cgrps
, 1);
1831 init_cgroup_housekeeping(cgrp
);
1832 idr_init(&root
->cgroup_idr
);
1834 root
->flags
= opts
->flags
;
1835 if (opts
->release_agent
)
1836 strcpy(root
->release_agent_path
, opts
->release_agent
);
1838 strcpy(root
->name
, opts
->name
);
1839 if (opts
->cpuset_clone_children
)
1840 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->cgrp
.flags
);
1843 int cgroup_setup_root(struct cgroup_root
*root
, u16 ss_mask
, int ref_flags
)
1845 LIST_HEAD(tmp_links
);
1846 struct cgroup
*root_cgrp
= &root
->cgrp
;
1847 struct kernfs_syscall_ops
*kf_sops
;
1848 struct css_set
*cset
;
1851 lockdep_assert_held(&cgroup_mutex
);
1853 ret
= cgroup_idr_alloc(&root
->cgroup_idr
, root_cgrp
, 1, 2, GFP_KERNEL
);
1856 root_cgrp
->id
= ret
;
1857 root_cgrp
->ancestor_ids
[0] = ret
;
1859 ret
= percpu_ref_init(&root_cgrp
->self
.refcnt
, css_release
,
1860 ref_flags
, GFP_KERNEL
);
1865 * We're accessing css_set_count without locking css_set_lock here,
1866 * but that's OK - it can only be increased by someone holding
1867 * cgroup_lock, and that's us. Later rebinding may disable
1868 * controllers on the default hierarchy and thus create new csets,
1869 * which can't be more than the existing ones. Allocate 2x.
1871 ret
= allocate_cgrp_cset_links(2 * css_set_count
, &tmp_links
);
1875 ret
= cgroup_init_root_id(root
);
1879 kf_sops
= root
== &cgrp_dfl_root
?
1880 &cgroup_kf_syscall_ops
: &cgroup1_kf_syscall_ops
;
1882 root
->kf_root
= kernfs_create_root(kf_sops
,
1883 KERNFS_ROOT_CREATE_DEACTIVATED
|
1884 KERNFS_ROOT_SUPPORT_EXPORTOP
,
1886 if (IS_ERR(root
->kf_root
)) {
1887 ret
= PTR_ERR(root
->kf_root
);
1890 root_cgrp
->kn
= root
->kf_root
->kn
;
1892 ret
= css_populate_dir(&root_cgrp
->self
);
1896 ret
= rebind_subsystems(root
, ss_mask
);
1900 trace_cgroup_setup_root(root
);
1903 * There must be no failure case after here, since rebinding takes
1904 * care of subsystems' refcounts, which are explicitly dropped in
1905 * the failure exit path.
1907 list_add(&root
->root_list
, &cgroup_roots
);
1908 cgroup_root_count
++;
1911 * Link the root cgroup in this hierarchy into all the css_set
1914 spin_lock_irq(&css_set_lock
);
1915 hash_for_each(css_set_table
, i
, cset
, hlist
) {
1916 link_css_set(&tmp_links
, cset
, root_cgrp
);
1917 if (css_set_populated(cset
))
1918 cgroup_update_populated(root_cgrp
, true);
1920 spin_unlock_irq(&css_set_lock
);
1922 BUG_ON(!list_empty(&root_cgrp
->self
.children
));
1923 BUG_ON(atomic_read(&root
->nr_cgrps
) != 1);
1925 kernfs_activate(root_cgrp
->kn
);
1930 kernfs_destroy_root(root
->kf_root
);
1931 root
->kf_root
= NULL
;
1933 cgroup_exit_root_id(root
);
1935 percpu_ref_exit(&root_cgrp
->self
.refcnt
);
1937 free_cgrp_cset_links(&tmp_links
);
1941 struct dentry
*cgroup_do_mount(struct file_system_type
*fs_type
, int flags
,
1942 struct cgroup_root
*root
, unsigned long magic
,
1943 struct cgroup_namespace
*ns
)
1945 struct dentry
*dentry
;
1946 bool new_sb
= false;
1948 dentry
= kernfs_mount(fs_type
, flags
, root
->kf_root
, magic
, &new_sb
);
1951 * In non-init cgroup namespace, instead of root cgroup's dentry,
1952 * we return the dentry corresponding to the cgroupns->root_cgrp.
1954 if (!IS_ERR(dentry
) && ns
!= &init_cgroup_ns
) {
1955 struct dentry
*nsdentry
;
1956 struct super_block
*sb
= dentry
->d_sb
;
1957 struct cgroup
*cgrp
;
1959 mutex_lock(&cgroup_mutex
);
1960 spin_lock_irq(&css_set_lock
);
1962 cgrp
= cset_cgroup_from_root(ns
->root_cset
, root
);
1964 spin_unlock_irq(&css_set_lock
);
1965 mutex_unlock(&cgroup_mutex
);
1967 nsdentry
= kernfs_node_dentry(cgrp
->kn
, sb
);
1969 if (IS_ERR(nsdentry
))
1970 deactivate_locked_super(sb
);
1975 cgroup_put(&root
->cgrp
);
1980 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1981 int flags
, const char *unused_dev_name
,
1984 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
1985 struct dentry
*dentry
;
1990 /* Check if the caller has permission to mount. */
1991 if (!ns_capable(ns
->user_ns
, CAP_SYS_ADMIN
)) {
1993 return ERR_PTR(-EPERM
);
1997 * The first time anyone tries to mount a cgroup, enable the list
1998 * linking each css_set to its tasks and fix up all existing tasks.
2000 if (!use_task_css_set_links
)
2001 cgroup_enable_task_cg_lists();
2003 if (fs_type
== &cgroup2_fs_type
) {
2004 unsigned int root_flags
;
2006 ret
= parse_cgroup_root_flags(data
, &root_flags
);
2009 return ERR_PTR(ret
);
2012 cgrp_dfl_visible
= true;
2013 cgroup_get_live(&cgrp_dfl_root
.cgrp
);
2015 dentry
= cgroup_do_mount(&cgroup2_fs_type
, flags
, &cgrp_dfl_root
,
2016 CGROUP2_SUPER_MAGIC
, ns
);
2017 if (!IS_ERR(dentry
))
2018 apply_cgroup_root_flags(root_flags
);
2020 dentry
= cgroup1_mount(&cgroup_fs_type
, flags
, data
,
2021 CGROUP_SUPER_MAGIC
, ns
);
2028 static void cgroup_kill_sb(struct super_block
*sb
)
2030 struct kernfs_root
*kf_root
= kernfs_root_from_sb(sb
);
2031 struct cgroup_root
*root
= cgroup_root_from_kf(kf_root
);
2034 * If @root doesn't have any mounts or children, start killing it.
2035 * This prevents new mounts by disabling percpu_ref_tryget_live().
2036 * cgroup_mount() may wait for @root's release.
2038 * And don't kill the default root.
2040 if (!list_empty(&root
->cgrp
.self
.children
) ||
2041 root
== &cgrp_dfl_root
)
2042 cgroup_put(&root
->cgrp
);
2044 percpu_ref_kill(&root
->cgrp
.self
.refcnt
);
2049 struct file_system_type cgroup_fs_type
= {
2051 .mount
= cgroup_mount
,
2052 .kill_sb
= cgroup_kill_sb
,
2053 .fs_flags
= FS_USERNS_MOUNT
,
2056 static struct file_system_type cgroup2_fs_type
= {
2058 .mount
= cgroup_mount
,
2059 .kill_sb
= cgroup_kill_sb
,
2060 .fs_flags
= FS_USERNS_MOUNT
,
2063 int cgroup_path_ns_locked(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
2064 struct cgroup_namespace
*ns
)
2066 struct cgroup
*root
= cset_cgroup_from_root(ns
->root_cset
, cgrp
->root
);
2068 return kernfs_path_from_node(cgrp
->kn
, root
->kn
, buf
, buflen
);
2071 int cgroup_path_ns(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
2072 struct cgroup_namespace
*ns
)
2076 mutex_lock(&cgroup_mutex
);
2077 spin_lock_irq(&css_set_lock
);
2079 ret
= cgroup_path_ns_locked(cgrp
, buf
, buflen
, ns
);
2081 spin_unlock_irq(&css_set_lock
);
2082 mutex_unlock(&cgroup_mutex
);
2086 EXPORT_SYMBOL_GPL(cgroup_path_ns
);
2089 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
2090 * @task: target task
2091 * @buf: the buffer to write the path into
2092 * @buflen: the length of the buffer
2094 * Determine @task's cgroup on the first (the one with the lowest non-zero
2095 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2096 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2097 * cgroup controller callbacks.
2099 * Return value is the same as kernfs_path().
2101 int task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
2103 struct cgroup_root
*root
;
2104 struct cgroup
*cgrp
;
2105 int hierarchy_id
= 1;
2108 mutex_lock(&cgroup_mutex
);
2109 spin_lock_irq(&css_set_lock
);
2111 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
2114 cgrp
= task_cgroup_from_root(task
, root
);
2115 ret
= cgroup_path_ns_locked(cgrp
, buf
, buflen
, &init_cgroup_ns
);
2117 /* if no hierarchy exists, everyone is in "/" */
2118 ret
= strlcpy(buf
, "/", buflen
);
2121 spin_unlock_irq(&css_set_lock
);
2122 mutex_unlock(&cgroup_mutex
);
2125 EXPORT_SYMBOL_GPL(task_cgroup_path
);
2128 * cgroup_migrate_add_task - add a migration target task to a migration context
2129 * @task: target task
2130 * @mgctx: target migration context
2132 * Add @task, which is a migration target, to @mgctx->tset. This function
2133 * becomes noop if @task doesn't need to be migrated. @task's css_set
2134 * should have been added as a migration source and @task->cg_list will be
2135 * moved from the css_set's tasks list to mg_tasks one.
2137 static void cgroup_migrate_add_task(struct task_struct
*task
,
2138 struct cgroup_mgctx
*mgctx
)
2140 struct css_set
*cset
;
2142 lockdep_assert_held(&css_set_lock
);
2144 /* @task either already exited or can't exit until the end */
2145 if (task
->flags
& PF_EXITING
)
2148 /* leave @task alone if post_fork() hasn't linked it yet */
2149 if (list_empty(&task
->cg_list
))
2152 cset
= task_css_set(task
);
2153 if (!cset
->mg_src_cgrp
)
2156 mgctx
->tset
.nr_tasks
++;
2158 list_move_tail(&task
->cg_list
, &cset
->mg_tasks
);
2159 if (list_empty(&cset
->mg_node
))
2160 list_add_tail(&cset
->mg_node
,
2161 &mgctx
->tset
.src_csets
);
2162 if (list_empty(&cset
->mg_dst_cset
->mg_node
))
2163 list_add_tail(&cset
->mg_dst_cset
->mg_node
,
2164 &mgctx
->tset
.dst_csets
);
2168 * cgroup_taskset_first - reset taskset and return the first task
2169 * @tset: taskset of interest
2170 * @dst_cssp: output variable for the destination css
2172 * @tset iteration is initialized and the first task is returned.
2174 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
,
2175 struct cgroup_subsys_state
**dst_cssp
)
2177 tset
->cur_cset
= list_first_entry(tset
->csets
, struct css_set
, mg_node
);
2178 tset
->cur_task
= NULL
;
2180 return cgroup_taskset_next(tset
, dst_cssp
);
2184 * cgroup_taskset_next - iterate to the next task in taskset
2185 * @tset: taskset of interest
2186 * @dst_cssp: output variable for the destination css
2188 * Return the next task in @tset. Iteration must have been initialized
2189 * with cgroup_taskset_first().
2191 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
,
2192 struct cgroup_subsys_state
**dst_cssp
)
2194 struct css_set
*cset
= tset
->cur_cset
;
2195 struct task_struct
*task
= tset
->cur_task
;
2197 while (&cset
->mg_node
!= tset
->csets
) {
2199 task
= list_first_entry(&cset
->mg_tasks
,
2200 struct task_struct
, cg_list
);
2202 task
= list_next_entry(task
, cg_list
);
2204 if (&task
->cg_list
!= &cset
->mg_tasks
) {
2205 tset
->cur_cset
= cset
;
2206 tset
->cur_task
= task
;
2209 * This function may be called both before and
2210 * after cgroup_taskset_migrate(). The two cases
2211 * can be distinguished by looking at whether @cset
2212 * has its ->mg_dst_cset set.
2214 if (cset
->mg_dst_cset
)
2215 *dst_cssp
= cset
->mg_dst_cset
->subsys
[tset
->ssid
];
2217 *dst_cssp
= cset
->subsys
[tset
->ssid
];
2222 cset
= list_next_entry(cset
, mg_node
);
2230 * cgroup_taskset_migrate - migrate a taskset
2231 * @mgctx: migration context
2233 * Migrate tasks in @mgctx as setup by migration preparation functions.
2234 * This function fails iff one of the ->can_attach callbacks fails and
2235 * guarantees that either all or none of the tasks in @mgctx are migrated.
2236 * @mgctx is consumed regardless of success.
2238 static int cgroup_migrate_execute(struct cgroup_mgctx
*mgctx
)
2240 struct cgroup_taskset
*tset
= &mgctx
->tset
;
2241 struct cgroup_subsys
*ss
;
2242 struct task_struct
*task
, *tmp_task
;
2243 struct css_set
*cset
, *tmp_cset
;
2244 int ssid
, failed_ssid
, ret
;
2246 /* check that we can legitimately attach to the cgroup */
2247 if (tset
->nr_tasks
) {
2248 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2249 if (ss
->can_attach
) {
2251 ret
= ss
->can_attach(tset
);
2254 goto out_cancel_attach
;
2257 } while_each_subsys_mask();
2261 * Now that we're guaranteed success, proceed to move all tasks to
2262 * the new cgroup. There are no failure cases after here, so this
2263 * is the commit point.
2265 spin_lock_irq(&css_set_lock
);
2266 list_for_each_entry(cset
, &tset
->src_csets
, mg_node
) {
2267 list_for_each_entry_safe(task
, tmp_task
, &cset
->mg_tasks
, cg_list
) {
2268 struct css_set
*from_cset
= task_css_set(task
);
2269 struct css_set
*to_cset
= cset
->mg_dst_cset
;
2271 get_css_set(to_cset
);
2272 to_cset
->nr_tasks
++;
2273 css_set_move_task(task
, from_cset
, to_cset
, true);
2274 put_css_set_locked(from_cset
);
2275 from_cset
->nr_tasks
--;
2278 spin_unlock_irq(&css_set_lock
);
2281 * Migration is committed, all target tasks are now on dst_csets.
2282 * Nothing is sensitive to fork() after this point. Notify
2283 * controllers that migration is complete.
2285 tset
->csets
= &tset
->dst_csets
;
2287 if (tset
->nr_tasks
) {
2288 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2293 } while_each_subsys_mask();
2297 goto out_release_tset
;
2300 if (tset
->nr_tasks
) {
2301 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2302 if (ssid
== failed_ssid
)
2304 if (ss
->cancel_attach
) {
2306 ss
->cancel_attach(tset
);
2308 } while_each_subsys_mask();
2311 spin_lock_irq(&css_set_lock
);
2312 list_splice_init(&tset
->dst_csets
, &tset
->src_csets
);
2313 list_for_each_entry_safe(cset
, tmp_cset
, &tset
->src_csets
, mg_node
) {
2314 list_splice_tail_init(&cset
->mg_tasks
, &cset
->tasks
);
2315 list_del_init(&cset
->mg_node
);
2317 spin_unlock_irq(&css_set_lock
);
2320 * Re-initialize the cgroup_taskset structure in case it is reused
2321 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
2325 tset
->csets
= &tset
->src_csets
;
2330 * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
2331 * @dst_cgrp: destination cgroup to test
2333 * On the default hierarchy, except for the mixable, (possible) thread root
2334 * and threaded cgroups, subtree_control must be zero for migration
2335 * destination cgroups with tasks so that child cgroups don't compete
2338 int cgroup_migrate_vet_dst(struct cgroup
*dst_cgrp
)
2340 /* v1 doesn't have any restriction */
2341 if (!cgroup_on_dfl(dst_cgrp
))
2344 /* verify @dst_cgrp can host resources */
2345 if (!cgroup_is_valid_domain(dst_cgrp
->dom_cgrp
))
2348 /* mixables don't care */
2349 if (cgroup_is_mixable(dst_cgrp
))
2353 * If @dst_cgrp is already or can become a thread root or is
2354 * threaded, it doesn't matter.
2356 if (cgroup_can_be_thread_root(dst_cgrp
) || cgroup_is_threaded(dst_cgrp
))
2359 /* apply no-internal-process constraint */
2360 if (dst_cgrp
->subtree_control
)
2367 * cgroup_migrate_finish - cleanup after attach
2368 * @mgctx: migration context
2370 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2371 * those functions for details.
2373 void cgroup_migrate_finish(struct cgroup_mgctx
*mgctx
)
2375 LIST_HEAD(preloaded
);
2376 struct css_set
*cset
, *tmp_cset
;
2378 lockdep_assert_held(&cgroup_mutex
);
2380 spin_lock_irq(&css_set_lock
);
2382 list_splice_tail_init(&mgctx
->preloaded_src_csets
, &preloaded
);
2383 list_splice_tail_init(&mgctx
->preloaded_dst_csets
, &preloaded
);
2385 list_for_each_entry_safe(cset
, tmp_cset
, &preloaded
, mg_preload_node
) {
2386 cset
->mg_src_cgrp
= NULL
;
2387 cset
->mg_dst_cgrp
= NULL
;
2388 cset
->mg_dst_cset
= NULL
;
2389 list_del_init(&cset
->mg_preload_node
);
2390 put_css_set_locked(cset
);
2393 spin_unlock_irq(&css_set_lock
);
2397 * cgroup_migrate_add_src - add a migration source css_set
2398 * @src_cset: the source css_set to add
2399 * @dst_cgrp: the destination cgroup
2400 * @mgctx: migration context
2402 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2403 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
2404 * up by cgroup_migrate_finish().
2406 * This function may be called without holding cgroup_threadgroup_rwsem
2407 * even if the target is a process. Threads may be created and destroyed
2408 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2409 * into play and the preloaded css_sets are guaranteed to cover all
2412 void cgroup_migrate_add_src(struct css_set
*src_cset
,
2413 struct cgroup
*dst_cgrp
,
2414 struct cgroup_mgctx
*mgctx
)
2416 struct cgroup
*src_cgrp
;
2418 lockdep_assert_held(&cgroup_mutex
);
2419 lockdep_assert_held(&css_set_lock
);
2422 * If ->dead, @src_set is associated with one or more dead cgroups
2423 * and doesn't contain any migratable tasks. Ignore it early so
2424 * that the rest of migration path doesn't get confused by it.
2429 src_cgrp
= cset_cgroup_from_root(src_cset
, dst_cgrp
->root
);
2431 if (!list_empty(&src_cset
->mg_preload_node
))
2434 WARN_ON(src_cset
->mg_src_cgrp
);
2435 WARN_ON(src_cset
->mg_dst_cgrp
);
2436 WARN_ON(!list_empty(&src_cset
->mg_tasks
));
2437 WARN_ON(!list_empty(&src_cset
->mg_node
));
2439 src_cset
->mg_src_cgrp
= src_cgrp
;
2440 src_cset
->mg_dst_cgrp
= dst_cgrp
;
2441 get_css_set(src_cset
);
2442 list_add_tail(&src_cset
->mg_preload_node
, &mgctx
->preloaded_src_csets
);
2446 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
2447 * @mgctx: migration context
2449 * Tasks are about to be moved and all the source css_sets have been
2450 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2451 * pins all destination css_sets, links each to its source, and append them
2452 * to @mgctx->preloaded_dst_csets.
2454 * This function must be called after cgroup_migrate_add_src() has been
2455 * called on each migration source css_set. After migration is performed
2456 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2459 int cgroup_migrate_prepare_dst(struct cgroup_mgctx
*mgctx
)
2461 struct css_set
*src_cset
, *tmp_cset
;
2463 lockdep_assert_held(&cgroup_mutex
);
2465 /* look up the dst cset for each src cset and link it to src */
2466 list_for_each_entry_safe(src_cset
, tmp_cset
, &mgctx
->preloaded_src_csets
,
2468 struct css_set
*dst_cset
;
2469 struct cgroup_subsys
*ss
;
2472 dst_cset
= find_css_set(src_cset
, src_cset
->mg_dst_cgrp
);
2476 WARN_ON_ONCE(src_cset
->mg_dst_cset
|| dst_cset
->mg_dst_cset
);
2479 * If src cset equals dst, it's noop. Drop the src.
2480 * cgroup_migrate() will skip the cset too. Note that we
2481 * can't handle src == dst as some nodes are used by both.
2483 if (src_cset
== dst_cset
) {
2484 src_cset
->mg_src_cgrp
= NULL
;
2485 src_cset
->mg_dst_cgrp
= NULL
;
2486 list_del_init(&src_cset
->mg_preload_node
);
2487 put_css_set(src_cset
);
2488 put_css_set(dst_cset
);
2492 src_cset
->mg_dst_cset
= dst_cset
;
2494 if (list_empty(&dst_cset
->mg_preload_node
))
2495 list_add_tail(&dst_cset
->mg_preload_node
,
2496 &mgctx
->preloaded_dst_csets
);
2498 put_css_set(dst_cset
);
2500 for_each_subsys(ss
, ssid
)
2501 if (src_cset
->subsys
[ssid
] != dst_cset
->subsys
[ssid
])
2502 mgctx
->ss_mask
|= 1 << ssid
;
2507 cgroup_migrate_finish(mgctx
);
2512 * cgroup_migrate - migrate a process or task to a cgroup
2513 * @leader: the leader of the process or the task to migrate
2514 * @threadgroup: whether @leader points to the whole process or a single task
2515 * @mgctx: migration context
2517 * Migrate a process or task denoted by @leader. If migrating a process,
2518 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2519 * responsible for invoking cgroup_migrate_add_src() and
2520 * cgroup_migrate_prepare_dst() on the targets before invoking this
2521 * function and following up with cgroup_migrate_finish().
2523 * As long as a controller's ->can_attach() doesn't fail, this function is
2524 * guaranteed to succeed. This means that, excluding ->can_attach()
2525 * failure, when migrating multiple targets, the success or failure can be
2526 * decided for all targets by invoking group_migrate_prepare_dst() before
2527 * actually starting migrating.
2529 int cgroup_migrate(struct task_struct
*leader
, bool threadgroup
,
2530 struct cgroup_mgctx
*mgctx
)
2532 struct task_struct
*task
;
2535 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2536 * already PF_EXITING could be freed from underneath us unless we
2537 * take an rcu_read_lock.
2539 spin_lock_irq(&css_set_lock
);
2543 cgroup_migrate_add_task(task
, mgctx
);
2546 } while_each_thread(leader
, task
);
2548 spin_unlock_irq(&css_set_lock
);
2550 return cgroup_migrate_execute(mgctx
);
2554 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2555 * @dst_cgrp: the cgroup to attach to
2556 * @leader: the task or the leader of the threadgroup to be attached
2557 * @threadgroup: attach the whole threadgroup?
2559 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
2561 int cgroup_attach_task(struct cgroup
*dst_cgrp
, struct task_struct
*leader
,
2564 DEFINE_CGROUP_MGCTX(mgctx
);
2565 struct task_struct
*task
;
2568 ret
= cgroup_migrate_vet_dst(dst_cgrp
);
2572 /* look up all src csets */
2573 spin_lock_irq(&css_set_lock
);
2577 cgroup_migrate_add_src(task_css_set(task
), dst_cgrp
, &mgctx
);
2580 } while_each_thread(leader
, task
);
2582 spin_unlock_irq(&css_set_lock
);
2584 /* prepare dst csets and commit */
2585 ret
= cgroup_migrate_prepare_dst(&mgctx
);
2587 ret
= cgroup_migrate(leader
, threadgroup
, &mgctx
);
2589 cgroup_migrate_finish(&mgctx
);
2592 trace_cgroup_attach_task(dst_cgrp
, leader
, threadgroup
);
2597 struct task_struct
*cgroup_procs_write_start(char *buf
, bool threadgroup
)
2598 __acquires(&cgroup_threadgroup_rwsem
)
2600 struct task_struct
*tsk
;
2603 if (kstrtoint(strstrip(buf
), 0, &pid
) || pid
< 0)
2604 return ERR_PTR(-EINVAL
);
2606 percpu_down_write(&cgroup_threadgroup_rwsem
);
2610 tsk
= find_task_by_vpid(pid
);
2612 tsk
= ERR_PTR(-ESRCH
);
2613 goto out_unlock_threadgroup
;
2620 tsk
= tsk
->group_leader
;
2623 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2624 * If userland migrates such a kthread to a non-root cgroup, it can
2625 * become trapped in a cpuset, or RT kthread may be born in a
2626 * cgroup with no rt_runtime allocated. Just say no.
2628 if (tsk
->no_cgroup_migration
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2629 tsk
= ERR_PTR(-EINVAL
);
2630 goto out_unlock_threadgroup
;
2633 get_task_struct(tsk
);
2634 goto out_unlock_rcu
;
2636 out_unlock_threadgroup
:
2637 percpu_up_write(&cgroup_threadgroup_rwsem
);
2643 void cgroup_procs_write_finish(struct task_struct
*task
)
2644 __releases(&cgroup_threadgroup_rwsem
)
2646 struct cgroup_subsys
*ss
;
2649 /* release reference from cgroup_procs_write_start() */
2650 put_task_struct(task
);
2652 percpu_up_write(&cgroup_threadgroup_rwsem
);
2653 for_each_subsys(ss
, ssid
)
2654 if (ss
->post_attach
)
2658 static void cgroup_print_ss_mask(struct seq_file
*seq
, u16 ss_mask
)
2660 struct cgroup_subsys
*ss
;
2661 bool printed
= false;
2664 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
2667 seq_printf(seq
, "%s", ss
->name
);
2669 } while_each_subsys_mask();
2671 seq_putc(seq
, '\n');
2674 /* show controllers which are enabled from the parent */
2675 static int cgroup_controllers_show(struct seq_file
*seq
, void *v
)
2677 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2679 cgroup_print_ss_mask(seq
, cgroup_control(cgrp
));
2683 /* show controllers which are enabled for a given cgroup's children */
2684 static int cgroup_subtree_control_show(struct seq_file
*seq
, void *v
)
2686 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2688 cgroup_print_ss_mask(seq
, cgrp
->subtree_control
);
2693 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2694 * @cgrp: root of the subtree to update csses for
2696 * @cgrp's control masks have changed and its subtree's css associations
2697 * need to be updated accordingly. This function looks up all css_sets
2698 * which are attached to the subtree, creates the matching updated css_sets
2699 * and migrates the tasks to the new ones.
2701 static int cgroup_update_dfl_csses(struct cgroup
*cgrp
)
2703 DEFINE_CGROUP_MGCTX(mgctx
);
2704 struct cgroup_subsys_state
*d_css
;
2705 struct cgroup
*dsct
;
2706 struct css_set
*src_cset
;
2709 lockdep_assert_held(&cgroup_mutex
);
2711 percpu_down_write(&cgroup_threadgroup_rwsem
);
2713 /* look up all csses currently attached to @cgrp's subtree */
2714 spin_lock_irq(&css_set_lock
);
2715 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2716 struct cgrp_cset_link
*link
;
2718 list_for_each_entry(link
, &dsct
->cset_links
, cset_link
)
2719 cgroup_migrate_add_src(link
->cset
, dsct
, &mgctx
);
2721 spin_unlock_irq(&css_set_lock
);
2723 /* NULL dst indicates self on default hierarchy */
2724 ret
= cgroup_migrate_prepare_dst(&mgctx
);
2728 spin_lock_irq(&css_set_lock
);
2729 list_for_each_entry(src_cset
, &mgctx
.preloaded_src_csets
, mg_preload_node
) {
2730 struct task_struct
*task
, *ntask
;
2732 /* all tasks in src_csets need to be migrated */
2733 list_for_each_entry_safe(task
, ntask
, &src_cset
->tasks
, cg_list
)
2734 cgroup_migrate_add_task(task
, &mgctx
);
2736 spin_unlock_irq(&css_set_lock
);
2738 ret
= cgroup_migrate_execute(&mgctx
);
2740 cgroup_migrate_finish(&mgctx
);
2741 percpu_up_write(&cgroup_threadgroup_rwsem
);
2746 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
2747 * @cgrp: root of the target subtree
2749 * Because css offlining is asynchronous, userland may try to re-enable a
2750 * controller while the previous css is still around. This function grabs
2751 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
2753 void cgroup_lock_and_drain_offline(struct cgroup
*cgrp
)
2754 __acquires(&cgroup_mutex
)
2756 struct cgroup
*dsct
;
2757 struct cgroup_subsys_state
*d_css
;
2758 struct cgroup_subsys
*ss
;
2762 mutex_lock(&cgroup_mutex
);
2764 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2765 for_each_subsys(ss
, ssid
) {
2766 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2769 if (!css
|| !percpu_ref_is_dying(&css
->refcnt
))
2772 cgroup_get_live(dsct
);
2773 prepare_to_wait(&dsct
->offline_waitq
, &wait
,
2774 TASK_UNINTERRUPTIBLE
);
2776 mutex_unlock(&cgroup_mutex
);
2778 finish_wait(&dsct
->offline_waitq
, &wait
);
2787 * cgroup_save_control - save control masks and dom_cgrp of a subtree
2788 * @cgrp: root of the target subtree
2790 * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the
2791 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
2794 static void cgroup_save_control(struct cgroup
*cgrp
)
2796 struct cgroup
*dsct
;
2797 struct cgroup_subsys_state
*d_css
;
2799 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2800 dsct
->old_subtree_control
= dsct
->subtree_control
;
2801 dsct
->old_subtree_ss_mask
= dsct
->subtree_ss_mask
;
2802 dsct
->old_dom_cgrp
= dsct
->dom_cgrp
;
2807 * cgroup_propagate_control - refresh control masks of a subtree
2808 * @cgrp: root of the target subtree
2810 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2811 * ->subtree_control and propagate controller availability through the
2812 * subtree so that descendants don't have unavailable controllers enabled.
2814 static void cgroup_propagate_control(struct cgroup
*cgrp
)
2816 struct cgroup
*dsct
;
2817 struct cgroup_subsys_state
*d_css
;
2819 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2820 dsct
->subtree_control
&= cgroup_control(dsct
);
2821 dsct
->subtree_ss_mask
=
2822 cgroup_calc_subtree_ss_mask(dsct
->subtree_control
,
2823 cgroup_ss_mask(dsct
));
2828 * cgroup_restore_control - restore control masks and dom_cgrp of a subtree
2829 * @cgrp: root of the target subtree
2831 * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the
2832 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
2835 static void cgroup_restore_control(struct cgroup
*cgrp
)
2837 struct cgroup
*dsct
;
2838 struct cgroup_subsys_state
*d_css
;
2840 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2841 dsct
->subtree_control
= dsct
->old_subtree_control
;
2842 dsct
->subtree_ss_mask
= dsct
->old_subtree_ss_mask
;
2843 dsct
->dom_cgrp
= dsct
->old_dom_cgrp
;
2847 static bool css_visible(struct cgroup_subsys_state
*css
)
2849 struct cgroup_subsys
*ss
= css
->ss
;
2850 struct cgroup
*cgrp
= css
->cgroup
;
2852 if (cgroup_control(cgrp
) & (1 << ss
->id
))
2854 if (!(cgroup_ss_mask(cgrp
) & (1 << ss
->id
)))
2856 return cgroup_on_dfl(cgrp
) && ss
->implicit_on_dfl
;
2860 * cgroup_apply_control_enable - enable or show csses according to control
2861 * @cgrp: root of the target subtree
2863 * Walk @cgrp's subtree and create new csses or make the existing ones
2864 * visible. A css is created invisible if it's being implicitly enabled
2865 * through dependency. An invisible css is made visible when the userland
2866 * explicitly enables it.
2868 * Returns 0 on success, -errno on failure. On failure, csses which have
2869 * been processed already aren't cleaned up. The caller is responsible for
2870 * cleaning up with cgroup_apply_control_disable().
2872 static int cgroup_apply_control_enable(struct cgroup
*cgrp
)
2874 struct cgroup
*dsct
;
2875 struct cgroup_subsys_state
*d_css
;
2876 struct cgroup_subsys
*ss
;
2879 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2880 for_each_subsys(ss
, ssid
) {
2881 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2883 WARN_ON_ONCE(css
&& percpu_ref_is_dying(&css
->refcnt
));
2885 if (!(cgroup_ss_mask(dsct
) & (1 << ss
->id
)))
2889 css
= css_create(dsct
, ss
);
2891 return PTR_ERR(css
);
2894 if (css_visible(css
)) {
2895 ret
= css_populate_dir(css
);
2906 * cgroup_apply_control_disable - kill or hide csses according to control
2907 * @cgrp: root of the target subtree
2909 * Walk @cgrp's subtree and kill and hide csses so that they match
2910 * cgroup_ss_mask() and cgroup_visible_mask().
2912 * A css is hidden when the userland requests it to be disabled while other
2913 * subsystems are still depending on it. The css must not actively control
2914 * resources and be in the vanilla state if it's made visible again later.
2915 * Controllers which may be depended upon should provide ->css_reset() for
2918 static void cgroup_apply_control_disable(struct cgroup
*cgrp
)
2920 struct cgroup
*dsct
;
2921 struct cgroup_subsys_state
*d_css
;
2922 struct cgroup_subsys
*ss
;
2925 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2926 for_each_subsys(ss
, ssid
) {
2927 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2929 WARN_ON_ONCE(css
&& percpu_ref_is_dying(&css
->refcnt
));
2935 !(cgroup_ss_mask(dsct
) & (1 << ss
->id
))) {
2937 } else if (!css_visible(css
)) {
2947 * cgroup_apply_control - apply control mask updates to the subtree
2948 * @cgrp: root of the target subtree
2950 * subsystems can be enabled and disabled in a subtree using the following
2953 * 1. Call cgroup_save_control() to stash the current state.
2954 * 2. Update ->subtree_control masks in the subtree as desired.
2955 * 3. Call cgroup_apply_control() to apply the changes.
2956 * 4. Optionally perform other related operations.
2957 * 5. Call cgroup_finalize_control() to finish up.
2959 * This function implements step 3 and propagates the mask changes
2960 * throughout @cgrp's subtree, updates csses accordingly and perform
2961 * process migrations.
2963 static int cgroup_apply_control(struct cgroup
*cgrp
)
2967 cgroup_propagate_control(cgrp
);
2969 ret
= cgroup_apply_control_enable(cgrp
);
2974 * At this point, cgroup_e_css() results reflect the new csses
2975 * making the following cgroup_update_dfl_csses() properly update
2976 * css associations of all tasks in the subtree.
2978 ret
= cgroup_update_dfl_csses(cgrp
);
2986 * cgroup_finalize_control - finalize control mask update
2987 * @cgrp: root of the target subtree
2988 * @ret: the result of the update
2990 * Finalize control mask update. See cgroup_apply_control() for more info.
2992 static void cgroup_finalize_control(struct cgroup
*cgrp
, int ret
)
2995 cgroup_restore_control(cgrp
);
2996 cgroup_propagate_control(cgrp
);
2999 cgroup_apply_control_disable(cgrp
);
3002 static int cgroup_vet_subtree_control_enable(struct cgroup
*cgrp
, u16 enable
)
3004 u16 domain_enable
= enable
& ~cgrp_dfl_threaded_ss_mask
;
3006 /* if nothing is getting enabled, nothing to worry about */
3010 /* can @cgrp host any resources? */
3011 if (!cgroup_is_valid_domain(cgrp
->dom_cgrp
))
3014 /* mixables don't care */
3015 if (cgroup_is_mixable(cgrp
))
3018 if (domain_enable
) {
3019 /* can't enable domain controllers inside a thread subtree */
3020 if (cgroup_is_thread_root(cgrp
) || cgroup_is_threaded(cgrp
))
3024 * Threaded controllers can handle internal competitions
3025 * and are always allowed inside a (prospective) thread
3028 if (cgroup_can_be_thread_root(cgrp
) || cgroup_is_threaded(cgrp
))
3033 * Controllers can't be enabled for a cgroup with tasks to avoid
3034 * child cgroups competing against tasks.
3036 if (cgroup_has_tasks(cgrp
))
3042 /* change the enabled child controllers for a cgroup in the default hierarchy */
3043 static ssize_t
cgroup_subtree_control_write(struct kernfs_open_file
*of
,
3044 char *buf
, size_t nbytes
,
3047 u16 enable
= 0, disable
= 0;
3048 struct cgroup
*cgrp
, *child
;
3049 struct cgroup_subsys
*ss
;
3054 * Parse input - space separated list of subsystem names prefixed
3055 * with either + or -.
3057 buf
= strstrip(buf
);
3058 while ((tok
= strsep(&buf
, " "))) {
3061 do_each_subsys_mask(ss
, ssid
, ~cgrp_dfl_inhibit_ss_mask
) {
3062 if (!cgroup_ssid_enabled(ssid
) ||
3063 strcmp(tok
+ 1, ss
->name
))
3067 enable
|= 1 << ssid
;
3068 disable
&= ~(1 << ssid
);
3069 } else if (*tok
== '-') {
3070 disable
|= 1 << ssid
;
3071 enable
&= ~(1 << ssid
);
3076 } while_each_subsys_mask();
3077 if (ssid
== CGROUP_SUBSYS_COUNT
)
3081 cgrp
= cgroup_kn_lock_live(of
->kn
, true);
3085 for_each_subsys(ss
, ssid
) {
3086 if (enable
& (1 << ssid
)) {
3087 if (cgrp
->subtree_control
& (1 << ssid
)) {
3088 enable
&= ~(1 << ssid
);
3092 if (!(cgroup_control(cgrp
) & (1 << ssid
))) {
3096 } else if (disable
& (1 << ssid
)) {
3097 if (!(cgrp
->subtree_control
& (1 << ssid
))) {
3098 disable
&= ~(1 << ssid
);
3102 /* a child has it enabled? */
3103 cgroup_for_each_live_child(child
, cgrp
) {
3104 if (child
->subtree_control
& (1 << ssid
)) {
3112 if (!enable
&& !disable
) {
3117 ret
= cgroup_vet_subtree_control_enable(cgrp
, enable
);
3121 /* save and update control masks and prepare csses */
3122 cgroup_save_control(cgrp
);
3124 cgrp
->subtree_control
|= enable
;
3125 cgrp
->subtree_control
&= ~disable
;
3127 ret
= cgroup_apply_control(cgrp
);
3128 cgroup_finalize_control(cgrp
, ret
);
3132 kernfs_activate(cgrp
->kn
);
3134 cgroup_kn_unlock(of
->kn
);
3135 return ret
?: nbytes
;
3139 * cgroup_enable_threaded - make @cgrp threaded
3140 * @cgrp: the target cgroup
3142 * Called when "threaded" is written to the cgroup.type interface file and
3143 * tries to make @cgrp threaded and join the parent's resource domain.
3144 * This function is never called on the root cgroup as cgroup.type doesn't
3147 static int cgroup_enable_threaded(struct cgroup
*cgrp
)
3149 struct cgroup
*parent
= cgroup_parent(cgrp
);
3150 struct cgroup
*dom_cgrp
= parent
->dom_cgrp
;
3151 struct cgroup
*dsct
;
3152 struct cgroup_subsys_state
*d_css
;
3155 lockdep_assert_held(&cgroup_mutex
);
3157 /* noop if already threaded */
3158 if (cgroup_is_threaded(cgrp
))
3162 * If @cgroup is populated or has domain controllers enabled, it
3163 * can't be switched. While the below cgroup_can_be_thread_root()
3164 * test can catch the same conditions, that's only when @parent is
3165 * not mixable, so let's check it explicitly.
3167 if (cgroup_is_populated(cgrp
) ||
3168 cgrp
->subtree_control
& ~cgrp_dfl_threaded_ss_mask
)
3171 /* we're joining the parent's domain, ensure its validity */
3172 if (!cgroup_is_valid_domain(dom_cgrp
) ||
3173 !cgroup_can_be_thread_root(dom_cgrp
))
3177 * The following shouldn't cause actual migrations and should
3180 cgroup_save_control(cgrp
);
3182 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
)
3183 if (dsct
== cgrp
|| cgroup_is_threaded(dsct
))
3184 dsct
->dom_cgrp
= dom_cgrp
;
3186 ret
= cgroup_apply_control(cgrp
);
3188 parent
->nr_threaded_children
++;
3190 cgroup_finalize_control(cgrp
, ret
);
3194 static int cgroup_type_show(struct seq_file
*seq
, void *v
)
3196 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
3198 if (cgroup_is_threaded(cgrp
))
3199 seq_puts(seq
, "threaded\n");
3200 else if (!cgroup_is_valid_domain(cgrp
))
3201 seq_puts(seq
, "domain invalid\n");
3202 else if (cgroup_is_thread_root(cgrp
))
3203 seq_puts(seq
, "domain threaded\n");
3205 seq_puts(seq
, "domain\n");
3210 static ssize_t
cgroup_type_write(struct kernfs_open_file
*of
, char *buf
,
3211 size_t nbytes
, loff_t off
)
3213 struct cgroup
*cgrp
;
3216 /* only switching to threaded mode is supported */
3217 if (strcmp(strstrip(buf
), "threaded"))
3220 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
3224 /* threaded can only be enabled */
3225 ret
= cgroup_enable_threaded(cgrp
);
3227 cgroup_kn_unlock(of
->kn
);
3228 return ret
?: nbytes
;
3231 static int cgroup_max_descendants_show(struct seq_file
*seq
, void *v
)
3233 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
3234 int descendants
= READ_ONCE(cgrp
->max_descendants
);
3236 if (descendants
== INT_MAX
)
3237 seq_puts(seq
, "max\n");
3239 seq_printf(seq
, "%d\n", descendants
);
3244 static ssize_t
cgroup_max_descendants_write(struct kernfs_open_file
*of
,
3245 char *buf
, size_t nbytes
, loff_t off
)
3247 struct cgroup
*cgrp
;
3251 buf
= strstrip(buf
);
3252 if (!strcmp(buf
, "max")) {
3253 descendants
= INT_MAX
;
3255 ret
= kstrtoint(buf
, 0, &descendants
);
3260 if (descendants
< 0)
3263 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
3267 cgrp
->max_descendants
= descendants
;
3269 cgroup_kn_unlock(of
->kn
);
3274 static int cgroup_max_depth_show(struct seq_file
*seq
, void *v
)
3276 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
3277 int depth
= READ_ONCE(cgrp
->max_depth
);
3279 if (depth
== INT_MAX
)
3280 seq_puts(seq
, "max\n");
3282 seq_printf(seq
, "%d\n", depth
);
3287 static ssize_t
cgroup_max_depth_write(struct kernfs_open_file
*of
,
3288 char *buf
, size_t nbytes
, loff_t off
)
3290 struct cgroup
*cgrp
;
3294 buf
= strstrip(buf
);
3295 if (!strcmp(buf
, "max")) {
3298 ret
= kstrtoint(buf
, 0, &depth
);
3306 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
3310 cgrp
->max_depth
= depth
;
3312 cgroup_kn_unlock(of
->kn
);
3317 static int cgroup_events_show(struct seq_file
*seq
, void *v
)
3319 seq_printf(seq
, "populated %d\n",
3320 cgroup_is_populated(seq_css(seq
)->cgroup
));
3324 static int cgroup_stat_show(struct seq_file
*seq
, void *v
)
3326 struct cgroup
*cgroup
= seq_css(seq
)->cgroup
;
3328 seq_printf(seq
, "nr_descendants %d\n",
3329 cgroup
->nr_descendants
);
3330 seq_printf(seq
, "nr_dying_descendants %d\n",
3331 cgroup
->nr_dying_descendants
);
3337 static int cgroup_io_pressure_show(struct seq_file
*seq
, void *v
)
3339 return psi_show(seq
, &seq_css(seq
)->cgroup
->psi
, PSI_IO
);
3341 static int cgroup_memory_pressure_show(struct seq_file
*seq
, void *v
)
3343 return psi_show(seq
, &seq_css(seq
)->cgroup
->psi
, PSI_MEM
);
3345 static int cgroup_cpu_pressure_show(struct seq_file
*seq
, void *v
)
3347 return psi_show(seq
, &seq_css(seq
)->cgroup
->psi
, PSI_CPU
);
3350 static ssize_t
cgroup_pressure_write(struct kernfs_open_file
*of
, char *buf
,
3351 size_t nbytes
, enum psi_res res
)
3353 struct psi_trigger
*new;
3354 struct cgroup
*cgrp
;
3356 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
3361 cgroup_kn_unlock(of
->kn
);
3363 new = psi_trigger_create(&cgrp
->psi
, buf
, nbytes
, res
);
3366 return PTR_ERR(new);
3369 psi_trigger_replace(&of
->priv
, new);
3376 static ssize_t
cgroup_io_pressure_write(struct kernfs_open_file
*of
,
3377 char *buf
, size_t nbytes
,
3380 return cgroup_pressure_write(of
, buf
, nbytes
, PSI_IO
);
3383 static ssize_t
cgroup_memory_pressure_write(struct kernfs_open_file
*of
,
3384 char *buf
, size_t nbytes
,
3387 return cgroup_pressure_write(of
, buf
, nbytes
, PSI_MEM
);
3390 static ssize_t
cgroup_cpu_pressure_write(struct kernfs_open_file
*of
,
3391 char *buf
, size_t nbytes
,
3394 return cgroup_pressure_write(of
, buf
, nbytes
, PSI_CPU
);
3397 static unsigned int cgroup_pressure_poll(struct kernfs_open_file
*of
,
3400 return psi_trigger_poll(&of
->priv
, of
->file
, pt
);
3403 static void cgroup_pressure_release(struct kernfs_open_file
*of
)
3405 psi_trigger_replace(&of
->priv
, NULL
);
3407 #endif /* CONFIG_PSI */
3409 static int cgroup_file_open(struct kernfs_open_file
*of
)
3411 struct cftype
*cft
= of
->kn
->priv
;
3414 return cft
->open(of
);
3418 static void cgroup_file_release(struct kernfs_open_file
*of
)
3420 struct cftype
*cft
= of
->kn
->priv
;
3426 static ssize_t
cgroup_file_write(struct kernfs_open_file
*of
, char *buf
,
3427 size_t nbytes
, loff_t off
)
3429 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
3430 struct cgroup
*cgrp
= of
->kn
->parent
->priv
;
3431 struct cftype
*cft
= of
->kn
->priv
;
3432 struct cgroup_subsys_state
*css
;
3436 * If namespaces are delegation boundaries, disallow writes to
3437 * files in an non-init namespace root from inside the namespace
3438 * except for the files explicitly marked delegatable -
3439 * cgroup.procs and cgroup.subtree_control.
3441 if ((cgrp
->root
->flags
& CGRP_ROOT_NS_DELEGATE
) &&
3442 !(cft
->flags
& CFTYPE_NS_DELEGATABLE
) &&
3443 ns
!= &init_cgroup_ns
&& ns
->root_cset
->dfl_cgrp
== cgrp
)
3447 return cft
->write(of
, buf
, nbytes
, off
);
3450 * kernfs guarantees that a file isn't deleted with operations in
3451 * flight, which means that the matching css is and stays alive and
3452 * doesn't need to be pinned. The RCU locking is not necessary
3453 * either. It's just for the convenience of using cgroup_css().
3456 css
= cgroup_css(cgrp
, cft
->ss
);
3459 if (cft
->write_u64
) {
3460 unsigned long long v
;
3461 ret
= kstrtoull(buf
, 0, &v
);
3463 ret
= cft
->write_u64(css
, cft
, v
);
3464 } else if (cft
->write_s64
) {
3466 ret
= kstrtoll(buf
, 0, &v
);
3468 ret
= cft
->write_s64(css
, cft
, v
);
3473 return ret
?: nbytes
;
3476 static void *cgroup_seqfile_start(struct seq_file
*seq
, loff_t
*ppos
)
3478 return seq_cft(seq
)->seq_start(seq
, ppos
);
3481 static void *cgroup_seqfile_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
3483 return seq_cft(seq
)->seq_next(seq
, v
, ppos
);
3486 static void cgroup_seqfile_stop(struct seq_file
*seq
, void *v
)
3488 if (seq_cft(seq
)->seq_stop
)
3489 seq_cft(seq
)->seq_stop(seq
, v
);
3492 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
3494 struct cftype
*cft
= seq_cft(m
);
3495 struct cgroup_subsys_state
*css
= seq_css(m
);
3498 return cft
->seq_show(m
, arg
);
3501 seq_printf(m
, "%llu\n", cft
->read_u64(css
, cft
));
3502 else if (cft
->read_s64
)
3503 seq_printf(m
, "%lld\n", cft
->read_s64(css
, cft
));
3509 static struct kernfs_ops cgroup_kf_single_ops
= {
3510 .atomic_write_len
= PAGE_SIZE
,
3511 .open
= cgroup_file_open
,
3512 .release
= cgroup_file_release
,
3513 .write
= cgroup_file_write
,
3514 .seq_show
= cgroup_seqfile_show
,
3517 static struct kernfs_ops cgroup_kf_ops
= {
3518 .atomic_write_len
= PAGE_SIZE
,
3519 .open
= cgroup_file_open
,
3520 .release
= cgroup_file_release
,
3521 .write
= cgroup_file_write
,
3522 .seq_start
= cgroup_seqfile_start
,
3523 .seq_next
= cgroup_seqfile_next
,
3524 .seq_stop
= cgroup_seqfile_stop
,
3525 .seq_show
= cgroup_seqfile_show
,
3528 /* set uid and gid of cgroup dirs and files to that of the creator */
3529 static int cgroup_kn_set_ugid(struct kernfs_node
*kn
)
3531 struct iattr iattr
= { .ia_valid
= ATTR_UID
| ATTR_GID
,
3532 .ia_uid
= current_fsuid(),
3533 .ia_gid
= current_fsgid(), };
3535 if (uid_eq(iattr
.ia_uid
, GLOBAL_ROOT_UID
) &&
3536 gid_eq(iattr
.ia_gid
, GLOBAL_ROOT_GID
))
3539 return kernfs_setattr(kn
, &iattr
);
3542 static int cgroup_add_file(struct cgroup_subsys_state
*css
, struct cgroup
*cgrp
,
3545 char name
[CGROUP_FILE_NAME_MAX
];
3546 struct kernfs_node
*kn
;
3547 struct lock_class_key
*key
= NULL
;
3550 #ifdef CONFIG_DEBUG_LOCK_ALLOC
3551 key
= &cft
->lockdep_key
;
3553 kn
= __kernfs_create_file(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
),
3554 cgroup_file_mode(cft
), 0, cft
->kf_ops
, cft
,
3559 ret
= cgroup_kn_set_ugid(kn
);
3565 if (cft
->file_offset
) {
3566 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
3568 spin_lock_irq(&cgroup_file_kn_lock
);
3570 spin_unlock_irq(&cgroup_file_kn_lock
);
3577 * cgroup_addrm_files - add or remove files to a cgroup directory
3578 * @css: the target css
3579 * @cgrp: the target cgroup (usually css->cgroup)
3580 * @cfts: array of cftypes to be added
3581 * @is_add: whether to add or remove
3583 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
3584 * For removals, this function never fails.
3586 static int cgroup_addrm_files(struct cgroup_subsys_state
*css
,
3587 struct cgroup
*cgrp
, struct cftype cfts
[],
3590 struct cftype
*cft
, *cft_end
= NULL
;
3593 lockdep_assert_held(&cgroup_mutex
);
3596 for (cft
= cfts
; cft
!= cft_end
&& cft
->name
[0] != '\0'; cft
++) {
3597 /* does cft->flags tell us to skip this file on @cgrp? */
3598 if ((cft
->flags
& __CFTYPE_ONLY_ON_DFL
) && !cgroup_on_dfl(cgrp
))
3600 if ((cft
->flags
& __CFTYPE_NOT_ON_DFL
) && cgroup_on_dfl(cgrp
))
3602 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgroup_parent(cgrp
))
3604 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgroup_parent(cgrp
))
3608 ret
= cgroup_add_file(css
, cgrp
, cft
);
3610 pr_warn("%s: failed to add %s, err=%d\n",
3611 __func__
, cft
->name
, ret
);
3617 cgroup_rm_file(cgrp
, cft
);
3623 static int cgroup_apply_cftypes(struct cftype
*cfts
, bool is_add
)
3625 struct cgroup_subsys
*ss
= cfts
[0].ss
;
3626 struct cgroup
*root
= &ss
->root
->cgrp
;
3627 struct cgroup_subsys_state
*css
;
3630 lockdep_assert_held(&cgroup_mutex
);
3632 /* add/rm files for all cgroups created before */
3633 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
)) {
3634 struct cgroup
*cgrp
= css
->cgroup
;
3636 if (!(css
->flags
& CSS_VISIBLE
))
3639 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, is_add
);
3645 kernfs_activate(root
->kn
);
3649 static void cgroup_exit_cftypes(struct cftype
*cfts
)
3653 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3654 /* free copy for custom atomic_write_len, see init_cftypes() */
3655 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
)
3660 /* revert flags set by cgroup core while adding @cfts */
3661 cft
->flags
&= ~(__CFTYPE_ONLY_ON_DFL
| __CFTYPE_NOT_ON_DFL
);
3665 static int cgroup_init_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3669 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3670 struct kernfs_ops
*kf_ops
;
3672 WARN_ON(cft
->ss
|| cft
->kf_ops
);
3675 kf_ops
= &cgroup_kf_ops
;
3677 kf_ops
= &cgroup_kf_single_ops
;
3680 * Ugh... if @cft wants a custom max_write_len, we need to
3681 * make a copy of kf_ops to set its atomic_write_len.
3683 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
) {
3684 kf_ops
= kmemdup(kf_ops
, sizeof(*kf_ops
), GFP_KERNEL
);
3686 cgroup_exit_cftypes(cfts
);
3689 kf_ops
->atomic_write_len
= cft
->max_write_len
;
3692 cft
->kf_ops
= kf_ops
;
3699 static int cgroup_rm_cftypes_locked(struct cftype
*cfts
)
3701 lockdep_assert_held(&cgroup_mutex
);
3703 if (!cfts
|| !cfts
[0].ss
)
3706 list_del(&cfts
->node
);
3707 cgroup_apply_cftypes(cfts
, false);
3708 cgroup_exit_cftypes(cfts
);
3713 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3714 * @cfts: zero-length name terminated array of cftypes
3716 * Unregister @cfts. Files described by @cfts are removed from all
3717 * existing cgroups and all future cgroups won't have them either. This
3718 * function can be called anytime whether @cfts' subsys is attached or not.
3720 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3723 int cgroup_rm_cftypes(struct cftype
*cfts
)
3727 mutex_lock(&cgroup_mutex
);
3728 ret
= cgroup_rm_cftypes_locked(cfts
);
3729 mutex_unlock(&cgroup_mutex
);
3734 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3735 * @ss: target cgroup subsystem
3736 * @cfts: zero-length name terminated array of cftypes
3738 * Register @cfts to @ss. Files described by @cfts are created for all
3739 * existing cgroups to which @ss is attached and all future cgroups will
3740 * have them too. This function can be called anytime whether @ss is
3743 * Returns 0 on successful registration, -errno on failure. Note that this
3744 * function currently returns 0 as long as @cfts registration is successful
3745 * even if some file creation attempts on existing cgroups fail.
3747 static int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3751 if (!cgroup_ssid_enabled(ss
->id
))
3754 if (!cfts
|| cfts
[0].name
[0] == '\0')
3757 ret
= cgroup_init_cftypes(ss
, cfts
);
3761 mutex_lock(&cgroup_mutex
);
3763 list_add_tail(&cfts
->node
, &ss
->cfts
);
3764 ret
= cgroup_apply_cftypes(cfts
, true);
3766 cgroup_rm_cftypes_locked(cfts
);
3768 mutex_unlock(&cgroup_mutex
);
3773 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3774 * @ss: target cgroup subsystem
3775 * @cfts: zero-length name terminated array of cftypes
3777 * Similar to cgroup_add_cftypes() but the added files are only used for
3778 * the default hierarchy.
3780 int cgroup_add_dfl_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3784 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3785 cft
->flags
|= __CFTYPE_ONLY_ON_DFL
;
3786 return cgroup_add_cftypes(ss
, cfts
);
3790 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3791 * @ss: target cgroup subsystem
3792 * @cfts: zero-length name terminated array of cftypes
3794 * Similar to cgroup_add_cftypes() but the added files are only used for
3795 * the legacy hierarchies.
3797 int cgroup_add_legacy_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3801 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3802 cft
->flags
|= __CFTYPE_NOT_ON_DFL
;
3803 return cgroup_add_cftypes(ss
, cfts
);
3807 * cgroup_file_notify - generate a file modified event for a cgroup_file
3808 * @cfile: target cgroup_file
3810 * @cfile must have been obtained by setting cftype->file_offset.
3812 void cgroup_file_notify(struct cgroup_file
*cfile
)
3814 unsigned long flags
;
3816 spin_lock_irqsave(&cgroup_file_kn_lock
, flags
);
3818 kernfs_notify(cfile
->kn
);
3819 spin_unlock_irqrestore(&cgroup_file_kn_lock
, flags
);
3823 * css_next_child - find the next child of a given css
3824 * @pos: the current position (%NULL to initiate traversal)
3825 * @parent: css whose children to walk
3827 * This function returns the next child of @parent and should be called
3828 * under either cgroup_mutex or RCU read lock. The only requirement is
3829 * that @parent and @pos are accessible. The next sibling is guaranteed to
3830 * be returned regardless of their states.
3832 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3833 * css which finished ->css_online() is guaranteed to be visible in the
3834 * future iterations and will stay visible until the last reference is put.
3835 * A css which hasn't finished ->css_online() or already finished
3836 * ->css_offline() may show up during traversal. It's each subsystem's
3837 * responsibility to synchronize against on/offlining.
3839 struct cgroup_subsys_state
*css_next_child(struct cgroup_subsys_state
*pos
,
3840 struct cgroup_subsys_state
*parent
)
3842 struct cgroup_subsys_state
*next
;
3844 cgroup_assert_mutex_or_rcu_locked();
3847 * @pos could already have been unlinked from the sibling list.
3848 * Once a cgroup is removed, its ->sibling.next is no longer
3849 * updated when its next sibling changes. CSS_RELEASED is set when
3850 * @pos is taken off list, at which time its next pointer is valid,
3851 * and, as releases are serialized, the one pointed to by the next
3852 * pointer is guaranteed to not have started release yet. This
3853 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3854 * critical section, the one pointed to by its next pointer is
3855 * guaranteed to not have finished its RCU grace period even if we
3856 * have dropped rcu_read_lock() inbetween iterations.
3858 * If @pos has CSS_RELEASED set, its next pointer can't be
3859 * dereferenced; however, as each css is given a monotonically
3860 * increasing unique serial number and always appended to the
3861 * sibling list, the next one can be found by walking the parent's
3862 * children until the first css with higher serial number than
3863 * @pos's. While this path can be slower, it happens iff iteration
3864 * races against release and the race window is very small.
3867 next
= list_entry_rcu(parent
->children
.next
, struct cgroup_subsys_state
, sibling
);
3868 } else if (likely(!(pos
->flags
& CSS_RELEASED
))) {
3869 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup_subsys_state
, sibling
);
3871 list_for_each_entry_rcu(next
, &parent
->children
, sibling
)
3872 if (next
->serial_nr
> pos
->serial_nr
)
3877 * @next, if not pointing to the head, can be dereferenced and is
3880 if (&next
->sibling
!= &parent
->children
)
3886 * css_next_descendant_pre - find the next descendant for pre-order walk
3887 * @pos: the current position (%NULL to initiate traversal)
3888 * @root: css whose descendants to walk
3890 * To be used by css_for_each_descendant_pre(). Find the next descendant
3891 * to visit for pre-order traversal of @root's descendants. @root is
3892 * included in the iteration and the first node to be visited.
3894 * While this function requires cgroup_mutex or RCU read locking, it
3895 * doesn't require the whole traversal to be contained in a single critical
3896 * section. This function will return the correct next descendant as long
3897 * as both @pos and @root are accessible and @pos is a descendant of @root.
3899 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3900 * css which finished ->css_online() is guaranteed to be visible in the
3901 * future iterations and will stay visible until the last reference is put.
3902 * A css which hasn't finished ->css_online() or already finished
3903 * ->css_offline() may show up during traversal. It's each subsystem's
3904 * responsibility to synchronize against on/offlining.
3906 struct cgroup_subsys_state
*
3907 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
3908 struct cgroup_subsys_state
*root
)
3910 struct cgroup_subsys_state
*next
;
3912 cgroup_assert_mutex_or_rcu_locked();
3914 /* if first iteration, visit @root */
3918 /* visit the first child if exists */
3919 next
= css_next_child(NULL
, pos
);
3923 /* no child, visit my or the closest ancestor's next sibling */
3924 while (pos
!= root
) {
3925 next
= css_next_child(pos
, pos
->parent
);
3935 * css_rightmost_descendant - return the rightmost descendant of a css
3936 * @pos: css of interest
3938 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3939 * is returned. This can be used during pre-order traversal to skip
3942 * While this function requires cgroup_mutex or RCU read locking, it
3943 * doesn't require the whole traversal to be contained in a single critical
3944 * section. This function will return the correct rightmost descendant as
3945 * long as @pos is accessible.
3947 struct cgroup_subsys_state
*
3948 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
3950 struct cgroup_subsys_state
*last
, *tmp
;
3952 cgroup_assert_mutex_or_rcu_locked();
3956 /* ->prev isn't RCU safe, walk ->next till the end */
3958 css_for_each_child(tmp
, last
)
3965 static struct cgroup_subsys_state
*
3966 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
3968 struct cgroup_subsys_state
*last
;
3972 pos
= css_next_child(NULL
, pos
);
3979 * css_next_descendant_post - find the next descendant for post-order walk
3980 * @pos: the current position (%NULL to initiate traversal)
3981 * @root: css whose descendants to walk
3983 * To be used by css_for_each_descendant_post(). Find the next descendant
3984 * to visit for post-order traversal of @root's descendants. @root is
3985 * included in the iteration and the last node to be visited.
3987 * While this function requires cgroup_mutex or RCU read locking, it
3988 * doesn't require the whole traversal to be contained in a single critical
3989 * section. This function will return the correct next descendant as long
3990 * as both @pos and @cgroup are accessible and @pos is a descendant of
3993 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3994 * css which finished ->css_online() is guaranteed to be visible in the
3995 * future iterations and will stay visible until the last reference is put.
3996 * A css which hasn't finished ->css_online() or already finished
3997 * ->css_offline() may show up during traversal. It's each subsystem's
3998 * responsibility to synchronize against on/offlining.
4000 struct cgroup_subsys_state
*
4001 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
4002 struct cgroup_subsys_state
*root
)
4004 struct cgroup_subsys_state
*next
;
4006 cgroup_assert_mutex_or_rcu_locked();
4008 /* if first iteration, visit leftmost descendant which may be @root */
4010 return css_leftmost_descendant(root
);
4012 /* if we visited @root, we're done */
4016 /* if there's an unvisited sibling, visit its leftmost descendant */
4017 next
= css_next_child(pos
, pos
->parent
);
4019 return css_leftmost_descendant(next
);
4021 /* no sibling left, visit parent */
4026 * css_has_online_children - does a css have online children
4027 * @css: the target css
4029 * Returns %true if @css has any online children; otherwise, %false. This
4030 * function can be called from any context but the caller is responsible
4031 * for synchronizing against on/offlining as necessary.
4033 bool css_has_online_children(struct cgroup_subsys_state
*css
)
4035 struct cgroup_subsys_state
*child
;
4039 css_for_each_child(child
, css
) {
4040 if (child
->flags
& CSS_ONLINE
) {
4049 static struct css_set
*css_task_iter_next_css_set(struct css_task_iter
*it
)
4051 struct list_head
*l
;
4052 struct cgrp_cset_link
*link
;
4053 struct css_set
*cset
;
4055 lockdep_assert_held(&css_set_lock
);
4057 /* find the next threaded cset */
4058 if (it
->tcset_pos
) {
4059 l
= it
->tcset_pos
->next
;
4061 if (l
!= it
->tcset_head
) {
4063 return container_of(l
, struct css_set
,
4064 threaded_csets_node
);
4067 it
->tcset_pos
= NULL
;
4070 /* find the next cset */
4073 if (l
== it
->cset_head
) {
4074 it
->cset_pos
= NULL
;
4079 cset
= container_of(l
, struct css_set
, e_cset_node
[it
->ss
->id
]);
4081 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
4087 /* initialize threaded css_set walking */
4088 if (it
->flags
& CSS_TASK_ITER_THREADED
) {
4090 put_css_set_locked(it
->cur_dcset
);
4091 it
->cur_dcset
= cset
;
4094 it
->tcset_head
= &cset
->threaded_csets
;
4095 it
->tcset_pos
= &cset
->threaded_csets
;
4102 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
4103 * @it: the iterator to advance
4105 * Advance @it to the next css_set to walk.
4107 static void css_task_iter_advance_css_set(struct css_task_iter
*it
)
4109 struct css_set
*cset
;
4111 lockdep_assert_held(&css_set_lock
);
4113 /* Advance to the next non-empty css_set */
4115 cset
= css_task_iter_next_css_set(it
);
4117 it
->task_pos
= NULL
;
4120 } while (!css_set_populated(cset
));
4122 if (!list_empty(&cset
->tasks
))
4123 it
->task_pos
= cset
->tasks
.next
;
4125 it
->task_pos
= cset
->mg_tasks
.next
;
4127 it
->tasks_head
= &cset
->tasks
;
4128 it
->mg_tasks_head
= &cset
->mg_tasks
;
4131 * We don't keep css_sets locked across iteration steps and thus
4132 * need to take steps to ensure that iteration can be resumed after
4133 * the lock is re-acquired. Iteration is performed at two levels -
4134 * css_sets and tasks in them.
4136 * Once created, a css_set never leaves its cgroup lists, so a
4137 * pinned css_set is guaranteed to stay put and we can resume
4138 * iteration afterwards.
4140 * Tasks may leave @cset across iteration steps. This is resolved
4141 * by registering each iterator with the css_set currently being
4142 * walked and making css_set_move_task() advance iterators whose
4143 * next task is leaving.
4146 list_del(&it
->iters_node
);
4147 put_css_set_locked(it
->cur_cset
);
4150 it
->cur_cset
= cset
;
4151 list_add(&it
->iters_node
, &cset
->task_iters
);
4154 static void css_task_iter_advance(struct css_task_iter
*it
)
4156 struct list_head
*next
;
4158 lockdep_assert_held(&css_set_lock
);
4162 * Advance iterator to find next entry. cset->tasks is
4163 * consumed first and then ->mg_tasks. After ->mg_tasks,
4164 * we move onto the next cset.
4166 next
= it
->task_pos
->next
;
4168 if (next
== it
->tasks_head
)
4169 next
= it
->mg_tasks_head
->next
;
4171 if (next
== it
->mg_tasks_head
)
4172 css_task_iter_advance_css_set(it
);
4174 it
->task_pos
= next
;
4176 /* called from start, proceed to the first cset */
4177 css_task_iter_advance_css_set(it
);
4180 /* if PROCS, skip over tasks which aren't group leaders */
4181 if ((it
->flags
& CSS_TASK_ITER_PROCS
) && it
->task_pos
&&
4182 !thread_group_leader(list_entry(it
->task_pos
, struct task_struct
,
4188 * css_task_iter_start - initiate task iteration
4189 * @css: the css to walk tasks of
4190 * @flags: CSS_TASK_ITER_* flags
4191 * @it: the task iterator to use
4193 * Initiate iteration through the tasks of @css. The caller can call
4194 * css_task_iter_next() to walk through the tasks until the function
4195 * returns NULL. On completion of iteration, css_task_iter_end() must be
4198 void css_task_iter_start(struct cgroup_subsys_state
*css
, unsigned int flags
,
4199 struct css_task_iter
*it
)
4201 /* no one should try to iterate before mounting cgroups */
4202 WARN_ON_ONCE(!use_task_css_set_links
);
4204 memset(it
, 0, sizeof(*it
));
4206 spin_lock_irq(&css_set_lock
);
4212 it
->cset_pos
= &css
->cgroup
->e_csets
[css
->ss
->id
];
4214 it
->cset_pos
= &css
->cgroup
->cset_links
;
4216 it
->cset_head
= it
->cset_pos
;
4218 css_task_iter_advance(it
);
4220 spin_unlock_irq(&css_set_lock
);
4224 * css_task_iter_next - return the next task for the iterator
4225 * @it: the task iterator being iterated
4227 * The "next" function for task iteration. @it should have been
4228 * initialized via css_task_iter_start(). Returns NULL when the iteration
4231 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
4234 put_task_struct(it
->cur_task
);
4235 it
->cur_task
= NULL
;
4238 spin_lock_irq(&css_set_lock
);
4241 it
->cur_task
= list_entry(it
->task_pos
, struct task_struct
,
4243 get_task_struct(it
->cur_task
);
4244 css_task_iter_advance(it
);
4247 spin_unlock_irq(&css_set_lock
);
4249 return it
->cur_task
;
4253 * css_task_iter_end - finish task iteration
4254 * @it: the task iterator to finish
4256 * Finish task iteration started by css_task_iter_start().
4258 void css_task_iter_end(struct css_task_iter
*it
)
4261 spin_lock_irq(&css_set_lock
);
4262 list_del(&it
->iters_node
);
4263 put_css_set_locked(it
->cur_cset
);
4264 spin_unlock_irq(&css_set_lock
);
4268 put_css_set(it
->cur_dcset
);
4271 put_task_struct(it
->cur_task
);
4274 static void cgroup_procs_release(struct kernfs_open_file
*of
)
4277 css_task_iter_end(of
->priv
);
4282 static void *cgroup_procs_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
4284 struct kernfs_open_file
*of
= s
->private;
4285 struct css_task_iter
*it
= of
->priv
;
4287 return css_task_iter_next(it
);
4290 static void *__cgroup_procs_start(struct seq_file
*s
, loff_t
*pos
,
4291 unsigned int iter_flags
)
4293 struct kernfs_open_file
*of
= s
->private;
4294 struct cgroup
*cgrp
= seq_css(s
)->cgroup
;
4295 struct css_task_iter
*it
= of
->priv
;
4298 * When a seq_file is seeked, it's always traversed sequentially
4299 * from position 0, so we can simply keep iterating on !0 *pos.
4302 if (WARN_ON_ONCE((*pos
)++))
4303 return ERR_PTR(-EINVAL
);
4305 it
= kzalloc(sizeof(*it
), GFP_KERNEL
);
4307 return ERR_PTR(-ENOMEM
);
4309 css_task_iter_start(&cgrp
->self
, iter_flags
, it
);
4310 } else if (!(*pos
)++) {
4311 css_task_iter_end(it
);
4312 css_task_iter_start(&cgrp
->self
, iter_flags
, it
);
4315 return cgroup_procs_next(s
, NULL
, NULL
);
4318 static void *cgroup_procs_start(struct seq_file
*s
, loff_t
*pos
)
4320 struct cgroup
*cgrp
= seq_css(s
)->cgroup
;
4323 * All processes of a threaded subtree belong to the domain cgroup
4324 * of the subtree. Only threads can be distributed across the
4325 * subtree. Reject reads on cgroup.procs in the subtree proper.
4326 * They're always empty anyway.
4328 if (cgroup_is_threaded(cgrp
))
4329 return ERR_PTR(-EOPNOTSUPP
);
4331 return __cgroup_procs_start(s
, pos
, CSS_TASK_ITER_PROCS
|
4332 CSS_TASK_ITER_THREADED
);
4335 static int cgroup_procs_show(struct seq_file
*s
, void *v
)
4337 seq_printf(s
, "%d\n", task_pid_vnr(v
));
4341 static int cgroup_procs_write_permission(struct cgroup
*src_cgrp
,
4342 struct cgroup
*dst_cgrp
,
4343 struct super_block
*sb
)
4345 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
4346 struct cgroup
*com_cgrp
= src_cgrp
;
4347 struct inode
*inode
;
4350 lockdep_assert_held(&cgroup_mutex
);
4352 /* find the common ancestor */
4353 while (!cgroup_is_descendant(dst_cgrp
, com_cgrp
))
4354 com_cgrp
= cgroup_parent(com_cgrp
);
4356 /* %current should be authorized to migrate to the common ancestor */
4357 inode
= kernfs_get_inode(sb
, com_cgrp
->procs_file
.kn
);
4361 ret
= inode_permission(inode
, MAY_WRITE
);
4367 * If namespaces are delegation boundaries, %current must be able
4368 * to see both source and destination cgroups from its namespace.
4370 if ((cgrp_dfl_root
.flags
& CGRP_ROOT_NS_DELEGATE
) &&
4371 (!cgroup_is_descendant(src_cgrp
, ns
->root_cset
->dfl_cgrp
) ||
4372 !cgroup_is_descendant(dst_cgrp
, ns
->root_cset
->dfl_cgrp
)))
4378 static ssize_t
cgroup_procs_write(struct kernfs_open_file
*of
,
4379 char *buf
, size_t nbytes
, loff_t off
)
4381 struct cgroup
*src_cgrp
, *dst_cgrp
;
4382 struct task_struct
*task
;
4385 dst_cgrp
= cgroup_kn_lock_live(of
->kn
, false);
4389 task
= cgroup_procs_write_start(buf
, true);
4390 ret
= PTR_ERR_OR_ZERO(task
);
4394 /* find the source cgroup */
4395 spin_lock_irq(&css_set_lock
);
4396 src_cgrp
= task_cgroup_from_root(task
, &cgrp_dfl_root
);
4397 spin_unlock_irq(&css_set_lock
);
4399 ret
= cgroup_procs_write_permission(src_cgrp
, dst_cgrp
,
4400 of
->file
->f_path
.dentry
->d_sb
);
4404 ret
= cgroup_attach_task(dst_cgrp
, task
, true);
4407 cgroup_procs_write_finish(task
);
4409 cgroup_kn_unlock(of
->kn
);
4411 return ret
?: nbytes
;
4414 static void *cgroup_threads_start(struct seq_file
*s
, loff_t
*pos
)
4416 return __cgroup_procs_start(s
, pos
, 0);
4419 static ssize_t
cgroup_threads_write(struct kernfs_open_file
*of
,
4420 char *buf
, size_t nbytes
, loff_t off
)
4422 struct cgroup
*src_cgrp
, *dst_cgrp
;
4423 struct task_struct
*task
;
4426 buf
= strstrip(buf
);
4428 dst_cgrp
= cgroup_kn_lock_live(of
->kn
, false);
4432 task
= cgroup_procs_write_start(buf
, false);
4433 ret
= PTR_ERR_OR_ZERO(task
);
4437 /* find the source cgroup */
4438 spin_lock_irq(&css_set_lock
);
4439 src_cgrp
= task_cgroup_from_root(task
, &cgrp_dfl_root
);
4440 spin_unlock_irq(&css_set_lock
);
4442 /* thread migrations follow the cgroup.procs delegation rule */
4443 ret
= cgroup_procs_write_permission(src_cgrp
, dst_cgrp
,
4444 of
->file
->f_path
.dentry
->d_sb
);
4448 /* and must be contained in the same domain */
4450 if (src_cgrp
->dom_cgrp
!= dst_cgrp
->dom_cgrp
)
4453 ret
= cgroup_attach_task(dst_cgrp
, task
, false);
4456 cgroup_procs_write_finish(task
);
4458 cgroup_kn_unlock(of
->kn
);
4460 return ret
?: nbytes
;
4463 /* cgroup core interface files for the default hierarchy */
4464 static struct cftype cgroup_base_files
[] = {
4466 .name
= "cgroup.type",
4467 .flags
= CFTYPE_NOT_ON_ROOT
,
4468 .seq_show
= cgroup_type_show
,
4469 .write
= cgroup_type_write
,
4472 .name
= "cgroup.procs",
4473 .flags
= CFTYPE_NS_DELEGATABLE
,
4474 .file_offset
= offsetof(struct cgroup
, procs_file
),
4475 .release
= cgroup_procs_release
,
4476 .seq_start
= cgroup_procs_start
,
4477 .seq_next
= cgroup_procs_next
,
4478 .seq_show
= cgroup_procs_show
,
4479 .write
= cgroup_procs_write
,
4482 .name
= "cgroup.threads",
4483 .release
= cgroup_procs_release
,
4484 .seq_start
= cgroup_threads_start
,
4485 .seq_next
= cgroup_procs_next
,
4486 .seq_show
= cgroup_procs_show
,
4487 .write
= cgroup_threads_write
,
4490 .name
= "cgroup.controllers",
4491 .seq_show
= cgroup_controllers_show
,
4494 .name
= "cgroup.subtree_control",
4495 .flags
= CFTYPE_NS_DELEGATABLE
,
4496 .seq_show
= cgroup_subtree_control_show
,
4497 .write
= cgroup_subtree_control_write
,
4500 .name
= "cgroup.events",
4501 .flags
= CFTYPE_NOT_ON_ROOT
,
4502 .file_offset
= offsetof(struct cgroup
, events_file
),
4503 .seq_show
= cgroup_events_show
,
4506 .name
= "cgroup.max.descendants",
4507 .seq_show
= cgroup_max_descendants_show
,
4508 .write
= cgroup_max_descendants_write
,
4511 .name
= "cgroup.max.depth",
4512 .seq_show
= cgroup_max_depth_show
,
4513 .write
= cgroup_max_depth_write
,
4516 .name
= "cgroup.stat",
4517 .seq_show
= cgroup_stat_show
,
4521 .name
= "io.pressure",
4522 .flags
= CFTYPE_NOT_ON_ROOT
,
4523 .seq_show
= cgroup_io_pressure_show
,
4524 .write
= cgroup_io_pressure_write
,
4525 .poll
= cgroup_pressure_poll
,
4526 .release
= cgroup_pressure_release
,
4529 .name
= "memory.pressure",
4530 .flags
= CFTYPE_NOT_ON_ROOT
,
4531 .seq_show
= cgroup_memory_pressure_show
,
4532 .write
= cgroup_memory_pressure_write
,
4533 .poll
= cgroup_pressure_poll
,
4534 .release
= cgroup_pressure_release
,
4537 .name
= "cpu.pressure",
4538 .flags
= CFTYPE_NOT_ON_ROOT
,
4539 .seq_show
= cgroup_cpu_pressure_show
,
4540 .write
= cgroup_cpu_pressure_write
,
4541 .poll
= cgroup_pressure_poll
,
4542 .release
= cgroup_pressure_release
,
4544 #endif /* CONFIG_PSI */
4549 * css destruction is four-stage process.
4551 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4552 * Implemented in kill_css().
4554 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4555 * and thus css_tryget_online() is guaranteed to fail, the css can be
4556 * offlined by invoking offline_css(). After offlining, the base ref is
4557 * put. Implemented in css_killed_work_fn().
4559 * 3. When the percpu_ref reaches zero, the only possible remaining
4560 * accessors are inside RCU read sections. css_release() schedules the
4563 * 4. After the grace period, the css can be freed. Implemented in
4564 * css_free_work_fn().
4566 * It is actually hairier because both step 2 and 4 require process context
4567 * and thus involve punting to css->destroy_work adding two additional
4568 * steps to the already complex sequence.
4570 static void css_free_work_fn(struct work_struct
*work
)
4572 struct cgroup_subsys_state
*css
=
4573 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4574 struct cgroup_subsys
*ss
= css
->ss
;
4575 struct cgroup
*cgrp
= css
->cgroup
;
4577 percpu_ref_exit(&css
->refcnt
);
4581 struct cgroup_subsys_state
*parent
= css
->parent
;
4585 cgroup_idr_remove(&ss
->css_idr
, id
);
4591 /* cgroup free path */
4592 atomic_dec(&cgrp
->root
->nr_cgrps
);
4593 cgroup1_pidlist_destroy_all(cgrp
);
4594 cancel_work_sync(&cgrp
->release_agent_work
);
4596 if (cgroup_parent(cgrp
)) {
4598 * We get a ref to the parent, and put the ref when
4599 * this cgroup is being freed, so it's guaranteed
4600 * that the parent won't be destroyed before its
4603 cgroup_put(cgroup_parent(cgrp
));
4604 kernfs_put(cgrp
->kn
);
4605 if (cgroup_on_dfl(cgrp
))
4606 psi_cgroup_free(cgrp
);
4610 * This is root cgroup's refcnt reaching zero,
4611 * which indicates that the root should be
4614 cgroup_destroy_root(cgrp
->root
);
4619 static void css_free_rcu_fn(struct rcu_head
*rcu_head
)
4621 struct cgroup_subsys_state
*css
=
4622 container_of(rcu_head
, struct cgroup_subsys_state
, rcu_head
);
4624 INIT_WORK(&css
->destroy_work
, css_free_work_fn
);
4625 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4628 static void css_release_work_fn(struct work_struct
*work
)
4630 struct cgroup_subsys_state
*css
=
4631 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4632 struct cgroup_subsys
*ss
= css
->ss
;
4633 struct cgroup
*cgrp
= css
->cgroup
;
4635 mutex_lock(&cgroup_mutex
);
4637 css
->flags
|= CSS_RELEASED
;
4638 list_del_rcu(&css
->sibling
);
4641 /* css release path */
4642 cgroup_idr_replace(&ss
->css_idr
, NULL
, css
->id
);
4643 if (ss
->css_released
)
4644 ss
->css_released(css
);
4646 struct cgroup
*tcgrp
;
4648 /* cgroup release path */
4649 trace_cgroup_release(cgrp
);
4651 for (tcgrp
= cgroup_parent(cgrp
); tcgrp
;
4652 tcgrp
= cgroup_parent(tcgrp
))
4653 tcgrp
->nr_dying_descendants
--;
4655 cgroup_idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
4659 * There are two control paths which try to determine
4660 * cgroup from dentry without going through kernfs -
4661 * cgroupstats_build() and css_tryget_online_from_dir().
4662 * Those are supported by RCU protecting clearing of
4663 * cgrp->kn->priv backpointer.
4666 RCU_INIT_POINTER(*(void __rcu __force
**)&cgrp
->kn
->priv
,
4669 cgroup_bpf_put(cgrp
);
4672 mutex_unlock(&cgroup_mutex
);
4674 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4677 static void css_release(struct percpu_ref
*ref
)
4679 struct cgroup_subsys_state
*css
=
4680 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4682 INIT_WORK(&css
->destroy_work
, css_release_work_fn
);
4683 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4686 static void init_and_link_css(struct cgroup_subsys_state
*css
,
4687 struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4689 lockdep_assert_held(&cgroup_mutex
);
4691 cgroup_get_live(cgrp
);
4693 memset(css
, 0, sizeof(*css
));
4697 INIT_LIST_HEAD(&css
->sibling
);
4698 INIT_LIST_HEAD(&css
->children
);
4699 css
->serial_nr
= css_serial_nr_next
++;
4700 atomic_set(&css
->online_cnt
, 0);
4702 if (cgroup_parent(cgrp
)) {
4703 css
->parent
= cgroup_css(cgroup_parent(cgrp
), ss
);
4704 css_get(css
->parent
);
4707 BUG_ON(cgroup_css(cgrp
, ss
));
4710 /* invoke ->css_online() on a new CSS and mark it online if successful */
4711 static int online_css(struct cgroup_subsys_state
*css
)
4713 struct cgroup_subsys
*ss
= css
->ss
;
4716 lockdep_assert_held(&cgroup_mutex
);
4719 ret
= ss
->css_online(css
);
4721 css
->flags
|= CSS_ONLINE
;
4722 rcu_assign_pointer(css
->cgroup
->subsys
[ss
->id
], css
);
4724 atomic_inc(&css
->online_cnt
);
4726 atomic_inc(&css
->parent
->online_cnt
);
4731 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4732 static void offline_css(struct cgroup_subsys_state
*css
)
4734 struct cgroup_subsys
*ss
= css
->ss
;
4736 lockdep_assert_held(&cgroup_mutex
);
4738 if (!(css
->flags
& CSS_ONLINE
))
4741 if (ss
->css_offline
)
4742 ss
->css_offline(css
);
4744 css
->flags
&= ~CSS_ONLINE
;
4745 RCU_INIT_POINTER(css
->cgroup
->subsys
[ss
->id
], NULL
);
4747 wake_up_all(&css
->cgroup
->offline_waitq
);
4751 * css_create - create a cgroup_subsys_state
4752 * @cgrp: the cgroup new css will be associated with
4753 * @ss: the subsys of new css
4755 * Create a new css associated with @cgrp - @ss pair. On success, the new
4756 * css is online and installed in @cgrp. This function doesn't create the
4757 * interface files. Returns 0 on success, -errno on failure.
4759 static struct cgroup_subsys_state
*css_create(struct cgroup
*cgrp
,
4760 struct cgroup_subsys
*ss
)
4762 struct cgroup
*parent
= cgroup_parent(cgrp
);
4763 struct cgroup_subsys_state
*parent_css
= cgroup_css(parent
, ss
);
4764 struct cgroup_subsys_state
*css
;
4767 lockdep_assert_held(&cgroup_mutex
);
4769 css
= ss
->css_alloc(parent_css
);
4771 css
= ERR_PTR(-ENOMEM
);
4775 init_and_link_css(css
, ss
, cgrp
);
4777 err
= percpu_ref_init(&css
->refcnt
, css_release
, 0, GFP_KERNEL
);
4781 err
= cgroup_idr_alloc(&ss
->css_idr
, NULL
, 2, 0, GFP_KERNEL
);
4786 /* @css is ready to be brought online now, make it visible */
4787 list_add_tail_rcu(&css
->sibling
, &parent_css
->children
);
4788 cgroup_idr_replace(&ss
->css_idr
, css
, css
->id
);
4790 err
= online_css(css
);
4794 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4795 cgroup_parent(parent
)) {
4796 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4797 current
->comm
, current
->pid
, ss
->name
);
4798 if (!strcmp(ss
->name
, "memory"))
4799 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
4800 ss
->warned_broken_hierarchy
= true;
4806 list_del_rcu(&css
->sibling
);
4808 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4809 return ERR_PTR(err
);
4813 * The returned cgroup is fully initialized including its control mask, but
4814 * it isn't associated with its kernfs_node and doesn't have the control
4817 static struct cgroup
*cgroup_create(struct cgroup
*parent
)
4819 struct cgroup_root
*root
= parent
->root
;
4820 struct cgroup
*cgrp
, *tcgrp
;
4821 int level
= parent
->level
+ 1;
4824 /* allocate the cgroup and its ID, 0 is reserved for the root */
4825 cgrp
= kzalloc(sizeof(*cgrp
) +
4826 sizeof(cgrp
->ancestor_ids
[0]) * (level
+ 1), GFP_KERNEL
);
4828 return ERR_PTR(-ENOMEM
);
4830 ret
= percpu_ref_init(&cgrp
->self
.refcnt
, css_release
, 0, GFP_KERNEL
);
4835 * Temporarily set the pointer to NULL, so idr_find() won't return
4836 * a half-baked cgroup.
4838 cgrp
->id
= cgroup_idr_alloc(&root
->cgroup_idr
, NULL
, 2, 0, GFP_KERNEL
);
4841 goto out_cancel_ref
;
4844 init_cgroup_housekeeping(cgrp
);
4846 cgrp
->self
.parent
= &parent
->self
;
4848 cgrp
->level
= level
;
4850 for (tcgrp
= cgrp
; tcgrp
; tcgrp
= cgroup_parent(tcgrp
)) {
4851 cgrp
->ancestor_ids
[tcgrp
->level
] = tcgrp
->id
;
4854 tcgrp
->nr_descendants
++;
4857 if (notify_on_release(parent
))
4858 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4860 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4861 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4863 cgrp
->self
.serial_nr
= css_serial_nr_next
++;
4865 /* allocation complete, commit to creation */
4866 list_add_tail_rcu(&cgrp
->self
.sibling
, &cgroup_parent(cgrp
)->self
.children
);
4867 atomic_inc(&root
->nr_cgrps
);
4868 cgroup_get_live(parent
);
4871 * @cgrp is now fully operational. If something fails after this
4872 * point, it'll be released via the normal destruction path.
4874 cgroup_idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
4877 * On the default hierarchy, a child doesn't automatically inherit
4878 * subtree_control from the parent. Each is configured manually.
4880 if (!cgroup_on_dfl(cgrp
))
4881 cgrp
->subtree_control
= cgroup_control(cgrp
);
4883 if (cgroup_on_dfl(cgrp
)) {
4884 ret
= psi_cgroup_alloc(cgrp
);
4890 cgroup_bpf_inherit(cgrp
, parent
);
4892 cgroup_propagate_control(cgrp
);
4897 cgroup_idr_remove(&root
->cgroup_idr
, cgrp
->id
);
4899 percpu_ref_exit(&cgrp
->self
.refcnt
);
4902 return ERR_PTR(ret
);
4905 static bool cgroup_check_hierarchy_limits(struct cgroup
*parent
)
4907 struct cgroup
*cgroup
;
4911 lockdep_assert_held(&cgroup_mutex
);
4913 for (cgroup
= parent
; cgroup
; cgroup
= cgroup_parent(cgroup
)) {
4914 if (cgroup
->nr_descendants
>= cgroup
->max_descendants
)
4917 if (level
> cgroup
->max_depth
)
4928 int cgroup_mkdir(struct kernfs_node
*parent_kn
, const char *name
, umode_t mode
)
4930 struct cgroup
*parent
, *cgrp
;
4931 struct kernfs_node
*kn
;
4934 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
4935 if (strchr(name
, '\n'))
4938 parent
= cgroup_kn_lock_live(parent_kn
, false);
4942 if (!cgroup_check_hierarchy_limits(parent
)) {
4947 cgrp
= cgroup_create(parent
);
4949 ret
= PTR_ERR(cgrp
);
4953 /* create the directory */
4954 kn
= kernfs_create_dir(parent
->kn
, name
, mode
, cgrp
);
4962 * This extra ref will be put in cgroup_free_fn() and guarantees
4963 * that @cgrp->kn is always accessible.
4967 ret
= cgroup_kn_set_ugid(kn
);
4971 ret
= css_populate_dir(&cgrp
->self
);
4975 ret
= cgroup_apply_control_enable(cgrp
);
4979 trace_cgroup_mkdir(cgrp
);
4981 /* let's create and online css's */
4982 kernfs_activate(kn
);
4988 cgroup_destroy_locked(cgrp
);
4990 cgroup_kn_unlock(parent_kn
);
4995 * This is called when the refcnt of a css is confirmed to be killed.
4996 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4997 * initate destruction and put the css ref from kill_css().
4999 static void css_killed_work_fn(struct work_struct
*work
)
5001 struct cgroup_subsys_state
*css
=
5002 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
5004 mutex_lock(&cgroup_mutex
);
5009 /* @css can't go away while we're holding cgroup_mutex */
5011 } while (css
&& atomic_dec_and_test(&css
->online_cnt
));
5013 mutex_unlock(&cgroup_mutex
);
5016 /* css kill confirmation processing requires process context, bounce */
5017 static void css_killed_ref_fn(struct percpu_ref
*ref
)
5019 struct cgroup_subsys_state
*css
=
5020 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
5022 if (atomic_dec_and_test(&css
->online_cnt
)) {
5023 INIT_WORK(&css
->destroy_work
, css_killed_work_fn
);
5024 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
5029 * kill_css - destroy a css
5030 * @css: css to destroy
5032 * This function initiates destruction of @css by removing cgroup interface
5033 * files and putting its base reference. ->css_offline() will be invoked
5034 * asynchronously once css_tryget_online() is guaranteed to fail and when
5035 * the reference count reaches zero, @css will be released.
5037 static void kill_css(struct cgroup_subsys_state
*css
)
5039 lockdep_assert_held(&cgroup_mutex
);
5041 if (css
->flags
& CSS_DYING
)
5044 css
->flags
|= CSS_DYING
;
5047 * This must happen before css is disassociated with its cgroup.
5048 * See seq_css() for details.
5053 * Killing would put the base ref, but we need to keep it alive
5054 * until after ->css_offline().
5059 * cgroup core guarantees that, by the time ->css_offline() is
5060 * invoked, no new css reference will be given out via
5061 * css_tryget_online(). We can't simply call percpu_ref_kill() and
5062 * proceed to offlining css's because percpu_ref_kill() doesn't
5063 * guarantee that the ref is seen as killed on all CPUs on return.
5065 * Use percpu_ref_kill_and_confirm() to get notifications as each
5066 * css is confirmed to be seen as killed on all CPUs.
5068 percpu_ref_kill_and_confirm(&css
->refcnt
, css_killed_ref_fn
);
5072 * cgroup_destroy_locked - the first stage of cgroup destruction
5073 * @cgrp: cgroup to be destroyed
5075 * css's make use of percpu refcnts whose killing latency shouldn't be
5076 * exposed to userland and are RCU protected. Also, cgroup core needs to
5077 * guarantee that css_tryget_online() won't succeed by the time
5078 * ->css_offline() is invoked. To satisfy all the requirements,
5079 * destruction is implemented in the following two steps.
5081 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5082 * userland visible parts and start killing the percpu refcnts of
5083 * css's. Set up so that the next stage will be kicked off once all
5084 * the percpu refcnts are confirmed to be killed.
5086 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5087 * rest of destruction. Once all cgroup references are gone, the
5088 * cgroup is RCU-freed.
5090 * This function implements s1. After this step, @cgrp is gone as far as
5091 * the userland is concerned and a new cgroup with the same name may be
5092 * created. As cgroup doesn't care about the names internally, this
5093 * doesn't cause any problem.
5095 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
5096 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
5098 struct cgroup
*tcgrp
, *parent
= cgroup_parent(cgrp
);
5099 struct cgroup_subsys_state
*css
;
5100 struct cgrp_cset_link
*link
;
5103 lockdep_assert_held(&cgroup_mutex
);
5106 * Only migration can raise populated from zero and we're already
5107 * holding cgroup_mutex.
5109 if (cgroup_is_populated(cgrp
))
5113 * Make sure there's no live children. We can't test emptiness of
5114 * ->self.children as dead children linger on it while being
5115 * drained; otherwise, "rmdir parent/child parent" may fail.
5117 if (css_has_online_children(&cgrp
->self
))
5121 * Mark @cgrp and the associated csets dead. The former prevents
5122 * further task migration and child creation by disabling
5123 * cgroup_lock_live_group(). The latter makes the csets ignored by
5124 * the migration path.
5126 cgrp
->self
.flags
&= ~CSS_ONLINE
;
5128 spin_lock_irq(&css_set_lock
);
5129 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
5130 link
->cset
->dead
= true;
5131 spin_unlock_irq(&css_set_lock
);
5133 /* initiate massacre of all css's */
5134 for_each_css(css
, ssid
, cgrp
)
5138 * Remove @cgrp directory along with the base files. @cgrp has an
5139 * extra ref on its kn.
5141 kernfs_remove(cgrp
->kn
);
5143 if (parent
&& cgroup_is_threaded(cgrp
))
5144 parent
->nr_threaded_children
--;
5146 for (tcgrp
= cgroup_parent(cgrp
); tcgrp
; tcgrp
= cgroup_parent(tcgrp
)) {
5147 tcgrp
->nr_descendants
--;
5148 tcgrp
->nr_dying_descendants
++;
5151 cgroup1_check_for_release(parent
);
5153 /* put the base reference */
5154 percpu_ref_kill(&cgrp
->self
.refcnt
);
5159 int cgroup_rmdir(struct kernfs_node
*kn
)
5161 struct cgroup
*cgrp
;
5164 cgrp
= cgroup_kn_lock_live(kn
, false);
5168 ret
= cgroup_destroy_locked(cgrp
);
5171 trace_cgroup_rmdir(cgrp
);
5173 cgroup_kn_unlock(kn
);
5177 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
= {
5178 .show_options
= cgroup_show_options
,
5179 .remount_fs
= cgroup_remount
,
5180 .mkdir
= cgroup_mkdir
,
5181 .rmdir
= cgroup_rmdir
,
5182 .show_path
= cgroup_show_path
,
5185 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
, bool early
)
5187 struct cgroup_subsys_state
*css
;
5189 pr_debug("Initializing cgroup subsys %s\n", ss
->name
);
5191 mutex_lock(&cgroup_mutex
);
5193 idr_init(&ss
->css_idr
);
5194 INIT_LIST_HEAD(&ss
->cfts
);
5196 /* Create the root cgroup state for this subsystem */
5197 ss
->root
= &cgrp_dfl_root
;
5198 css
= ss
->css_alloc(cgroup_css(&cgrp_dfl_root
.cgrp
, ss
));
5199 /* We don't handle early failures gracefully */
5200 BUG_ON(IS_ERR(css
));
5201 init_and_link_css(css
, ss
, &cgrp_dfl_root
.cgrp
);
5204 * Root csses are never destroyed and we can't initialize
5205 * percpu_ref during early init. Disable refcnting.
5207 css
->flags
|= CSS_NO_REF
;
5210 /* allocation can't be done safely during early init */
5213 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2, GFP_KERNEL
);
5214 BUG_ON(css
->id
< 0);
5217 /* Update the init_css_set to contain a subsys
5218 * pointer to this state - since the subsystem is
5219 * newly registered, all tasks and hence the
5220 * init_css_set is in the subsystem's root cgroup. */
5221 init_css_set
.subsys
[ss
->id
] = css
;
5223 have_fork_callback
|= (bool)ss
->fork
<< ss
->id
;
5224 have_exit_callback
|= (bool)ss
->exit
<< ss
->id
;
5225 have_release_callback
|= (bool)ss
->release
<< ss
->id
;
5226 have_canfork_callback
|= (bool)ss
->can_fork
<< ss
->id
;
5228 /* At system boot, before all subsystems have been
5229 * registered, no tasks have been forked, so we don't
5230 * need to invoke fork callbacks here. */
5231 BUG_ON(!list_empty(&init_task
.tasks
));
5233 BUG_ON(online_css(css
));
5235 mutex_unlock(&cgroup_mutex
);
5239 * cgroup_init_early - cgroup initialization at system boot
5241 * Initialize cgroups at system boot, and initialize any
5242 * subsystems that request early init.
5244 int __init
cgroup_init_early(void)
5246 static struct cgroup_sb_opts __initdata opts
;
5247 struct cgroup_subsys
*ss
;
5250 init_cgroup_root(&cgrp_dfl_root
, &opts
);
5251 cgrp_dfl_root
.cgrp
.self
.flags
|= CSS_NO_REF
;
5253 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
5255 for_each_subsys(ss
, i
) {
5256 WARN(!ss
->css_alloc
|| !ss
->css_free
|| ss
->name
|| ss
->id
,
5257 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
5258 i
, cgroup_subsys_name
[i
], ss
->css_alloc
, ss
->css_free
,
5260 WARN(strlen(cgroup_subsys_name
[i
]) > MAX_CGROUP_TYPE_NAMELEN
,
5261 "cgroup_subsys_name %s too long\n", cgroup_subsys_name
[i
]);
5264 ss
->name
= cgroup_subsys_name
[i
];
5265 if (!ss
->legacy_name
)
5266 ss
->legacy_name
= cgroup_subsys_name
[i
];
5269 cgroup_init_subsys(ss
, true);
5274 static u16 cgroup_disable_mask __initdata
;
5277 * cgroup_init - cgroup initialization
5279 * Register cgroup filesystem and /proc file, and initialize
5280 * any subsystems that didn't request early init.
5282 int __init
cgroup_init(void)
5284 struct cgroup_subsys
*ss
;
5287 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT
> 16);
5288 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem
));
5289 BUG_ON(cgroup_init_cftypes(NULL
, cgroup_base_files
));
5290 BUG_ON(cgroup_init_cftypes(NULL
, cgroup1_base_files
));
5293 * The latency of the synchronize_sched() is too high for cgroups,
5294 * avoid it at the cost of forcing all readers into the slow path.
5296 rcu_sync_enter_start(&cgroup_threadgroup_rwsem
.rss
);
5298 get_user_ns(init_cgroup_ns
.user_ns
);
5300 mutex_lock(&cgroup_mutex
);
5303 * Add init_css_set to the hash table so that dfl_root can link to
5306 hash_add(css_set_table
, &init_css_set
.hlist
,
5307 css_set_hash(init_css_set
.subsys
));
5309 BUG_ON(cgroup_setup_root(&cgrp_dfl_root
, 0, 0));
5311 mutex_unlock(&cgroup_mutex
);
5313 for_each_subsys(ss
, ssid
) {
5314 if (ss
->early_init
) {
5315 struct cgroup_subsys_state
*css
=
5316 init_css_set
.subsys
[ss
->id
];
5318 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2,
5320 BUG_ON(css
->id
< 0);
5322 cgroup_init_subsys(ss
, false);
5325 list_add_tail(&init_css_set
.e_cset_node
[ssid
],
5326 &cgrp_dfl_root
.cgrp
.e_csets
[ssid
]);
5329 * Setting dfl_root subsys_mask needs to consider the
5330 * disabled flag and cftype registration needs kmalloc,
5331 * both of which aren't available during early_init.
5333 if (cgroup_disable_mask
& (1 << ssid
)) {
5334 static_branch_disable(cgroup_subsys_enabled_key
[ssid
]);
5335 printk(KERN_INFO
"Disabling %s control group subsystem\n",
5340 if (cgroup1_ssid_disabled(ssid
))
5341 printk(KERN_INFO
"Disabling %s control group subsystem in v1 mounts\n",
5344 cgrp_dfl_root
.subsys_mask
|= 1 << ss
->id
;
5346 /* implicit controllers must be threaded too */
5347 WARN_ON(ss
->implicit_on_dfl
&& !ss
->threaded
);
5349 if (ss
->implicit_on_dfl
)
5350 cgrp_dfl_implicit_ss_mask
|= 1 << ss
->id
;
5351 else if (!ss
->dfl_cftypes
)
5352 cgrp_dfl_inhibit_ss_mask
|= 1 << ss
->id
;
5355 cgrp_dfl_threaded_ss_mask
|= 1 << ss
->id
;
5357 if (ss
->dfl_cftypes
== ss
->legacy_cftypes
) {
5358 WARN_ON(cgroup_add_cftypes(ss
, ss
->dfl_cftypes
));
5360 WARN_ON(cgroup_add_dfl_cftypes(ss
, ss
->dfl_cftypes
));
5361 WARN_ON(cgroup_add_legacy_cftypes(ss
, ss
->legacy_cftypes
));
5365 ss
->bind(init_css_set
.subsys
[ssid
]);
5367 mutex_lock(&cgroup_mutex
);
5368 css_populate_dir(init_css_set
.subsys
[ssid
]);
5369 mutex_unlock(&cgroup_mutex
);
5372 /* init_css_set.subsys[] has been updated, re-hash */
5373 hash_del(&init_css_set
.hlist
);
5374 hash_add(css_set_table
, &init_css_set
.hlist
,
5375 css_set_hash(init_css_set
.subsys
));
5377 WARN_ON(sysfs_create_mount_point(fs_kobj
, "cgroup"));
5378 WARN_ON(register_filesystem(&cgroup_fs_type
));
5379 WARN_ON(register_filesystem(&cgroup2_fs_type
));
5380 WARN_ON(!proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
));
5385 static int __init
cgroup_wq_init(void)
5388 * There isn't much point in executing destruction path in
5389 * parallel. Good chunk is serialized with cgroup_mutex anyway.
5390 * Use 1 for @max_active.
5392 * We would prefer to do this in cgroup_init() above, but that
5393 * is called before init_workqueues(): so leave this until after.
5395 cgroup_destroy_wq
= alloc_workqueue("cgroup_destroy", 0, 1);
5396 BUG_ON(!cgroup_destroy_wq
);
5399 core_initcall(cgroup_wq_init
);
5401 void cgroup_path_from_kernfs_id(const union kernfs_node_id
*id
,
5402 char *buf
, size_t buflen
)
5404 struct kernfs_node
*kn
;
5406 kn
= kernfs_get_node_by_id(cgrp_dfl_root
.kf_root
, id
);
5409 kernfs_path(kn
, buf
, buflen
);
5414 * proc_cgroup_show()
5415 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5416 * - Used for /proc/<pid>/cgroup.
5418 int proc_cgroup_show(struct seq_file
*m
, struct pid_namespace
*ns
,
5419 struct pid
*pid
, struct task_struct
*tsk
)
5423 struct cgroup_root
*root
;
5426 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
5430 mutex_lock(&cgroup_mutex
);
5431 spin_lock_irq(&css_set_lock
);
5433 for_each_root(root
) {
5434 struct cgroup_subsys
*ss
;
5435 struct cgroup
*cgrp
;
5436 int ssid
, count
= 0;
5438 if (root
== &cgrp_dfl_root
&& !cgrp_dfl_visible
)
5441 seq_printf(m
, "%d:", root
->hierarchy_id
);
5442 if (root
!= &cgrp_dfl_root
)
5443 for_each_subsys(ss
, ssid
)
5444 if (root
->subsys_mask
& (1 << ssid
))
5445 seq_printf(m
, "%s%s", count
++ ? "," : "",
5447 if (strlen(root
->name
))
5448 seq_printf(m
, "%sname=%s", count
? "," : "",
5452 cgrp
= task_cgroup_from_root(tsk
, root
);
5455 * On traditional hierarchies, all zombie tasks show up as
5456 * belonging to the root cgroup. On the default hierarchy,
5457 * while a zombie doesn't show up in "cgroup.procs" and
5458 * thus can't be migrated, its /proc/PID/cgroup keeps
5459 * reporting the cgroup it belonged to before exiting. If
5460 * the cgroup is removed before the zombie is reaped,
5461 * " (deleted)" is appended to the cgroup path.
5463 if (cgroup_on_dfl(cgrp
) || !(tsk
->flags
& PF_EXITING
)) {
5464 retval
= cgroup_path_ns_locked(cgrp
, buf
, PATH_MAX
,
5465 current
->nsproxy
->cgroup_ns
);
5466 if (retval
>= PATH_MAX
)
5467 retval
= -ENAMETOOLONG
;
5476 if (cgroup_on_dfl(cgrp
) && cgroup_is_dead(cgrp
))
5477 seq_puts(m
, " (deleted)\n");
5484 spin_unlock_irq(&css_set_lock
);
5485 mutex_unlock(&cgroup_mutex
);
5492 * cgroup_fork - initialize cgroup related fields during copy_process()
5493 * @child: pointer to task_struct of forking parent process.
5495 * A task is associated with the init_css_set until cgroup_post_fork()
5496 * attaches it to the parent's css_set. Empty cg_list indicates that
5497 * @child isn't holding reference to its css_set.
5499 void cgroup_fork(struct task_struct
*child
)
5501 RCU_INIT_POINTER(child
->cgroups
, &init_css_set
);
5502 INIT_LIST_HEAD(&child
->cg_list
);
5506 * cgroup_can_fork - called on a new task before the process is exposed
5507 * @child: the task in question.
5509 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5510 * returns an error, the fork aborts with that error code. This allows for
5511 * a cgroup subsystem to conditionally allow or deny new forks.
5513 int cgroup_can_fork(struct task_struct
*child
)
5515 struct cgroup_subsys
*ss
;
5518 do_each_subsys_mask(ss
, i
, have_canfork_callback
) {
5519 ret
= ss
->can_fork(child
);
5522 } while_each_subsys_mask();
5527 for_each_subsys(ss
, j
) {
5530 if (ss
->cancel_fork
)
5531 ss
->cancel_fork(child
);
5538 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5539 * @child: the task in question
5541 * This calls the cancel_fork() callbacks if a fork failed *after*
5542 * cgroup_can_fork() succeded.
5544 void cgroup_cancel_fork(struct task_struct
*child
)
5546 struct cgroup_subsys
*ss
;
5549 for_each_subsys(ss
, i
)
5550 if (ss
->cancel_fork
)
5551 ss
->cancel_fork(child
);
5555 * cgroup_post_fork - called on a new task after adding it to the task list
5556 * @child: the task in question
5558 * Adds the task to the list running through its css_set if necessary and
5559 * call the subsystem fork() callbacks. Has to be after the task is
5560 * visible on the task list in case we race with the first call to
5561 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5564 void cgroup_post_fork(struct task_struct
*child
)
5566 struct cgroup_subsys
*ss
;
5570 * This may race against cgroup_enable_task_cg_lists(). As that
5571 * function sets use_task_css_set_links before grabbing
5572 * tasklist_lock and we just went through tasklist_lock to add
5573 * @child, it's guaranteed that either we see the set
5574 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5575 * @child during its iteration.
5577 * If we won the race, @child is associated with %current's
5578 * css_set. Grabbing css_set_lock guarantees both that the
5579 * association is stable, and, on completion of the parent's
5580 * migration, @child is visible in the source of migration or
5581 * already in the destination cgroup. This guarantee is necessary
5582 * when implementing operations which need to migrate all tasks of
5583 * a cgroup to another.
5585 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
5586 * will remain in init_css_set. This is safe because all tasks are
5587 * in the init_css_set before cg_links is enabled and there's no
5588 * operation which transfers all tasks out of init_css_set.
5590 if (use_task_css_set_links
) {
5591 struct css_set
*cset
;
5593 spin_lock_irq(&css_set_lock
);
5594 cset
= task_css_set(current
);
5595 if (list_empty(&child
->cg_list
)) {
5598 css_set_move_task(child
, NULL
, cset
, false);
5600 spin_unlock_irq(&css_set_lock
);
5604 * Call ss->fork(). This must happen after @child is linked on
5605 * css_set; otherwise, @child might change state between ->fork()
5606 * and addition to css_set.
5608 do_each_subsys_mask(ss
, i
, have_fork_callback
) {
5610 } while_each_subsys_mask();
5614 * cgroup_exit - detach cgroup from exiting task
5615 * @tsk: pointer to task_struct of exiting process
5617 * Description: Detach cgroup from @tsk and release it.
5619 * Note that cgroups marked notify_on_release force every task in
5620 * them to take the global cgroup_mutex mutex when exiting.
5621 * This could impact scaling on very large systems. Be reluctant to
5622 * use notify_on_release cgroups where very high task exit scaling
5623 * is required on large systems.
5625 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5626 * call cgroup_exit() while the task is still competent to handle
5627 * notify_on_release(), then leave the task attached to the root cgroup in
5628 * each hierarchy for the remainder of its exit. No need to bother with
5629 * init_css_set refcnting. init_css_set never goes away and we can't race
5630 * with migration path - PF_EXITING is visible to migration path.
5632 void cgroup_exit(struct task_struct
*tsk
)
5634 struct cgroup_subsys
*ss
;
5635 struct css_set
*cset
;
5639 * Unlink from @tsk from its css_set. As migration path can't race
5640 * with us, we can check css_set and cg_list without synchronization.
5642 cset
= task_css_set(tsk
);
5644 if (!list_empty(&tsk
->cg_list
)) {
5645 spin_lock_irq(&css_set_lock
);
5646 css_set_move_task(tsk
, cset
, NULL
, false);
5648 spin_unlock_irq(&css_set_lock
);
5653 /* see cgroup_post_fork() for details */
5654 do_each_subsys_mask(ss
, i
, have_exit_callback
) {
5656 } while_each_subsys_mask();
5659 void cgroup_release(struct task_struct
*task
)
5661 struct cgroup_subsys
*ss
;
5664 do_each_subsys_mask(ss
, ssid
, have_release_callback
) {
5666 } while_each_subsys_mask();
5669 void cgroup_free(struct task_struct
*task
)
5671 struct css_set
*cset
= task_css_set(task
);
5675 static int __init
cgroup_disable(char *str
)
5677 struct cgroup_subsys
*ss
;
5681 while ((token
= strsep(&str
, ",")) != NULL
) {
5685 for_each_subsys(ss
, i
) {
5686 if (strcmp(token
, ss
->name
) &&
5687 strcmp(token
, ss
->legacy_name
))
5689 cgroup_disable_mask
|= 1 << i
;
5694 __setup("cgroup_disable=", cgroup_disable
);
5697 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
5698 * @dentry: directory dentry of interest
5699 * @ss: subsystem of interest
5701 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5702 * to get the corresponding css and return it. If such css doesn't exist
5703 * or can't be pinned, an ERR_PTR value is returned.
5705 struct cgroup_subsys_state
*css_tryget_online_from_dir(struct dentry
*dentry
,
5706 struct cgroup_subsys
*ss
)
5708 struct kernfs_node
*kn
= kernfs_node_from_dentry(dentry
);
5709 struct file_system_type
*s_type
= dentry
->d_sb
->s_type
;
5710 struct cgroup_subsys_state
*css
= NULL
;
5711 struct cgroup
*cgrp
;
5713 /* is @dentry a cgroup dir? */
5714 if ((s_type
!= &cgroup_fs_type
&& s_type
!= &cgroup2_fs_type
) ||
5715 !kn
|| kernfs_type(kn
) != KERNFS_DIR
)
5716 return ERR_PTR(-EBADF
);
5721 * This path doesn't originate from kernfs and @kn could already
5722 * have been or be removed at any point. @kn->priv is RCU
5723 * protected for this access. See css_release_work_fn() for details.
5725 cgrp
= rcu_dereference(*(void __rcu __force
**)&kn
->priv
);
5727 css
= cgroup_css(cgrp
, ss
);
5729 if (!css
|| !css_tryget_online(css
))
5730 css
= ERR_PTR(-ENOENT
);
5737 * css_from_id - lookup css by id
5738 * @id: the cgroup id
5739 * @ss: cgroup subsys to be looked into
5741 * Returns the css if there's valid one with @id, otherwise returns NULL.
5742 * Should be called under rcu_read_lock().
5744 struct cgroup_subsys_state
*css_from_id(int id
, struct cgroup_subsys
*ss
)
5746 WARN_ON_ONCE(!rcu_read_lock_held());
5747 return idr_find(&ss
->css_idr
, id
);
5751 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5752 * @path: path on the default hierarchy
5754 * Find the cgroup at @path on the default hierarchy, increment its
5755 * reference count and return it. Returns pointer to the found cgroup on
5756 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5757 * if @path points to a non-directory.
5759 struct cgroup
*cgroup_get_from_path(const char *path
)
5761 struct kernfs_node
*kn
;
5762 struct cgroup
*cgrp
;
5764 mutex_lock(&cgroup_mutex
);
5766 kn
= kernfs_walk_and_get(cgrp_dfl_root
.cgrp
.kn
, path
);
5768 if (kernfs_type(kn
) == KERNFS_DIR
) {
5770 cgroup_get_live(cgrp
);
5772 cgrp
= ERR_PTR(-ENOTDIR
);
5776 cgrp
= ERR_PTR(-ENOENT
);
5779 mutex_unlock(&cgroup_mutex
);
5782 EXPORT_SYMBOL_GPL(cgroup_get_from_path
);
5785 * cgroup_get_from_fd - get a cgroup pointer from a fd
5786 * @fd: fd obtained by open(cgroup2_dir)
5788 * Find the cgroup from a fd which should be obtained
5789 * by opening a cgroup directory. Returns a pointer to the
5790 * cgroup on success. ERR_PTR is returned if the cgroup
5793 struct cgroup
*cgroup_get_from_fd(int fd
)
5795 struct cgroup_subsys_state
*css
;
5796 struct cgroup
*cgrp
;
5801 return ERR_PTR(-EBADF
);
5803 css
= css_tryget_online_from_dir(f
->f_path
.dentry
, NULL
);
5806 return ERR_CAST(css
);
5809 if (!cgroup_on_dfl(cgrp
)) {
5811 return ERR_PTR(-EBADF
);
5816 EXPORT_SYMBOL_GPL(cgroup_get_from_fd
);
5819 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5820 * definition in cgroup-defs.h.
5822 #ifdef CONFIG_SOCK_CGROUP_DATA
5824 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5826 DEFINE_SPINLOCK(cgroup_sk_update_lock
);
5827 static bool cgroup_sk_alloc_disabled __read_mostly
;
5829 void cgroup_sk_alloc_disable(void)
5831 if (cgroup_sk_alloc_disabled
)
5833 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5834 cgroup_sk_alloc_disabled
= true;
5839 #define cgroup_sk_alloc_disabled false
5843 void cgroup_sk_alloc(struct sock_cgroup_data
*skcd
)
5845 if (cgroup_sk_alloc_disabled
)
5848 /* Socket clone path */
5851 * We might be cloning a socket which is left in an empty
5852 * cgroup and the cgroup might have already been rmdir'd.
5853 * Don't use cgroup_get_live().
5855 cgroup_get(sock_cgroup_ptr(skcd
));
5862 struct css_set
*cset
;
5864 cset
= task_css_set(current
);
5865 if (likely(cgroup_tryget(cset
->dfl_cgrp
))) {
5866 skcd
->val
= (unsigned long)cset
->dfl_cgrp
;
5875 void cgroup_sk_free(struct sock_cgroup_data
*skcd
)
5877 cgroup_put(sock_cgroup_ptr(skcd
));
5880 #endif /* CONFIG_SOCK_CGROUP_DATA */
5882 #ifdef CONFIG_CGROUP_BPF
5883 int cgroup_bpf_update(struct cgroup
*cgrp
, struct bpf_prog
*prog
,
5884 enum bpf_attach_type type
, bool overridable
)
5886 struct cgroup
*parent
= cgroup_parent(cgrp
);
5889 mutex_lock(&cgroup_mutex
);
5890 ret
= __cgroup_bpf_update(cgrp
, parent
, prog
, type
, overridable
);
5891 mutex_unlock(&cgroup_mutex
);
5894 #endif /* CONFIG_CGROUP_BPF */