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>
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/cgroup.h>
62 #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
66 * cgroup_mutex is the master lock. Any modification to cgroup or its
67 * hierarchy must be performed while holding it.
69 * css_set_lock protects task->cgroups pointer, the list of css_set
70 * objects, and the chain of tasks off each css_set.
72 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
73 * cgroup.h can use them for lockdep annotations.
75 DEFINE_MUTEX(cgroup_mutex
);
76 DEFINE_SPINLOCK(css_set_lock
);
78 #ifdef CONFIG_PROVE_RCU
79 EXPORT_SYMBOL_GPL(cgroup_mutex
);
80 EXPORT_SYMBOL_GPL(css_set_lock
);
84 * Protects cgroup_idr and css_idr so that IDs can be released without
85 * grabbing cgroup_mutex.
87 static DEFINE_SPINLOCK(cgroup_idr_lock
);
90 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
91 * against file removal/re-creation across css hiding.
93 static DEFINE_SPINLOCK(cgroup_file_kn_lock
);
95 struct percpu_rw_semaphore cgroup_threadgroup_rwsem
;
97 #define cgroup_assert_mutex_or_rcu_locked() \
98 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
99 !lockdep_is_held(&cgroup_mutex), \
100 "cgroup_mutex or RCU read lock required");
103 * cgroup destruction makes heavy use of work items and there can be a lot
104 * of concurrent destructions. Use a separate workqueue so that cgroup
105 * destruction work items don't end up filling up max_active of system_wq
106 * which may lead to deadlock.
108 static struct workqueue_struct
*cgroup_destroy_wq
;
110 /* generate an array of cgroup subsystem pointers */
111 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
112 struct cgroup_subsys
*cgroup_subsys
[] = {
113 #include <linux/cgroup_subsys.h>
117 /* array of cgroup subsystem names */
118 #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
119 static const char *cgroup_subsys_name
[] = {
120 #include <linux/cgroup_subsys.h>
124 /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
126 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
127 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
128 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
129 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
130 #include <linux/cgroup_subsys.h>
133 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
134 static struct static_key_true
*cgroup_subsys_enabled_key
[] = {
135 #include <linux/cgroup_subsys.h>
139 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
140 static struct static_key_true
*cgroup_subsys_on_dfl_key
[] = {
141 #include <linux/cgroup_subsys.h>
146 * The default hierarchy, reserved for the subsystems that are otherwise
147 * unattached - it never has more than a single cgroup, and all tasks are
148 * part of that cgroup.
150 struct cgroup_root cgrp_dfl_root
;
151 EXPORT_SYMBOL_GPL(cgrp_dfl_root
);
154 * The default hierarchy always exists but is hidden until mounted for the
155 * first time. This is for backward compatibility.
157 static bool cgrp_dfl_visible
;
159 /* some controllers are not supported in the default hierarchy */
160 static u16 cgrp_dfl_inhibit_ss_mask
;
162 /* some controllers are implicitly enabled on the default hierarchy */
163 static u16 cgrp_dfl_implicit_ss_mask
;
165 /* The list of hierarchy roots */
166 LIST_HEAD(cgroup_roots
);
167 static int cgroup_root_count
;
169 /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
170 static DEFINE_IDR(cgroup_hierarchy_idr
);
173 * Assign a monotonically increasing serial number to csses. It guarantees
174 * cgroups with bigger numbers are newer than those with smaller numbers.
175 * Also, as csses are always appended to the parent's ->children list, it
176 * guarantees that sibling csses are always sorted in the ascending serial
177 * number order on the list. Protected by cgroup_mutex.
179 static u64 css_serial_nr_next
= 1;
182 * These bitmasks identify subsystems with specific features to avoid
183 * having to do iterative checks repeatedly.
185 static u16 have_fork_callback __read_mostly
;
186 static u16 have_exit_callback __read_mostly
;
187 static u16 have_free_callback __read_mostly
;
188 static u16 have_canfork_callback __read_mostly
;
190 /* cgroup namespace for init task */
191 struct cgroup_namespace init_cgroup_ns
= {
192 .count
= REFCOUNT_INIT(2),
193 .user_ns
= &init_user_ns
,
194 .ns
.ops
= &cgroupns_operations
,
195 .ns
.inum
= PROC_CGROUP_INIT_INO
,
196 .root_cset
= &init_css_set
,
199 static struct file_system_type cgroup2_fs_type
;
200 static struct cftype cgroup_base_files
[];
202 static int cgroup_apply_control(struct cgroup
*cgrp
);
203 static void cgroup_finalize_control(struct cgroup
*cgrp
, int ret
);
204 static void css_task_iter_advance(struct css_task_iter
*it
);
205 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
206 static struct cgroup_subsys_state
*css_create(struct cgroup
*cgrp
,
207 struct cgroup_subsys
*ss
);
208 static void css_release(struct percpu_ref
*ref
);
209 static void kill_css(struct cgroup_subsys_state
*css
);
210 static int cgroup_addrm_files(struct cgroup_subsys_state
*css
,
211 struct cgroup
*cgrp
, struct cftype cfts
[],
215 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
216 * @ssid: subsys ID of interest
218 * cgroup_subsys_enabled() can only be used with literal subsys names which
219 * is fine for individual subsystems but unsuitable for cgroup core. This
220 * is slower static_key_enabled() based test indexed by @ssid.
222 bool cgroup_ssid_enabled(int ssid
)
224 if (CGROUP_SUBSYS_COUNT
== 0)
227 return static_key_enabled(cgroup_subsys_enabled_key
[ssid
]);
231 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
232 * @cgrp: the cgroup of interest
234 * The default hierarchy is the v2 interface of cgroup and this function
235 * can be used to test whether a cgroup is on the default hierarchy for
236 * cases where a subsystem should behave differnetly depending on the
239 * The set of behaviors which change on the default hierarchy are still
240 * being determined and the mount option is prefixed with __DEVEL__.
242 * List of changed behaviors:
244 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
245 * and "name" are disallowed.
247 * - When mounting an existing superblock, mount options should match.
249 * - Remount is disallowed.
251 * - rename(2) is disallowed.
253 * - "tasks" is removed. Everything should be at process granularity. Use
254 * "cgroup.procs" instead.
256 * - "cgroup.procs" is not sorted. pids will be unique unless they got
257 * recycled inbetween reads.
259 * - "release_agent" and "notify_on_release" are removed. Replacement
260 * notification mechanism will be implemented.
262 * - "cgroup.clone_children" is removed.
264 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
265 * and its descendants contain no task; otherwise, 1. The file also
266 * generates kernfs notification which can be monitored through poll and
267 * [di]notify when the value of the file changes.
269 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
270 * take masks of ancestors with non-empty cpus/mems, instead of being
271 * moved to an ancestor.
273 * - cpuset: a task can be moved into an empty cpuset, and again it takes
274 * masks of ancestors.
276 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
279 * - blkcg: blk-throttle becomes properly hierarchical.
281 * - debug: disallowed on the default hierarchy.
283 bool cgroup_on_dfl(const struct cgroup
*cgrp
)
285 return cgrp
->root
== &cgrp_dfl_root
;
288 /* IDR wrappers which synchronize using cgroup_idr_lock */
289 static int cgroup_idr_alloc(struct idr
*idr
, void *ptr
, int start
, int end
,
294 idr_preload(gfp_mask
);
295 spin_lock_bh(&cgroup_idr_lock
);
296 ret
= idr_alloc(idr
, ptr
, start
, end
, gfp_mask
& ~__GFP_DIRECT_RECLAIM
);
297 spin_unlock_bh(&cgroup_idr_lock
);
302 static void *cgroup_idr_replace(struct idr
*idr
, void *ptr
, int id
)
306 spin_lock_bh(&cgroup_idr_lock
);
307 ret
= idr_replace(idr
, ptr
, id
);
308 spin_unlock_bh(&cgroup_idr_lock
);
312 static void cgroup_idr_remove(struct idr
*idr
, int id
)
314 spin_lock_bh(&cgroup_idr_lock
);
316 spin_unlock_bh(&cgroup_idr_lock
);
319 static struct cgroup
*cgroup_parent(struct cgroup
*cgrp
)
321 struct cgroup_subsys_state
*parent_css
= cgrp
->self
.parent
;
324 return container_of(parent_css
, struct cgroup
, self
);
328 /* subsystems visibly enabled on a cgroup */
329 static u16
cgroup_control(struct cgroup
*cgrp
)
331 struct cgroup
*parent
= cgroup_parent(cgrp
);
332 u16 root_ss_mask
= cgrp
->root
->subsys_mask
;
335 return parent
->subtree_control
;
337 if (cgroup_on_dfl(cgrp
))
338 root_ss_mask
&= ~(cgrp_dfl_inhibit_ss_mask
|
339 cgrp_dfl_implicit_ss_mask
);
343 /* subsystems enabled on a cgroup */
344 static u16
cgroup_ss_mask(struct cgroup
*cgrp
)
346 struct cgroup
*parent
= cgroup_parent(cgrp
);
349 return parent
->subtree_ss_mask
;
351 return cgrp
->root
->subsys_mask
;
355 * cgroup_css - obtain a cgroup's css for the specified subsystem
356 * @cgrp: the cgroup of interest
357 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
359 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
360 * function must be called either under cgroup_mutex or rcu_read_lock() and
361 * the caller is responsible for pinning the returned css if it wants to
362 * keep accessing it outside the said locks. This function may return
363 * %NULL if @cgrp doesn't have @subsys_id enabled.
365 static struct cgroup_subsys_state
*cgroup_css(struct cgroup
*cgrp
,
366 struct cgroup_subsys
*ss
)
369 return rcu_dereference_check(cgrp
->subsys
[ss
->id
],
370 lockdep_is_held(&cgroup_mutex
));
376 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
377 * @cgrp: the cgroup of interest
378 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
380 * Similar to cgroup_css() but returns the effective css, which is defined
381 * as the matching css of the nearest ancestor including self which has @ss
382 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
383 * function is guaranteed to return non-NULL css.
385 static struct cgroup_subsys_state
*cgroup_e_css(struct cgroup
*cgrp
,
386 struct cgroup_subsys
*ss
)
388 lockdep_assert_held(&cgroup_mutex
);
394 * This function is used while updating css associations and thus
395 * can't test the csses directly. Test ss_mask.
397 while (!(cgroup_ss_mask(cgrp
) & (1 << ss
->id
))) {
398 cgrp
= cgroup_parent(cgrp
);
403 return cgroup_css(cgrp
, ss
);
407 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
408 * @cgrp: the cgroup of interest
409 * @ss: the subsystem of interest
411 * Find and get the effective css of @cgrp for @ss. The effective css is
412 * defined as the matching css of the nearest ancestor including self which
413 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
414 * the root css is returned, so this function always returns a valid css.
415 * The returned css must be put using css_put().
417 struct cgroup_subsys_state
*cgroup_get_e_css(struct cgroup
*cgrp
,
418 struct cgroup_subsys
*ss
)
420 struct cgroup_subsys_state
*css
;
425 css
= cgroup_css(cgrp
, ss
);
427 if (css
&& css_tryget_online(css
))
429 cgrp
= cgroup_parent(cgrp
);
432 css
= init_css_set
.subsys
[ss
->id
];
439 static void __maybe_unused
cgroup_get(struct cgroup
*cgrp
)
441 css_get(&cgrp
->self
);
444 static void cgroup_get_live(struct cgroup
*cgrp
)
446 WARN_ON_ONCE(cgroup_is_dead(cgrp
));
447 css_get(&cgrp
->self
);
450 static bool cgroup_tryget(struct cgroup
*cgrp
)
452 return css_tryget(&cgrp
->self
);
455 struct cgroup_subsys_state
*of_css(struct kernfs_open_file
*of
)
457 struct cgroup
*cgrp
= of
->kn
->parent
->priv
;
458 struct cftype
*cft
= of_cft(of
);
461 * This is open and unprotected implementation of cgroup_css().
462 * seq_css() is only called from a kernfs file operation which has
463 * an active reference on the file. Because all the subsystem
464 * files are drained before a css is disassociated with a cgroup,
465 * the matching css from the cgroup's subsys table is guaranteed to
466 * be and stay valid until the enclosing operation is complete.
469 return rcu_dereference_raw(cgrp
->subsys
[cft
->ss
->id
]);
473 EXPORT_SYMBOL_GPL(of_css
);
476 * for_each_css - iterate all css's of a cgroup
477 * @css: the iteration cursor
478 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
479 * @cgrp: the target cgroup to iterate css's of
481 * Should be called under cgroup_[tree_]mutex.
483 #define for_each_css(css, ssid, cgrp) \
484 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
485 if (!((css) = rcu_dereference_check( \
486 (cgrp)->subsys[(ssid)], \
487 lockdep_is_held(&cgroup_mutex)))) { } \
491 * for_each_e_css - iterate all effective css's of a cgroup
492 * @css: the iteration cursor
493 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
494 * @cgrp: the target cgroup to iterate css's of
496 * Should be called under cgroup_[tree_]mutex.
498 #define for_each_e_css(css, ssid, cgrp) \
499 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
500 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
505 * do_each_subsys_mask - filter for_each_subsys with a bitmask
506 * @ss: the iteration cursor
507 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
508 * @ss_mask: the bitmask
510 * The block will only run for cases where the ssid-th bit (1 << ssid) of
513 #define do_each_subsys_mask(ss, ssid, ss_mask) do { \
514 unsigned long __ss_mask = (ss_mask); \
515 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
519 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
520 (ss) = cgroup_subsys[ssid]; \
523 #define while_each_subsys_mask() \
528 /* iterate over child cgrps, lock should be held throughout iteration */
529 #define cgroup_for_each_live_child(child, cgrp) \
530 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
531 if (({ lockdep_assert_held(&cgroup_mutex); \
532 cgroup_is_dead(child); })) \
536 /* walk live descendants in preorder */
537 #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
538 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
539 if (({ lockdep_assert_held(&cgroup_mutex); \
540 (dsct) = (d_css)->cgroup; \
541 cgroup_is_dead(dsct); })) \
545 /* walk live descendants in postorder */
546 #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
547 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
548 if (({ lockdep_assert_held(&cgroup_mutex); \
549 (dsct) = (d_css)->cgroup; \
550 cgroup_is_dead(dsct); })) \
555 * The default css_set - used by init and its children prior to any
556 * hierarchies being mounted. It contains a pointer to the root state
557 * for each subsystem. Also used to anchor the list of css_sets. Not
558 * reference-counted, to improve performance when child cgroups
559 * haven't been created.
561 struct css_set init_css_set
= {
562 .refcount
= REFCOUNT_INIT(1),
563 .tasks
= LIST_HEAD_INIT(init_css_set
.tasks
),
564 .mg_tasks
= LIST_HEAD_INIT(init_css_set
.mg_tasks
),
565 .task_iters
= LIST_HEAD_INIT(init_css_set
.task_iters
),
566 .cgrp_links
= LIST_HEAD_INIT(init_css_set
.cgrp_links
),
567 .mg_preload_node
= LIST_HEAD_INIT(init_css_set
.mg_preload_node
),
568 .mg_node
= LIST_HEAD_INIT(init_css_set
.mg_node
),
571 static int css_set_count
= 1; /* 1 for init_css_set */
574 * css_set_populated - does a css_set contain any tasks?
575 * @cset: target css_set
577 static bool css_set_populated(struct css_set
*cset
)
579 lockdep_assert_held(&css_set_lock
);
581 return !list_empty(&cset
->tasks
) || !list_empty(&cset
->mg_tasks
);
585 * cgroup_update_populated - updated populated count of a cgroup
586 * @cgrp: the target cgroup
587 * @populated: inc or dec populated count
589 * One of the css_sets associated with @cgrp is either getting its first
590 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
591 * count is propagated towards root so that a given cgroup's populated_cnt
592 * is zero iff the cgroup and all its descendants don't contain any tasks.
594 * @cgrp's interface file "cgroup.populated" is zero if
595 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
596 * changes from or to zero, userland is notified that the content of the
597 * interface file has changed. This can be used to detect when @cgrp and
598 * its descendants become populated or empty.
600 static void cgroup_update_populated(struct cgroup
*cgrp
, bool populated
)
602 lockdep_assert_held(&css_set_lock
);
608 trigger
= !cgrp
->populated_cnt
++;
610 trigger
= !--cgrp
->populated_cnt
;
615 cgroup1_check_for_release(cgrp
);
616 cgroup_file_notify(&cgrp
->events_file
);
618 cgrp
= cgroup_parent(cgrp
);
623 * css_set_update_populated - update populated state of a css_set
624 * @cset: target css_set
625 * @populated: whether @cset is populated or depopulated
627 * @cset is either getting the first task or losing the last. Update the
628 * ->populated_cnt of all associated cgroups accordingly.
630 static void css_set_update_populated(struct css_set
*cset
, bool populated
)
632 struct cgrp_cset_link
*link
;
634 lockdep_assert_held(&css_set_lock
);
636 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
)
637 cgroup_update_populated(link
->cgrp
, populated
);
641 * css_set_move_task - move a task from one css_set to another
642 * @task: task being moved
643 * @from_cset: css_set @task currently belongs to (may be NULL)
644 * @to_cset: new css_set @task is being moved to (may be NULL)
645 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
647 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
648 * css_set, @from_cset can be NULL. If @task is being disassociated
649 * instead of moved, @to_cset can be NULL.
651 * This function automatically handles populated_cnt updates and
652 * css_task_iter adjustments but the caller is responsible for managing
653 * @from_cset and @to_cset's reference counts.
655 static void css_set_move_task(struct task_struct
*task
,
656 struct css_set
*from_cset
, struct css_set
*to_cset
,
659 lockdep_assert_held(&css_set_lock
);
661 if (to_cset
&& !css_set_populated(to_cset
))
662 css_set_update_populated(to_cset
, true);
665 struct css_task_iter
*it
, *pos
;
667 WARN_ON_ONCE(list_empty(&task
->cg_list
));
670 * @task is leaving, advance task iterators which are
671 * pointing to it so that they can resume at the next
672 * position. Advancing an iterator might remove it from
673 * the list, use safe walk. See css_task_iter_advance*()
676 list_for_each_entry_safe(it
, pos
, &from_cset
->task_iters
,
678 if (it
->task_pos
== &task
->cg_list
)
679 css_task_iter_advance(it
);
681 list_del_init(&task
->cg_list
);
682 if (!css_set_populated(from_cset
))
683 css_set_update_populated(from_cset
, false);
685 WARN_ON_ONCE(!list_empty(&task
->cg_list
));
690 * We are synchronized through cgroup_threadgroup_rwsem
691 * against PF_EXITING setting such that we can't race
692 * against cgroup_exit() changing the css_set to
693 * init_css_set and dropping the old one.
695 WARN_ON_ONCE(task
->flags
& PF_EXITING
);
697 rcu_assign_pointer(task
->cgroups
, to_cset
);
698 list_add_tail(&task
->cg_list
, use_mg_tasks
? &to_cset
->mg_tasks
:
704 * hash table for cgroup groups. This improves the performance to find
705 * an existing css_set. This hash doesn't (currently) take into
706 * account cgroups in empty hierarchies.
708 #define CSS_SET_HASH_BITS 7
709 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
711 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
713 unsigned long key
= 0UL;
714 struct cgroup_subsys
*ss
;
717 for_each_subsys(ss
, i
)
718 key
+= (unsigned long)css
[i
];
719 key
= (key
>> 16) ^ key
;
724 void put_css_set_locked(struct css_set
*cset
)
726 struct cgrp_cset_link
*link
, *tmp_link
;
727 struct cgroup_subsys
*ss
;
730 lockdep_assert_held(&css_set_lock
);
732 if (!refcount_dec_and_test(&cset
->refcount
))
735 /* This css_set is dead. unlink it and release cgroup and css refs */
736 for_each_subsys(ss
, ssid
) {
737 list_del(&cset
->e_cset_node
[ssid
]);
738 css_put(cset
->subsys
[ssid
]);
740 hash_del(&cset
->hlist
);
743 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
744 list_del(&link
->cset_link
);
745 list_del(&link
->cgrp_link
);
746 if (cgroup_parent(link
->cgrp
))
747 cgroup_put(link
->cgrp
);
751 kfree_rcu(cset
, rcu_head
);
755 * compare_css_sets - helper function for find_existing_css_set().
756 * @cset: candidate css_set being tested
757 * @old_cset: existing css_set for a task
758 * @new_cgrp: cgroup that's being entered by the task
759 * @template: desired set of css pointers in css_set (pre-calculated)
761 * Returns true if "cset" matches "old_cset" except for the hierarchy
762 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
764 static bool compare_css_sets(struct css_set
*cset
,
765 struct css_set
*old_cset
,
766 struct cgroup
*new_cgrp
,
767 struct cgroup_subsys_state
*template[])
769 struct list_head
*l1
, *l2
;
772 * On the default hierarchy, there can be csets which are
773 * associated with the same set of cgroups but different csses.
774 * Let's first ensure that csses match.
776 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
)))
780 * Compare cgroup pointers in order to distinguish between
781 * different cgroups in hierarchies. As different cgroups may
782 * share the same effective css, this comparison is always
785 l1
= &cset
->cgrp_links
;
786 l2
= &old_cset
->cgrp_links
;
788 struct cgrp_cset_link
*link1
, *link2
;
789 struct cgroup
*cgrp1
, *cgrp2
;
793 /* See if we reached the end - both lists are equal length. */
794 if (l1
== &cset
->cgrp_links
) {
795 BUG_ON(l2
!= &old_cset
->cgrp_links
);
798 BUG_ON(l2
== &old_cset
->cgrp_links
);
800 /* Locate the cgroups associated with these links. */
801 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
802 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
805 /* Hierarchies should be linked in the same order. */
806 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
809 * If this hierarchy is the hierarchy of the cgroup
810 * that's changing, then we need to check that this
811 * css_set points to the new cgroup; if it's any other
812 * hierarchy, then this css_set should point to the
813 * same cgroup as the old css_set.
815 if (cgrp1
->root
== new_cgrp
->root
) {
816 if (cgrp1
!= new_cgrp
)
827 * find_existing_css_set - init css array and find the matching css_set
828 * @old_cset: the css_set that we're using before the cgroup transition
829 * @cgrp: the cgroup that we're moving into
830 * @template: out param for the new set of csses, should be clear on entry
832 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
834 struct cgroup_subsys_state
*template[])
836 struct cgroup_root
*root
= cgrp
->root
;
837 struct cgroup_subsys
*ss
;
838 struct css_set
*cset
;
843 * Build the set of subsystem state objects that we want to see in the
844 * new css_set. while subsystems can change globally, the entries here
845 * won't change, so no need for locking.
847 for_each_subsys(ss
, i
) {
848 if (root
->subsys_mask
& (1UL << i
)) {
850 * @ss is in this hierarchy, so we want the
851 * effective css from @cgrp.
853 template[i
] = cgroup_e_css(cgrp
, ss
);
856 * @ss is not in this hierarchy, so we don't want
859 template[i
] = old_cset
->subsys
[i
];
863 key
= css_set_hash(template);
864 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
865 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
868 /* This css_set matches what we need */
872 /* No existing cgroup group matched */
876 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
878 struct cgrp_cset_link
*link
, *tmp_link
;
880 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
881 list_del(&link
->cset_link
);
887 * allocate_cgrp_cset_links - allocate cgrp_cset_links
888 * @count: the number of links to allocate
889 * @tmp_links: list_head the allocated links are put on
891 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
892 * through ->cset_link. Returns 0 on success or -errno.
894 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
896 struct cgrp_cset_link
*link
;
899 INIT_LIST_HEAD(tmp_links
);
901 for (i
= 0; i
< count
; i
++) {
902 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
904 free_cgrp_cset_links(tmp_links
);
907 list_add(&link
->cset_link
, tmp_links
);
913 * link_css_set - a helper function to link a css_set to a cgroup
914 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
915 * @cset: the css_set to be linked
916 * @cgrp: the destination cgroup
918 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
921 struct cgrp_cset_link
*link
;
923 BUG_ON(list_empty(tmp_links
));
925 if (cgroup_on_dfl(cgrp
))
926 cset
->dfl_cgrp
= cgrp
;
928 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
933 * Always add links to the tail of the lists so that the lists are
934 * in choronological order.
936 list_move_tail(&link
->cset_link
, &cgrp
->cset_links
);
937 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
939 if (cgroup_parent(cgrp
))
940 cgroup_get_live(cgrp
);
944 * find_css_set - return a new css_set with one cgroup updated
945 * @old_cset: the baseline css_set
946 * @cgrp: the cgroup to be updated
948 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
949 * substituted into the appropriate hierarchy.
951 static struct css_set
*find_css_set(struct css_set
*old_cset
,
954 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
955 struct css_set
*cset
;
956 struct list_head tmp_links
;
957 struct cgrp_cset_link
*link
;
958 struct cgroup_subsys
*ss
;
962 lockdep_assert_held(&cgroup_mutex
);
964 /* First see if we already have a cgroup group that matches
966 spin_lock_irq(&css_set_lock
);
967 cset
= find_existing_css_set(old_cset
, cgrp
, template);
970 spin_unlock_irq(&css_set_lock
);
975 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
979 /* Allocate all the cgrp_cset_link objects that we'll need */
980 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
985 refcount_set(&cset
->refcount
, 1);
986 INIT_LIST_HEAD(&cset
->tasks
);
987 INIT_LIST_HEAD(&cset
->mg_tasks
);
988 INIT_LIST_HEAD(&cset
->task_iters
);
989 INIT_HLIST_NODE(&cset
->hlist
);
990 INIT_LIST_HEAD(&cset
->cgrp_links
);
991 INIT_LIST_HEAD(&cset
->mg_preload_node
);
992 INIT_LIST_HEAD(&cset
->mg_node
);
994 /* Copy the set of subsystem state objects generated in
995 * find_existing_css_set() */
996 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
998 spin_lock_irq(&css_set_lock
);
999 /* Add reference counts and links from the new css_set. */
1000 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
1001 struct cgroup
*c
= link
->cgrp
;
1003 if (c
->root
== cgrp
->root
)
1005 link_css_set(&tmp_links
, cset
, c
);
1008 BUG_ON(!list_empty(&tmp_links
));
1012 /* Add @cset to the hash table */
1013 key
= css_set_hash(cset
->subsys
);
1014 hash_add(css_set_table
, &cset
->hlist
, key
);
1016 for_each_subsys(ss
, ssid
) {
1017 struct cgroup_subsys_state
*css
= cset
->subsys
[ssid
];
1019 list_add_tail(&cset
->e_cset_node
[ssid
],
1020 &css
->cgroup
->e_csets
[ssid
]);
1024 spin_unlock_irq(&css_set_lock
);
1029 struct cgroup_root
*cgroup_root_from_kf(struct kernfs_root
*kf_root
)
1031 struct cgroup
*root_cgrp
= kf_root
->kn
->priv
;
1033 return root_cgrp
->root
;
1036 static int cgroup_init_root_id(struct cgroup_root
*root
)
1040 lockdep_assert_held(&cgroup_mutex
);
1042 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, 0, 0, GFP_KERNEL
);
1046 root
->hierarchy_id
= id
;
1050 static void cgroup_exit_root_id(struct cgroup_root
*root
)
1052 lockdep_assert_held(&cgroup_mutex
);
1054 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1057 void cgroup_free_root(struct cgroup_root
*root
)
1060 idr_destroy(&root
->cgroup_idr
);
1065 static void cgroup_destroy_root(struct cgroup_root
*root
)
1067 struct cgroup
*cgrp
= &root
->cgrp
;
1068 struct cgrp_cset_link
*link
, *tmp_link
;
1070 trace_cgroup_destroy_root(root
);
1072 cgroup_lock_and_drain_offline(&cgrp_dfl_root
.cgrp
);
1074 BUG_ON(atomic_read(&root
->nr_cgrps
));
1075 BUG_ON(!list_empty(&cgrp
->self
.children
));
1077 /* Rebind all subsystems back to the default hierarchy */
1078 WARN_ON(rebind_subsystems(&cgrp_dfl_root
, root
->subsys_mask
));
1081 * Release all the links from cset_links to this hierarchy's
1084 spin_lock_irq(&css_set_lock
);
1086 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1087 list_del(&link
->cset_link
);
1088 list_del(&link
->cgrp_link
);
1092 spin_unlock_irq(&css_set_lock
);
1094 if (!list_empty(&root
->root_list
)) {
1095 list_del(&root
->root_list
);
1096 cgroup_root_count
--;
1099 cgroup_exit_root_id(root
);
1101 mutex_unlock(&cgroup_mutex
);
1103 kernfs_destroy_root(root
->kf_root
);
1104 cgroup_free_root(root
);
1108 * look up cgroup associated with current task's cgroup namespace on the
1109 * specified hierarchy
1111 static struct cgroup
*
1112 current_cgns_cgroup_from_root(struct cgroup_root
*root
)
1114 struct cgroup
*res
= NULL
;
1115 struct css_set
*cset
;
1117 lockdep_assert_held(&css_set_lock
);
1121 cset
= current
->nsproxy
->cgroup_ns
->root_cset
;
1122 if (cset
== &init_css_set
) {
1125 struct cgrp_cset_link
*link
;
1127 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
1128 struct cgroup
*c
= link
->cgrp
;
1130 if (c
->root
== root
) {
1142 /* look up cgroup associated with given css_set on the specified hierarchy */
1143 static struct cgroup
*cset_cgroup_from_root(struct css_set
*cset
,
1144 struct cgroup_root
*root
)
1146 struct cgroup
*res
= NULL
;
1148 lockdep_assert_held(&cgroup_mutex
);
1149 lockdep_assert_held(&css_set_lock
);
1151 if (cset
== &init_css_set
) {
1154 struct cgrp_cset_link
*link
;
1156 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
1157 struct cgroup
*c
= link
->cgrp
;
1159 if (c
->root
== root
) {
1171 * Return the cgroup for "task" from the given hierarchy. Must be
1172 * called with cgroup_mutex and css_set_lock held.
1174 struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
1175 struct cgroup_root
*root
)
1178 * No need to lock the task - since we hold cgroup_mutex the
1179 * task can't change groups, so the only thing that can happen
1180 * is that it exits and its css is set back to init_css_set.
1182 return cset_cgroup_from_root(task_css_set(task
), root
);
1186 * A task must hold cgroup_mutex to modify cgroups.
1188 * Any task can increment and decrement the count field without lock.
1189 * So in general, code holding cgroup_mutex can't rely on the count
1190 * field not changing. However, if the count goes to zero, then only
1191 * cgroup_attach_task() can increment it again. Because a count of zero
1192 * means that no tasks are currently attached, therefore there is no
1193 * way a task attached to that cgroup can fork (the other way to
1194 * increment the count). So code holding cgroup_mutex can safely
1195 * assume that if the count is zero, it will stay zero. Similarly, if
1196 * a task holds cgroup_mutex on a cgroup with zero count, it
1197 * knows that the cgroup won't be removed, as cgroup_rmdir()
1200 * A cgroup can only be deleted if both its 'count' of using tasks
1201 * is zero, and its list of 'children' cgroups is empty. Since all
1202 * tasks in the system use _some_ cgroup, and since there is always at
1203 * least one task in the system (init, pid == 1), therefore, root cgroup
1204 * always has either children cgroups and/or using tasks. So we don't
1205 * need a special hack to ensure that root cgroup cannot be deleted.
1207 * P.S. One more locking exception. RCU is used to guard the
1208 * update of a tasks cgroup pointer by cgroup_attach_task()
1211 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
;
1213 static char *cgroup_file_name(struct cgroup
*cgrp
, const struct cftype
*cft
,
1216 struct cgroup_subsys
*ss
= cft
->ss
;
1218 if (cft
->ss
&& !(cft
->flags
& CFTYPE_NO_PREFIX
) &&
1219 !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
))
1220 snprintf(buf
, CGROUP_FILE_NAME_MAX
, "%s.%s",
1221 cgroup_on_dfl(cgrp
) ? ss
->name
: ss
->legacy_name
,
1224 strncpy(buf
, cft
->name
, CGROUP_FILE_NAME_MAX
);
1229 * cgroup_file_mode - deduce file mode of a control file
1230 * @cft: the control file in question
1232 * S_IRUGO for read, S_IWUSR for write.
1234 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
1238 if (cft
->read_u64
|| cft
->read_s64
|| cft
->seq_show
)
1241 if (cft
->write_u64
|| cft
->write_s64
|| cft
->write
) {
1242 if (cft
->flags
& CFTYPE_WORLD_WRITABLE
)
1252 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
1253 * @subtree_control: the new subtree_control mask to consider
1254 * @this_ss_mask: available subsystems
1256 * On the default hierarchy, a subsystem may request other subsystems to be
1257 * enabled together through its ->depends_on mask. In such cases, more
1258 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1260 * This function calculates which subsystems need to be enabled if
1261 * @subtree_control is to be applied while restricted to @this_ss_mask.
1263 static u16
cgroup_calc_subtree_ss_mask(u16 subtree_control
, u16 this_ss_mask
)
1265 u16 cur_ss_mask
= subtree_control
;
1266 struct cgroup_subsys
*ss
;
1269 lockdep_assert_held(&cgroup_mutex
);
1271 cur_ss_mask
|= cgrp_dfl_implicit_ss_mask
;
1274 u16 new_ss_mask
= cur_ss_mask
;
1276 do_each_subsys_mask(ss
, ssid
, cur_ss_mask
) {
1277 new_ss_mask
|= ss
->depends_on
;
1278 } while_each_subsys_mask();
1281 * Mask out subsystems which aren't available. This can
1282 * happen only if some depended-upon subsystems were bound
1283 * to non-default hierarchies.
1285 new_ss_mask
&= this_ss_mask
;
1287 if (new_ss_mask
== cur_ss_mask
)
1289 cur_ss_mask
= new_ss_mask
;
1296 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1297 * @kn: the kernfs_node being serviced
1299 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1300 * the method finishes if locking succeeded. Note that once this function
1301 * returns the cgroup returned by cgroup_kn_lock_live() may become
1302 * inaccessible any time. If the caller intends to continue to access the
1303 * cgroup, it should pin it before invoking this function.
1305 void cgroup_kn_unlock(struct kernfs_node
*kn
)
1307 struct cgroup
*cgrp
;
1309 if (kernfs_type(kn
) == KERNFS_DIR
)
1312 cgrp
= kn
->parent
->priv
;
1314 mutex_unlock(&cgroup_mutex
);
1316 kernfs_unbreak_active_protection(kn
);
1321 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1322 * @kn: the kernfs_node being serviced
1323 * @drain_offline: perform offline draining on the cgroup
1325 * This helper is to be used by a cgroup kernfs method currently servicing
1326 * @kn. It breaks the active protection, performs cgroup locking and
1327 * verifies that the associated cgroup is alive. Returns the cgroup if
1328 * alive; otherwise, %NULL. A successful return should be undone by a
1329 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1330 * cgroup is drained of offlining csses before return.
1332 * Any cgroup kernfs method implementation which requires locking the
1333 * associated cgroup should use this helper. It avoids nesting cgroup
1334 * locking under kernfs active protection and allows all kernfs operations
1335 * including self-removal.
1337 struct cgroup
*cgroup_kn_lock_live(struct kernfs_node
*kn
, bool drain_offline
)
1339 struct cgroup
*cgrp
;
1341 if (kernfs_type(kn
) == KERNFS_DIR
)
1344 cgrp
= kn
->parent
->priv
;
1347 * We're gonna grab cgroup_mutex which nests outside kernfs
1348 * active_ref. cgroup liveliness check alone provides enough
1349 * protection against removal. Ensure @cgrp stays accessible and
1350 * break the active_ref protection.
1352 if (!cgroup_tryget(cgrp
))
1354 kernfs_break_active_protection(kn
);
1357 cgroup_lock_and_drain_offline(cgrp
);
1359 mutex_lock(&cgroup_mutex
);
1361 if (!cgroup_is_dead(cgrp
))
1364 cgroup_kn_unlock(kn
);
1368 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
1370 char name
[CGROUP_FILE_NAME_MAX
];
1372 lockdep_assert_held(&cgroup_mutex
);
1374 if (cft
->file_offset
) {
1375 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, cft
->ss
);
1376 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
1378 spin_lock_irq(&cgroup_file_kn_lock
);
1380 spin_unlock_irq(&cgroup_file_kn_lock
);
1383 kernfs_remove_by_name(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
));
1387 * css_clear_dir - remove subsys files in a cgroup directory
1390 static void css_clear_dir(struct cgroup_subsys_state
*css
)
1392 struct cgroup
*cgrp
= css
->cgroup
;
1393 struct cftype
*cfts
;
1395 if (!(css
->flags
& CSS_VISIBLE
))
1398 css
->flags
&= ~CSS_VISIBLE
;
1400 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
)
1401 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1405 * css_populate_dir - create subsys files in a cgroup directory
1408 * On failure, no file is added.
1410 static int css_populate_dir(struct cgroup_subsys_state
*css
)
1412 struct cgroup
*cgrp
= css
->cgroup
;
1413 struct cftype
*cfts
, *failed_cfts
;
1416 if ((css
->flags
& CSS_VISIBLE
) || !cgrp
->kn
)
1420 if (cgroup_on_dfl(cgrp
))
1421 cfts
= cgroup_base_files
;
1423 cfts
= cgroup1_base_files
;
1425 return cgroup_addrm_files(&cgrp
->self
, cgrp
, cfts
, true);
1428 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1429 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, true);
1436 css
->flags
|= CSS_VISIBLE
;
1440 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1441 if (cfts
== failed_cfts
)
1443 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1448 int rebind_subsystems(struct cgroup_root
*dst_root
, u16 ss_mask
)
1450 struct cgroup
*dcgrp
= &dst_root
->cgrp
;
1451 struct cgroup_subsys
*ss
;
1454 lockdep_assert_held(&cgroup_mutex
);
1456 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
1458 * If @ss has non-root csses attached to it, can't move.
1459 * If @ss is an implicit controller, it is exempt from this
1460 * rule and can be stolen.
1462 if (css_next_child(NULL
, cgroup_css(&ss
->root
->cgrp
, ss
)) &&
1463 !ss
->implicit_on_dfl
)
1466 /* can't move between two non-dummy roots either */
1467 if (ss
->root
!= &cgrp_dfl_root
&& dst_root
!= &cgrp_dfl_root
)
1469 } while_each_subsys_mask();
1471 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
1472 struct cgroup_root
*src_root
= ss
->root
;
1473 struct cgroup
*scgrp
= &src_root
->cgrp
;
1474 struct cgroup_subsys_state
*css
= cgroup_css(scgrp
, ss
);
1475 struct css_set
*cset
;
1477 WARN_ON(!css
|| cgroup_css(dcgrp
, ss
));
1479 /* disable from the source */
1480 src_root
->subsys_mask
&= ~(1 << ssid
);
1481 WARN_ON(cgroup_apply_control(scgrp
));
1482 cgroup_finalize_control(scgrp
, 0);
1485 RCU_INIT_POINTER(scgrp
->subsys
[ssid
], NULL
);
1486 rcu_assign_pointer(dcgrp
->subsys
[ssid
], css
);
1487 ss
->root
= dst_root
;
1488 css
->cgroup
= dcgrp
;
1490 spin_lock_irq(&css_set_lock
);
1491 hash_for_each(css_set_table
, i
, cset
, hlist
)
1492 list_move_tail(&cset
->e_cset_node
[ss
->id
],
1493 &dcgrp
->e_csets
[ss
->id
]);
1494 spin_unlock_irq(&css_set_lock
);
1496 /* default hierarchy doesn't enable controllers by default */
1497 dst_root
->subsys_mask
|= 1 << ssid
;
1498 if (dst_root
== &cgrp_dfl_root
) {
1499 static_branch_enable(cgroup_subsys_on_dfl_key
[ssid
]);
1501 dcgrp
->subtree_control
|= 1 << ssid
;
1502 static_branch_disable(cgroup_subsys_on_dfl_key
[ssid
]);
1505 ret
= cgroup_apply_control(dcgrp
);
1507 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1512 } while_each_subsys_mask();
1514 kernfs_activate(dcgrp
->kn
);
1518 int cgroup_show_path(struct seq_file
*sf
, struct kernfs_node
*kf_node
,
1519 struct kernfs_root
*kf_root
)
1523 struct cgroup_root
*kf_cgroot
= cgroup_root_from_kf(kf_root
);
1524 struct cgroup
*ns_cgroup
;
1526 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1530 spin_lock_irq(&css_set_lock
);
1531 ns_cgroup
= current_cgns_cgroup_from_root(kf_cgroot
);
1532 len
= kernfs_path_from_node(kf_node
, ns_cgroup
->kn
, buf
, PATH_MAX
);
1533 spin_unlock_irq(&css_set_lock
);
1535 if (len
>= PATH_MAX
)
1538 seq_escape(sf
, buf
, " \t\n\\");
1545 static int cgroup_remount(struct kernfs_root
*kf_root
, int *flags
, char *data
)
1547 pr_err("remount is not allowed\n");
1552 * To reduce the fork() overhead for systems that are not actually using
1553 * their cgroups capability, we don't maintain the lists running through
1554 * each css_set to its tasks until we see the list actually used - in other
1555 * words after the first mount.
1557 static bool use_task_css_set_links __read_mostly
;
1559 static void cgroup_enable_task_cg_lists(void)
1561 struct task_struct
*p
, *g
;
1563 spin_lock_irq(&css_set_lock
);
1565 if (use_task_css_set_links
)
1568 use_task_css_set_links
= true;
1571 * We need tasklist_lock because RCU is not safe against
1572 * while_each_thread(). Besides, a forking task that has passed
1573 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1574 * is not guaranteed to have its child immediately visible in the
1575 * tasklist if we walk through it with RCU.
1577 read_lock(&tasklist_lock
);
1578 do_each_thread(g
, p
) {
1579 WARN_ON_ONCE(!list_empty(&p
->cg_list
) ||
1580 task_css_set(p
) != &init_css_set
);
1583 * We should check if the process is exiting, otherwise
1584 * it will race with cgroup_exit() in that the list
1585 * entry won't be deleted though the process has exited.
1586 * Do it while holding siglock so that we don't end up
1587 * racing against cgroup_exit().
1589 * Interrupts were already disabled while acquiring
1590 * the css_set_lock, so we do not need to disable it
1591 * again when acquiring the sighand->siglock here.
1593 spin_lock(&p
->sighand
->siglock
);
1594 if (!(p
->flags
& PF_EXITING
)) {
1595 struct css_set
*cset
= task_css_set(p
);
1597 if (!css_set_populated(cset
))
1598 css_set_update_populated(cset
, true);
1599 list_add_tail(&p
->cg_list
, &cset
->tasks
);
1602 spin_unlock(&p
->sighand
->siglock
);
1603 } while_each_thread(g
, p
);
1604 read_unlock(&tasklist_lock
);
1606 spin_unlock_irq(&css_set_lock
);
1609 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1611 struct cgroup_subsys
*ss
;
1614 INIT_LIST_HEAD(&cgrp
->self
.sibling
);
1615 INIT_LIST_HEAD(&cgrp
->self
.children
);
1616 INIT_LIST_HEAD(&cgrp
->cset_links
);
1617 INIT_LIST_HEAD(&cgrp
->pidlists
);
1618 mutex_init(&cgrp
->pidlist_mutex
);
1619 cgrp
->self
.cgroup
= cgrp
;
1620 cgrp
->self
.flags
|= CSS_ONLINE
;
1622 for_each_subsys(ss
, ssid
)
1623 INIT_LIST_HEAD(&cgrp
->e_csets
[ssid
]);
1625 init_waitqueue_head(&cgrp
->offline_waitq
);
1626 INIT_WORK(&cgrp
->release_agent_work
, cgroup1_release_agent
);
1629 void init_cgroup_root(struct cgroup_root
*root
, struct cgroup_sb_opts
*opts
)
1631 struct cgroup
*cgrp
= &root
->cgrp
;
1633 INIT_LIST_HEAD(&root
->root_list
);
1634 atomic_set(&root
->nr_cgrps
, 1);
1636 init_cgroup_housekeeping(cgrp
);
1637 idr_init(&root
->cgroup_idr
);
1639 root
->flags
= opts
->flags
;
1640 if (opts
->release_agent
)
1641 strcpy(root
->release_agent_path
, opts
->release_agent
);
1643 strcpy(root
->name
, opts
->name
);
1644 if (opts
->cpuset_clone_children
)
1645 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->cgrp
.flags
);
1648 int cgroup_setup_root(struct cgroup_root
*root
, u16 ss_mask
, int ref_flags
)
1650 LIST_HEAD(tmp_links
);
1651 struct cgroup
*root_cgrp
= &root
->cgrp
;
1652 struct kernfs_syscall_ops
*kf_sops
;
1653 struct css_set
*cset
;
1656 lockdep_assert_held(&cgroup_mutex
);
1658 ret
= cgroup_idr_alloc(&root
->cgroup_idr
, root_cgrp
, 1, 2, GFP_KERNEL
);
1661 root_cgrp
->id
= ret
;
1662 root_cgrp
->ancestor_ids
[0] = ret
;
1664 ret
= percpu_ref_init(&root_cgrp
->self
.refcnt
, css_release
,
1665 ref_flags
, GFP_KERNEL
);
1670 * We're accessing css_set_count without locking css_set_lock here,
1671 * but that's OK - it can only be increased by someone holding
1672 * cgroup_lock, and that's us. Later rebinding may disable
1673 * controllers on the default hierarchy and thus create new csets,
1674 * which can't be more than the existing ones. Allocate 2x.
1676 ret
= allocate_cgrp_cset_links(2 * css_set_count
, &tmp_links
);
1680 ret
= cgroup_init_root_id(root
);
1684 kf_sops
= root
== &cgrp_dfl_root
?
1685 &cgroup_kf_syscall_ops
: &cgroup1_kf_syscall_ops
;
1687 root
->kf_root
= kernfs_create_root(kf_sops
,
1688 KERNFS_ROOT_CREATE_DEACTIVATED
,
1690 if (IS_ERR(root
->kf_root
)) {
1691 ret
= PTR_ERR(root
->kf_root
);
1694 root_cgrp
->kn
= root
->kf_root
->kn
;
1696 ret
= css_populate_dir(&root_cgrp
->self
);
1700 ret
= rebind_subsystems(root
, ss_mask
);
1704 trace_cgroup_setup_root(root
);
1707 * There must be no failure case after here, since rebinding takes
1708 * care of subsystems' refcounts, which are explicitly dropped in
1709 * the failure exit path.
1711 list_add(&root
->root_list
, &cgroup_roots
);
1712 cgroup_root_count
++;
1715 * Link the root cgroup in this hierarchy into all the css_set
1718 spin_lock_irq(&css_set_lock
);
1719 hash_for_each(css_set_table
, i
, cset
, hlist
) {
1720 link_css_set(&tmp_links
, cset
, root_cgrp
);
1721 if (css_set_populated(cset
))
1722 cgroup_update_populated(root_cgrp
, true);
1724 spin_unlock_irq(&css_set_lock
);
1726 BUG_ON(!list_empty(&root_cgrp
->self
.children
));
1727 BUG_ON(atomic_read(&root
->nr_cgrps
) != 1);
1729 kernfs_activate(root_cgrp
->kn
);
1734 kernfs_destroy_root(root
->kf_root
);
1735 root
->kf_root
= NULL
;
1737 cgroup_exit_root_id(root
);
1739 percpu_ref_exit(&root_cgrp
->self
.refcnt
);
1741 free_cgrp_cset_links(&tmp_links
);
1745 struct dentry
*cgroup_do_mount(struct file_system_type
*fs_type
, int flags
,
1746 struct cgroup_root
*root
, unsigned long magic
,
1747 struct cgroup_namespace
*ns
)
1749 struct dentry
*dentry
;
1752 dentry
= kernfs_mount(fs_type
, flags
, root
->kf_root
, magic
, &new_sb
);
1755 * In non-init cgroup namespace, instead of root cgroup's dentry,
1756 * we return the dentry corresponding to the cgroupns->root_cgrp.
1758 if (!IS_ERR(dentry
) && ns
!= &init_cgroup_ns
) {
1759 struct dentry
*nsdentry
;
1760 struct cgroup
*cgrp
;
1762 mutex_lock(&cgroup_mutex
);
1763 spin_lock_irq(&css_set_lock
);
1765 cgrp
= cset_cgroup_from_root(ns
->root_cset
, root
);
1767 spin_unlock_irq(&css_set_lock
);
1768 mutex_unlock(&cgroup_mutex
);
1770 nsdentry
= kernfs_node_dentry(cgrp
->kn
, dentry
->d_sb
);
1775 if (IS_ERR(dentry
) || !new_sb
)
1776 cgroup_put(&root
->cgrp
);
1781 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1782 int flags
, const char *unused_dev_name
,
1785 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
1786 struct dentry
*dentry
;
1790 /* Check if the caller has permission to mount. */
1791 if (!ns_capable(ns
->user_ns
, CAP_SYS_ADMIN
)) {
1793 return ERR_PTR(-EPERM
);
1797 * The first time anyone tries to mount a cgroup, enable the list
1798 * linking each css_set to its tasks and fix up all existing tasks.
1800 if (!use_task_css_set_links
)
1801 cgroup_enable_task_cg_lists();
1803 if (fs_type
== &cgroup2_fs_type
) {
1805 pr_err("cgroup2: unknown option \"%s\"\n", (char *)data
);
1807 return ERR_PTR(-EINVAL
);
1809 cgrp_dfl_visible
= true;
1810 cgroup_get_live(&cgrp_dfl_root
.cgrp
);
1812 dentry
= cgroup_do_mount(&cgroup2_fs_type
, flags
, &cgrp_dfl_root
,
1813 CGROUP2_SUPER_MAGIC
, ns
);
1815 dentry
= cgroup1_mount(&cgroup_fs_type
, flags
, data
,
1816 CGROUP_SUPER_MAGIC
, ns
);
1823 static void cgroup_kill_sb(struct super_block
*sb
)
1825 struct kernfs_root
*kf_root
= kernfs_root_from_sb(sb
);
1826 struct cgroup_root
*root
= cgroup_root_from_kf(kf_root
);
1829 * If @root doesn't have any mounts or children, start killing it.
1830 * This prevents new mounts by disabling percpu_ref_tryget_live().
1831 * cgroup_mount() may wait for @root's release.
1833 * And don't kill the default root.
1835 if (!list_empty(&root
->cgrp
.self
.children
) ||
1836 root
== &cgrp_dfl_root
)
1837 cgroup_put(&root
->cgrp
);
1839 percpu_ref_kill(&root
->cgrp
.self
.refcnt
);
1844 struct file_system_type cgroup_fs_type
= {
1846 .mount
= cgroup_mount
,
1847 .kill_sb
= cgroup_kill_sb
,
1848 .fs_flags
= FS_USERNS_MOUNT
,
1851 static struct file_system_type cgroup2_fs_type
= {
1853 .mount
= cgroup_mount
,
1854 .kill_sb
= cgroup_kill_sb
,
1855 .fs_flags
= FS_USERNS_MOUNT
,
1858 int cgroup_path_ns_locked(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
1859 struct cgroup_namespace
*ns
)
1861 struct cgroup
*root
= cset_cgroup_from_root(ns
->root_cset
, cgrp
->root
);
1863 return kernfs_path_from_node(cgrp
->kn
, root
->kn
, buf
, buflen
);
1866 int cgroup_path_ns(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
1867 struct cgroup_namespace
*ns
)
1871 mutex_lock(&cgroup_mutex
);
1872 spin_lock_irq(&css_set_lock
);
1874 ret
= cgroup_path_ns_locked(cgrp
, buf
, buflen
, ns
);
1876 spin_unlock_irq(&css_set_lock
);
1877 mutex_unlock(&cgroup_mutex
);
1881 EXPORT_SYMBOL_GPL(cgroup_path_ns
);
1884 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1885 * @task: target task
1886 * @buf: the buffer to write the path into
1887 * @buflen: the length of the buffer
1889 * Determine @task's cgroup on the first (the one with the lowest non-zero
1890 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1891 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1892 * cgroup controller callbacks.
1894 * Return value is the same as kernfs_path().
1896 int task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
1898 struct cgroup_root
*root
;
1899 struct cgroup
*cgrp
;
1900 int hierarchy_id
= 1;
1903 mutex_lock(&cgroup_mutex
);
1904 spin_lock_irq(&css_set_lock
);
1906 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
1909 cgrp
= task_cgroup_from_root(task
, root
);
1910 ret
= cgroup_path_ns_locked(cgrp
, buf
, buflen
, &init_cgroup_ns
);
1912 /* if no hierarchy exists, everyone is in "/" */
1913 ret
= strlcpy(buf
, "/", buflen
);
1916 spin_unlock_irq(&css_set_lock
);
1917 mutex_unlock(&cgroup_mutex
);
1920 EXPORT_SYMBOL_GPL(task_cgroup_path
);
1923 * cgroup_migrate_add_task - add a migration target task to a migration context
1924 * @task: target task
1925 * @mgctx: target migration context
1927 * Add @task, which is a migration target, to @mgctx->tset. This function
1928 * becomes noop if @task doesn't need to be migrated. @task's css_set
1929 * should have been added as a migration source and @task->cg_list will be
1930 * moved from the css_set's tasks list to mg_tasks one.
1932 static void cgroup_migrate_add_task(struct task_struct
*task
,
1933 struct cgroup_mgctx
*mgctx
)
1935 struct css_set
*cset
;
1937 lockdep_assert_held(&css_set_lock
);
1939 /* @task either already exited or can't exit until the end */
1940 if (task
->flags
& PF_EXITING
)
1943 /* leave @task alone if post_fork() hasn't linked it yet */
1944 if (list_empty(&task
->cg_list
))
1947 cset
= task_css_set(task
);
1948 if (!cset
->mg_src_cgrp
)
1951 list_move_tail(&task
->cg_list
, &cset
->mg_tasks
);
1952 if (list_empty(&cset
->mg_node
))
1953 list_add_tail(&cset
->mg_node
,
1954 &mgctx
->tset
.src_csets
);
1955 if (list_empty(&cset
->mg_dst_cset
->mg_node
))
1956 list_add_tail(&cset
->mg_dst_cset
->mg_node
,
1957 &mgctx
->tset
.dst_csets
);
1961 * cgroup_taskset_first - reset taskset and return the first task
1962 * @tset: taskset of interest
1963 * @dst_cssp: output variable for the destination css
1965 * @tset iteration is initialized and the first task is returned.
1967 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
,
1968 struct cgroup_subsys_state
**dst_cssp
)
1970 tset
->cur_cset
= list_first_entry(tset
->csets
, struct css_set
, mg_node
);
1971 tset
->cur_task
= NULL
;
1973 return cgroup_taskset_next(tset
, dst_cssp
);
1977 * cgroup_taskset_next - iterate to the next task in taskset
1978 * @tset: taskset of interest
1979 * @dst_cssp: output variable for the destination css
1981 * Return the next task in @tset. Iteration must have been initialized
1982 * with cgroup_taskset_first().
1984 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
,
1985 struct cgroup_subsys_state
**dst_cssp
)
1987 struct css_set
*cset
= tset
->cur_cset
;
1988 struct task_struct
*task
= tset
->cur_task
;
1990 while (&cset
->mg_node
!= tset
->csets
) {
1992 task
= list_first_entry(&cset
->mg_tasks
,
1993 struct task_struct
, cg_list
);
1995 task
= list_next_entry(task
, cg_list
);
1997 if (&task
->cg_list
!= &cset
->mg_tasks
) {
1998 tset
->cur_cset
= cset
;
1999 tset
->cur_task
= task
;
2002 * This function may be called both before and
2003 * after cgroup_taskset_migrate(). The two cases
2004 * can be distinguished by looking at whether @cset
2005 * has its ->mg_dst_cset set.
2007 if (cset
->mg_dst_cset
)
2008 *dst_cssp
= cset
->mg_dst_cset
->subsys
[tset
->ssid
];
2010 *dst_cssp
= cset
->subsys
[tset
->ssid
];
2015 cset
= list_next_entry(cset
, mg_node
);
2023 * cgroup_taskset_migrate - migrate a taskset
2024 * @mgctx: migration context
2026 * Migrate tasks in @mgctx as setup by migration preparation functions.
2027 * This function fails iff one of the ->can_attach callbacks fails and
2028 * guarantees that either all or none of the tasks in @mgctx are migrated.
2029 * @mgctx is consumed regardless of success.
2031 static int cgroup_migrate_execute(struct cgroup_mgctx
*mgctx
)
2033 struct cgroup_taskset
*tset
= &mgctx
->tset
;
2034 struct cgroup_subsys
*ss
;
2035 struct task_struct
*task
, *tmp_task
;
2036 struct css_set
*cset
, *tmp_cset
;
2037 int ssid
, failed_ssid
, ret
;
2039 /* methods shouldn't be called if no task is actually migrating */
2040 if (list_empty(&tset
->src_csets
))
2043 /* check that we can legitimately attach to the cgroup */
2044 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2045 if (ss
->can_attach
) {
2047 ret
= ss
->can_attach(tset
);
2050 goto out_cancel_attach
;
2053 } while_each_subsys_mask();
2056 * Now that we're guaranteed success, proceed to move all tasks to
2057 * the new cgroup. There are no failure cases after here, so this
2058 * is the commit point.
2060 spin_lock_irq(&css_set_lock
);
2061 list_for_each_entry(cset
, &tset
->src_csets
, mg_node
) {
2062 list_for_each_entry_safe(task
, tmp_task
, &cset
->mg_tasks
, cg_list
) {
2063 struct css_set
*from_cset
= task_css_set(task
);
2064 struct css_set
*to_cset
= cset
->mg_dst_cset
;
2066 get_css_set(to_cset
);
2067 css_set_move_task(task
, from_cset
, to_cset
, true);
2068 put_css_set_locked(from_cset
);
2071 spin_unlock_irq(&css_set_lock
);
2074 * Migration is committed, all target tasks are now on dst_csets.
2075 * Nothing is sensitive to fork() after this point. Notify
2076 * controllers that migration is complete.
2078 tset
->csets
= &tset
->dst_csets
;
2080 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2085 } while_each_subsys_mask();
2088 goto out_release_tset
;
2091 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2092 if (ssid
== failed_ssid
)
2094 if (ss
->cancel_attach
) {
2096 ss
->cancel_attach(tset
);
2098 } while_each_subsys_mask();
2100 spin_lock_irq(&css_set_lock
);
2101 list_splice_init(&tset
->dst_csets
, &tset
->src_csets
);
2102 list_for_each_entry_safe(cset
, tmp_cset
, &tset
->src_csets
, mg_node
) {
2103 list_splice_tail_init(&cset
->mg_tasks
, &cset
->tasks
);
2104 list_del_init(&cset
->mg_node
);
2106 spin_unlock_irq(&css_set_lock
);
2111 * cgroup_may_migrate_to - verify whether a cgroup can be migration destination
2112 * @dst_cgrp: destination cgroup to test
2114 * On the default hierarchy, except for the root, subtree_control must be
2115 * zero for migration destination cgroups with tasks so that child cgroups
2116 * don't compete against tasks.
2118 bool cgroup_may_migrate_to(struct cgroup
*dst_cgrp
)
2120 return !cgroup_on_dfl(dst_cgrp
) || !cgroup_parent(dst_cgrp
) ||
2121 !dst_cgrp
->subtree_control
;
2125 * cgroup_migrate_finish - cleanup after attach
2126 * @mgctx: migration context
2128 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2129 * those functions for details.
2131 void cgroup_migrate_finish(struct cgroup_mgctx
*mgctx
)
2133 LIST_HEAD(preloaded
);
2134 struct css_set
*cset
, *tmp_cset
;
2136 lockdep_assert_held(&cgroup_mutex
);
2138 spin_lock_irq(&css_set_lock
);
2140 list_splice_tail_init(&mgctx
->preloaded_src_csets
, &preloaded
);
2141 list_splice_tail_init(&mgctx
->preloaded_dst_csets
, &preloaded
);
2143 list_for_each_entry_safe(cset
, tmp_cset
, &preloaded
, mg_preload_node
) {
2144 cset
->mg_src_cgrp
= NULL
;
2145 cset
->mg_dst_cgrp
= NULL
;
2146 cset
->mg_dst_cset
= NULL
;
2147 list_del_init(&cset
->mg_preload_node
);
2148 put_css_set_locked(cset
);
2151 spin_unlock_irq(&css_set_lock
);
2155 * cgroup_migrate_add_src - add a migration source css_set
2156 * @src_cset: the source css_set to add
2157 * @dst_cgrp: the destination cgroup
2158 * @mgctx: migration context
2160 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2161 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
2162 * up by cgroup_migrate_finish().
2164 * This function may be called without holding cgroup_threadgroup_rwsem
2165 * even if the target is a process. Threads may be created and destroyed
2166 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2167 * into play and the preloaded css_sets are guaranteed to cover all
2170 void cgroup_migrate_add_src(struct css_set
*src_cset
,
2171 struct cgroup
*dst_cgrp
,
2172 struct cgroup_mgctx
*mgctx
)
2174 struct cgroup
*src_cgrp
;
2176 lockdep_assert_held(&cgroup_mutex
);
2177 lockdep_assert_held(&css_set_lock
);
2180 * If ->dead, @src_set is associated with one or more dead cgroups
2181 * and doesn't contain any migratable tasks. Ignore it early so
2182 * that the rest of migration path doesn't get confused by it.
2187 src_cgrp
= cset_cgroup_from_root(src_cset
, dst_cgrp
->root
);
2189 if (!list_empty(&src_cset
->mg_preload_node
))
2192 WARN_ON(src_cset
->mg_src_cgrp
);
2193 WARN_ON(src_cset
->mg_dst_cgrp
);
2194 WARN_ON(!list_empty(&src_cset
->mg_tasks
));
2195 WARN_ON(!list_empty(&src_cset
->mg_node
));
2197 src_cset
->mg_src_cgrp
= src_cgrp
;
2198 src_cset
->mg_dst_cgrp
= dst_cgrp
;
2199 get_css_set(src_cset
);
2200 list_add_tail(&src_cset
->mg_preload_node
, &mgctx
->preloaded_src_csets
);
2204 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
2205 * @mgctx: migration context
2207 * Tasks are about to be moved and all the source css_sets have been
2208 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2209 * pins all destination css_sets, links each to its source, and append them
2210 * to @mgctx->preloaded_dst_csets.
2212 * This function must be called after cgroup_migrate_add_src() has been
2213 * called on each migration source css_set. After migration is performed
2214 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2217 int cgroup_migrate_prepare_dst(struct cgroup_mgctx
*mgctx
)
2219 struct css_set
*src_cset
, *tmp_cset
;
2221 lockdep_assert_held(&cgroup_mutex
);
2223 /* look up the dst cset for each src cset and link it to src */
2224 list_for_each_entry_safe(src_cset
, tmp_cset
, &mgctx
->preloaded_src_csets
,
2226 struct css_set
*dst_cset
;
2227 struct cgroup_subsys
*ss
;
2230 dst_cset
= find_css_set(src_cset
, src_cset
->mg_dst_cgrp
);
2234 WARN_ON_ONCE(src_cset
->mg_dst_cset
|| dst_cset
->mg_dst_cset
);
2237 * If src cset equals dst, it's noop. Drop the src.
2238 * cgroup_migrate() will skip the cset too. Note that we
2239 * can't handle src == dst as some nodes are used by both.
2241 if (src_cset
== dst_cset
) {
2242 src_cset
->mg_src_cgrp
= NULL
;
2243 src_cset
->mg_dst_cgrp
= NULL
;
2244 list_del_init(&src_cset
->mg_preload_node
);
2245 put_css_set(src_cset
);
2246 put_css_set(dst_cset
);
2250 src_cset
->mg_dst_cset
= dst_cset
;
2252 if (list_empty(&dst_cset
->mg_preload_node
))
2253 list_add_tail(&dst_cset
->mg_preload_node
,
2254 &mgctx
->preloaded_dst_csets
);
2256 put_css_set(dst_cset
);
2258 for_each_subsys(ss
, ssid
)
2259 if (src_cset
->subsys
[ssid
] != dst_cset
->subsys
[ssid
])
2260 mgctx
->ss_mask
|= 1 << ssid
;
2265 cgroup_migrate_finish(mgctx
);
2270 * cgroup_migrate - migrate a process or task to a cgroup
2271 * @leader: the leader of the process or the task to migrate
2272 * @threadgroup: whether @leader points to the whole process or a single task
2273 * @mgctx: migration context
2275 * Migrate a process or task denoted by @leader. If migrating a process,
2276 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2277 * responsible for invoking cgroup_migrate_add_src() and
2278 * cgroup_migrate_prepare_dst() on the targets before invoking this
2279 * function and following up with cgroup_migrate_finish().
2281 * As long as a controller's ->can_attach() doesn't fail, this function is
2282 * guaranteed to succeed. This means that, excluding ->can_attach()
2283 * failure, when migrating multiple targets, the success or failure can be
2284 * decided for all targets by invoking group_migrate_prepare_dst() before
2285 * actually starting migrating.
2287 int cgroup_migrate(struct task_struct
*leader
, bool threadgroup
,
2288 struct cgroup_mgctx
*mgctx
)
2290 struct task_struct
*task
;
2293 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2294 * already PF_EXITING could be freed from underneath us unless we
2295 * take an rcu_read_lock.
2297 spin_lock_irq(&css_set_lock
);
2301 cgroup_migrate_add_task(task
, mgctx
);
2304 } while_each_thread(leader
, task
);
2306 spin_unlock_irq(&css_set_lock
);
2308 return cgroup_migrate_execute(mgctx
);
2312 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2313 * @dst_cgrp: the cgroup to attach to
2314 * @leader: the task or the leader of the threadgroup to be attached
2315 * @threadgroup: attach the whole threadgroup?
2317 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
2319 int cgroup_attach_task(struct cgroup
*dst_cgrp
, struct task_struct
*leader
,
2322 DEFINE_CGROUP_MGCTX(mgctx
);
2323 struct task_struct
*task
;
2326 if (!cgroup_may_migrate_to(dst_cgrp
))
2329 /* look up all src csets */
2330 spin_lock_irq(&css_set_lock
);
2334 cgroup_migrate_add_src(task_css_set(task
), dst_cgrp
, &mgctx
);
2337 } while_each_thread(leader
, task
);
2339 spin_unlock_irq(&css_set_lock
);
2341 /* prepare dst csets and commit */
2342 ret
= cgroup_migrate_prepare_dst(&mgctx
);
2344 ret
= cgroup_migrate(leader
, threadgroup
, &mgctx
);
2346 cgroup_migrate_finish(&mgctx
);
2349 trace_cgroup_attach_task(dst_cgrp
, leader
, threadgroup
);
2354 static int cgroup_procs_write_permission(struct task_struct
*task
,
2355 struct cgroup
*dst_cgrp
,
2356 struct kernfs_open_file
*of
)
2360 if (cgroup_on_dfl(dst_cgrp
)) {
2361 struct super_block
*sb
= of
->file
->f_path
.dentry
->d_sb
;
2362 struct cgroup
*cgrp
;
2363 struct inode
*inode
;
2365 spin_lock_irq(&css_set_lock
);
2366 cgrp
= task_cgroup_from_root(task
, &cgrp_dfl_root
);
2367 spin_unlock_irq(&css_set_lock
);
2369 while (!cgroup_is_descendant(dst_cgrp
, cgrp
))
2370 cgrp
= cgroup_parent(cgrp
);
2373 inode
= kernfs_get_inode(sb
, cgrp
->procs_file
.kn
);
2375 ret
= inode_permission(inode
, MAY_WRITE
);
2379 const struct cred
*cred
= current_cred();
2380 const struct cred
*tcred
= get_task_cred(task
);
2383 * even if we're attaching all tasks in the thread group,
2384 * we only need to check permissions on one of them.
2386 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2387 !uid_eq(cred
->euid
, tcred
->uid
) &&
2388 !uid_eq(cred
->euid
, tcred
->suid
))
2397 * Find the task_struct of the task to attach by vpid and pass it along to the
2398 * function to attach either it or all tasks in its threadgroup. Will lock
2399 * cgroup_mutex and threadgroup.
2401 ssize_t
__cgroup_procs_write(struct kernfs_open_file
*of
, char *buf
,
2402 size_t nbytes
, loff_t off
, bool threadgroup
)
2404 struct task_struct
*tsk
;
2405 struct cgroup_subsys
*ss
;
2406 struct cgroup
*cgrp
;
2410 if (kstrtoint(strstrip(buf
), 0, &pid
) || pid
< 0)
2413 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
2417 percpu_down_write(&cgroup_threadgroup_rwsem
);
2420 tsk
= find_task_by_vpid(pid
);
2423 goto out_unlock_rcu
;
2430 tsk
= tsk
->group_leader
;
2433 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2434 * If userland migrates such a kthread to a non-root cgroup, it can
2435 * become trapped in a cpuset, or RT kthread may be born in a
2436 * cgroup with no rt_runtime allocated. Just say no.
2438 if (tsk
->no_cgroup_migration
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2440 goto out_unlock_rcu
;
2443 get_task_struct(tsk
);
2446 ret
= cgroup_procs_write_permission(tsk
, cgrp
, of
);
2448 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2450 put_task_struct(tsk
);
2451 goto out_unlock_threadgroup
;
2455 out_unlock_threadgroup
:
2456 percpu_up_write(&cgroup_threadgroup_rwsem
);
2457 for_each_subsys(ss
, ssid
)
2458 if (ss
->post_attach
)
2460 cgroup_kn_unlock(of
->kn
);
2461 return ret
?: nbytes
;
2464 ssize_t
cgroup_procs_write(struct kernfs_open_file
*of
, char *buf
, size_t nbytes
,
2467 return __cgroup_procs_write(of
, buf
, nbytes
, off
, true);
2470 static void cgroup_print_ss_mask(struct seq_file
*seq
, u16 ss_mask
)
2472 struct cgroup_subsys
*ss
;
2473 bool printed
= false;
2476 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
2479 seq_printf(seq
, "%s", ss
->name
);
2481 } while_each_subsys_mask();
2483 seq_putc(seq
, '\n');
2486 /* show controllers which are enabled from the parent */
2487 static int cgroup_controllers_show(struct seq_file
*seq
, void *v
)
2489 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2491 cgroup_print_ss_mask(seq
, cgroup_control(cgrp
));
2495 /* show controllers which are enabled for a given cgroup's children */
2496 static int cgroup_subtree_control_show(struct seq_file
*seq
, void *v
)
2498 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2500 cgroup_print_ss_mask(seq
, cgrp
->subtree_control
);
2505 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2506 * @cgrp: root of the subtree to update csses for
2508 * @cgrp's control masks have changed and its subtree's css associations
2509 * need to be updated accordingly. This function looks up all css_sets
2510 * which are attached to the subtree, creates the matching updated css_sets
2511 * and migrates the tasks to the new ones.
2513 static int cgroup_update_dfl_csses(struct cgroup
*cgrp
)
2515 DEFINE_CGROUP_MGCTX(mgctx
);
2516 struct cgroup_subsys_state
*d_css
;
2517 struct cgroup
*dsct
;
2518 struct css_set
*src_cset
;
2521 lockdep_assert_held(&cgroup_mutex
);
2523 percpu_down_write(&cgroup_threadgroup_rwsem
);
2525 /* look up all csses currently attached to @cgrp's subtree */
2526 spin_lock_irq(&css_set_lock
);
2527 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2528 struct cgrp_cset_link
*link
;
2530 list_for_each_entry(link
, &dsct
->cset_links
, cset_link
)
2531 cgroup_migrate_add_src(link
->cset
, dsct
, &mgctx
);
2533 spin_unlock_irq(&css_set_lock
);
2535 /* NULL dst indicates self on default hierarchy */
2536 ret
= cgroup_migrate_prepare_dst(&mgctx
);
2540 spin_lock_irq(&css_set_lock
);
2541 list_for_each_entry(src_cset
, &mgctx
.preloaded_src_csets
, mg_preload_node
) {
2542 struct task_struct
*task
, *ntask
;
2544 /* all tasks in src_csets need to be migrated */
2545 list_for_each_entry_safe(task
, ntask
, &src_cset
->tasks
, cg_list
)
2546 cgroup_migrate_add_task(task
, &mgctx
);
2548 spin_unlock_irq(&css_set_lock
);
2550 ret
= cgroup_migrate_execute(&mgctx
);
2552 cgroup_migrate_finish(&mgctx
);
2553 percpu_up_write(&cgroup_threadgroup_rwsem
);
2558 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
2559 * @cgrp: root of the target subtree
2561 * Because css offlining is asynchronous, userland may try to re-enable a
2562 * controller while the previous css is still around. This function grabs
2563 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
2565 void cgroup_lock_and_drain_offline(struct cgroup
*cgrp
)
2566 __acquires(&cgroup_mutex
)
2568 struct cgroup
*dsct
;
2569 struct cgroup_subsys_state
*d_css
;
2570 struct cgroup_subsys
*ss
;
2574 mutex_lock(&cgroup_mutex
);
2576 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2577 for_each_subsys(ss
, ssid
) {
2578 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2581 if (!css
|| !percpu_ref_is_dying(&css
->refcnt
))
2584 cgroup_get_live(dsct
);
2585 prepare_to_wait(&dsct
->offline_waitq
, &wait
,
2586 TASK_UNINTERRUPTIBLE
);
2588 mutex_unlock(&cgroup_mutex
);
2590 finish_wait(&dsct
->offline_waitq
, &wait
);
2599 * cgroup_save_control - save control masks of a subtree
2600 * @cgrp: root of the target subtree
2602 * Save ->subtree_control and ->subtree_ss_mask to the respective old_
2603 * prefixed fields for @cgrp's subtree including @cgrp itself.
2605 static void cgroup_save_control(struct cgroup
*cgrp
)
2607 struct cgroup
*dsct
;
2608 struct cgroup_subsys_state
*d_css
;
2610 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2611 dsct
->old_subtree_control
= dsct
->subtree_control
;
2612 dsct
->old_subtree_ss_mask
= dsct
->subtree_ss_mask
;
2617 * cgroup_propagate_control - refresh control masks of a subtree
2618 * @cgrp: root of the target subtree
2620 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2621 * ->subtree_control and propagate controller availability through the
2622 * subtree so that descendants don't have unavailable controllers enabled.
2624 static void cgroup_propagate_control(struct cgroup
*cgrp
)
2626 struct cgroup
*dsct
;
2627 struct cgroup_subsys_state
*d_css
;
2629 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2630 dsct
->subtree_control
&= cgroup_control(dsct
);
2631 dsct
->subtree_ss_mask
=
2632 cgroup_calc_subtree_ss_mask(dsct
->subtree_control
,
2633 cgroup_ss_mask(dsct
));
2638 * cgroup_restore_control - restore control masks of a subtree
2639 * @cgrp: root of the target subtree
2641 * Restore ->subtree_control and ->subtree_ss_mask from the respective old_
2642 * prefixed fields for @cgrp's subtree including @cgrp itself.
2644 static void cgroup_restore_control(struct cgroup
*cgrp
)
2646 struct cgroup
*dsct
;
2647 struct cgroup_subsys_state
*d_css
;
2649 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2650 dsct
->subtree_control
= dsct
->old_subtree_control
;
2651 dsct
->subtree_ss_mask
= dsct
->old_subtree_ss_mask
;
2655 static bool css_visible(struct cgroup_subsys_state
*css
)
2657 struct cgroup_subsys
*ss
= css
->ss
;
2658 struct cgroup
*cgrp
= css
->cgroup
;
2660 if (cgroup_control(cgrp
) & (1 << ss
->id
))
2662 if (!(cgroup_ss_mask(cgrp
) & (1 << ss
->id
)))
2664 return cgroup_on_dfl(cgrp
) && ss
->implicit_on_dfl
;
2668 * cgroup_apply_control_enable - enable or show csses according to control
2669 * @cgrp: root of the target subtree
2671 * Walk @cgrp's subtree and create new csses or make the existing ones
2672 * visible. A css is created invisible if it's being implicitly enabled
2673 * through dependency. An invisible css is made visible when the userland
2674 * explicitly enables it.
2676 * Returns 0 on success, -errno on failure. On failure, csses which have
2677 * been processed already aren't cleaned up. The caller is responsible for
2678 * cleaning up with cgroup_apply_control_disable().
2680 static int cgroup_apply_control_enable(struct cgroup
*cgrp
)
2682 struct cgroup
*dsct
;
2683 struct cgroup_subsys_state
*d_css
;
2684 struct cgroup_subsys
*ss
;
2687 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2688 for_each_subsys(ss
, ssid
) {
2689 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2691 WARN_ON_ONCE(css
&& percpu_ref_is_dying(&css
->refcnt
));
2693 if (!(cgroup_ss_mask(dsct
) & (1 << ss
->id
)))
2697 css
= css_create(dsct
, ss
);
2699 return PTR_ERR(css
);
2702 if (css_visible(css
)) {
2703 ret
= css_populate_dir(css
);
2714 * cgroup_apply_control_disable - kill or hide csses according to control
2715 * @cgrp: root of the target subtree
2717 * Walk @cgrp's subtree and kill and hide csses so that they match
2718 * cgroup_ss_mask() and cgroup_visible_mask().
2720 * A css is hidden when the userland requests it to be disabled while other
2721 * subsystems are still depending on it. The css must not actively control
2722 * resources and be in the vanilla state if it's made visible again later.
2723 * Controllers which may be depended upon should provide ->css_reset() for
2726 static void cgroup_apply_control_disable(struct cgroup
*cgrp
)
2728 struct cgroup
*dsct
;
2729 struct cgroup_subsys_state
*d_css
;
2730 struct cgroup_subsys
*ss
;
2733 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2734 for_each_subsys(ss
, ssid
) {
2735 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2737 WARN_ON_ONCE(css
&& percpu_ref_is_dying(&css
->refcnt
));
2743 !(cgroup_ss_mask(dsct
) & (1 << ss
->id
))) {
2745 } else if (!css_visible(css
)) {
2755 * cgroup_apply_control - apply control mask updates to the subtree
2756 * @cgrp: root of the target subtree
2758 * subsystems can be enabled and disabled in a subtree using the following
2761 * 1. Call cgroup_save_control() to stash the current state.
2762 * 2. Update ->subtree_control masks in the subtree as desired.
2763 * 3. Call cgroup_apply_control() to apply the changes.
2764 * 4. Optionally perform other related operations.
2765 * 5. Call cgroup_finalize_control() to finish up.
2767 * This function implements step 3 and propagates the mask changes
2768 * throughout @cgrp's subtree, updates csses accordingly and perform
2769 * process migrations.
2771 static int cgroup_apply_control(struct cgroup
*cgrp
)
2775 cgroup_propagate_control(cgrp
);
2777 ret
= cgroup_apply_control_enable(cgrp
);
2782 * At this point, cgroup_e_css() results reflect the new csses
2783 * making the following cgroup_update_dfl_csses() properly update
2784 * css associations of all tasks in the subtree.
2786 ret
= cgroup_update_dfl_csses(cgrp
);
2794 * cgroup_finalize_control - finalize control mask update
2795 * @cgrp: root of the target subtree
2796 * @ret: the result of the update
2798 * Finalize control mask update. See cgroup_apply_control() for more info.
2800 static void cgroup_finalize_control(struct cgroup
*cgrp
, int ret
)
2803 cgroup_restore_control(cgrp
);
2804 cgroup_propagate_control(cgrp
);
2807 cgroup_apply_control_disable(cgrp
);
2810 /* change the enabled child controllers for a cgroup in the default hierarchy */
2811 static ssize_t
cgroup_subtree_control_write(struct kernfs_open_file
*of
,
2812 char *buf
, size_t nbytes
,
2815 u16 enable
= 0, disable
= 0;
2816 struct cgroup
*cgrp
, *child
;
2817 struct cgroup_subsys
*ss
;
2822 * Parse input - space separated list of subsystem names prefixed
2823 * with either + or -.
2825 buf
= strstrip(buf
);
2826 while ((tok
= strsep(&buf
, " "))) {
2829 do_each_subsys_mask(ss
, ssid
, ~cgrp_dfl_inhibit_ss_mask
) {
2830 if (!cgroup_ssid_enabled(ssid
) ||
2831 strcmp(tok
+ 1, ss
->name
))
2835 enable
|= 1 << ssid
;
2836 disable
&= ~(1 << ssid
);
2837 } else if (*tok
== '-') {
2838 disable
|= 1 << ssid
;
2839 enable
&= ~(1 << ssid
);
2844 } while_each_subsys_mask();
2845 if (ssid
== CGROUP_SUBSYS_COUNT
)
2849 cgrp
= cgroup_kn_lock_live(of
->kn
, true);
2853 for_each_subsys(ss
, ssid
) {
2854 if (enable
& (1 << ssid
)) {
2855 if (cgrp
->subtree_control
& (1 << ssid
)) {
2856 enable
&= ~(1 << ssid
);
2860 if (!(cgroup_control(cgrp
) & (1 << ssid
))) {
2864 } else if (disable
& (1 << ssid
)) {
2865 if (!(cgrp
->subtree_control
& (1 << ssid
))) {
2866 disable
&= ~(1 << ssid
);
2870 /* a child has it enabled? */
2871 cgroup_for_each_live_child(child
, cgrp
) {
2872 if (child
->subtree_control
& (1 << ssid
)) {
2880 if (!enable
&& !disable
) {
2886 * Except for the root, subtree_control must be zero for a cgroup
2887 * with tasks so that child cgroups don't compete against tasks.
2889 if (enable
&& cgroup_parent(cgrp
)) {
2890 struct cgrp_cset_link
*link
;
2893 * Because namespaces pin csets too, @cgrp->cset_links
2894 * might not be empty even when @cgrp is empty. Walk and
2897 spin_lock_irq(&css_set_lock
);
2900 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
) {
2901 if (css_set_populated(link
->cset
)) {
2907 spin_unlock_irq(&css_set_lock
);
2913 /* save and update control masks and prepare csses */
2914 cgroup_save_control(cgrp
);
2916 cgrp
->subtree_control
|= enable
;
2917 cgrp
->subtree_control
&= ~disable
;
2919 ret
= cgroup_apply_control(cgrp
);
2921 cgroup_finalize_control(cgrp
, ret
);
2923 kernfs_activate(cgrp
->kn
);
2926 cgroup_kn_unlock(of
->kn
);
2927 return ret
?: nbytes
;
2930 static int cgroup_events_show(struct seq_file
*seq
, void *v
)
2932 seq_printf(seq
, "populated %d\n",
2933 cgroup_is_populated(seq_css(seq
)->cgroup
));
2937 static int cgroup_file_open(struct kernfs_open_file
*of
)
2939 struct cftype
*cft
= of
->kn
->priv
;
2942 return cft
->open(of
);
2946 static void cgroup_file_release(struct kernfs_open_file
*of
)
2948 struct cftype
*cft
= of
->kn
->priv
;
2954 static ssize_t
cgroup_file_write(struct kernfs_open_file
*of
, char *buf
,
2955 size_t nbytes
, loff_t off
)
2957 struct cgroup
*cgrp
= of
->kn
->parent
->priv
;
2958 struct cftype
*cft
= of
->kn
->priv
;
2959 struct cgroup_subsys_state
*css
;
2963 return cft
->write(of
, buf
, nbytes
, off
);
2966 * kernfs guarantees that a file isn't deleted with operations in
2967 * flight, which means that the matching css is and stays alive and
2968 * doesn't need to be pinned. The RCU locking is not necessary
2969 * either. It's just for the convenience of using cgroup_css().
2972 css
= cgroup_css(cgrp
, cft
->ss
);
2975 if (cft
->write_u64
) {
2976 unsigned long long v
;
2977 ret
= kstrtoull(buf
, 0, &v
);
2979 ret
= cft
->write_u64(css
, cft
, v
);
2980 } else if (cft
->write_s64
) {
2982 ret
= kstrtoll(buf
, 0, &v
);
2984 ret
= cft
->write_s64(css
, cft
, v
);
2989 return ret
?: nbytes
;
2992 static void *cgroup_seqfile_start(struct seq_file
*seq
, loff_t
*ppos
)
2994 return seq_cft(seq
)->seq_start(seq
, ppos
);
2997 static void *cgroup_seqfile_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
2999 return seq_cft(seq
)->seq_next(seq
, v
, ppos
);
3002 static void cgroup_seqfile_stop(struct seq_file
*seq
, void *v
)
3004 if (seq_cft(seq
)->seq_stop
)
3005 seq_cft(seq
)->seq_stop(seq
, v
);
3008 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
3010 struct cftype
*cft
= seq_cft(m
);
3011 struct cgroup_subsys_state
*css
= seq_css(m
);
3014 return cft
->seq_show(m
, arg
);
3017 seq_printf(m
, "%llu\n", cft
->read_u64(css
, cft
));
3018 else if (cft
->read_s64
)
3019 seq_printf(m
, "%lld\n", cft
->read_s64(css
, cft
));
3025 static struct kernfs_ops cgroup_kf_single_ops
= {
3026 .atomic_write_len
= PAGE_SIZE
,
3027 .open
= cgroup_file_open
,
3028 .release
= cgroup_file_release
,
3029 .write
= cgroup_file_write
,
3030 .seq_show
= cgroup_seqfile_show
,
3033 static struct kernfs_ops cgroup_kf_ops
= {
3034 .atomic_write_len
= PAGE_SIZE
,
3035 .open
= cgroup_file_open
,
3036 .release
= cgroup_file_release
,
3037 .write
= cgroup_file_write
,
3038 .seq_start
= cgroup_seqfile_start
,
3039 .seq_next
= cgroup_seqfile_next
,
3040 .seq_stop
= cgroup_seqfile_stop
,
3041 .seq_show
= cgroup_seqfile_show
,
3044 /* set uid and gid of cgroup dirs and files to that of the creator */
3045 static int cgroup_kn_set_ugid(struct kernfs_node
*kn
)
3047 struct iattr iattr
= { .ia_valid
= ATTR_UID
| ATTR_GID
,
3048 .ia_uid
= current_fsuid(),
3049 .ia_gid
= current_fsgid(), };
3051 if (uid_eq(iattr
.ia_uid
, GLOBAL_ROOT_UID
) &&
3052 gid_eq(iattr
.ia_gid
, GLOBAL_ROOT_GID
))
3055 return kernfs_setattr(kn
, &iattr
);
3058 static int cgroup_add_file(struct cgroup_subsys_state
*css
, struct cgroup
*cgrp
,
3061 char name
[CGROUP_FILE_NAME_MAX
];
3062 struct kernfs_node
*kn
;
3063 struct lock_class_key
*key
= NULL
;
3066 #ifdef CONFIG_DEBUG_LOCK_ALLOC
3067 key
= &cft
->lockdep_key
;
3069 kn
= __kernfs_create_file(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
),
3070 cgroup_file_mode(cft
), 0, cft
->kf_ops
, cft
,
3075 ret
= cgroup_kn_set_ugid(kn
);
3081 if (cft
->file_offset
) {
3082 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
3084 spin_lock_irq(&cgroup_file_kn_lock
);
3086 spin_unlock_irq(&cgroup_file_kn_lock
);
3093 * cgroup_addrm_files - add or remove files to a cgroup directory
3094 * @css: the target css
3095 * @cgrp: the target cgroup (usually css->cgroup)
3096 * @cfts: array of cftypes to be added
3097 * @is_add: whether to add or remove
3099 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
3100 * For removals, this function never fails.
3102 static int cgroup_addrm_files(struct cgroup_subsys_state
*css
,
3103 struct cgroup
*cgrp
, struct cftype cfts
[],
3106 struct cftype
*cft
, *cft_end
= NULL
;
3109 lockdep_assert_held(&cgroup_mutex
);
3112 for (cft
= cfts
; cft
!= cft_end
&& cft
->name
[0] != '\0'; cft
++) {
3113 /* does cft->flags tell us to skip this file on @cgrp? */
3114 if ((cft
->flags
& __CFTYPE_ONLY_ON_DFL
) && !cgroup_on_dfl(cgrp
))
3116 if ((cft
->flags
& __CFTYPE_NOT_ON_DFL
) && cgroup_on_dfl(cgrp
))
3118 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgroup_parent(cgrp
))
3120 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgroup_parent(cgrp
))
3124 ret
= cgroup_add_file(css
, cgrp
, cft
);
3126 pr_warn("%s: failed to add %s, err=%d\n",
3127 __func__
, cft
->name
, ret
);
3133 cgroup_rm_file(cgrp
, cft
);
3139 static int cgroup_apply_cftypes(struct cftype
*cfts
, bool is_add
)
3142 struct cgroup_subsys
*ss
= cfts
[0].ss
;
3143 struct cgroup
*root
= &ss
->root
->cgrp
;
3144 struct cgroup_subsys_state
*css
;
3147 lockdep_assert_held(&cgroup_mutex
);
3149 /* add/rm files for all cgroups created before */
3150 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
)) {
3151 struct cgroup
*cgrp
= css
->cgroup
;
3153 if (!(css
->flags
& CSS_VISIBLE
))
3156 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, is_add
);
3162 kernfs_activate(root
->kn
);
3166 static void cgroup_exit_cftypes(struct cftype
*cfts
)
3170 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3171 /* free copy for custom atomic_write_len, see init_cftypes() */
3172 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
)
3177 /* revert flags set by cgroup core while adding @cfts */
3178 cft
->flags
&= ~(__CFTYPE_ONLY_ON_DFL
| __CFTYPE_NOT_ON_DFL
);
3182 static int cgroup_init_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3186 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3187 struct kernfs_ops
*kf_ops
;
3189 WARN_ON(cft
->ss
|| cft
->kf_ops
);
3192 kf_ops
= &cgroup_kf_ops
;
3194 kf_ops
= &cgroup_kf_single_ops
;
3197 * Ugh... if @cft wants a custom max_write_len, we need to
3198 * make a copy of kf_ops to set its atomic_write_len.
3200 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
) {
3201 kf_ops
= kmemdup(kf_ops
, sizeof(*kf_ops
), GFP_KERNEL
);
3203 cgroup_exit_cftypes(cfts
);
3206 kf_ops
->atomic_write_len
= cft
->max_write_len
;
3209 cft
->kf_ops
= kf_ops
;
3216 static int cgroup_rm_cftypes_locked(struct cftype
*cfts
)
3218 lockdep_assert_held(&cgroup_mutex
);
3220 if (!cfts
|| !cfts
[0].ss
)
3223 list_del(&cfts
->node
);
3224 cgroup_apply_cftypes(cfts
, false);
3225 cgroup_exit_cftypes(cfts
);
3230 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3231 * @cfts: zero-length name terminated array of cftypes
3233 * Unregister @cfts. Files described by @cfts are removed from all
3234 * existing cgroups and all future cgroups won't have them either. This
3235 * function can be called anytime whether @cfts' subsys is attached or not.
3237 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3240 int cgroup_rm_cftypes(struct cftype
*cfts
)
3244 mutex_lock(&cgroup_mutex
);
3245 ret
= cgroup_rm_cftypes_locked(cfts
);
3246 mutex_unlock(&cgroup_mutex
);
3251 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3252 * @ss: target cgroup subsystem
3253 * @cfts: zero-length name terminated array of cftypes
3255 * Register @cfts to @ss. Files described by @cfts are created for all
3256 * existing cgroups to which @ss is attached and all future cgroups will
3257 * have them too. This function can be called anytime whether @ss is
3260 * Returns 0 on successful registration, -errno on failure. Note that this
3261 * function currently returns 0 as long as @cfts registration is successful
3262 * even if some file creation attempts on existing cgroups fail.
3264 static int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3268 if (!cgroup_ssid_enabled(ss
->id
))
3271 if (!cfts
|| cfts
[0].name
[0] == '\0')
3274 ret
= cgroup_init_cftypes(ss
, cfts
);
3278 mutex_lock(&cgroup_mutex
);
3280 list_add_tail(&cfts
->node
, &ss
->cfts
);
3281 ret
= cgroup_apply_cftypes(cfts
, true);
3283 cgroup_rm_cftypes_locked(cfts
);
3285 mutex_unlock(&cgroup_mutex
);
3290 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3291 * @ss: target cgroup subsystem
3292 * @cfts: zero-length name terminated array of cftypes
3294 * Similar to cgroup_add_cftypes() but the added files are only used for
3295 * the default hierarchy.
3297 int cgroup_add_dfl_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3301 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3302 cft
->flags
|= __CFTYPE_ONLY_ON_DFL
;
3303 return cgroup_add_cftypes(ss
, cfts
);
3307 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3308 * @ss: target cgroup subsystem
3309 * @cfts: zero-length name terminated array of cftypes
3311 * Similar to cgroup_add_cftypes() but the added files are only used for
3312 * the legacy hierarchies.
3314 int cgroup_add_legacy_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3318 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3319 cft
->flags
|= __CFTYPE_NOT_ON_DFL
;
3320 return cgroup_add_cftypes(ss
, cfts
);
3324 * cgroup_file_notify - generate a file modified event for a cgroup_file
3325 * @cfile: target cgroup_file
3327 * @cfile must have been obtained by setting cftype->file_offset.
3329 void cgroup_file_notify(struct cgroup_file
*cfile
)
3331 unsigned long flags
;
3333 spin_lock_irqsave(&cgroup_file_kn_lock
, flags
);
3335 kernfs_notify(cfile
->kn
);
3336 spin_unlock_irqrestore(&cgroup_file_kn_lock
, flags
);
3340 * css_next_child - find the next child of a given css
3341 * @pos: the current position (%NULL to initiate traversal)
3342 * @parent: css whose children to walk
3344 * This function returns the next child of @parent and should be called
3345 * under either cgroup_mutex or RCU read lock. The only requirement is
3346 * that @parent and @pos are accessible. The next sibling is guaranteed to
3347 * be returned regardless of their states.
3349 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3350 * css which finished ->css_online() is guaranteed to be visible in the
3351 * future iterations and will stay visible until the last reference is put.
3352 * A css which hasn't finished ->css_online() or already finished
3353 * ->css_offline() may show up during traversal. It's each subsystem's
3354 * responsibility to synchronize against on/offlining.
3356 struct cgroup_subsys_state
*css_next_child(struct cgroup_subsys_state
*pos
,
3357 struct cgroup_subsys_state
*parent
)
3359 struct cgroup_subsys_state
*next
;
3361 cgroup_assert_mutex_or_rcu_locked();
3364 * @pos could already have been unlinked from the sibling list.
3365 * Once a cgroup is removed, its ->sibling.next is no longer
3366 * updated when its next sibling changes. CSS_RELEASED is set when
3367 * @pos is taken off list, at which time its next pointer is valid,
3368 * and, as releases are serialized, the one pointed to by the next
3369 * pointer is guaranteed to not have started release yet. This
3370 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3371 * critical section, the one pointed to by its next pointer is
3372 * guaranteed to not have finished its RCU grace period even if we
3373 * have dropped rcu_read_lock() inbetween iterations.
3375 * If @pos has CSS_RELEASED set, its next pointer can't be
3376 * dereferenced; however, as each css is given a monotonically
3377 * increasing unique serial number and always appended to the
3378 * sibling list, the next one can be found by walking the parent's
3379 * children until the first css with higher serial number than
3380 * @pos's. While this path can be slower, it happens iff iteration
3381 * races against release and the race window is very small.
3384 next
= list_entry_rcu(parent
->children
.next
, struct cgroup_subsys_state
, sibling
);
3385 } else if (likely(!(pos
->flags
& CSS_RELEASED
))) {
3386 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup_subsys_state
, sibling
);
3388 list_for_each_entry_rcu(next
, &parent
->children
, sibling
)
3389 if (next
->serial_nr
> pos
->serial_nr
)
3394 * @next, if not pointing to the head, can be dereferenced and is
3397 if (&next
->sibling
!= &parent
->children
)
3403 * css_next_descendant_pre - find the next descendant for pre-order walk
3404 * @pos: the current position (%NULL to initiate traversal)
3405 * @root: css whose descendants to walk
3407 * To be used by css_for_each_descendant_pre(). Find the next descendant
3408 * to visit for pre-order traversal of @root's descendants. @root is
3409 * included in the iteration and the first node to be visited.
3411 * While this function requires cgroup_mutex or RCU read locking, it
3412 * doesn't require the whole traversal to be contained in a single critical
3413 * section. This function will return the correct next descendant as long
3414 * as both @pos and @root are accessible and @pos is a descendant of @root.
3416 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3417 * css which finished ->css_online() is guaranteed to be visible in the
3418 * future iterations and will stay visible until the last reference is put.
3419 * A css which hasn't finished ->css_online() or already finished
3420 * ->css_offline() may show up during traversal. It's each subsystem's
3421 * responsibility to synchronize against on/offlining.
3423 struct cgroup_subsys_state
*
3424 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
3425 struct cgroup_subsys_state
*root
)
3427 struct cgroup_subsys_state
*next
;
3429 cgroup_assert_mutex_or_rcu_locked();
3431 /* if first iteration, visit @root */
3435 /* visit the first child if exists */
3436 next
= css_next_child(NULL
, pos
);
3440 /* no child, visit my or the closest ancestor's next sibling */
3441 while (pos
!= root
) {
3442 next
= css_next_child(pos
, pos
->parent
);
3452 * css_rightmost_descendant - return the rightmost descendant of a css
3453 * @pos: css of interest
3455 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3456 * is returned. This can be used during pre-order traversal to skip
3459 * While this function requires cgroup_mutex or RCU read locking, it
3460 * doesn't require the whole traversal to be contained in a single critical
3461 * section. This function will return the correct rightmost descendant as
3462 * long as @pos is accessible.
3464 struct cgroup_subsys_state
*
3465 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
3467 struct cgroup_subsys_state
*last
, *tmp
;
3469 cgroup_assert_mutex_or_rcu_locked();
3473 /* ->prev isn't RCU safe, walk ->next till the end */
3475 css_for_each_child(tmp
, last
)
3482 static struct cgroup_subsys_state
*
3483 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
3485 struct cgroup_subsys_state
*last
;
3489 pos
= css_next_child(NULL
, pos
);
3496 * css_next_descendant_post - find the next descendant for post-order walk
3497 * @pos: the current position (%NULL to initiate traversal)
3498 * @root: css whose descendants to walk
3500 * To be used by css_for_each_descendant_post(). Find the next descendant
3501 * to visit for post-order traversal of @root's descendants. @root is
3502 * included in the iteration and the last node to be visited.
3504 * While this function requires cgroup_mutex or RCU read locking, it
3505 * doesn't require the whole traversal to be contained in a single critical
3506 * section. This function will return the correct next descendant as long
3507 * as both @pos and @cgroup are accessible and @pos is a descendant of
3510 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3511 * css which finished ->css_online() is guaranteed to be visible in the
3512 * future iterations and will stay visible until the last reference is put.
3513 * A css which hasn't finished ->css_online() or already finished
3514 * ->css_offline() may show up during traversal. It's each subsystem's
3515 * responsibility to synchronize against on/offlining.
3517 struct cgroup_subsys_state
*
3518 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
3519 struct cgroup_subsys_state
*root
)
3521 struct cgroup_subsys_state
*next
;
3523 cgroup_assert_mutex_or_rcu_locked();
3525 /* if first iteration, visit leftmost descendant which may be @root */
3527 return css_leftmost_descendant(root
);
3529 /* if we visited @root, we're done */
3533 /* if there's an unvisited sibling, visit its leftmost descendant */
3534 next
= css_next_child(pos
, pos
->parent
);
3536 return css_leftmost_descendant(next
);
3538 /* no sibling left, visit parent */
3543 * css_has_online_children - does a css have online children
3544 * @css: the target css
3546 * Returns %true if @css has any online children; otherwise, %false. This
3547 * function can be called from any context but the caller is responsible
3548 * for synchronizing against on/offlining as necessary.
3550 bool css_has_online_children(struct cgroup_subsys_state
*css
)
3552 struct cgroup_subsys_state
*child
;
3556 css_for_each_child(child
, css
) {
3557 if (child
->flags
& CSS_ONLINE
) {
3567 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
3568 * @it: the iterator to advance
3570 * Advance @it to the next css_set to walk.
3572 static void css_task_iter_advance_css_set(struct css_task_iter
*it
)
3574 struct list_head
*l
= it
->cset_pos
;
3575 struct cgrp_cset_link
*link
;
3576 struct css_set
*cset
;
3578 lockdep_assert_held(&css_set_lock
);
3580 /* Advance to the next non-empty css_set */
3583 if (l
== it
->cset_head
) {
3584 it
->cset_pos
= NULL
;
3585 it
->task_pos
= NULL
;
3590 cset
= container_of(l
, struct css_set
,
3591 e_cset_node
[it
->ss
->id
]);
3593 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
3596 } while (!css_set_populated(cset
));
3600 if (!list_empty(&cset
->tasks
))
3601 it
->task_pos
= cset
->tasks
.next
;
3603 it
->task_pos
= cset
->mg_tasks
.next
;
3605 it
->tasks_head
= &cset
->tasks
;
3606 it
->mg_tasks_head
= &cset
->mg_tasks
;
3609 * We don't keep css_sets locked across iteration steps and thus
3610 * need to take steps to ensure that iteration can be resumed after
3611 * the lock is re-acquired. Iteration is performed at two levels -
3612 * css_sets and tasks in them.
3614 * Once created, a css_set never leaves its cgroup lists, so a
3615 * pinned css_set is guaranteed to stay put and we can resume
3616 * iteration afterwards.
3618 * Tasks may leave @cset across iteration steps. This is resolved
3619 * by registering each iterator with the css_set currently being
3620 * walked and making css_set_move_task() advance iterators whose
3621 * next task is leaving.
3624 list_del(&it
->iters_node
);
3625 put_css_set_locked(it
->cur_cset
);
3628 it
->cur_cset
= cset
;
3629 list_add(&it
->iters_node
, &cset
->task_iters
);
3632 static void css_task_iter_advance(struct css_task_iter
*it
)
3634 struct list_head
*l
= it
->task_pos
;
3636 lockdep_assert_held(&css_set_lock
);
3640 * Advance iterator to find next entry. cset->tasks is consumed
3641 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3646 if (l
== it
->tasks_head
)
3647 l
= it
->mg_tasks_head
->next
;
3649 if (l
== it
->mg_tasks_head
)
3650 css_task_iter_advance_css_set(it
);
3656 * css_task_iter_start - initiate task iteration
3657 * @css: the css to walk tasks of
3658 * @it: the task iterator to use
3660 * Initiate iteration through the tasks of @css. The caller can call
3661 * css_task_iter_next() to walk through the tasks until the function
3662 * returns NULL. On completion of iteration, css_task_iter_end() must be
3665 void css_task_iter_start(struct cgroup_subsys_state
*css
,
3666 struct css_task_iter
*it
)
3668 /* no one should try to iterate before mounting cgroups */
3669 WARN_ON_ONCE(!use_task_css_set_links
);
3671 memset(it
, 0, sizeof(*it
));
3673 spin_lock_irq(&css_set_lock
);
3678 it
->cset_pos
= &css
->cgroup
->e_csets
[css
->ss
->id
];
3680 it
->cset_pos
= &css
->cgroup
->cset_links
;
3682 it
->cset_head
= it
->cset_pos
;
3684 css_task_iter_advance_css_set(it
);
3686 spin_unlock_irq(&css_set_lock
);
3690 * css_task_iter_next - return the next task for the iterator
3691 * @it: the task iterator being iterated
3693 * The "next" function for task iteration. @it should have been
3694 * initialized via css_task_iter_start(). Returns NULL when the iteration
3697 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
3700 put_task_struct(it
->cur_task
);
3701 it
->cur_task
= NULL
;
3704 spin_lock_irq(&css_set_lock
);
3707 it
->cur_task
= list_entry(it
->task_pos
, struct task_struct
,
3709 get_task_struct(it
->cur_task
);
3710 css_task_iter_advance(it
);
3713 spin_unlock_irq(&css_set_lock
);
3715 return it
->cur_task
;
3719 * css_task_iter_end - finish task iteration
3720 * @it: the task iterator to finish
3722 * Finish task iteration started by css_task_iter_start().
3724 void css_task_iter_end(struct css_task_iter
*it
)
3727 spin_lock_irq(&css_set_lock
);
3728 list_del(&it
->iters_node
);
3729 put_css_set_locked(it
->cur_cset
);
3730 spin_unlock_irq(&css_set_lock
);
3734 put_task_struct(it
->cur_task
);
3737 static void cgroup_procs_release(struct kernfs_open_file
*of
)
3740 css_task_iter_end(of
->priv
);
3745 static void *cgroup_procs_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3747 struct kernfs_open_file
*of
= s
->private;
3748 struct css_task_iter
*it
= of
->priv
;
3749 struct task_struct
*task
;
3752 task
= css_task_iter_next(it
);
3753 } while (task
&& !thread_group_leader(task
));
3758 static void *cgroup_procs_start(struct seq_file
*s
, loff_t
*pos
)
3760 struct kernfs_open_file
*of
= s
->private;
3761 struct cgroup
*cgrp
= seq_css(s
)->cgroup
;
3762 struct css_task_iter
*it
= of
->priv
;
3765 * When a seq_file is seeked, it's always traversed sequentially
3766 * from position 0, so we can simply keep iterating on !0 *pos.
3769 if (WARN_ON_ONCE((*pos
)++))
3770 return ERR_PTR(-EINVAL
);
3772 it
= kzalloc(sizeof(*it
), GFP_KERNEL
);
3774 return ERR_PTR(-ENOMEM
);
3776 css_task_iter_start(&cgrp
->self
, it
);
3777 } else if (!(*pos
)++) {
3778 css_task_iter_end(it
);
3779 css_task_iter_start(&cgrp
->self
, it
);
3782 return cgroup_procs_next(s
, NULL
, NULL
);
3785 static int cgroup_procs_show(struct seq_file
*s
, void *v
)
3787 seq_printf(s
, "%d\n", task_tgid_vnr(v
));
3791 /* cgroup core interface files for the default hierarchy */
3792 static struct cftype cgroup_base_files
[] = {
3794 .name
= "cgroup.procs",
3795 .file_offset
= offsetof(struct cgroup
, procs_file
),
3796 .release
= cgroup_procs_release
,
3797 .seq_start
= cgroup_procs_start
,
3798 .seq_next
= cgroup_procs_next
,
3799 .seq_show
= cgroup_procs_show
,
3800 .write
= cgroup_procs_write
,
3803 .name
= "cgroup.controllers",
3804 .seq_show
= cgroup_controllers_show
,
3807 .name
= "cgroup.subtree_control",
3808 .seq_show
= cgroup_subtree_control_show
,
3809 .write
= cgroup_subtree_control_write
,
3812 .name
= "cgroup.events",
3813 .flags
= CFTYPE_NOT_ON_ROOT
,
3814 .file_offset
= offsetof(struct cgroup
, events_file
),
3815 .seq_show
= cgroup_events_show
,
3821 * css destruction is four-stage process.
3823 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3824 * Implemented in kill_css().
3826 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3827 * and thus css_tryget_online() is guaranteed to fail, the css can be
3828 * offlined by invoking offline_css(). After offlining, the base ref is
3829 * put. Implemented in css_killed_work_fn().
3831 * 3. When the percpu_ref reaches zero, the only possible remaining
3832 * accessors are inside RCU read sections. css_release() schedules the
3835 * 4. After the grace period, the css can be freed. Implemented in
3836 * css_free_work_fn().
3838 * It is actually hairier because both step 2 and 4 require process context
3839 * and thus involve punting to css->destroy_work adding two additional
3840 * steps to the already complex sequence.
3842 static void css_free_work_fn(struct work_struct
*work
)
3844 struct cgroup_subsys_state
*css
=
3845 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
3846 struct cgroup_subsys
*ss
= css
->ss
;
3847 struct cgroup
*cgrp
= css
->cgroup
;
3849 percpu_ref_exit(&css
->refcnt
);
3853 struct cgroup_subsys_state
*parent
= css
->parent
;
3857 cgroup_idr_remove(&ss
->css_idr
, id
);
3863 /* cgroup free path */
3864 atomic_dec(&cgrp
->root
->nr_cgrps
);
3865 cgroup1_pidlist_destroy_all(cgrp
);
3866 cancel_work_sync(&cgrp
->release_agent_work
);
3868 if (cgroup_parent(cgrp
)) {
3870 * We get a ref to the parent, and put the ref when
3871 * this cgroup is being freed, so it's guaranteed
3872 * that the parent won't be destroyed before its
3875 cgroup_put(cgroup_parent(cgrp
));
3876 kernfs_put(cgrp
->kn
);
3880 * This is root cgroup's refcnt reaching zero,
3881 * which indicates that the root should be
3884 cgroup_destroy_root(cgrp
->root
);
3889 static void css_free_rcu_fn(struct rcu_head
*rcu_head
)
3891 struct cgroup_subsys_state
*css
=
3892 container_of(rcu_head
, struct cgroup_subsys_state
, rcu_head
);
3894 INIT_WORK(&css
->destroy_work
, css_free_work_fn
);
3895 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
3898 static void css_release_work_fn(struct work_struct
*work
)
3900 struct cgroup_subsys_state
*css
=
3901 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
3902 struct cgroup_subsys
*ss
= css
->ss
;
3903 struct cgroup
*cgrp
= css
->cgroup
;
3905 mutex_lock(&cgroup_mutex
);
3907 css
->flags
|= CSS_RELEASED
;
3908 list_del_rcu(&css
->sibling
);
3911 /* css release path */
3912 cgroup_idr_replace(&ss
->css_idr
, NULL
, css
->id
);
3913 if (ss
->css_released
)
3914 ss
->css_released(css
);
3916 /* cgroup release path */
3917 trace_cgroup_release(cgrp
);
3919 cgroup_idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
3923 * There are two control paths which try to determine
3924 * cgroup from dentry without going through kernfs -
3925 * cgroupstats_build() and css_tryget_online_from_dir().
3926 * Those are supported by RCU protecting clearing of
3927 * cgrp->kn->priv backpointer.
3930 RCU_INIT_POINTER(*(void __rcu __force
**)&cgrp
->kn
->priv
,
3933 cgroup_bpf_put(cgrp
);
3936 mutex_unlock(&cgroup_mutex
);
3938 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
3941 static void css_release(struct percpu_ref
*ref
)
3943 struct cgroup_subsys_state
*css
=
3944 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
3946 INIT_WORK(&css
->destroy_work
, css_release_work_fn
);
3947 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
3950 static void init_and_link_css(struct cgroup_subsys_state
*css
,
3951 struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
3953 lockdep_assert_held(&cgroup_mutex
);
3955 cgroup_get_live(cgrp
);
3957 memset(css
, 0, sizeof(*css
));
3961 INIT_LIST_HEAD(&css
->sibling
);
3962 INIT_LIST_HEAD(&css
->children
);
3963 css
->serial_nr
= css_serial_nr_next
++;
3964 atomic_set(&css
->online_cnt
, 0);
3966 if (cgroup_parent(cgrp
)) {
3967 css
->parent
= cgroup_css(cgroup_parent(cgrp
), ss
);
3968 css_get(css
->parent
);
3971 BUG_ON(cgroup_css(cgrp
, ss
));
3974 /* invoke ->css_online() on a new CSS and mark it online if successful */
3975 static int online_css(struct cgroup_subsys_state
*css
)
3977 struct cgroup_subsys
*ss
= css
->ss
;
3980 lockdep_assert_held(&cgroup_mutex
);
3983 ret
= ss
->css_online(css
);
3985 css
->flags
|= CSS_ONLINE
;
3986 rcu_assign_pointer(css
->cgroup
->subsys
[ss
->id
], css
);
3988 atomic_inc(&css
->online_cnt
);
3990 atomic_inc(&css
->parent
->online_cnt
);
3995 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
3996 static void offline_css(struct cgroup_subsys_state
*css
)
3998 struct cgroup_subsys
*ss
= css
->ss
;
4000 lockdep_assert_held(&cgroup_mutex
);
4002 if (!(css
->flags
& CSS_ONLINE
))
4008 if (ss
->css_offline
)
4009 ss
->css_offline(css
);
4011 css
->flags
&= ~CSS_ONLINE
;
4012 RCU_INIT_POINTER(css
->cgroup
->subsys
[ss
->id
], NULL
);
4014 wake_up_all(&css
->cgroup
->offline_waitq
);
4018 * css_create - create a cgroup_subsys_state
4019 * @cgrp: the cgroup new css will be associated with
4020 * @ss: the subsys of new css
4022 * Create a new css associated with @cgrp - @ss pair. On success, the new
4023 * css is online and installed in @cgrp. This function doesn't create the
4024 * interface files. Returns 0 on success, -errno on failure.
4026 static struct cgroup_subsys_state
*css_create(struct cgroup
*cgrp
,
4027 struct cgroup_subsys
*ss
)
4029 struct cgroup
*parent
= cgroup_parent(cgrp
);
4030 struct cgroup_subsys_state
*parent_css
= cgroup_css(parent
, ss
);
4031 struct cgroup_subsys_state
*css
;
4034 lockdep_assert_held(&cgroup_mutex
);
4036 css
= ss
->css_alloc(parent_css
);
4038 css
= ERR_PTR(-ENOMEM
);
4042 init_and_link_css(css
, ss
, cgrp
);
4044 err
= percpu_ref_init(&css
->refcnt
, css_release
, 0, GFP_KERNEL
);
4048 err
= cgroup_idr_alloc(&ss
->css_idr
, NULL
, 2, 0, GFP_KERNEL
);
4053 /* @css is ready to be brought online now, make it visible */
4054 list_add_tail_rcu(&css
->sibling
, &parent_css
->children
);
4055 cgroup_idr_replace(&ss
->css_idr
, css
, css
->id
);
4057 err
= online_css(css
);
4061 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4062 cgroup_parent(parent
)) {
4063 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4064 current
->comm
, current
->pid
, ss
->name
);
4065 if (!strcmp(ss
->name
, "memory"))
4066 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
4067 ss
->warned_broken_hierarchy
= true;
4073 list_del_rcu(&css
->sibling
);
4075 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4076 return ERR_PTR(err
);
4080 * The returned cgroup is fully initialized including its control mask, but
4081 * it isn't associated with its kernfs_node and doesn't have the control
4084 static struct cgroup
*cgroup_create(struct cgroup
*parent
)
4086 struct cgroup_root
*root
= parent
->root
;
4087 struct cgroup
*cgrp
, *tcgrp
;
4088 int level
= parent
->level
+ 1;
4091 /* allocate the cgroup and its ID, 0 is reserved for the root */
4092 cgrp
= kzalloc(sizeof(*cgrp
) +
4093 sizeof(cgrp
->ancestor_ids
[0]) * (level
+ 1), GFP_KERNEL
);
4095 return ERR_PTR(-ENOMEM
);
4097 ret
= percpu_ref_init(&cgrp
->self
.refcnt
, css_release
, 0, GFP_KERNEL
);
4102 * Temporarily set the pointer to NULL, so idr_find() won't return
4103 * a half-baked cgroup.
4105 cgrp
->id
= cgroup_idr_alloc(&root
->cgroup_idr
, NULL
, 2, 0, GFP_KERNEL
);
4108 goto out_cancel_ref
;
4111 init_cgroup_housekeeping(cgrp
);
4113 cgrp
->self
.parent
= &parent
->self
;
4115 cgrp
->level
= level
;
4117 for (tcgrp
= cgrp
; tcgrp
; tcgrp
= cgroup_parent(tcgrp
))
4118 cgrp
->ancestor_ids
[tcgrp
->level
] = tcgrp
->id
;
4120 if (notify_on_release(parent
))
4121 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4123 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4124 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4126 cgrp
->self
.serial_nr
= css_serial_nr_next
++;
4128 /* allocation complete, commit to creation */
4129 list_add_tail_rcu(&cgrp
->self
.sibling
, &cgroup_parent(cgrp
)->self
.children
);
4130 atomic_inc(&root
->nr_cgrps
);
4131 cgroup_get_live(parent
);
4134 * @cgrp is now fully operational. If something fails after this
4135 * point, it'll be released via the normal destruction path.
4137 cgroup_idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
4140 * On the default hierarchy, a child doesn't automatically inherit
4141 * subtree_control from the parent. Each is configured manually.
4143 if (!cgroup_on_dfl(cgrp
))
4144 cgrp
->subtree_control
= cgroup_control(cgrp
);
4147 cgroup_bpf_inherit(cgrp
, parent
);
4149 cgroup_propagate_control(cgrp
);
4154 percpu_ref_exit(&cgrp
->self
.refcnt
);
4157 return ERR_PTR(ret
);
4160 int cgroup_mkdir(struct kernfs_node
*parent_kn
, const char *name
, umode_t mode
)
4162 struct cgroup
*parent
, *cgrp
;
4163 struct kernfs_node
*kn
;
4166 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
4167 if (strchr(name
, '\n'))
4170 parent
= cgroup_kn_lock_live(parent_kn
, false);
4174 cgrp
= cgroup_create(parent
);
4176 ret
= PTR_ERR(cgrp
);
4180 /* create the directory */
4181 kn
= kernfs_create_dir(parent
->kn
, name
, mode
, cgrp
);
4189 * This extra ref will be put in cgroup_free_fn() and guarantees
4190 * that @cgrp->kn is always accessible.
4194 ret
= cgroup_kn_set_ugid(kn
);
4198 ret
= css_populate_dir(&cgrp
->self
);
4202 ret
= cgroup_apply_control_enable(cgrp
);
4206 trace_cgroup_mkdir(cgrp
);
4208 /* let's create and online css's */
4209 kernfs_activate(kn
);
4215 cgroup_destroy_locked(cgrp
);
4217 cgroup_kn_unlock(parent_kn
);
4222 * This is called when the refcnt of a css is confirmed to be killed.
4223 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4224 * initate destruction and put the css ref from kill_css().
4226 static void css_killed_work_fn(struct work_struct
*work
)
4228 struct cgroup_subsys_state
*css
=
4229 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4231 mutex_lock(&cgroup_mutex
);
4236 /* @css can't go away while we're holding cgroup_mutex */
4238 } while (css
&& atomic_dec_and_test(&css
->online_cnt
));
4240 mutex_unlock(&cgroup_mutex
);
4243 /* css kill confirmation processing requires process context, bounce */
4244 static void css_killed_ref_fn(struct percpu_ref
*ref
)
4246 struct cgroup_subsys_state
*css
=
4247 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4249 if (atomic_dec_and_test(&css
->online_cnt
)) {
4250 INIT_WORK(&css
->destroy_work
, css_killed_work_fn
);
4251 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4256 * kill_css - destroy a css
4257 * @css: css to destroy
4259 * This function initiates destruction of @css by removing cgroup interface
4260 * files and putting its base reference. ->css_offline() will be invoked
4261 * asynchronously once css_tryget_online() is guaranteed to fail and when
4262 * the reference count reaches zero, @css will be released.
4264 static void kill_css(struct cgroup_subsys_state
*css
)
4266 lockdep_assert_held(&cgroup_mutex
);
4268 if (css
->flags
& CSS_DYING
)
4271 css
->flags
|= CSS_DYING
;
4274 * This must happen before css is disassociated with its cgroup.
4275 * See seq_css() for details.
4280 * Killing would put the base ref, but we need to keep it alive
4281 * until after ->css_offline().
4286 * cgroup core guarantees that, by the time ->css_offline() is
4287 * invoked, no new css reference will be given out via
4288 * css_tryget_online(). We can't simply call percpu_ref_kill() and
4289 * proceed to offlining css's because percpu_ref_kill() doesn't
4290 * guarantee that the ref is seen as killed on all CPUs on return.
4292 * Use percpu_ref_kill_and_confirm() to get notifications as each
4293 * css is confirmed to be seen as killed on all CPUs.
4295 percpu_ref_kill_and_confirm(&css
->refcnt
, css_killed_ref_fn
);
4299 * cgroup_destroy_locked - the first stage of cgroup destruction
4300 * @cgrp: cgroup to be destroyed
4302 * css's make use of percpu refcnts whose killing latency shouldn't be
4303 * exposed to userland and are RCU protected. Also, cgroup core needs to
4304 * guarantee that css_tryget_online() won't succeed by the time
4305 * ->css_offline() is invoked. To satisfy all the requirements,
4306 * destruction is implemented in the following two steps.
4308 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4309 * userland visible parts and start killing the percpu refcnts of
4310 * css's. Set up so that the next stage will be kicked off once all
4311 * the percpu refcnts are confirmed to be killed.
4313 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4314 * rest of destruction. Once all cgroup references are gone, the
4315 * cgroup is RCU-freed.
4317 * This function implements s1. After this step, @cgrp is gone as far as
4318 * the userland is concerned and a new cgroup with the same name may be
4319 * created. As cgroup doesn't care about the names internally, this
4320 * doesn't cause any problem.
4322 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4323 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4325 struct cgroup_subsys_state
*css
;
4326 struct cgrp_cset_link
*link
;
4329 lockdep_assert_held(&cgroup_mutex
);
4332 * Only migration can raise populated from zero and we're already
4333 * holding cgroup_mutex.
4335 if (cgroup_is_populated(cgrp
))
4339 * Make sure there's no live children. We can't test emptiness of
4340 * ->self.children as dead children linger on it while being
4341 * drained; otherwise, "rmdir parent/child parent" may fail.
4343 if (css_has_online_children(&cgrp
->self
))
4347 * Mark @cgrp and the associated csets dead. The former prevents
4348 * further task migration and child creation by disabling
4349 * cgroup_lock_live_group(). The latter makes the csets ignored by
4350 * the migration path.
4352 cgrp
->self
.flags
&= ~CSS_ONLINE
;
4354 spin_lock_irq(&css_set_lock
);
4355 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
4356 link
->cset
->dead
= true;
4357 spin_unlock_irq(&css_set_lock
);
4359 /* initiate massacre of all css's */
4360 for_each_css(css
, ssid
, cgrp
)
4364 * Remove @cgrp directory along with the base files. @cgrp has an
4365 * extra ref on its kn.
4367 kernfs_remove(cgrp
->kn
);
4369 cgroup1_check_for_release(cgroup_parent(cgrp
));
4371 /* put the base reference */
4372 percpu_ref_kill(&cgrp
->self
.refcnt
);
4377 int cgroup_rmdir(struct kernfs_node
*kn
)
4379 struct cgroup
*cgrp
;
4382 cgrp
= cgroup_kn_lock_live(kn
, false);
4386 ret
= cgroup_destroy_locked(cgrp
);
4389 trace_cgroup_rmdir(cgrp
);
4391 cgroup_kn_unlock(kn
);
4395 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
= {
4396 .remount_fs
= cgroup_remount
,
4397 .mkdir
= cgroup_mkdir
,
4398 .rmdir
= cgroup_rmdir
,
4399 .show_path
= cgroup_show_path
,
4402 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
, bool early
)
4404 struct cgroup_subsys_state
*css
;
4406 pr_debug("Initializing cgroup subsys %s\n", ss
->name
);
4408 mutex_lock(&cgroup_mutex
);
4410 idr_init(&ss
->css_idr
);
4411 INIT_LIST_HEAD(&ss
->cfts
);
4413 /* Create the root cgroup state for this subsystem */
4414 ss
->root
= &cgrp_dfl_root
;
4415 css
= ss
->css_alloc(cgroup_css(&cgrp_dfl_root
.cgrp
, ss
));
4416 /* We don't handle early failures gracefully */
4417 BUG_ON(IS_ERR(css
));
4418 init_and_link_css(css
, ss
, &cgrp_dfl_root
.cgrp
);
4421 * Root csses are never destroyed and we can't initialize
4422 * percpu_ref during early init. Disable refcnting.
4424 css
->flags
|= CSS_NO_REF
;
4427 /* allocation can't be done safely during early init */
4430 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2, GFP_KERNEL
);
4431 BUG_ON(css
->id
< 0);
4434 /* Update the init_css_set to contain a subsys
4435 * pointer to this state - since the subsystem is
4436 * newly registered, all tasks and hence the
4437 * init_css_set is in the subsystem's root cgroup. */
4438 init_css_set
.subsys
[ss
->id
] = css
;
4440 have_fork_callback
|= (bool)ss
->fork
<< ss
->id
;
4441 have_exit_callback
|= (bool)ss
->exit
<< ss
->id
;
4442 have_free_callback
|= (bool)ss
->free
<< ss
->id
;
4443 have_canfork_callback
|= (bool)ss
->can_fork
<< ss
->id
;
4445 /* At system boot, before all subsystems have been
4446 * registered, no tasks have been forked, so we don't
4447 * need to invoke fork callbacks here. */
4448 BUG_ON(!list_empty(&init_task
.tasks
));
4450 BUG_ON(online_css(css
));
4452 mutex_unlock(&cgroup_mutex
);
4456 * cgroup_init_early - cgroup initialization at system boot
4458 * Initialize cgroups at system boot, and initialize any
4459 * subsystems that request early init.
4461 int __init
cgroup_init_early(void)
4463 static struct cgroup_sb_opts __initdata opts
;
4464 struct cgroup_subsys
*ss
;
4467 init_cgroup_root(&cgrp_dfl_root
, &opts
);
4468 cgrp_dfl_root
.cgrp
.self
.flags
|= CSS_NO_REF
;
4470 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
4472 for_each_subsys(ss
, i
) {
4473 WARN(!ss
->css_alloc
|| !ss
->css_free
|| ss
->name
|| ss
->id
,
4474 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
4475 i
, cgroup_subsys_name
[i
], ss
->css_alloc
, ss
->css_free
,
4477 WARN(strlen(cgroup_subsys_name
[i
]) > MAX_CGROUP_TYPE_NAMELEN
,
4478 "cgroup_subsys_name %s too long\n", cgroup_subsys_name
[i
]);
4481 ss
->name
= cgroup_subsys_name
[i
];
4482 if (!ss
->legacy_name
)
4483 ss
->legacy_name
= cgroup_subsys_name
[i
];
4486 cgroup_init_subsys(ss
, true);
4491 static u16 cgroup_disable_mask __initdata
;
4494 * cgroup_init - cgroup initialization
4496 * Register cgroup filesystem and /proc file, and initialize
4497 * any subsystems that didn't request early init.
4499 int __init
cgroup_init(void)
4501 struct cgroup_subsys
*ss
;
4504 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT
> 16);
4505 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem
));
4506 BUG_ON(cgroup_init_cftypes(NULL
, cgroup_base_files
));
4507 BUG_ON(cgroup_init_cftypes(NULL
, cgroup1_base_files
));
4510 * The latency of the synchronize_sched() is too high for cgroups,
4511 * avoid it at the cost of forcing all readers into the slow path.
4513 rcu_sync_enter_start(&cgroup_threadgroup_rwsem
.rss
);
4515 get_user_ns(init_cgroup_ns
.user_ns
);
4517 mutex_lock(&cgroup_mutex
);
4520 * Add init_css_set to the hash table so that dfl_root can link to
4523 hash_add(css_set_table
, &init_css_set
.hlist
,
4524 css_set_hash(init_css_set
.subsys
));
4526 BUG_ON(cgroup_setup_root(&cgrp_dfl_root
, 0, 0));
4528 mutex_unlock(&cgroup_mutex
);
4530 for_each_subsys(ss
, ssid
) {
4531 if (ss
->early_init
) {
4532 struct cgroup_subsys_state
*css
=
4533 init_css_set
.subsys
[ss
->id
];
4535 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2,
4537 BUG_ON(css
->id
< 0);
4539 cgroup_init_subsys(ss
, false);
4542 list_add_tail(&init_css_set
.e_cset_node
[ssid
],
4543 &cgrp_dfl_root
.cgrp
.e_csets
[ssid
]);
4546 * Setting dfl_root subsys_mask needs to consider the
4547 * disabled flag and cftype registration needs kmalloc,
4548 * both of which aren't available during early_init.
4550 if (cgroup_disable_mask
& (1 << ssid
)) {
4551 static_branch_disable(cgroup_subsys_enabled_key
[ssid
]);
4552 printk(KERN_INFO
"Disabling %s control group subsystem\n",
4557 if (cgroup1_ssid_disabled(ssid
))
4558 printk(KERN_INFO
"Disabling %s control group subsystem in v1 mounts\n",
4561 cgrp_dfl_root
.subsys_mask
|= 1 << ss
->id
;
4563 if (ss
->implicit_on_dfl
)
4564 cgrp_dfl_implicit_ss_mask
|= 1 << ss
->id
;
4565 else if (!ss
->dfl_cftypes
)
4566 cgrp_dfl_inhibit_ss_mask
|= 1 << ss
->id
;
4568 if (ss
->dfl_cftypes
== ss
->legacy_cftypes
) {
4569 WARN_ON(cgroup_add_cftypes(ss
, ss
->dfl_cftypes
));
4571 WARN_ON(cgroup_add_dfl_cftypes(ss
, ss
->dfl_cftypes
));
4572 WARN_ON(cgroup_add_legacy_cftypes(ss
, ss
->legacy_cftypes
));
4576 ss
->bind(init_css_set
.subsys
[ssid
]);
4579 /* init_css_set.subsys[] has been updated, re-hash */
4580 hash_del(&init_css_set
.hlist
);
4581 hash_add(css_set_table
, &init_css_set
.hlist
,
4582 css_set_hash(init_css_set
.subsys
));
4584 WARN_ON(sysfs_create_mount_point(fs_kobj
, "cgroup"));
4585 WARN_ON(register_filesystem(&cgroup_fs_type
));
4586 WARN_ON(register_filesystem(&cgroup2_fs_type
));
4587 WARN_ON(!proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
));
4592 static int __init
cgroup_wq_init(void)
4595 * There isn't much point in executing destruction path in
4596 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4597 * Use 1 for @max_active.
4599 * We would prefer to do this in cgroup_init() above, but that
4600 * is called before init_workqueues(): so leave this until after.
4602 cgroup_destroy_wq
= alloc_workqueue("cgroup_destroy", 0, 1);
4603 BUG_ON(!cgroup_destroy_wq
);
4606 core_initcall(cgroup_wq_init
);
4609 * proc_cgroup_show()
4610 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4611 * - Used for /proc/<pid>/cgroup.
4613 int proc_cgroup_show(struct seq_file
*m
, struct pid_namespace
*ns
,
4614 struct pid
*pid
, struct task_struct
*tsk
)
4618 struct cgroup_root
*root
;
4621 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
4625 mutex_lock(&cgroup_mutex
);
4626 spin_lock_irq(&css_set_lock
);
4628 for_each_root(root
) {
4629 struct cgroup_subsys
*ss
;
4630 struct cgroup
*cgrp
;
4631 int ssid
, count
= 0;
4633 if (root
== &cgrp_dfl_root
&& !cgrp_dfl_visible
)
4636 seq_printf(m
, "%d:", root
->hierarchy_id
);
4637 if (root
!= &cgrp_dfl_root
)
4638 for_each_subsys(ss
, ssid
)
4639 if (root
->subsys_mask
& (1 << ssid
))
4640 seq_printf(m
, "%s%s", count
++ ? "," : "",
4642 if (strlen(root
->name
))
4643 seq_printf(m
, "%sname=%s", count
? "," : "",
4647 cgrp
= task_cgroup_from_root(tsk
, root
);
4650 * On traditional hierarchies, all zombie tasks show up as
4651 * belonging to the root cgroup. On the default hierarchy,
4652 * while a zombie doesn't show up in "cgroup.procs" and
4653 * thus can't be migrated, its /proc/PID/cgroup keeps
4654 * reporting the cgroup it belonged to before exiting. If
4655 * the cgroup is removed before the zombie is reaped,
4656 * " (deleted)" is appended to the cgroup path.
4658 if (cgroup_on_dfl(cgrp
) || !(tsk
->flags
& PF_EXITING
)) {
4659 retval
= cgroup_path_ns_locked(cgrp
, buf
, PATH_MAX
,
4660 current
->nsproxy
->cgroup_ns
);
4661 if (retval
>= PATH_MAX
)
4662 retval
= -ENAMETOOLONG
;
4671 if (cgroup_on_dfl(cgrp
) && cgroup_is_dead(cgrp
))
4672 seq_puts(m
, " (deleted)\n");
4679 spin_unlock_irq(&css_set_lock
);
4680 mutex_unlock(&cgroup_mutex
);
4687 * cgroup_fork - initialize cgroup related fields during copy_process()
4688 * @child: pointer to task_struct of forking parent process.
4690 * A task is associated with the init_css_set until cgroup_post_fork()
4691 * attaches it to the parent's css_set. Empty cg_list indicates that
4692 * @child isn't holding reference to its css_set.
4694 void cgroup_fork(struct task_struct
*child
)
4696 RCU_INIT_POINTER(child
->cgroups
, &init_css_set
);
4697 INIT_LIST_HEAD(&child
->cg_list
);
4701 * cgroup_can_fork - called on a new task before the process is exposed
4702 * @child: the task in question.
4704 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
4705 * returns an error, the fork aborts with that error code. This allows for
4706 * a cgroup subsystem to conditionally allow or deny new forks.
4708 int cgroup_can_fork(struct task_struct
*child
)
4710 struct cgroup_subsys
*ss
;
4713 do_each_subsys_mask(ss
, i
, have_canfork_callback
) {
4714 ret
= ss
->can_fork(child
);
4717 } while_each_subsys_mask();
4722 for_each_subsys(ss
, j
) {
4725 if (ss
->cancel_fork
)
4726 ss
->cancel_fork(child
);
4733 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
4734 * @child: the task in question
4736 * This calls the cancel_fork() callbacks if a fork failed *after*
4737 * cgroup_can_fork() succeded.
4739 void cgroup_cancel_fork(struct task_struct
*child
)
4741 struct cgroup_subsys
*ss
;
4744 for_each_subsys(ss
, i
)
4745 if (ss
->cancel_fork
)
4746 ss
->cancel_fork(child
);
4750 * cgroup_post_fork - called on a new task after adding it to the task list
4751 * @child: the task in question
4753 * Adds the task to the list running through its css_set if necessary and
4754 * call the subsystem fork() callbacks. Has to be after the task is
4755 * visible on the task list in case we race with the first call to
4756 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
4759 void cgroup_post_fork(struct task_struct
*child
)
4761 struct cgroup_subsys
*ss
;
4765 * This may race against cgroup_enable_task_cg_lists(). As that
4766 * function sets use_task_css_set_links before grabbing
4767 * tasklist_lock and we just went through tasklist_lock to add
4768 * @child, it's guaranteed that either we see the set
4769 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4770 * @child during its iteration.
4772 * If we won the race, @child is associated with %current's
4773 * css_set. Grabbing css_set_lock guarantees both that the
4774 * association is stable, and, on completion of the parent's
4775 * migration, @child is visible in the source of migration or
4776 * already in the destination cgroup. This guarantee is necessary
4777 * when implementing operations which need to migrate all tasks of
4778 * a cgroup to another.
4780 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
4781 * will remain in init_css_set. This is safe because all tasks are
4782 * in the init_css_set before cg_links is enabled and there's no
4783 * operation which transfers all tasks out of init_css_set.
4785 if (use_task_css_set_links
) {
4786 struct css_set
*cset
;
4788 spin_lock_irq(&css_set_lock
);
4789 cset
= task_css_set(current
);
4790 if (list_empty(&child
->cg_list
)) {
4792 css_set_move_task(child
, NULL
, cset
, false);
4794 spin_unlock_irq(&css_set_lock
);
4798 * Call ss->fork(). This must happen after @child is linked on
4799 * css_set; otherwise, @child might change state between ->fork()
4800 * and addition to css_set.
4802 do_each_subsys_mask(ss
, i
, have_fork_callback
) {
4804 } while_each_subsys_mask();
4808 * cgroup_exit - detach cgroup from exiting task
4809 * @tsk: pointer to task_struct of exiting process
4811 * Description: Detach cgroup from @tsk and release it.
4813 * Note that cgroups marked notify_on_release force every task in
4814 * them to take the global cgroup_mutex mutex when exiting.
4815 * This could impact scaling on very large systems. Be reluctant to
4816 * use notify_on_release cgroups where very high task exit scaling
4817 * is required on large systems.
4819 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4820 * call cgroup_exit() while the task is still competent to handle
4821 * notify_on_release(), then leave the task attached to the root cgroup in
4822 * each hierarchy for the remainder of its exit. No need to bother with
4823 * init_css_set refcnting. init_css_set never goes away and we can't race
4824 * with migration path - PF_EXITING is visible to migration path.
4826 void cgroup_exit(struct task_struct
*tsk
)
4828 struct cgroup_subsys
*ss
;
4829 struct css_set
*cset
;
4833 * Unlink from @tsk from its css_set. As migration path can't race
4834 * with us, we can check css_set and cg_list without synchronization.
4836 cset
= task_css_set(tsk
);
4838 if (!list_empty(&tsk
->cg_list
)) {
4839 spin_lock_irq(&css_set_lock
);
4840 css_set_move_task(tsk
, cset
, NULL
, false);
4841 spin_unlock_irq(&css_set_lock
);
4846 /* see cgroup_post_fork() for details */
4847 do_each_subsys_mask(ss
, i
, have_exit_callback
) {
4849 } while_each_subsys_mask();
4852 void cgroup_free(struct task_struct
*task
)
4854 struct css_set
*cset
= task_css_set(task
);
4855 struct cgroup_subsys
*ss
;
4858 do_each_subsys_mask(ss
, ssid
, have_free_callback
) {
4860 } while_each_subsys_mask();
4865 static int __init
cgroup_disable(char *str
)
4867 struct cgroup_subsys
*ss
;
4871 while ((token
= strsep(&str
, ",")) != NULL
) {
4875 for_each_subsys(ss
, i
) {
4876 if (strcmp(token
, ss
->name
) &&
4877 strcmp(token
, ss
->legacy_name
))
4879 cgroup_disable_mask
|= 1 << i
;
4884 __setup("cgroup_disable=", cgroup_disable
);
4887 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
4888 * @dentry: directory dentry of interest
4889 * @ss: subsystem of interest
4891 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4892 * to get the corresponding css and return it. If such css doesn't exist
4893 * or can't be pinned, an ERR_PTR value is returned.
4895 struct cgroup_subsys_state
*css_tryget_online_from_dir(struct dentry
*dentry
,
4896 struct cgroup_subsys
*ss
)
4898 struct kernfs_node
*kn
= kernfs_node_from_dentry(dentry
);
4899 struct file_system_type
*s_type
= dentry
->d_sb
->s_type
;
4900 struct cgroup_subsys_state
*css
= NULL
;
4901 struct cgroup
*cgrp
;
4903 /* is @dentry a cgroup dir? */
4904 if ((s_type
!= &cgroup_fs_type
&& s_type
!= &cgroup2_fs_type
) ||
4905 !kn
|| kernfs_type(kn
) != KERNFS_DIR
)
4906 return ERR_PTR(-EBADF
);
4911 * This path doesn't originate from kernfs and @kn could already
4912 * have been or be removed at any point. @kn->priv is RCU
4913 * protected for this access. See css_release_work_fn() for details.
4915 cgrp
= rcu_dereference(*(void __rcu __force
**)&kn
->priv
);
4917 css
= cgroup_css(cgrp
, ss
);
4919 if (!css
|| !css_tryget_online(css
))
4920 css
= ERR_PTR(-ENOENT
);
4927 * css_from_id - lookup css by id
4928 * @id: the cgroup id
4929 * @ss: cgroup subsys to be looked into
4931 * Returns the css if there's valid one with @id, otherwise returns NULL.
4932 * Should be called under rcu_read_lock().
4934 struct cgroup_subsys_state
*css_from_id(int id
, struct cgroup_subsys
*ss
)
4936 WARN_ON_ONCE(!rcu_read_lock_held());
4937 return idr_find(&ss
->css_idr
, id
);
4941 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
4942 * @path: path on the default hierarchy
4944 * Find the cgroup at @path on the default hierarchy, increment its
4945 * reference count and return it. Returns pointer to the found cgroup on
4946 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
4947 * if @path points to a non-directory.
4949 struct cgroup
*cgroup_get_from_path(const char *path
)
4951 struct kernfs_node
*kn
;
4952 struct cgroup
*cgrp
;
4954 mutex_lock(&cgroup_mutex
);
4956 kn
= kernfs_walk_and_get(cgrp_dfl_root
.cgrp
.kn
, path
);
4958 if (kernfs_type(kn
) == KERNFS_DIR
) {
4960 cgroup_get_live(cgrp
);
4962 cgrp
= ERR_PTR(-ENOTDIR
);
4966 cgrp
= ERR_PTR(-ENOENT
);
4969 mutex_unlock(&cgroup_mutex
);
4972 EXPORT_SYMBOL_GPL(cgroup_get_from_path
);
4975 * cgroup_get_from_fd - get a cgroup pointer from a fd
4976 * @fd: fd obtained by open(cgroup2_dir)
4978 * Find the cgroup from a fd which should be obtained
4979 * by opening a cgroup directory. Returns a pointer to the
4980 * cgroup on success. ERR_PTR is returned if the cgroup
4983 struct cgroup
*cgroup_get_from_fd(int fd
)
4985 struct cgroup_subsys_state
*css
;
4986 struct cgroup
*cgrp
;
4991 return ERR_PTR(-EBADF
);
4993 css
= css_tryget_online_from_dir(f
->f_path
.dentry
, NULL
);
4996 return ERR_CAST(css
);
4999 if (!cgroup_on_dfl(cgrp
)) {
5001 return ERR_PTR(-EBADF
);
5006 EXPORT_SYMBOL_GPL(cgroup_get_from_fd
);
5009 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5010 * definition in cgroup-defs.h.
5012 #ifdef CONFIG_SOCK_CGROUP_DATA
5014 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5016 DEFINE_SPINLOCK(cgroup_sk_update_lock
);
5017 static bool cgroup_sk_alloc_disabled __read_mostly
;
5019 void cgroup_sk_alloc_disable(void)
5021 if (cgroup_sk_alloc_disabled
)
5023 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5024 cgroup_sk_alloc_disabled
= true;
5029 #define cgroup_sk_alloc_disabled false
5033 void cgroup_sk_alloc(struct sock_cgroup_data
*skcd
)
5035 if (cgroup_sk_alloc_disabled
)
5038 /* Socket clone path */
5041 * We might be cloning a socket which is left in an empty
5042 * cgroup and the cgroup might have already been rmdir'd.
5043 * Don't use cgroup_get_live().
5045 cgroup_get(sock_cgroup_ptr(skcd
));
5052 struct css_set
*cset
;
5054 cset
= task_css_set(current
);
5055 if (likely(cgroup_tryget(cset
->dfl_cgrp
))) {
5056 skcd
->val
= (unsigned long)cset
->dfl_cgrp
;
5065 void cgroup_sk_free(struct sock_cgroup_data
*skcd
)
5067 cgroup_put(sock_cgroup_ptr(skcd
));
5070 #endif /* CONFIG_SOCK_CGROUP_DATA */
5072 #ifdef CONFIG_CGROUP_BPF
5073 int cgroup_bpf_update(struct cgroup
*cgrp
, struct bpf_prog
*prog
,
5074 enum bpf_attach_type type
, bool overridable
)
5076 struct cgroup
*parent
= cgroup_parent(cgrp
);
5079 mutex_lock(&cgroup_mutex
);
5080 ret
= __cgroup_bpf_update(cgrp
, parent
, prog
, type
, overridable
);
5081 mutex_unlock(&cgroup_mutex
);
5084 #endif /* CONFIG_CGROUP_BPF */