2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
11 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include "cgroup-internal.h"
33 #include <linux/cred.h>
34 #include <linux/errno.h>
35 #include <linux/init_task.h>
36 #include <linux/kernel.h>
37 #include <linux/magic.h>
38 #include <linux/mutex.h>
39 #include <linux/mount.h>
40 #include <linux/pagemap.h>
41 #include <linux/proc_fs.h>
42 #include <linux/rcupdate.h>
43 #include <linux/sched.h>
44 #include <linux/sched/task.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/percpu-rwsem.h>
48 #include <linux/string.h>
49 #include <linux/hashtable.h>
50 #include <linux/idr.h>
51 #include <linux/kthread.h>
52 #include <linux/atomic.h>
53 #include <linux/cpuset.h>
54 #include <linux/proc_ns.h>
55 #include <linux/nsproxy.h>
56 #include <linux/file.h>
57 #include <linux/sched/cputime.h>
58 #include <linux/psi.h>
61 #define CREATE_TRACE_POINTS
62 #include <trace/events/cgroup.h>
64 #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
66 /* let's not notify more than 100 times per second */
67 #define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100)
70 * cgroup_mutex is the master lock. Any modification to cgroup or its
71 * hierarchy must be performed while holding it.
73 * css_set_lock protects task->cgroups pointer, the list of css_set
74 * objects, and the chain of tasks off each css_set.
76 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
77 * cgroup.h can use them for lockdep annotations.
79 DEFINE_MUTEX(cgroup_mutex
);
80 DEFINE_SPINLOCK(css_set_lock
);
82 #ifdef CONFIG_PROVE_RCU
83 EXPORT_SYMBOL_GPL(cgroup_mutex
);
84 EXPORT_SYMBOL_GPL(css_set_lock
);
87 DEFINE_SPINLOCK(trace_cgroup_path_lock
);
88 char trace_cgroup_path
[TRACE_CGROUP_PATH_LEN
];
91 * Protects cgroup_idr and css_idr so that IDs can be released without
92 * grabbing cgroup_mutex.
94 static DEFINE_SPINLOCK(cgroup_idr_lock
);
97 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
98 * against file removal/re-creation across css hiding.
100 static DEFINE_SPINLOCK(cgroup_file_kn_lock
);
102 struct percpu_rw_semaphore cgroup_threadgroup_rwsem
;
104 #define cgroup_assert_mutex_or_rcu_locked() \
105 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
106 !lockdep_is_held(&cgroup_mutex), \
107 "cgroup_mutex or RCU read lock required");
110 * cgroup destruction makes heavy use of work items and there can be a lot
111 * of concurrent destructions. Use a separate workqueue so that cgroup
112 * destruction work items don't end up filling up max_active of system_wq
113 * which may lead to deadlock.
115 static struct workqueue_struct
*cgroup_destroy_wq
;
117 /* generate an array of cgroup subsystem pointers */
118 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
119 struct cgroup_subsys
*cgroup_subsys
[] = {
120 #include <linux/cgroup_subsys.h>
124 /* array of cgroup subsystem names */
125 #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
126 static const char *cgroup_subsys_name
[] = {
127 #include <linux/cgroup_subsys.h>
131 /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
133 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
134 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
135 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
136 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
137 #include <linux/cgroup_subsys.h>
140 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
141 static struct static_key_true
*cgroup_subsys_enabled_key
[] = {
142 #include <linux/cgroup_subsys.h>
146 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
147 static struct static_key_true
*cgroup_subsys_on_dfl_key
[] = {
148 #include <linux/cgroup_subsys.h>
152 static DEFINE_PER_CPU(struct cgroup_rstat_cpu
, cgrp_dfl_root_rstat_cpu
);
155 * The default hierarchy, reserved for the subsystems that are otherwise
156 * unattached - it never has more than a single cgroup, and all tasks are
157 * part of that cgroup.
159 struct cgroup_root cgrp_dfl_root
= { .cgrp
.rstat_cpu
= &cgrp_dfl_root_rstat_cpu
};
160 EXPORT_SYMBOL_GPL(cgrp_dfl_root
);
163 * The default hierarchy always exists but is hidden until mounted for the
164 * first time. This is for backward compatibility.
166 static bool cgrp_dfl_visible
;
168 /* some controllers are not supported in the default hierarchy */
169 static u16 cgrp_dfl_inhibit_ss_mask
;
171 /* some controllers are implicitly enabled on the default hierarchy */
172 static u16 cgrp_dfl_implicit_ss_mask
;
174 /* some controllers can be threaded on the default hierarchy */
175 static u16 cgrp_dfl_threaded_ss_mask
;
177 /* The list of hierarchy roots */
178 LIST_HEAD(cgroup_roots
);
179 static int cgroup_root_count
;
181 /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
182 static DEFINE_IDR(cgroup_hierarchy_idr
);
185 * Assign a monotonically increasing serial number to csses. It guarantees
186 * cgroups with bigger numbers are newer than those with smaller numbers.
187 * Also, as csses are always appended to the parent's ->children list, it
188 * guarantees that sibling csses are always sorted in the ascending serial
189 * number order on the list. Protected by cgroup_mutex.
191 static u64 css_serial_nr_next
= 1;
194 * These bitmasks identify subsystems with specific features to avoid
195 * having to do iterative checks repeatedly.
197 static u16 have_fork_callback __read_mostly
;
198 static u16 have_exit_callback __read_mostly
;
199 static u16 have_free_callback __read_mostly
;
200 static u16 have_canfork_callback __read_mostly
;
202 /* cgroup namespace for init task */
203 struct cgroup_namespace init_cgroup_ns
= {
204 .count
= REFCOUNT_INIT(2),
205 .user_ns
= &init_user_ns
,
206 .ns
.ops
= &cgroupns_operations
,
207 .ns
.inum
= PROC_CGROUP_INIT_INO
,
208 .root_cset
= &init_css_set
,
211 static struct file_system_type cgroup2_fs_type
;
212 static struct cftype cgroup_base_files
[];
214 static int cgroup_apply_control(struct cgroup
*cgrp
);
215 static void cgroup_finalize_control(struct cgroup
*cgrp
, int ret
);
216 static void css_task_iter_advance(struct css_task_iter
*it
);
217 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
218 static struct cgroup_subsys_state
*css_create(struct cgroup
*cgrp
,
219 struct cgroup_subsys
*ss
);
220 static void css_release(struct percpu_ref
*ref
);
221 static void kill_css(struct cgroup_subsys_state
*css
);
222 static int cgroup_addrm_files(struct cgroup_subsys_state
*css
,
223 struct cgroup
*cgrp
, struct cftype cfts
[],
227 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
228 * @ssid: subsys ID of interest
230 * cgroup_subsys_enabled() can only be used with literal subsys names which
231 * is fine for individual subsystems but unsuitable for cgroup core. This
232 * is slower static_key_enabled() based test indexed by @ssid.
234 bool cgroup_ssid_enabled(int ssid
)
236 if (CGROUP_SUBSYS_COUNT
== 0)
239 return static_key_enabled(cgroup_subsys_enabled_key
[ssid
]);
243 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
244 * @cgrp: the cgroup of interest
246 * The default hierarchy is the v2 interface of cgroup and this function
247 * can be used to test whether a cgroup is on the default hierarchy for
248 * cases where a subsystem should behave differnetly depending on the
251 * The set of behaviors which change on the default hierarchy are still
252 * being determined and the mount option is prefixed with __DEVEL__.
254 * List of changed behaviors:
256 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
257 * and "name" are disallowed.
259 * - When mounting an existing superblock, mount options should match.
261 * - Remount is disallowed.
263 * - rename(2) is disallowed.
265 * - "tasks" is removed. Everything should be at process granularity. Use
266 * "cgroup.procs" instead.
268 * - "cgroup.procs" is not sorted. pids will be unique unless they got
269 * recycled inbetween reads.
271 * - "release_agent" and "notify_on_release" are removed. Replacement
272 * notification mechanism will be implemented.
274 * - "cgroup.clone_children" is removed.
276 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
277 * and its descendants contain no task; otherwise, 1. The file also
278 * generates kernfs notification which can be monitored through poll and
279 * [di]notify when the value of the file changes.
281 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
282 * take masks of ancestors with non-empty cpus/mems, instead of being
283 * moved to an ancestor.
285 * - cpuset: a task can be moved into an empty cpuset, and again it takes
286 * masks of ancestors.
288 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
291 * - blkcg: blk-throttle becomes properly hierarchical.
293 * - debug: disallowed on the default hierarchy.
295 bool cgroup_on_dfl(const struct cgroup
*cgrp
)
297 return cgrp
->root
== &cgrp_dfl_root
;
300 /* IDR wrappers which synchronize using cgroup_idr_lock */
301 static int cgroup_idr_alloc(struct idr
*idr
, void *ptr
, int start
, int end
,
306 idr_preload(gfp_mask
);
307 spin_lock_bh(&cgroup_idr_lock
);
308 ret
= idr_alloc(idr
, ptr
, start
, end
, gfp_mask
& ~__GFP_DIRECT_RECLAIM
);
309 spin_unlock_bh(&cgroup_idr_lock
);
314 static void *cgroup_idr_replace(struct idr
*idr
, void *ptr
, int id
)
318 spin_lock_bh(&cgroup_idr_lock
);
319 ret
= idr_replace(idr
, ptr
, id
);
320 spin_unlock_bh(&cgroup_idr_lock
);
324 static void cgroup_idr_remove(struct idr
*idr
, int id
)
326 spin_lock_bh(&cgroup_idr_lock
);
328 spin_unlock_bh(&cgroup_idr_lock
);
331 static bool cgroup_has_tasks(struct cgroup
*cgrp
)
333 return cgrp
->nr_populated_csets
;
336 bool cgroup_is_threaded(struct cgroup
*cgrp
)
338 return cgrp
->dom_cgrp
!= cgrp
;
341 /* can @cgrp host both domain and threaded children? */
342 static bool cgroup_is_mixable(struct cgroup
*cgrp
)
345 * Root isn't under domain level resource control exempting it from
346 * the no-internal-process constraint, so it can serve as a thread
347 * root and a parent of resource domains at the same time.
349 return !cgroup_parent(cgrp
);
352 /* can @cgrp become a thread root? should always be true for a thread root */
353 static bool cgroup_can_be_thread_root(struct cgroup
*cgrp
)
355 /* mixables don't care */
356 if (cgroup_is_mixable(cgrp
))
359 /* domain roots can't be nested under threaded */
360 if (cgroup_is_threaded(cgrp
))
363 /* can only have either domain or threaded children */
364 if (cgrp
->nr_populated_domain_children
)
367 /* and no domain controllers can be enabled */
368 if (cgrp
->subtree_control
& ~cgrp_dfl_threaded_ss_mask
)
374 /* is @cgrp root of a threaded subtree? */
375 bool cgroup_is_thread_root(struct cgroup
*cgrp
)
377 /* thread root should be a domain */
378 if (cgroup_is_threaded(cgrp
))
381 /* a domain w/ threaded children is a thread root */
382 if (cgrp
->nr_threaded_children
)
386 * A domain which has tasks and explicit threaded controllers
387 * enabled is a thread root.
389 if (cgroup_has_tasks(cgrp
) &&
390 (cgrp
->subtree_control
& cgrp_dfl_threaded_ss_mask
))
396 /* a domain which isn't connected to the root w/o brekage can't be used */
397 static bool cgroup_is_valid_domain(struct cgroup
*cgrp
)
399 /* the cgroup itself can be a thread root */
400 if (cgroup_is_threaded(cgrp
))
403 /* but the ancestors can't be unless mixable */
404 while ((cgrp
= cgroup_parent(cgrp
))) {
405 if (!cgroup_is_mixable(cgrp
) && cgroup_is_thread_root(cgrp
))
407 if (cgroup_is_threaded(cgrp
))
414 /* subsystems visibly enabled on a cgroup */
415 static u16
cgroup_control(struct cgroup
*cgrp
)
417 struct cgroup
*parent
= cgroup_parent(cgrp
);
418 u16 root_ss_mask
= cgrp
->root
->subsys_mask
;
421 u16 ss_mask
= parent
->subtree_control
;
423 /* threaded cgroups can only have threaded controllers */
424 if (cgroup_is_threaded(cgrp
))
425 ss_mask
&= cgrp_dfl_threaded_ss_mask
;
429 if (cgroup_on_dfl(cgrp
))
430 root_ss_mask
&= ~(cgrp_dfl_inhibit_ss_mask
|
431 cgrp_dfl_implicit_ss_mask
);
435 /* subsystems enabled on a cgroup */
436 static u16
cgroup_ss_mask(struct cgroup
*cgrp
)
438 struct cgroup
*parent
= cgroup_parent(cgrp
);
441 u16 ss_mask
= parent
->subtree_ss_mask
;
443 /* threaded cgroups can only have threaded controllers */
444 if (cgroup_is_threaded(cgrp
))
445 ss_mask
&= cgrp_dfl_threaded_ss_mask
;
449 return cgrp
->root
->subsys_mask
;
453 * cgroup_css - obtain a cgroup's css for the specified subsystem
454 * @cgrp: the cgroup of interest
455 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
457 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
458 * function must be called either under cgroup_mutex or rcu_read_lock() and
459 * the caller is responsible for pinning the returned css if it wants to
460 * keep accessing it outside the said locks. This function may return
461 * %NULL if @cgrp doesn't have @subsys_id enabled.
463 static struct cgroup_subsys_state
*cgroup_css(struct cgroup
*cgrp
,
464 struct cgroup_subsys
*ss
)
467 return rcu_dereference_check(cgrp
->subsys
[ss
->id
],
468 lockdep_is_held(&cgroup_mutex
));
474 * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem
475 * @cgrp: the cgroup of interest
476 * @ss: the subsystem of interest
478 * Find and get @cgrp's css assocaited with @ss. If the css doesn't exist
479 * or is offline, %NULL is returned.
481 static struct cgroup_subsys_state
*cgroup_tryget_css(struct cgroup
*cgrp
,
482 struct cgroup_subsys
*ss
)
484 struct cgroup_subsys_state
*css
;
487 css
= cgroup_css(cgrp
, ss
);
488 if (!css
|| !css_tryget_online(css
))
496 * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss
497 * @cgrp: the cgroup of interest
498 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
500 * Similar to cgroup_css() but returns the effective css, which is defined
501 * as the matching css of the nearest ancestor including self which has @ss
502 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
503 * function is guaranteed to return non-NULL css.
505 static struct cgroup_subsys_state
*cgroup_e_css_by_mask(struct cgroup
*cgrp
,
506 struct cgroup_subsys
*ss
)
508 lockdep_assert_held(&cgroup_mutex
);
514 * This function is used while updating css associations and thus
515 * can't test the csses directly. Test ss_mask.
517 while (!(cgroup_ss_mask(cgrp
) & (1 << ss
->id
))) {
518 cgrp
= cgroup_parent(cgrp
);
523 return cgroup_css(cgrp
, ss
);
527 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
528 * @cgrp: the cgroup of interest
529 * @ss: the subsystem of interest
531 * Find and get the effective css of @cgrp for @ss. The effective css is
532 * defined as the matching css of the nearest ancestor including self which
533 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
534 * the root css is returned, so this function always returns a valid css.
536 * The returned css is not guaranteed to be online, and therefore it is the
537 * callers responsiblity to tryget a reference for it.
539 struct cgroup_subsys_state
*cgroup_e_css(struct cgroup
*cgrp
,
540 struct cgroup_subsys
*ss
)
542 struct cgroup_subsys_state
*css
;
545 css
= cgroup_css(cgrp
, ss
);
549 cgrp
= cgroup_parent(cgrp
);
552 return init_css_set
.subsys
[ss
->id
];
556 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
557 * @cgrp: the cgroup of interest
558 * @ss: the subsystem of interest
560 * Find and get the effective css of @cgrp for @ss. The effective css is
561 * defined as the matching css of the nearest ancestor including self which
562 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
563 * the root css is returned, so this function always returns a valid css.
564 * The returned css must be put using css_put().
566 struct cgroup_subsys_state
*cgroup_get_e_css(struct cgroup
*cgrp
,
567 struct cgroup_subsys
*ss
)
569 struct cgroup_subsys_state
*css
;
574 css
= cgroup_css(cgrp
, ss
);
576 if (css
&& css_tryget_online(css
))
578 cgrp
= cgroup_parent(cgrp
);
581 css
= init_css_set
.subsys
[ss
->id
];
588 static void cgroup_get_live(struct cgroup
*cgrp
)
590 WARN_ON_ONCE(cgroup_is_dead(cgrp
));
591 css_get(&cgrp
->self
);
594 struct cgroup_subsys_state
*of_css(struct kernfs_open_file
*of
)
596 struct cgroup
*cgrp
= of
->kn
->parent
->priv
;
597 struct cftype
*cft
= of_cft(of
);
600 * This is open and unprotected implementation of cgroup_css().
601 * seq_css() is only called from a kernfs file operation which has
602 * an active reference on the file. Because all the subsystem
603 * files are drained before a css is disassociated with a cgroup,
604 * the matching css from the cgroup's subsys table is guaranteed to
605 * be and stay valid until the enclosing operation is complete.
608 return rcu_dereference_raw(cgrp
->subsys
[cft
->ss
->id
]);
612 EXPORT_SYMBOL_GPL(of_css
);
615 * for_each_css - iterate all css's of a cgroup
616 * @css: the iteration cursor
617 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
618 * @cgrp: the target cgroup to iterate css's of
620 * Should be called under cgroup_[tree_]mutex.
622 #define for_each_css(css, ssid, cgrp) \
623 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
624 if (!((css) = rcu_dereference_check( \
625 (cgrp)->subsys[(ssid)], \
626 lockdep_is_held(&cgroup_mutex)))) { } \
630 * for_each_e_css - iterate all effective css's of a cgroup
631 * @css: the iteration cursor
632 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
633 * @cgrp: the target cgroup to iterate css's of
635 * Should be called under cgroup_[tree_]mutex.
637 #define for_each_e_css(css, ssid, cgrp) \
638 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
639 if (!((css) = cgroup_e_css_by_mask(cgrp, \
640 cgroup_subsys[(ssid)]))) \
645 * do_each_subsys_mask - filter for_each_subsys with a bitmask
646 * @ss: the iteration cursor
647 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
648 * @ss_mask: the bitmask
650 * The block will only run for cases where the ssid-th bit (1 << ssid) of
653 #define do_each_subsys_mask(ss, ssid, ss_mask) do { \
654 unsigned long __ss_mask = (ss_mask); \
655 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
659 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
660 (ss) = cgroup_subsys[ssid]; \
663 #define while_each_subsys_mask() \
668 /* iterate over child cgrps, lock should be held throughout iteration */
669 #define cgroup_for_each_live_child(child, cgrp) \
670 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
671 if (({ lockdep_assert_held(&cgroup_mutex); \
672 cgroup_is_dead(child); })) \
676 /* walk live descendants in preorder */
677 #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
678 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
679 if (({ lockdep_assert_held(&cgroup_mutex); \
680 (dsct) = (d_css)->cgroup; \
681 cgroup_is_dead(dsct); })) \
685 /* walk live descendants in postorder */
686 #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
687 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
688 if (({ lockdep_assert_held(&cgroup_mutex); \
689 (dsct) = (d_css)->cgroup; \
690 cgroup_is_dead(dsct); })) \
695 * The default css_set - used by init and its children prior to any
696 * hierarchies being mounted. It contains a pointer to the root state
697 * for each subsystem. Also used to anchor the list of css_sets. Not
698 * reference-counted, to improve performance when child cgroups
699 * haven't been created.
701 struct css_set init_css_set
= {
702 .refcount
= REFCOUNT_INIT(1),
703 .dom_cset
= &init_css_set
,
704 .tasks
= LIST_HEAD_INIT(init_css_set
.tasks
),
705 .mg_tasks
= LIST_HEAD_INIT(init_css_set
.mg_tasks
),
706 .task_iters
= LIST_HEAD_INIT(init_css_set
.task_iters
),
707 .threaded_csets
= LIST_HEAD_INIT(init_css_set
.threaded_csets
),
708 .cgrp_links
= LIST_HEAD_INIT(init_css_set
.cgrp_links
),
709 .mg_preload_node
= LIST_HEAD_INIT(init_css_set
.mg_preload_node
),
710 .mg_node
= LIST_HEAD_INIT(init_css_set
.mg_node
),
713 * The following field is re-initialized when this cset gets linked
714 * in cgroup_init(). However, let's initialize the field
715 * statically too so that the default cgroup can be accessed safely
718 .dfl_cgrp
= &cgrp_dfl_root
.cgrp
,
721 static int css_set_count
= 1; /* 1 for init_css_set */
723 static bool css_set_threaded(struct css_set
*cset
)
725 return cset
->dom_cset
!= cset
;
729 * css_set_populated - does a css_set contain any tasks?
730 * @cset: target css_set
732 * css_set_populated() should be the same as !!cset->nr_tasks at steady
733 * state. However, css_set_populated() can be called while a task is being
734 * added to or removed from the linked list before the nr_tasks is
735 * properly updated. Hence, we can't just look at ->nr_tasks here.
737 static bool css_set_populated(struct css_set
*cset
)
739 lockdep_assert_held(&css_set_lock
);
741 return !list_empty(&cset
->tasks
) || !list_empty(&cset
->mg_tasks
);
745 * cgroup_update_populated - update the populated count of a cgroup
746 * @cgrp: the target cgroup
747 * @populated: inc or dec populated count
749 * One of the css_sets associated with @cgrp is either getting its first
750 * task or losing the last. Update @cgrp->nr_populated_* accordingly. The
751 * count is propagated towards root so that a given cgroup's
752 * nr_populated_children is zero iff none of its descendants contain any
755 * @cgrp's interface file "cgroup.populated" is zero if both
756 * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and
757 * 1 otherwise. When the sum changes from or to zero, userland is notified
758 * that the content of the interface file has changed. This can be used to
759 * detect when @cgrp and its descendants become populated or empty.
761 static void cgroup_update_populated(struct cgroup
*cgrp
, bool populated
)
763 struct cgroup
*child
= NULL
;
764 int adj
= populated
? 1 : -1;
766 lockdep_assert_held(&css_set_lock
);
769 bool was_populated
= cgroup_is_populated(cgrp
);
772 cgrp
->nr_populated_csets
+= adj
;
774 if (cgroup_is_threaded(child
))
775 cgrp
->nr_populated_threaded_children
+= adj
;
777 cgrp
->nr_populated_domain_children
+= adj
;
780 if (was_populated
== cgroup_is_populated(cgrp
))
783 cgroup1_check_for_release(cgrp
);
784 cgroup_file_notify(&cgrp
->events_file
);
787 cgrp
= cgroup_parent(cgrp
);
792 * css_set_update_populated - update populated state of a css_set
793 * @cset: target css_set
794 * @populated: whether @cset is populated or depopulated
796 * @cset is either getting the first task or losing the last. Update the
797 * populated counters of all associated cgroups accordingly.
799 static void css_set_update_populated(struct css_set
*cset
, bool populated
)
801 struct cgrp_cset_link
*link
;
803 lockdep_assert_held(&css_set_lock
);
805 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
)
806 cgroup_update_populated(link
->cgrp
, populated
);
810 * css_set_move_task - move a task from one css_set to another
811 * @task: task being moved
812 * @from_cset: css_set @task currently belongs to (may be NULL)
813 * @to_cset: new css_set @task is being moved to (may be NULL)
814 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
816 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
817 * css_set, @from_cset can be NULL. If @task is being disassociated
818 * instead of moved, @to_cset can be NULL.
820 * This function automatically handles populated counter updates and
821 * css_task_iter adjustments but the caller is responsible for managing
822 * @from_cset and @to_cset's reference counts.
824 static void css_set_move_task(struct task_struct
*task
,
825 struct css_set
*from_cset
, struct css_set
*to_cset
,
828 lockdep_assert_held(&css_set_lock
);
830 if (to_cset
&& !css_set_populated(to_cset
))
831 css_set_update_populated(to_cset
, true);
834 struct css_task_iter
*it
, *pos
;
836 WARN_ON_ONCE(list_empty(&task
->cg_list
));
839 * @task is leaving, advance task iterators which are
840 * pointing to it so that they can resume at the next
841 * position. Advancing an iterator might remove it from
842 * the list, use safe walk. See css_task_iter_advance*()
845 list_for_each_entry_safe(it
, pos
, &from_cset
->task_iters
,
847 if (it
->task_pos
== &task
->cg_list
)
848 css_task_iter_advance(it
);
850 list_del_init(&task
->cg_list
);
851 if (!css_set_populated(from_cset
))
852 css_set_update_populated(from_cset
, false);
854 WARN_ON_ONCE(!list_empty(&task
->cg_list
));
859 * We are synchronized through cgroup_threadgroup_rwsem
860 * against PF_EXITING setting such that we can't race
861 * against cgroup_exit() changing the css_set to
862 * init_css_set and dropping the old one.
864 WARN_ON_ONCE(task
->flags
& PF_EXITING
);
866 cgroup_move_task(task
, to_cset
);
867 list_add_tail(&task
->cg_list
, use_mg_tasks
? &to_cset
->mg_tasks
:
873 * hash table for cgroup groups. This improves the performance to find
874 * an existing css_set. This hash doesn't (currently) take into
875 * account cgroups in empty hierarchies.
877 #define CSS_SET_HASH_BITS 7
878 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
880 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
882 unsigned long key
= 0UL;
883 struct cgroup_subsys
*ss
;
886 for_each_subsys(ss
, i
)
887 key
+= (unsigned long)css
[i
];
888 key
= (key
>> 16) ^ key
;
893 void put_css_set_locked(struct css_set
*cset
)
895 struct cgrp_cset_link
*link
, *tmp_link
;
896 struct cgroup_subsys
*ss
;
899 lockdep_assert_held(&css_set_lock
);
901 if (!refcount_dec_and_test(&cset
->refcount
))
904 WARN_ON_ONCE(!list_empty(&cset
->threaded_csets
));
906 /* This css_set is dead. unlink it and release cgroup and css refs */
907 for_each_subsys(ss
, ssid
) {
908 list_del(&cset
->e_cset_node
[ssid
]);
909 css_put(cset
->subsys
[ssid
]);
911 hash_del(&cset
->hlist
);
914 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
915 list_del(&link
->cset_link
);
916 list_del(&link
->cgrp_link
);
917 if (cgroup_parent(link
->cgrp
))
918 cgroup_put(link
->cgrp
);
922 if (css_set_threaded(cset
)) {
923 list_del(&cset
->threaded_csets_node
);
924 put_css_set_locked(cset
->dom_cset
);
927 kfree_rcu(cset
, rcu_head
);
931 * compare_css_sets - helper function for find_existing_css_set().
932 * @cset: candidate css_set being tested
933 * @old_cset: existing css_set for a task
934 * @new_cgrp: cgroup that's being entered by the task
935 * @template: desired set of css pointers in css_set (pre-calculated)
937 * Returns true if "cset" matches "old_cset" except for the hierarchy
938 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
940 static bool compare_css_sets(struct css_set
*cset
,
941 struct css_set
*old_cset
,
942 struct cgroup
*new_cgrp
,
943 struct cgroup_subsys_state
*template[])
945 struct cgroup
*new_dfl_cgrp
;
946 struct list_head
*l1
, *l2
;
949 * On the default hierarchy, there can be csets which are
950 * associated with the same set of cgroups but different csses.
951 * Let's first ensure that csses match.
953 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
)))
957 /* @cset's domain should match the default cgroup's */
958 if (cgroup_on_dfl(new_cgrp
))
959 new_dfl_cgrp
= new_cgrp
;
961 new_dfl_cgrp
= old_cset
->dfl_cgrp
;
963 if (new_dfl_cgrp
->dom_cgrp
!= cset
->dom_cset
->dfl_cgrp
)
967 * Compare cgroup pointers in order to distinguish between
968 * different cgroups in hierarchies. As different cgroups may
969 * share the same effective css, this comparison is always
972 l1
= &cset
->cgrp_links
;
973 l2
= &old_cset
->cgrp_links
;
975 struct cgrp_cset_link
*link1
, *link2
;
976 struct cgroup
*cgrp1
, *cgrp2
;
980 /* See if we reached the end - both lists are equal length. */
981 if (l1
== &cset
->cgrp_links
) {
982 BUG_ON(l2
!= &old_cset
->cgrp_links
);
985 BUG_ON(l2
== &old_cset
->cgrp_links
);
987 /* Locate the cgroups associated with these links. */
988 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
989 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
992 /* Hierarchies should be linked in the same order. */
993 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
996 * If this hierarchy is the hierarchy of the cgroup
997 * that's changing, then we need to check that this
998 * css_set points to the new cgroup; if it's any other
999 * hierarchy, then this css_set should point to the
1000 * same cgroup as the old css_set.
1002 if (cgrp1
->root
== new_cgrp
->root
) {
1003 if (cgrp1
!= new_cgrp
)
1014 * find_existing_css_set - init css array and find the matching css_set
1015 * @old_cset: the css_set that we're using before the cgroup transition
1016 * @cgrp: the cgroup that we're moving into
1017 * @template: out param for the new set of csses, should be clear on entry
1019 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
1020 struct cgroup
*cgrp
,
1021 struct cgroup_subsys_state
*template[])
1023 struct cgroup_root
*root
= cgrp
->root
;
1024 struct cgroup_subsys
*ss
;
1025 struct css_set
*cset
;
1030 * Build the set of subsystem state objects that we want to see in the
1031 * new css_set. while subsystems can change globally, the entries here
1032 * won't change, so no need for locking.
1034 for_each_subsys(ss
, i
) {
1035 if (root
->subsys_mask
& (1UL << i
)) {
1037 * @ss is in this hierarchy, so we want the
1038 * effective css from @cgrp.
1040 template[i
] = cgroup_e_css_by_mask(cgrp
, ss
);
1043 * @ss is not in this hierarchy, so we don't want
1044 * to change the css.
1046 template[i
] = old_cset
->subsys
[i
];
1050 key
= css_set_hash(template);
1051 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
1052 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
1055 /* This css_set matches what we need */
1059 /* No existing cgroup group matched */
1063 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
1065 struct cgrp_cset_link
*link
, *tmp_link
;
1067 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
1068 list_del(&link
->cset_link
);
1074 * allocate_cgrp_cset_links - allocate cgrp_cset_links
1075 * @count: the number of links to allocate
1076 * @tmp_links: list_head the allocated links are put on
1078 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
1079 * through ->cset_link. Returns 0 on success or -errno.
1081 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
1083 struct cgrp_cset_link
*link
;
1086 INIT_LIST_HEAD(tmp_links
);
1088 for (i
= 0; i
< count
; i
++) {
1089 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
1091 free_cgrp_cset_links(tmp_links
);
1094 list_add(&link
->cset_link
, tmp_links
);
1100 * link_css_set - a helper function to link a css_set to a cgroup
1101 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
1102 * @cset: the css_set to be linked
1103 * @cgrp: the destination cgroup
1105 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
1106 struct cgroup
*cgrp
)
1108 struct cgrp_cset_link
*link
;
1110 BUG_ON(list_empty(tmp_links
));
1112 if (cgroup_on_dfl(cgrp
))
1113 cset
->dfl_cgrp
= cgrp
;
1115 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
1120 * Always add links to the tail of the lists so that the lists are
1121 * in choronological order.
1123 list_move_tail(&link
->cset_link
, &cgrp
->cset_links
);
1124 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
1126 if (cgroup_parent(cgrp
))
1127 cgroup_get_live(cgrp
);
1131 * find_css_set - return a new css_set with one cgroup updated
1132 * @old_cset: the baseline css_set
1133 * @cgrp: the cgroup to be updated
1135 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1136 * substituted into the appropriate hierarchy.
1138 static struct css_set
*find_css_set(struct css_set
*old_cset
,
1139 struct cgroup
*cgrp
)
1141 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
1142 struct css_set
*cset
;
1143 struct list_head tmp_links
;
1144 struct cgrp_cset_link
*link
;
1145 struct cgroup_subsys
*ss
;
1149 lockdep_assert_held(&cgroup_mutex
);
1151 /* First see if we already have a cgroup group that matches
1152 * the desired set */
1153 spin_lock_irq(&css_set_lock
);
1154 cset
= find_existing_css_set(old_cset
, cgrp
, template);
1157 spin_unlock_irq(&css_set_lock
);
1162 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
1166 /* Allocate all the cgrp_cset_link objects that we'll need */
1167 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
1172 refcount_set(&cset
->refcount
, 1);
1173 cset
->dom_cset
= cset
;
1174 INIT_LIST_HEAD(&cset
->tasks
);
1175 INIT_LIST_HEAD(&cset
->mg_tasks
);
1176 INIT_LIST_HEAD(&cset
->task_iters
);
1177 INIT_LIST_HEAD(&cset
->threaded_csets
);
1178 INIT_HLIST_NODE(&cset
->hlist
);
1179 INIT_LIST_HEAD(&cset
->cgrp_links
);
1180 INIT_LIST_HEAD(&cset
->mg_preload_node
);
1181 INIT_LIST_HEAD(&cset
->mg_node
);
1183 /* Copy the set of subsystem state objects generated in
1184 * find_existing_css_set() */
1185 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
1187 spin_lock_irq(&css_set_lock
);
1188 /* Add reference counts and links from the new css_set. */
1189 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
1190 struct cgroup
*c
= link
->cgrp
;
1192 if (c
->root
== cgrp
->root
)
1194 link_css_set(&tmp_links
, cset
, c
);
1197 BUG_ON(!list_empty(&tmp_links
));
1201 /* Add @cset to the hash table */
1202 key
= css_set_hash(cset
->subsys
);
1203 hash_add(css_set_table
, &cset
->hlist
, key
);
1205 for_each_subsys(ss
, ssid
) {
1206 struct cgroup_subsys_state
*css
= cset
->subsys
[ssid
];
1208 list_add_tail(&cset
->e_cset_node
[ssid
],
1209 &css
->cgroup
->e_csets
[ssid
]);
1213 spin_unlock_irq(&css_set_lock
);
1216 * If @cset should be threaded, look up the matching dom_cset and
1217 * link them up. We first fully initialize @cset then look for the
1218 * dom_cset. It's simpler this way and safe as @cset is guaranteed
1219 * to stay empty until we return.
1221 if (cgroup_is_threaded(cset
->dfl_cgrp
)) {
1222 struct css_set
*dcset
;
1224 dcset
= find_css_set(cset
, cset
->dfl_cgrp
->dom_cgrp
);
1230 spin_lock_irq(&css_set_lock
);
1231 cset
->dom_cset
= dcset
;
1232 list_add_tail(&cset
->threaded_csets_node
,
1233 &dcset
->threaded_csets
);
1234 spin_unlock_irq(&css_set_lock
);
1240 struct cgroup_root
*cgroup_root_from_kf(struct kernfs_root
*kf_root
)
1242 struct cgroup
*root_cgrp
= kf_root
->kn
->priv
;
1244 return root_cgrp
->root
;
1247 static int cgroup_init_root_id(struct cgroup_root
*root
)
1251 lockdep_assert_held(&cgroup_mutex
);
1253 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, 0, 0, GFP_KERNEL
);
1257 root
->hierarchy_id
= id
;
1261 static void cgroup_exit_root_id(struct cgroup_root
*root
)
1263 lockdep_assert_held(&cgroup_mutex
);
1265 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1268 void cgroup_free_root(struct cgroup_root
*root
)
1271 idr_destroy(&root
->cgroup_idr
);
1276 static void cgroup_destroy_root(struct cgroup_root
*root
)
1278 struct cgroup
*cgrp
= &root
->cgrp
;
1279 struct cgrp_cset_link
*link
, *tmp_link
;
1281 trace_cgroup_destroy_root(root
);
1283 cgroup_lock_and_drain_offline(&cgrp_dfl_root
.cgrp
);
1285 BUG_ON(atomic_read(&root
->nr_cgrps
));
1286 BUG_ON(!list_empty(&cgrp
->self
.children
));
1288 /* Rebind all subsystems back to the default hierarchy */
1289 WARN_ON(rebind_subsystems(&cgrp_dfl_root
, root
->subsys_mask
));
1292 * Release all the links from cset_links to this hierarchy's
1295 spin_lock_irq(&css_set_lock
);
1297 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1298 list_del(&link
->cset_link
);
1299 list_del(&link
->cgrp_link
);
1303 spin_unlock_irq(&css_set_lock
);
1305 if (!list_empty(&root
->root_list
)) {
1306 list_del(&root
->root_list
);
1307 cgroup_root_count
--;
1310 cgroup_exit_root_id(root
);
1312 mutex_unlock(&cgroup_mutex
);
1314 kernfs_destroy_root(root
->kf_root
);
1315 cgroup_free_root(root
);
1319 * look up cgroup associated with current task's cgroup namespace on the
1320 * specified hierarchy
1322 static struct cgroup
*
1323 current_cgns_cgroup_from_root(struct cgroup_root
*root
)
1325 struct cgroup
*res
= NULL
;
1326 struct css_set
*cset
;
1328 lockdep_assert_held(&css_set_lock
);
1332 cset
= current
->nsproxy
->cgroup_ns
->root_cset
;
1333 if (cset
== &init_css_set
) {
1336 struct cgrp_cset_link
*link
;
1338 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
1339 struct cgroup
*c
= link
->cgrp
;
1341 if (c
->root
== root
) {
1353 /* look up cgroup associated with given css_set on the specified hierarchy */
1354 static struct cgroup
*cset_cgroup_from_root(struct css_set
*cset
,
1355 struct cgroup_root
*root
)
1357 struct cgroup
*res
= NULL
;
1359 lockdep_assert_held(&cgroup_mutex
);
1360 lockdep_assert_held(&css_set_lock
);
1362 if (cset
== &init_css_set
) {
1364 } else if (root
== &cgrp_dfl_root
) {
1365 res
= cset
->dfl_cgrp
;
1367 struct cgrp_cset_link
*link
;
1369 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
1370 struct cgroup
*c
= link
->cgrp
;
1372 if (c
->root
== root
) {
1384 * Return the cgroup for "task" from the given hierarchy. Must be
1385 * called with cgroup_mutex and css_set_lock held.
1387 struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
1388 struct cgroup_root
*root
)
1391 * No need to lock the task - since we hold cgroup_mutex the
1392 * task can't change groups, so the only thing that can happen
1393 * is that it exits and its css is set back to init_css_set.
1395 return cset_cgroup_from_root(task_css_set(task
), root
);
1399 * A task must hold cgroup_mutex to modify cgroups.
1401 * Any task can increment and decrement the count field without lock.
1402 * So in general, code holding cgroup_mutex can't rely on the count
1403 * field not changing. However, if the count goes to zero, then only
1404 * cgroup_attach_task() can increment it again. Because a count of zero
1405 * means that no tasks are currently attached, therefore there is no
1406 * way a task attached to that cgroup can fork (the other way to
1407 * increment the count). So code holding cgroup_mutex can safely
1408 * assume that if the count is zero, it will stay zero. Similarly, if
1409 * a task holds cgroup_mutex on a cgroup with zero count, it
1410 * knows that the cgroup won't be removed, as cgroup_rmdir()
1413 * A cgroup can only be deleted if both its 'count' of using tasks
1414 * is zero, and its list of 'children' cgroups is empty. Since all
1415 * tasks in the system use _some_ cgroup, and since there is always at
1416 * least one task in the system (init, pid == 1), therefore, root cgroup
1417 * always has either children cgroups and/or using tasks. So we don't
1418 * need a special hack to ensure that root cgroup cannot be deleted.
1420 * P.S. One more locking exception. RCU is used to guard the
1421 * update of a tasks cgroup pointer by cgroup_attach_task()
1424 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
;
1426 static char *cgroup_file_name(struct cgroup
*cgrp
, const struct cftype
*cft
,
1429 struct cgroup_subsys
*ss
= cft
->ss
;
1431 if (cft
->ss
&& !(cft
->flags
& CFTYPE_NO_PREFIX
) &&
1432 !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
))
1433 snprintf(buf
, CGROUP_FILE_NAME_MAX
, "%s.%s",
1434 cgroup_on_dfl(cgrp
) ? ss
->name
: ss
->legacy_name
,
1437 strscpy(buf
, cft
->name
, CGROUP_FILE_NAME_MAX
);
1442 * cgroup_file_mode - deduce file mode of a control file
1443 * @cft: the control file in question
1445 * S_IRUGO for read, S_IWUSR for write.
1447 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
1451 if (cft
->read_u64
|| cft
->read_s64
|| cft
->seq_show
)
1454 if (cft
->write_u64
|| cft
->write_s64
|| cft
->write
) {
1455 if (cft
->flags
& CFTYPE_WORLD_WRITABLE
)
1465 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
1466 * @subtree_control: the new subtree_control mask to consider
1467 * @this_ss_mask: available subsystems
1469 * On the default hierarchy, a subsystem may request other subsystems to be
1470 * enabled together through its ->depends_on mask. In such cases, more
1471 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1473 * This function calculates which subsystems need to be enabled if
1474 * @subtree_control is to be applied while restricted to @this_ss_mask.
1476 static u16
cgroup_calc_subtree_ss_mask(u16 subtree_control
, u16 this_ss_mask
)
1478 u16 cur_ss_mask
= subtree_control
;
1479 struct cgroup_subsys
*ss
;
1482 lockdep_assert_held(&cgroup_mutex
);
1484 cur_ss_mask
|= cgrp_dfl_implicit_ss_mask
;
1487 u16 new_ss_mask
= cur_ss_mask
;
1489 do_each_subsys_mask(ss
, ssid
, cur_ss_mask
) {
1490 new_ss_mask
|= ss
->depends_on
;
1491 } while_each_subsys_mask();
1494 * Mask out subsystems which aren't available. This can
1495 * happen only if some depended-upon subsystems were bound
1496 * to non-default hierarchies.
1498 new_ss_mask
&= this_ss_mask
;
1500 if (new_ss_mask
== cur_ss_mask
)
1502 cur_ss_mask
= new_ss_mask
;
1509 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1510 * @kn: the kernfs_node being serviced
1512 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1513 * the method finishes if locking succeeded. Note that once this function
1514 * returns the cgroup returned by cgroup_kn_lock_live() may become
1515 * inaccessible any time. If the caller intends to continue to access the
1516 * cgroup, it should pin it before invoking this function.
1518 void cgroup_kn_unlock(struct kernfs_node
*kn
)
1520 struct cgroup
*cgrp
;
1522 if (kernfs_type(kn
) == KERNFS_DIR
)
1525 cgrp
= kn
->parent
->priv
;
1527 mutex_unlock(&cgroup_mutex
);
1529 kernfs_unbreak_active_protection(kn
);
1534 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1535 * @kn: the kernfs_node being serviced
1536 * @drain_offline: perform offline draining on the cgroup
1538 * This helper is to be used by a cgroup kernfs method currently servicing
1539 * @kn. It breaks the active protection, performs cgroup locking and
1540 * verifies that the associated cgroup is alive. Returns the cgroup if
1541 * alive; otherwise, %NULL. A successful return should be undone by a
1542 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1543 * cgroup is drained of offlining csses before return.
1545 * Any cgroup kernfs method implementation which requires locking the
1546 * associated cgroup should use this helper. It avoids nesting cgroup
1547 * locking under kernfs active protection and allows all kernfs operations
1548 * including self-removal.
1550 struct cgroup
*cgroup_kn_lock_live(struct kernfs_node
*kn
, bool drain_offline
)
1552 struct cgroup
*cgrp
;
1554 if (kernfs_type(kn
) == KERNFS_DIR
)
1557 cgrp
= kn
->parent
->priv
;
1560 * We're gonna grab cgroup_mutex which nests outside kernfs
1561 * active_ref. cgroup liveliness check alone provides enough
1562 * protection against removal. Ensure @cgrp stays accessible and
1563 * break the active_ref protection.
1565 if (!cgroup_tryget(cgrp
))
1567 kernfs_break_active_protection(kn
);
1570 cgroup_lock_and_drain_offline(cgrp
);
1572 mutex_lock(&cgroup_mutex
);
1574 if (!cgroup_is_dead(cgrp
))
1577 cgroup_kn_unlock(kn
);
1581 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
1583 char name
[CGROUP_FILE_NAME_MAX
];
1585 lockdep_assert_held(&cgroup_mutex
);
1587 if (cft
->file_offset
) {
1588 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, cft
->ss
);
1589 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
1591 spin_lock_irq(&cgroup_file_kn_lock
);
1593 spin_unlock_irq(&cgroup_file_kn_lock
);
1595 del_timer_sync(&cfile
->notify_timer
);
1598 kernfs_remove_by_name(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
));
1602 * css_clear_dir - remove subsys files in a cgroup directory
1605 static void css_clear_dir(struct cgroup_subsys_state
*css
)
1607 struct cgroup
*cgrp
= css
->cgroup
;
1608 struct cftype
*cfts
;
1610 if (!(css
->flags
& CSS_VISIBLE
))
1613 css
->flags
&= ~CSS_VISIBLE
;
1616 if (cgroup_on_dfl(cgrp
))
1617 cfts
= cgroup_base_files
;
1619 cfts
= cgroup1_base_files
;
1621 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1623 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
)
1624 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1629 * css_populate_dir - create subsys files in a cgroup directory
1632 * On failure, no file is added.
1634 static int css_populate_dir(struct cgroup_subsys_state
*css
)
1636 struct cgroup
*cgrp
= css
->cgroup
;
1637 struct cftype
*cfts
, *failed_cfts
;
1640 if ((css
->flags
& CSS_VISIBLE
) || !cgrp
->kn
)
1644 if (cgroup_on_dfl(cgrp
))
1645 cfts
= cgroup_base_files
;
1647 cfts
= cgroup1_base_files
;
1649 ret
= cgroup_addrm_files(&cgrp
->self
, cgrp
, cfts
, true);
1653 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1654 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, true);
1662 css
->flags
|= CSS_VISIBLE
;
1666 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1667 if (cfts
== failed_cfts
)
1669 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1674 int rebind_subsystems(struct cgroup_root
*dst_root
, u16 ss_mask
)
1676 struct cgroup
*dcgrp
= &dst_root
->cgrp
;
1677 struct cgroup_subsys
*ss
;
1680 lockdep_assert_held(&cgroup_mutex
);
1682 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
1684 * If @ss has non-root csses attached to it, can't move.
1685 * If @ss is an implicit controller, it is exempt from this
1686 * rule and can be stolen.
1688 if (css_next_child(NULL
, cgroup_css(&ss
->root
->cgrp
, ss
)) &&
1689 !ss
->implicit_on_dfl
)
1692 /* can't move between two non-dummy roots either */
1693 if (ss
->root
!= &cgrp_dfl_root
&& dst_root
!= &cgrp_dfl_root
)
1695 } while_each_subsys_mask();
1697 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
1698 struct cgroup_root
*src_root
= ss
->root
;
1699 struct cgroup
*scgrp
= &src_root
->cgrp
;
1700 struct cgroup_subsys_state
*css
= cgroup_css(scgrp
, ss
);
1701 struct css_set
*cset
;
1703 WARN_ON(!css
|| cgroup_css(dcgrp
, ss
));
1705 /* disable from the source */
1706 src_root
->subsys_mask
&= ~(1 << ssid
);
1707 WARN_ON(cgroup_apply_control(scgrp
));
1708 cgroup_finalize_control(scgrp
, 0);
1711 RCU_INIT_POINTER(scgrp
->subsys
[ssid
], NULL
);
1712 rcu_assign_pointer(dcgrp
->subsys
[ssid
], css
);
1713 ss
->root
= dst_root
;
1714 css
->cgroup
= dcgrp
;
1716 spin_lock_irq(&css_set_lock
);
1717 hash_for_each(css_set_table
, i
, cset
, hlist
)
1718 list_move_tail(&cset
->e_cset_node
[ss
->id
],
1719 &dcgrp
->e_csets
[ss
->id
]);
1720 spin_unlock_irq(&css_set_lock
);
1722 /* default hierarchy doesn't enable controllers by default */
1723 dst_root
->subsys_mask
|= 1 << ssid
;
1724 if (dst_root
== &cgrp_dfl_root
) {
1725 static_branch_enable(cgroup_subsys_on_dfl_key
[ssid
]);
1727 dcgrp
->subtree_control
|= 1 << ssid
;
1728 static_branch_disable(cgroup_subsys_on_dfl_key
[ssid
]);
1731 ret
= cgroup_apply_control(dcgrp
);
1733 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1738 } while_each_subsys_mask();
1740 kernfs_activate(dcgrp
->kn
);
1744 int cgroup_show_path(struct seq_file
*sf
, struct kernfs_node
*kf_node
,
1745 struct kernfs_root
*kf_root
)
1749 struct cgroup_root
*kf_cgroot
= cgroup_root_from_kf(kf_root
);
1750 struct cgroup
*ns_cgroup
;
1752 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1756 spin_lock_irq(&css_set_lock
);
1757 ns_cgroup
= current_cgns_cgroup_from_root(kf_cgroot
);
1758 len
= kernfs_path_from_node(kf_node
, ns_cgroup
->kn
, buf
, PATH_MAX
);
1759 spin_unlock_irq(&css_set_lock
);
1761 if (len
>= PATH_MAX
)
1764 seq_escape(sf
, buf
, " \t\n\\");
1771 static int parse_cgroup_root_flags(char *data
, unsigned int *root_flags
)
1780 while ((token
= strsep(&data
, ",")) != NULL
) {
1781 if (!strcmp(token
, "nsdelegate")) {
1782 *root_flags
|= CGRP_ROOT_NS_DELEGATE
;
1786 pr_err("cgroup2: unknown option \"%s\"\n", token
);
1793 static void apply_cgroup_root_flags(unsigned int root_flags
)
1795 if (current
->nsproxy
->cgroup_ns
== &init_cgroup_ns
) {
1796 if (root_flags
& CGRP_ROOT_NS_DELEGATE
)
1797 cgrp_dfl_root
.flags
|= CGRP_ROOT_NS_DELEGATE
;
1799 cgrp_dfl_root
.flags
&= ~CGRP_ROOT_NS_DELEGATE
;
1803 static int cgroup_show_options(struct seq_file
*seq
, struct kernfs_root
*kf_root
)
1805 if (cgrp_dfl_root
.flags
& CGRP_ROOT_NS_DELEGATE
)
1806 seq_puts(seq
, ",nsdelegate");
1810 static int cgroup_remount(struct kernfs_root
*kf_root
, int *flags
, char *data
)
1812 unsigned int root_flags
;
1815 ret
= parse_cgroup_root_flags(data
, &root_flags
);
1819 apply_cgroup_root_flags(root_flags
);
1824 * To reduce the fork() overhead for systems that are not actually using
1825 * their cgroups capability, we don't maintain the lists running through
1826 * each css_set to its tasks until we see the list actually used - in other
1827 * words after the first mount.
1829 static bool use_task_css_set_links __read_mostly
;
1831 static void cgroup_enable_task_cg_lists(void)
1833 struct task_struct
*p
, *g
;
1836 * We need tasklist_lock because RCU is not safe against
1837 * while_each_thread(). Besides, a forking task that has passed
1838 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1839 * is not guaranteed to have its child immediately visible in the
1840 * tasklist if we walk through it with RCU.
1842 read_lock(&tasklist_lock
);
1843 spin_lock_irq(&css_set_lock
);
1845 if (use_task_css_set_links
)
1848 use_task_css_set_links
= true;
1850 do_each_thread(g
, p
) {
1851 WARN_ON_ONCE(!list_empty(&p
->cg_list
) ||
1852 task_css_set(p
) != &init_css_set
);
1855 * We should check if the process is exiting, otherwise
1856 * it will race with cgroup_exit() in that the list
1857 * entry won't be deleted though the process has exited.
1858 * Do it while holding siglock so that we don't end up
1859 * racing against cgroup_exit().
1861 * Interrupts were already disabled while acquiring
1862 * the css_set_lock, so we do not need to disable it
1863 * again when acquiring the sighand->siglock here.
1865 spin_lock(&p
->sighand
->siglock
);
1866 if (!(p
->flags
& PF_EXITING
)) {
1867 struct css_set
*cset
= task_css_set(p
);
1869 if (!css_set_populated(cset
))
1870 css_set_update_populated(cset
, true);
1871 list_add_tail(&p
->cg_list
, &cset
->tasks
);
1875 spin_unlock(&p
->sighand
->siglock
);
1876 } while_each_thread(g
, p
);
1878 spin_unlock_irq(&css_set_lock
);
1879 read_unlock(&tasklist_lock
);
1882 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1884 struct cgroup_subsys
*ss
;
1887 INIT_LIST_HEAD(&cgrp
->self
.sibling
);
1888 INIT_LIST_HEAD(&cgrp
->self
.children
);
1889 INIT_LIST_HEAD(&cgrp
->cset_links
);
1890 INIT_LIST_HEAD(&cgrp
->pidlists
);
1891 mutex_init(&cgrp
->pidlist_mutex
);
1892 cgrp
->self
.cgroup
= cgrp
;
1893 cgrp
->self
.flags
|= CSS_ONLINE
;
1894 cgrp
->dom_cgrp
= cgrp
;
1895 cgrp
->max_descendants
= INT_MAX
;
1896 cgrp
->max_depth
= INT_MAX
;
1897 INIT_LIST_HEAD(&cgrp
->rstat_css_list
);
1898 prev_cputime_init(&cgrp
->prev_cputime
);
1900 for_each_subsys(ss
, ssid
)
1901 INIT_LIST_HEAD(&cgrp
->e_csets
[ssid
]);
1903 init_waitqueue_head(&cgrp
->offline_waitq
);
1904 INIT_WORK(&cgrp
->release_agent_work
, cgroup1_release_agent
);
1907 void init_cgroup_root(struct cgroup_root
*root
, struct cgroup_sb_opts
*opts
)
1909 struct cgroup
*cgrp
= &root
->cgrp
;
1911 INIT_LIST_HEAD(&root
->root_list
);
1912 atomic_set(&root
->nr_cgrps
, 1);
1914 init_cgroup_housekeeping(cgrp
);
1915 idr_init(&root
->cgroup_idr
);
1917 root
->flags
= opts
->flags
;
1918 if (opts
->release_agent
)
1919 strscpy(root
->release_agent_path
, opts
->release_agent
, PATH_MAX
);
1921 strscpy(root
->name
, opts
->name
, MAX_CGROUP_ROOT_NAMELEN
);
1922 if (opts
->cpuset_clone_children
)
1923 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->cgrp
.flags
);
1926 int cgroup_setup_root(struct cgroup_root
*root
, u16 ss_mask
, int ref_flags
)
1928 LIST_HEAD(tmp_links
);
1929 struct cgroup
*root_cgrp
= &root
->cgrp
;
1930 struct kernfs_syscall_ops
*kf_sops
;
1931 struct css_set
*cset
;
1934 lockdep_assert_held(&cgroup_mutex
);
1936 ret
= cgroup_idr_alloc(&root
->cgroup_idr
, root_cgrp
, 1, 2, GFP_KERNEL
);
1939 root_cgrp
->id
= ret
;
1940 root_cgrp
->ancestor_ids
[0] = ret
;
1942 ret
= percpu_ref_init(&root_cgrp
->self
.refcnt
, css_release
,
1943 ref_flags
, GFP_KERNEL
);
1948 * We're accessing css_set_count without locking css_set_lock here,
1949 * but that's OK - it can only be increased by someone holding
1950 * cgroup_lock, and that's us. Later rebinding may disable
1951 * controllers on the default hierarchy and thus create new csets,
1952 * which can't be more than the existing ones. Allocate 2x.
1954 ret
= allocate_cgrp_cset_links(2 * css_set_count
, &tmp_links
);
1958 ret
= cgroup_init_root_id(root
);
1962 kf_sops
= root
== &cgrp_dfl_root
?
1963 &cgroup_kf_syscall_ops
: &cgroup1_kf_syscall_ops
;
1965 root
->kf_root
= kernfs_create_root(kf_sops
,
1966 KERNFS_ROOT_CREATE_DEACTIVATED
|
1967 KERNFS_ROOT_SUPPORT_EXPORTOP
,
1969 if (IS_ERR(root
->kf_root
)) {
1970 ret
= PTR_ERR(root
->kf_root
);
1973 root_cgrp
->kn
= root
->kf_root
->kn
;
1975 ret
= css_populate_dir(&root_cgrp
->self
);
1979 ret
= rebind_subsystems(root
, ss_mask
);
1983 ret
= cgroup_bpf_inherit(root_cgrp
);
1986 trace_cgroup_setup_root(root
);
1989 * There must be no failure case after here, since rebinding takes
1990 * care of subsystems' refcounts, which are explicitly dropped in
1991 * the failure exit path.
1993 list_add(&root
->root_list
, &cgroup_roots
);
1994 cgroup_root_count
++;
1997 * Link the root cgroup in this hierarchy into all the css_set
2000 spin_lock_irq(&css_set_lock
);
2001 hash_for_each(css_set_table
, i
, cset
, hlist
) {
2002 link_css_set(&tmp_links
, cset
, root_cgrp
);
2003 if (css_set_populated(cset
))
2004 cgroup_update_populated(root_cgrp
, true);
2006 spin_unlock_irq(&css_set_lock
);
2008 BUG_ON(!list_empty(&root_cgrp
->self
.children
));
2009 BUG_ON(atomic_read(&root
->nr_cgrps
) != 1);
2011 kernfs_activate(root_cgrp
->kn
);
2016 kernfs_destroy_root(root
->kf_root
);
2017 root
->kf_root
= NULL
;
2019 cgroup_exit_root_id(root
);
2021 percpu_ref_exit(&root_cgrp
->self
.refcnt
);
2023 free_cgrp_cset_links(&tmp_links
);
2027 struct dentry
*cgroup_do_mount(struct file_system_type
*fs_type
, int flags
,
2028 struct cgroup_root
*root
, unsigned long magic
,
2029 struct cgroup_namespace
*ns
)
2031 struct dentry
*dentry
;
2034 dentry
= kernfs_mount(fs_type
, flags
, root
->kf_root
, magic
, &new_sb
);
2037 * In non-init cgroup namespace, instead of root cgroup's dentry,
2038 * we return the dentry corresponding to the cgroupns->root_cgrp.
2040 if (!IS_ERR(dentry
) && ns
!= &init_cgroup_ns
) {
2041 struct dentry
*nsdentry
;
2042 struct cgroup
*cgrp
;
2044 mutex_lock(&cgroup_mutex
);
2045 spin_lock_irq(&css_set_lock
);
2047 cgrp
= cset_cgroup_from_root(ns
->root_cset
, root
);
2049 spin_unlock_irq(&css_set_lock
);
2050 mutex_unlock(&cgroup_mutex
);
2052 nsdentry
= kernfs_node_dentry(cgrp
->kn
, dentry
->d_sb
);
2057 if (IS_ERR(dentry
) || !new_sb
)
2058 cgroup_put(&root
->cgrp
);
2063 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
2064 int flags
, const char *unused_dev_name
,
2067 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
2068 struct dentry
*dentry
;
2073 /* Check if the caller has permission to mount. */
2074 if (!ns_capable(ns
->user_ns
, CAP_SYS_ADMIN
)) {
2076 return ERR_PTR(-EPERM
);
2080 * The first time anyone tries to mount a cgroup, enable the list
2081 * linking each css_set to its tasks and fix up all existing tasks.
2083 if (!use_task_css_set_links
)
2084 cgroup_enable_task_cg_lists();
2086 if (fs_type
== &cgroup2_fs_type
) {
2087 unsigned int root_flags
;
2089 ret
= parse_cgroup_root_flags(data
, &root_flags
);
2092 return ERR_PTR(ret
);
2095 cgrp_dfl_visible
= true;
2096 cgroup_get_live(&cgrp_dfl_root
.cgrp
);
2098 dentry
= cgroup_do_mount(&cgroup2_fs_type
, flags
, &cgrp_dfl_root
,
2099 CGROUP2_SUPER_MAGIC
, ns
);
2100 if (!IS_ERR(dentry
))
2101 apply_cgroup_root_flags(root_flags
);
2103 dentry
= cgroup1_mount(&cgroup_fs_type
, flags
, data
,
2104 CGROUP_SUPER_MAGIC
, ns
);
2111 static void cgroup_kill_sb(struct super_block
*sb
)
2113 struct kernfs_root
*kf_root
= kernfs_root_from_sb(sb
);
2114 struct cgroup_root
*root
= cgroup_root_from_kf(kf_root
);
2117 * If @root doesn't have any mounts or children, start killing it.
2118 * This prevents new mounts by disabling percpu_ref_tryget_live().
2119 * cgroup_mount() may wait for @root's release.
2121 * And don't kill the default root.
2123 if (!list_empty(&root
->cgrp
.self
.children
) ||
2124 root
== &cgrp_dfl_root
)
2125 cgroup_put(&root
->cgrp
);
2127 percpu_ref_kill(&root
->cgrp
.self
.refcnt
);
2132 struct file_system_type cgroup_fs_type
= {
2134 .mount
= cgroup_mount
,
2135 .kill_sb
= cgroup_kill_sb
,
2136 .fs_flags
= FS_USERNS_MOUNT
,
2139 static struct file_system_type cgroup2_fs_type
= {
2141 .mount
= cgroup_mount
,
2142 .kill_sb
= cgroup_kill_sb
,
2143 .fs_flags
= FS_USERNS_MOUNT
,
2146 int cgroup_path_ns_locked(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
2147 struct cgroup_namespace
*ns
)
2149 struct cgroup
*root
= cset_cgroup_from_root(ns
->root_cset
, cgrp
->root
);
2151 return kernfs_path_from_node(cgrp
->kn
, root
->kn
, buf
, buflen
);
2154 int cgroup_path_ns(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
2155 struct cgroup_namespace
*ns
)
2159 mutex_lock(&cgroup_mutex
);
2160 spin_lock_irq(&css_set_lock
);
2162 ret
= cgroup_path_ns_locked(cgrp
, buf
, buflen
, ns
);
2164 spin_unlock_irq(&css_set_lock
);
2165 mutex_unlock(&cgroup_mutex
);
2169 EXPORT_SYMBOL_GPL(cgroup_path_ns
);
2172 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
2173 * @task: target task
2174 * @buf: the buffer to write the path into
2175 * @buflen: the length of the buffer
2177 * Determine @task's cgroup on the first (the one with the lowest non-zero
2178 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2179 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2180 * cgroup controller callbacks.
2182 * Return value is the same as kernfs_path().
2184 int task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
2186 struct cgroup_root
*root
;
2187 struct cgroup
*cgrp
;
2188 int hierarchy_id
= 1;
2191 mutex_lock(&cgroup_mutex
);
2192 spin_lock_irq(&css_set_lock
);
2194 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
2197 cgrp
= task_cgroup_from_root(task
, root
);
2198 ret
= cgroup_path_ns_locked(cgrp
, buf
, buflen
, &init_cgroup_ns
);
2200 /* if no hierarchy exists, everyone is in "/" */
2201 ret
= strlcpy(buf
, "/", buflen
);
2204 spin_unlock_irq(&css_set_lock
);
2205 mutex_unlock(&cgroup_mutex
);
2208 EXPORT_SYMBOL_GPL(task_cgroup_path
);
2211 * cgroup_migrate_add_task - add a migration target task to a migration context
2212 * @task: target task
2213 * @mgctx: target migration context
2215 * Add @task, which is a migration target, to @mgctx->tset. This function
2216 * becomes noop if @task doesn't need to be migrated. @task's css_set
2217 * should have been added as a migration source and @task->cg_list will be
2218 * moved from the css_set's tasks list to mg_tasks one.
2220 static void cgroup_migrate_add_task(struct task_struct
*task
,
2221 struct cgroup_mgctx
*mgctx
)
2223 struct css_set
*cset
;
2225 lockdep_assert_held(&css_set_lock
);
2227 /* @task either already exited or can't exit until the end */
2228 if (task
->flags
& PF_EXITING
)
2231 /* leave @task alone if post_fork() hasn't linked it yet */
2232 if (list_empty(&task
->cg_list
))
2235 cset
= task_css_set(task
);
2236 if (!cset
->mg_src_cgrp
)
2239 mgctx
->tset
.nr_tasks
++;
2241 list_move_tail(&task
->cg_list
, &cset
->mg_tasks
);
2242 if (list_empty(&cset
->mg_node
))
2243 list_add_tail(&cset
->mg_node
,
2244 &mgctx
->tset
.src_csets
);
2245 if (list_empty(&cset
->mg_dst_cset
->mg_node
))
2246 list_add_tail(&cset
->mg_dst_cset
->mg_node
,
2247 &mgctx
->tset
.dst_csets
);
2251 * cgroup_taskset_first - reset taskset and return the first task
2252 * @tset: taskset of interest
2253 * @dst_cssp: output variable for the destination css
2255 * @tset iteration is initialized and the first task is returned.
2257 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
,
2258 struct cgroup_subsys_state
**dst_cssp
)
2260 tset
->cur_cset
= list_first_entry(tset
->csets
, struct css_set
, mg_node
);
2261 tset
->cur_task
= NULL
;
2263 return cgroup_taskset_next(tset
, dst_cssp
);
2267 * cgroup_taskset_next - iterate to the next task in taskset
2268 * @tset: taskset of interest
2269 * @dst_cssp: output variable for the destination css
2271 * Return the next task in @tset. Iteration must have been initialized
2272 * with cgroup_taskset_first().
2274 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
,
2275 struct cgroup_subsys_state
**dst_cssp
)
2277 struct css_set
*cset
= tset
->cur_cset
;
2278 struct task_struct
*task
= tset
->cur_task
;
2280 while (&cset
->mg_node
!= tset
->csets
) {
2282 task
= list_first_entry(&cset
->mg_tasks
,
2283 struct task_struct
, cg_list
);
2285 task
= list_next_entry(task
, cg_list
);
2287 if (&task
->cg_list
!= &cset
->mg_tasks
) {
2288 tset
->cur_cset
= cset
;
2289 tset
->cur_task
= task
;
2292 * This function may be called both before and
2293 * after cgroup_taskset_migrate(). The two cases
2294 * can be distinguished by looking at whether @cset
2295 * has its ->mg_dst_cset set.
2297 if (cset
->mg_dst_cset
)
2298 *dst_cssp
= cset
->mg_dst_cset
->subsys
[tset
->ssid
];
2300 *dst_cssp
= cset
->subsys
[tset
->ssid
];
2305 cset
= list_next_entry(cset
, mg_node
);
2313 * cgroup_taskset_migrate - migrate a taskset
2314 * @mgctx: migration context
2316 * Migrate tasks in @mgctx as setup by migration preparation functions.
2317 * This function fails iff one of the ->can_attach callbacks fails and
2318 * guarantees that either all or none of the tasks in @mgctx are migrated.
2319 * @mgctx is consumed regardless of success.
2321 static int cgroup_migrate_execute(struct cgroup_mgctx
*mgctx
)
2323 struct cgroup_taskset
*tset
= &mgctx
->tset
;
2324 struct cgroup_subsys
*ss
;
2325 struct task_struct
*task
, *tmp_task
;
2326 struct css_set
*cset
, *tmp_cset
;
2327 int ssid
, failed_ssid
, ret
;
2329 /* check that we can legitimately attach to the cgroup */
2330 if (tset
->nr_tasks
) {
2331 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2332 if (ss
->can_attach
) {
2334 ret
= ss
->can_attach(tset
);
2337 goto out_cancel_attach
;
2340 } while_each_subsys_mask();
2344 * Now that we're guaranteed success, proceed to move all tasks to
2345 * the new cgroup. There are no failure cases after here, so this
2346 * is the commit point.
2348 spin_lock_irq(&css_set_lock
);
2349 list_for_each_entry(cset
, &tset
->src_csets
, mg_node
) {
2350 list_for_each_entry_safe(task
, tmp_task
, &cset
->mg_tasks
, cg_list
) {
2351 struct css_set
*from_cset
= task_css_set(task
);
2352 struct css_set
*to_cset
= cset
->mg_dst_cset
;
2354 get_css_set(to_cset
);
2355 to_cset
->nr_tasks
++;
2356 css_set_move_task(task
, from_cset
, to_cset
, true);
2357 put_css_set_locked(from_cset
);
2358 from_cset
->nr_tasks
--;
2361 spin_unlock_irq(&css_set_lock
);
2364 * Migration is committed, all target tasks are now on dst_csets.
2365 * Nothing is sensitive to fork() after this point. Notify
2366 * controllers that migration is complete.
2368 tset
->csets
= &tset
->dst_csets
;
2370 if (tset
->nr_tasks
) {
2371 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2376 } while_each_subsys_mask();
2380 goto out_release_tset
;
2383 if (tset
->nr_tasks
) {
2384 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2385 if (ssid
== failed_ssid
)
2387 if (ss
->cancel_attach
) {
2389 ss
->cancel_attach(tset
);
2391 } while_each_subsys_mask();
2394 spin_lock_irq(&css_set_lock
);
2395 list_splice_init(&tset
->dst_csets
, &tset
->src_csets
);
2396 list_for_each_entry_safe(cset
, tmp_cset
, &tset
->src_csets
, mg_node
) {
2397 list_splice_tail_init(&cset
->mg_tasks
, &cset
->tasks
);
2398 list_del_init(&cset
->mg_node
);
2400 spin_unlock_irq(&css_set_lock
);
2403 * Re-initialize the cgroup_taskset structure in case it is reused
2404 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
2408 tset
->csets
= &tset
->src_csets
;
2413 * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
2414 * @dst_cgrp: destination cgroup to test
2416 * On the default hierarchy, except for the mixable, (possible) thread root
2417 * and threaded cgroups, subtree_control must be zero for migration
2418 * destination cgroups with tasks so that child cgroups don't compete
2421 int cgroup_migrate_vet_dst(struct cgroup
*dst_cgrp
)
2423 /* v1 doesn't have any restriction */
2424 if (!cgroup_on_dfl(dst_cgrp
))
2427 /* verify @dst_cgrp can host resources */
2428 if (!cgroup_is_valid_domain(dst_cgrp
->dom_cgrp
))
2431 /* mixables don't care */
2432 if (cgroup_is_mixable(dst_cgrp
))
2436 * If @dst_cgrp is already or can become a thread root or is
2437 * threaded, it doesn't matter.
2439 if (cgroup_can_be_thread_root(dst_cgrp
) || cgroup_is_threaded(dst_cgrp
))
2442 /* apply no-internal-process constraint */
2443 if (dst_cgrp
->subtree_control
)
2450 * cgroup_migrate_finish - cleanup after attach
2451 * @mgctx: migration context
2453 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2454 * those functions for details.
2456 void cgroup_migrate_finish(struct cgroup_mgctx
*mgctx
)
2458 LIST_HEAD(preloaded
);
2459 struct css_set
*cset
, *tmp_cset
;
2461 lockdep_assert_held(&cgroup_mutex
);
2463 spin_lock_irq(&css_set_lock
);
2465 list_splice_tail_init(&mgctx
->preloaded_src_csets
, &preloaded
);
2466 list_splice_tail_init(&mgctx
->preloaded_dst_csets
, &preloaded
);
2468 list_for_each_entry_safe(cset
, tmp_cset
, &preloaded
, mg_preload_node
) {
2469 cset
->mg_src_cgrp
= NULL
;
2470 cset
->mg_dst_cgrp
= NULL
;
2471 cset
->mg_dst_cset
= NULL
;
2472 list_del_init(&cset
->mg_preload_node
);
2473 put_css_set_locked(cset
);
2476 spin_unlock_irq(&css_set_lock
);
2480 * cgroup_migrate_add_src - add a migration source css_set
2481 * @src_cset: the source css_set to add
2482 * @dst_cgrp: the destination cgroup
2483 * @mgctx: migration context
2485 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2486 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
2487 * up by cgroup_migrate_finish().
2489 * This function may be called without holding cgroup_threadgroup_rwsem
2490 * even if the target is a process. Threads may be created and destroyed
2491 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2492 * into play and the preloaded css_sets are guaranteed to cover all
2495 void cgroup_migrate_add_src(struct css_set
*src_cset
,
2496 struct cgroup
*dst_cgrp
,
2497 struct cgroup_mgctx
*mgctx
)
2499 struct cgroup
*src_cgrp
;
2501 lockdep_assert_held(&cgroup_mutex
);
2502 lockdep_assert_held(&css_set_lock
);
2505 * If ->dead, @src_set is associated with one or more dead cgroups
2506 * and doesn't contain any migratable tasks. Ignore it early so
2507 * that the rest of migration path doesn't get confused by it.
2512 src_cgrp
= cset_cgroup_from_root(src_cset
, dst_cgrp
->root
);
2514 if (!list_empty(&src_cset
->mg_preload_node
))
2517 WARN_ON(src_cset
->mg_src_cgrp
);
2518 WARN_ON(src_cset
->mg_dst_cgrp
);
2519 WARN_ON(!list_empty(&src_cset
->mg_tasks
));
2520 WARN_ON(!list_empty(&src_cset
->mg_node
));
2522 src_cset
->mg_src_cgrp
= src_cgrp
;
2523 src_cset
->mg_dst_cgrp
= dst_cgrp
;
2524 get_css_set(src_cset
);
2525 list_add_tail(&src_cset
->mg_preload_node
, &mgctx
->preloaded_src_csets
);
2529 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
2530 * @mgctx: migration context
2532 * Tasks are about to be moved and all the source css_sets have been
2533 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2534 * pins all destination css_sets, links each to its source, and append them
2535 * to @mgctx->preloaded_dst_csets.
2537 * This function must be called after cgroup_migrate_add_src() has been
2538 * called on each migration source css_set. After migration is performed
2539 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2542 int cgroup_migrate_prepare_dst(struct cgroup_mgctx
*mgctx
)
2544 struct css_set
*src_cset
, *tmp_cset
;
2546 lockdep_assert_held(&cgroup_mutex
);
2548 /* look up the dst cset for each src cset and link it to src */
2549 list_for_each_entry_safe(src_cset
, tmp_cset
, &mgctx
->preloaded_src_csets
,
2551 struct css_set
*dst_cset
;
2552 struct cgroup_subsys
*ss
;
2555 dst_cset
= find_css_set(src_cset
, src_cset
->mg_dst_cgrp
);
2559 WARN_ON_ONCE(src_cset
->mg_dst_cset
|| dst_cset
->mg_dst_cset
);
2562 * If src cset equals dst, it's noop. Drop the src.
2563 * cgroup_migrate() will skip the cset too. Note that we
2564 * can't handle src == dst as some nodes are used by both.
2566 if (src_cset
== dst_cset
) {
2567 src_cset
->mg_src_cgrp
= NULL
;
2568 src_cset
->mg_dst_cgrp
= NULL
;
2569 list_del_init(&src_cset
->mg_preload_node
);
2570 put_css_set(src_cset
);
2571 put_css_set(dst_cset
);
2575 src_cset
->mg_dst_cset
= dst_cset
;
2577 if (list_empty(&dst_cset
->mg_preload_node
))
2578 list_add_tail(&dst_cset
->mg_preload_node
,
2579 &mgctx
->preloaded_dst_csets
);
2581 put_css_set(dst_cset
);
2583 for_each_subsys(ss
, ssid
)
2584 if (src_cset
->subsys
[ssid
] != dst_cset
->subsys
[ssid
])
2585 mgctx
->ss_mask
|= 1 << ssid
;
2590 cgroup_migrate_finish(mgctx
);
2595 * cgroup_migrate - migrate a process or task to a cgroup
2596 * @leader: the leader of the process or the task to migrate
2597 * @threadgroup: whether @leader points to the whole process or a single task
2598 * @mgctx: migration context
2600 * Migrate a process or task denoted by @leader. If migrating a process,
2601 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2602 * responsible for invoking cgroup_migrate_add_src() and
2603 * cgroup_migrate_prepare_dst() on the targets before invoking this
2604 * function and following up with cgroup_migrate_finish().
2606 * As long as a controller's ->can_attach() doesn't fail, this function is
2607 * guaranteed to succeed. This means that, excluding ->can_attach()
2608 * failure, when migrating multiple targets, the success or failure can be
2609 * decided for all targets by invoking group_migrate_prepare_dst() before
2610 * actually starting migrating.
2612 int cgroup_migrate(struct task_struct
*leader
, bool threadgroup
,
2613 struct cgroup_mgctx
*mgctx
)
2615 struct task_struct
*task
;
2618 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2619 * already PF_EXITING could be freed from underneath us unless we
2620 * take an rcu_read_lock.
2622 spin_lock_irq(&css_set_lock
);
2626 cgroup_migrate_add_task(task
, mgctx
);
2629 } while_each_thread(leader
, task
);
2631 spin_unlock_irq(&css_set_lock
);
2633 return cgroup_migrate_execute(mgctx
);
2637 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2638 * @dst_cgrp: the cgroup to attach to
2639 * @leader: the task or the leader of the threadgroup to be attached
2640 * @threadgroup: attach the whole threadgroup?
2642 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
2644 int cgroup_attach_task(struct cgroup
*dst_cgrp
, struct task_struct
*leader
,
2647 DEFINE_CGROUP_MGCTX(mgctx
);
2648 struct task_struct
*task
;
2651 ret
= cgroup_migrate_vet_dst(dst_cgrp
);
2655 /* look up all src csets */
2656 spin_lock_irq(&css_set_lock
);
2660 cgroup_migrate_add_src(task_css_set(task
), dst_cgrp
, &mgctx
);
2663 } while_each_thread(leader
, task
);
2665 spin_unlock_irq(&css_set_lock
);
2667 /* prepare dst csets and commit */
2668 ret
= cgroup_migrate_prepare_dst(&mgctx
);
2670 ret
= cgroup_migrate(leader
, threadgroup
, &mgctx
);
2672 cgroup_migrate_finish(&mgctx
);
2675 TRACE_CGROUP_PATH(attach_task
, dst_cgrp
, leader
, threadgroup
);
2680 struct task_struct
*cgroup_procs_write_start(char *buf
, bool threadgroup
)
2681 __acquires(&cgroup_threadgroup_rwsem
)
2683 struct task_struct
*tsk
;
2686 if (kstrtoint(strstrip(buf
), 0, &pid
) || pid
< 0)
2687 return ERR_PTR(-EINVAL
);
2689 percpu_down_write(&cgroup_threadgroup_rwsem
);
2693 tsk
= find_task_by_vpid(pid
);
2695 tsk
= ERR_PTR(-ESRCH
);
2696 goto out_unlock_threadgroup
;
2703 tsk
= tsk
->group_leader
;
2706 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2707 * If userland migrates such a kthread to a non-root cgroup, it can
2708 * become trapped in a cpuset, or RT kthread may be born in a
2709 * cgroup with no rt_runtime allocated. Just say no.
2711 if (tsk
->no_cgroup_migration
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2712 tsk
= ERR_PTR(-EINVAL
);
2713 goto out_unlock_threadgroup
;
2716 get_task_struct(tsk
);
2717 goto out_unlock_rcu
;
2719 out_unlock_threadgroup
:
2720 percpu_up_write(&cgroup_threadgroup_rwsem
);
2726 void cgroup_procs_write_finish(struct task_struct
*task
)
2727 __releases(&cgroup_threadgroup_rwsem
)
2729 struct cgroup_subsys
*ss
;
2732 /* release reference from cgroup_procs_write_start() */
2733 put_task_struct(task
);
2735 percpu_up_write(&cgroup_threadgroup_rwsem
);
2736 for_each_subsys(ss
, ssid
)
2737 if (ss
->post_attach
)
2741 static void cgroup_print_ss_mask(struct seq_file
*seq
, u16 ss_mask
)
2743 struct cgroup_subsys
*ss
;
2744 bool printed
= false;
2747 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
2750 seq_printf(seq
, "%s", ss
->name
);
2752 } while_each_subsys_mask();
2754 seq_putc(seq
, '\n');
2757 /* show controllers which are enabled from the parent */
2758 static int cgroup_controllers_show(struct seq_file
*seq
, void *v
)
2760 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2762 cgroup_print_ss_mask(seq
, cgroup_control(cgrp
));
2766 /* show controllers which are enabled for a given cgroup's children */
2767 static int cgroup_subtree_control_show(struct seq_file
*seq
, void *v
)
2769 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2771 cgroup_print_ss_mask(seq
, cgrp
->subtree_control
);
2776 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2777 * @cgrp: root of the subtree to update csses for
2779 * @cgrp's control masks have changed and its subtree's css associations
2780 * need to be updated accordingly. This function looks up all css_sets
2781 * which are attached to the subtree, creates the matching updated css_sets
2782 * and migrates the tasks to the new ones.
2784 static int cgroup_update_dfl_csses(struct cgroup
*cgrp
)
2786 DEFINE_CGROUP_MGCTX(mgctx
);
2787 struct cgroup_subsys_state
*d_css
;
2788 struct cgroup
*dsct
;
2789 struct css_set
*src_cset
;
2792 lockdep_assert_held(&cgroup_mutex
);
2794 percpu_down_write(&cgroup_threadgroup_rwsem
);
2796 /* look up all csses currently attached to @cgrp's subtree */
2797 spin_lock_irq(&css_set_lock
);
2798 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2799 struct cgrp_cset_link
*link
;
2801 list_for_each_entry(link
, &dsct
->cset_links
, cset_link
)
2802 cgroup_migrate_add_src(link
->cset
, dsct
, &mgctx
);
2804 spin_unlock_irq(&css_set_lock
);
2806 /* NULL dst indicates self on default hierarchy */
2807 ret
= cgroup_migrate_prepare_dst(&mgctx
);
2811 spin_lock_irq(&css_set_lock
);
2812 list_for_each_entry(src_cset
, &mgctx
.preloaded_src_csets
, mg_preload_node
) {
2813 struct task_struct
*task
, *ntask
;
2815 /* all tasks in src_csets need to be migrated */
2816 list_for_each_entry_safe(task
, ntask
, &src_cset
->tasks
, cg_list
)
2817 cgroup_migrate_add_task(task
, &mgctx
);
2819 spin_unlock_irq(&css_set_lock
);
2821 ret
= cgroup_migrate_execute(&mgctx
);
2823 cgroup_migrate_finish(&mgctx
);
2824 percpu_up_write(&cgroup_threadgroup_rwsem
);
2829 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
2830 * @cgrp: root of the target subtree
2832 * Because css offlining is asynchronous, userland may try to re-enable a
2833 * controller while the previous css is still around. This function grabs
2834 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
2836 void cgroup_lock_and_drain_offline(struct cgroup
*cgrp
)
2837 __acquires(&cgroup_mutex
)
2839 struct cgroup
*dsct
;
2840 struct cgroup_subsys_state
*d_css
;
2841 struct cgroup_subsys
*ss
;
2845 mutex_lock(&cgroup_mutex
);
2847 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2848 for_each_subsys(ss
, ssid
) {
2849 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2852 if (!css
|| !percpu_ref_is_dying(&css
->refcnt
))
2855 cgroup_get_live(dsct
);
2856 prepare_to_wait(&dsct
->offline_waitq
, &wait
,
2857 TASK_UNINTERRUPTIBLE
);
2859 mutex_unlock(&cgroup_mutex
);
2861 finish_wait(&dsct
->offline_waitq
, &wait
);
2870 * cgroup_save_control - save control masks and dom_cgrp of a subtree
2871 * @cgrp: root of the target subtree
2873 * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the
2874 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
2877 static void cgroup_save_control(struct cgroup
*cgrp
)
2879 struct cgroup
*dsct
;
2880 struct cgroup_subsys_state
*d_css
;
2882 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2883 dsct
->old_subtree_control
= dsct
->subtree_control
;
2884 dsct
->old_subtree_ss_mask
= dsct
->subtree_ss_mask
;
2885 dsct
->old_dom_cgrp
= dsct
->dom_cgrp
;
2890 * cgroup_propagate_control - refresh control masks of a subtree
2891 * @cgrp: root of the target subtree
2893 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2894 * ->subtree_control and propagate controller availability through the
2895 * subtree so that descendants don't have unavailable controllers enabled.
2897 static void cgroup_propagate_control(struct cgroup
*cgrp
)
2899 struct cgroup
*dsct
;
2900 struct cgroup_subsys_state
*d_css
;
2902 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2903 dsct
->subtree_control
&= cgroup_control(dsct
);
2904 dsct
->subtree_ss_mask
=
2905 cgroup_calc_subtree_ss_mask(dsct
->subtree_control
,
2906 cgroup_ss_mask(dsct
));
2911 * cgroup_restore_control - restore control masks and dom_cgrp of a subtree
2912 * @cgrp: root of the target subtree
2914 * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the
2915 * respective old_ prefixed fields for @cgrp's subtree including @cgrp
2918 static void cgroup_restore_control(struct cgroup
*cgrp
)
2920 struct cgroup
*dsct
;
2921 struct cgroup_subsys_state
*d_css
;
2923 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2924 dsct
->subtree_control
= dsct
->old_subtree_control
;
2925 dsct
->subtree_ss_mask
= dsct
->old_subtree_ss_mask
;
2926 dsct
->dom_cgrp
= dsct
->old_dom_cgrp
;
2930 static bool css_visible(struct cgroup_subsys_state
*css
)
2932 struct cgroup_subsys
*ss
= css
->ss
;
2933 struct cgroup
*cgrp
= css
->cgroup
;
2935 if (cgroup_control(cgrp
) & (1 << ss
->id
))
2937 if (!(cgroup_ss_mask(cgrp
) & (1 << ss
->id
)))
2939 return cgroup_on_dfl(cgrp
) && ss
->implicit_on_dfl
;
2943 * cgroup_apply_control_enable - enable or show csses according to control
2944 * @cgrp: root of the target subtree
2946 * Walk @cgrp's subtree and create new csses or make the existing ones
2947 * visible. A css is created invisible if it's being implicitly enabled
2948 * through dependency. An invisible css is made visible when the userland
2949 * explicitly enables it.
2951 * Returns 0 on success, -errno on failure. On failure, csses which have
2952 * been processed already aren't cleaned up. The caller is responsible for
2953 * cleaning up with cgroup_apply_control_disable().
2955 static int cgroup_apply_control_enable(struct cgroup
*cgrp
)
2957 struct cgroup
*dsct
;
2958 struct cgroup_subsys_state
*d_css
;
2959 struct cgroup_subsys
*ss
;
2962 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2963 for_each_subsys(ss
, ssid
) {
2964 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2966 WARN_ON_ONCE(css
&& percpu_ref_is_dying(&css
->refcnt
));
2968 if (!(cgroup_ss_mask(dsct
) & (1 << ss
->id
)))
2972 css
= css_create(dsct
, ss
);
2974 return PTR_ERR(css
);
2977 if (css_visible(css
)) {
2978 ret
= css_populate_dir(css
);
2989 * cgroup_apply_control_disable - kill or hide csses according to control
2990 * @cgrp: root of the target subtree
2992 * Walk @cgrp's subtree and kill and hide csses so that they match
2993 * cgroup_ss_mask() and cgroup_visible_mask().
2995 * A css is hidden when the userland requests it to be disabled while other
2996 * subsystems are still depending on it. The css must not actively control
2997 * resources and be in the vanilla state if it's made visible again later.
2998 * Controllers which may be depended upon should provide ->css_reset() for
3001 static void cgroup_apply_control_disable(struct cgroup
*cgrp
)
3003 struct cgroup
*dsct
;
3004 struct cgroup_subsys_state
*d_css
;
3005 struct cgroup_subsys
*ss
;
3008 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
3009 for_each_subsys(ss
, ssid
) {
3010 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
3012 WARN_ON_ONCE(css
&& percpu_ref_is_dying(&css
->refcnt
));
3018 !(cgroup_ss_mask(dsct
) & (1 << ss
->id
))) {
3020 } else if (!css_visible(css
)) {
3030 * cgroup_apply_control - apply control mask updates to the subtree
3031 * @cgrp: root of the target subtree
3033 * subsystems can be enabled and disabled in a subtree using the following
3036 * 1. Call cgroup_save_control() to stash the current state.
3037 * 2. Update ->subtree_control masks in the subtree as desired.
3038 * 3. Call cgroup_apply_control() to apply the changes.
3039 * 4. Optionally perform other related operations.
3040 * 5. Call cgroup_finalize_control() to finish up.
3042 * This function implements step 3 and propagates the mask changes
3043 * throughout @cgrp's subtree, updates csses accordingly and perform
3044 * process migrations.
3046 static int cgroup_apply_control(struct cgroup
*cgrp
)
3050 cgroup_propagate_control(cgrp
);
3052 ret
= cgroup_apply_control_enable(cgrp
);
3057 * At this point, cgroup_e_css_by_mask() results reflect the new csses
3058 * making the following cgroup_update_dfl_csses() properly update
3059 * css associations of all tasks in the subtree.
3061 ret
= cgroup_update_dfl_csses(cgrp
);
3069 * cgroup_finalize_control - finalize control mask update
3070 * @cgrp: root of the target subtree
3071 * @ret: the result of the update
3073 * Finalize control mask update. See cgroup_apply_control() for more info.
3075 static void cgroup_finalize_control(struct cgroup
*cgrp
, int ret
)
3078 cgroup_restore_control(cgrp
);
3079 cgroup_propagate_control(cgrp
);
3082 cgroup_apply_control_disable(cgrp
);
3085 static int cgroup_vet_subtree_control_enable(struct cgroup
*cgrp
, u16 enable
)
3087 u16 domain_enable
= enable
& ~cgrp_dfl_threaded_ss_mask
;
3089 /* if nothing is getting enabled, nothing to worry about */
3093 /* can @cgrp host any resources? */
3094 if (!cgroup_is_valid_domain(cgrp
->dom_cgrp
))
3097 /* mixables don't care */
3098 if (cgroup_is_mixable(cgrp
))
3101 if (domain_enable
) {
3102 /* can't enable domain controllers inside a thread subtree */
3103 if (cgroup_is_thread_root(cgrp
) || cgroup_is_threaded(cgrp
))
3107 * Threaded controllers can handle internal competitions
3108 * and are always allowed inside a (prospective) thread
3111 if (cgroup_can_be_thread_root(cgrp
) || cgroup_is_threaded(cgrp
))
3116 * Controllers can't be enabled for a cgroup with tasks to avoid
3117 * child cgroups competing against tasks.
3119 if (cgroup_has_tasks(cgrp
))
3125 /* change the enabled child controllers for a cgroup in the default hierarchy */
3126 static ssize_t
cgroup_subtree_control_write(struct kernfs_open_file
*of
,
3127 char *buf
, size_t nbytes
,
3130 u16 enable
= 0, disable
= 0;
3131 struct cgroup
*cgrp
, *child
;
3132 struct cgroup_subsys
*ss
;
3137 * Parse input - space separated list of subsystem names prefixed
3138 * with either + or -.
3140 buf
= strstrip(buf
);
3141 while ((tok
= strsep(&buf
, " "))) {
3144 do_each_subsys_mask(ss
, ssid
, ~cgrp_dfl_inhibit_ss_mask
) {
3145 if (!cgroup_ssid_enabled(ssid
) ||
3146 strcmp(tok
+ 1, ss
->name
))
3150 enable
|= 1 << ssid
;
3151 disable
&= ~(1 << ssid
);
3152 } else if (*tok
== '-') {
3153 disable
|= 1 << ssid
;
3154 enable
&= ~(1 << ssid
);
3159 } while_each_subsys_mask();
3160 if (ssid
== CGROUP_SUBSYS_COUNT
)
3164 cgrp
= cgroup_kn_lock_live(of
->kn
, true);
3168 for_each_subsys(ss
, ssid
) {
3169 if (enable
& (1 << ssid
)) {
3170 if (cgrp
->subtree_control
& (1 << ssid
)) {
3171 enable
&= ~(1 << ssid
);
3175 if (!(cgroup_control(cgrp
) & (1 << ssid
))) {
3179 } else if (disable
& (1 << ssid
)) {
3180 if (!(cgrp
->subtree_control
& (1 << ssid
))) {
3181 disable
&= ~(1 << ssid
);
3185 /* a child has it enabled? */
3186 cgroup_for_each_live_child(child
, cgrp
) {
3187 if (child
->subtree_control
& (1 << ssid
)) {
3195 if (!enable
&& !disable
) {
3200 ret
= cgroup_vet_subtree_control_enable(cgrp
, enable
);
3204 /* save and update control masks and prepare csses */
3205 cgroup_save_control(cgrp
);
3207 cgrp
->subtree_control
|= enable
;
3208 cgrp
->subtree_control
&= ~disable
;
3210 ret
= cgroup_apply_control(cgrp
);
3211 cgroup_finalize_control(cgrp
, ret
);
3215 kernfs_activate(cgrp
->kn
);
3217 cgroup_kn_unlock(of
->kn
);
3218 return ret
?: nbytes
;
3222 * cgroup_enable_threaded - make @cgrp threaded
3223 * @cgrp: the target cgroup
3225 * Called when "threaded" is written to the cgroup.type interface file and
3226 * tries to make @cgrp threaded and join the parent's resource domain.
3227 * This function is never called on the root cgroup as cgroup.type doesn't
3230 static int cgroup_enable_threaded(struct cgroup
*cgrp
)
3232 struct cgroup
*parent
= cgroup_parent(cgrp
);
3233 struct cgroup
*dom_cgrp
= parent
->dom_cgrp
;
3234 struct cgroup
*dsct
;
3235 struct cgroup_subsys_state
*d_css
;
3238 lockdep_assert_held(&cgroup_mutex
);
3240 /* noop if already threaded */
3241 if (cgroup_is_threaded(cgrp
))
3245 * If @cgroup is populated or has domain controllers enabled, it
3246 * can't be switched. While the below cgroup_can_be_thread_root()
3247 * test can catch the same conditions, that's only when @parent is
3248 * not mixable, so let's check it explicitly.
3250 if (cgroup_is_populated(cgrp
) ||
3251 cgrp
->subtree_control
& ~cgrp_dfl_threaded_ss_mask
)
3254 /* we're joining the parent's domain, ensure its validity */
3255 if (!cgroup_is_valid_domain(dom_cgrp
) ||
3256 !cgroup_can_be_thread_root(dom_cgrp
))
3260 * The following shouldn't cause actual migrations and should
3263 cgroup_save_control(cgrp
);
3265 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
)
3266 if (dsct
== cgrp
|| cgroup_is_threaded(dsct
))
3267 dsct
->dom_cgrp
= dom_cgrp
;
3269 ret
= cgroup_apply_control(cgrp
);
3271 parent
->nr_threaded_children
++;
3273 cgroup_finalize_control(cgrp
, ret
);
3277 static int cgroup_type_show(struct seq_file
*seq
, void *v
)
3279 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
3281 if (cgroup_is_threaded(cgrp
))
3282 seq_puts(seq
, "threaded\n");
3283 else if (!cgroup_is_valid_domain(cgrp
))
3284 seq_puts(seq
, "domain invalid\n");
3285 else if (cgroup_is_thread_root(cgrp
))
3286 seq_puts(seq
, "domain threaded\n");
3288 seq_puts(seq
, "domain\n");
3293 static ssize_t
cgroup_type_write(struct kernfs_open_file
*of
, char *buf
,
3294 size_t nbytes
, loff_t off
)
3296 struct cgroup
*cgrp
;
3299 /* only switching to threaded mode is supported */
3300 if (strcmp(strstrip(buf
), "threaded"))
3303 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
3307 /* threaded can only be enabled */
3308 ret
= cgroup_enable_threaded(cgrp
);
3310 cgroup_kn_unlock(of
->kn
);
3311 return ret
?: nbytes
;
3314 static int cgroup_max_descendants_show(struct seq_file
*seq
, void *v
)
3316 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
3317 int descendants
= READ_ONCE(cgrp
->max_descendants
);
3319 if (descendants
== INT_MAX
)
3320 seq_puts(seq
, "max\n");
3322 seq_printf(seq
, "%d\n", descendants
);
3327 static ssize_t
cgroup_max_descendants_write(struct kernfs_open_file
*of
,
3328 char *buf
, size_t nbytes
, loff_t off
)
3330 struct cgroup
*cgrp
;
3334 buf
= strstrip(buf
);
3335 if (!strcmp(buf
, "max")) {
3336 descendants
= INT_MAX
;
3338 ret
= kstrtoint(buf
, 0, &descendants
);
3343 if (descendants
< 0)
3346 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
3350 cgrp
->max_descendants
= descendants
;
3352 cgroup_kn_unlock(of
->kn
);
3357 static int cgroup_max_depth_show(struct seq_file
*seq
, void *v
)
3359 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
3360 int depth
= READ_ONCE(cgrp
->max_depth
);
3362 if (depth
== INT_MAX
)
3363 seq_puts(seq
, "max\n");
3365 seq_printf(seq
, "%d\n", depth
);
3370 static ssize_t
cgroup_max_depth_write(struct kernfs_open_file
*of
,
3371 char *buf
, size_t nbytes
, loff_t off
)
3373 struct cgroup
*cgrp
;
3377 buf
= strstrip(buf
);
3378 if (!strcmp(buf
, "max")) {
3381 ret
= kstrtoint(buf
, 0, &depth
);
3389 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
3393 cgrp
->max_depth
= depth
;
3395 cgroup_kn_unlock(of
->kn
);
3400 static int cgroup_events_show(struct seq_file
*seq
, void *v
)
3402 seq_printf(seq
, "populated %d\n",
3403 cgroup_is_populated(seq_css(seq
)->cgroup
));
3407 static int cgroup_stat_show(struct seq_file
*seq
, void *v
)
3409 struct cgroup
*cgroup
= seq_css(seq
)->cgroup
;
3411 seq_printf(seq
, "nr_descendants %d\n",
3412 cgroup
->nr_descendants
);
3413 seq_printf(seq
, "nr_dying_descendants %d\n",
3414 cgroup
->nr_dying_descendants
);
3419 static int __maybe_unused
cgroup_extra_stat_show(struct seq_file
*seq
,
3420 struct cgroup
*cgrp
, int ssid
)
3422 struct cgroup_subsys
*ss
= cgroup_subsys
[ssid
];
3423 struct cgroup_subsys_state
*css
;
3426 if (!ss
->css_extra_stat_show
)
3429 css
= cgroup_tryget_css(cgrp
, ss
);
3433 ret
= ss
->css_extra_stat_show(seq
, css
);
3438 static int cpu_stat_show(struct seq_file
*seq
, void *v
)
3440 struct cgroup __maybe_unused
*cgrp
= seq_css(seq
)->cgroup
;
3443 cgroup_base_stat_cputime_show(seq
);
3444 #ifdef CONFIG_CGROUP_SCHED
3445 ret
= cgroup_extra_stat_show(seq
, cgrp
, cpu_cgrp_id
);
3451 static int cgroup_io_pressure_show(struct seq_file
*seq
, void *v
)
3453 return psi_show(seq
, &seq_css(seq
)->cgroup
->psi
, PSI_IO
);
3455 static int cgroup_memory_pressure_show(struct seq_file
*seq
, void *v
)
3457 return psi_show(seq
, &seq_css(seq
)->cgroup
->psi
, PSI_MEM
);
3459 static int cgroup_cpu_pressure_show(struct seq_file
*seq
, void *v
)
3461 return psi_show(seq
, &seq_css(seq
)->cgroup
->psi
, PSI_CPU
);
3465 static int cgroup_file_open(struct kernfs_open_file
*of
)
3467 struct cftype
*cft
= of
->kn
->priv
;
3470 return cft
->open(of
);
3474 static void cgroup_file_release(struct kernfs_open_file
*of
)
3476 struct cftype
*cft
= of
->kn
->priv
;
3482 static ssize_t
cgroup_file_write(struct kernfs_open_file
*of
, char *buf
,
3483 size_t nbytes
, loff_t off
)
3485 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
3486 struct cgroup
*cgrp
= of
->kn
->parent
->priv
;
3487 struct cftype
*cft
= of
->kn
->priv
;
3488 struct cgroup_subsys_state
*css
;
3492 * If namespaces are delegation boundaries, disallow writes to
3493 * files in an non-init namespace root from inside the namespace
3494 * except for the files explicitly marked delegatable -
3495 * cgroup.procs and cgroup.subtree_control.
3497 if ((cgrp
->root
->flags
& CGRP_ROOT_NS_DELEGATE
) &&
3498 !(cft
->flags
& CFTYPE_NS_DELEGATABLE
) &&
3499 ns
!= &init_cgroup_ns
&& ns
->root_cset
->dfl_cgrp
== cgrp
)
3503 return cft
->write(of
, buf
, nbytes
, off
);
3506 * kernfs guarantees that a file isn't deleted with operations in
3507 * flight, which means that the matching css is and stays alive and
3508 * doesn't need to be pinned. The RCU locking is not necessary
3509 * either. It's just for the convenience of using cgroup_css().
3512 css
= cgroup_css(cgrp
, cft
->ss
);
3515 if (cft
->write_u64
) {
3516 unsigned long long v
;
3517 ret
= kstrtoull(buf
, 0, &v
);
3519 ret
= cft
->write_u64(css
, cft
, v
);
3520 } else if (cft
->write_s64
) {
3522 ret
= kstrtoll(buf
, 0, &v
);
3524 ret
= cft
->write_s64(css
, cft
, v
);
3529 return ret
?: nbytes
;
3532 static void *cgroup_seqfile_start(struct seq_file
*seq
, loff_t
*ppos
)
3534 return seq_cft(seq
)->seq_start(seq
, ppos
);
3537 static void *cgroup_seqfile_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
3539 return seq_cft(seq
)->seq_next(seq
, v
, ppos
);
3542 static void cgroup_seqfile_stop(struct seq_file
*seq
, void *v
)
3544 if (seq_cft(seq
)->seq_stop
)
3545 seq_cft(seq
)->seq_stop(seq
, v
);
3548 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
3550 struct cftype
*cft
= seq_cft(m
);
3551 struct cgroup_subsys_state
*css
= seq_css(m
);
3554 return cft
->seq_show(m
, arg
);
3557 seq_printf(m
, "%llu\n", cft
->read_u64(css
, cft
));
3558 else if (cft
->read_s64
)
3559 seq_printf(m
, "%lld\n", cft
->read_s64(css
, cft
));
3565 static struct kernfs_ops cgroup_kf_single_ops
= {
3566 .atomic_write_len
= PAGE_SIZE
,
3567 .open
= cgroup_file_open
,
3568 .release
= cgroup_file_release
,
3569 .write
= cgroup_file_write
,
3570 .seq_show
= cgroup_seqfile_show
,
3573 static struct kernfs_ops cgroup_kf_ops
= {
3574 .atomic_write_len
= PAGE_SIZE
,
3575 .open
= cgroup_file_open
,
3576 .release
= cgroup_file_release
,
3577 .write
= cgroup_file_write
,
3578 .seq_start
= cgroup_seqfile_start
,
3579 .seq_next
= cgroup_seqfile_next
,
3580 .seq_stop
= cgroup_seqfile_stop
,
3581 .seq_show
= cgroup_seqfile_show
,
3584 /* set uid and gid of cgroup dirs and files to that of the creator */
3585 static int cgroup_kn_set_ugid(struct kernfs_node
*kn
)
3587 struct iattr iattr
= { .ia_valid
= ATTR_UID
| ATTR_GID
,
3588 .ia_uid
= current_fsuid(),
3589 .ia_gid
= current_fsgid(), };
3591 if (uid_eq(iattr
.ia_uid
, GLOBAL_ROOT_UID
) &&
3592 gid_eq(iattr
.ia_gid
, GLOBAL_ROOT_GID
))
3595 return kernfs_setattr(kn
, &iattr
);
3598 static void cgroup_file_notify_timer(struct timer_list
*timer
)
3600 cgroup_file_notify(container_of(timer
, struct cgroup_file
,
3604 static int cgroup_add_file(struct cgroup_subsys_state
*css
, struct cgroup
*cgrp
,
3607 char name
[CGROUP_FILE_NAME_MAX
];
3608 struct kernfs_node
*kn
;
3609 struct lock_class_key
*key
= NULL
;
3612 #ifdef CONFIG_DEBUG_LOCK_ALLOC
3613 key
= &cft
->lockdep_key
;
3615 kn
= __kernfs_create_file(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
),
3616 cgroup_file_mode(cft
),
3617 GLOBAL_ROOT_UID
, GLOBAL_ROOT_GID
,
3618 0, cft
->kf_ops
, cft
,
3623 ret
= cgroup_kn_set_ugid(kn
);
3629 if (cft
->file_offset
) {
3630 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
3632 timer_setup(&cfile
->notify_timer
, cgroup_file_notify_timer
, 0);
3634 spin_lock_irq(&cgroup_file_kn_lock
);
3636 spin_unlock_irq(&cgroup_file_kn_lock
);
3643 * cgroup_addrm_files - add or remove files to a cgroup directory
3644 * @css: the target css
3645 * @cgrp: the target cgroup (usually css->cgroup)
3646 * @cfts: array of cftypes to be added
3647 * @is_add: whether to add or remove
3649 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
3650 * For removals, this function never fails.
3652 static int cgroup_addrm_files(struct cgroup_subsys_state
*css
,
3653 struct cgroup
*cgrp
, struct cftype cfts
[],
3656 struct cftype
*cft
, *cft_end
= NULL
;
3659 lockdep_assert_held(&cgroup_mutex
);
3662 for (cft
= cfts
; cft
!= cft_end
&& cft
->name
[0] != '\0'; cft
++) {
3663 /* does cft->flags tell us to skip this file on @cgrp? */
3664 if ((cft
->flags
& __CFTYPE_ONLY_ON_DFL
) && !cgroup_on_dfl(cgrp
))
3666 if ((cft
->flags
& __CFTYPE_NOT_ON_DFL
) && cgroup_on_dfl(cgrp
))
3668 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgroup_parent(cgrp
))
3670 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgroup_parent(cgrp
))
3674 ret
= cgroup_add_file(css
, cgrp
, cft
);
3676 pr_warn("%s: failed to add %s, err=%d\n",
3677 __func__
, cft
->name
, ret
);
3683 cgroup_rm_file(cgrp
, cft
);
3689 static int cgroup_apply_cftypes(struct cftype
*cfts
, bool is_add
)
3691 struct cgroup_subsys
*ss
= cfts
[0].ss
;
3692 struct cgroup
*root
= &ss
->root
->cgrp
;
3693 struct cgroup_subsys_state
*css
;
3696 lockdep_assert_held(&cgroup_mutex
);
3698 /* add/rm files for all cgroups created before */
3699 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
)) {
3700 struct cgroup
*cgrp
= css
->cgroup
;
3702 if (!(css
->flags
& CSS_VISIBLE
))
3705 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, is_add
);
3711 kernfs_activate(root
->kn
);
3715 static void cgroup_exit_cftypes(struct cftype
*cfts
)
3719 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3720 /* free copy for custom atomic_write_len, see init_cftypes() */
3721 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
)
3726 /* revert flags set by cgroup core while adding @cfts */
3727 cft
->flags
&= ~(__CFTYPE_ONLY_ON_DFL
| __CFTYPE_NOT_ON_DFL
);
3731 static int cgroup_init_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3735 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3736 struct kernfs_ops
*kf_ops
;
3738 WARN_ON(cft
->ss
|| cft
->kf_ops
);
3741 kf_ops
= &cgroup_kf_ops
;
3743 kf_ops
= &cgroup_kf_single_ops
;
3746 * Ugh... if @cft wants a custom max_write_len, we need to
3747 * make a copy of kf_ops to set its atomic_write_len.
3749 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
) {
3750 kf_ops
= kmemdup(kf_ops
, sizeof(*kf_ops
), GFP_KERNEL
);
3752 cgroup_exit_cftypes(cfts
);
3755 kf_ops
->atomic_write_len
= cft
->max_write_len
;
3758 cft
->kf_ops
= kf_ops
;
3765 static int cgroup_rm_cftypes_locked(struct cftype
*cfts
)
3767 lockdep_assert_held(&cgroup_mutex
);
3769 if (!cfts
|| !cfts
[0].ss
)
3772 list_del(&cfts
->node
);
3773 cgroup_apply_cftypes(cfts
, false);
3774 cgroup_exit_cftypes(cfts
);
3779 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3780 * @cfts: zero-length name terminated array of cftypes
3782 * Unregister @cfts. Files described by @cfts are removed from all
3783 * existing cgroups and all future cgroups won't have them either. This
3784 * function can be called anytime whether @cfts' subsys is attached or not.
3786 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3789 int cgroup_rm_cftypes(struct cftype
*cfts
)
3793 mutex_lock(&cgroup_mutex
);
3794 ret
= cgroup_rm_cftypes_locked(cfts
);
3795 mutex_unlock(&cgroup_mutex
);
3800 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3801 * @ss: target cgroup subsystem
3802 * @cfts: zero-length name terminated array of cftypes
3804 * Register @cfts to @ss. Files described by @cfts are created for all
3805 * existing cgroups to which @ss is attached and all future cgroups will
3806 * have them too. This function can be called anytime whether @ss is
3809 * Returns 0 on successful registration, -errno on failure. Note that this
3810 * function currently returns 0 as long as @cfts registration is successful
3811 * even if some file creation attempts on existing cgroups fail.
3813 static int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3817 if (!cgroup_ssid_enabled(ss
->id
))
3820 if (!cfts
|| cfts
[0].name
[0] == '\0')
3823 ret
= cgroup_init_cftypes(ss
, cfts
);
3827 mutex_lock(&cgroup_mutex
);
3829 list_add_tail(&cfts
->node
, &ss
->cfts
);
3830 ret
= cgroup_apply_cftypes(cfts
, true);
3832 cgroup_rm_cftypes_locked(cfts
);
3834 mutex_unlock(&cgroup_mutex
);
3839 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3840 * @ss: target cgroup subsystem
3841 * @cfts: zero-length name terminated array of cftypes
3843 * Similar to cgroup_add_cftypes() but the added files are only used for
3844 * the default hierarchy.
3846 int cgroup_add_dfl_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3850 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3851 cft
->flags
|= __CFTYPE_ONLY_ON_DFL
;
3852 return cgroup_add_cftypes(ss
, cfts
);
3856 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3857 * @ss: target cgroup subsystem
3858 * @cfts: zero-length name terminated array of cftypes
3860 * Similar to cgroup_add_cftypes() but the added files are only used for
3861 * the legacy hierarchies.
3863 int cgroup_add_legacy_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3867 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3868 cft
->flags
|= __CFTYPE_NOT_ON_DFL
;
3869 return cgroup_add_cftypes(ss
, cfts
);
3873 * cgroup_file_notify - generate a file modified event for a cgroup_file
3874 * @cfile: target cgroup_file
3876 * @cfile must have been obtained by setting cftype->file_offset.
3878 void cgroup_file_notify(struct cgroup_file
*cfile
)
3880 unsigned long flags
;
3882 spin_lock_irqsave(&cgroup_file_kn_lock
, flags
);
3884 unsigned long last
= cfile
->notified_at
;
3885 unsigned long next
= last
+ CGROUP_FILE_NOTIFY_MIN_INTV
;
3887 if (time_in_range(jiffies
, last
, next
)) {
3888 timer_reduce(&cfile
->notify_timer
, next
);
3890 kernfs_notify(cfile
->kn
);
3891 cfile
->notified_at
= jiffies
;
3894 spin_unlock_irqrestore(&cgroup_file_kn_lock
, flags
);
3898 * css_next_child - find the next child of a given css
3899 * @pos: the current position (%NULL to initiate traversal)
3900 * @parent: css whose children to walk
3902 * This function returns the next child of @parent and should be called
3903 * under either cgroup_mutex or RCU read lock. The only requirement is
3904 * that @parent and @pos are accessible. The next sibling is guaranteed to
3905 * be returned regardless of their states.
3907 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3908 * css which finished ->css_online() is guaranteed to be visible in the
3909 * future iterations and will stay visible until the last reference is put.
3910 * A css which hasn't finished ->css_online() or already finished
3911 * ->css_offline() may show up during traversal. It's each subsystem's
3912 * responsibility to synchronize against on/offlining.
3914 struct cgroup_subsys_state
*css_next_child(struct cgroup_subsys_state
*pos
,
3915 struct cgroup_subsys_state
*parent
)
3917 struct cgroup_subsys_state
*next
;
3919 cgroup_assert_mutex_or_rcu_locked();
3922 * @pos could already have been unlinked from the sibling list.
3923 * Once a cgroup is removed, its ->sibling.next is no longer
3924 * updated when its next sibling changes. CSS_RELEASED is set when
3925 * @pos is taken off list, at which time its next pointer is valid,
3926 * and, as releases are serialized, the one pointed to by the next
3927 * pointer is guaranteed to not have started release yet. This
3928 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3929 * critical section, the one pointed to by its next pointer is
3930 * guaranteed to not have finished its RCU grace period even if we
3931 * have dropped rcu_read_lock() inbetween iterations.
3933 * If @pos has CSS_RELEASED set, its next pointer can't be
3934 * dereferenced; however, as each css is given a monotonically
3935 * increasing unique serial number and always appended to the
3936 * sibling list, the next one can be found by walking the parent's
3937 * children until the first css with higher serial number than
3938 * @pos's. While this path can be slower, it happens iff iteration
3939 * races against release and the race window is very small.
3942 next
= list_entry_rcu(parent
->children
.next
, struct cgroup_subsys_state
, sibling
);
3943 } else if (likely(!(pos
->flags
& CSS_RELEASED
))) {
3944 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup_subsys_state
, sibling
);
3946 list_for_each_entry_rcu(next
, &parent
->children
, sibling
)
3947 if (next
->serial_nr
> pos
->serial_nr
)
3952 * @next, if not pointing to the head, can be dereferenced and is
3955 if (&next
->sibling
!= &parent
->children
)
3961 * css_next_descendant_pre - find the next descendant for pre-order walk
3962 * @pos: the current position (%NULL to initiate traversal)
3963 * @root: css whose descendants to walk
3965 * To be used by css_for_each_descendant_pre(). Find the next descendant
3966 * to visit for pre-order traversal of @root's descendants. @root is
3967 * included in the iteration and the first node to be visited.
3969 * While this function requires cgroup_mutex or RCU read locking, it
3970 * doesn't require the whole traversal to be contained in a single critical
3971 * section. This function will return the correct next descendant as long
3972 * as both @pos and @root are accessible and @pos is a descendant of @root.
3974 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3975 * css which finished ->css_online() is guaranteed to be visible in the
3976 * future iterations and will stay visible until the last reference is put.
3977 * A css which hasn't finished ->css_online() or already finished
3978 * ->css_offline() may show up during traversal. It's each subsystem's
3979 * responsibility to synchronize against on/offlining.
3981 struct cgroup_subsys_state
*
3982 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
3983 struct cgroup_subsys_state
*root
)
3985 struct cgroup_subsys_state
*next
;
3987 cgroup_assert_mutex_or_rcu_locked();
3989 /* if first iteration, visit @root */
3993 /* visit the first child if exists */
3994 next
= css_next_child(NULL
, pos
);
3998 /* no child, visit my or the closest ancestor's next sibling */
3999 while (pos
!= root
) {
4000 next
= css_next_child(pos
, pos
->parent
);
4010 * css_rightmost_descendant - return the rightmost descendant of a css
4011 * @pos: css of interest
4013 * Return the rightmost descendant of @pos. If there's no descendant, @pos
4014 * is returned. This can be used during pre-order traversal to skip
4017 * While this function requires cgroup_mutex or RCU read locking, it
4018 * doesn't require the whole traversal to be contained in a single critical
4019 * section. This function will return the correct rightmost descendant as
4020 * long as @pos is accessible.
4022 struct cgroup_subsys_state
*
4023 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
4025 struct cgroup_subsys_state
*last
, *tmp
;
4027 cgroup_assert_mutex_or_rcu_locked();
4031 /* ->prev isn't RCU safe, walk ->next till the end */
4033 css_for_each_child(tmp
, last
)
4040 static struct cgroup_subsys_state
*
4041 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
4043 struct cgroup_subsys_state
*last
;
4047 pos
= css_next_child(NULL
, pos
);
4054 * css_next_descendant_post - find the next descendant for post-order walk
4055 * @pos: the current position (%NULL to initiate traversal)
4056 * @root: css whose descendants to walk
4058 * To be used by css_for_each_descendant_post(). Find the next descendant
4059 * to visit for post-order traversal of @root's descendants. @root is
4060 * included in the iteration and the last node to be visited.
4062 * While this function requires cgroup_mutex or RCU read locking, it
4063 * doesn't require the whole traversal to be contained in a single critical
4064 * section. This function will return the correct next descendant as long
4065 * as both @pos and @cgroup are accessible and @pos is a descendant of
4068 * If a subsystem synchronizes ->css_online() and the start of iteration, a
4069 * css which finished ->css_online() is guaranteed to be visible in the
4070 * future iterations and will stay visible until the last reference is put.
4071 * A css which hasn't finished ->css_online() or already finished
4072 * ->css_offline() may show up during traversal. It's each subsystem's
4073 * responsibility to synchronize against on/offlining.
4075 struct cgroup_subsys_state
*
4076 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
4077 struct cgroup_subsys_state
*root
)
4079 struct cgroup_subsys_state
*next
;
4081 cgroup_assert_mutex_or_rcu_locked();
4083 /* if first iteration, visit leftmost descendant which may be @root */
4085 return css_leftmost_descendant(root
);
4087 /* if we visited @root, we're done */
4091 /* if there's an unvisited sibling, visit its leftmost descendant */
4092 next
= css_next_child(pos
, pos
->parent
);
4094 return css_leftmost_descendant(next
);
4096 /* no sibling left, visit parent */
4101 * css_has_online_children - does a css have online children
4102 * @css: the target css
4104 * Returns %true if @css has any online children; otherwise, %false. This
4105 * function can be called from any context but the caller is responsible
4106 * for synchronizing against on/offlining as necessary.
4108 bool css_has_online_children(struct cgroup_subsys_state
*css
)
4110 struct cgroup_subsys_state
*child
;
4114 css_for_each_child(child
, css
) {
4115 if (child
->flags
& CSS_ONLINE
) {
4124 static struct css_set
*css_task_iter_next_css_set(struct css_task_iter
*it
)
4126 struct list_head
*l
;
4127 struct cgrp_cset_link
*link
;
4128 struct css_set
*cset
;
4130 lockdep_assert_held(&css_set_lock
);
4132 /* find the next threaded cset */
4133 if (it
->tcset_pos
) {
4134 l
= it
->tcset_pos
->next
;
4136 if (l
!= it
->tcset_head
) {
4138 return container_of(l
, struct css_set
,
4139 threaded_csets_node
);
4142 it
->tcset_pos
= NULL
;
4145 /* find the next cset */
4148 if (l
== it
->cset_head
) {
4149 it
->cset_pos
= NULL
;
4154 cset
= container_of(l
, struct css_set
, e_cset_node
[it
->ss
->id
]);
4156 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
4162 /* initialize threaded css_set walking */
4163 if (it
->flags
& CSS_TASK_ITER_THREADED
) {
4165 put_css_set_locked(it
->cur_dcset
);
4166 it
->cur_dcset
= cset
;
4169 it
->tcset_head
= &cset
->threaded_csets
;
4170 it
->tcset_pos
= &cset
->threaded_csets
;
4177 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
4178 * @it: the iterator to advance
4180 * Advance @it to the next css_set to walk.
4182 static void css_task_iter_advance_css_set(struct css_task_iter
*it
)
4184 struct css_set
*cset
;
4186 lockdep_assert_held(&css_set_lock
);
4188 /* Advance to the next non-empty css_set */
4190 cset
= css_task_iter_next_css_set(it
);
4192 it
->task_pos
= NULL
;
4195 } while (!css_set_populated(cset
));
4197 if (!list_empty(&cset
->tasks
))
4198 it
->task_pos
= cset
->tasks
.next
;
4200 it
->task_pos
= cset
->mg_tasks
.next
;
4202 it
->tasks_head
= &cset
->tasks
;
4203 it
->mg_tasks_head
= &cset
->mg_tasks
;
4206 * We don't keep css_sets locked across iteration steps and thus
4207 * need to take steps to ensure that iteration can be resumed after
4208 * the lock is re-acquired. Iteration is performed at two levels -
4209 * css_sets and tasks in them.
4211 * Once created, a css_set never leaves its cgroup lists, so a
4212 * pinned css_set is guaranteed to stay put and we can resume
4213 * iteration afterwards.
4215 * Tasks may leave @cset across iteration steps. This is resolved
4216 * by registering each iterator with the css_set currently being
4217 * walked and making css_set_move_task() advance iterators whose
4218 * next task is leaving.
4221 list_del(&it
->iters_node
);
4222 put_css_set_locked(it
->cur_cset
);
4225 it
->cur_cset
= cset
;
4226 list_add(&it
->iters_node
, &cset
->task_iters
);
4229 static void css_task_iter_advance(struct css_task_iter
*it
)
4231 struct list_head
*next
;
4233 lockdep_assert_held(&css_set_lock
);
4236 * Advance iterator to find next entry. cset->tasks is consumed
4237 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
4240 next
= it
->task_pos
->next
;
4242 if (next
== it
->tasks_head
)
4243 next
= it
->mg_tasks_head
->next
;
4245 if (next
== it
->mg_tasks_head
)
4246 css_task_iter_advance_css_set(it
);
4248 it
->task_pos
= next
;
4250 /* if PROCS, skip over tasks which aren't group leaders */
4251 if ((it
->flags
& CSS_TASK_ITER_PROCS
) && it
->task_pos
&&
4252 !thread_group_leader(list_entry(it
->task_pos
, struct task_struct
,
4258 * css_task_iter_start - initiate task iteration
4259 * @css: the css to walk tasks of
4260 * @flags: CSS_TASK_ITER_* flags
4261 * @it: the task iterator to use
4263 * Initiate iteration through the tasks of @css. The caller can call
4264 * css_task_iter_next() to walk through the tasks until the function
4265 * returns NULL. On completion of iteration, css_task_iter_end() must be
4268 void css_task_iter_start(struct cgroup_subsys_state
*css
, unsigned int flags
,
4269 struct css_task_iter
*it
)
4271 /* no one should try to iterate before mounting cgroups */
4272 WARN_ON_ONCE(!use_task_css_set_links
);
4274 memset(it
, 0, sizeof(*it
));
4276 spin_lock_irq(&css_set_lock
);
4282 it
->cset_pos
= &css
->cgroup
->e_csets
[css
->ss
->id
];
4284 it
->cset_pos
= &css
->cgroup
->cset_links
;
4286 it
->cset_head
= it
->cset_pos
;
4288 css_task_iter_advance_css_set(it
);
4290 spin_unlock_irq(&css_set_lock
);
4294 * css_task_iter_next - return the next task for the iterator
4295 * @it: the task iterator being iterated
4297 * The "next" function for task iteration. @it should have been
4298 * initialized via css_task_iter_start(). Returns NULL when the iteration
4301 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
4304 put_task_struct(it
->cur_task
);
4305 it
->cur_task
= NULL
;
4308 spin_lock_irq(&css_set_lock
);
4311 it
->cur_task
= list_entry(it
->task_pos
, struct task_struct
,
4313 get_task_struct(it
->cur_task
);
4314 css_task_iter_advance(it
);
4317 spin_unlock_irq(&css_set_lock
);
4319 return it
->cur_task
;
4323 * css_task_iter_end - finish task iteration
4324 * @it: the task iterator to finish
4326 * Finish task iteration started by css_task_iter_start().
4328 void css_task_iter_end(struct css_task_iter
*it
)
4331 spin_lock_irq(&css_set_lock
);
4332 list_del(&it
->iters_node
);
4333 put_css_set_locked(it
->cur_cset
);
4334 spin_unlock_irq(&css_set_lock
);
4338 put_css_set(it
->cur_dcset
);
4341 put_task_struct(it
->cur_task
);
4344 static void cgroup_procs_release(struct kernfs_open_file
*of
)
4347 css_task_iter_end(of
->priv
);
4352 static void *cgroup_procs_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
4354 struct kernfs_open_file
*of
= s
->private;
4355 struct css_task_iter
*it
= of
->priv
;
4357 return css_task_iter_next(it
);
4360 static void *__cgroup_procs_start(struct seq_file
*s
, loff_t
*pos
,
4361 unsigned int iter_flags
)
4363 struct kernfs_open_file
*of
= s
->private;
4364 struct cgroup
*cgrp
= seq_css(s
)->cgroup
;
4365 struct css_task_iter
*it
= of
->priv
;
4368 * When a seq_file is seeked, it's always traversed sequentially
4369 * from position 0, so we can simply keep iterating on !0 *pos.
4372 if (WARN_ON_ONCE((*pos
)++))
4373 return ERR_PTR(-EINVAL
);
4375 it
= kzalloc(sizeof(*it
), GFP_KERNEL
);
4377 return ERR_PTR(-ENOMEM
);
4379 css_task_iter_start(&cgrp
->self
, iter_flags
, it
);
4380 } else if (!(*pos
)++) {
4381 css_task_iter_end(it
);
4382 css_task_iter_start(&cgrp
->self
, iter_flags
, it
);
4385 return cgroup_procs_next(s
, NULL
, NULL
);
4388 static void *cgroup_procs_start(struct seq_file
*s
, loff_t
*pos
)
4390 struct cgroup
*cgrp
= seq_css(s
)->cgroup
;
4393 * All processes of a threaded subtree belong to the domain cgroup
4394 * of the subtree. Only threads can be distributed across the
4395 * subtree. Reject reads on cgroup.procs in the subtree proper.
4396 * They're always empty anyway.
4398 if (cgroup_is_threaded(cgrp
))
4399 return ERR_PTR(-EOPNOTSUPP
);
4401 return __cgroup_procs_start(s
, pos
, CSS_TASK_ITER_PROCS
|
4402 CSS_TASK_ITER_THREADED
);
4405 static int cgroup_procs_show(struct seq_file
*s
, void *v
)
4407 seq_printf(s
, "%d\n", task_pid_vnr(v
));
4411 static int cgroup_procs_write_permission(struct cgroup
*src_cgrp
,
4412 struct cgroup
*dst_cgrp
,
4413 struct super_block
*sb
)
4415 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
4416 struct cgroup
*com_cgrp
= src_cgrp
;
4417 struct inode
*inode
;
4420 lockdep_assert_held(&cgroup_mutex
);
4422 /* find the common ancestor */
4423 while (!cgroup_is_descendant(dst_cgrp
, com_cgrp
))
4424 com_cgrp
= cgroup_parent(com_cgrp
);
4426 /* %current should be authorized to migrate to the common ancestor */
4427 inode
= kernfs_get_inode(sb
, com_cgrp
->procs_file
.kn
);
4431 ret
= inode_permission(inode
, MAY_WRITE
);
4437 * If namespaces are delegation boundaries, %current must be able
4438 * to see both source and destination cgroups from its namespace.
4440 if ((cgrp_dfl_root
.flags
& CGRP_ROOT_NS_DELEGATE
) &&
4441 (!cgroup_is_descendant(src_cgrp
, ns
->root_cset
->dfl_cgrp
) ||
4442 !cgroup_is_descendant(dst_cgrp
, ns
->root_cset
->dfl_cgrp
)))
4448 static ssize_t
cgroup_procs_write(struct kernfs_open_file
*of
,
4449 char *buf
, size_t nbytes
, loff_t off
)
4451 struct cgroup
*src_cgrp
, *dst_cgrp
;
4452 struct task_struct
*task
;
4455 dst_cgrp
= cgroup_kn_lock_live(of
->kn
, false);
4459 task
= cgroup_procs_write_start(buf
, true);
4460 ret
= PTR_ERR_OR_ZERO(task
);
4464 /* find the source cgroup */
4465 spin_lock_irq(&css_set_lock
);
4466 src_cgrp
= task_cgroup_from_root(task
, &cgrp_dfl_root
);
4467 spin_unlock_irq(&css_set_lock
);
4469 ret
= cgroup_procs_write_permission(src_cgrp
, dst_cgrp
,
4470 of
->file
->f_path
.dentry
->d_sb
);
4474 ret
= cgroup_attach_task(dst_cgrp
, task
, true);
4477 cgroup_procs_write_finish(task
);
4479 cgroup_kn_unlock(of
->kn
);
4481 return ret
?: nbytes
;
4484 static void *cgroup_threads_start(struct seq_file
*s
, loff_t
*pos
)
4486 return __cgroup_procs_start(s
, pos
, 0);
4489 static ssize_t
cgroup_threads_write(struct kernfs_open_file
*of
,
4490 char *buf
, size_t nbytes
, loff_t off
)
4492 struct cgroup
*src_cgrp
, *dst_cgrp
;
4493 struct task_struct
*task
;
4496 buf
= strstrip(buf
);
4498 dst_cgrp
= cgroup_kn_lock_live(of
->kn
, false);
4502 task
= cgroup_procs_write_start(buf
, false);
4503 ret
= PTR_ERR_OR_ZERO(task
);
4507 /* find the source cgroup */
4508 spin_lock_irq(&css_set_lock
);
4509 src_cgrp
= task_cgroup_from_root(task
, &cgrp_dfl_root
);
4510 spin_unlock_irq(&css_set_lock
);
4512 /* thread migrations follow the cgroup.procs delegation rule */
4513 ret
= cgroup_procs_write_permission(src_cgrp
, dst_cgrp
,
4514 of
->file
->f_path
.dentry
->d_sb
);
4518 /* and must be contained in the same domain */
4520 if (src_cgrp
->dom_cgrp
!= dst_cgrp
->dom_cgrp
)
4523 ret
= cgroup_attach_task(dst_cgrp
, task
, false);
4526 cgroup_procs_write_finish(task
);
4528 cgroup_kn_unlock(of
->kn
);
4530 return ret
?: nbytes
;
4533 /* cgroup core interface files for the default hierarchy */
4534 static struct cftype cgroup_base_files
[] = {
4536 .name
= "cgroup.type",
4537 .flags
= CFTYPE_NOT_ON_ROOT
,
4538 .seq_show
= cgroup_type_show
,
4539 .write
= cgroup_type_write
,
4542 .name
= "cgroup.procs",
4543 .flags
= CFTYPE_NS_DELEGATABLE
,
4544 .file_offset
= offsetof(struct cgroup
, procs_file
),
4545 .release
= cgroup_procs_release
,
4546 .seq_start
= cgroup_procs_start
,
4547 .seq_next
= cgroup_procs_next
,
4548 .seq_show
= cgroup_procs_show
,
4549 .write
= cgroup_procs_write
,
4552 .name
= "cgroup.threads",
4553 .flags
= CFTYPE_NS_DELEGATABLE
,
4554 .release
= cgroup_procs_release
,
4555 .seq_start
= cgroup_threads_start
,
4556 .seq_next
= cgroup_procs_next
,
4557 .seq_show
= cgroup_procs_show
,
4558 .write
= cgroup_threads_write
,
4561 .name
= "cgroup.controllers",
4562 .seq_show
= cgroup_controllers_show
,
4565 .name
= "cgroup.subtree_control",
4566 .flags
= CFTYPE_NS_DELEGATABLE
,
4567 .seq_show
= cgroup_subtree_control_show
,
4568 .write
= cgroup_subtree_control_write
,
4571 .name
= "cgroup.events",
4572 .flags
= CFTYPE_NOT_ON_ROOT
,
4573 .file_offset
= offsetof(struct cgroup
, events_file
),
4574 .seq_show
= cgroup_events_show
,
4577 .name
= "cgroup.max.descendants",
4578 .seq_show
= cgroup_max_descendants_show
,
4579 .write
= cgroup_max_descendants_write
,
4582 .name
= "cgroup.max.depth",
4583 .seq_show
= cgroup_max_depth_show
,
4584 .write
= cgroup_max_depth_write
,
4587 .name
= "cgroup.stat",
4588 .seq_show
= cgroup_stat_show
,
4592 .flags
= CFTYPE_NOT_ON_ROOT
,
4593 .seq_show
= cpu_stat_show
,
4597 .name
= "io.pressure",
4598 .flags
= CFTYPE_NOT_ON_ROOT
,
4599 .seq_show
= cgroup_io_pressure_show
,
4602 .name
= "memory.pressure",
4603 .flags
= CFTYPE_NOT_ON_ROOT
,
4604 .seq_show
= cgroup_memory_pressure_show
,
4607 .name
= "cpu.pressure",
4608 .flags
= CFTYPE_NOT_ON_ROOT
,
4609 .seq_show
= cgroup_cpu_pressure_show
,
4616 * css destruction is four-stage process.
4618 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4619 * Implemented in kill_css().
4621 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4622 * and thus css_tryget_online() is guaranteed to fail, the css can be
4623 * offlined by invoking offline_css(). After offlining, the base ref is
4624 * put. Implemented in css_killed_work_fn().
4626 * 3. When the percpu_ref reaches zero, the only possible remaining
4627 * accessors are inside RCU read sections. css_release() schedules the
4630 * 4. After the grace period, the css can be freed. Implemented in
4631 * css_free_work_fn().
4633 * It is actually hairier because both step 2 and 4 require process context
4634 * and thus involve punting to css->destroy_work adding two additional
4635 * steps to the already complex sequence.
4637 static void css_free_rwork_fn(struct work_struct
*work
)
4639 struct cgroup_subsys_state
*css
= container_of(to_rcu_work(work
),
4640 struct cgroup_subsys_state
, destroy_rwork
);
4641 struct cgroup_subsys
*ss
= css
->ss
;
4642 struct cgroup
*cgrp
= css
->cgroup
;
4644 percpu_ref_exit(&css
->refcnt
);
4648 struct cgroup_subsys_state
*parent
= css
->parent
;
4652 cgroup_idr_remove(&ss
->css_idr
, id
);
4658 /* cgroup free path */
4659 atomic_dec(&cgrp
->root
->nr_cgrps
);
4660 cgroup1_pidlist_destroy_all(cgrp
);
4661 cancel_work_sync(&cgrp
->release_agent_work
);
4663 if (cgroup_parent(cgrp
)) {
4665 * We get a ref to the parent, and put the ref when
4666 * this cgroup is being freed, so it's guaranteed
4667 * that the parent won't be destroyed before its
4670 cgroup_put(cgroup_parent(cgrp
));
4671 kernfs_put(cgrp
->kn
);
4672 psi_cgroup_free(cgrp
);
4673 if (cgroup_on_dfl(cgrp
))
4674 cgroup_rstat_exit(cgrp
);
4678 * This is root cgroup's refcnt reaching zero,
4679 * which indicates that the root should be
4682 cgroup_destroy_root(cgrp
->root
);
4687 static void css_release_work_fn(struct work_struct
*work
)
4689 struct cgroup_subsys_state
*css
=
4690 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4691 struct cgroup_subsys
*ss
= css
->ss
;
4692 struct cgroup
*cgrp
= css
->cgroup
;
4694 mutex_lock(&cgroup_mutex
);
4696 css
->flags
|= CSS_RELEASED
;
4697 list_del_rcu(&css
->sibling
);
4700 /* css release path */
4701 if (!list_empty(&css
->rstat_css_node
)) {
4702 cgroup_rstat_flush(cgrp
);
4703 list_del_rcu(&css
->rstat_css_node
);
4706 cgroup_idr_replace(&ss
->css_idr
, NULL
, css
->id
);
4707 if (ss
->css_released
)
4708 ss
->css_released(css
);
4710 struct cgroup
*tcgrp
;
4712 /* cgroup release path */
4713 TRACE_CGROUP_PATH(release
, cgrp
);
4715 if (cgroup_on_dfl(cgrp
))
4716 cgroup_rstat_flush(cgrp
);
4718 for (tcgrp
= cgroup_parent(cgrp
); tcgrp
;
4719 tcgrp
= cgroup_parent(tcgrp
))
4720 tcgrp
->nr_dying_descendants
--;
4722 cgroup_idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
4726 * There are two control paths which try to determine
4727 * cgroup from dentry without going through kernfs -
4728 * cgroupstats_build() and css_tryget_online_from_dir().
4729 * Those are supported by RCU protecting clearing of
4730 * cgrp->kn->priv backpointer.
4733 RCU_INIT_POINTER(*(void __rcu __force
**)&cgrp
->kn
->priv
,
4736 cgroup_bpf_put(cgrp
);
4739 mutex_unlock(&cgroup_mutex
);
4741 INIT_RCU_WORK(&css
->destroy_rwork
, css_free_rwork_fn
);
4742 queue_rcu_work(cgroup_destroy_wq
, &css
->destroy_rwork
);
4745 static void css_release(struct percpu_ref
*ref
)
4747 struct cgroup_subsys_state
*css
=
4748 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4750 INIT_WORK(&css
->destroy_work
, css_release_work_fn
);
4751 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4754 static void init_and_link_css(struct cgroup_subsys_state
*css
,
4755 struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4757 lockdep_assert_held(&cgroup_mutex
);
4759 cgroup_get_live(cgrp
);
4761 memset(css
, 0, sizeof(*css
));
4765 INIT_LIST_HEAD(&css
->sibling
);
4766 INIT_LIST_HEAD(&css
->children
);
4767 INIT_LIST_HEAD(&css
->rstat_css_node
);
4768 css
->serial_nr
= css_serial_nr_next
++;
4769 atomic_set(&css
->online_cnt
, 0);
4771 if (cgroup_parent(cgrp
)) {
4772 css
->parent
= cgroup_css(cgroup_parent(cgrp
), ss
);
4773 css_get(css
->parent
);
4776 if (cgroup_on_dfl(cgrp
) && ss
->css_rstat_flush
)
4777 list_add_rcu(&css
->rstat_css_node
, &cgrp
->rstat_css_list
);
4779 BUG_ON(cgroup_css(cgrp
, ss
));
4782 /* invoke ->css_online() on a new CSS and mark it online if successful */
4783 static int online_css(struct cgroup_subsys_state
*css
)
4785 struct cgroup_subsys
*ss
= css
->ss
;
4788 lockdep_assert_held(&cgroup_mutex
);
4791 ret
= ss
->css_online(css
);
4793 css
->flags
|= CSS_ONLINE
;
4794 rcu_assign_pointer(css
->cgroup
->subsys
[ss
->id
], css
);
4796 atomic_inc(&css
->online_cnt
);
4798 atomic_inc(&css
->parent
->online_cnt
);
4803 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4804 static void offline_css(struct cgroup_subsys_state
*css
)
4806 struct cgroup_subsys
*ss
= css
->ss
;
4808 lockdep_assert_held(&cgroup_mutex
);
4810 if (!(css
->flags
& CSS_ONLINE
))
4813 if (ss
->css_offline
)
4814 ss
->css_offline(css
);
4816 css
->flags
&= ~CSS_ONLINE
;
4817 RCU_INIT_POINTER(css
->cgroup
->subsys
[ss
->id
], NULL
);
4819 wake_up_all(&css
->cgroup
->offline_waitq
);
4823 * css_create - create a cgroup_subsys_state
4824 * @cgrp: the cgroup new css will be associated with
4825 * @ss: the subsys of new css
4827 * Create a new css associated with @cgrp - @ss pair. On success, the new
4828 * css is online and installed in @cgrp. This function doesn't create the
4829 * interface files. Returns 0 on success, -errno on failure.
4831 static struct cgroup_subsys_state
*css_create(struct cgroup
*cgrp
,
4832 struct cgroup_subsys
*ss
)
4834 struct cgroup
*parent
= cgroup_parent(cgrp
);
4835 struct cgroup_subsys_state
*parent_css
= cgroup_css(parent
, ss
);
4836 struct cgroup_subsys_state
*css
;
4839 lockdep_assert_held(&cgroup_mutex
);
4841 css
= ss
->css_alloc(parent_css
);
4843 css
= ERR_PTR(-ENOMEM
);
4847 init_and_link_css(css
, ss
, cgrp
);
4849 err
= percpu_ref_init(&css
->refcnt
, css_release
, 0, GFP_KERNEL
);
4853 err
= cgroup_idr_alloc(&ss
->css_idr
, NULL
, 2, 0, GFP_KERNEL
);
4858 /* @css is ready to be brought online now, make it visible */
4859 list_add_tail_rcu(&css
->sibling
, &parent_css
->children
);
4860 cgroup_idr_replace(&ss
->css_idr
, css
, css
->id
);
4862 err
= online_css(css
);
4866 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4867 cgroup_parent(parent
)) {
4868 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4869 current
->comm
, current
->pid
, ss
->name
);
4870 if (!strcmp(ss
->name
, "memory"))
4871 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
4872 ss
->warned_broken_hierarchy
= true;
4878 list_del_rcu(&css
->sibling
);
4880 list_del_rcu(&css
->rstat_css_node
);
4881 INIT_RCU_WORK(&css
->destroy_rwork
, css_free_rwork_fn
);
4882 queue_rcu_work(cgroup_destroy_wq
, &css
->destroy_rwork
);
4883 return ERR_PTR(err
);
4887 * The returned cgroup is fully initialized including its control mask, but
4888 * it isn't associated with its kernfs_node and doesn't have the control
4891 static struct cgroup
*cgroup_create(struct cgroup
*parent
)
4893 struct cgroup_root
*root
= parent
->root
;
4894 struct cgroup
*cgrp
, *tcgrp
;
4895 int level
= parent
->level
+ 1;
4898 /* allocate the cgroup and its ID, 0 is reserved for the root */
4899 cgrp
= kzalloc(struct_size(cgrp
, ancestor_ids
, (level
+ 1)),
4902 return ERR_PTR(-ENOMEM
);
4904 ret
= percpu_ref_init(&cgrp
->self
.refcnt
, css_release
, 0, GFP_KERNEL
);
4908 if (cgroup_on_dfl(parent
)) {
4909 ret
= cgroup_rstat_init(cgrp
);
4911 goto out_cancel_ref
;
4915 * Temporarily set the pointer to NULL, so idr_find() won't return
4916 * a half-baked cgroup.
4918 cgrp
->id
= cgroup_idr_alloc(&root
->cgroup_idr
, NULL
, 2, 0, GFP_KERNEL
);
4924 init_cgroup_housekeeping(cgrp
);
4926 cgrp
->self
.parent
= &parent
->self
;
4928 cgrp
->level
= level
;
4930 ret
= psi_cgroup_alloc(cgrp
);
4934 ret
= cgroup_bpf_inherit(cgrp
);
4938 for (tcgrp
= cgrp
; tcgrp
; tcgrp
= cgroup_parent(tcgrp
)) {
4939 cgrp
->ancestor_ids
[tcgrp
->level
] = tcgrp
->id
;
4942 tcgrp
->nr_descendants
++;
4945 if (notify_on_release(parent
))
4946 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4948 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4949 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4951 cgrp
->self
.serial_nr
= css_serial_nr_next
++;
4953 /* allocation complete, commit to creation */
4954 list_add_tail_rcu(&cgrp
->self
.sibling
, &cgroup_parent(cgrp
)->self
.children
);
4955 atomic_inc(&root
->nr_cgrps
);
4956 cgroup_get_live(parent
);
4959 * @cgrp is now fully operational. If something fails after this
4960 * point, it'll be released via the normal destruction path.
4962 cgroup_idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
4965 * On the default hierarchy, a child doesn't automatically inherit
4966 * subtree_control from the parent. Each is configured manually.
4968 if (!cgroup_on_dfl(cgrp
))
4969 cgrp
->subtree_control
= cgroup_control(cgrp
);
4971 cgroup_propagate_control(cgrp
);
4976 psi_cgroup_free(cgrp
);
4978 cgroup_idr_remove(&root
->cgroup_idr
, cgrp
->id
);
4980 if (cgroup_on_dfl(parent
))
4981 cgroup_rstat_exit(cgrp
);
4983 percpu_ref_exit(&cgrp
->self
.refcnt
);
4986 return ERR_PTR(ret
);
4989 static bool cgroup_check_hierarchy_limits(struct cgroup
*parent
)
4991 struct cgroup
*cgroup
;
4995 lockdep_assert_held(&cgroup_mutex
);
4997 for (cgroup
= parent
; cgroup
; cgroup
= cgroup_parent(cgroup
)) {
4998 if (cgroup
->nr_descendants
>= cgroup
->max_descendants
)
5001 if (level
> cgroup
->max_depth
)
5012 int cgroup_mkdir(struct kernfs_node
*parent_kn
, const char *name
, umode_t mode
)
5014 struct cgroup
*parent
, *cgrp
;
5015 struct kernfs_node
*kn
;
5018 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
5019 if (strchr(name
, '\n'))
5022 parent
= cgroup_kn_lock_live(parent_kn
, false);
5026 if (!cgroup_check_hierarchy_limits(parent
)) {
5031 cgrp
= cgroup_create(parent
);
5033 ret
= PTR_ERR(cgrp
);
5037 /* create the directory */
5038 kn
= kernfs_create_dir(parent
->kn
, name
, mode
, cgrp
);
5046 * This extra ref will be put in cgroup_free_fn() and guarantees
5047 * that @cgrp->kn is always accessible.
5051 ret
= cgroup_kn_set_ugid(kn
);
5055 ret
= css_populate_dir(&cgrp
->self
);
5059 ret
= cgroup_apply_control_enable(cgrp
);
5063 TRACE_CGROUP_PATH(mkdir
, cgrp
);
5065 /* let's create and online css's */
5066 kernfs_activate(kn
);
5072 cgroup_destroy_locked(cgrp
);
5074 cgroup_kn_unlock(parent_kn
);
5079 * This is called when the refcnt of a css is confirmed to be killed.
5080 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5081 * initate destruction and put the css ref from kill_css().
5083 static void css_killed_work_fn(struct work_struct
*work
)
5085 struct cgroup_subsys_state
*css
=
5086 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
5088 mutex_lock(&cgroup_mutex
);
5093 /* @css can't go away while we're holding cgroup_mutex */
5095 } while (css
&& atomic_dec_and_test(&css
->online_cnt
));
5097 mutex_unlock(&cgroup_mutex
);
5100 /* css kill confirmation processing requires process context, bounce */
5101 static void css_killed_ref_fn(struct percpu_ref
*ref
)
5103 struct cgroup_subsys_state
*css
=
5104 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
5106 if (atomic_dec_and_test(&css
->online_cnt
)) {
5107 INIT_WORK(&css
->destroy_work
, css_killed_work_fn
);
5108 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
5113 * kill_css - destroy a css
5114 * @css: css to destroy
5116 * This function initiates destruction of @css by removing cgroup interface
5117 * files and putting its base reference. ->css_offline() will be invoked
5118 * asynchronously once css_tryget_online() is guaranteed to fail and when
5119 * the reference count reaches zero, @css will be released.
5121 static void kill_css(struct cgroup_subsys_state
*css
)
5123 lockdep_assert_held(&cgroup_mutex
);
5125 if (css
->flags
& CSS_DYING
)
5128 css
->flags
|= CSS_DYING
;
5131 * This must happen before css is disassociated with its cgroup.
5132 * See seq_css() for details.
5137 * Killing would put the base ref, but we need to keep it alive
5138 * until after ->css_offline().
5143 * cgroup core guarantees that, by the time ->css_offline() is
5144 * invoked, no new css reference will be given out via
5145 * css_tryget_online(). We can't simply call percpu_ref_kill() and
5146 * proceed to offlining css's because percpu_ref_kill() doesn't
5147 * guarantee that the ref is seen as killed on all CPUs on return.
5149 * Use percpu_ref_kill_and_confirm() to get notifications as each
5150 * css is confirmed to be seen as killed on all CPUs.
5152 percpu_ref_kill_and_confirm(&css
->refcnt
, css_killed_ref_fn
);
5156 * cgroup_destroy_locked - the first stage of cgroup destruction
5157 * @cgrp: cgroup to be destroyed
5159 * css's make use of percpu refcnts whose killing latency shouldn't be
5160 * exposed to userland and are RCU protected. Also, cgroup core needs to
5161 * guarantee that css_tryget_online() won't succeed by the time
5162 * ->css_offline() is invoked. To satisfy all the requirements,
5163 * destruction is implemented in the following two steps.
5165 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5166 * userland visible parts and start killing the percpu refcnts of
5167 * css's. Set up so that the next stage will be kicked off once all
5168 * the percpu refcnts are confirmed to be killed.
5170 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5171 * rest of destruction. Once all cgroup references are gone, the
5172 * cgroup is RCU-freed.
5174 * This function implements s1. After this step, @cgrp is gone as far as
5175 * the userland is concerned and a new cgroup with the same name may be
5176 * created. As cgroup doesn't care about the names internally, this
5177 * doesn't cause any problem.
5179 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
5180 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
5182 struct cgroup
*tcgrp
, *parent
= cgroup_parent(cgrp
);
5183 struct cgroup_subsys_state
*css
;
5184 struct cgrp_cset_link
*link
;
5187 lockdep_assert_held(&cgroup_mutex
);
5190 * Only migration can raise populated from zero and we're already
5191 * holding cgroup_mutex.
5193 if (cgroup_is_populated(cgrp
))
5197 * Make sure there's no live children. We can't test emptiness of
5198 * ->self.children as dead children linger on it while being
5199 * drained; otherwise, "rmdir parent/child parent" may fail.
5201 if (css_has_online_children(&cgrp
->self
))
5205 * Mark @cgrp and the associated csets dead. The former prevents
5206 * further task migration and child creation by disabling
5207 * cgroup_lock_live_group(). The latter makes the csets ignored by
5208 * the migration path.
5210 cgrp
->self
.flags
&= ~CSS_ONLINE
;
5212 spin_lock_irq(&css_set_lock
);
5213 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
5214 link
->cset
->dead
= true;
5215 spin_unlock_irq(&css_set_lock
);
5217 /* initiate massacre of all css's */
5218 for_each_css(css
, ssid
, cgrp
)
5221 /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
5222 css_clear_dir(&cgrp
->self
);
5223 kernfs_remove(cgrp
->kn
);
5225 if (parent
&& cgroup_is_threaded(cgrp
))
5226 parent
->nr_threaded_children
--;
5228 for (tcgrp
= cgroup_parent(cgrp
); tcgrp
; tcgrp
= cgroup_parent(tcgrp
)) {
5229 tcgrp
->nr_descendants
--;
5230 tcgrp
->nr_dying_descendants
++;
5233 cgroup1_check_for_release(parent
);
5235 /* put the base reference */
5236 percpu_ref_kill(&cgrp
->self
.refcnt
);
5241 int cgroup_rmdir(struct kernfs_node
*kn
)
5243 struct cgroup
*cgrp
;
5246 cgrp
= cgroup_kn_lock_live(kn
, false);
5250 ret
= cgroup_destroy_locked(cgrp
);
5252 TRACE_CGROUP_PATH(rmdir
, cgrp
);
5254 cgroup_kn_unlock(kn
);
5258 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
= {
5259 .show_options
= cgroup_show_options
,
5260 .remount_fs
= cgroup_remount
,
5261 .mkdir
= cgroup_mkdir
,
5262 .rmdir
= cgroup_rmdir
,
5263 .show_path
= cgroup_show_path
,
5266 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
, bool early
)
5268 struct cgroup_subsys_state
*css
;
5270 pr_debug("Initializing cgroup subsys %s\n", ss
->name
);
5272 mutex_lock(&cgroup_mutex
);
5274 idr_init(&ss
->css_idr
);
5275 INIT_LIST_HEAD(&ss
->cfts
);
5277 /* Create the root cgroup state for this subsystem */
5278 ss
->root
= &cgrp_dfl_root
;
5279 css
= ss
->css_alloc(cgroup_css(&cgrp_dfl_root
.cgrp
, ss
));
5280 /* We don't handle early failures gracefully */
5281 BUG_ON(IS_ERR(css
));
5282 init_and_link_css(css
, ss
, &cgrp_dfl_root
.cgrp
);
5285 * Root csses are never destroyed and we can't initialize
5286 * percpu_ref during early init. Disable refcnting.
5288 css
->flags
|= CSS_NO_REF
;
5291 /* allocation can't be done safely during early init */
5294 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2, GFP_KERNEL
);
5295 BUG_ON(css
->id
< 0);
5298 /* Update the init_css_set to contain a subsys
5299 * pointer to this state - since the subsystem is
5300 * newly registered, all tasks and hence the
5301 * init_css_set is in the subsystem's root cgroup. */
5302 init_css_set
.subsys
[ss
->id
] = css
;
5304 have_fork_callback
|= (bool)ss
->fork
<< ss
->id
;
5305 have_exit_callback
|= (bool)ss
->exit
<< ss
->id
;
5306 have_free_callback
|= (bool)ss
->free
<< ss
->id
;
5307 have_canfork_callback
|= (bool)ss
->can_fork
<< ss
->id
;
5309 /* At system boot, before all subsystems have been
5310 * registered, no tasks have been forked, so we don't
5311 * need to invoke fork callbacks here. */
5312 BUG_ON(!list_empty(&init_task
.tasks
));
5314 BUG_ON(online_css(css
));
5316 mutex_unlock(&cgroup_mutex
);
5320 * cgroup_init_early - cgroup initialization at system boot
5322 * Initialize cgroups at system boot, and initialize any
5323 * subsystems that request early init.
5325 int __init
cgroup_init_early(void)
5327 static struct cgroup_sb_opts __initdata opts
;
5328 struct cgroup_subsys
*ss
;
5331 init_cgroup_root(&cgrp_dfl_root
, &opts
);
5332 cgrp_dfl_root
.cgrp
.self
.flags
|= CSS_NO_REF
;
5334 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
5336 for_each_subsys(ss
, i
) {
5337 WARN(!ss
->css_alloc
|| !ss
->css_free
|| ss
->name
|| ss
->id
,
5338 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
5339 i
, cgroup_subsys_name
[i
], ss
->css_alloc
, ss
->css_free
,
5341 WARN(strlen(cgroup_subsys_name
[i
]) > MAX_CGROUP_TYPE_NAMELEN
,
5342 "cgroup_subsys_name %s too long\n", cgroup_subsys_name
[i
]);
5345 ss
->name
= cgroup_subsys_name
[i
];
5346 if (!ss
->legacy_name
)
5347 ss
->legacy_name
= cgroup_subsys_name
[i
];
5350 cgroup_init_subsys(ss
, true);
5355 static u16 cgroup_disable_mask __initdata
;
5358 * cgroup_init - cgroup initialization
5360 * Register cgroup filesystem and /proc file, and initialize
5361 * any subsystems that didn't request early init.
5363 int __init
cgroup_init(void)
5365 struct cgroup_subsys
*ss
;
5368 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT
> 16);
5369 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem
));
5370 BUG_ON(cgroup_init_cftypes(NULL
, cgroup_base_files
));
5371 BUG_ON(cgroup_init_cftypes(NULL
, cgroup1_base_files
));
5373 cgroup_rstat_boot();
5376 * The latency of the synchronize_sched() is too high for cgroups,
5377 * avoid it at the cost of forcing all readers into the slow path.
5379 rcu_sync_enter_start(&cgroup_threadgroup_rwsem
.rss
);
5381 get_user_ns(init_cgroup_ns
.user_ns
);
5383 mutex_lock(&cgroup_mutex
);
5386 * Add init_css_set to the hash table so that dfl_root can link to
5389 hash_add(css_set_table
, &init_css_set
.hlist
,
5390 css_set_hash(init_css_set
.subsys
));
5392 BUG_ON(cgroup_setup_root(&cgrp_dfl_root
, 0, 0));
5394 mutex_unlock(&cgroup_mutex
);
5396 for_each_subsys(ss
, ssid
) {
5397 if (ss
->early_init
) {
5398 struct cgroup_subsys_state
*css
=
5399 init_css_set
.subsys
[ss
->id
];
5401 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2,
5403 BUG_ON(css
->id
< 0);
5405 cgroup_init_subsys(ss
, false);
5408 list_add_tail(&init_css_set
.e_cset_node
[ssid
],
5409 &cgrp_dfl_root
.cgrp
.e_csets
[ssid
]);
5412 * Setting dfl_root subsys_mask needs to consider the
5413 * disabled flag and cftype registration needs kmalloc,
5414 * both of which aren't available during early_init.
5416 if (cgroup_disable_mask
& (1 << ssid
)) {
5417 static_branch_disable(cgroup_subsys_enabled_key
[ssid
]);
5418 printk(KERN_INFO
"Disabling %s control group subsystem\n",
5423 if (cgroup1_ssid_disabled(ssid
))
5424 printk(KERN_INFO
"Disabling %s control group subsystem in v1 mounts\n",
5427 cgrp_dfl_root
.subsys_mask
|= 1 << ss
->id
;
5429 /* implicit controllers must be threaded too */
5430 WARN_ON(ss
->implicit_on_dfl
&& !ss
->threaded
);
5432 if (ss
->implicit_on_dfl
)
5433 cgrp_dfl_implicit_ss_mask
|= 1 << ss
->id
;
5434 else if (!ss
->dfl_cftypes
)
5435 cgrp_dfl_inhibit_ss_mask
|= 1 << ss
->id
;
5438 cgrp_dfl_threaded_ss_mask
|= 1 << ss
->id
;
5440 if (ss
->dfl_cftypes
== ss
->legacy_cftypes
) {
5441 WARN_ON(cgroup_add_cftypes(ss
, ss
->dfl_cftypes
));
5443 WARN_ON(cgroup_add_dfl_cftypes(ss
, ss
->dfl_cftypes
));
5444 WARN_ON(cgroup_add_legacy_cftypes(ss
, ss
->legacy_cftypes
));
5448 ss
->bind(init_css_set
.subsys
[ssid
]);
5450 mutex_lock(&cgroup_mutex
);
5451 css_populate_dir(init_css_set
.subsys
[ssid
]);
5452 mutex_unlock(&cgroup_mutex
);
5455 /* init_css_set.subsys[] has been updated, re-hash */
5456 hash_del(&init_css_set
.hlist
);
5457 hash_add(css_set_table
, &init_css_set
.hlist
,
5458 css_set_hash(init_css_set
.subsys
));
5460 WARN_ON(sysfs_create_mount_point(fs_kobj
, "cgroup"));
5461 WARN_ON(register_filesystem(&cgroup_fs_type
));
5462 WARN_ON(register_filesystem(&cgroup2_fs_type
));
5463 WARN_ON(!proc_create_single("cgroups", 0, NULL
, proc_cgroupstats_show
));
5468 static int __init
cgroup_wq_init(void)
5471 * There isn't much point in executing destruction path in
5472 * parallel. Good chunk is serialized with cgroup_mutex anyway.
5473 * Use 1 for @max_active.
5475 * We would prefer to do this in cgroup_init() above, but that
5476 * is called before init_workqueues(): so leave this until after.
5478 cgroup_destroy_wq
= alloc_workqueue("cgroup_destroy", 0, 1);
5479 BUG_ON(!cgroup_destroy_wq
);
5482 core_initcall(cgroup_wq_init
);
5484 void cgroup_path_from_kernfs_id(const union kernfs_node_id
*id
,
5485 char *buf
, size_t buflen
)
5487 struct kernfs_node
*kn
;
5489 kn
= kernfs_get_node_by_id(cgrp_dfl_root
.kf_root
, id
);
5492 kernfs_path(kn
, buf
, buflen
);
5497 * proc_cgroup_show()
5498 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5499 * - Used for /proc/<pid>/cgroup.
5501 int proc_cgroup_show(struct seq_file
*m
, struct pid_namespace
*ns
,
5502 struct pid
*pid
, struct task_struct
*tsk
)
5506 struct cgroup_root
*root
;
5509 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
5513 mutex_lock(&cgroup_mutex
);
5514 spin_lock_irq(&css_set_lock
);
5516 for_each_root(root
) {
5517 struct cgroup_subsys
*ss
;
5518 struct cgroup
*cgrp
;
5519 int ssid
, count
= 0;
5521 if (root
== &cgrp_dfl_root
&& !cgrp_dfl_visible
)
5524 seq_printf(m
, "%d:", root
->hierarchy_id
);
5525 if (root
!= &cgrp_dfl_root
)
5526 for_each_subsys(ss
, ssid
)
5527 if (root
->subsys_mask
& (1 << ssid
))
5528 seq_printf(m
, "%s%s", count
++ ? "," : "",
5530 if (strlen(root
->name
))
5531 seq_printf(m
, "%sname=%s", count
? "," : "",
5535 cgrp
= task_cgroup_from_root(tsk
, root
);
5538 * On traditional hierarchies, all zombie tasks show up as
5539 * belonging to the root cgroup. On the default hierarchy,
5540 * while a zombie doesn't show up in "cgroup.procs" and
5541 * thus can't be migrated, its /proc/PID/cgroup keeps
5542 * reporting the cgroup it belonged to before exiting. If
5543 * the cgroup is removed before the zombie is reaped,
5544 * " (deleted)" is appended to the cgroup path.
5546 if (cgroup_on_dfl(cgrp
) || !(tsk
->flags
& PF_EXITING
)) {
5547 retval
= cgroup_path_ns_locked(cgrp
, buf
, PATH_MAX
,
5548 current
->nsproxy
->cgroup_ns
);
5549 if (retval
>= PATH_MAX
)
5550 retval
= -ENAMETOOLONG
;
5559 if (cgroup_on_dfl(cgrp
) && cgroup_is_dead(cgrp
))
5560 seq_puts(m
, " (deleted)\n");
5567 spin_unlock_irq(&css_set_lock
);
5568 mutex_unlock(&cgroup_mutex
);
5575 * cgroup_fork - initialize cgroup related fields during copy_process()
5576 * @child: pointer to task_struct of forking parent process.
5578 * A task is associated with the init_css_set until cgroup_post_fork()
5579 * attaches it to the parent's css_set. Empty cg_list indicates that
5580 * @child isn't holding reference to its css_set.
5582 void cgroup_fork(struct task_struct
*child
)
5584 RCU_INIT_POINTER(child
->cgroups
, &init_css_set
);
5585 INIT_LIST_HEAD(&child
->cg_list
);
5589 * cgroup_can_fork - called on a new task before the process is exposed
5590 * @child: the task in question.
5592 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5593 * returns an error, the fork aborts with that error code. This allows for
5594 * a cgroup subsystem to conditionally allow or deny new forks.
5596 int cgroup_can_fork(struct task_struct
*child
)
5598 struct cgroup_subsys
*ss
;
5601 do_each_subsys_mask(ss
, i
, have_canfork_callback
) {
5602 ret
= ss
->can_fork(child
);
5605 } while_each_subsys_mask();
5610 for_each_subsys(ss
, j
) {
5613 if (ss
->cancel_fork
)
5614 ss
->cancel_fork(child
);
5621 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5622 * @child: the task in question
5624 * This calls the cancel_fork() callbacks if a fork failed *after*
5625 * cgroup_can_fork() succeded.
5627 void cgroup_cancel_fork(struct task_struct
*child
)
5629 struct cgroup_subsys
*ss
;
5632 for_each_subsys(ss
, i
)
5633 if (ss
->cancel_fork
)
5634 ss
->cancel_fork(child
);
5638 * cgroup_post_fork - called on a new task after adding it to the task list
5639 * @child: the task in question
5641 * Adds the task to the list running through its css_set if necessary and
5642 * call the subsystem fork() callbacks. Has to be after the task is
5643 * visible on the task list in case we race with the first call to
5644 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5647 void cgroup_post_fork(struct task_struct
*child
)
5649 struct cgroup_subsys
*ss
;
5653 * This may race against cgroup_enable_task_cg_lists(). As that
5654 * function sets use_task_css_set_links before grabbing
5655 * tasklist_lock and we just went through tasklist_lock to add
5656 * @child, it's guaranteed that either we see the set
5657 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5658 * @child during its iteration.
5660 * If we won the race, @child is associated with %current's
5661 * css_set. Grabbing css_set_lock guarantees both that the
5662 * association is stable, and, on completion of the parent's
5663 * migration, @child is visible in the source of migration or
5664 * already in the destination cgroup. This guarantee is necessary
5665 * when implementing operations which need to migrate all tasks of
5666 * a cgroup to another.
5668 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
5669 * will remain in init_css_set. This is safe because all tasks are
5670 * in the init_css_set before cg_links is enabled and there's no
5671 * operation which transfers all tasks out of init_css_set.
5673 if (use_task_css_set_links
) {
5674 struct css_set
*cset
;
5676 spin_lock_irq(&css_set_lock
);
5677 cset
= task_css_set(current
);
5678 if (list_empty(&child
->cg_list
)) {
5681 css_set_move_task(child
, NULL
, cset
, false);
5683 spin_unlock_irq(&css_set_lock
);
5687 * Call ss->fork(). This must happen after @child is linked on
5688 * css_set; otherwise, @child might change state between ->fork()
5689 * and addition to css_set.
5691 do_each_subsys_mask(ss
, i
, have_fork_callback
) {
5693 } while_each_subsys_mask();
5697 * cgroup_exit - detach cgroup from exiting task
5698 * @tsk: pointer to task_struct of exiting process
5700 * Description: Detach cgroup from @tsk and release it.
5702 * Note that cgroups marked notify_on_release force every task in
5703 * them to take the global cgroup_mutex mutex when exiting.
5704 * This could impact scaling on very large systems. Be reluctant to
5705 * use notify_on_release cgroups where very high task exit scaling
5706 * is required on large systems.
5708 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5709 * call cgroup_exit() while the task is still competent to handle
5710 * notify_on_release(), then leave the task attached to the root cgroup in
5711 * each hierarchy for the remainder of its exit. No need to bother with
5712 * init_css_set refcnting. init_css_set never goes away and we can't race
5713 * with migration path - PF_EXITING is visible to migration path.
5715 void cgroup_exit(struct task_struct
*tsk
)
5717 struct cgroup_subsys
*ss
;
5718 struct css_set
*cset
;
5722 * Unlink from @tsk from its css_set. As migration path can't race
5723 * with us, we can check css_set and cg_list without synchronization.
5725 cset
= task_css_set(tsk
);
5727 if (!list_empty(&tsk
->cg_list
)) {
5728 spin_lock_irq(&css_set_lock
);
5729 css_set_move_task(tsk
, cset
, NULL
, false);
5731 spin_unlock_irq(&css_set_lock
);
5736 /* see cgroup_post_fork() for details */
5737 do_each_subsys_mask(ss
, i
, have_exit_callback
) {
5739 } while_each_subsys_mask();
5742 void cgroup_free(struct task_struct
*task
)
5744 struct css_set
*cset
= task_css_set(task
);
5745 struct cgroup_subsys
*ss
;
5748 do_each_subsys_mask(ss
, ssid
, have_free_callback
) {
5750 } while_each_subsys_mask();
5755 static int __init
cgroup_disable(char *str
)
5757 struct cgroup_subsys
*ss
;
5761 while ((token
= strsep(&str
, ",")) != NULL
) {
5765 for_each_subsys(ss
, i
) {
5766 if (strcmp(token
, ss
->name
) &&
5767 strcmp(token
, ss
->legacy_name
))
5769 cgroup_disable_mask
|= 1 << i
;
5774 __setup("cgroup_disable=", cgroup_disable
);
5777 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
5778 * @dentry: directory dentry of interest
5779 * @ss: subsystem of interest
5781 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5782 * to get the corresponding css and return it. If such css doesn't exist
5783 * or can't be pinned, an ERR_PTR value is returned.
5785 struct cgroup_subsys_state
*css_tryget_online_from_dir(struct dentry
*dentry
,
5786 struct cgroup_subsys
*ss
)
5788 struct kernfs_node
*kn
= kernfs_node_from_dentry(dentry
);
5789 struct file_system_type
*s_type
= dentry
->d_sb
->s_type
;
5790 struct cgroup_subsys_state
*css
= NULL
;
5791 struct cgroup
*cgrp
;
5793 /* is @dentry a cgroup dir? */
5794 if ((s_type
!= &cgroup_fs_type
&& s_type
!= &cgroup2_fs_type
) ||
5795 !kn
|| kernfs_type(kn
) != KERNFS_DIR
)
5796 return ERR_PTR(-EBADF
);
5801 * This path doesn't originate from kernfs and @kn could already
5802 * have been or be removed at any point. @kn->priv is RCU
5803 * protected for this access. See css_release_work_fn() for details.
5805 cgrp
= rcu_dereference(*(void __rcu __force
**)&kn
->priv
);
5807 css
= cgroup_css(cgrp
, ss
);
5809 if (!css
|| !css_tryget_online(css
))
5810 css
= ERR_PTR(-ENOENT
);
5817 * css_from_id - lookup css by id
5818 * @id: the cgroup id
5819 * @ss: cgroup subsys to be looked into
5821 * Returns the css if there's valid one with @id, otherwise returns NULL.
5822 * Should be called under rcu_read_lock().
5824 struct cgroup_subsys_state
*css_from_id(int id
, struct cgroup_subsys
*ss
)
5826 WARN_ON_ONCE(!rcu_read_lock_held());
5827 return idr_find(&ss
->css_idr
, id
);
5831 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5832 * @path: path on the default hierarchy
5834 * Find the cgroup at @path on the default hierarchy, increment its
5835 * reference count and return it. Returns pointer to the found cgroup on
5836 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5837 * if @path points to a non-directory.
5839 struct cgroup
*cgroup_get_from_path(const char *path
)
5841 struct kernfs_node
*kn
;
5842 struct cgroup
*cgrp
;
5844 mutex_lock(&cgroup_mutex
);
5846 kn
= kernfs_walk_and_get(cgrp_dfl_root
.cgrp
.kn
, path
);
5848 if (kernfs_type(kn
) == KERNFS_DIR
) {
5850 cgroup_get_live(cgrp
);
5852 cgrp
= ERR_PTR(-ENOTDIR
);
5856 cgrp
= ERR_PTR(-ENOENT
);
5859 mutex_unlock(&cgroup_mutex
);
5862 EXPORT_SYMBOL_GPL(cgroup_get_from_path
);
5865 * cgroup_get_from_fd - get a cgroup pointer from a fd
5866 * @fd: fd obtained by open(cgroup2_dir)
5868 * Find the cgroup from a fd which should be obtained
5869 * by opening a cgroup directory. Returns a pointer to the
5870 * cgroup on success. ERR_PTR is returned if the cgroup
5873 struct cgroup
*cgroup_get_from_fd(int fd
)
5875 struct cgroup_subsys_state
*css
;
5876 struct cgroup
*cgrp
;
5881 return ERR_PTR(-EBADF
);
5883 css
= css_tryget_online_from_dir(f
->f_path
.dentry
, NULL
);
5886 return ERR_CAST(css
);
5889 if (!cgroup_on_dfl(cgrp
)) {
5891 return ERR_PTR(-EBADF
);
5896 EXPORT_SYMBOL_GPL(cgroup_get_from_fd
);
5899 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5900 * definition in cgroup-defs.h.
5902 #ifdef CONFIG_SOCK_CGROUP_DATA
5904 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5906 DEFINE_SPINLOCK(cgroup_sk_update_lock
);
5907 static bool cgroup_sk_alloc_disabled __read_mostly
;
5909 void cgroup_sk_alloc_disable(void)
5911 if (cgroup_sk_alloc_disabled
)
5913 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5914 cgroup_sk_alloc_disabled
= true;
5919 #define cgroup_sk_alloc_disabled false
5923 void cgroup_sk_alloc(struct sock_cgroup_data
*skcd
)
5925 if (cgroup_sk_alloc_disabled
)
5928 /* Socket clone path */
5931 * We might be cloning a socket which is left in an empty
5932 * cgroup and the cgroup might have already been rmdir'd.
5933 * Don't use cgroup_get_live().
5935 cgroup_get(sock_cgroup_ptr(skcd
));
5942 struct css_set
*cset
;
5944 cset
= task_css_set(current
);
5945 if (likely(cgroup_tryget(cset
->dfl_cgrp
))) {
5946 skcd
->val
= (unsigned long)cset
->dfl_cgrp
;
5955 void cgroup_sk_free(struct sock_cgroup_data
*skcd
)
5957 cgroup_put(sock_cgroup_ptr(skcd
));
5960 #endif /* CONFIG_SOCK_CGROUP_DATA */
5962 #ifdef CONFIG_CGROUP_BPF
5963 int cgroup_bpf_attach(struct cgroup
*cgrp
, struct bpf_prog
*prog
,
5964 enum bpf_attach_type type
, u32 flags
)
5968 mutex_lock(&cgroup_mutex
);
5969 ret
= __cgroup_bpf_attach(cgrp
, prog
, type
, flags
);
5970 mutex_unlock(&cgroup_mutex
);
5973 int cgroup_bpf_detach(struct cgroup
*cgrp
, struct bpf_prog
*prog
,
5974 enum bpf_attach_type type
, u32 flags
)
5978 mutex_lock(&cgroup_mutex
);
5979 ret
= __cgroup_bpf_detach(cgrp
, prog
, type
, flags
);
5980 mutex_unlock(&cgroup_mutex
);
5983 int cgroup_bpf_query(struct cgroup
*cgrp
, const union bpf_attr
*attr
,
5984 union bpf_attr __user
*uattr
)
5988 mutex_lock(&cgroup_mutex
);
5989 ret
= __cgroup_bpf_query(cgrp
, attr
, uattr
);
5990 mutex_unlock(&cgroup_mutex
);
5993 #endif /* CONFIG_CGROUP_BPF */
5996 static ssize_t
show_delegatable_files(struct cftype
*files
, char *buf
,
5997 ssize_t size
, const char *prefix
)
6002 for (cft
= files
; cft
&& cft
->name
[0] != '\0'; cft
++) {
6003 if (!(cft
->flags
& CFTYPE_NS_DELEGATABLE
))
6007 ret
+= snprintf(buf
+ ret
, size
- ret
, "%s.", prefix
);
6009 ret
+= snprintf(buf
+ ret
, size
- ret
, "%s\n", cft
->name
);
6011 if (unlikely(ret
>= size
)) {
6020 static ssize_t
delegate_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
6023 struct cgroup_subsys
*ss
;
6027 ret
= show_delegatable_files(cgroup_base_files
, buf
, PAGE_SIZE
- ret
,
6030 for_each_subsys(ss
, ssid
)
6031 ret
+= show_delegatable_files(ss
->dfl_cftypes
, buf
+ ret
,
6033 cgroup_subsys_name
[ssid
]);
6037 static struct kobj_attribute cgroup_delegate_attr
= __ATTR_RO(delegate
);
6039 static ssize_t
features_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
6042 return snprintf(buf
, PAGE_SIZE
, "nsdelegate\n");
6044 static struct kobj_attribute cgroup_features_attr
= __ATTR_RO(features
);
6046 static struct attribute
*cgroup_sysfs_attrs
[] = {
6047 &cgroup_delegate_attr
.attr
,
6048 &cgroup_features_attr
.attr
,
6052 static const struct attribute_group cgroup_sysfs_attr_group
= {
6053 .attrs
= cgroup_sysfs_attrs
,
6057 static int __init
cgroup_sysfs_init(void)
6059 return sysfs_create_group(kernel_kobj
, &cgroup_sysfs_attr_group
);
6061 subsys_initcall(cgroup_sysfs_init
);
6062 #endif /* CONFIG_SYSFS */