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 * css_set_populated() should be the same as !!cset->nr_tasks at steady
578 * state. However, css_set_populated() can be called while a task is being
579 * added to or removed from the linked list before the nr_tasks is
580 * properly updated. Hence, we can't just look at ->nr_tasks here.
582 static bool css_set_populated(struct css_set
*cset
)
584 lockdep_assert_held(&css_set_lock
);
586 return !list_empty(&cset
->tasks
) || !list_empty(&cset
->mg_tasks
);
590 * cgroup_update_populated - updated populated count of a cgroup
591 * @cgrp: the target cgroup
592 * @populated: inc or dec populated count
594 * One of the css_sets associated with @cgrp is either getting its first
595 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
596 * count is propagated towards root so that a given cgroup's populated_cnt
597 * is zero iff the cgroup and all its descendants don't contain any tasks.
599 * @cgrp's interface file "cgroup.populated" is zero if
600 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
601 * changes from or to zero, userland is notified that the content of the
602 * interface file has changed. This can be used to detect when @cgrp and
603 * its descendants become populated or empty.
605 static void cgroup_update_populated(struct cgroup
*cgrp
, bool populated
)
607 lockdep_assert_held(&css_set_lock
);
613 trigger
= !cgrp
->populated_cnt
++;
615 trigger
= !--cgrp
->populated_cnt
;
620 cgroup1_check_for_release(cgrp
);
621 cgroup_file_notify(&cgrp
->events_file
);
623 cgrp
= cgroup_parent(cgrp
);
628 * css_set_update_populated - update populated state of a css_set
629 * @cset: target css_set
630 * @populated: whether @cset is populated or depopulated
632 * @cset is either getting the first task or losing the last. Update the
633 * ->populated_cnt of all associated cgroups accordingly.
635 static void css_set_update_populated(struct css_set
*cset
, bool populated
)
637 struct cgrp_cset_link
*link
;
639 lockdep_assert_held(&css_set_lock
);
641 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
)
642 cgroup_update_populated(link
->cgrp
, populated
);
646 * css_set_move_task - move a task from one css_set to another
647 * @task: task being moved
648 * @from_cset: css_set @task currently belongs to (may be NULL)
649 * @to_cset: new css_set @task is being moved to (may be NULL)
650 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
652 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
653 * css_set, @from_cset can be NULL. If @task is being disassociated
654 * instead of moved, @to_cset can be NULL.
656 * This function automatically handles populated_cnt updates and
657 * css_task_iter adjustments but the caller is responsible for managing
658 * @from_cset and @to_cset's reference counts.
660 static void css_set_move_task(struct task_struct
*task
,
661 struct css_set
*from_cset
, struct css_set
*to_cset
,
664 lockdep_assert_held(&css_set_lock
);
666 if (to_cset
&& !css_set_populated(to_cset
))
667 css_set_update_populated(to_cset
, true);
670 struct css_task_iter
*it
, *pos
;
672 WARN_ON_ONCE(list_empty(&task
->cg_list
));
675 * @task is leaving, advance task iterators which are
676 * pointing to it so that they can resume at the next
677 * position. Advancing an iterator might remove it from
678 * the list, use safe walk. See css_task_iter_advance*()
681 list_for_each_entry_safe(it
, pos
, &from_cset
->task_iters
,
683 if (it
->task_pos
== &task
->cg_list
)
684 css_task_iter_advance(it
);
686 list_del_init(&task
->cg_list
);
687 if (!css_set_populated(from_cset
))
688 css_set_update_populated(from_cset
, false);
690 WARN_ON_ONCE(!list_empty(&task
->cg_list
));
695 * We are synchronized through cgroup_threadgroup_rwsem
696 * against PF_EXITING setting such that we can't race
697 * against cgroup_exit() changing the css_set to
698 * init_css_set and dropping the old one.
700 WARN_ON_ONCE(task
->flags
& PF_EXITING
);
702 rcu_assign_pointer(task
->cgroups
, to_cset
);
703 list_add_tail(&task
->cg_list
, use_mg_tasks
? &to_cset
->mg_tasks
:
709 * hash table for cgroup groups. This improves the performance to find
710 * an existing css_set. This hash doesn't (currently) take into
711 * account cgroups in empty hierarchies.
713 #define CSS_SET_HASH_BITS 7
714 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
716 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
718 unsigned long key
= 0UL;
719 struct cgroup_subsys
*ss
;
722 for_each_subsys(ss
, i
)
723 key
+= (unsigned long)css
[i
];
724 key
= (key
>> 16) ^ key
;
729 void put_css_set_locked(struct css_set
*cset
)
731 struct cgrp_cset_link
*link
, *tmp_link
;
732 struct cgroup_subsys
*ss
;
735 lockdep_assert_held(&css_set_lock
);
737 if (!refcount_dec_and_test(&cset
->refcount
))
740 /* This css_set is dead. unlink it and release cgroup and css refs */
741 for_each_subsys(ss
, ssid
) {
742 list_del(&cset
->e_cset_node
[ssid
]);
743 css_put(cset
->subsys
[ssid
]);
745 hash_del(&cset
->hlist
);
748 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
749 list_del(&link
->cset_link
);
750 list_del(&link
->cgrp_link
);
751 if (cgroup_parent(link
->cgrp
))
752 cgroup_put(link
->cgrp
);
756 kfree_rcu(cset
, rcu_head
);
760 * compare_css_sets - helper function for find_existing_css_set().
761 * @cset: candidate css_set being tested
762 * @old_cset: existing css_set for a task
763 * @new_cgrp: cgroup that's being entered by the task
764 * @template: desired set of css pointers in css_set (pre-calculated)
766 * Returns true if "cset" matches "old_cset" except for the hierarchy
767 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
769 static bool compare_css_sets(struct css_set
*cset
,
770 struct css_set
*old_cset
,
771 struct cgroup
*new_cgrp
,
772 struct cgroup_subsys_state
*template[])
774 struct list_head
*l1
, *l2
;
777 * On the default hierarchy, there can be csets which are
778 * associated with the same set of cgroups but different csses.
779 * Let's first ensure that csses match.
781 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
)))
785 * Compare cgroup pointers in order to distinguish between
786 * different cgroups in hierarchies. As different cgroups may
787 * share the same effective css, this comparison is always
790 l1
= &cset
->cgrp_links
;
791 l2
= &old_cset
->cgrp_links
;
793 struct cgrp_cset_link
*link1
, *link2
;
794 struct cgroup
*cgrp1
, *cgrp2
;
798 /* See if we reached the end - both lists are equal length. */
799 if (l1
== &cset
->cgrp_links
) {
800 BUG_ON(l2
!= &old_cset
->cgrp_links
);
803 BUG_ON(l2
== &old_cset
->cgrp_links
);
805 /* Locate the cgroups associated with these links. */
806 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
807 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
810 /* Hierarchies should be linked in the same order. */
811 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
814 * If this hierarchy is the hierarchy of the cgroup
815 * that's changing, then we need to check that this
816 * css_set points to the new cgroup; if it's any other
817 * hierarchy, then this css_set should point to the
818 * same cgroup as the old css_set.
820 if (cgrp1
->root
== new_cgrp
->root
) {
821 if (cgrp1
!= new_cgrp
)
832 * find_existing_css_set - init css array and find the matching css_set
833 * @old_cset: the css_set that we're using before the cgroup transition
834 * @cgrp: the cgroup that we're moving into
835 * @template: out param for the new set of csses, should be clear on entry
837 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
839 struct cgroup_subsys_state
*template[])
841 struct cgroup_root
*root
= cgrp
->root
;
842 struct cgroup_subsys
*ss
;
843 struct css_set
*cset
;
848 * Build the set of subsystem state objects that we want to see in the
849 * new css_set. while subsystems can change globally, the entries here
850 * won't change, so no need for locking.
852 for_each_subsys(ss
, i
) {
853 if (root
->subsys_mask
& (1UL << i
)) {
855 * @ss is in this hierarchy, so we want the
856 * effective css from @cgrp.
858 template[i
] = cgroup_e_css(cgrp
, ss
);
861 * @ss is not in this hierarchy, so we don't want
864 template[i
] = old_cset
->subsys
[i
];
868 key
= css_set_hash(template);
869 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
870 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
873 /* This css_set matches what we need */
877 /* No existing cgroup group matched */
881 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
883 struct cgrp_cset_link
*link
, *tmp_link
;
885 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
886 list_del(&link
->cset_link
);
892 * allocate_cgrp_cset_links - allocate cgrp_cset_links
893 * @count: the number of links to allocate
894 * @tmp_links: list_head the allocated links are put on
896 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
897 * through ->cset_link. Returns 0 on success or -errno.
899 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
901 struct cgrp_cset_link
*link
;
904 INIT_LIST_HEAD(tmp_links
);
906 for (i
= 0; i
< count
; i
++) {
907 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
909 free_cgrp_cset_links(tmp_links
);
912 list_add(&link
->cset_link
, tmp_links
);
918 * link_css_set - a helper function to link a css_set to a cgroup
919 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
920 * @cset: the css_set to be linked
921 * @cgrp: the destination cgroup
923 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
926 struct cgrp_cset_link
*link
;
928 BUG_ON(list_empty(tmp_links
));
930 if (cgroup_on_dfl(cgrp
))
931 cset
->dfl_cgrp
= cgrp
;
933 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
938 * Always add links to the tail of the lists so that the lists are
939 * in choronological order.
941 list_move_tail(&link
->cset_link
, &cgrp
->cset_links
);
942 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
944 if (cgroup_parent(cgrp
))
945 cgroup_get_live(cgrp
);
949 * find_css_set - return a new css_set with one cgroup updated
950 * @old_cset: the baseline css_set
951 * @cgrp: the cgroup to be updated
953 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
954 * substituted into the appropriate hierarchy.
956 static struct css_set
*find_css_set(struct css_set
*old_cset
,
959 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
960 struct css_set
*cset
;
961 struct list_head tmp_links
;
962 struct cgrp_cset_link
*link
;
963 struct cgroup_subsys
*ss
;
967 lockdep_assert_held(&cgroup_mutex
);
969 /* First see if we already have a cgroup group that matches
971 spin_lock_irq(&css_set_lock
);
972 cset
= find_existing_css_set(old_cset
, cgrp
, template);
975 spin_unlock_irq(&css_set_lock
);
980 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
984 /* Allocate all the cgrp_cset_link objects that we'll need */
985 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
990 refcount_set(&cset
->refcount
, 1);
991 INIT_LIST_HEAD(&cset
->tasks
);
992 INIT_LIST_HEAD(&cset
->mg_tasks
);
993 INIT_LIST_HEAD(&cset
->task_iters
);
994 INIT_HLIST_NODE(&cset
->hlist
);
995 INIT_LIST_HEAD(&cset
->cgrp_links
);
996 INIT_LIST_HEAD(&cset
->mg_preload_node
);
997 INIT_LIST_HEAD(&cset
->mg_node
);
999 /* Copy the set of subsystem state objects generated in
1000 * find_existing_css_set() */
1001 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
1003 spin_lock_irq(&css_set_lock
);
1004 /* Add reference counts and links from the new css_set. */
1005 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
1006 struct cgroup
*c
= link
->cgrp
;
1008 if (c
->root
== cgrp
->root
)
1010 link_css_set(&tmp_links
, cset
, c
);
1013 BUG_ON(!list_empty(&tmp_links
));
1017 /* Add @cset to the hash table */
1018 key
= css_set_hash(cset
->subsys
);
1019 hash_add(css_set_table
, &cset
->hlist
, key
);
1021 for_each_subsys(ss
, ssid
) {
1022 struct cgroup_subsys_state
*css
= cset
->subsys
[ssid
];
1024 list_add_tail(&cset
->e_cset_node
[ssid
],
1025 &css
->cgroup
->e_csets
[ssid
]);
1029 spin_unlock_irq(&css_set_lock
);
1034 struct cgroup_root
*cgroup_root_from_kf(struct kernfs_root
*kf_root
)
1036 struct cgroup
*root_cgrp
= kf_root
->kn
->priv
;
1038 return root_cgrp
->root
;
1041 static int cgroup_init_root_id(struct cgroup_root
*root
)
1045 lockdep_assert_held(&cgroup_mutex
);
1047 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, 0, 0, GFP_KERNEL
);
1051 root
->hierarchy_id
= id
;
1055 static void cgroup_exit_root_id(struct cgroup_root
*root
)
1057 lockdep_assert_held(&cgroup_mutex
);
1059 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1062 void cgroup_free_root(struct cgroup_root
*root
)
1065 idr_destroy(&root
->cgroup_idr
);
1070 static void cgroup_destroy_root(struct cgroup_root
*root
)
1072 struct cgroup
*cgrp
= &root
->cgrp
;
1073 struct cgrp_cset_link
*link
, *tmp_link
;
1075 trace_cgroup_destroy_root(root
);
1077 cgroup_lock_and_drain_offline(&cgrp_dfl_root
.cgrp
);
1079 BUG_ON(atomic_read(&root
->nr_cgrps
));
1080 BUG_ON(!list_empty(&cgrp
->self
.children
));
1082 /* Rebind all subsystems back to the default hierarchy */
1083 WARN_ON(rebind_subsystems(&cgrp_dfl_root
, root
->subsys_mask
));
1086 * Release all the links from cset_links to this hierarchy's
1089 spin_lock_irq(&css_set_lock
);
1091 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1092 list_del(&link
->cset_link
);
1093 list_del(&link
->cgrp_link
);
1097 spin_unlock_irq(&css_set_lock
);
1099 if (!list_empty(&root
->root_list
)) {
1100 list_del(&root
->root_list
);
1101 cgroup_root_count
--;
1104 cgroup_exit_root_id(root
);
1106 mutex_unlock(&cgroup_mutex
);
1108 kernfs_destroy_root(root
->kf_root
);
1109 cgroup_free_root(root
);
1113 * look up cgroup associated with current task's cgroup namespace on the
1114 * specified hierarchy
1116 static struct cgroup
*
1117 current_cgns_cgroup_from_root(struct cgroup_root
*root
)
1119 struct cgroup
*res
= NULL
;
1120 struct css_set
*cset
;
1122 lockdep_assert_held(&css_set_lock
);
1126 cset
= current
->nsproxy
->cgroup_ns
->root_cset
;
1127 if (cset
== &init_css_set
) {
1130 struct cgrp_cset_link
*link
;
1132 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
1133 struct cgroup
*c
= link
->cgrp
;
1135 if (c
->root
== root
) {
1147 /* look up cgroup associated with given css_set on the specified hierarchy */
1148 static struct cgroup
*cset_cgroup_from_root(struct css_set
*cset
,
1149 struct cgroup_root
*root
)
1151 struct cgroup
*res
= NULL
;
1153 lockdep_assert_held(&cgroup_mutex
);
1154 lockdep_assert_held(&css_set_lock
);
1156 if (cset
== &init_css_set
) {
1159 struct cgrp_cset_link
*link
;
1161 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
1162 struct cgroup
*c
= link
->cgrp
;
1164 if (c
->root
== root
) {
1176 * Return the cgroup for "task" from the given hierarchy. Must be
1177 * called with cgroup_mutex and css_set_lock held.
1179 struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
1180 struct cgroup_root
*root
)
1183 * No need to lock the task - since we hold cgroup_mutex the
1184 * task can't change groups, so the only thing that can happen
1185 * is that it exits and its css is set back to init_css_set.
1187 return cset_cgroup_from_root(task_css_set(task
), root
);
1191 * A task must hold cgroup_mutex to modify cgroups.
1193 * Any task can increment and decrement the count field without lock.
1194 * So in general, code holding cgroup_mutex can't rely on the count
1195 * field not changing. However, if the count goes to zero, then only
1196 * cgroup_attach_task() can increment it again. Because a count of zero
1197 * means that no tasks are currently attached, therefore there is no
1198 * way a task attached to that cgroup can fork (the other way to
1199 * increment the count). So code holding cgroup_mutex can safely
1200 * assume that if the count is zero, it will stay zero. Similarly, if
1201 * a task holds cgroup_mutex on a cgroup with zero count, it
1202 * knows that the cgroup won't be removed, as cgroup_rmdir()
1205 * A cgroup can only be deleted if both its 'count' of using tasks
1206 * is zero, and its list of 'children' cgroups is empty. Since all
1207 * tasks in the system use _some_ cgroup, and since there is always at
1208 * least one task in the system (init, pid == 1), therefore, root cgroup
1209 * always has either children cgroups and/or using tasks. So we don't
1210 * need a special hack to ensure that root cgroup cannot be deleted.
1212 * P.S. One more locking exception. RCU is used to guard the
1213 * update of a tasks cgroup pointer by cgroup_attach_task()
1216 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
;
1218 static char *cgroup_file_name(struct cgroup
*cgrp
, const struct cftype
*cft
,
1221 struct cgroup_subsys
*ss
= cft
->ss
;
1223 if (cft
->ss
&& !(cft
->flags
& CFTYPE_NO_PREFIX
) &&
1224 !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
))
1225 snprintf(buf
, CGROUP_FILE_NAME_MAX
, "%s.%s",
1226 cgroup_on_dfl(cgrp
) ? ss
->name
: ss
->legacy_name
,
1229 strncpy(buf
, cft
->name
, CGROUP_FILE_NAME_MAX
);
1234 * cgroup_file_mode - deduce file mode of a control file
1235 * @cft: the control file in question
1237 * S_IRUGO for read, S_IWUSR for write.
1239 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
1243 if (cft
->read_u64
|| cft
->read_s64
|| cft
->seq_show
)
1246 if (cft
->write_u64
|| cft
->write_s64
|| cft
->write
) {
1247 if (cft
->flags
& CFTYPE_WORLD_WRITABLE
)
1257 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
1258 * @subtree_control: the new subtree_control mask to consider
1259 * @this_ss_mask: available subsystems
1261 * On the default hierarchy, a subsystem may request other subsystems to be
1262 * enabled together through its ->depends_on mask. In such cases, more
1263 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1265 * This function calculates which subsystems need to be enabled if
1266 * @subtree_control is to be applied while restricted to @this_ss_mask.
1268 static u16
cgroup_calc_subtree_ss_mask(u16 subtree_control
, u16 this_ss_mask
)
1270 u16 cur_ss_mask
= subtree_control
;
1271 struct cgroup_subsys
*ss
;
1274 lockdep_assert_held(&cgroup_mutex
);
1276 cur_ss_mask
|= cgrp_dfl_implicit_ss_mask
;
1279 u16 new_ss_mask
= cur_ss_mask
;
1281 do_each_subsys_mask(ss
, ssid
, cur_ss_mask
) {
1282 new_ss_mask
|= ss
->depends_on
;
1283 } while_each_subsys_mask();
1286 * Mask out subsystems which aren't available. This can
1287 * happen only if some depended-upon subsystems were bound
1288 * to non-default hierarchies.
1290 new_ss_mask
&= this_ss_mask
;
1292 if (new_ss_mask
== cur_ss_mask
)
1294 cur_ss_mask
= new_ss_mask
;
1301 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1302 * @kn: the kernfs_node being serviced
1304 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1305 * the method finishes if locking succeeded. Note that once this function
1306 * returns the cgroup returned by cgroup_kn_lock_live() may become
1307 * inaccessible any time. If the caller intends to continue to access the
1308 * cgroup, it should pin it before invoking this function.
1310 void cgroup_kn_unlock(struct kernfs_node
*kn
)
1312 struct cgroup
*cgrp
;
1314 if (kernfs_type(kn
) == KERNFS_DIR
)
1317 cgrp
= kn
->parent
->priv
;
1319 mutex_unlock(&cgroup_mutex
);
1321 kernfs_unbreak_active_protection(kn
);
1326 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1327 * @kn: the kernfs_node being serviced
1328 * @drain_offline: perform offline draining on the cgroup
1330 * This helper is to be used by a cgroup kernfs method currently servicing
1331 * @kn. It breaks the active protection, performs cgroup locking and
1332 * verifies that the associated cgroup is alive. Returns the cgroup if
1333 * alive; otherwise, %NULL. A successful return should be undone by a
1334 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1335 * cgroup is drained of offlining csses before return.
1337 * Any cgroup kernfs method implementation which requires locking the
1338 * associated cgroup should use this helper. It avoids nesting cgroup
1339 * locking under kernfs active protection and allows all kernfs operations
1340 * including self-removal.
1342 struct cgroup
*cgroup_kn_lock_live(struct kernfs_node
*kn
, bool drain_offline
)
1344 struct cgroup
*cgrp
;
1346 if (kernfs_type(kn
) == KERNFS_DIR
)
1349 cgrp
= kn
->parent
->priv
;
1352 * We're gonna grab cgroup_mutex which nests outside kernfs
1353 * active_ref. cgroup liveliness check alone provides enough
1354 * protection against removal. Ensure @cgrp stays accessible and
1355 * break the active_ref protection.
1357 if (!cgroup_tryget(cgrp
))
1359 kernfs_break_active_protection(kn
);
1362 cgroup_lock_and_drain_offline(cgrp
);
1364 mutex_lock(&cgroup_mutex
);
1366 if (!cgroup_is_dead(cgrp
))
1369 cgroup_kn_unlock(kn
);
1373 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
1375 char name
[CGROUP_FILE_NAME_MAX
];
1377 lockdep_assert_held(&cgroup_mutex
);
1379 if (cft
->file_offset
) {
1380 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, cft
->ss
);
1381 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
1383 spin_lock_irq(&cgroup_file_kn_lock
);
1385 spin_unlock_irq(&cgroup_file_kn_lock
);
1388 kernfs_remove_by_name(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
));
1392 * css_clear_dir - remove subsys files in a cgroup directory
1395 static void css_clear_dir(struct cgroup_subsys_state
*css
)
1397 struct cgroup
*cgrp
= css
->cgroup
;
1398 struct cftype
*cfts
;
1400 if (!(css
->flags
& CSS_VISIBLE
))
1403 css
->flags
&= ~CSS_VISIBLE
;
1405 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
)
1406 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1410 * css_populate_dir - create subsys files in a cgroup directory
1413 * On failure, no file is added.
1415 static int css_populate_dir(struct cgroup_subsys_state
*css
)
1417 struct cgroup
*cgrp
= css
->cgroup
;
1418 struct cftype
*cfts
, *failed_cfts
;
1421 if ((css
->flags
& CSS_VISIBLE
) || !cgrp
->kn
)
1425 if (cgroup_on_dfl(cgrp
))
1426 cfts
= cgroup_base_files
;
1428 cfts
= cgroup1_base_files
;
1430 return cgroup_addrm_files(&cgrp
->self
, cgrp
, cfts
, true);
1433 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1434 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, true);
1441 css
->flags
|= CSS_VISIBLE
;
1445 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1446 if (cfts
== failed_cfts
)
1448 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1453 int rebind_subsystems(struct cgroup_root
*dst_root
, u16 ss_mask
)
1455 struct cgroup
*dcgrp
= &dst_root
->cgrp
;
1456 struct cgroup_subsys
*ss
;
1459 lockdep_assert_held(&cgroup_mutex
);
1461 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
1463 * If @ss has non-root csses attached to it, can't move.
1464 * If @ss is an implicit controller, it is exempt from this
1465 * rule and can be stolen.
1467 if (css_next_child(NULL
, cgroup_css(&ss
->root
->cgrp
, ss
)) &&
1468 !ss
->implicit_on_dfl
)
1471 /* can't move between two non-dummy roots either */
1472 if (ss
->root
!= &cgrp_dfl_root
&& dst_root
!= &cgrp_dfl_root
)
1474 } while_each_subsys_mask();
1476 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
1477 struct cgroup_root
*src_root
= ss
->root
;
1478 struct cgroup
*scgrp
= &src_root
->cgrp
;
1479 struct cgroup_subsys_state
*css
= cgroup_css(scgrp
, ss
);
1480 struct css_set
*cset
;
1482 WARN_ON(!css
|| cgroup_css(dcgrp
, ss
));
1484 /* disable from the source */
1485 src_root
->subsys_mask
&= ~(1 << ssid
);
1486 WARN_ON(cgroup_apply_control(scgrp
));
1487 cgroup_finalize_control(scgrp
, 0);
1490 RCU_INIT_POINTER(scgrp
->subsys
[ssid
], NULL
);
1491 rcu_assign_pointer(dcgrp
->subsys
[ssid
], css
);
1492 ss
->root
= dst_root
;
1493 css
->cgroup
= dcgrp
;
1495 spin_lock_irq(&css_set_lock
);
1496 hash_for_each(css_set_table
, i
, cset
, hlist
)
1497 list_move_tail(&cset
->e_cset_node
[ss
->id
],
1498 &dcgrp
->e_csets
[ss
->id
]);
1499 spin_unlock_irq(&css_set_lock
);
1501 /* default hierarchy doesn't enable controllers by default */
1502 dst_root
->subsys_mask
|= 1 << ssid
;
1503 if (dst_root
== &cgrp_dfl_root
) {
1504 static_branch_enable(cgroup_subsys_on_dfl_key
[ssid
]);
1506 dcgrp
->subtree_control
|= 1 << ssid
;
1507 static_branch_disable(cgroup_subsys_on_dfl_key
[ssid
]);
1510 ret
= cgroup_apply_control(dcgrp
);
1512 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1517 } while_each_subsys_mask();
1519 kernfs_activate(dcgrp
->kn
);
1523 int cgroup_show_path(struct seq_file
*sf
, struct kernfs_node
*kf_node
,
1524 struct kernfs_root
*kf_root
)
1528 struct cgroup_root
*kf_cgroot
= cgroup_root_from_kf(kf_root
);
1529 struct cgroup
*ns_cgroup
;
1531 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1535 spin_lock_irq(&css_set_lock
);
1536 ns_cgroup
= current_cgns_cgroup_from_root(kf_cgroot
);
1537 len
= kernfs_path_from_node(kf_node
, ns_cgroup
->kn
, buf
, PATH_MAX
);
1538 spin_unlock_irq(&css_set_lock
);
1540 if (len
>= PATH_MAX
)
1543 seq_escape(sf
, buf
, " \t\n\\");
1550 static int parse_cgroup_root_flags(char *data
, unsigned int *root_flags
)
1559 while ((token
= strsep(&data
, ",")) != NULL
) {
1560 if (!strcmp(token
, "nsdelegate")) {
1561 *root_flags
|= CGRP_ROOT_NS_DELEGATE
;
1565 pr_err("cgroup2: unknown option \"%s\"\n", token
);
1572 static void apply_cgroup_root_flags(unsigned int root_flags
)
1574 if (current
->nsproxy
->cgroup_ns
== &init_cgroup_ns
) {
1575 if (root_flags
& CGRP_ROOT_NS_DELEGATE
)
1576 cgrp_dfl_root
.flags
|= CGRP_ROOT_NS_DELEGATE
;
1578 cgrp_dfl_root
.flags
&= ~CGRP_ROOT_NS_DELEGATE
;
1582 static int cgroup_show_options(struct seq_file
*seq
, struct kernfs_root
*kf_root
)
1584 if (cgrp_dfl_root
.flags
& CGRP_ROOT_NS_DELEGATE
)
1585 seq_puts(seq
, ",nsdelegate");
1589 static int cgroup_remount(struct kernfs_root
*kf_root
, int *flags
, char *data
)
1591 unsigned int root_flags
;
1594 ret
= parse_cgroup_root_flags(data
, &root_flags
);
1598 apply_cgroup_root_flags(root_flags
);
1603 * To reduce the fork() overhead for systems that are not actually using
1604 * their cgroups capability, we don't maintain the lists running through
1605 * each css_set to its tasks until we see the list actually used - in other
1606 * words after the first mount.
1608 static bool use_task_css_set_links __read_mostly
;
1610 static void cgroup_enable_task_cg_lists(void)
1612 struct task_struct
*p
, *g
;
1614 spin_lock_irq(&css_set_lock
);
1616 if (use_task_css_set_links
)
1619 use_task_css_set_links
= true;
1622 * We need tasklist_lock because RCU is not safe against
1623 * while_each_thread(). Besides, a forking task that has passed
1624 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1625 * is not guaranteed to have its child immediately visible in the
1626 * tasklist if we walk through it with RCU.
1628 read_lock(&tasklist_lock
);
1629 do_each_thread(g
, p
) {
1630 WARN_ON_ONCE(!list_empty(&p
->cg_list
) ||
1631 task_css_set(p
) != &init_css_set
);
1634 * We should check if the process is exiting, otherwise
1635 * it will race with cgroup_exit() in that the list
1636 * entry won't be deleted though the process has exited.
1637 * Do it while holding siglock so that we don't end up
1638 * racing against cgroup_exit().
1640 * Interrupts were already disabled while acquiring
1641 * the css_set_lock, so we do not need to disable it
1642 * again when acquiring the sighand->siglock here.
1644 spin_lock(&p
->sighand
->siglock
);
1645 if (!(p
->flags
& PF_EXITING
)) {
1646 struct css_set
*cset
= task_css_set(p
);
1648 if (!css_set_populated(cset
))
1649 css_set_update_populated(cset
, true);
1650 list_add_tail(&p
->cg_list
, &cset
->tasks
);
1654 spin_unlock(&p
->sighand
->siglock
);
1655 } while_each_thread(g
, p
);
1656 read_unlock(&tasklist_lock
);
1658 spin_unlock_irq(&css_set_lock
);
1661 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1663 struct cgroup_subsys
*ss
;
1666 INIT_LIST_HEAD(&cgrp
->self
.sibling
);
1667 INIT_LIST_HEAD(&cgrp
->self
.children
);
1668 INIT_LIST_HEAD(&cgrp
->cset_links
);
1669 INIT_LIST_HEAD(&cgrp
->pidlists
);
1670 mutex_init(&cgrp
->pidlist_mutex
);
1671 cgrp
->self
.cgroup
= cgrp
;
1672 cgrp
->self
.flags
|= CSS_ONLINE
;
1674 for_each_subsys(ss
, ssid
)
1675 INIT_LIST_HEAD(&cgrp
->e_csets
[ssid
]);
1677 init_waitqueue_head(&cgrp
->offline_waitq
);
1678 INIT_WORK(&cgrp
->release_agent_work
, cgroup1_release_agent
);
1681 void init_cgroup_root(struct cgroup_root
*root
, struct cgroup_sb_opts
*opts
)
1683 struct cgroup
*cgrp
= &root
->cgrp
;
1685 INIT_LIST_HEAD(&root
->root_list
);
1686 atomic_set(&root
->nr_cgrps
, 1);
1688 init_cgroup_housekeeping(cgrp
);
1689 idr_init(&root
->cgroup_idr
);
1691 root
->flags
= opts
->flags
;
1692 if (opts
->release_agent
)
1693 strcpy(root
->release_agent_path
, opts
->release_agent
);
1695 strcpy(root
->name
, opts
->name
);
1696 if (opts
->cpuset_clone_children
)
1697 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->cgrp
.flags
);
1700 int cgroup_setup_root(struct cgroup_root
*root
, u16 ss_mask
, int ref_flags
)
1702 LIST_HEAD(tmp_links
);
1703 struct cgroup
*root_cgrp
= &root
->cgrp
;
1704 struct kernfs_syscall_ops
*kf_sops
;
1705 struct css_set
*cset
;
1708 lockdep_assert_held(&cgroup_mutex
);
1710 ret
= cgroup_idr_alloc(&root
->cgroup_idr
, root_cgrp
, 1, 2, GFP_KERNEL
);
1713 root_cgrp
->id
= ret
;
1714 root_cgrp
->ancestor_ids
[0] = ret
;
1716 ret
= percpu_ref_init(&root_cgrp
->self
.refcnt
, css_release
,
1717 ref_flags
, GFP_KERNEL
);
1722 * We're accessing css_set_count without locking css_set_lock here,
1723 * but that's OK - it can only be increased by someone holding
1724 * cgroup_lock, and that's us. Later rebinding may disable
1725 * controllers on the default hierarchy and thus create new csets,
1726 * which can't be more than the existing ones. Allocate 2x.
1728 ret
= allocate_cgrp_cset_links(2 * css_set_count
, &tmp_links
);
1732 ret
= cgroup_init_root_id(root
);
1736 kf_sops
= root
== &cgrp_dfl_root
?
1737 &cgroup_kf_syscall_ops
: &cgroup1_kf_syscall_ops
;
1739 root
->kf_root
= kernfs_create_root(kf_sops
,
1740 KERNFS_ROOT_CREATE_DEACTIVATED
,
1742 if (IS_ERR(root
->kf_root
)) {
1743 ret
= PTR_ERR(root
->kf_root
);
1746 root_cgrp
->kn
= root
->kf_root
->kn
;
1748 ret
= css_populate_dir(&root_cgrp
->self
);
1752 ret
= rebind_subsystems(root
, ss_mask
);
1756 trace_cgroup_setup_root(root
);
1759 * There must be no failure case after here, since rebinding takes
1760 * care of subsystems' refcounts, which are explicitly dropped in
1761 * the failure exit path.
1763 list_add(&root
->root_list
, &cgroup_roots
);
1764 cgroup_root_count
++;
1767 * Link the root cgroup in this hierarchy into all the css_set
1770 spin_lock_irq(&css_set_lock
);
1771 hash_for_each(css_set_table
, i
, cset
, hlist
) {
1772 link_css_set(&tmp_links
, cset
, root_cgrp
);
1773 if (css_set_populated(cset
))
1774 cgroup_update_populated(root_cgrp
, true);
1776 spin_unlock_irq(&css_set_lock
);
1778 BUG_ON(!list_empty(&root_cgrp
->self
.children
));
1779 BUG_ON(atomic_read(&root
->nr_cgrps
) != 1);
1781 kernfs_activate(root_cgrp
->kn
);
1786 kernfs_destroy_root(root
->kf_root
);
1787 root
->kf_root
= NULL
;
1789 cgroup_exit_root_id(root
);
1791 percpu_ref_exit(&root_cgrp
->self
.refcnt
);
1793 free_cgrp_cset_links(&tmp_links
);
1797 struct dentry
*cgroup_do_mount(struct file_system_type
*fs_type
, int flags
,
1798 struct cgroup_root
*root
, unsigned long magic
,
1799 struct cgroup_namespace
*ns
)
1801 struct dentry
*dentry
;
1804 dentry
= kernfs_mount(fs_type
, flags
, root
->kf_root
, magic
, &new_sb
);
1807 * In non-init cgroup namespace, instead of root cgroup's dentry,
1808 * we return the dentry corresponding to the cgroupns->root_cgrp.
1810 if (!IS_ERR(dentry
) && ns
!= &init_cgroup_ns
) {
1811 struct dentry
*nsdentry
;
1812 struct cgroup
*cgrp
;
1814 mutex_lock(&cgroup_mutex
);
1815 spin_lock_irq(&css_set_lock
);
1817 cgrp
= cset_cgroup_from_root(ns
->root_cset
, root
);
1819 spin_unlock_irq(&css_set_lock
);
1820 mutex_unlock(&cgroup_mutex
);
1822 nsdentry
= kernfs_node_dentry(cgrp
->kn
, dentry
->d_sb
);
1827 if (IS_ERR(dentry
) || !new_sb
)
1828 cgroup_put(&root
->cgrp
);
1833 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1834 int flags
, const char *unused_dev_name
,
1837 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
1838 struct dentry
*dentry
;
1843 /* Check if the caller has permission to mount. */
1844 if (!ns_capable(ns
->user_ns
, CAP_SYS_ADMIN
)) {
1846 return ERR_PTR(-EPERM
);
1850 * The first time anyone tries to mount a cgroup, enable the list
1851 * linking each css_set to its tasks and fix up all existing tasks.
1853 if (!use_task_css_set_links
)
1854 cgroup_enable_task_cg_lists();
1856 if (fs_type
== &cgroup2_fs_type
) {
1857 unsigned int root_flags
;
1859 ret
= parse_cgroup_root_flags(data
, &root_flags
);
1862 return ERR_PTR(ret
);
1865 cgrp_dfl_visible
= true;
1866 cgroup_get_live(&cgrp_dfl_root
.cgrp
);
1868 dentry
= cgroup_do_mount(&cgroup2_fs_type
, flags
, &cgrp_dfl_root
,
1869 CGROUP2_SUPER_MAGIC
, ns
);
1870 if (!IS_ERR(dentry
))
1871 apply_cgroup_root_flags(root_flags
);
1873 dentry
= cgroup1_mount(&cgroup_fs_type
, flags
, data
,
1874 CGROUP_SUPER_MAGIC
, ns
);
1881 static void cgroup_kill_sb(struct super_block
*sb
)
1883 struct kernfs_root
*kf_root
= kernfs_root_from_sb(sb
);
1884 struct cgroup_root
*root
= cgroup_root_from_kf(kf_root
);
1887 * If @root doesn't have any mounts or children, start killing it.
1888 * This prevents new mounts by disabling percpu_ref_tryget_live().
1889 * cgroup_mount() may wait for @root's release.
1891 * And don't kill the default root.
1893 if (!list_empty(&root
->cgrp
.self
.children
) ||
1894 root
== &cgrp_dfl_root
)
1895 cgroup_put(&root
->cgrp
);
1897 percpu_ref_kill(&root
->cgrp
.self
.refcnt
);
1902 struct file_system_type cgroup_fs_type
= {
1904 .mount
= cgroup_mount
,
1905 .kill_sb
= cgroup_kill_sb
,
1906 .fs_flags
= FS_USERNS_MOUNT
,
1909 static struct file_system_type cgroup2_fs_type
= {
1911 .mount
= cgroup_mount
,
1912 .kill_sb
= cgroup_kill_sb
,
1913 .fs_flags
= FS_USERNS_MOUNT
,
1916 int cgroup_path_ns_locked(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
1917 struct cgroup_namespace
*ns
)
1919 struct cgroup
*root
= cset_cgroup_from_root(ns
->root_cset
, cgrp
->root
);
1921 return kernfs_path_from_node(cgrp
->kn
, root
->kn
, buf
, buflen
);
1924 int cgroup_path_ns(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
1925 struct cgroup_namespace
*ns
)
1929 mutex_lock(&cgroup_mutex
);
1930 spin_lock_irq(&css_set_lock
);
1932 ret
= cgroup_path_ns_locked(cgrp
, buf
, buflen
, ns
);
1934 spin_unlock_irq(&css_set_lock
);
1935 mutex_unlock(&cgroup_mutex
);
1939 EXPORT_SYMBOL_GPL(cgroup_path_ns
);
1942 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1943 * @task: target task
1944 * @buf: the buffer to write the path into
1945 * @buflen: the length of the buffer
1947 * Determine @task's cgroup on the first (the one with the lowest non-zero
1948 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1949 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1950 * cgroup controller callbacks.
1952 * Return value is the same as kernfs_path().
1954 int task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
1956 struct cgroup_root
*root
;
1957 struct cgroup
*cgrp
;
1958 int hierarchy_id
= 1;
1961 mutex_lock(&cgroup_mutex
);
1962 spin_lock_irq(&css_set_lock
);
1964 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
1967 cgrp
= task_cgroup_from_root(task
, root
);
1968 ret
= cgroup_path_ns_locked(cgrp
, buf
, buflen
, &init_cgroup_ns
);
1970 /* if no hierarchy exists, everyone is in "/" */
1971 ret
= strlcpy(buf
, "/", buflen
);
1974 spin_unlock_irq(&css_set_lock
);
1975 mutex_unlock(&cgroup_mutex
);
1978 EXPORT_SYMBOL_GPL(task_cgroup_path
);
1981 * cgroup_migrate_add_task - add a migration target task to a migration context
1982 * @task: target task
1983 * @mgctx: target migration context
1985 * Add @task, which is a migration target, to @mgctx->tset. This function
1986 * becomes noop if @task doesn't need to be migrated. @task's css_set
1987 * should have been added as a migration source and @task->cg_list will be
1988 * moved from the css_set's tasks list to mg_tasks one.
1990 static void cgroup_migrate_add_task(struct task_struct
*task
,
1991 struct cgroup_mgctx
*mgctx
)
1993 struct css_set
*cset
;
1995 lockdep_assert_held(&css_set_lock
);
1997 /* @task either already exited or can't exit until the end */
1998 if (task
->flags
& PF_EXITING
)
2001 /* leave @task alone if post_fork() hasn't linked it yet */
2002 if (list_empty(&task
->cg_list
))
2005 cset
= task_css_set(task
);
2006 if (!cset
->mg_src_cgrp
)
2009 mgctx
->tset
.nr_tasks
++;
2011 list_move_tail(&task
->cg_list
, &cset
->mg_tasks
);
2012 if (list_empty(&cset
->mg_node
))
2013 list_add_tail(&cset
->mg_node
,
2014 &mgctx
->tset
.src_csets
);
2015 if (list_empty(&cset
->mg_dst_cset
->mg_node
))
2016 list_add_tail(&cset
->mg_dst_cset
->mg_node
,
2017 &mgctx
->tset
.dst_csets
);
2021 * cgroup_taskset_first - reset taskset and return the first task
2022 * @tset: taskset of interest
2023 * @dst_cssp: output variable for the destination css
2025 * @tset iteration is initialized and the first task is returned.
2027 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
,
2028 struct cgroup_subsys_state
**dst_cssp
)
2030 tset
->cur_cset
= list_first_entry(tset
->csets
, struct css_set
, mg_node
);
2031 tset
->cur_task
= NULL
;
2033 return cgroup_taskset_next(tset
, dst_cssp
);
2037 * cgroup_taskset_next - iterate to the next task in taskset
2038 * @tset: taskset of interest
2039 * @dst_cssp: output variable for the destination css
2041 * Return the next task in @tset. Iteration must have been initialized
2042 * with cgroup_taskset_first().
2044 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
,
2045 struct cgroup_subsys_state
**dst_cssp
)
2047 struct css_set
*cset
= tset
->cur_cset
;
2048 struct task_struct
*task
= tset
->cur_task
;
2050 while (&cset
->mg_node
!= tset
->csets
) {
2052 task
= list_first_entry(&cset
->mg_tasks
,
2053 struct task_struct
, cg_list
);
2055 task
= list_next_entry(task
, cg_list
);
2057 if (&task
->cg_list
!= &cset
->mg_tasks
) {
2058 tset
->cur_cset
= cset
;
2059 tset
->cur_task
= task
;
2062 * This function may be called both before and
2063 * after cgroup_taskset_migrate(). The two cases
2064 * can be distinguished by looking at whether @cset
2065 * has its ->mg_dst_cset set.
2067 if (cset
->mg_dst_cset
)
2068 *dst_cssp
= cset
->mg_dst_cset
->subsys
[tset
->ssid
];
2070 *dst_cssp
= cset
->subsys
[tset
->ssid
];
2075 cset
= list_next_entry(cset
, mg_node
);
2083 * cgroup_taskset_migrate - migrate a taskset
2084 * @mgctx: migration context
2086 * Migrate tasks in @mgctx as setup by migration preparation functions.
2087 * This function fails iff one of the ->can_attach callbacks fails and
2088 * guarantees that either all or none of the tasks in @mgctx are migrated.
2089 * @mgctx is consumed regardless of success.
2091 static int cgroup_migrate_execute(struct cgroup_mgctx
*mgctx
)
2093 struct cgroup_taskset
*tset
= &mgctx
->tset
;
2094 struct cgroup_subsys
*ss
;
2095 struct task_struct
*task
, *tmp_task
;
2096 struct css_set
*cset
, *tmp_cset
;
2097 int ssid
, failed_ssid
, ret
;
2099 /* check that we can legitimately attach to the cgroup */
2100 if (tset
->nr_tasks
) {
2101 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2102 if (ss
->can_attach
) {
2104 ret
= ss
->can_attach(tset
);
2107 goto out_cancel_attach
;
2110 } while_each_subsys_mask();
2114 * Now that we're guaranteed success, proceed to move all tasks to
2115 * the new cgroup. There are no failure cases after here, so this
2116 * is the commit point.
2118 spin_lock_irq(&css_set_lock
);
2119 list_for_each_entry(cset
, &tset
->src_csets
, mg_node
) {
2120 list_for_each_entry_safe(task
, tmp_task
, &cset
->mg_tasks
, cg_list
) {
2121 struct css_set
*from_cset
= task_css_set(task
);
2122 struct css_set
*to_cset
= cset
->mg_dst_cset
;
2124 get_css_set(to_cset
);
2125 to_cset
->nr_tasks
++;
2126 css_set_move_task(task
, from_cset
, to_cset
, true);
2127 put_css_set_locked(from_cset
);
2128 from_cset
->nr_tasks
--;
2131 spin_unlock_irq(&css_set_lock
);
2134 * Migration is committed, all target tasks are now on dst_csets.
2135 * Nothing is sensitive to fork() after this point. Notify
2136 * controllers that migration is complete.
2138 tset
->csets
= &tset
->dst_csets
;
2140 if (tset
->nr_tasks
) {
2141 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2146 } while_each_subsys_mask();
2150 goto out_release_tset
;
2153 if (tset
->nr_tasks
) {
2154 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2155 if (ssid
== failed_ssid
)
2157 if (ss
->cancel_attach
) {
2159 ss
->cancel_attach(tset
);
2161 } while_each_subsys_mask();
2164 spin_lock_irq(&css_set_lock
);
2165 list_splice_init(&tset
->dst_csets
, &tset
->src_csets
);
2166 list_for_each_entry_safe(cset
, tmp_cset
, &tset
->src_csets
, mg_node
) {
2167 list_splice_tail_init(&cset
->mg_tasks
, &cset
->tasks
);
2168 list_del_init(&cset
->mg_node
);
2170 spin_unlock_irq(&css_set_lock
);
2173 * Re-initialize the cgroup_taskset structure in case it is reused
2174 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
2178 tset
->csets
= &tset
->src_csets
;
2183 * cgroup_may_migrate_to - verify whether a cgroup can be migration destination
2184 * @dst_cgrp: destination cgroup to test
2186 * On the default hierarchy, except for the root, subtree_control must be
2187 * zero for migration destination cgroups with tasks so that child cgroups
2188 * don't compete against tasks.
2190 bool cgroup_may_migrate_to(struct cgroup
*dst_cgrp
)
2192 return !cgroup_on_dfl(dst_cgrp
) || !cgroup_parent(dst_cgrp
) ||
2193 !dst_cgrp
->subtree_control
;
2197 * cgroup_migrate_finish - cleanup after attach
2198 * @mgctx: migration context
2200 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2201 * those functions for details.
2203 void cgroup_migrate_finish(struct cgroup_mgctx
*mgctx
)
2205 LIST_HEAD(preloaded
);
2206 struct css_set
*cset
, *tmp_cset
;
2208 lockdep_assert_held(&cgroup_mutex
);
2210 spin_lock_irq(&css_set_lock
);
2212 list_splice_tail_init(&mgctx
->preloaded_src_csets
, &preloaded
);
2213 list_splice_tail_init(&mgctx
->preloaded_dst_csets
, &preloaded
);
2215 list_for_each_entry_safe(cset
, tmp_cset
, &preloaded
, mg_preload_node
) {
2216 cset
->mg_src_cgrp
= NULL
;
2217 cset
->mg_dst_cgrp
= NULL
;
2218 cset
->mg_dst_cset
= NULL
;
2219 list_del_init(&cset
->mg_preload_node
);
2220 put_css_set_locked(cset
);
2223 spin_unlock_irq(&css_set_lock
);
2227 * cgroup_migrate_add_src - add a migration source css_set
2228 * @src_cset: the source css_set to add
2229 * @dst_cgrp: the destination cgroup
2230 * @mgctx: migration context
2232 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2233 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
2234 * up by cgroup_migrate_finish().
2236 * This function may be called without holding cgroup_threadgroup_rwsem
2237 * even if the target is a process. Threads may be created and destroyed
2238 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2239 * into play and the preloaded css_sets are guaranteed to cover all
2242 void cgroup_migrate_add_src(struct css_set
*src_cset
,
2243 struct cgroup
*dst_cgrp
,
2244 struct cgroup_mgctx
*mgctx
)
2246 struct cgroup
*src_cgrp
;
2248 lockdep_assert_held(&cgroup_mutex
);
2249 lockdep_assert_held(&css_set_lock
);
2252 * If ->dead, @src_set is associated with one or more dead cgroups
2253 * and doesn't contain any migratable tasks. Ignore it early so
2254 * that the rest of migration path doesn't get confused by it.
2259 src_cgrp
= cset_cgroup_from_root(src_cset
, dst_cgrp
->root
);
2261 if (!list_empty(&src_cset
->mg_preload_node
))
2264 WARN_ON(src_cset
->mg_src_cgrp
);
2265 WARN_ON(src_cset
->mg_dst_cgrp
);
2266 WARN_ON(!list_empty(&src_cset
->mg_tasks
));
2267 WARN_ON(!list_empty(&src_cset
->mg_node
));
2269 src_cset
->mg_src_cgrp
= src_cgrp
;
2270 src_cset
->mg_dst_cgrp
= dst_cgrp
;
2271 get_css_set(src_cset
);
2272 list_add_tail(&src_cset
->mg_preload_node
, &mgctx
->preloaded_src_csets
);
2276 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
2277 * @mgctx: migration context
2279 * Tasks are about to be moved and all the source css_sets have been
2280 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2281 * pins all destination css_sets, links each to its source, and append them
2282 * to @mgctx->preloaded_dst_csets.
2284 * This function must be called after cgroup_migrate_add_src() has been
2285 * called on each migration source css_set. After migration is performed
2286 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2289 int cgroup_migrate_prepare_dst(struct cgroup_mgctx
*mgctx
)
2291 struct css_set
*src_cset
, *tmp_cset
;
2293 lockdep_assert_held(&cgroup_mutex
);
2295 /* look up the dst cset for each src cset and link it to src */
2296 list_for_each_entry_safe(src_cset
, tmp_cset
, &mgctx
->preloaded_src_csets
,
2298 struct css_set
*dst_cset
;
2299 struct cgroup_subsys
*ss
;
2302 dst_cset
= find_css_set(src_cset
, src_cset
->mg_dst_cgrp
);
2306 WARN_ON_ONCE(src_cset
->mg_dst_cset
|| dst_cset
->mg_dst_cset
);
2309 * If src cset equals dst, it's noop. Drop the src.
2310 * cgroup_migrate() will skip the cset too. Note that we
2311 * can't handle src == dst as some nodes are used by both.
2313 if (src_cset
== dst_cset
) {
2314 src_cset
->mg_src_cgrp
= NULL
;
2315 src_cset
->mg_dst_cgrp
= NULL
;
2316 list_del_init(&src_cset
->mg_preload_node
);
2317 put_css_set(src_cset
);
2318 put_css_set(dst_cset
);
2322 src_cset
->mg_dst_cset
= dst_cset
;
2324 if (list_empty(&dst_cset
->mg_preload_node
))
2325 list_add_tail(&dst_cset
->mg_preload_node
,
2326 &mgctx
->preloaded_dst_csets
);
2328 put_css_set(dst_cset
);
2330 for_each_subsys(ss
, ssid
)
2331 if (src_cset
->subsys
[ssid
] != dst_cset
->subsys
[ssid
])
2332 mgctx
->ss_mask
|= 1 << ssid
;
2337 cgroup_migrate_finish(mgctx
);
2342 * cgroup_migrate - migrate a process or task to a cgroup
2343 * @leader: the leader of the process or the task to migrate
2344 * @threadgroup: whether @leader points to the whole process or a single task
2345 * @mgctx: migration context
2347 * Migrate a process or task denoted by @leader. If migrating a process,
2348 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2349 * responsible for invoking cgroup_migrate_add_src() and
2350 * cgroup_migrate_prepare_dst() on the targets before invoking this
2351 * function and following up with cgroup_migrate_finish().
2353 * As long as a controller's ->can_attach() doesn't fail, this function is
2354 * guaranteed to succeed. This means that, excluding ->can_attach()
2355 * failure, when migrating multiple targets, the success or failure can be
2356 * decided for all targets by invoking group_migrate_prepare_dst() before
2357 * actually starting migrating.
2359 int cgroup_migrate(struct task_struct
*leader
, bool threadgroup
,
2360 struct cgroup_mgctx
*mgctx
)
2362 struct task_struct
*task
;
2365 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2366 * already PF_EXITING could be freed from underneath us unless we
2367 * take an rcu_read_lock.
2369 spin_lock_irq(&css_set_lock
);
2373 cgroup_migrate_add_task(task
, mgctx
);
2376 } while_each_thread(leader
, task
);
2378 spin_unlock_irq(&css_set_lock
);
2380 return cgroup_migrate_execute(mgctx
);
2384 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2385 * @dst_cgrp: the cgroup to attach to
2386 * @leader: the task or the leader of the threadgroup to be attached
2387 * @threadgroup: attach the whole threadgroup?
2389 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
2391 int cgroup_attach_task(struct cgroup
*dst_cgrp
, struct task_struct
*leader
,
2394 DEFINE_CGROUP_MGCTX(mgctx
);
2395 struct task_struct
*task
;
2398 if (!cgroup_may_migrate_to(dst_cgrp
))
2401 /* look up all src csets */
2402 spin_lock_irq(&css_set_lock
);
2406 cgroup_migrate_add_src(task_css_set(task
), dst_cgrp
, &mgctx
);
2409 } while_each_thread(leader
, task
);
2411 spin_unlock_irq(&css_set_lock
);
2413 /* prepare dst csets and commit */
2414 ret
= cgroup_migrate_prepare_dst(&mgctx
);
2416 ret
= cgroup_migrate(leader
, threadgroup
, &mgctx
);
2418 cgroup_migrate_finish(&mgctx
);
2421 trace_cgroup_attach_task(dst_cgrp
, leader
, threadgroup
);
2426 static int cgroup_procs_write_permission(struct task_struct
*task
,
2427 struct cgroup
*dst_cgrp
,
2428 struct kernfs_open_file
*of
)
2430 struct super_block
*sb
= of
->file
->f_path
.dentry
->d_sb
;
2431 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
2432 struct cgroup
*root_cgrp
= ns
->root_cset
->dfl_cgrp
;
2433 struct cgroup
*src_cgrp
, *com_cgrp
;
2434 struct inode
*inode
;
2437 if (!cgroup_on_dfl(dst_cgrp
)) {
2438 const struct cred
*cred
= current_cred();
2439 const struct cred
*tcred
= get_task_cred(task
);
2442 * even if we're attaching all tasks in the thread group,
2443 * we only need to check permissions on one of them.
2445 if (uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) ||
2446 uid_eq(cred
->euid
, tcred
->uid
) ||
2447 uid_eq(cred
->euid
, tcred
->suid
))
2456 /* find the source cgroup */
2457 spin_lock_irq(&css_set_lock
);
2458 src_cgrp
= task_cgroup_from_root(task
, &cgrp_dfl_root
);
2459 spin_unlock_irq(&css_set_lock
);
2461 /* and the common ancestor */
2462 com_cgrp
= src_cgrp
;
2463 while (!cgroup_is_descendant(dst_cgrp
, com_cgrp
))
2464 com_cgrp
= cgroup_parent(com_cgrp
);
2466 /* %current should be authorized to migrate to the common ancestor */
2467 inode
= kernfs_get_inode(sb
, com_cgrp
->procs_file
.kn
);
2471 ret
= inode_permission(inode
, MAY_WRITE
);
2477 * If namespaces are delegation boundaries, %current must be able
2478 * to see both source and destination cgroups from its namespace.
2480 if ((cgrp_dfl_root
.flags
& CGRP_ROOT_NS_DELEGATE
) &&
2481 (!cgroup_is_descendant(src_cgrp
, root_cgrp
) ||
2482 !cgroup_is_descendant(dst_cgrp
, root_cgrp
)))
2489 * Find the task_struct of the task to attach by vpid and pass it along to the
2490 * function to attach either it or all tasks in its threadgroup. Will lock
2491 * cgroup_mutex and threadgroup.
2493 ssize_t
__cgroup_procs_write(struct kernfs_open_file
*of
, char *buf
,
2494 size_t nbytes
, loff_t off
, bool threadgroup
)
2496 struct task_struct
*tsk
;
2497 struct cgroup_subsys
*ss
;
2498 struct cgroup
*cgrp
;
2502 if (kstrtoint(strstrip(buf
), 0, &pid
) || pid
< 0)
2505 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
2509 percpu_down_write(&cgroup_threadgroup_rwsem
);
2512 tsk
= find_task_by_vpid(pid
);
2515 goto out_unlock_rcu
;
2522 tsk
= tsk
->group_leader
;
2525 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2526 * If userland migrates such a kthread to a non-root cgroup, it can
2527 * become trapped in a cpuset, or RT kthread may be born in a
2528 * cgroup with no rt_runtime allocated. Just say no.
2530 if (tsk
->no_cgroup_migration
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2532 goto out_unlock_rcu
;
2535 get_task_struct(tsk
);
2538 ret
= cgroup_procs_write_permission(tsk
, cgrp
, of
);
2540 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2542 put_task_struct(tsk
);
2543 goto out_unlock_threadgroup
;
2547 out_unlock_threadgroup
:
2548 percpu_up_write(&cgroup_threadgroup_rwsem
);
2549 for_each_subsys(ss
, ssid
)
2550 if (ss
->post_attach
)
2552 cgroup_kn_unlock(of
->kn
);
2553 return ret
?: nbytes
;
2556 ssize_t
cgroup_procs_write(struct kernfs_open_file
*of
, char *buf
, size_t nbytes
,
2559 return __cgroup_procs_write(of
, buf
, nbytes
, off
, true);
2562 static void cgroup_print_ss_mask(struct seq_file
*seq
, u16 ss_mask
)
2564 struct cgroup_subsys
*ss
;
2565 bool printed
= false;
2568 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
2571 seq_printf(seq
, "%s", ss
->name
);
2573 } while_each_subsys_mask();
2575 seq_putc(seq
, '\n');
2578 /* show controllers which are enabled from the parent */
2579 static int cgroup_controllers_show(struct seq_file
*seq
, void *v
)
2581 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2583 cgroup_print_ss_mask(seq
, cgroup_control(cgrp
));
2587 /* show controllers which are enabled for a given cgroup's children */
2588 static int cgroup_subtree_control_show(struct seq_file
*seq
, void *v
)
2590 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2592 cgroup_print_ss_mask(seq
, cgrp
->subtree_control
);
2597 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2598 * @cgrp: root of the subtree to update csses for
2600 * @cgrp's control masks have changed and its subtree's css associations
2601 * need to be updated accordingly. This function looks up all css_sets
2602 * which are attached to the subtree, creates the matching updated css_sets
2603 * and migrates the tasks to the new ones.
2605 static int cgroup_update_dfl_csses(struct cgroup
*cgrp
)
2607 DEFINE_CGROUP_MGCTX(mgctx
);
2608 struct cgroup_subsys_state
*d_css
;
2609 struct cgroup
*dsct
;
2610 struct css_set
*src_cset
;
2613 lockdep_assert_held(&cgroup_mutex
);
2615 percpu_down_write(&cgroup_threadgroup_rwsem
);
2617 /* look up all csses currently attached to @cgrp's subtree */
2618 spin_lock_irq(&css_set_lock
);
2619 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2620 struct cgrp_cset_link
*link
;
2622 list_for_each_entry(link
, &dsct
->cset_links
, cset_link
)
2623 cgroup_migrate_add_src(link
->cset
, dsct
, &mgctx
);
2625 spin_unlock_irq(&css_set_lock
);
2627 /* NULL dst indicates self on default hierarchy */
2628 ret
= cgroup_migrate_prepare_dst(&mgctx
);
2632 spin_lock_irq(&css_set_lock
);
2633 list_for_each_entry(src_cset
, &mgctx
.preloaded_src_csets
, mg_preload_node
) {
2634 struct task_struct
*task
, *ntask
;
2636 /* all tasks in src_csets need to be migrated */
2637 list_for_each_entry_safe(task
, ntask
, &src_cset
->tasks
, cg_list
)
2638 cgroup_migrate_add_task(task
, &mgctx
);
2640 spin_unlock_irq(&css_set_lock
);
2642 ret
= cgroup_migrate_execute(&mgctx
);
2644 cgroup_migrate_finish(&mgctx
);
2645 percpu_up_write(&cgroup_threadgroup_rwsem
);
2650 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
2651 * @cgrp: root of the target subtree
2653 * Because css offlining is asynchronous, userland may try to re-enable a
2654 * controller while the previous css is still around. This function grabs
2655 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
2657 void cgroup_lock_and_drain_offline(struct cgroup
*cgrp
)
2658 __acquires(&cgroup_mutex
)
2660 struct cgroup
*dsct
;
2661 struct cgroup_subsys_state
*d_css
;
2662 struct cgroup_subsys
*ss
;
2666 mutex_lock(&cgroup_mutex
);
2668 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2669 for_each_subsys(ss
, ssid
) {
2670 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2673 if (!css
|| !percpu_ref_is_dying(&css
->refcnt
))
2676 cgroup_get_live(dsct
);
2677 prepare_to_wait(&dsct
->offline_waitq
, &wait
,
2678 TASK_UNINTERRUPTIBLE
);
2680 mutex_unlock(&cgroup_mutex
);
2682 finish_wait(&dsct
->offline_waitq
, &wait
);
2691 * cgroup_save_control - save control masks of a subtree
2692 * @cgrp: root of the target subtree
2694 * Save ->subtree_control and ->subtree_ss_mask to the respective old_
2695 * prefixed fields for @cgrp's subtree including @cgrp itself.
2697 static void cgroup_save_control(struct cgroup
*cgrp
)
2699 struct cgroup
*dsct
;
2700 struct cgroup_subsys_state
*d_css
;
2702 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2703 dsct
->old_subtree_control
= dsct
->subtree_control
;
2704 dsct
->old_subtree_ss_mask
= dsct
->subtree_ss_mask
;
2709 * cgroup_propagate_control - refresh control masks of a subtree
2710 * @cgrp: root of the target subtree
2712 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2713 * ->subtree_control and propagate controller availability through the
2714 * subtree so that descendants don't have unavailable controllers enabled.
2716 static void cgroup_propagate_control(struct cgroup
*cgrp
)
2718 struct cgroup
*dsct
;
2719 struct cgroup_subsys_state
*d_css
;
2721 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2722 dsct
->subtree_control
&= cgroup_control(dsct
);
2723 dsct
->subtree_ss_mask
=
2724 cgroup_calc_subtree_ss_mask(dsct
->subtree_control
,
2725 cgroup_ss_mask(dsct
));
2730 * cgroup_restore_control - restore control masks of a subtree
2731 * @cgrp: root of the target subtree
2733 * Restore ->subtree_control and ->subtree_ss_mask from the respective old_
2734 * prefixed fields for @cgrp's subtree including @cgrp itself.
2736 static void cgroup_restore_control(struct cgroup
*cgrp
)
2738 struct cgroup
*dsct
;
2739 struct cgroup_subsys_state
*d_css
;
2741 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2742 dsct
->subtree_control
= dsct
->old_subtree_control
;
2743 dsct
->subtree_ss_mask
= dsct
->old_subtree_ss_mask
;
2747 static bool css_visible(struct cgroup_subsys_state
*css
)
2749 struct cgroup_subsys
*ss
= css
->ss
;
2750 struct cgroup
*cgrp
= css
->cgroup
;
2752 if (cgroup_control(cgrp
) & (1 << ss
->id
))
2754 if (!(cgroup_ss_mask(cgrp
) & (1 << ss
->id
)))
2756 return cgroup_on_dfl(cgrp
) && ss
->implicit_on_dfl
;
2760 * cgroup_apply_control_enable - enable or show csses according to control
2761 * @cgrp: root of the target subtree
2763 * Walk @cgrp's subtree and create new csses or make the existing ones
2764 * visible. A css is created invisible if it's being implicitly enabled
2765 * through dependency. An invisible css is made visible when the userland
2766 * explicitly enables it.
2768 * Returns 0 on success, -errno on failure. On failure, csses which have
2769 * been processed already aren't cleaned up. The caller is responsible for
2770 * cleaning up with cgroup_apply_control_disable().
2772 static int cgroup_apply_control_enable(struct cgroup
*cgrp
)
2774 struct cgroup
*dsct
;
2775 struct cgroup_subsys_state
*d_css
;
2776 struct cgroup_subsys
*ss
;
2779 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2780 for_each_subsys(ss
, ssid
) {
2781 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2783 WARN_ON_ONCE(css
&& percpu_ref_is_dying(&css
->refcnt
));
2785 if (!(cgroup_ss_mask(dsct
) & (1 << ss
->id
)))
2789 css
= css_create(dsct
, ss
);
2791 return PTR_ERR(css
);
2794 if (css_visible(css
)) {
2795 ret
= css_populate_dir(css
);
2806 * cgroup_apply_control_disable - kill or hide csses according to control
2807 * @cgrp: root of the target subtree
2809 * Walk @cgrp's subtree and kill and hide csses so that they match
2810 * cgroup_ss_mask() and cgroup_visible_mask().
2812 * A css is hidden when the userland requests it to be disabled while other
2813 * subsystems are still depending on it. The css must not actively control
2814 * resources and be in the vanilla state if it's made visible again later.
2815 * Controllers which may be depended upon should provide ->css_reset() for
2818 static void cgroup_apply_control_disable(struct cgroup
*cgrp
)
2820 struct cgroup
*dsct
;
2821 struct cgroup_subsys_state
*d_css
;
2822 struct cgroup_subsys
*ss
;
2825 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2826 for_each_subsys(ss
, ssid
) {
2827 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2829 WARN_ON_ONCE(css
&& percpu_ref_is_dying(&css
->refcnt
));
2835 !(cgroup_ss_mask(dsct
) & (1 << ss
->id
))) {
2837 } else if (!css_visible(css
)) {
2847 * cgroup_apply_control - apply control mask updates to the subtree
2848 * @cgrp: root of the target subtree
2850 * subsystems can be enabled and disabled in a subtree using the following
2853 * 1. Call cgroup_save_control() to stash the current state.
2854 * 2. Update ->subtree_control masks in the subtree as desired.
2855 * 3. Call cgroup_apply_control() to apply the changes.
2856 * 4. Optionally perform other related operations.
2857 * 5. Call cgroup_finalize_control() to finish up.
2859 * This function implements step 3 and propagates the mask changes
2860 * throughout @cgrp's subtree, updates csses accordingly and perform
2861 * process migrations.
2863 static int cgroup_apply_control(struct cgroup
*cgrp
)
2867 cgroup_propagate_control(cgrp
);
2869 ret
= cgroup_apply_control_enable(cgrp
);
2874 * At this point, cgroup_e_css() results reflect the new csses
2875 * making the following cgroup_update_dfl_csses() properly update
2876 * css associations of all tasks in the subtree.
2878 ret
= cgroup_update_dfl_csses(cgrp
);
2886 * cgroup_finalize_control - finalize control mask update
2887 * @cgrp: root of the target subtree
2888 * @ret: the result of the update
2890 * Finalize control mask update. See cgroup_apply_control() for more info.
2892 static void cgroup_finalize_control(struct cgroup
*cgrp
, int ret
)
2895 cgroup_restore_control(cgrp
);
2896 cgroup_propagate_control(cgrp
);
2899 cgroup_apply_control_disable(cgrp
);
2902 /* change the enabled child controllers for a cgroup in the default hierarchy */
2903 static ssize_t
cgroup_subtree_control_write(struct kernfs_open_file
*of
,
2904 char *buf
, size_t nbytes
,
2907 u16 enable
= 0, disable
= 0;
2908 struct cgroup
*cgrp
, *child
;
2909 struct cgroup_subsys
*ss
;
2914 * Parse input - space separated list of subsystem names prefixed
2915 * with either + or -.
2917 buf
= strstrip(buf
);
2918 while ((tok
= strsep(&buf
, " "))) {
2921 do_each_subsys_mask(ss
, ssid
, ~cgrp_dfl_inhibit_ss_mask
) {
2922 if (!cgroup_ssid_enabled(ssid
) ||
2923 strcmp(tok
+ 1, ss
->name
))
2927 enable
|= 1 << ssid
;
2928 disable
&= ~(1 << ssid
);
2929 } else if (*tok
== '-') {
2930 disable
|= 1 << ssid
;
2931 enable
&= ~(1 << ssid
);
2936 } while_each_subsys_mask();
2937 if (ssid
== CGROUP_SUBSYS_COUNT
)
2941 cgrp
= cgroup_kn_lock_live(of
->kn
, true);
2945 for_each_subsys(ss
, ssid
) {
2946 if (enable
& (1 << ssid
)) {
2947 if (cgrp
->subtree_control
& (1 << ssid
)) {
2948 enable
&= ~(1 << ssid
);
2952 if (!(cgroup_control(cgrp
) & (1 << ssid
))) {
2956 } else if (disable
& (1 << ssid
)) {
2957 if (!(cgrp
->subtree_control
& (1 << ssid
))) {
2958 disable
&= ~(1 << ssid
);
2962 /* a child has it enabled? */
2963 cgroup_for_each_live_child(child
, cgrp
) {
2964 if (child
->subtree_control
& (1 << ssid
)) {
2972 if (!enable
&& !disable
) {
2978 * Except for the root, subtree_control must be zero for a cgroup
2979 * with tasks so that child cgroups don't compete against tasks.
2981 if (enable
&& cgroup_parent(cgrp
)) {
2982 struct cgrp_cset_link
*link
;
2985 * Because namespaces pin csets too, @cgrp->cset_links
2986 * might not be empty even when @cgrp is empty. Walk and
2989 spin_lock_irq(&css_set_lock
);
2992 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
) {
2993 if (css_set_populated(link
->cset
)) {
2999 spin_unlock_irq(&css_set_lock
);
3005 /* save and update control masks and prepare csses */
3006 cgroup_save_control(cgrp
);
3008 cgrp
->subtree_control
|= enable
;
3009 cgrp
->subtree_control
&= ~disable
;
3011 ret
= cgroup_apply_control(cgrp
);
3012 cgroup_finalize_control(cgrp
, ret
);
3016 kernfs_activate(cgrp
->kn
);
3018 cgroup_kn_unlock(of
->kn
);
3019 return ret
?: nbytes
;
3022 static int cgroup_events_show(struct seq_file
*seq
, void *v
)
3024 seq_printf(seq
, "populated %d\n",
3025 cgroup_is_populated(seq_css(seq
)->cgroup
));
3029 static int cgroup_file_open(struct kernfs_open_file
*of
)
3031 struct cftype
*cft
= of
->kn
->priv
;
3034 return cft
->open(of
);
3038 static void cgroup_file_release(struct kernfs_open_file
*of
)
3040 struct cftype
*cft
= of
->kn
->priv
;
3046 static ssize_t
cgroup_file_write(struct kernfs_open_file
*of
, char *buf
,
3047 size_t nbytes
, loff_t off
)
3049 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
3050 struct cgroup
*cgrp
= of
->kn
->parent
->priv
;
3051 struct cftype
*cft
= of
->kn
->priv
;
3052 struct cgroup_subsys_state
*css
;
3056 * If namespaces are delegation boundaries, disallow writes to
3057 * files in an non-init namespace root from inside the namespace
3058 * except for the files explicitly marked delegatable -
3059 * cgroup.procs and cgroup.subtree_control.
3061 if ((cgrp
->root
->flags
& CGRP_ROOT_NS_DELEGATE
) &&
3062 !(cft
->flags
& CFTYPE_NS_DELEGATABLE
) &&
3063 ns
!= &init_cgroup_ns
&& ns
->root_cset
->dfl_cgrp
== cgrp
)
3067 return cft
->write(of
, buf
, nbytes
, off
);
3070 * kernfs guarantees that a file isn't deleted with operations in
3071 * flight, which means that the matching css is and stays alive and
3072 * doesn't need to be pinned. The RCU locking is not necessary
3073 * either. It's just for the convenience of using cgroup_css().
3076 css
= cgroup_css(cgrp
, cft
->ss
);
3079 if (cft
->write_u64
) {
3080 unsigned long long v
;
3081 ret
= kstrtoull(buf
, 0, &v
);
3083 ret
= cft
->write_u64(css
, cft
, v
);
3084 } else if (cft
->write_s64
) {
3086 ret
= kstrtoll(buf
, 0, &v
);
3088 ret
= cft
->write_s64(css
, cft
, v
);
3093 return ret
?: nbytes
;
3096 static void *cgroup_seqfile_start(struct seq_file
*seq
, loff_t
*ppos
)
3098 return seq_cft(seq
)->seq_start(seq
, ppos
);
3101 static void *cgroup_seqfile_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
3103 return seq_cft(seq
)->seq_next(seq
, v
, ppos
);
3106 static void cgroup_seqfile_stop(struct seq_file
*seq
, void *v
)
3108 if (seq_cft(seq
)->seq_stop
)
3109 seq_cft(seq
)->seq_stop(seq
, v
);
3112 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
3114 struct cftype
*cft
= seq_cft(m
);
3115 struct cgroup_subsys_state
*css
= seq_css(m
);
3118 return cft
->seq_show(m
, arg
);
3121 seq_printf(m
, "%llu\n", cft
->read_u64(css
, cft
));
3122 else if (cft
->read_s64
)
3123 seq_printf(m
, "%lld\n", cft
->read_s64(css
, cft
));
3129 static struct kernfs_ops cgroup_kf_single_ops
= {
3130 .atomic_write_len
= PAGE_SIZE
,
3131 .open
= cgroup_file_open
,
3132 .release
= cgroup_file_release
,
3133 .write
= cgroup_file_write
,
3134 .seq_show
= cgroup_seqfile_show
,
3137 static struct kernfs_ops cgroup_kf_ops
= {
3138 .atomic_write_len
= PAGE_SIZE
,
3139 .open
= cgroup_file_open
,
3140 .release
= cgroup_file_release
,
3141 .write
= cgroup_file_write
,
3142 .seq_start
= cgroup_seqfile_start
,
3143 .seq_next
= cgroup_seqfile_next
,
3144 .seq_stop
= cgroup_seqfile_stop
,
3145 .seq_show
= cgroup_seqfile_show
,
3148 /* set uid and gid of cgroup dirs and files to that of the creator */
3149 static int cgroup_kn_set_ugid(struct kernfs_node
*kn
)
3151 struct iattr iattr
= { .ia_valid
= ATTR_UID
| ATTR_GID
,
3152 .ia_uid
= current_fsuid(),
3153 .ia_gid
= current_fsgid(), };
3155 if (uid_eq(iattr
.ia_uid
, GLOBAL_ROOT_UID
) &&
3156 gid_eq(iattr
.ia_gid
, GLOBAL_ROOT_GID
))
3159 return kernfs_setattr(kn
, &iattr
);
3162 static int cgroup_add_file(struct cgroup_subsys_state
*css
, struct cgroup
*cgrp
,
3165 char name
[CGROUP_FILE_NAME_MAX
];
3166 struct kernfs_node
*kn
;
3167 struct lock_class_key
*key
= NULL
;
3170 #ifdef CONFIG_DEBUG_LOCK_ALLOC
3171 key
= &cft
->lockdep_key
;
3173 kn
= __kernfs_create_file(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
),
3174 cgroup_file_mode(cft
), 0, cft
->kf_ops
, cft
,
3179 ret
= cgroup_kn_set_ugid(kn
);
3185 if (cft
->file_offset
) {
3186 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
3188 spin_lock_irq(&cgroup_file_kn_lock
);
3190 spin_unlock_irq(&cgroup_file_kn_lock
);
3197 * cgroup_addrm_files - add or remove files to a cgroup directory
3198 * @css: the target css
3199 * @cgrp: the target cgroup (usually css->cgroup)
3200 * @cfts: array of cftypes to be added
3201 * @is_add: whether to add or remove
3203 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
3204 * For removals, this function never fails.
3206 static int cgroup_addrm_files(struct cgroup_subsys_state
*css
,
3207 struct cgroup
*cgrp
, struct cftype cfts
[],
3210 struct cftype
*cft
, *cft_end
= NULL
;
3213 lockdep_assert_held(&cgroup_mutex
);
3216 for (cft
= cfts
; cft
!= cft_end
&& cft
->name
[0] != '\0'; cft
++) {
3217 /* does cft->flags tell us to skip this file on @cgrp? */
3218 if ((cft
->flags
& __CFTYPE_ONLY_ON_DFL
) && !cgroup_on_dfl(cgrp
))
3220 if ((cft
->flags
& __CFTYPE_NOT_ON_DFL
) && cgroup_on_dfl(cgrp
))
3222 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgroup_parent(cgrp
))
3224 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgroup_parent(cgrp
))
3228 ret
= cgroup_add_file(css
, cgrp
, cft
);
3230 pr_warn("%s: failed to add %s, err=%d\n",
3231 __func__
, cft
->name
, ret
);
3237 cgroup_rm_file(cgrp
, cft
);
3243 static int cgroup_apply_cftypes(struct cftype
*cfts
, bool is_add
)
3246 struct cgroup_subsys
*ss
= cfts
[0].ss
;
3247 struct cgroup
*root
= &ss
->root
->cgrp
;
3248 struct cgroup_subsys_state
*css
;
3251 lockdep_assert_held(&cgroup_mutex
);
3253 /* add/rm files for all cgroups created before */
3254 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
)) {
3255 struct cgroup
*cgrp
= css
->cgroup
;
3257 if (!(css
->flags
& CSS_VISIBLE
))
3260 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, is_add
);
3266 kernfs_activate(root
->kn
);
3270 static void cgroup_exit_cftypes(struct cftype
*cfts
)
3274 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3275 /* free copy for custom atomic_write_len, see init_cftypes() */
3276 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
)
3281 /* revert flags set by cgroup core while adding @cfts */
3282 cft
->flags
&= ~(__CFTYPE_ONLY_ON_DFL
| __CFTYPE_NOT_ON_DFL
);
3286 static int cgroup_init_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3290 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3291 struct kernfs_ops
*kf_ops
;
3293 WARN_ON(cft
->ss
|| cft
->kf_ops
);
3296 kf_ops
= &cgroup_kf_ops
;
3298 kf_ops
= &cgroup_kf_single_ops
;
3301 * Ugh... if @cft wants a custom max_write_len, we need to
3302 * make a copy of kf_ops to set its atomic_write_len.
3304 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
) {
3305 kf_ops
= kmemdup(kf_ops
, sizeof(*kf_ops
), GFP_KERNEL
);
3307 cgroup_exit_cftypes(cfts
);
3310 kf_ops
->atomic_write_len
= cft
->max_write_len
;
3313 cft
->kf_ops
= kf_ops
;
3320 static int cgroup_rm_cftypes_locked(struct cftype
*cfts
)
3322 lockdep_assert_held(&cgroup_mutex
);
3324 if (!cfts
|| !cfts
[0].ss
)
3327 list_del(&cfts
->node
);
3328 cgroup_apply_cftypes(cfts
, false);
3329 cgroup_exit_cftypes(cfts
);
3334 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3335 * @cfts: zero-length name terminated array of cftypes
3337 * Unregister @cfts. Files described by @cfts are removed from all
3338 * existing cgroups and all future cgroups won't have them either. This
3339 * function can be called anytime whether @cfts' subsys is attached or not.
3341 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3344 int cgroup_rm_cftypes(struct cftype
*cfts
)
3348 mutex_lock(&cgroup_mutex
);
3349 ret
= cgroup_rm_cftypes_locked(cfts
);
3350 mutex_unlock(&cgroup_mutex
);
3355 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3356 * @ss: target cgroup subsystem
3357 * @cfts: zero-length name terminated array of cftypes
3359 * Register @cfts to @ss. Files described by @cfts are created for all
3360 * existing cgroups to which @ss is attached and all future cgroups will
3361 * have them too. This function can be called anytime whether @ss is
3364 * Returns 0 on successful registration, -errno on failure. Note that this
3365 * function currently returns 0 as long as @cfts registration is successful
3366 * even if some file creation attempts on existing cgroups fail.
3368 static int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3372 if (!cgroup_ssid_enabled(ss
->id
))
3375 if (!cfts
|| cfts
[0].name
[0] == '\0')
3378 ret
= cgroup_init_cftypes(ss
, cfts
);
3382 mutex_lock(&cgroup_mutex
);
3384 list_add_tail(&cfts
->node
, &ss
->cfts
);
3385 ret
= cgroup_apply_cftypes(cfts
, true);
3387 cgroup_rm_cftypes_locked(cfts
);
3389 mutex_unlock(&cgroup_mutex
);
3394 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3395 * @ss: target cgroup subsystem
3396 * @cfts: zero-length name terminated array of cftypes
3398 * Similar to cgroup_add_cftypes() but the added files are only used for
3399 * the default hierarchy.
3401 int cgroup_add_dfl_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3405 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3406 cft
->flags
|= __CFTYPE_ONLY_ON_DFL
;
3407 return cgroup_add_cftypes(ss
, cfts
);
3411 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3412 * @ss: target cgroup subsystem
3413 * @cfts: zero-length name terminated array of cftypes
3415 * Similar to cgroup_add_cftypes() but the added files are only used for
3416 * the legacy hierarchies.
3418 int cgroup_add_legacy_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3422 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3423 cft
->flags
|= __CFTYPE_NOT_ON_DFL
;
3424 return cgroup_add_cftypes(ss
, cfts
);
3428 * cgroup_file_notify - generate a file modified event for a cgroup_file
3429 * @cfile: target cgroup_file
3431 * @cfile must have been obtained by setting cftype->file_offset.
3433 void cgroup_file_notify(struct cgroup_file
*cfile
)
3435 unsigned long flags
;
3437 spin_lock_irqsave(&cgroup_file_kn_lock
, flags
);
3439 kernfs_notify(cfile
->kn
);
3440 spin_unlock_irqrestore(&cgroup_file_kn_lock
, flags
);
3444 * css_next_child - find the next child of a given css
3445 * @pos: the current position (%NULL to initiate traversal)
3446 * @parent: css whose children to walk
3448 * This function returns the next child of @parent and should be called
3449 * under either cgroup_mutex or RCU read lock. The only requirement is
3450 * that @parent and @pos are accessible. The next sibling is guaranteed to
3451 * be returned regardless of their states.
3453 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3454 * css which finished ->css_online() is guaranteed to be visible in the
3455 * future iterations and will stay visible until the last reference is put.
3456 * A css which hasn't finished ->css_online() or already finished
3457 * ->css_offline() may show up during traversal. It's each subsystem's
3458 * responsibility to synchronize against on/offlining.
3460 struct cgroup_subsys_state
*css_next_child(struct cgroup_subsys_state
*pos
,
3461 struct cgroup_subsys_state
*parent
)
3463 struct cgroup_subsys_state
*next
;
3465 cgroup_assert_mutex_or_rcu_locked();
3468 * @pos could already have been unlinked from the sibling list.
3469 * Once a cgroup is removed, its ->sibling.next is no longer
3470 * updated when its next sibling changes. CSS_RELEASED is set when
3471 * @pos is taken off list, at which time its next pointer is valid,
3472 * and, as releases are serialized, the one pointed to by the next
3473 * pointer is guaranteed to not have started release yet. This
3474 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3475 * critical section, the one pointed to by its next pointer is
3476 * guaranteed to not have finished its RCU grace period even if we
3477 * have dropped rcu_read_lock() inbetween iterations.
3479 * If @pos has CSS_RELEASED set, its next pointer can't be
3480 * dereferenced; however, as each css is given a monotonically
3481 * increasing unique serial number and always appended to the
3482 * sibling list, the next one can be found by walking the parent's
3483 * children until the first css with higher serial number than
3484 * @pos's. While this path can be slower, it happens iff iteration
3485 * races against release and the race window is very small.
3488 next
= list_entry_rcu(parent
->children
.next
, struct cgroup_subsys_state
, sibling
);
3489 } else if (likely(!(pos
->flags
& CSS_RELEASED
))) {
3490 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup_subsys_state
, sibling
);
3492 list_for_each_entry_rcu(next
, &parent
->children
, sibling
)
3493 if (next
->serial_nr
> pos
->serial_nr
)
3498 * @next, if not pointing to the head, can be dereferenced and is
3501 if (&next
->sibling
!= &parent
->children
)
3507 * css_next_descendant_pre - find the next descendant for pre-order walk
3508 * @pos: the current position (%NULL to initiate traversal)
3509 * @root: css whose descendants to walk
3511 * To be used by css_for_each_descendant_pre(). Find the next descendant
3512 * to visit for pre-order traversal of @root's descendants. @root is
3513 * included in the iteration and the first node to be visited.
3515 * While this function requires cgroup_mutex or RCU read locking, it
3516 * doesn't require the whole traversal to be contained in a single critical
3517 * section. This function will return the correct next descendant as long
3518 * as both @pos and @root are accessible and @pos is a descendant of @root.
3520 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3521 * css which finished ->css_online() is guaranteed to be visible in the
3522 * future iterations and will stay visible until the last reference is put.
3523 * A css which hasn't finished ->css_online() or already finished
3524 * ->css_offline() may show up during traversal. It's each subsystem's
3525 * responsibility to synchronize against on/offlining.
3527 struct cgroup_subsys_state
*
3528 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
3529 struct cgroup_subsys_state
*root
)
3531 struct cgroup_subsys_state
*next
;
3533 cgroup_assert_mutex_or_rcu_locked();
3535 /* if first iteration, visit @root */
3539 /* visit the first child if exists */
3540 next
= css_next_child(NULL
, pos
);
3544 /* no child, visit my or the closest ancestor's next sibling */
3545 while (pos
!= root
) {
3546 next
= css_next_child(pos
, pos
->parent
);
3556 * css_rightmost_descendant - return the rightmost descendant of a css
3557 * @pos: css of interest
3559 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3560 * is returned. This can be used during pre-order traversal to skip
3563 * While this function requires cgroup_mutex or RCU read locking, it
3564 * doesn't require the whole traversal to be contained in a single critical
3565 * section. This function will return the correct rightmost descendant as
3566 * long as @pos is accessible.
3568 struct cgroup_subsys_state
*
3569 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
3571 struct cgroup_subsys_state
*last
, *tmp
;
3573 cgroup_assert_mutex_or_rcu_locked();
3577 /* ->prev isn't RCU safe, walk ->next till the end */
3579 css_for_each_child(tmp
, last
)
3586 static struct cgroup_subsys_state
*
3587 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
3589 struct cgroup_subsys_state
*last
;
3593 pos
= css_next_child(NULL
, pos
);
3600 * css_next_descendant_post - find the next descendant for post-order walk
3601 * @pos: the current position (%NULL to initiate traversal)
3602 * @root: css whose descendants to walk
3604 * To be used by css_for_each_descendant_post(). Find the next descendant
3605 * to visit for post-order traversal of @root's descendants. @root is
3606 * included in the iteration and the last node to be visited.
3608 * While this function requires cgroup_mutex or RCU read locking, it
3609 * doesn't require the whole traversal to be contained in a single critical
3610 * section. This function will return the correct next descendant as long
3611 * as both @pos and @cgroup are accessible and @pos is a descendant of
3614 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3615 * css which finished ->css_online() is guaranteed to be visible in the
3616 * future iterations and will stay visible until the last reference is put.
3617 * A css which hasn't finished ->css_online() or already finished
3618 * ->css_offline() may show up during traversal. It's each subsystem's
3619 * responsibility to synchronize against on/offlining.
3621 struct cgroup_subsys_state
*
3622 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
3623 struct cgroup_subsys_state
*root
)
3625 struct cgroup_subsys_state
*next
;
3627 cgroup_assert_mutex_or_rcu_locked();
3629 /* if first iteration, visit leftmost descendant which may be @root */
3631 return css_leftmost_descendant(root
);
3633 /* if we visited @root, we're done */
3637 /* if there's an unvisited sibling, visit its leftmost descendant */
3638 next
= css_next_child(pos
, pos
->parent
);
3640 return css_leftmost_descendant(next
);
3642 /* no sibling left, visit parent */
3647 * css_has_online_children - does a css have online children
3648 * @css: the target css
3650 * Returns %true if @css has any online children; otherwise, %false. This
3651 * function can be called from any context but the caller is responsible
3652 * for synchronizing against on/offlining as necessary.
3654 bool css_has_online_children(struct cgroup_subsys_state
*css
)
3656 struct cgroup_subsys_state
*child
;
3660 css_for_each_child(child
, css
) {
3661 if (child
->flags
& CSS_ONLINE
) {
3671 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
3672 * @it: the iterator to advance
3674 * Advance @it to the next css_set to walk.
3676 static void css_task_iter_advance_css_set(struct css_task_iter
*it
)
3678 struct list_head
*l
= it
->cset_pos
;
3679 struct cgrp_cset_link
*link
;
3680 struct css_set
*cset
;
3682 lockdep_assert_held(&css_set_lock
);
3684 /* Advance to the next non-empty css_set */
3687 if (l
== it
->cset_head
) {
3688 it
->cset_pos
= NULL
;
3689 it
->task_pos
= NULL
;
3694 cset
= container_of(l
, struct css_set
,
3695 e_cset_node
[it
->ss
->id
]);
3697 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
3700 } while (!css_set_populated(cset
));
3704 if (!list_empty(&cset
->tasks
))
3705 it
->task_pos
= cset
->tasks
.next
;
3707 it
->task_pos
= cset
->mg_tasks
.next
;
3709 it
->tasks_head
= &cset
->tasks
;
3710 it
->mg_tasks_head
= &cset
->mg_tasks
;
3713 * We don't keep css_sets locked across iteration steps and thus
3714 * need to take steps to ensure that iteration can be resumed after
3715 * the lock is re-acquired. Iteration is performed at two levels -
3716 * css_sets and tasks in them.
3718 * Once created, a css_set never leaves its cgroup lists, so a
3719 * pinned css_set is guaranteed to stay put and we can resume
3720 * iteration afterwards.
3722 * Tasks may leave @cset across iteration steps. This is resolved
3723 * by registering each iterator with the css_set currently being
3724 * walked and making css_set_move_task() advance iterators whose
3725 * next task is leaving.
3728 list_del(&it
->iters_node
);
3729 put_css_set_locked(it
->cur_cset
);
3732 it
->cur_cset
= cset
;
3733 list_add(&it
->iters_node
, &cset
->task_iters
);
3736 static void css_task_iter_advance(struct css_task_iter
*it
)
3738 struct list_head
*l
= it
->task_pos
;
3740 lockdep_assert_held(&css_set_lock
);
3744 * Advance iterator to find next entry. cset->tasks is consumed
3745 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3750 if (l
== it
->tasks_head
)
3751 l
= it
->mg_tasks_head
->next
;
3753 if (l
== it
->mg_tasks_head
)
3754 css_task_iter_advance_css_set(it
);
3760 * css_task_iter_start - initiate task iteration
3761 * @css: the css to walk tasks of
3762 * @it: the task iterator to use
3764 * Initiate iteration through the tasks of @css. The caller can call
3765 * css_task_iter_next() to walk through the tasks until the function
3766 * returns NULL. On completion of iteration, css_task_iter_end() must be
3769 void css_task_iter_start(struct cgroup_subsys_state
*css
,
3770 struct css_task_iter
*it
)
3772 /* no one should try to iterate before mounting cgroups */
3773 WARN_ON_ONCE(!use_task_css_set_links
);
3775 memset(it
, 0, sizeof(*it
));
3777 spin_lock_irq(&css_set_lock
);
3782 it
->cset_pos
= &css
->cgroup
->e_csets
[css
->ss
->id
];
3784 it
->cset_pos
= &css
->cgroup
->cset_links
;
3786 it
->cset_head
= it
->cset_pos
;
3788 css_task_iter_advance_css_set(it
);
3790 spin_unlock_irq(&css_set_lock
);
3794 * css_task_iter_next - return the next task for the iterator
3795 * @it: the task iterator being iterated
3797 * The "next" function for task iteration. @it should have been
3798 * initialized via css_task_iter_start(). Returns NULL when the iteration
3801 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
3804 put_task_struct(it
->cur_task
);
3805 it
->cur_task
= NULL
;
3808 spin_lock_irq(&css_set_lock
);
3811 it
->cur_task
= list_entry(it
->task_pos
, struct task_struct
,
3813 get_task_struct(it
->cur_task
);
3814 css_task_iter_advance(it
);
3817 spin_unlock_irq(&css_set_lock
);
3819 return it
->cur_task
;
3823 * css_task_iter_end - finish task iteration
3824 * @it: the task iterator to finish
3826 * Finish task iteration started by css_task_iter_start().
3828 void css_task_iter_end(struct css_task_iter
*it
)
3831 spin_lock_irq(&css_set_lock
);
3832 list_del(&it
->iters_node
);
3833 put_css_set_locked(it
->cur_cset
);
3834 spin_unlock_irq(&css_set_lock
);
3838 put_task_struct(it
->cur_task
);
3841 static void cgroup_procs_release(struct kernfs_open_file
*of
)
3844 css_task_iter_end(of
->priv
);
3849 static void *cgroup_procs_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3851 struct kernfs_open_file
*of
= s
->private;
3852 struct css_task_iter
*it
= of
->priv
;
3853 struct task_struct
*task
;
3856 task
= css_task_iter_next(it
);
3857 } while (task
&& !thread_group_leader(task
));
3862 static void *cgroup_procs_start(struct seq_file
*s
, loff_t
*pos
)
3864 struct kernfs_open_file
*of
= s
->private;
3865 struct cgroup
*cgrp
= seq_css(s
)->cgroup
;
3866 struct css_task_iter
*it
= of
->priv
;
3869 * When a seq_file is seeked, it's always traversed sequentially
3870 * from position 0, so we can simply keep iterating on !0 *pos.
3873 if (WARN_ON_ONCE((*pos
)++))
3874 return ERR_PTR(-EINVAL
);
3876 it
= kzalloc(sizeof(*it
), GFP_KERNEL
);
3878 return ERR_PTR(-ENOMEM
);
3880 css_task_iter_start(&cgrp
->self
, it
);
3881 } else if (!(*pos
)++) {
3882 css_task_iter_end(it
);
3883 css_task_iter_start(&cgrp
->self
, it
);
3886 return cgroup_procs_next(s
, NULL
, NULL
);
3889 static int cgroup_procs_show(struct seq_file
*s
, void *v
)
3891 seq_printf(s
, "%d\n", task_tgid_vnr(v
));
3895 /* cgroup core interface files for the default hierarchy */
3896 static struct cftype cgroup_base_files
[] = {
3898 .name
= "cgroup.procs",
3899 .flags
= CFTYPE_NS_DELEGATABLE
,
3900 .file_offset
= offsetof(struct cgroup
, procs_file
),
3901 .release
= cgroup_procs_release
,
3902 .seq_start
= cgroup_procs_start
,
3903 .seq_next
= cgroup_procs_next
,
3904 .seq_show
= cgroup_procs_show
,
3905 .write
= cgroup_procs_write
,
3908 .name
= "cgroup.controllers",
3909 .seq_show
= cgroup_controllers_show
,
3912 .name
= "cgroup.subtree_control",
3913 .flags
= CFTYPE_NS_DELEGATABLE
,
3914 .seq_show
= cgroup_subtree_control_show
,
3915 .write
= cgroup_subtree_control_write
,
3918 .name
= "cgroup.events",
3919 .flags
= CFTYPE_NOT_ON_ROOT
,
3920 .file_offset
= offsetof(struct cgroup
, events_file
),
3921 .seq_show
= cgroup_events_show
,
3927 * css destruction is four-stage process.
3929 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3930 * Implemented in kill_css().
3932 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3933 * and thus css_tryget_online() is guaranteed to fail, the css can be
3934 * offlined by invoking offline_css(). After offlining, the base ref is
3935 * put. Implemented in css_killed_work_fn().
3937 * 3. When the percpu_ref reaches zero, the only possible remaining
3938 * accessors are inside RCU read sections. css_release() schedules the
3941 * 4. After the grace period, the css can be freed. Implemented in
3942 * css_free_work_fn().
3944 * It is actually hairier because both step 2 and 4 require process context
3945 * and thus involve punting to css->destroy_work adding two additional
3946 * steps to the already complex sequence.
3948 static void css_free_work_fn(struct work_struct
*work
)
3950 struct cgroup_subsys_state
*css
=
3951 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
3952 struct cgroup_subsys
*ss
= css
->ss
;
3953 struct cgroup
*cgrp
= css
->cgroup
;
3955 percpu_ref_exit(&css
->refcnt
);
3959 struct cgroup_subsys_state
*parent
= css
->parent
;
3963 cgroup_idr_remove(&ss
->css_idr
, id
);
3969 /* cgroup free path */
3970 atomic_dec(&cgrp
->root
->nr_cgrps
);
3971 cgroup1_pidlist_destroy_all(cgrp
);
3972 cancel_work_sync(&cgrp
->release_agent_work
);
3974 if (cgroup_parent(cgrp
)) {
3976 * We get a ref to the parent, and put the ref when
3977 * this cgroup is being freed, so it's guaranteed
3978 * that the parent won't be destroyed before its
3981 cgroup_put(cgroup_parent(cgrp
));
3982 kernfs_put(cgrp
->kn
);
3986 * This is root cgroup's refcnt reaching zero,
3987 * which indicates that the root should be
3990 cgroup_destroy_root(cgrp
->root
);
3995 static void css_free_rcu_fn(struct rcu_head
*rcu_head
)
3997 struct cgroup_subsys_state
*css
=
3998 container_of(rcu_head
, struct cgroup_subsys_state
, rcu_head
);
4000 INIT_WORK(&css
->destroy_work
, css_free_work_fn
);
4001 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4004 static void css_release_work_fn(struct work_struct
*work
)
4006 struct cgroup_subsys_state
*css
=
4007 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4008 struct cgroup_subsys
*ss
= css
->ss
;
4009 struct cgroup
*cgrp
= css
->cgroup
;
4011 mutex_lock(&cgroup_mutex
);
4013 css
->flags
|= CSS_RELEASED
;
4014 list_del_rcu(&css
->sibling
);
4017 /* css release path */
4018 cgroup_idr_replace(&ss
->css_idr
, NULL
, css
->id
);
4019 if (ss
->css_released
)
4020 ss
->css_released(css
);
4022 /* cgroup release path */
4023 trace_cgroup_release(cgrp
);
4025 cgroup_idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
4029 * There are two control paths which try to determine
4030 * cgroup from dentry without going through kernfs -
4031 * cgroupstats_build() and css_tryget_online_from_dir().
4032 * Those are supported by RCU protecting clearing of
4033 * cgrp->kn->priv backpointer.
4036 RCU_INIT_POINTER(*(void __rcu __force
**)&cgrp
->kn
->priv
,
4039 cgroup_bpf_put(cgrp
);
4042 mutex_unlock(&cgroup_mutex
);
4044 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4047 static void css_release(struct percpu_ref
*ref
)
4049 struct cgroup_subsys_state
*css
=
4050 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4052 INIT_WORK(&css
->destroy_work
, css_release_work_fn
);
4053 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4056 static void init_and_link_css(struct cgroup_subsys_state
*css
,
4057 struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4059 lockdep_assert_held(&cgroup_mutex
);
4061 cgroup_get_live(cgrp
);
4063 memset(css
, 0, sizeof(*css
));
4067 INIT_LIST_HEAD(&css
->sibling
);
4068 INIT_LIST_HEAD(&css
->children
);
4069 css
->serial_nr
= css_serial_nr_next
++;
4070 atomic_set(&css
->online_cnt
, 0);
4072 if (cgroup_parent(cgrp
)) {
4073 css
->parent
= cgroup_css(cgroup_parent(cgrp
), ss
);
4074 css_get(css
->parent
);
4077 BUG_ON(cgroup_css(cgrp
, ss
));
4080 /* invoke ->css_online() on a new CSS and mark it online if successful */
4081 static int online_css(struct cgroup_subsys_state
*css
)
4083 struct cgroup_subsys
*ss
= css
->ss
;
4086 lockdep_assert_held(&cgroup_mutex
);
4089 ret
= ss
->css_online(css
);
4091 css
->flags
|= CSS_ONLINE
;
4092 rcu_assign_pointer(css
->cgroup
->subsys
[ss
->id
], css
);
4094 atomic_inc(&css
->online_cnt
);
4096 atomic_inc(&css
->parent
->online_cnt
);
4101 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4102 static void offline_css(struct cgroup_subsys_state
*css
)
4104 struct cgroup_subsys
*ss
= css
->ss
;
4106 lockdep_assert_held(&cgroup_mutex
);
4108 if (!(css
->flags
& CSS_ONLINE
))
4114 if (ss
->css_offline
)
4115 ss
->css_offline(css
);
4117 css
->flags
&= ~CSS_ONLINE
;
4118 RCU_INIT_POINTER(css
->cgroup
->subsys
[ss
->id
], NULL
);
4120 wake_up_all(&css
->cgroup
->offline_waitq
);
4124 * css_create - create a cgroup_subsys_state
4125 * @cgrp: the cgroup new css will be associated with
4126 * @ss: the subsys of new css
4128 * Create a new css associated with @cgrp - @ss pair. On success, the new
4129 * css is online and installed in @cgrp. This function doesn't create the
4130 * interface files. Returns 0 on success, -errno on failure.
4132 static struct cgroup_subsys_state
*css_create(struct cgroup
*cgrp
,
4133 struct cgroup_subsys
*ss
)
4135 struct cgroup
*parent
= cgroup_parent(cgrp
);
4136 struct cgroup_subsys_state
*parent_css
= cgroup_css(parent
, ss
);
4137 struct cgroup_subsys_state
*css
;
4140 lockdep_assert_held(&cgroup_mutex
);
4142 css
= ss
->css_alloc(parent_css
);
4144 css
= ERR_PTR(-ENOMEM
);
4148 init_and_link_css(css
, ss
, cgrp
);
4150 err
= percpu_ref_init(&css
->refcnt
, css_release
, 0, GFP_KERNEL
);
4154 err
= cgroup_idr_alloc(&ss
->css_idr
, NULL
, 2, 0, GFP_KERNEL
);
4159 /* @css is ready to be brought online now, make it visible */
4160 list_add_tail_rcu(&css
->sibling
, &parent_css
->children
);
4161 cgroup_idr_replace(&ss
->css_idr
, css
, css
->id
);
4163 err
= online_css(css
);
4167 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4168 cgroup_parent(parent
)) {
4169 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4170 current
->comm
, current
->pid
, ss
->name
);
4171 if (!strcmp(ss
->name
, "memory"))
4172 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
4173 ss
->warned_broken_hierarchy
= true;
4179 list_del_rcu(&css
->sibling
);
4181 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4182 return ERR_PTR(err
);
4186 * The returned cgroup is fully initialized including its control mask, but
4187 * it isn't associated with its kernfs_node and doesn't have the control
4190 static struct cgroup
*cgroup_create(struct cgroup
*parent
)
4192 struct cgroup_root
*root
= parent
->root
;
4193 struct cgroup
*cgrp
, *tcgrp
;
4194 int level
= parent
->level
+ 1;
4197 /* allocate the cgroup and its ID, 0 is reserved for the root */
4198 cgrp
= kzalloc(sizeof(*cgrp
) +
4199 sizeof(cgrp
->ancestor_ids
[0]) * (level
+ 1), GFP_KERNEL
);
4201 return ERR_PTR(-ENOMEM
);
4203 ret
= percpu_ref_init(&cgrp
->self
.refcnt
, css_release
, 0, GFP_KERNEL
);
4208 * Temporarily set the pointer to NULL, so idr_find() won't return
4209 * a half-baked cgroup.
4211 cgrp
->id
= cgroup_idr_alloc(&root
->cgroup_idr
, NULL
, 2, 0, GFP_KERNEL
);
4214 goto out_cancel_ref
;
4217 init_cgroup_housekeeping(cgrp
);
4219 cgrp
->self
.parent
= &parent
->self
;
4221 cgrp
->level
= level
;
4223 for (tcgrp
= cgrp
; tcgrp
; tcgrp
= cgroup_parent(tcgrp
))
4224 cgrp
->ancestor_ids
[tcgrp
->level
] = tcgrp
->id
;
4226 if (notify_on_release(parent
))
4227 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4229 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4230 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4232 cgrp
->self
.serial_nr
= css_serial_nr_next
++;
4234 /* allocation complete, commit to creation */
4235 list_add_tail_rcu(&cgrp
->self
.sibling
, &cgroup_parent(cgrp
)->self
.children
);
4236 atomic_inc(&root
->nr_cgrps
);
4237 cgroup_get_live(parent
);
4240 * @cgrp is now fully operational. If something fails after this
4241 * point, it'll be released via the normal destruction path.
4243 cgroup_idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
4246 * On the default hierarchy, a child doesn't automatically inherit
4247 * subtree_control from the parent. Each is configured manually.
4249 if (!cgroup_on_dfl(cgrp
))
4250 cgrp
->subtree_control
= cgroup_control(cgrp
);
4253 cgroup_bpf_inherit(cgrp
, parent
);
4255 cgroup_propagate_control(cgrp
);
4260 percpu_ref_exit(&cgrp
->self
.refcnt
);
4263 return ERR_PTR(ret
);
4266 int cgroup_mkdir(struct kernfs_node
*parent_kn
, const char *name
, umode_t mode
)
4268 struct cgroup
*parent
, *cgrp
;
4269 struct kernfs_node
*kn
;
4272 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
4273 if (strchr(name
, '\n'))
4276 parent
= cgroup_kn_lock_live(parent_kn
, false);
4280 cgrp
= cgroup_create(parent
);
4282 ret
= PTR_ERR(cgrp
);
4286 /* create the directory */
4287 kn
= kernfs_create_dir(parent
->kn
, name
, mode
, cgrp
);
4295 * This extra ref will be put in cgroup_free_fn() and guarantees
4296 * that @cgrp->kn is always accessible.
4300 ret
= cgroup_kn_set_ugid(kn
);
4304 ret
= css_populate_dir(&cgrp
->self
);
4308 ret
= cgroup_apply_control_enable(cgrp
);
4312 trace_cgroup_mkdir(cgrp
);
4314 /* let's create and online css's */
4315 kernfs_activate(kn
);
4321 cgroup_destroy_locked(cgrp
);
4323 cgroup_kn_unlock(parent_kn
);
4328 * This is called when the refcnt of a css is confirmed to be killed.
4329 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4330 * initate destruction and put the css ref from kill_css().
4332 static void css_killed_work_fn(struct work_struct
*work
)
4334 struct cgroup_subsys_state
*css
=
4335 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4337 mutex_lock(&cgroup_mutex
);
4342 /* @css can't go away while we're holding cgroup_mutex */
4344 } while (css
&& atomic_dec_and_test(&css
->online_cnt
));
4346 mutex_unlock(&cgroup_mutex
);
4349 /* css kill confirmation processing requires process context, bounce */
4350 static void css_killed_ref_fn(struct percpu_ref
*ref
)
4352 struct cgroup_subsys_state
*css
=
4353 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4355 if (atomic_dec_and_test(&css
->online_cnt
)) {
4356 INIT_WORK(&css
->destroy_work
, css_killed_work_fn
);
4357 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4362 * kill_css - destroy a css
4363 * @css: css to destroy
4365 * This function initiates destruction of @css by removing cgroup interface
4366 * files and putting its base reference. ->css_offline() will be invoked
4367 * asynchronously once css_tryget_online() is guaranteed to fail and when
4368 * the reference count reaches zero, @css will be released.
4370 static void kill_css(struct cgroup_subsys_state
*css
)
4372 lockdep_assert_held(&cgroup_mutex
);
4374 if (css
->flags
& CSS_DYING
)
4377 css
->flags
|= CSS_DYING
;
4380 * This must happen before css is disassociated with its cgroup.
4381 * See seq_css() for details.
4386 * Killing would put the base ref, but we need to keep it alive
4387 * until after ->css_offline().
4392 * cgroup core guarantees that, by the time ->css_offline() is
4393 * invoked, no new css reference will be given out via
4394 * css_tryget_online(). We can't simply call percpu_ref_kill() and
4395 * proceed to offlining css's because percpu_ref_kill() doesn't
4396 * guarantee that the ref is seen as killed on all CPUs on return.
4398 * Use percpu_ref_kill_and_confirm() to get notifications as each
4399 * css is confirmed to be seen as killed on all CPUs.
4401 percpu_ref_kill_and_confirm(&css
->refcnt
, css_killed_ref_fn
);
4405 * cgroup_destroy_locked - the first stage of cgroup destruction
4406 * @cgrp: cgroup to be destroyed
4408 * css's make use of percpu refcnts whose killing latency shouldn't be
4409 * exposed to userland and are RCU protected. Also, cgroup core needs to
4410 * guarantee that css_tryget_online() won't succeed by the time
4411 * ->css_offline() is invoked. To satisfy all the requirements,
4412 * destruction is implemented in the following two steps.
4414 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4415 * userland visible parts and start killing the percpu refcnts of
4416 * css's. Set up so that the next stage will be kicked off once all
4417 * the percpu refcnts are confirmed to be killed.
4419 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4420 * rest of destruction. Once all cgroup references are gone, the
4421 * cgroup is RCU-freed.
4423 * This function implements s1. After this step, @cgrp is gone as far as
4424 * the userland is concerned and a new cgroup with the same name may be
4425 * created. As cgroup doesn't care about the names internally, this
4426 * doesn't cause any problem.
4428 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4429 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4431 struct cgroup_subsys_state
*css
;
4432 struct cgrp_cset_link
*link
;
4435 lockdep_assert_held(&cgroup_mutex
);
4438 * Only migration can raise populated from zero and we're already
4439 * holding cgroup_mutex.
4441 if (cgroup_is_populated(cgrp
))
4445 * Make sure there's no live children. We can't test emptiness of
4446 * ->self.children as dead children linger on it while being
4447 * drained; otherwise, "rmdir parent/child parent" may fail.
4449 if (css_has_online_children(&cgrp
->self
))
4453 * Mark @cgrp and the associated csets dead. The former prevents
4454 * further task migration and child creation by disabling
4455 * cgroup_lock_live_group(). The latter makes the csets ignored by
4456 * the migration path.
4458 cgrp
->self
.flags
&= ~CSS_ONLINE
;
4460 spin_lock_irq(&css_set_lock
);
4461 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
4462 link
->cset
->dead
= true;
4463 spin_unlock_irq(&css_set_lock
);
4465 /* initiate massacre of all css's */
4466 for_each_css(css
, ssid
, cgrp
)
4470 * Remove @cgrp directory along with the base files. @cgrp has an
4471 * extra ref on its kn.
4473 kernfs_remove(cgrp
->kn
);
4475 cgroup1_check_for_release(cgroup_parent(cgrp
));
4477 /* put the base reference */
4478 percpu_ref_kill(&cgrp
->self
.refcnt
);
4483 int cgroup_rmdir(struct kernfs_node
*kn
)
4485 struct cgroup
*cgrp
;
4488 cgrp
= cgroup_kn_lock_live(kn
, false);
4492 ret
= cgroup_destroy_locked(cgrp
);
4495 trace_cgroup_rmdir(cgrp
);
4497 cgroup_kn_unlock(kn
);
4501 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
= {
4502 .show_options
= cgroup_show_options
,
4503 .remount_fs
= cgroup_remount
,
4504 .mkdir
= cgroup_mkdir
,
4505 .rmdir
= cgroup_rmdir
,
4506 .show_path
= cgroup_show_path
,
4509 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
, bool early
)
4511 struct cgroup_subsys_state
*css
;
4513 pr_debug("Initializing cgroup subsys %s\n", ss
->name
);
4515 mutex_lock(&cgroup_mutex
);
4517 idr_init(&ss
->css_idr
);
4518 INIT_LIST_HEAD(&ss
->cfts
);
4520 /* Create the root cgroup state for this subsystem */
4521 ss
->root
= &cgrp_dfl_root
;
4522 css
= ss
->css_alloc(cgroup_css(&cgrp_dfl_root
.cgrp
, ss
));
4523 /* We don't handle early failures gracefully */
4524 BUG_ON(IS_ERR(css
));
4525 init_and_link_css(css
, ss
, &cgrp_dfl_root
.cgrp
);
4528 * Root csses are never destroyed and we can't initialize
4529 * percpu_ref during early init. Disable refcnting.
4531 css
->flags
|= CSS_NO_REF
;
4534 /* allocation can't be done safely during early init */
4537 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2, GFP_KERNEL
);
4538 BUG_ON(css
->id
< 0);
4541 /* Update the init_css_set to contain a subsys
4542 * pointer to this state - since the subsystem is
4543 * newly registered, all tasks and hence the
4544 * init_css_set is in the subsystem's root cgroup. */
4545 init_css_set
.subsys
[ss
->id
] = css
;
4547 have_fork_callback
|= (bool)ss
->fork
<< ss
->id
;
4548 have_exit_callback
|= (bool)ss
->exit
<< ss
->id
;
4549 have_free_callback
|= (bool)ss
->free
<< ss
->id
;
4550 have_canfork_callback
|= (bool)ss
->can_fork
<< ss
->id
;
4552 /* At system boot, before all subsystems have been
4553 * registered, no tasks have been forked, so we don't
4554 * need to invoke fork callbacks here. */
4555 BUG_ON(!list_empty(&init_task
.tasks
));
4557 BUG_ON(online_css(css
));
4559 mutex_unlock(&cgroup_mutex
);
4563 * cgroup_init_early - cgroup initialization at system boot
4565 * Initialize cgroups at system boot, and initialize any
4566 * subsystems that request early init.
4568 int __init
cgroup_init_early(void)
4570 static struct cgroup_sb_opts __initdata opts
;
4571 struct cgroup_subsys
*ss
;
4574 init_cgroup_root(&cgrp_dfl_root
, &opts
);
4575 cgrp_dfl_root
.cgrp
.self
.flags
|= CSS_NO_REF
;
4577 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
4579 for_each_subsys(ss
, i
) {
4580 WARN(!ss
->css_alloc
|| !ss
->css_free
|| ss
->name
|| ss
->id
,
4581 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
4582 i
, cgroup_subsys_name
[i
], ss
->css_alloc
, ss
->css_free
,
4584 WARN(strlen(cgroup_subsys_name
[i
]) > MAX_CGROUP_TYPE_NAMELEN
,
4585 "cgroup_subsys_name %s too long\n", cgroup_subsys_name
[i
]);
4588 ss
->name
= cgroup_subsys_name
[i
];
4589 if (!ss
->legacy_name
)
4590 ss
->legacy_name
= cgroup_subsys_name
[i
];
4593 cgroup_init_subsys(ss
, true);
4598 static u16 cgroup_disable_mask __initdata
;
4601 * cgroup_init - cgroup initialization
4603 * Register cgroup filesystem and /proc file, and initialize
4604 * any subsystems that didn't request early init.
4606 int __init
cgroup_init(void)
4608 struct cgroup_subsys
*ss
;
4611 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT
> 16);
4612 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem
));
4613 BUG_ON(cgroup_init_cftypes(NULL
, cgroup_base_files
));
4614 BUG_ON(cgroup_init_cftypes(NULL
, cgroup1_base_files
));
4617 * The latency of the synchronize_sched() is too high for cgroups,
4618 * avoid it at the cost of forcing all readers into the slow path.
4620 rcu_sync_enter_start(&cgroup_threadgroup_rwsem
.rss
);
4622 get_user_ns(init_cgroup_ns
.user_ns
);
4624 mutex_lock(&cgroup_mutex
);
4627 * Add init_css_set to the hash table so that dfl_root can link to
4630 hash_add(css_set_table
, &init_css_set
.hlist
,
4631 css_set_hash(init_css_set
.subsys
));
4633 BUG_ON(cgroup_setup_root(&cgrp_dfl_root
, 0, 0));
4635 mutex_unlock(&cgroup_mutex
);
4637 for_each_subsys(ss
, ssid
) {
4638 if (ss
->early_init
) {
4639 struct cgroup_subsys_state
*css
=
4640 init_css_set
.subsys
[ss
->id
];
4642 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2,
4644 BUG_ON(css
->id
< 0);
4646 cgroup_init_subsys(ss
, false);
4649 list_add_tail(&init_css_set
.e_cset_node
[ssid
],
4650 &cgrp_dfl_root
.cgrp
.e_csets
[ssid
]);
4653 * Setting dfl_root subsys_mask needs to consider the
4654 * disabled flag and cftype registration needs kmalloc,
4655 * both of which aren't available during early_init.
4657 if (cgroup_disable_mask
& (1 << ssid
)) {
4658 static_branch_disable(cgroup_subsys_enabled_key
[ssid
]);
4659 printk(KERN_INFO
"Disabling %s control group subsystem\n",
4664 if (cgroup1_ssid_disabled(ssid
))
4665 printk(KERN_INFO
"Disabling %s control group subsystem in v1 mounts\n",
4668 cgrp_dfl_root
.subsys_mask
|= 1 << ss
->id
;
4670 if (ss
->implicit_on_dfl
)
4671 cgrp_dfl_implicit_ss_mask
|= 1 << ss
->id
;
4672 else if (!ss
->dfl_cftypes
)
4673 cgrp_dfl_inhibit_ss_mask
|= 1 << ss
->id
;
4675 if (ss
->dfl_cftypes
== ss
->legacy_cftypes
) {
4676 WARN_ON(cgroup_add_cftypes(ss
, ss
->dfl_cftypes
));
4678 WARN_ON(cgroup_add_dfl_cftypes(ss
, ss
->dfl_cftypes
));
4679 WARN_ON(cgroup_add_legacy_cftypes(ss
, ss
->legacy_cftypes
));
4683 ss
->bind(init_css_set
.subsys
[ssid
]);
4685 mutex_lock(&cgroup_mutex
);
4686 css_populate_dir(init_css_set
.subsys
[ssid
]);
4687 mutex_unlock(&cgroup_mutex
);
4690 /* init_css_set.subsys[] has been updated, re-hash */
4691 hash_del(&init_css_set
.hlist
);
4692 hash_add(css_set_table
, &init_css_set
.hlist
,
4693 css_set_hash(init_css_set
.subsys
));
4695 WARN_ON(sysfs_create_mount_point(fs_kobj
, "cgroup"));
4696 WARN_ON(register_filesystem(&cgroup_fs_type
));
4697 WARN_ON(register_filesystem(&cgroup2_fs_type
));
4698 WARN_ON(!proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
));
4703 static int __init
cgroup_wq_init(void)
4706 * There isn't much point in executing destruction path in
4707 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4708 * Use 1 for @max_active.
4710 * We would prefer to do this in cgroup_init() above, but that
4711 * is called before init_workqueues(): so leave this until after.
4713 cgroup_destroy_wq
= alloc_workqueue("cgroup_destroy", 0, 1);
4714 BUG_ON(!cgroup_destroy_wq
);
4717 core_initcall(cgroup_wq_init
);
4720 * proc_cgroup_show()
4721 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4722 * - Used for /proc/<pid>/cgroup.
4724 int proc_cgroup_show(struct seq_file
*m
, struct pid_namespace
*ns
,
4725 struct pid
*pid
, struct task_struct
*tsk
)
4729 struct cgroup_root
*root
;
4732 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
4736 mutex_lock(&cgroup_mutex
);
4737 spin_lock_irq(&css_set_lock
);
4739 for_each_root(root
) {
4740 struct cgroup_subsys
*ss
;
4741 struct cgroup
*cgrp
;
4742 int ssid
, count
= 0;
4744 if (root
== &cgrp_dfl_root
&& !cgrp_dfl_visible
)
4747 seq_printf(m
, "%d:", root
->hierarchy_id
);
4748 if (root
!= &cgrp_dfl_root
)
4749 for_each_subsys(ss
, ssid
)
4750 if (root
->subsys_mask
& (1 << ssid
))
4751 seq_printf(m
, "%s%s", count
++ ? "," : "",
4753 if (strlen(root
->name
))
4754 seq_printf(m
, "%sname=%s", count
? "," : "",
4758 cgrp
= task_cgroup_from_root(tsk
, root
);
4761 * On traditional hierarchies, all zombie tasks show up as
4762 * belonging to the root cgroup. On the default hierarchy,
4763 * while a zombie doesn't show up in "cgroup.procs" and
4764 * thus can't be migrated, its /proc/PID/cgroup keeps
4765 * reporting the cgroup it belonged to before exiting. If
4766 * the cgroup is removed before the zombie is reaped,
4767 * " (deleted)" is appended to the cgroup path.
4769 if (cgroup_on_dfl(cgrp
) || !(tsk
->flags
& PF_EXITING
)) {
4770 retval
= cgroup_path_ns_locked(cgrp
, buf
, PATH_MAX
,
4771 current
->nsproxy
->cgroup_ns
);
4772 if (retval
>= PATH_MAX
)
4773 retval
= -ENAMETOOLONG
;
4782 if (cgroup_on_dfl(cgrp
) && cgroup_is_dead(cgrp
))
4783 seq_puts(m
, " (deleted)\n");
4790 spin_unlock_irq(&css_set_lock
);
4791 mutex_unlock(&cgroup_mutex
);
4798 * cgroup_fork - initialize cgroup related fields during copy_process()
4799 * @child: pointer to task_struct of forking parent process.
4801 * A task is associated with the init_css_set until cgroup_post_fork()
4802 * attaches it to the parent's css_set. Empty cg_list indicates that
4803 * @child isn't holding reference to its css_set.
4805 void cgroup_fork(struct task_struct
*child
)
4807 RCU_INIT_POINTER(child
->cgroups
, &init_css_set
);
4808 INIT_LIST_HEAD(&child
->cg_list
);
4812 * cgroup_can_fork - called on a new task before the process is exposed
4813 * @child: the task in question.
4815 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
4816 * returns an error, the fork aborts with that error code. This allows for
4817 * a cgroup subsystem to conditionally allow or deny new forks.
4819 int cgroup_can_fork(struct task_struct
*child
)
4821 struct cgroup_subsys
*ss
;
4824 do_each_subsys_mask(ss
, i
, have_canfork_callback
) {
4825 ret
= ss
->can_fork(child
);
4828 } while_each_subsys_mask();
4833 for_each_subsys(ss
, j
) {
4836 if (ss
->cancel_fork
)
4837 ss
->cancel_fork(child
);
4844 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
4845 * @child: the task in question
4847 * This calls the cancel_fork() callbacks if a fork failed *after*
4848 * cgroup_can_fork() succeded.
4850 void cgroup_cancel_fork(struct task_struct
*child
)
4852 struct cgroup_subsys
*ss
;
4855 for_each_subsys(ss
, i
)
4856 if (ss
->cancel_fork
)
4857 ss
->cancel_fork(child
);
4861 * cgroup_post_fork - called on a new task after adding it to the task list
4862 * @child: the task in question
4864 * Adds the task to the list running through its css_set if necessary and
4865 * call the subsystem fork() callbacks. Has to be after the task is
4866 * visible on the task list in case we race with the first call to
4867 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
4870 void cgroup_post_fork(struct task_struct
*child
)
4872 struct cgroup_subsys
*ss
;
4876 * This may race against cgroup_enable_task_cg_lists(). As that
4877 * function sets use_task_css_set_links before grabbing
4878 * tasklist_lock and we just went through tasklist_lock to add
4879 * @child, it's guaranteed that either we see the set
4880 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4881 * @child during its iteration.
4883 * If we won the race, @child is associated with %current's
4884 * css_set. Grabbing css_set_lock guarantees both that the
4885 * association is stable, and, on completion of the parent's
4886 * migration, @child is visible in the source of migration or
4887 * already in the destination cgroup. This guarantee is necessary
4888 * when implementing operations which need to migrate all tasks of
4889 * a cgroup to another.
4891 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
4892 * will remain in init_css_set. This is safe because all tasks are
4893 * in the init_css_set before cg_links is enabled and there's no
4894 * operation which transfers all tasks out of init_css_set.
4896 if (use_task_css_set_links
) {
4897 struct css_set
*cset
;
4899 spin_lock_irq(&css_set_lock
);
4900 cset
= task_css_set(current
);
4901 if (list_empty(&child
->cg_list
)) {
4904 css_set_move_task(child
, NULL
, cset
, false);
4906 spin_unlock_irq(&css_set_lock
);
4910 * Call ss->fork(). This must happen after @child is linked on
4911 * css_set; otherwise, @child might change state between ->fork()
4912 * and addition to css_set.
4914 do_each_subsys_mask(ss
, i
, have_fork_callback
) {
4916 } while_each_subsys_mask();
4920 * cgroup_exit - detach cgroup from exiting task
4921 * @tsk: pointer to task_struct of exiting process
4923 * Description: Detach cgroup from @tsk and release it.
4925 * Note that cgroups marked notify_on_release force every task in
4926 * them to take the global cgroup_mutex mutex when exiting.
4927 * This could impact scaling on very large systems. Be reluctant to
4928 * use notify_on_release cgroups where very high task exit scaling
4929 * is required on large systems.
4931 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4932 * call cgroup_exit() while the task is still competent to handle
4933 * notify_on_release(), then leave the task attached to the root cgroup in
4934 * each hierarchy for the remainder of its exit. No need to bother with
4935 * init_css_set refcnting. init_css_set never goes away and we can't race
4936 * with migration path - PF_EXITING is visible to migration path.
4938 void cgroup_exit(struct task_struct
*tsk
)
4940 struct cgroup_subsys
*ss
;
4941 struct css_set
*cset
;
4945 * Unlink from @tsk from its css_set. As migration path can't race
4946 * with us, we can check css_set and cg_list without synchronization.
4948 cset
= task_css_set(tsk
);
4950 if (!list_empty(&tsk
->cg_list
)) {
4951 spin_lock_irq(&css_set_lock
);
4952 css_set_move_task(tsk
, cset
, NULL
, false);
4954 spin_unlock_irq(&css_set_lock
);
4959 /* see cgroup_post_fork() for details */
4960 do_each_subsys_mask(ss
, i
, have_exit_callback
) {
4962 } while_each_subsys_mask();
4965 void cgroup_free(struct task_struct
*task
)
4967 struct css_set
*cset
= task_css_set(task
);
4968 struct cgroup_subsys
*ss
;
4971 do_each_subsys_mask(ss
, ssid
, have_free_callback
) {
4973 } while_each_subsys_mask();
4978 static int __init
cgroup_disable(char *str
)
4980 struct cgroup_subsys
*ss
;
4984 while ((token
= strsep(&str
, ",")) != NULL
) {
4988 for_each_subsys(ss
, i
) {
4989 if (strcmp(token
, ss
->name
) &&
4990 strcmp(token
, ss
->legacy_name
))
4992 cgroup_disable_mask
|= 1 << i
;
4997 __setup("cgroup_disable=", cgroup_disable
);
5000 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
5001 * @dentry: directory dentry of interest
5002 * @ss: subsystem of interest
5004 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5005 * to get the corresponding css and return it. If such css doesn't exist
5006 * or can't be pinned, an ERR_PTR value is returned.
5008 struct cgroup_subsys_state
*css_tryget_online_from_dir(struct dentry
*dentry
,
5009 struct cgroup_subsys
*ss
)
5011 struct kernfs_node
*kn
= kernfs_node_from_dentry(dentry
);
5012 struct file_system_type
*s_type
= dentry
->d_sb
->s_type
;
5013 struct cgroup_subsys_state
*css
= NULL
;
5014 struct cgroup
*cgrp
;
5016 /* is @dentry a cgroup dir? */
5017 if ((s_type
!= &cgroup_fs_type
&& s_type
!= &cgroup2_fs_type
) ||
5018 !kn
|| kernfs_type(kn
) != KERNFS_DIR
)
5019 return ERR_PTR(-EBADF
);
5024 * This path doesn't originate from kernfs and @kn could already
5025 * have been or be removed at any point. @kn->priv is RCU
5026 * protected for this access. See css_release_work_fn() for details.
5028 cgrp
= rcu_dereference(*(void __rcu __force
**)&kn
->priv
);
5030 css
= cgroup_css(cgrp
, ss
);
5032 if (!css
|| !css_tryget_online(css
))
5033 css
= ERR_PTR(-ENOENT
);
5040 * css_from_id - lookup css by id
5041 * @id: the cgroup id
5042 * @ss: cgroup subsys to be looked into
5044 * Returns the css if there's valid one with @id, otherwise returns NULL.
5045 * Should be called under rcu_read_lock().
5047 struct cgroup_subsys_state
*css_from_id(int id
, struct cgroup_subsys
*ss
)
5049 WARN_ON_ONCE(!rcu_read_lock_held());
5050 return idr_find(&ss
->css_idr
, id
);
5054 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
5055 * @path: path on the default hierarchy
5057 * Find the cgroup at @path on the default hierarchy, increment its
5058 * reference count and return it. Returns pointer to the found cgroup on
5059 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
5060 * if @path points to a non-directory.
5062 struct cgroup
*cgroup_get_from_path(const char *path
)
5064 struct kernfs_node
*kn
;
5065 struct cgroup
*cgrp
;
5067 mutex_lock(&cgroup_mutex
);
5069 kn
= kernfs_walk_and_get(cgrp_dfl_root
.cgrp
.kn
, path
);
5071 if (kernfs_type(kn
) == KERNFS_DIR
) {
5073 cgroup_get_live(cgrp
);
5075 cgrp
= ERR_PTR(-ENOTDIR
);
5079 cgrp
= ERR_PTR(-ENOENT
);
5082 mutex_unlock(&cgroup_mutex
);
5085 EXPORT_SYMBOL_GPL(cgroup_get_from_path
);
5088 * cgroup_get_from_fd - get a cgroup pointer from a fd
5089 * @fd: fd obtained by open(cgroup2_dir)
5091 * Find the cgroup from a fd which should be obtained
5092 * by opening a cgroup directory. Returns a pointer to the
5093 * cgroup on success. ERR_PTR is returned if the cgroup
5096 struct cgroup
*cgroup_get_from_fd(int fd
)
5098 struct cgroup_subsys_state
*css
;
5099 struct cgroup
*cgrp
;
5104 return ERR_PTR(-EBADF
);
5106 css
= css_tryget_online_from_dir(f
->f_path
.dentry
, NULL
);
5109 return ERR_CAST(css
);
5112 if (!cgroup_on_dfl(cgrp
)) {
5114 return ERR_PTR(-EBADF
);
5119 EXPORT_SYMBOL_GPL(cgroup_get_from_fd
);
5122 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5123 * definition in cgroup-defs.h.
5125 #ifdef CONFIG_SOCK_CGROUP_DATA
5127 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5129 DEFINE_SPINLOCK(cgroup_sk_update_lock
);
5130 static bool cgroup_sk_alloc_disabled __read_mostly
;
5132 void cgroup_sk_alloc_disable(void)
5134 if (cgroup_sk_alloc_disabled
)
5136 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5137 cgroup_sk_alloc_disabled
= true;
5142 #define cgroup_sk_alloc_disabled false
5146 void cgroup_sk_alloc(struct sock_cgroup_data
*skcd
)
5148 if (cgroup_sk_alloc_disabled
)
5151 /* Socket clone path */
5154 * We might be cloning a socket which is left in an empty
5155 * cgroup and the cgroup might have already been rmdir'd.
5156 * Don't use cgroup_get_live().
5158 cgroup_get(sock_cgroup_ptr(skcd
));
5165 struct css_set
*cset
;
5167 cset
= task_css_set(current
);
5168 if (likely(cgroup_tryget(cset
->dfl_cgrp
))) {
5169 skcd
->val
= (unsigned long)cset
->dfl_cgrp
;
5178 void cgroup_sk_free(struct sock_cgroup_data
*skcd
)
5180 cgroup_put(sock_cgroup_ptr(skcd
));
5183 #endif /* CONFIG_SOCK_CGROUP_DATA */
5185 #ifdef CONFIG_CGROUP_BPF
5186 int cgroup_bpf_update(struct cgroup
*cgrp
, struct bpf_prog
*prog
,
5187 enum bpf_attach_type type
, bool overridable
)
5189 struct cgroup
*parent
= cgroup_parent(cgrp
);
5192 mutex_lock(&cgroup_mutex
);
5193 ret
= __cgroup_bpf_update(cgrp
, parent
, prog
, type
, overridable
);
5194 mutex_unlock(&cgroup_mutex
);
5197 #endif /* CONFIG_CGROUP_BPF */