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
= { .counter
= 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 cgroup_get(struct cgroup
*cgrp
)
441 WARN_ON_ONCE(cgroup_is_dead(cgrp
));
442 css_get(&cgrp
->self
);
445 static bool cgroup_tryget(struct cgroup
*cgrp
)
447 return css_tryget(&cgrp
->self
);
450 struct cgroup_subsys_state
*of_css(struct kernfs_open_file
*of
)
452 struct cgroup
*cgrp
= of
->kn
->parent
->priv
;
453 struct cftype
*cft
= of_cft(of
);
456 * This is open and unprotected implementation of cgroup_css().
457 * seq_css() is only called from a kernfs file operation which has
458 * an active reference on the file. Because all the subsystem
459 * files are drained before a css is disassociated with a cgroup,
460 * the matching css from the cgroup's subsys table is guaranteed to
461 * be and stay valid until the enclosing operation is complete.
464 return rcu_dereference_raw(cgrp
->subsys
[cft
->ss
->id
]);
468 EXPORT_SYMBOL_GPL(of_css
);
471 * for_each_css - iterate all css's of a cgroup
472 * @css: the iteration cursor
473 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
474 * @cgrp: the target cgroup to iterate css's of
476 * Should be called under cgroup_[tree_]mutex.
478 #define for_each_css(css, ssid, cgrp) \
479 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
480 if (!((css) = rcu_dereference_check( \
481 (cgrp)->subsys[(ssid)], \
482 lockdep_is_held(&cgroup_mutex)))) { } \
486 * for_each_e_css - iterate all effective css's of a cgroup
487 * @css: the iteration cursor
488 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
489 * @cgrp: the target cgroup to iterate css's of
491 * Should be called under cgroup_[tree_]mutex.
493 #define for_each_e_css(css, ssid, cgrp) \
494 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
495 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
500 * do_each_subsys_mask - filter for_each_subsys with a bitmask
501 * @ss: the iteration cursor
502 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
503 * @ss_mask: the bitmask
505 * The block will only run for cases where the ssid-th bit (1 << ssid) of
508 #define do_each_subsys_mask(ss, ssid, ss_mask) do { \
509 unsigned long __ss_mask = (ss_mask); \
510 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
514 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
515 (ss) = cgroup_subsys[ssid]; \
518 #define while_each_subsys_mask() \
523 /* iterate over child cgrps, lock should be held throughout iteration */
524 #define cgroup_for_each_live_child(child, cgrp) \
525 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
526 if (({ lockdep_assert_held(&cgroup_mutex); \
527 cgroup_is_dead(child); })) \
531 /* walk live descendants in preorder */
532 #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
533 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
534 if (({ lockdep_assert_held(&cgroup_mutex); \
535 (dsct) = (d_css)->cgroup; \
536 cgroup_is_dead(dsct); })) \
540 /* walk live descendants in postorder */
541 #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
542 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
543 if (({ lockdep_assert_held(&cgroup_mutex); \
544 (dsct) = (d_css)->cgroup; \
545 cgroup_is_dead(dsct); })) \
550 * The default css_set - used by init and its children prior to any
551 * hierarchies being mounted. It contains a pointer to the root state
552 * for each subsystem. Also used to anchor the list of css_sets. Not
553 * reference-counted, to improve performance when child cgroups
554 * haven't been created.
556 struct css_set init_css_set
= {
557 .refcount
= ATOMIC_INIT(1),
558 .tasks
= LIST_HEAD_INIT(init_css_set
.tasks
),
559 .mg_tasks
= LIST_HEAD_INIT(init_css_set
.mg_tasks
),
560 .task_iters
= LIST_HEAD_INIT(init_css_set
.task_iters
),
561 .cgrp_links
= LIST_HEAD_INIT(init_css_set
.cgrp_links
),
562 .mg_preload_node
= LIST_HEAD_INIT(init_css_set
.mg_preload_node
),
563 .mg_node
= LIST_HEAD_INIT(init_css_set
.mg_node
),
566 static int css_set_count
= 1; /* 1 for init_css_set */
569 * css_set_populated - does a css_set contain any tasks?
570 * @cset: target css_set
572 static bool css_set_populated(struct css_set
*cset
)
574 lockdep_assert_held(&css_set_lock
);
576 return !list_empty(&cset
->tasks
) || !list_empty(&cset
->mg_tasks
);
580 * cgroup_update_populated - updated populated count of a cgroup
581 * @cgrp: the target cgroup
582 * @populated: inc or dec populated count
584 * One of the css_sets associated with @cgrp is either getting its first
585 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
586 * count is propagated towards root so that a given cgroup's populated_cnt
587 * is zero iff the cgroup and all its descendants don't contain any tasks.
589 * @cgrp's interface file "cgroup.populated" is zero if
590 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
591 * changes from or to zero, userland is notified that the content of the
592 * interface file has changed. This can be used to detect when @cgrp and
593 * its descendants become populated or empty.
595 static void cgroup_update_populated(struct cgroup
*cgrp
, bool populated
)
597 lockdep_assert_held(&css_set_lock
);
603 trigger
= !cgrp
->populated_cnt
++;
605 trigger
= !--cgrp
->populated_cnt
;
610 cgroup1_check_for_release(cgrp
);
611 cgroup_file_notify(&cgrp
->events_file
);
613 cgrp
= cgroup_parent(cgrp
);
618 * css_set_update_populated - update populated state of a css_set
619 * @cset: target css_set
620 * @populated: whether @cset is populated or depopulated
622 * @cset is either getting the first task or losing the last. Update the
623 * ->populated_cnt of all associated cgroups accordingly.
625 static void css_set_update_populated(struct css_set
*cset
, bool populated
)
627 struct cgrp_cset_link
*link
;
629 lockdep_assert_held(&css_set_lock
);
631 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
)
632 cgroup_update_populated(link
->cgrp
, populated
);
636 * css_set_move_task - move a task from one css_set to another
637 * @task: task being moved
638 * @from_cset: css_set @task currently belongs to (may be NULL)
639 * @to_cset: new css_set @task is being moved to (may be NULL)
640 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
642 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
643 * css_set, @from_cset can be NULL. If @task is being disassociated
644 * instead of moved, @to_cset can be NULL.
646 * This function automatically handles populated_cnt updates and
647 * css_task_iter adjustments but the caller is responsible for managing
648 * @from_cset and @to_cset's reference counts.
650 static void css_set_move_task(struct task_struct
*task
,
651 struct css_set
*from_cset
, struct css_set
*to_cset
,
654 lockdep_assert_held(&css_set_lock
);
656 if (to_cset
&& !css_set_populated(to_cset
))
657 css_set_update_populated(to_cset
, true);
660 struct css_task_iter
*it
, *pos
;
662 WARN_ON_ONCE(list_empty(&task
->cg_list
));
665 * @task is leaving, advance task iterators which are
666 * pointing to it so that they can resume at the next
667 * position. Advancing an iterator might remove it from
668 * the list, use safe walk. See css_task_iter_advance*()
671 list_for_each_entry_safe(it
, pos
, &from_cset
->task_iters
,
673 if (it
->task_pos
== &task
->cg_list
)
674 css_task_iter_advance(it
);
676 list_del_init(&task
->cg_list
);
677 if (!css_set_populated(from_cset
))
678 css_set_update_populated(from_cset
, false);
680 WARN_ON_ONCE(!list_empty(&task
->cg_list
));
685 * We are synchronized through cgroup_threadgroup_rwsem
686 * against PF_EXITING setting such that we can't race
687 * against cgroup_exit() changing the css_set to
688 * init_css_set and dropping the old one.
690 WARN_ON_ONCE(task
->flags
& PF_EXITING
);
692 rcu_assign_pointer(task
->cgroups
, to_cset
);
693 list_add_tail(&task
->cg_list
, use_mg_tasks
? &to_cset
->mg_tasks
:
699 * hash table for cgroup groups. This improves the performance to find
700 * an existing css_set. This hash doesn't (currently) take into
701 * account cgroups in empty hierarchies.
703 #define CSS_SET_HASH_BITS 7
704 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
706 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
708 unsigned long key
= 0UL;
709 struct cgroup_subsys
*ss
;
712 for_each_subsys(ss
, i
)
713 key
+= (unsigned long)css
[i
];
714 key
= (key
>> 16) ^ key
;
719 void put_css_set_locked(struct css_set
*cset
)
721 struct cgrp_cset_link
*link
, *tmp_link
;
722 struct cgroup_subsys
*ss
;
725 lockdep_assert_held(&css_set_lock
);
727 if (!atomic_dec_and_test(&cset
->refcount
))
730 /* This css_set is dead. unlink it and release cgroup and css refs */
731 for_each_subsys(ss
, ssid
) {
732 list_del(&cset
->e_cset_node
[ssid
]);
733 css_put(cset
->subsys
[ssid
]);
735 hash_del(&cset
->hlist
);
738 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
739 list_del(&link
->cset_link
);
740 list_del(&link
->cgrp_link
);
741 if (cgroup_parent(link
->cgrp
))
742 cgroup_put(link
->cgrp
);
746 kfree_rcu(cset
, rcu_head
);
750 * compare_css_sets - helper function for find_existing_css_set().
751 * @cset: candidate css_set being tested
752 * @old_cset: existing css_set for a task
753 * @new_cgrp: cgroup that's being entered by the task
754 * @template: desired set of css pointers in css_set (pre-calculated)
756 * Returns true if "cset" matches "old_cset" except for the hierarchy
757 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
759 static bool compare_css_sets(struct css_set
*cset
,
760 struct css_set
*old_cset
,
761 struct cgroup
*new_cgrp
,
762 struct cgroup_subsys_state
*template[])
764 struct list_head
*l1
, *l2
;
767 * On the default hierarchy, there can be csets which are
768 * associated with the same set of cgroups but different csses.
769 * Let's first ensure that csses match.
771 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
)))
775 * Compare cgroup pointers in order to distinguish between
776 * different cgroups in hierarchies. As different cgroups may
777 * share the same effective css, this comparison is always
780 l1
= &cset
->cgrp_links
;
781 l2
= &old_cset
->cgrp_links
;
783 struct cgrp_cset_link
*link1
, *link2
;
784 struct cgroup
*cgrp1
, *cgrp2
;
788 /* See if we reached the end - both lists are equal length. */
789 if (l1
== &cset
->cgrp_links
) {
790 BUG_ON(l2
!= &old_cset
->cgrp_links
);
793 BUG_ON(l2
== &old_cset
->cgrp_links
);
795 /* Locate the cgroups associated with these links. */
796 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
797 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
800 /* Hierarchies should be linked in the same order. */
801 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
804 * If this hierarchy is the hierarchy of the cgroup
805 * that's changing, then we need to check that this
806 * css_set points to the new cgroup; if it's any other
807 * hierarchy, then this css_set should point to the
808 * same cgroup as the old css_set.
810 if (cgrp1
->root
== new_cgrp
->root
) {
811 if (cgrp1
!= new_cgrp
)
822 * find_existing_css_set - init css array and find the matching css_set
823 * @old_cset: the css_set that we're using before the cgroup transition
824 * @cgrp: the cgroup that we're moving into
825 * @template: out param for the new set of csses, should be clear on entry
827 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
829 struct cgroup_subsys_state
*template[])
831 struct cgroup_root
*root
= cgrp
->root
;
832 struct cgroup_subsys
*ss
;
833 struct css_set
*cset
;
838 * Build the set of subsystem state objects that we want to see in the
839 * new css_set. while subsystems can change globally, the entries here
840 * won't change, so no need for locking.
842 for_each_subsys(ss
, i
) {
843 if (root
->subsys_mask
& (1UL << i
)) {
845 * @ss is in this hierarchy, so we want the
846 * effective css from @cgrp.
848 template[i
] = cgroup_e_css(cgrp
, ss
);
851 * @ss is not in this hierarchy, so we don't want
854 template[i
] = old_cset
->subsys
[i
];
858 key
= css_set_hash(template);
859 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
860 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
863 /* This css_set matches what we need */
867 /* No existing cgroup group matched */
871 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
873 struct cgrp_cset_link
*link
, *tmp_link
;
875 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
876 list_del(&link
->cset_link
);
882 * allocate_cgrp_cset_links - allocate cgrp_cset_links
883 * @count: the number of links to allocate
884 * @tmp_links: list_head the allocated links are put on
886 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
887 * through ->cset_link. Returns 0 on success or -errno.
889 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
891 struct cgrp_cset_link
*link
;
894 INIT_LIST_HEAD(tmp_links
);
896 for (i
= 0; i
< count
; i
++) {
897 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
899 free_cgrp_cset_links(tmp_links
);
902 list_add(&link
->cset_link
, tmp_links
);
908 * link_css_set - a helper function to link a css_set to a cgroup
909 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
910 * @cset: the css_set to be linked
911 * @cgrp: the destination cgroup
913 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
916 struct cgrp_cset_link
*link
;
918 BUG_ON(list_empty(tmp_links
));
920 if (cgroup_on_dfl(cgrp
))
921 cset
->dfl_cgrp
= cgrp
;
923 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
928 * Always add links to the tail of the lists so that the lists are
929 * in choronological order.
931 list_move_tail(&link
->cset_link
, &cgrp
->cset_links
);
932 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
934 if (cgroup_parent(cgrp
))
939 * find_css_set - return a new css_set with one cgroup updated
940 * @old_cset: the baseline css_set
941 * @cgrp: the cgroup to be updated
943 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
944 * substituted into the appropriate hierarchy.
946 static struct css_set
*find_css_set(struct css_set
*old_cset
,
949 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
950 struct css_set
*cset
;
951 struct list_head tmp_links
;
952 struct cgrp_cset_link
*link
;
953 struct cgroup_subsys
*ss
;
957 lockdep_assert_held(&cgroup_mutex
);
959 /* First see if we already have a cgroup group that matches
961 spin_lock_irq(&css_set_lock
);
962 cset
= find_existing_css_set(old_cset
, cgrp
, template);
965 spin_unlock_irq(&css_set_lock
);
970 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
974 /* Allocate all the cgrp_cset_link objects that we'll need */
975 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
980 atomic_set(&cset
->refcount
, 1);
981 INIT_LIST_HEAD(&cset
->tasks
);
982 INIT_LIST_HEAD(&cset
->mg_tasks
);
983 INIT_LIST_HEAD(&cset
->task_iters
);
984 INIT_HLIST_NODE(&cset
->hlist
);
985 INIT_LIST_HEAD(&cset
->cgrp_links
);
986 INIT_LIST_HEAD(&cset
->mg_preload_node
);
987 INIT_LIST_HEAD(&cset
->mg_node
);
989 /* Copy the set of subsystem state objects generated in
990 * find_existing_css_set() */
991 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
993 spin_lock_irq(&css_set_lock
);
994 /* Add reference counts and links from the new css_set. */
995 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
996 struct cgroup
*c
= link
->cgrp
;
998 if (c
->root
== cgrp
->root
)
1000 link_css_set(&tmp_links
, cset
, c
);
1003 BUG_ON(!list_empty(&tmp_links
));
1007 /* Add @cset to the hash table */
1008 key
= css_set_hash(cset
->subsys
);
1009 hash_add(css_set_table
, &cset
->hlist
, key
);
1011 for_each_subsys(ss
, ssid
) {
1012 struct cgroup_subsys_state
*css
= cset
->subsys
[ssid
];
1014 list_add_tail(&cset
->e_cset_node
[ssid
],
1015 &css
->cgroup
->e_csets
[ssid
]);
1019 spin_unlock_irq(&css_set_lock
);
1024 struct cgroup_root
*cgroup_root_from_kf(struct kernfs_root
*kf_root
)
1026 struct cgroup
*root_cgrp
= kf_root
->kn
->priv
;
1028 return root_cgrp
->root
;
1031 static int cgroup_init_root_id(struct cgroup_root
*root
)
1035 lockdep_assert_held(&cgroup_mutex
);
1037 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, 0, 0, GFP_KERNEL
);
1041 root
->hierarchy_id
= id
;
1045 static void cgroup_exit_root_id(struct cgroup_root
*root
)
1047 lockdep_assert_held(&cgroup_mutex
);
1049 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1052 void cgroup_free_root(struct cgroup_root
*root
)
1055 idr_destroy(&root
->cgroup_idr
);
1060 static void cgroup_destroy_root(struct cgroup_root
*root
)
1062 struct cgroup
*cgrp
= &root
->cgrp
;
1063 struct cgrp_cset_link
*link
, *tmp_link
;
1065 trace_cgroup_destroy_root(root
);
1067 cgroup_lock_and_drain_offline(&cgrp_dfl_root
.cgrp
);
1069 BUG_ON(atomic_read(&root
->nr_cgrps
));
1070 BUG_ON(!list_empty(&cgrp
->self
.children
));
1072 /* Rebind all subsystems back to the default hierarchy */
1073 WARN_ON(rebind_subsystems(&cgrp_dfl_root
, root
->subsys_mask
));
1076 * Release all the links from cset_links to this hierarchy's
1079 spin_lock_irq(&css_set_lock
);
1081 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1082 list_del(&link
->cset_link
);
1083 list_del(&link
->cgrp_link
);
1087 spin_unlock_irq(&css_set_lock
);
1089 if (!list_empty(&root
->root_list
)) {
1090 list_del(&root
->root_list
);
1091 cgroup_root_count
--;
1094 cgroup_exit_root_id(root
);
1096 mutex_unlock(&cgroup_mutex
);
1098 kernfs_destroy_root(root
->kf_root
);
1099 cgroup_free_root(root
);
1103 * look up cgroup associated with current task's cgroup namespace on the
1104 * specified hierarchy
1106 static struct cgroup
*
1107 current_cgns_cgroup_from_root(struct cgroup_root
*root
)
1109 struct cgroup
*res
= NULL
;
1110 struct css_set
*cset
;
1112 lockdep_assert_held(&css_set_lock
);
1116 cset
= current
->nsproxy
->cgroup_ns
->root_cset
;
1117 if (cset
== &init_css_set
) {
1120 struct cgrp_cset_link
*link
;
1122 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
1123 struct cgroup
*c
= link
->cgrp
;
1125 if (c
->root
== root
) {
1137 /* look up cgroup associated with given css_set on the specified hierarchy */
1138 static struct cgroup
*cset_cgroup_from_root(struct css_set
*cset
,
1139 struct cgroup_root
*root
)
1141 struct cgroup
*res
= NULL
;
1143 lockdep_assert_held(&cgroup_mutex
);
1144 lockdep_assert_held(&css_set_lock
);
1146 if (cset
== &init_css_set
) {
1149 struct cgrp_cset_link
*link
;
1151 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
1152 struct cgroup
*c
= link
->cgrp
;
1154 if (c
->root
== root
) {
1166 * Return the cgroup for "task" from the given hierarchy. Must be
1167 * called with cgroup_mutex and css_set_lock held.
1169 struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
1170 struct cgroup_root
*root
)
1173 * No need to lock the task - since we hold cgroup_mutex the
1174 * task can't change groups, so the only thing that can happen
1175 * is that it exits and its css is set back to init_css_set.
1177 return cset_cgroup_from_root(task_css_set(task
), root
);
1181 * A task must hold cgroup_mutex to modify cgroups.
1183 * Any task can increment and decrement the count field without lock.
1184 * So in general, code holding cgroup_mutex can't rely on the count
1185 * field not changing. However, if the count goes to zero, then only
1186 * cgroup_attach_task() can increment it again. Because a count of zero
1187 * means that no tasks are currently attached, therefore there is no
1188 * way a task attached to that cgroup can fork (the other way to
1189 * increment the count). So code holding cgroup_mutex can safely
1190 * assume that if the count is zero, it will stay zero. Similarly, if
1191 * a task holds cgroup_mutex on a cgroup with zero count, it
1192 * knows that the cgroup won't be removed, as cgroup_rmdir()
1195 * A cgroup can only be deleted if both its 'count' of using tasks
1196 * is zero, and its list of 'children' cgroups is empty. Since all
1197 * tasks in the system use _some_ cgroup, and since there is always at
1198 * least one task in the system (init, pid == 1), therefore, root cgroup
1199 * always has either children cgroups and/or using tasks. So we don't
1200 * need a special hack to ensure that root cgroup cannot be deleted.
1202 * P.S. One more locking exception. RCU is used to guard the
1203 * update of a tasks cgroup pointer by cgroup_attach_task()
1206 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
;
1208 static char *cgroup_file_name(struct cgroup
*cgrp
, const struct cftype
*cft
,
1211 struct cgroup_subsys
*ss
= cft
->ss
;
1213 if (cft
->ss
&& !(cft
->flags
& CFTYPE_NO_PREFIX
) &&
1214 !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
))
1215 snprintf(buf
, CGROUP_FILE_NAME_MAX
, "%s.%s",
1216 cgroup_on_dfl(cgrp
) ? ss
->name
: ss
->legacy_name
,
1219 strncpy(buf
, cft
->name
, CGROUP_FILE_NAME_MAX
);
1224 * cgroup_file_mode - deduce file mode of a control file
1225 * @cft: the control file in question
1227 * S_IRUGO for read, S_IWUSR for write.
1229 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
1233 if (cft
->read_u64
|| cft
->read_s64
|| cft
->seq_show
)
1236 if (cft
->write_u64
|| cft
->write_s64
|| cft
->write
) {
1237 if (cft
->flags
& CFTYPE_WORLD_WRITABLE
)
1247 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
1248 * @subtree_control: the new subtree_control mask to consider
1249 * @this_ss_mask: available subsystems
1251 * On the default hierarchy, a subsystem may request other subsystems to be
1252 * enabled together through its ->depends_on mask. In such cases, more
1253 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1255 * This function calculates which subsystems need to be enabled if
1256 * @subtree_control is to be applied while restricted to @this_ss_mask.
1258 static u16
cgroup_calc_subtree_ss_mask(u16 subtree_control
, u16 this_ss_mask
)
1260 u16 cur_ss_mask
= subtree_control
;
1261 struct cgroup_subsys
*ss
;
1264 lockdep_assert_held(&cgroup_mutex
);
1266 cur_ss_mask
|= cgrp_dfl_implicit_ss_mask
;
1269 u16 new_ss_mask
= cur_ss_mask
;
1271 do_each_subsys_mask(ss
, ssid
, cur_ss_mask
) {
1272 new_ss_mask
|= ss
->depends_on
;
1273 } while_each_subsys_mask();
1276 * Mask out subsystems which aren't available. This can
1277 * happen only if some depended-upon subsystems were bound
1278 * to non-default hierarchies.
1280 new_ss_mask
&= this_ss_mask
;
1282 if (new_ss_mask
== cur_ss_mask
)
1284 cur_ss_mask
= new_ss_mask
;
1291 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1292 * @kn: the kernfs_node being serviced
1294 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1295 * the method finishes if locking succeeded. Note that once this function
1296 * returns the cgroup returned by cgroup_kn_lock_live() may become
1297 * inaccessible any time. If the caller intends to continue to access the
1298 * cgroup, it should pin it before invoking this function.
1300 void cgroup_kn_unlock(struct kernfs_node
*kn
)
1302 struct cgroup
*cgrp
;
1304 if (kernfs_type(kn
) == KERNFS_DIR
)
1307 cgrp
= kn
->parent
->priv
;
1309 mutex_unlock(&cgroup_mutex
);
1311 kernfs_unbreak_active_protection(kn
);
1316 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1317 * @kn: the kernfs_node being serviced
1318 * @drain_offline: perform offline draining on the cgroup
1320 * This helper is to be used by a cgroup kernfs method currently servicing
1321 * @kn. It breaks the active protection, performs cgroup locking and
1322 * verifies that the associated cgroup is alive. Returns the cgroup if
1323 * alive; otherwise, %NULL. A successful return should be undone by a
1324 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1325 * cgroup is drained of offlining csses before return.
1327 * Any cgroup kernfs method implementation which requires locking the
1328 * associated cgroup should use this helper. It avoids nesting cgroup
1329 * locking under kernfs active protection and allows all kernfs operations
1330 * including self-removal.
1332 struct cgroup
*cgroup_kn_lock_live(struct kernfs_node
*kn
, bool drain_offline
)
1334 struct cgroup
*cgrp
;
1336 if (kernfs_type(kn
) == KERNFS_DIR
)
1339 cgrp
= kn
->parent
->priv
;
1342 * We're gonna grab cgroup_mutex which nests outside kernfs
1343 * active_ref. cgroup liveliness check alone provides enough
1344 * protection against removal. Ensure @cgrp stays accessible and
1345 * break the active_ref protection.
1347 if (!cgroup_tryget(cgrp
))
1349 kernfs_break_active_protection(kn
);
1352 cgroup_lock_and_drain_offline(cgrp
);
1354 mutex_lock(&cgroup_mutex
);
1356 if (!cgroup_is_dead(cgrp
))
1359 cgroup_kn_unlock(kn
);
1363 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
1365 char name
[CGROUP_FILE_NAME_MAX
];
1367 lockdep_assert_held(&cgroup_mutex
);
1369 if (cft
->file_offset
) {
1370 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, cft
->ss
);
1371 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
1373 spin_lock_irq(&cgroup_file_kn_lock
);
1375 spin_unlock_irq(&cgroup_file_kn_lock
);
1378 kernfs_remove_by_name(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
));
1382 * css_clear_dir - remove subsys files in a cgroup directory
1385 static void css_clear_dir(struct cgroup_subsys_state
*css
)
1387 struct cgroup
*cgrp
= css
->cgroup
;
1388 struct cftype
*cfts
;
1390 if (!(css
->flags
& CSS_VISIBLE
))
1393 css
->flags
&= ~CSS_VISIBLE
;
1395 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
)
1396 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1400 * css_populate_dir - create subsys files in a cgroup directory
1403 * On failure, no file is added.
1405 static int css_populate_dir(struct cgroup_subsys_state
*css
)
1407 struct cgroup
*cgrp
= css
->cgroup
;
1408 struct cftype
*cfts
, *failed_cfts
;
1411 if ((css
->flags
& CSS_VISIBLE
) || !cgrp
->kn
)
1415 if (cgroup_on_dfl(cgrp
))
1416 cfts
= cgroup_base_files
;
1418 cfts
= cgroup1_base_files
;
1420 return cgroup_addrm_files(&cgrp
->self
, cgrp
, cfts
, true);
1423 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1424 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, true);
1431 css
->flags
|= CSS_VISIBLE
;
1435 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1436 if (cfts
== failed_cfts
)
1438 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1443 int rebind_subsystems(struct cgroup_root
*dst_root
, u16 ss_mask
)
1445 struct cgroup
*dcgrp
= &dst_root
->cgrp
;
1446 struct cgroup_subsys
*ss
;
1449 lockdep_assert_held(&cgroup_mutex
);
1451 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
1453 * If @ss has non-root csses attached to it, can't move.
1454 * If @ss is an implicit controller, it is exempt from this
1455 * rule and can be stolen.
1457 if (css_next_child(NULL
, cgroup_css(&ss
->root
->cgrp
, ss
)) &&
1458 !ss
->implicit_on_dfl
)
1461 /* can't move between two non-dummy roots either */
1462 if (ss
->root
!= &cgrp_dfl_root
&& dst_root
!= &cgrp_dfl_root
)
1464 } while_each_subsys_mask();
1466 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
1467 struct cgroup_root
*src_root
= ss
->root
;
1468 struct cgroup
*scgrp
= &src_root
->cgrp
;
1469 struct cgroup_subsys_state
*css
= cgroup_css(scgrp
, ss
);
1470 struct css_set
*cset
;
1472 WARN_ON(!css
|| cgroup_css(dcgrp
, ss
));
1474 /* disable from the source */
1475 src_root
->subsys_mask
&= ~(1 << ssid
);
1476 WARN_ON(cgroup_apply_control(scgrp
));
1477 cgroup_finalize_control(scgrp
, 0);
1480 RCU_INIT_POINTER(scgrp
->subsys
[ssid
], NULL
);
1481 rcu_assign_pointer(dcgrp
->subsys
[ssid
], css
);
1482 ss
->root
= dst_root
;
1483 css
->cgroup
= dcgrp
;
1485 spin_lock_irq(&css_set_lock
);
1486 hash_for_each(css_set_table
, i
, cset
, hlist
)
1487 list_move_tail(&cset
->e_cset_node
[ss
->id
],
1488 &dcgrp
->e_csets
[ss
->id
]);
1489 spin_unlock_irq(&css_set_lock
);
1491 /* default hierarchy doesn't enable controllers by default */
1492 dst_root
->subsys_mask
|= 1 << ssid
;
1493 if (dst_root
== &cgrp_dfl_root
) {
1494 static_branch_enable(cgroup_subsys_on_dfl_key
[ssid
]);
1496 dcgrp
->subtree_control
|= 1 << ssid
;
1497 static_branch_disable(cgroup_subsys_on_dfl_key
[ssid
]);
1500 ret
= cgroup_apply_control(dcgrp
);
1502 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1507 } while_each_subsys_mask();
1509 kernfs_activate(dcgrp
->kn
);
1513 int cgroup_show_path(struct seq_file
*sf
, struct kernfs_node
*kf_node
,
1514 struct kernfs_root
*kf_root
)
1518 struct cgroup_root
*kf_cgroot
= cgroup_root_from_kf(kf_root
);
1519 struct cgroup
*ns_cgroup
;
1521 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1525 spin_lock_irq(&css_set_lock
);
1526 ns_cgroup
= current_cgns_cgroup_from_root(kf_cgroot
);
1527 len
= kernfs_path_from_node(kf_node
, ns_cgroup
->kn
, buf
, PATH_MAX
);
1528 spin_unlock_irq(&css_set_lock
);
1530 if (len
>= PATH_MAX
)
1533 seq_escape(sf
, buf
, " \t\n\\");
1540 static int cgroup_remount(struct kernfs_root
*kf_root
, int *flags
, char *data
)
1542 pr_err("remount is not allowed\n");
1547 * To reduce the fork() overhead for systems that are not actually using
1548 * their cgroups capability, we don't maintain the lists running through
1549 * each css_set to its tasks until we see the list actually used - in other
1550 * words after the first mount.
1552 static bool use_task_css_set_links __read_mostly
;
1554 static void cgroup_enable_task_cg_lists(void)
1556 struct task_struct
*p
, *g
;
1558 spin_lock_irq(&css_set_lock
);
1560 if (use_task_css_set_links
)
1563 use_task_css_set_links
= true;
1566 * We need tasklist_lock because RCU is not safe against
1567 * while_each_thread(). Besides, a forking task that has passed
1568 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1569 * is not guaranteed to have its child immediately visible in the
1570 * tasklist if we walk through it with RCU.
1572 read_lock(&tasklist_lock
);
1573 do_each_thread(g
, p
) {
1574 WARN_ON_ONCE(!list_empty(&p
->cg_list
) ||
1575 task_css_set(p
) != &init_css_set
);
1578 * We should check if the process is exiting, otherwise
1579 * it will race with cgroup_exit() in that the list
1580 * entry won't be deleted though the process has exited.
1581 * Do it while holding siglock so that we don't end up
1582 * racing against cgroup_exit().
1584 * Interrupts were already disabled while acquiring
1585 * the css_set_lock, so we do not need to disable it
1586 * again when acquiring the sighand->siglock here.
1588 spin_lock(&p
->sighand
->siglock
);
1589 if (!(p
->flags
& PF_EXITING
)) {
1590 struct css_set
*cset
= task_css_set(p
);
1592 if (!css_set_populated(cset
))
1593 css_set_update_populated(cset
, true);
1594 list_add_tail(&p
->cg_list
, &cset
->tasks
);
1597 spin_unlock(&p
->sighand
->siglock
);
1598 } while_each_thread(g
, p
);
1599 read_unlock(&tasklist_lock
);
1601 spin_unlock_irq(&css_set_lock
);
1604 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1606 struct cgroup_subsys
*ss
;
1609 INIT_LIST_HEAD(&cgrp
->self
.sibling
);
1610 INIT_LIST_HEAD(&cgrp
->self
.children
);
1611 INIT_LIST_HEAD(&cgrp
->cset_links
);
1612 INIT_LIST_HEAD(&cgrp
->pidlists
);
1613 mutex_init(&cgrp
->pidlist_mutex
);
1614 cgrp
->self
.cgroup
= cgrp
;
1615 cgrp
->self
.flags
|= CSS_ONLINE
;
1617 for_each_subsys(ss
, ssid
)
1618 INIT_LIST_HEAD(&cgrp
->e_csets
[ssid
]);
1620 init_waitqueue_head(&cgrp
->offline_waitq
);
1621 INIT_WORK(&cgrp
->release_agent_work
, cgroup1_release_agent
);
1624 void init_cgroup_root(struct cgroup_root
*root
, struct cgroup_sb_opts
*opts
)
1626 struct cgroup
*cgrp
= &root
->cgrp
;
1628 INIT_LIST_HEAD(&root
->root_list
);
1629 atomic_set(&root
->nr_cgrps
, 1);
1631 init_cgroup_housekeeping(cgrp
);
1632 idr_init(&root
->cgroup_idr
);
1634 root
->flags
= opts
->flags
;
1635 if (opts
->release_agent
)
1636 strcpy(root
->release_agent_path
, opts
->release_agent
);
1638 strcpy(root
->name
, opts
->name
);
1639 if (opts
->cpuset_clone_children
)
1640 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->cgrp
.flags
);
1643 int cgroup_setup_root(struct cgroup_root
*root
, u16 ss_mask
)
1645 LIST_HEAD(tmp_links
);
1646 struct cgroup
*root_cgrp
= &root
->cgrp
;
1647 struct kernfs_syscall_ops
*kf_sops
;
1648 struct css_set
*cset
;
1651 lockdep_assert_held(&cgroup_mutex
);
1653 ret
= cgroup_idr_alloc(&root
->cgroup_idr
, root_cgrp
, 1, 2, GFP_KERNEL
);
1656 root_cgrp
->id
= ret
;
1657 root_cgrp
->ancestor_ids
[0] = ret
;
1659 ret
= percpu_ref_init(&root_cgrp
->self
.refcnt
, css_release
, 0,
1665 * We're accessing css_set_count without locking css_set_lock here,
1666 * but that's OK - it can only be increased by someone holding
1667 * cgroup_lock, and that's us. Later rebinding may disable
1668 * controllers on the default hierarchy and thus create new csets,
1669 * which can't be more than the existing ones. Allocate 2x.
1671 ret
= allocate_cgrp_cset_links(2 * css_set_count
, &tmp_links
);
1675 ret
= cgroup_init_root_id(root
);
1679 kf_sops
= root
== &cgrp_dfl_root
?
1680 &cgroup_kf_syscall_ops
: &cgroup1_kf_syscall_ops
;
1682 root
->kf_root
= kernfs_create_root(kf_sops
,
1683 KERNFS_ROOT_CREATE_DEACTIVATED
,
1685 if (IS_ERR(root
->kf_root
)) {
1686 ret
= PTR_ERR(root
->kf_root
);
1689 root_cgrp
->kn
= root
->kf_root
->kn
;
1691 ret
= css_populate_dir(&root_cgrp
->self
);
1695 ret
= rebind_subsystems(root
, ss_mask
);
1699 trace_cgroup_setup_root(root
);
1702 * There must be no failure case after here, since rebinding takes
1703 * care of subsystems' refcounts, which are explicitly dropped in
1704 * the failure exit path.
1706 list_add(&root
->root_list
, &cgroup_roots
);
1707 cgroup_root_count
++;
1710 * Link the root cgroup in this hierarchy into all the css_set
1713 spin_lock_irq(&css_set_lock
);
1714 hash_for_each(css_set_table
, i
, cset
, hlist
) {
1715 link_css_set(&tmp_links
, cset
, root_cgrp
);
1716 if (css_set_populated(cset
))
1717 cgroup_update_populated(root_cgrp
, true);
1719 spin_unlock_irq(&css_set_lock
);
1721 BUG_ON(!list_empty(&root_cgrp
->self
.children
));
1722 BUG_ON(atomic_read(&root
->nr_cgrps
) != 1);
1724 kernfs_activate(root_cgrp
->kn
);
1729 kernfs_destroy_root(root
->kf_root
);
1730 root
->kf_root
= NULL
;
1732 cgroup_exit_root_id(root
);
1734 percpu_ref_exit(&root_cgrp
->self
.refcnt
);
1736 free_cgrp_cset_links(&tmp_links
);
1740 struct dentry
*cgroup_do_mount(struct file_system_type
*fs_type
, int flags
,
1741 struct cgroup_root
*root
, unsigned long magic
,
1742 struct cgroup_namespace
*ns
)
1744 struct dentry
*dentry
;
1747 dentry
= kernfs_mount(fs_type
, flags
, root
->kf_root
, magic
, &new_sb
);
1750 * In non-init cgroup namespace, instead of root cgroup's dentry,
1751 * we return the dentry corresponding to the cgroupns->root_cgrp.
1753 if (!IS_ERR(dentry
) && ns
!= &init_cgroup_ns
) {
1754 struct dentry
*nsdentry
;
1755 struct cgroup
*cgrp
;
1757 mutex_lock(&cgroup_mutex
);
1758 spin_lock_irq(&css_set_lock
);
1760 cgrp
= cset_cgroup_from_root(ns
->root_cset
, root
);
1762 spin_unlock_irq(&css_set_lock
);
1763 mutex_unlock(&cgroup_mutex
);
1765 nsdentry
= kernfs_node_dentry(cgrp
->kn
, dentry
->d_sb
);
1770 if (IS_ERR(dentry
) || !new_sb
)
1771 cgroup_put(&root
->cgrp
);
1776 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1777 int flags
, const char *unused_dev_name
,
1780 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
1781 struct dentry
*dentry
;
1785 /* Check if the caller has permission to mount. */
1786 if (!ns_capable(ns
->user_ns
, CAP_SYS_ADMIN
)) {
1788 return ERR_PTR(-EPERM
);
1792 * The first time anyone tries to mount a cgroup, enable the list
1793 * linking each css_set to its tasks and fix up all existing tasks.
1795 if (!use_task_css_set_links
)
1796 cgroup_enable_task_cg_lists();
1798 if (fs_type
== &cgroup2_fs_type
) {
1800 pr_err("cgroup2: unknown option \"%s\"\n", (char *)data
);
1802 return ERR_PTR(-EINVAL
);
1804 cgrp_dfl_visible
= true;
1805 cgroup_get(&cgrp_dfl_root
.cgrp
);
1807 dentry
= cgroup_do_mount(&cgroup2_fs_type
, flags
, &cgrp_dfl_root
,
1808 CGROUP2_SUPER_MAGIC
, ns
);
1810 dentry
= cgroup1_mount(&cgroup_fs_type
, flags
, data
,
1811 CGROUP_SUPER_MAGIC
, ns
);
1818 static void cgroup_kill_sb(struct super_block
*sb
)
1820 struct kernfs_root
*kf_root
= kernfs_root_from_sb(sb
);
1821 struct cgroup_root
*root
= cgroup_root_from_kf(kf_root
);
1824 * If @root doesn't have any mounts or children, start killing it.
1825 * This prevents new mounts by disabling percpu_ref_tryget_live().
1826 * cgroup_mount() may wait for @root's release.
1828 * And don't kill the default root.
1830 if (!list_empty(&root
->cgrp
.self
.children
) ||
1831 root
== &cgrp_dfl_root
)
1832 cgroup_put(&root
->cgrp
);
1834 percpu_ref_kill(&root
->cgrp
.self
.refcnt
);
1839 struct file_system_type cgroup_fs_type
= {
1841 .mount
= cgroup_mount
,
1842 .kill_sb
= cgroup_kill_sb
,
1843 .fs_flags
= FS_USERNS_MOUNT
,
1846 static struct file_system_type cgroup2_fs_type
= {
1848 .mount
= cgroup_mount
,
1849 .kill_sb
= cgroup_kill_sb
,
1850 .fs_flags
= FS_USERNS_MOUNT
,
1853 int cgroup_path_ns_locked(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
1854 struct cgroup_namespace
*ns
)
1856 struct cgroup
*root
= cset_cgroup_from_root(ns
->root_cset
, cgrp
->root
);
1858 return kernfs_path_from_node(cgrp
->kn
, root
->kn
, buf
, buflen
);
1861 int cgroup_path_ns(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
1862 struct cgroup_namespace
*ns
)
1866 mutex_lock(&cgroup_mutex
);
1867 spin_lock_irq(&css_set_lock
);
1869 ret
= cgroup_path_ns_locked(cgrp
, buf
, buflen
, ns
);
1871 spin_unlock_irq(&css_set_lock
);
1872 mutex_unlock(&cgroup_mutex
);
1876 EXPORT_SYMBOL_GPL(cgroup_path_ns
);
1879 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1880 * @task: target task
1881 * @buf: the buffer to write the path into
1882 * @buflen: the length of the buffer
1884 * Determine @task's cgroup on the first (the one with the lowest non-zero
1885 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1886 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1887 * cgroup controller callbacks.
1889 * Return value is the same as kernfs_path().
1891 int task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
1893 struct cgroup_root
*root
;
1894 struct cgroup
*cgrp
;
1895 int hierarchy_id
= 1;
1898 mutex_lock(&cgroup_mutex
);
1899 spin_lock_irq(&css_set_lock
);
1901 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
1904 cgrp
= task_cgroup_from_root(task
, root
);
1905 ret
= cgroup_path_ns_locked(cgrp
, buf
, buflen
, &init_cgroup_ns
);
1907 /* if no hierarchy exists, everyone is in "/" */
1908 ret
= strlcpy(buf
, "/", buflen
);
1911 spin_unlock_irq(&css_set_lock
);
1912 mutex_unlock(&cgroup_mutex
);
1915 EXPORT_SYMBOL_GPL(task_cgroup_path
);
1918 * cgroup_migrate_add_task - add a migration target task to a migration context
1919 * @task: target task
1920 * @mgctx: target migration context
1922 * Add @task, which is a migration target, to @mgctx->tset. This function
1923 * becomes noop if @task doesn't need to be migrated. @task's css_set
1924 * should have been added as a migration source and @task->cg_list will be
1925 * moved from the css_set's tasks list to mg_tasks one.
1927 static void cgroup_migrate_add_task(struct task_struct
*task
,
1928 struct cgroup_mgctx
*mgctx
)
1930 struct css_set
*cset
;
1932 lockdep_assert_held(&css_set_lock
);
1934 /* @task either already exited or can't exit until the end */
1935 if (task
->flags
& PF_EXITING
)
1938 /* leave @task alone if post_fork() hasn't linked it yet */
1939 if (list_empty(&task
->cg_list
))
1942 cset
= task_css_set(task
);
1943 if (!cset
->mg_src_cgrp
)
1946 list_move_tail(&task
->cg_list
, &cset
->mg_tasks
);
1947 if (list_empty(&cset
->mg_node
))
1948 list_add_tail(&cset
->mg_node
,
1949 &mgctx
->tset
.src_csets
);
1950 if (list_empty(&cset
->mg_dst_cset
->mg_node
))
1951 list_add_tail(&cset
->mg_dst_cset
->mg_node
,
1952 &mgctx
->tset
.dst_csets
);
1956 * cgroup_taskset_first - reset taskset and return the first task
1957 * @tset: taskset of interest
1958 * @dst_cssp: output variable for the destination css
1960 * @tset iteration is initialized and the first task is returned.
1962 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
,
1963 struct cgroup_subsys_state
**dst_cssp
)
1965 tset
->cur_cset
= list_first_entry(tset
->csets
, struct css_set
, mg_node
);
1966 tset
->cur_task
= NULL
;
1968 return cgroup_taskset_next(tset
, dst_cssp
);
1972 * cgroup_taskset_next - iterate to the next task in taskset
1973 * @tset: taskset of interest
1974 * @dst_cssp: output variable for the destination css
1976 * Return the next task in @tset. Iteration must have been initialized
1977 * with cgroup_taskset_first().
1979 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
,
1980 struct cgroup_subsys_state
**dst_cssp
)
1982 struct css_set
*cset
= tset
->cur_cset
;
1983 struct task_struct
*task
= tset
->cur_task
;
1985 while (&cset
->mg_node
!= tset
->csets
) {
1987 task
= list_first_entry(&cset
->mg_tasks
,
1988 struct task_struct
, cg_list
);
1990 task
= list_next_entry(task
, cg_list
);
1992 if (&task
->cg_list
!= &cset
->mg_tasks
) {
1993 tset
->cur_cset
= cset
;
1994 tset
->cur_task
= task
;
1997 * This function may be called both before and
1998 * after cgroup_taskset_migrate(). The two cases
1999 * can be distinguished by looking at whether @cset
2000 * has its ->mg_dst_cset set.
2002 if (cset
->mg_dst_cset
)
2003 *dst_cssp
= cset
->mg_dst_cset
->subsys
[tset
->ssid
];
2005 *dst_cssp
= cset
->subsys
[tset
->ssid
];
2010 cset
= list_next_entry(cset
, mg_node
);
2018 * cgroup_taskset_migrate - migrate a taskset
2019 * @mgctx: migration context
2021 * Migrate tasks in @mgctx as setup by migration preparation functions.
2022 * This function fails iff one of the ->can_attach callbacks fails and
2023 * guarantees that either all or none of the tasks in @mgctx are migrated.
2024 * @mgctx is consumed regardless of success.
2026 static int cgroup_migrate_execute(struct cgroup_mgctx
*mgctx
)
2028 struct cgroup_taskset
*tset
= &mgctx
->tset
;
2029 struct cgroup_subsys
*ss
;
2030 struct task_struct
*task
, *tmp_task
;
2031 struct css_set
*cset
, *tmp_cset
;
2032 int ssid
, failed_ssid
, ret
;
2034 /* methods shouldn't be called if no task is actually migrating */
2035 if (list_empty(&tset
->src_csets
))
2038 /* check that we can legitimately attach to the cgroup */
2039 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2040 if (ss
->can_attach
) {
2042 ret
= ss
->can_attach(tset
);
2045 goto out_cancel_attach
;
2048 } while_each_subsys_mask();
2051 * Now that we're guaranteed success, proceed to move all tasks to
2052 * the new cgroup. There are no failure cases after here, so this
2053 * is the commit point.
2055 spin_lock_irq(&css_set_lock
);
2056 list_for_each_entry(cset
, &tset
->src_csets
, mg_node
) {
2057 list_for_each_entry_safe(task
, tmp_task
, &cset
->mg_tasks
, cg_list
) {
2058 struct css_set
*from_cset
= task_css_set(task
);
2059 struct css_set
*to_cset
= cset
->mg_dst_cset
;
2061 get_css_set(to_cset
);
2062 css_set_move_task(task
, from_cset
, to_cset
, true);
2063 put_css_set_locked(from_cset
);
2066 spin_unlock_irq(&css_set_lock
);
2069 * Migration is committed, all target tasks are now on dst_csets.
2070 * Nothing is sensitive to fork() after this point. Notify
2071 * controllers that migration is complete.
2073 tset
->csets
= &tset
->dst_csets
;
2075 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2080 } while_each_subsys_mask();
2083 goto out_release_tset
;
2086 do_each_subsys_mask(ss
, ssid
, mgctx
->ss_mask
) {
2087 if (ssid
== failed_ssid
)
2089 if (ss
->cancel_attach
) {
2091 ss
->cancel_attach(tset
);
2093 } while_each_subsys_mask();
2095 spin_lock_irq(&css_set_lock
);
2096 list_splice_init(&tset
->dst_csets
, &tset
->src_csets
);
2097 list_for_each_entry_safe(cset
, tmp_cset
, &tset
->src_csets
, mg_node
) {
2098 list_splice_tail_init(&cset
->mg_tasks
, &cset
->tasks
);
2099 list_del_init(&cset
->mg_node
);
2101 spin_unlock_irq(&css_set_lock
);
2106 * cgroup_may_migrate_to - verify whether a cgroup can be migration destination
2107 * @dst_cgrp: destination cgroup to test
2109 * On the default hierarchy, except for the root, subtree_control must be
2110 * zero for migration destination cgroups with tasks so that child cgroups
2111 * don't compete against tasks.
2113 bool cgroup_may_migrate_to(struct cgroup
*dst_cgrp
)
2115 return !cgroup_on_dfl(dst_cgrp
) || !cgroup_parent(dst_cgrp
) ||
2116 !dst_cgrp
->subtree_control
;
2120 * cgroup_migrate_finish - cleanup after attach
2121 * @mgctx: migration context
2123 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2124 * those functions for details.
2126 void cgroup_migrate_finish(struct cgroup_mgctx
*mgctx
)
2128 LIST_HEAD(preloaded
);
2129 struct css_set
*cset
, *tmp_cset
;
2131 lockdep_assert_held(&cgroup_mutex
);
2133 spin_lock_irq(&css_set_lock
);
2135 list_splice_tail_init(&mgctx
->preloaded_src_csets
, &preloaded
);
2136 list_splice_tail_init(&mgctx
->preloaded_dst_csets
, &preloaded
);
2138 list_for_each_entry_safe(cset
, tmp_cset
, &preloaded
, mg_preload_node
) {
2139 cset
->mg_src_cgrp
= NULL
;
2140 cset
->mg_dst_cgrp
= NULL
;
2141 cset
->mg_dst_cset
= NULL
;
2142 list_del_init(&cset
->mg_preload_node
);
2143 put_css_set_locked(cset
);
2146 spin_unlock_irq(&css_set_lock
);
2150 * cgroup_migrate_add_src - add a migration source css_set
2151 * @src_cset: the source css_set to add
2152 * @dst_cgrp: the destination cgroup
2153 * @mgctx: migration context
2155 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2156 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
2157 * up by cgroup_migrate_finish().
2159 * This function may be called without holding cgroup_threadgroup_rwsem
2160 * even if the target is a process. Threads may be created and destroyed
2161 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2162 * into play and the preloaded css_sets are guaranteed to cover all
2165 void cgroup_migrate_add_src(struct css_set
*src_cset
,
2166 struct cgroup
*dst_cgrp
,
2167 struct cgroup_mgctx
*mgctx
)
2169 struct cgroup
*src_cgrp
;
2171 lockdep_assert_held(&cgroup_mutex
);
2172 lockdep_assert_held(&css_set_lock
);
2175 * If ->dead, @src_set is associated with one or more dead cgroups
2176 * and doesn't contain any migratable tasks. Ignore it early so
2177 * that the rest of migration path doesn't get confused by it.
2182 src_cgrp
= cset_cgroup_from_root(src_cset
, dst_cgrp
->root
);
2184 if (!list_empty(&src_cset
->mg_preload_node
))
2187 WARN_ON(src_cset
->mg_src_cgrp
);
2188 WARN_ON(src_cset
->mg_dst_cgrp
);
2189 WARN_ON(!list_empty(&src_cset
->mg_tasks
));
2190 WARN_ON(!list_empty(&src_cset
->mg_node
));
2192 src_cset
->mg_src_cgrp
= src_cgrp
;
2193 src_cset
->mg_dst_cgrp
= dst_cgrp
;
2194 get_css_set(src_cset
);
2195 list_add_tail(&src_cset
->mg_preload_node
, &mgctx
->preloaded_src_csets
);
2199 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
2200 * @mgctx: migration context
2202 * Tasks are about to be moved and all the source css_sets have been
2203 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2204 * pins all destination css_sets, links each to its source, and append them
2205 * to @mgctx->preloaded_dst_csets.
2207 * This function must be called after cgroup_migrate_add_src() has been
2208 * called on each migration source css_set. After migration is performed
2209 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2212 int cgroup_migrate_prepare_dst(struct cgroup_mgctx
*mgctx
)
2214 struct css_set
*src_cset
, *tmp_cset
;
2216 lockdep_assert_held(&cgroup_mutex
);
2218 /* look up the dst cset for each src cset and link it to src */
2219 list_for_each_entry_safe(src_cset
, tmp_cset
, &mgctx
->preloaded_src_csets
,
2221 struct css_set
*dst_cset
;
2222 struct cgroup_subsys
*ss
;
2225 dst_cset
= find_css_set(src_cset
, src_cset
->mg_dst_cgrp
);
2229 WARN_ON_ONCE(src_cset
->mg_dst_cset
|| dst_cset
->mg_dst_cset
);
2232 * If src cset equals dst, it's noop. Drop the src.
2233 * cgroup_migrate() will skip the cset too. Note that we
2234 * can't handle src == dst as some nodes are used by both.
2236 if (src_cset
== dst_cset
) {
2237 src_cset
->mg_src_cgrp
= NULL
;
2238 src_cset
->mg_dst_cgrp
= NULL
;
2239 list_del_init(&src_cset
->mg_preload_node
);
2240 put_css_set(src_cset
);
2241 put_css_set(dst_cset
);
2245 src_cset
->mg_dst_cset
= dst_cset
;
2247 if (list_empty(&dst_cset
->mg_preload_node
))
2248 list_add_tail(&dst_cset
->mg_preload_node
,
2249 &mgctx
->preloaded_dst_csets
);
2251 put_css_set(dst_cset
);
2253 for_each_subsys(ss
, ssid
)
2254 if (src_cset
->subsys
[ssid
] != dst_cset
->subsys
[ssid
])
2255 mgctx
->ss_mask
|= 1 << ssid
;
2260 cgroup_migrate_finish(mgctx
);
2265 * cgroup_migrate - migrate a process or task to a cgroup
2266 * @leader: the leader of the process or the task to migrate
2267 * @threadgroup: whether @leader points to the whole process or a single task
2268 * @mgctx: migration context
2270 * Migrate a process or task denoted by @leader. If migrating a process,
2271 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2272 * responsible for invoking cgroup_migrate_add_src() and
2273 * cgroup_migrate_prepare_dst() on the targets before invoking this
2274 * function and following up with cgroup_migrate_finish().
2276 * As long as a controller's ->can_attach() doesn't fail, this function is
2277 * guaranteed to succeed. This means that, excluding ->can_attach()
2278 * failure, when migrating multiple targets, the success or failure can be
2279 * decided for all targets by invoking group_migrate_prepare_dst() before
2280 * actually starting migrating.
2282 int cgroup_migrate(struct task_struct
*leader
, bool threadgroup
,
2283 struct cgroup_mgctx
*mgctx
)
2285 struct task_struct
*task
;
2288 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2289 * already PF_EXITING could be freed from underneath us unless we
2290 * take an rcu_read_lock.
2292 spin_lock_irq(&css_set_lock
);
2296 cgroup_migrate_add_task(task
, mgctx
);
2299 } while_each_thread(leader
, task
);
2301 spin_unlock_irq(&css_set_lock
);
2303 return cgroup_migrate_execute(mgctx
);
2307 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2308 * @dst_cgrp: the cgroup to attach to
2309 * @leader: the task or the leader of the threadgroup to be attached
2310 * @threadgroup: attach the whole threadgroup?
2312 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
2314 int cgroup_attach_task(struct cgroup
*dst_cgrp
, struct task_struct
*leader
,
2317 DEFINE_CGROUP_MGCTX(mgctx
);
2318 struct task_struct
*task
;
2321 if (!cgroup_may_migrate_to(dst_cgrp
))
2324 /* look up all src csets */
2325 spin_lock_irq(&css_set_lock
);
2329 cgroup_migrate_add_src(task_css_set(task
), dst_cgrp
, &mgctx
);
2332 } while_each_thread(leader
, task
);
2334 spin_unlock_irq(&css_set_lock
);
2336 /* prepare dst csets and commit */
2337 ret
= cgroup_migrate_prepare_dst(&mgctx
);
2339 ret
= cgroup_migrate(leader
, threadgroup
, &mgctx
);
2341 cgroup_migrate_finish(&mgctx
);
2344 trace_cgroup_attach_task(dst_cgrp
, leader
, threadgroup
);
2349 static int cgroup_procs_write_permission(struct task_struct
*task
,
2350 struct cgroup
*dst_cgrp
,
2351 struct kernfs_open_file
*of
)
2355 if (cgroup_on_dfl(dst_cgrp
)) {
2356 struct super_block
*sb
= of
->file
->f_path
.dentry
->d_sb
;
2357 struct cgroup
*cgrp
;
2358 struct inode
*inode
;
2360 spin_lock_irq(&css_set_lock
);
2361 cgrp
= task_cgroup_from_root(task
, &cgrp_dfl_root
);
2362 spin_unlock_irq(&css_set_lock
);
2364 while (!cgroup_is_descendant(dst_cgrp
, cgrp
))
2365 cgrp
= cgroup_parent(cgrp
);
2368 inode
= kernfs_get_inode(sb
, cgrp
->procs_file
.kn
);
2370 ret
= inode_permission(inode
, MAY_WRITE
);
2374 const struct cred
*cred
= current_cred();
2375 const struct cred
*tcred
= get_task_cred(task
);
2378 * even if we're attaching all tasks in the thread group,
2379 * we only need to check permissions on one of them.
2381 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2382 !uid_eq(cred
->euid
, tcred
->uid
) &&
2383 !uid_eq(cred
->euid
, tcred
->suid
))
2392 * Find the task_struct of the task to attach by vpid and pass it along to the
2393 * function to attach either it or all tasks in its threadgroup. Will lock
2394 * cgroup_mutex and threadgroup.
2396 ssize_t
__cgroup_procs_write(struct kernfs_open_file
*of
, char *buf
,
2397 size_t nbytes
, loff_t off
, bool threadgroup
)
2399 struct task_struct
*tsk
;
2400 struct cgroup_subsys
*ss
;
2401 struct cgroup
*cgrp
;
2405 if (kstrtoint(strstrip(buf
), 0, &pid
) || pid
< 0)
2408 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
2412 percpu_down_write(&cgroup_threadgroup_rwsem
);
2415 tsk
= find_task_by_vpid(pid
);
2418 goto out_unlock_rcu
;
2425 tsk
= tsk
->group_leader
;
2428 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2429 * trapped in a cpuset, or RT worker may be born in a cgroup
2430 * with no rt_runtime allocated. Just say no.
2432 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2434 goto out_unlock_rcu
;
2437 get_task_struct(tsk
);
2440 ret
= cgroup_procs_write_permission(tsk
, cgrp
, of
);
2442 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2444 put_task_struct(tsk
);
2445 goto out_unlock_threadgroup
;
2449 out_unlock_threadgroup
:
2450 percpu_up_write(&cgroup_threadgroup_rwsem
);
2451 for_each_subsys(ss
, ssid
)
2452 if (ss
->post_attach
)
2454 cgroup_kn_unlock(of
->kn
);
2455 return ret
?: nbytes
;
2458 ssize_t
cgroup_procs_write(struct kernfs_open_file
*of
, char *buf
, size_t nbytes
,
2461 return __cgroup_procs_write(of
, buf
, nbytes
, off
, true);
2464 static void cgroup_print_ss_mask(struct seq_file
*seq
, u16 ss_mask
)
2466 struct cgroup_subsys
*ss
;
2467 bool printed
= false;
2470 do_each_subsys_mask(ss
, ssid
, ss_mask
) {
2473 seq_printf(seq
, "%s", ss
->name
);
2475 } while_each_subsys_mask();
2477 seq_putc(seq
, '\n');
2480 /* show controllers which are enabled from the parent */
2481 static int cgroup_controllers_show(struct seq_file
*seq
, void *v
)
2483 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2485 cgroup_print_ss_mask(seq
, cgroup_control(cgrp
));
2489 /* show controllers which are enabled for a given cgroup's children */
2490 static int cgroup_subtree_control_show(struct seq_file
*seq
, void *v
)
2492 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2494 cgroup_print_ss_mask(seq
, cgrp
->subtree_control
);
2499 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2500 * @cgrp: root of the subtree to update csses for
2502 * @cgrp's control masks have changed and its subtree's css associations
2503 * need to be updated accordingly. This function looks up all css_sets
2504 * which are attached to the subtree, creates the matching updated css_sets
2505 * and migrates the tasks to the new ones.
2507 static int cgroup_update_dfl_csses(struct cgroup
*cgrp
)
2509 DEFINE_CGROUP_MGCTX(mgctx
);
2510 struct cgroup_subsys_state
*d_css
;
2511 struct cgroup
*dsct
;
2512 struct css_set
*src_cset
;
2515 lockdep_assert_held(&cgroup_mutex
);
2517 percpu_down_write(&cgroup_threadgroup_rwsem
);
2519 /* look up all csses currently attached to @cgrp's subtree */
2520 spin_lock_irq(&css_set_lock
);
2521 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2522 struct cgrp_cset_link
*link
;
2524 list_for_each_entry(link
, &dsct
->cset_links
, cset_link
)
2525 cgroup_migrate_add_src(link
->cset
, dsct
, &mgctx
);
2527 spin_unlock_irq(&css_set_lock
);
2529 /* NULL dst indicates self on default hierarchy */
2530 ret
= cgroup_migrate_prepare_dst(&mgctx
);
2534 spin_lock_irq(&css_set_lock
);
2535 list_for_each_entry(src_cset
, &mgctx
.preloaded_src_csets
, mg_preload_node
) {
2536 struct task_struct
*task
, *ntask
;
2538 /* all tasks in src_csets need to be migrated */
2539 list_for_each_entry_safe(task
, ntask
, &src_cset
->tasks
, cg_list
)
2540 cgroup_migrate_add_task(task
, &mgctx
);
2542 spin_unlock_irq(&css_set_lock
);
2544 ret
= cgroup_migrate_execute(&mgctx
);
2546 cgroup_migrate_finish(&mgctx
);
2547 percpu_up_write(&cgroup_threadgroup_rwsem
);
2552 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
2553 * @cgrp: root of the target subtree
2555 * Because css offlining is asynchronous, userland may try to re-enable a
2556 * controller while the previous css is still around. This function grabs
2557 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
2559 void cgroup_lock_and_drain_offline(struct cgroup
*cgrp
)
2560 __acquires(&cgroup_mutex
)
2562 struct cgroup
*dsct
;
2563 struct cgroup_subsys_state
*d_css
;
2564 struct cgroup_subsys
*ss
;
2568 mutex_lock(&cgroup_mutex
);
2570 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2571 for_each_subsys(ss
, ssid
) {
2572 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2575 if (!css
|| !percpu_ref_is_dying(&css
->refcnt
))
2579 prepare_to_wait(&dsct
->offline_waitq
, &wait
,
2580 TASK_UNINTERRUPTIBLE
);
2582 mutex_unlock(&cgroup_mutex
);
2584 finish_wait(&dsct
->offline_waitq
, &wait
);
2593 * cgroup_save_control - save control masks of a subtree
2594 * @cgrp: root of the target subtree
2596 * Save ->subtree_control and ->subtree_ss_mask to the respective old_
2597 * prefixed fields for @cgrp's subtree including @cgrp itself.
2599 static void cgroup_save_control(struct cgroup
*cgrp
)
2601 struct cgroup
*dsct
;
2602 struct cgroup_subsys_state
*d_css
;
2604 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2605 dsct
->old_subtree_control
= dsct
->subtree_control
;
2606 dsct
->old_subtree_ss_mask
= dsct
->subtree_ss_mask
;
2611 * cgroup_propagate_control - refresh control masks of a subtree
2612 * @cgrp: root of the target subtree
2614 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2615 * ->subtree_control and propagate controller availability through the
2616 * subtree so that descendants don't have unavailable controllers enabled.
2618 static void cgroup_propagate_control(struct cgroup
*cgrp
)
2620 struct cgroup
*dsct
;
2621 struct cgroup_subsys_state
*d_css
;
2623 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2624 dsct
->subtree_control
&= cgroup_control(dsct
);
2625 dsct
->subtree_ss_mask
=
2626 cgroup_calc_subtree_ss_mask(dsct
->subtree_control
,
2627 cgroup_ss_mask(dsct
));
2632 * cgroup_restore_control - restore control masks of a subtree
2633 * @cgrp: root of the target subtree
2635 * Restore ->subtree_control and ->subtree_ss_mask from the respective old_
2636 * prefixed fields for @cgrp's subtree including @cgrp itself.
2638 static void cgroup_restore_control(struct cgroup
*cgrp
)
2640 struct cgroup
*dsct
;
2641 struct cgroup_subsys_state
*d_css
;
2643 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2644 dsct
->subtree_control
= dsct
->old_subtree_control
;
2645 dsct
->subtree_ss_mask
= dsct
->old_subtree_ss_mask
;
2649 static bool css_visible(struct cgroup_subsys_state
*css
)
2651 struct cgroup_subsys
*ss
= css
->ss
;
2652 struct cgroup
*cgrp
= css
->cgroup
;
2654 if (cgroup_control(cgrp
) & (1 << ss
->id
))
2656 if (!(cgroup_ss_mask(cgrp
) & (1 << ss
->id
)))
2658 return cgroup_on_dfl(cgrp
) && ss
->implicit_on_dfl
;
2662 * cgroup_apply_control_enable - enable or show csses according to control
2663 * @cgrp: root of the target subtree
2665 * Walk @cgrp's subtree and create new csses or make the existing ones
2666 * visible. A css is created invisible if it's being implicitly enabled
2667 * through dependency. An invisible css is made visible when the userland
2668 * explicitly enables it.
2670 * Returns 0 on success, -errno on failure. On failure, csses which have
2671 * been processed already aren't cleaned up. The caller is responsible for
2672 * cleaning up with cgroup_apply_control_disable().
2674 static int cgroup_apply_control_enable(struct cgroup
*cgrp
)
2676 struct cgroup
*dsct
;
2677 struct cgroup_subsys_state
*d_css
;
2678 struct cgroup_subsys
*ss
;
2681 cgroup_for_each_live_descendant_pre(dsct
, d_css
, cgrp
) {
2682 for_each_subsys(ss
, ssid
) {
2683 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2685 WARN_ON_ONCE(css
&& percpu_ref_is_dying(&css
->refcnt
));
2687 if (!(cgroup_ss_mask(dsct
) & (1 << ss
->id
)))
2691 css
= css_create(dsct
, ss
);
2693 return PTR_ERR(css
);
2696 if (css_visible(css
)) {
2697 ret
= css_populate_dir(css
);
2708 * cgroup_apply_control_disable - kill or hide csses according to control
2709 * @cgrp: root of the target subtree
2711 * Walk @cgrp's subtree and kill and hide csses so that they match
2712 * cgroup_ss_mask() and cgroup_visible_mask().
2714 * A css is hidden when the userland requests it to be disabled while other
2715 * subsystems are still depending on it. The css must not actively control
2716 * resources and be in the vanilla state if it's made visible again later.
2717 * Controllers which may be depended upon should provide ->css_reset() for
2720 static void cgroup_apply_control_disable(struct cgroup
*cgrp
)
2722 struct cgroup
*dsct
;
2723 struct cgroup_subsys_state
*d_css
;
2724 struct cgroup_subsys
*ss
;
2727 cgroup_for_each_live_descendant_post(dsct
, d_css
, cgrp
) {
2728 for_each_subsys(ss
, ssid
) {
2729 struct cgroup_subsys_state
*css
= cgroup_css(dsct
, ss
);
2731 WARN_ON_ONCE(css
&& percpu_ref_is_dying(&css
->refcnt
));
2737 !(cgroup_ss_mask(dsct
) & (1 << ss
->id
))) {
2739 } else if (!css_visible(css
)) {
2749 * cgroup_apply_control - apply control mask updates to the subtree
2750 * @cgrp: root of the target subtree
2752 * subsystems can be enabled and disabled in a subtree using the following
2755 * 1. Call cgroup_save_control() to stash the current state.
2756 * 2. Update ->subtree_control masks in the subtree as desired.
2757 * 3. Call cgroup_apply_control() to apply the changes.
2758 * 4. Optionally perform other related operations.
2759 * 5. Call cgroup_finalize_control() to finish up.
2761 * This function implements step 3 and propagates the mask changes
2762 * throughout @cgrp's subtree, updates csses accordingly and perform
2763 * process migrations.
2765 static int cgroup_apply_control(struct cgroup
*cgrp
)
2769 cgroup_propagate_control(cgrp
);
2771 ret
= cgroup_apply_control_enable(cgrp
);
2776 * At this point, cgroup_e_css() results reflect the new csses
2777 * making the following cgroup_update_dfl_csses() properly update
2778 * css associations of all tasks in the subtree.
2780 ret
= cgroup_update_dfl_csses(cgrp
);
2788 * cgroup_finalize_control - finalize control mask update
2789 * @cgrp: root of the target subtree
2790 * @ret: the result of the update
2792 * Finalize control mask update. See cgroup_apply_control() for more info.
2794 static void cgroup_finalize_control(struct cgroup
*cgrp
, int ret
)
2797 cgroup_restore_control(cgrp
);
2798 cgroup_propagate_control(cgrp
);
2801 cgroup_apply_control_disable(cgrp
);
2804 /* change the enabled child controllers for a cgroup in the default hierarchy */
2805 static ssize_t
cgroup_subtree_control_write(struct kernfs_open_file
*of
,
2806 char *buf
, size_t nbytes
,
2809 u16 enable
= 0, disable
= 0;
2810 struct cgroup
*cgrp
, *child
;
2811 struct cgroup_subsys
*ss
;
2816 * Parse input - space separated list of subsystem names prefixed
2817 * with either + or -.
2819 buf
= strstrip(buf
);
2820 while ((tok
= strsep(&buf
, " "))) {
2823 do_each_subsys_mask(ss
, ssid
, ~cgrp_dfl_inhibit_ss_mask
) {
2824 if (!cgroup_ssid_enabled(ssid
) ||
2825 strcmp(tok
+ 1, ss
->name
))
2829 enable
|= 1 << ssid
;
2830 disable
&= ~(1 << ssid
);
2831 } else if (*tok
== '-') {
2832 disable
|= 1 << ssid
;
2833 enable
&= ~(1 << ssid
);
2838 } while_each_subsys_mask();
2839 if (ssid
== CGROUP_SUBSYS_COUNT
)
2843 cgrp
= cgroup_kn_lock_live(of
->kn
, true);
2847 for_each_subsys(ss
, ssid
) {
2848 if (enable
& (1 << ssid
)) {
2849 if (cgrp
->subtree_control
& (1 << ssid
)) {
2850 enable
&= ~(1 << ssid
);
2854 if (!(cgroup_control(cgrp
) & (1 << ssid
))) {
2858 } else if (disable
& (1 << ssid
)) {
2859 if (!(cgrp
->subtree_control
& (1 << ssid
))) {
2860 disable
&= ~(1 << ssid
);
2864 /* a child has it enabled? */
2865 cgroup_for_each_live_child(child
, cgrp
) {
2866 if (child
->subtree_control
& (1 << ssid
)) {
2874 if (!enable
&& !disable
) {
2880 * Except for the root, subtree_control must be zero for a cgroup
2881 * with tasks so that child cgroups don't compete against tasks.
2883 if (enable
&& cgroup_parent(cgrp
)) {
2884 struct cgrp_cset_link
*link
;
2887 * Because namespaces pin csets too, @cgrp->cset_links
2888 * might not be empty even when @cgrp is empty. Walk and
2891 spin_lock_irq(&css_set_lock
);
2894 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
) {
2895 if (css_set_populated(link
->cset
)) {
2901 spin_unlock_irq(&css_set_lock
);
2907 /* save and update control masks and prepare csses */
2908 cgroup_save_control(cgrp
);
2910 cgrp
->subtree_control
|= enable
;
2911 cgrp
->subtree_control
&= ~disable
;
2913 ret
= cgroup_apply_control(cgrp
);
2915 cgroup_finalize_control(cgrp
, ret
);
2917 kernfs_activate(cgrp
->kn
);
2920 cgroup_kn_unlock(of
->kn
);
2921 return ret
?: nbytes
;
2924 static int cgroup_events_show(struct seq_file
*seq
, void *v
)
2926 seq_printf(seq
, "populated %d\n",
2927 cgroup_is_populated(seq_css(seq
)->cgroup
));
2931 static int cgroup_file_open(struct kernfs_open_file
*of
)
2933 struct cftype
*cft
= of
->kn
->priv
;
2936 return cft
->open(of
);
2940 static void cgroup_file_release(struct kernfs_open_file
*of
)
2942 struct cftype
*cft
= of
->kn
->priv
;
2948 static ssize_t
cgroup_file_write(struct kernfs_open_file
*of
, char *buf
,
2949 size_t nbytes
, loff_t off
)
2951 struct cgroup
*cgrp
= of
->kn
->parent
->priv
;
2952 struct cftype
*cft
= of
->kn
->priv
;
2953 struct cgroup_subsys_state
*css
;
2957 return cft
->write(of
, buf
, nbytes
, off
);
2960 * kernfs guarantees that a file isn't deleted with operations in
2961 * flight, which means that the matching css is and stays alive and
2962 * doesn't need to be pinned. The RCU locking is not necessary
2963 * either. It's just for the convenience of using cgroup_css().
2966 css
= cgroup_css(cgrp
, cft
->ss
);
2969 if (cft
->write_u64
) {
2970 unsigned long long v
;
2971 ret
= kstrtoull(buf
, 0, &v
);
2973 ret
= cft
->write_u64(css
, cft
, v
);
2974 } else if (cft
->write_s64
) {
2976 ret
= kstrtoll(buf
, 0, &v
);
2978 ret
= cft
->write_s64(css
, cft
, v
);
2983 return ret
?: nbytes
;
2986 static void *cgroup_seqfile_start(struct seq_file
*seq
, loff_t
*ppos
)
2988 return seq_cft(seq
)->seq_start(seq
, ppos
);
2991 static void *cgroup_seqfile_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
2993 return seq_cft(seq
)->seq_next(seq
, v
, ppos
);
2996 static void cgroup_seqfile_stop(struct seq_file
*seq
, void *v
)
2998 if (seq_cft(seq
)->seq_stop
)
2999 seq_cft(seq
)->seq_stop(seq
, v
);
3002 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
3004 struct cftype
*cft
= seq_cft(m
);
3005 struct cgroup_subsys_state
*css
= seq_css(m
);
3008 return cft
->seq_show(m
, arg
);
3011 seq_printf(m
, "%llu\n", cft
->read_u64(css
, cft
));
3012 else if (cft
->read_s64
)
3013 seq_printf(m
, "%lld\n", cft
->read_s64(css
, cft
));
3019 static struct kernfs_ops cgroup_kf_single_ops
= {
3020 .atomic_write_len
= PAGE_SIZE
,
3021 .open
= cgroup_file_open
,
3022 .release
= cgroup_file_release
,
3023 .write
= cgroup_file_write
,
3024 .seq_show
= cgroup_seqfile_show
,
3027 static struct kernfs_ops cgroup_kf_ops
= {
3028 .atomic_write_len
= PAGE_SIZE
,
3029 .open
= cgroup_file_open
,
3030 .release
= cgroup_file_release
,
3031 .write
= cgroup_file_write
,
3032 .seq_start
= cgroup_seqfile_start
,
3033 .seq_next
= cgroup_seqfile_next
,
3034 .seq_stop
= cgroup_seqfile_stop
,
3035 .seq_show
= cgroup_seqfile_show
,
3038 /* set uid and gid of cgroup dirs and files to that of the creator */
3039 static int cgroup_kn_set_ugid(struct kernfs_node
*kn
)
3041 struct iattr iattr
= { .ia_valid
= ATTR_UID
| ATTR_GID
,
3042 .ia_uid
= current_fsuid(),
3043 .ia_gid
= current_fsgid(), };
3045 if (uid_eq(iattr
.ia_uid
, GLOBAL_ROOT_UID
) &&
3046 gid_eq(iattr
.ia_gid
, GLOBAL_ROOT_GID
))
3049 return kernfs_setattr(kn
, &iattr
);
3052 static int cgroup_add_file(struct cgroup_subsys_state
*css
, struct cgroup
*cgrp
,
3055 char name
[CGROUP_FILE_NAME_MAX
];
3056 struct kernfs_node
*kn
;
3057 struct lock_class_key
*key
= NULL
;
3060 #ifdef CONFIG_DEBUG_LOCK_ALLOC
3061 key
= &cft
->lockdep_key
;
3063 kn
= __kernfs_create_file(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
),
3064 cgroup_file_mode(cft
), 0, cft
->kf_ops
, cft
,
3069 ret
= cgroup_kn_set_ugid(kn
);
3075 if (cft
->file_offset
) {
3076 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
3078 spin_lock_irq(&cgroup_file_kn_lock
);
3080 spin_unlock_irq(&cgroup_file_kn_lock
);
3087 * cgroup_addrm_files - add or remove files to a cgroup directory
3088 * @css: the target css
3089 * @cgrp: the target cgroup (usually css->cgroup)
3090 * @cfts: array of cftypes to be added
3091 * @is_add: whether to add or remove
3093 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
3094 * For removals, this function never fails.
3096 static int cgroup_addrm_files(struct cgroup_subsys_state
*css
,
3097 struct cgroup
*cgrp
, struct cftype cfts
[],
3100 struct cftype
*cft
, *cft_end
= NULL
;
3103 lockdep_assert_held(&cgroup_mutex
);
3106 for (cft
= cfts
; cft
!= cft_end
&& cft
->name
[0] != '\0'; cft
++) {
3107 /* does cft->flags tell us to skip this file on @cgrp? */
3108 if ((cft
->flags
& __CFTYPE_ONLY_ON_DFL
) && !cgroup_on_dfl(cgrp
))
3110 if ((cft
->flags
& __CFTYPE_NOT_ON_DFL
) && cgroup_on_dfl(cgrp
))
3112 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgroup_parent(cgrp
))
3114 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgroup_parent(cgrp
))
3118 ret
= cgroup_add_file(css
, cgrp
, cft
);
3120 pr_warn("%s: failed to add %s, err=%d\n",
3121 __func__
, cft
->name
, ret
);
3127 cgroup_rm_file(cgrp
, cft
);
3133 static int cgroup_apply_cftypes(struct cftype
*cfts
, bool is_add
)
3136 struct cgroup_subsys
*ss
= cfts
[0].ss
;
3137 struct cgroup
*root
= &ss
->root
->cgrp
;
3138 struct cgroup_subsys_state
*css
;
3141 lockdep_assert_held(&cgroup_mutex
);
3143 /* add/rm files for all cgroups created before */
3144 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
)) {
3145 struct cgroup
*cgrp
= css
->cgroup
;
3147 if (!(css
->flags
& CSS_VISIBLE
))
3150 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, is_add
);
3156 kernfs_activate(root
->kn
);
3160 static void cgroup_exit_cftypes(struct cftype
*cfts
)
3164 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3165 /* free copy for custom atomic_write_len, see init_cftypes() */
3166 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
)
3171 /* revert flags set by cgroup core while adding @cfts */
3172 cft
->flags
&= ~(__CFTYPE_ONLY_ON_DFL
| __CFTYPE_NOT_ON_DFL
);
3176 static int cgroup_init_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3180 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3181 struct kernfs_ops
*kf_ops
;
3183 WARN_ON(cft
->ss
|| cft
->kf_ops
);
3186 kf_ops
= &cgroup_kf_ops
;
3188 kf_ops
= &cgroup_kf_single_ops
;
3191 * Ugh... if @cft wants a custom max_write_len, we need to
3192 * make a copy of kf_ops to set its atomic_write_len.
3194 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
) {
3195 kf_ops
= kmemdup(kf_ops
, sizeof(*kf_ops
), GFP_KERNEL
);
3197 cgroup_exit_cftypes(cfts
);
3200 kf_ops
->atomic_write_len
= cft
->max_write_len
;
3203 cft
->kf_ops
= kf_ops
;
3210 static int cgroup_rm_cftypes_locked(struct cftype
*cfts
)
3212 lockdep_assert_held(&cgroup_mutex
);
3214 if (!cfts
|| !cfts
[0].ss
)
3217 list_del(&cfts
->node
);
3218 cgroup_apply_cftypes(cfts
, false);
3219 cgroup_exit_cftypes(cfts
);
3224 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3225 * @cfts: zero-length name terminated array of cftypes
3227 * Unregister @cfts. Files described by @cfts are removed from all
3228 * existing cgroups and all future cgroups won't have them either. This
3229 * function can be called anytime whether @cfts' subsys is attached or not.
3231 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3234 int cgroup_rm_cftypes(struct cftype
*cfts
)
3238 mutex_lock(&cgroup_mutex
);
3239 ret
= cgroup_rm_cftypes_locked(cfts
);
3240 mutex_unlock(&cgroup_mutex
);
3245 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3246 * @ss: target cgroup subsystem
3247 * @cfts: zero-length name terminated array of cftypes
3249 * Register @cfts to @ss. Files described by @cfts are created for all
3250 * existing cgroups to which @ss is attached and all future cgroups will
3251 * have them too. This function can be called anytime whether @ss is
3254 * Returns 0 on successful registration, -errno on failure. Note that this
3255 * function currently returns 0 as long as @cfts registration is successful
3256 * even if some file creation attempts on existing cgroups fail.
3258 static int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3262 if (!cgroup_ssid_enabled(ss
->id
))
3265 if (!cfts
|| cfts
[0].name
[0] == '\0')
3268 ret
= cgroup_init_cftypes(ss
, cfts
);
3272 mutex_lock(&cgroup_mutex
);
3274 list_add_tail(&cfts
->node
, &ss
->cfts
);
3275 ret
= cgroup_apply_cftypes(cfts
, true);
3277 cgroup_rm_cftypes_locked(cfts
);
3279 mutex_unlock(&cgroup_mutex
);
3284 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3285 * @ss: target cgroup subsystem
3286 * @cfts: zero-length name terminated array of cftypes
3288 * Similar to cgroup_add_cftypes() but the added files are only used for
3289 * the default hierarchy.
3291 int cgroup_add_dfl_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3295 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3296 cft
->flags
|= __CFTYPE_ONLY_ON_DFL
;
3297 return cgroup_add_cftypes(ss
, cfts
);
3301 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3302 * @ss: target cgroup subsystem
3303 * @cfts: zero-length name terminated array of cftypes
3305 * Similar to cgroup_add_cftypes() but the added files are only used for
3306 * the legacy hierarchies.
3308 int cgroup_add_legacy_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3312 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3313 cft
->flags
|= __CFTYPE_NOT_ON_DFL
;
3314 return cgroup_add_cftypes(ss
, cfts
);
3318 * cgroup_file_notify - generate a file modified event for a cgroup_file
3319 * @cfile: target cgroup_file
3321 * @cfile must have been obtained by setting cftype->file_offset.
3323 void cgroup_file_notify(struct cgroup_file
*cfile
)
3325 unsigned long flags
;
3327 spin_lock_irqsave(&cgroup_file_kn_lock
, flags
);
3329 kernfs_notify(cfile
->kn
);
3330 spin_unlock_irqrestore(&cgroup_file_kn_lock
, flags
);
3334 * css_next_child - find the next child of a given css
3335 * @pos: the current position (%NULL to initiate traversal)
3336 * @parent: css whose children to walk
3338 * This function returns the next child of @parent and should be called
3339 * under either cgroup_mutex or RCU read lock. The only requirement is
3340 * that @parent and @pos are accessible. The next sibling is guaranteed to
3341 * be returned regardless of their states.
3343 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3344 * css which finished ->css_online() is guaranteed to be visible in the
3345 * future iterations and will stay visible until the last reference is put.
3346 * A css which hasn't finished ->css_online() or already finished
3347 * ->css_offline() may show up during traversal. It's each subsystem's
3348 * responsibility to synchronize against on/offlining.
3350 struct cgroup_subsys_state
*css_next_child(struct cgroup_subsys_state
*pos
,
3351 struct cgroup_subsys_state
*parent
)
3353 struct cgroup_subsys_state
*next
;
3355 cgroup_assert_mutex_or_rcu_locked();
3358 * @pos could already have been unlinked from the sibling list.
3359 * Once a cgroup is removed, its ->sibling.next is no longer
3360 * updated when its next sibling changes. CSS_RELEASED is set when
3361 * @pos is taken off list, at which time its next pointer is valid,
3362 * and, as releases are serialized, the one pointed to by the next
3363 * pointer is guaranteed to not have started release yet. This
3364 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3365 * critical section, the one pointed to by its next pointer is
3366 * guaranteed to not have finished its RCU grace period even if we
3367 * have dropped rcu_read_lock() inbetween iterations.
3369 * If @pos has CSS_RELEASED set, its next pointer can't be
3370 * dereferenced; however, as each css is given a monotonically
3371 * increasing unique serial number and always appended to the
3372 * sibling list, the next one can be found by walking the parent's
3373 * children until the first css with higher serial number than
3374 * @pos's. While this path can be slower, it happens iff iteration
3375 * races against release and the race window is very small.
3378 next
= list_entry_rcu(parent
->children
.next
, struct cgroup_subsys_state
, sibling
);
3379 } else if (likely(!(pos
->flags
& CSS_RELEASED
))) {
3380 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup_subsys_state
, sibling
);
3382 list_for_each_entry_rcu(next
, &parent
->children
, sibling
)
3383 if (next
->serial_nr
> pos
->serial_nr
)
3388 * @next, if not pointing to the head, can be dereferenced and is
3391 if (&next
->sibling
!= &parent
->children
)
3397 * css_next_descendant_pre - find the next descendant for pre-order walk
3398 * @pos: the current position (%NULL to initiate traversal)
3399 * @root: css whose descendants to walk
3401 * To be used by css_for_each_descendant_pre(). Find the next descendant
3402 * to visit for pre-order traversal of @root's descendants. @root is
3403 * included in the iteration and the first node to be visited.
3405 * While this function requires cgroup_mutex or RCU read locking, it
3406 * doesn't require the whole traversal to be contained in a single critical
3407 * section. This function will return the correct next descendant as long
3408 * as both @pos and @root are accessible and @pos is a descendant of @root.
3410 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3411 * css which finished ->css_online() is guaranteed to be visible in the
3412 * future iterations and will stay visible until the last reference is put.
3413 * A css which hasn't finished ->css_online() or already finished
3414 * ->css_offline() may show up during traversal. It's each subsystem's
3415 * responsibility to synchronize against on/offlining.
3417 struct cgroup_subsys_state
*
3418 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
3419 struct cgroup_subsys_state
*root
)
3421 struct cgroup_subsys_state
*next
;
3423 cgroup_assert_mutex_or_rcu_locked();
3425 /* if first iteration, visit @root */
3429 /* visit the first child if exists */
3430 next
= css_next_child(NULL
, pos
);
3434 /* no child, visit my or the closest ancestor's next sibling */
3435 while (pos
!= root
) {
3436 next
= css_next_child(pos
, pos
->parent
);
3446 * css_rightmost_descendant - return the rightmost descendant of a css
3447 * @pos: css of interest
3449 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3450 * is returned. This can be used during pre-order traversal to skip
3453 * While this function requires cgroup_mutex or RCU read locking, it
3454 * doesn't require the whole traversal to be contained in a single critical
3455 * section. This function will return the correct rightmost descendant as
3456 * long as @pos is accessible.
3458 struct cgroup_subsys_state
*
3459 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
3461 struct cgroup_subsys_state
*last
, *tmp
;
3463 cgroup_assert_mutex_or_rcu_locked();
3467 /* ->prev isn't RCU safe, walk ->next till the end */
3469 css_for_each_child(tmp
, last
)
3476 static struct cgroup_subsys_state
*
3477 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
3479 struct cgroup_subsys_state
*last
;
3483 pos
= css_next_child(NULL
, pos
);
3490 * css_next_descendant_post - find the next descendant for post-order walk
3491 * @pos: the current position (%NULL to initiate traversal)
3492 * @root: css whose descendants to walk
3494 * To be used by css_for_each_descendant_post(). Find the next descendant
3495 * to visit for post-order traversal of @root's descendants. @root is
3496 * included in the iteration and the last node to be visited.
3498 * While this function requires cgroup_mutex or RCU read locking, it
3499 * doesn't require the whole traversal to be contained in a single critical
3500 * section. This function will return the correct next descendant as long
3501 * as both @pos and @cgroup are accessible and @pos is a descendant of
3504 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3505 * css which finished ->css_online() is guaranteed to be visible in the
3506 * future iterations and will stay visible until the last reference is put.
3507 * A css which hasn't finished ->css_online() or already finished
3508 * ->css_offline() may show up during traversal. It's each subsystem's
3509 * responsibility to synchronize against on/offlining.
3511 struct cgroup_subsys_state
*
3512 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
3513 struct cgroup_subsys_state
*root
)
3515 struct cgroup_subsys_state
*next
;
3517 cgroup_assert_mutex_or_rcu_locked();
3519 /* if first iteration, visit leftmost descendant which may be @root */
3521 return css_leftmost_descendant(root
);
3523 /* if we visited @root, we're done */
3527 /* if there's an unvisited sibling, visit its leftmost descendant */
3528 next
= css_next_child(pos
, pos
->parent
);
3530 return css_leftmost_descendant(next
);
3532 /* no sibling left, visit parent */
3537 * css_has_online_children - does a css have online children
3538 * @css: the target css
3540 * Returns %true if @css has any online children; otherwise, %false. This
3541 * function can be called from any context but the caller is responsible
3542 * for synchronizing against on/offlining as necessary.
3544 bool css_has_online_children(struct cgroup_subsys_state
*css
)
3546 struct cgroup_subsys_state
*child
;
3550 css_for_each_child(child
, css
) {
3551 if (child
->flags
& CSS_ONLINE
) {
3561 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
3562 * @it: the iterator to advance
3564 * Advance @it to the next css_set to walk.
3566 static void css_task_iter_advance_css_set(struct css_task_iter
*it
)
3568 struct list_head
*l
= it
->cset_pos
;
3569 struct cgrp_cset_link
*link
;
3570 struct css_set
*cset
;
3572 lockdep_assert_held(&css_set_lock
);
3574 /* Advance to the next non-empty css_set */
3577 if (l
== it
->cset_head
) {
3578 it
->cset_pos
= NULL
;
3579 it
->task_pos
= NULL
;
3584 cset
= container_of(l
, struct css_set
,
3585 e_cset_node
[it
->ss
->id
]);
3587 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
3590 } while (!css_set_populated(cset
));
3594 if (!list_empty(&cset
->tasks
))
3595 it
->task_pos
= cset
->tasks
.next
;
3597 it
->task_pos
= cset
->mg_tasks
.next
;
3599 it
->tasks_head
= &cset
->tasks
;
3600 it
->mg_tasks_head
= &cset
->mg_tasks
;
3603 * We don't keep css_sets locked across iteration steps and thus
3604 * need to take steps to ensure that iteration can be resumed after
3605 * the lock is re-acquired. Iteration is performed at two levels -
3606 * css_sets and tasks in them.
3608 * Once created, a css_set never leaves its cgroup lists, so a
3609 * pinned css_set is guaranteed to stay put and we can resume
3610 * iteration afterwards.
3612 * Tasks may leave @cset across iteration steps. This is resolved
3613 * by registering each iterator with the css_set currently being
3614 * walked and making css_set_move_task() advance iterators whose
3615 * next task is leaving.
3618 list_del(&it
->iters_node
);
3619 put_css_set_locked(it
->cur_cset
);
3622 it
->cur_cset
= cset
;
3623 list_add(&it
->iters_node
, &cset
->task_iters
);
3626 static void css_task_iter_advance(struct css_task_iter
*it
)
3628 struct list_head
*l
= it
->task_pos
;
3630 lockdep_assert_held(&css_set_lock
);
3634 * Advance iterator to find next entry. cset->tasks is consumed
3635 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3640 if (l
== it
->tasks_head
)
3641 l
= it
->mg_tasks_head
->next
;
3643 if (l
== it
->mg_tasks_head
)
3644 css_task_iter_advance_css_set(it
);
3650 * css_task_iter_start - initiate task iteration
3651 * @css: the css to walk tasks of
3652 * @it: the task iterator to use
3654 * Initiate iteration through the tasks of @css. The caller can call
3655 * css_task_iter_next() to walk through the tasks until the function
3656 * returns NULL. On completion of iteration, css_task_iter_end() must be
3659 void css_task_iter_start(struct cgroup_subsys_state
*css
,
3660 struct css_task_iter
*it
)
3662 /* no one should try to iterate before mounting cgroups */
3663 WARN_ON_ONCE(!use_task_css_set_links
);
3665 memset(it
, 0, sizeof(*it
));
3667 spin_lock_irq(&css_set_lock
);
3672 it
->cset_pos
= &css
->cgroup
->e_csets
[css
->ss
->id
];
3674 it
->cset_pos
= &css
->cgroup
->cset_links
;
3676 it
->cset_head
= it
->cset_pos
;
3678 css_task_iter_advance_css_set(it
);
3680 spin_unlock_irq(&css_set_lock
);
3684 * css_task_iter_next - return the next task for the iterator
3685 * @it: the task iterator being iterated
3687 * The "next" function for task iteration. @it should have been
3688 * initialized via css_task_iter_start(). Returns NULL when the iteration
3691 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
3694 put_task_struct(it
->cur_task
);
3695 it
->cur_task
= NULL
;
3698 spin_lock_irq(&css_set_lock
);
3701 it
->cur_task
= list_entry(it
->task_pos
, struct task_struct
,
3703 get_task_struct(it
->cur_task
);
3704 css_task_iter_advance(it
);
3707 spin_unlock_irq(&css_set_lock
);
3709 return it
->cur_task
;
3713 * css_task_iter_end - finish task iteration
3714 * @it: the task iterator to finish
3716 * Finish task iteration started by css_task_iter_start().
3718 void css_task_iter_end(struct css_task_iter
*it
)
3721 spin_lock_irq(&css_set_lock
);
3722 list_del(&it
->iters_node
);
3723 put_css_set_locked(it
->cur_cset
);
3724 spin_unlock_irq(&css_set_lock
);
3728 put_task_struct(it
->cur_task
);
3731 static void cgroup_procs_release(struct kernfs_open_file
*of
)
3734 css_task_iter_end(of
->priv
);
3739 static void *cgroup_procs_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3741 struct kernfs_open_file
*of
= s
->private;
3742 struct css_task_iter
*it
= of
->priv
;
3743 struct task_struct
*task
;
3746 task
= css_task_iter_next(it
);
3747 } while (task
&& !thread_group_leader(task
));
3752 static void *cgroup_procs_start(struct seq_file
*s
, loff_t
*pos
)
3754 struct kernfs_open_file
*of
= s
->private;
3755 struct cgroup
*cgrp
= seq_css(s
)->cgroup
;
3756 struct css_task_iter
*it
= of
->priv
;
3759 * When a seq_file is seeked, it's always traversed sequentially
3760 * from position 0, so we can simply keep iterating on !0 *pos.
3763 if (WARN_ON_ONCE((*pos
)++))
3764 return ERR_PTR(-EINVAL
);
3766 it
= kzalloc(sizeof(*it
), GFP_KERNEL
);
3768 return ERR_PTR(-ENOMEM
);
3770 css_task_iter_start(&cgrp
->self
, it
);
3771 } else if (!(*pos
)++) {
3772 css_task_iter_end(it
);
3773 css_task_iter_start(&cgrp
->self
, it
);
3776 return cgroup_procs_next(s
, NULL
, NULL
);
3779 static int cgroup_procs_show(struct seq_file
*s
, void *v
)
3781 seq_printf(s
, "%d\n", task_tgid_vnr(v
));
3785 /* cgroup core interface files for the default hierarchy */
3786 static struct cftype cgroup_base_files
[] = {
3788 .name
= "cgroup.procs",
3789 .file_offset
= offsetof(struct cgroup
, procs_file
),
3790 .release
= cgroup_procs_release
,
3791 .seq_start
= cgroup_procs_start
,
3792 .seq_next
= cgroup_procs_next
,
3793 .seq_show
= cgroup_procs_show
,
3794 .write
= cgroup_procs_write
,
3797 .name
= "cgroup.controllers",
3798 .seq_show
= cgroup_controllers_show
,
3801 .name
= "cgroup.subtree_control",
3802 .seq_show
= cgroup_subtree_control_show
,
3803 .write
= cgroup_subtree_control_write
,
3806 .name
= "cgroup.events",
3807 .flags
= CFTYPE_NOT_ON_ROOT
,
3808 .file_offset
= offsetof(struct cgroup
, events_file
),
3809 .seq_show
= cgroup_events_show
,
3815 * css destruction is four-stage process.
3817 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3818 * Implemented in kill_css().
3820 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3821 * and thus css_tryget_online() is guaranteed to fail, the css can be
3822 * offlined by invoking offline_css(). After offlining, the base ref is
3823 * put. Implemented in css_killed_work_fn().
3825 * 3. When the percpu_ref reaches zero, the only possible remaining
3826 * accessors are inside RCU read sections. css_release() schedules the
3829 * 4. After the grace period, the css can be freed. Implemented in
3830 * css_free_work_fn().
3832 * It is actually hairier because both step 2 and 4 require process context
3833 * and thus involve punting to css->destroy_work adding two additional
3834 * steps to the already complex sequence.
3836 static void css_free_work_fn(struct work_struct
*work
)
3838 struct cgroup_subsys_state
*css
=
3839 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
3840 struct cgroup_subsys
*ss
= css
->ss
;
3841 struct cgroup
*cgrp
= css
->cgroup
;
3843 percpu_ref_exit(&css
->refcnt
);
3847 struct cgroup_subsys_state
*parent
= css
->parent
;
3851 cgroup_idr_remove(&ss
->css_idr
, id
);
3857 /* cgroup free path */
3858 atomic_dec(&cgrp
->root
->nr_cgrps
);
3859 cgroup1_pidlist_destroy_all(cgrp
);
3860 cancel_work_sync(&cgrp
->release_agent_work
);
3862 if (cgroup_parent(cgrp
)) {
3864 * We get a ref to the parent, and put the ref when
3865 * this cgroup is being freed, so it's guaranteed
3866 * that the parent won't be destroyed before its
3869 cgroup_put(cgroup_parent(cgrp
));
3870 kernfs_put(cgrp
->kn
);
3874 * This is root cgroup's refcnt reaching zero,
3875 * which indicates that the root should be
3878 cgroup_destroy_root(cgrp
->root
);
3883 static void css_free_rcu_fn(struct rcu_head
*rcu_head
)
3885 struct cgroup_subsys_state
*css
=
3886 container_of(rcu_head
, struct cgroup_subsys_state
, rcu_head
);
3888 INIT_WORK(&css
->destroy_work
, css_free_work_fn
);
3889 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
3892 static void css_release_work_fn(struct work_struct
*work
)
3894 struct cgroup_subsys_state
*css
=
3895 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
3896 struct cgroup_subsys
*ss
= css
->ss
;
3897 struct cgroup
*cgrp
= css
->cgroup
;
3899 mutex_lock(&cgroup_mutex
);
3901 css
->flags
|= CSS_RELEASED
;
3902 list_del_rcu(&css
->sibling
);
3905 /* css release path */
3906 cgroup_idr_replace(&ss
->css_idr
, NULL
, css
->id
);
3907 if (ss
->css_released
)
3908 ss
->css_released(css
);
3910 /* cgroup release path */
3911 trace_cgroup_release(cgrp
);
3913 cgroup_idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
3917 * There are two control paths which try to determine
3918 * cgroup from dentry without going through kernfs -
3919 * cgroupstats_build() and css_tryget_online_from_dir().
3920 * Those are supported by RCU protecting clearing of
3921 * cgrp->kn->priv backpointer.
3924 RCU_INIT_POINTER(*(void __rcu __force
**)&cgrp
->kn
->priv
,
3927 cgroup_bpf_put(cgrp
);
3930 mutex_unlock(&cgroup_mutex
);
3932 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
3935 static void css_release(struct percpu_ref
*ref
)
3937 struct cgroup_subsys_state
*css
=
3938 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
3940 INIT_WORK(&css
->destroy_work
, css_release_work_fn
);
3941 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
3944 static void init_and_link_css(struct cgroup_subsys_state
*css
,
3945 struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
3947 lockdep_assert_held(&cgroup_mutex
);
3951 memset(css
, 0, sizeof(*css
));
3955 INIT_LIST_HEAD(&css
->sibling
);
3956 INIT_LIST_HEAD(&css
->children
);
3957 css
->serial_nr
= css_serial_nr_next
++;
3958 atomic_set(&css
->online_cnt
, 0);
3960 if (cgroup_parent(cgrp
)) {
3961 css
->parent
= cgroup_css(cgroup_parent(cgrp
), ss
);
3962 css_get(css
->parent
);
3965 BUG_ON(cgroup_css(cgrp
, ss
));
3968 /* invoke ->css_online() on a new CSS and mark it online if successful */
3969 static int online_css(struct cgroup_subsys_state
*css
)
3971 struct cgroup_subsys
*ss
= css
->ss
;
3974 lockdep_assert_held(&cgroup_mutex
);
3977 ret
= ss
->css_online(css
);
3979 css
->flags
|= CSS_ONLINE
;
3980 rcu_assign_pointer(css
->cgroup
->subsys
[ss
->id
], css
);
3982 atomic_inc(&css
->online_cnt
);
3984 atomic_inc(&css
->parent
->online_cnt
);
3989 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
3990 static void offline_css(struct cgroup_subsys_state
*css
)
3992 struct cgroup_subsys
*ss
= css
->ss
;
3994 lockdep_assert_held(&cgroup_mutex
);
3996 if (!(css
->flags
& CSS_ONLINE
))
4002 if (ss
->css_offline
)
4003 ss
->css_offline(css
);
4005 css
->flags
&= ~CSS_ONLINE
;
4006 RCU_INIT_POINTER(css
->cgroup
->subsys
[ss
->id
], NULL
);
4008 wake_up_all(&css
->cgroup
->offline_waitq
);
4012 * css_create - create a cgroup_subsys_state
4013 * @cgrp: the cgroup new css will be associated with
4014 * @ss: the subsys of new css
4016 * Create a new css associated with @cgrp - @ss pair. On success, the new
4017 * css is online and installed in @cgrp. This function doesn't create the
4018 * interface files. Returns 0 on success, -errno on failure.
4020 static struct cgroup_subsys_state
*css_create(struct cgroup
*cgrp
,
4021 struct cgroup_subsys
*ss
)
4023 struct cgroup
*parent
= cgroup_parent(cgrp
);
4024 struct cgroup_subsys_state
*parent_css
= cgroup_css(parent
, ss
);
4025 struct cgroup_subsys_state
*css
;
4028 lockdep_assert_held(&cgroup_mutex
);
4030 css
= ss
->css_alloc(parent_css
);
4032 css
= ERR_PTR(-ENOMEM
);
4036 init_and_link_css(css
, ss
, cgrp
);
4038 err
= percpu_ref_init(&css
->refcnt
, css_release
, 0, GFP_KERNEL
);
4042 err
= cgroup_idr_alloc(&ss
->css_idr
, NULL
, 2, 0, GFP_KERNEL
);
4047 /* @css is ready to be brought online now, make it visible */
4048 list_add_tail_rcu(&css
->sibling
, &parent_css
->children
);
4049 cgroup_idr_replace(&ss
->css_idr
, css
, css
->id
);
4051 err
= online_css(css
);
4055 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4056 cgroup_parent(parent
)) {
4057 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4058 current
->comm
, current
->pid
, ss
->name
);
4059 if (!strcmp(ss
->name
, "memory"))
4060 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
4061 ss
->warned_broken_hierarchy
= true;
4067 list_del_rcu(&css
->sibling
);
4069 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4070 return ERR_PTR(err
);
4074 * The returned cgroup is fully initialized including its control mask, but
4075 * it isn't associated with its kernfs_node and doesn't have the control
4078 static struct cgroup
*cgroup_create(struct cgroup
*parent
)
4080 struct cgroup_root
*root
= parent
->root
;
4081 struct cgroup
*cgrp
, *tcgrp
;
4082 int level
= parent
->level
+ 1;
4085 /* allocate the cgroup and its ID, 0 is reserved for the root */
4086 cgrp
= kzalloc(sizeof(*cgrp
) +
4087 sizeof(cgrp
->ancestor_ids
[0]) * (level
+ 1), GFP_KERNEL
);
4089 return ERR_PTR(-ENOMEM
);
4091 ret
= percpu_ref_init(&cgrp
->self
.refcnt
, css_release
, 0, GFP_KERNEL
);
4096 * Temporarily set the pointer to NULL, so idr_find() won't return
4097 * a half-baked cgroup.
4099 cgrp
->id
= cgroup_idr_alloc(&root
->cgroup_idr
, NULL
, 2, 0, GFP_KERNEL
);
4102 goto out_cancel_ref
;
4105 init_cgroup_housekeeping(cgrp
);
4107 cgrp
->self
.parent
= &parent
->self
;
4109 cgrp
->level
= level
;
4111 for (tcgrp
= cgrp
; tcgrp
; tcgrp
= cgroup_parent(tcgrp
))
4112 cgrp
->ancestor_ids
[tcgrp
->level
] = tcgrp
->id
;
4114 if (notify_on_release(parent
))
4115 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4117 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4118 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4120 cgrp
->self
.serial_nr
= css_serial_nr_next
++;
4122 /* allocation complete, commit to creation */
4123 list_add_tail_rcu(&cgrp
->self
.sibling
, &cgroup_parent(cgrp
)->self
.children
);
4124 atomic_inc(&root
->nr_cgrps
);
4128 * @cgrp is now fully operational. If something fails after this
4129 * point, it'll be released via the normal destruction path.
4131 cgroup_idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
4134 * On the default hierarchy, a child doesn't automatically inherit
4135 * subtree_control from the parent. Each is configured manually.
4137 if (!cgroup_on_dfl(cgrp
))
4138 cgrp
->subtree_control
= cgroup_control(cgrp
);
4141 cgroup_bpf_inherit(cgrp
, parent
);
4143 cgroup_propagate_control(cgrp
);
4148 percpu_ref_exit(&cgrp
->self
.refcnt
);
4151 return ERR_PTR(ret
);
4154 int cgroup_mkdir(struct kernfs_node
*parent_kn
, const char *name
, umode_t mode
)
4156 struct cgroup
*parent
, *cgrp
;
4157 struct kernfs_node
*kn
;
4160 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
4161 if (strchr(name
, '\n'))
4164 parent
= cgroup_kn_lock_live(parent_kn
, false);
4168 cgrp
= cgroup_create(parent
);
4170 ret
= PTR_ERR(cgrp
);
4174 /* create the directory */
4175 kn
= kernfs_create_dir(parent
->kn
, name
, mode
, cgrp
);
4183 * This extra ref will be put in cgroup_free_fn() and guarantees
4184 * that @cgrp->kn is always accessible.
4188 ret
= cgroup_kn_set_ugid(kn
);
4192 ret
= css_populate_dir(&cgrp
->self
);
4196 ret
= cgroup_apply_control_enable(cgrp
);
4200 trace_cgroup_mkdir(cgrp
);
4202 /* let's create and online css's */
4203 kernfs_activate(kn
);
4209 cgroup_destroy_locked(cgrp
);
4211 cgroup_kn_unlock(parent_kn
);
4216 * This is called when the refcnt of a css is confirmed to be killed.
4217 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4218 * initate destruction and put the css ref from kill_css().
4220 static void css_killed_work_fn(struct work_struct
*work
)
4222 struct cgroup_subsys_state
*css
=
4223 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4225 mutex_lock(&cgroup_mutex
);
4230 /* @css can't go away while we're holding cgroup_mutex */
4232 } while (css
&& atomic_dec_and_test(&css
->online_cnt
));
4234 mutex_unlock(&cgroup_mutex
);
4237 /* css kill confirmation processing requires process context, bounce */
4238 static void css_killed_ref_fn(struct percpu_ref
*ref
)
4240 struct cgroup_subsys_state
*css
=
4241 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4243 if (atomic_dec_and_test(&css
->online_cnt
)) {
4244 INIT_WORK(&css
->destroy_work
, css_killed_work_fn
);
4245 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4250 * kill_css - destroy a css
4251 * @css: css to destroy
4253 * This function initiates destruction of @css by removing cgroup interface
4254 * files and putting its base reference. ->css_offline() will be invoked
4255 * asynchronously once css_tryget_online() is guaranteed to fail and when
4256 * the reference count reaches zero, @css will be released.
4258 static void kill_css(struct cgroup_subsys_state
*css
)
4260 lockdep_assert_held(&cgroup_mutex
);
4263 * This must happen before css is disassociated with its cgroup.
4264 * See seq_css() for details.
4269 * Killing would put the base ref, but we need to keep it alive
4270 * until after ->css_offline().
4275 * cgroup core guarantees that, by the time ->css_offline() is
4276 * invoked, no new css reference will be given out via
4277 * css_tryget_online(). We can't simply call percpu_ref_kill() and
4278 * proceed to offlining css's because percpu_ref_kill() doesn't
4279 * guarantee that the ref is seen as killed on all CPUs on return.
4281 * Use percpu_ref_kill_and_confirm() to get notifications as each
4282 * css is confirmed to be seen as killed on all CPUs.
4284 percpu_ref_kill_and_confirm(&css
->refcnt
, css_killed_ref_fn
);
4288 * cgroup_destroy_locked - the first stage of cgroup destruction
4289 * @cgrp: cgroup to be destroyed
4291 * css's make use of percpu refcnts whose killing latency shouldn't be
4292 * exposed to userland and are RCU protected. Also, cgroup core needs to
4293 * guarantee that css_tryget_online() won't succeed by the time
4294 * ->css_offline() is invoked. To satisfy all the requirements,
4295 * destruction is implemented in the following two steps.
4297 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4298 * userland visible parts and start killing the percpu refcnts of
4299 * css's. Set up so that the next stage will be kicked off once all
4300 * the percpu refcnts are confirmed to be killed.
4302 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4303 * rest of destruction. Once all cgroup references are gone, the
4304 * cgroup is RCU-freed.
4306 * This function implements s1. After this step, @cgrp is gone as far as
4307 * the userland is concerned and a new cgroup with the same name may be
4308 * created. As cgroup doesn't care about the names internally, this
4309 * doesn't cause any problem.
4311 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4312 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4314 struct cgroup_subsys_state
*css
;
4315 struct cgrp_cset_link
*link
;
4318 lockdep_assert_held(&cgroup_mutex
);
4321 * Only migration can raise populated from zero and we're already
4322 * holding cgroup_mutex.
4324 if (cgroup_is_populated(cgrp
))
4328 * Make sure there's no live children. We can't test emptiness of
4329 * ->self.children as dead children linger on it while being
4330 * drained; otherwise, "rmdir parent/child parent" may fail.
4332 if (css_has_online_children(&cgrp
->self
))
4336 * Mark @cgrp and the associated csets dead. The former prevents
4337 * further task migration and child creation by disabling
4338 * cgroup_lock_live_group(). The latter makes the csets ignored by
4339 * the migration path.
4341 cgrp
->self
.flags
&= ~CSS_ONLINE
;
4343 spin_lock_irq(&css_set_lock
);
4344 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
4345 link
->cset
->dead
= true;
4346 spin_unlock_irq(&css_set_lock
);
4348 /* initiate massacre of all css's */
4349 for_each_css(css
, ssid
, cgrp
)
4353 * Remove @cgrp directory along with the base files. @cgrp has an
4354 * extra ref on its kn.
4356 kernfs_remove(cgrp
->kn
);
4358 cgroup1_check_for_release(cgroup_parent(cgrp
));
4360 /* put the base reference */
4361 percpu_ref_kill(&cgrp
->self
.refcnt
);
4366 int cgroup_rmdir(struct kernfs_node
*kn
)
4368 struct cgroup
*cgrp
;
4371 cgrp
= cgroup_kn_lock_live(kn
, false);
4375 ret
= cgroup_destroy_locked(cgrp
);
4378 trace_cgroup_rmdir(cgrp
);
4380 cgroup_kn_unlock(kn
);
4384 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
= {
4385 .remount_fs
= cgroup_remount
,
4386 .mkdir
= cgroup_mkdir
,
4387 .rmdir
= cgroup_rmdir
,
4388 .show_path
= cgroup_show_path
,
4391 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
, bool early
)
4393 struct cgroup_subsys_state
*css
;
4395 pr_debug("Initializing cgroup subsys %s\n", ss
->name
);
4397 mutex_lock(&cgroup_mutex
);
4399 idr_init(&ss
->css_idr
);
4400 INIT_LIST_HEAD(&ss
->cfts
);
4402 /* Create the root cgroup state for this subsystem */
4403 ss
->root
= &cgrp_dfl_root
;
4404 css
= ss
->css_alloc(cgroup_css(&cgrp_dfl_root
.cgrp
, ss
));
4405 /* We don't handle early failures gracefully */
4406 BUG_ON(IS_ERR(css
));
4407 init_and_link_css(css
, ss
, &cgrp_dfl_root
.cgrp
);
4410 * Root csses are never destroyed and we can't initialize
4411 * percpu_ref during early init. Disable refcnting.
4413 css
->flags
|= CSS_NO_REF
;
4416 /* allocation can't be done safely during early init */
4419 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2, GFP_KERNEL
);
4420 BUG_ON(css
->id
< 0);
4423 /* Update the init_css_set to contain a subsys
4424 * pointer to this state - since the subsystem is
4425 * newly registered, all tasks and hence the
4426 * init_css_set is in the subsystem's root cgroup. */
4427 init_css_set
.subsys
[ss
->id
] = css
;
4429 have_fork_callback
|= (bool)ss
->fork
<< ss
->id
;
4430 have_exit_callback
|= (bool)ss
->exit
<< ss
->id
;
4431 have_free_callback
|= (bool)ss
->free
<< ss
->id
;
4432 have_canfork_callback
|= (bool)ss
->can_fork
<< ss
->id
;
4434 /* At system boot, before all subsystems have been
4435 * registered, no tasks have been forked, so we don't
4436 * need to invoke fork callbacks here. */
4437 BUG_ON(!list_empty(&init_task
.tasks
));
4439 BUG_ON(online_css(css
));
4441 mutex_unlock(&cgroup_mutex
);
4445 * cgroup_init_early - cgroup initialization at system boot
4447 * Initialize cgroups at system boot, and initialize any
4448 * subsystems that request early init.
4450 int __init
cgroup_init_early(void)
4452 static struct cgroup_sb_opts __initdata opts
;
4453 struct cgroup_subsys
*ss
;
4456 init_cgroup_root(&cgrp_dfl_root
, &opts
);
4457 cgrp_dfl_root
.cgrp
.self
.flags
|= CSS_NO_REF
;
4459 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
4461 for_each_subsys(ss
, i
) {
4462 WARN(!ss
->css_alloc
|| !ss
->css_free
|| ss
->name
|| ss
->id
,
4463 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
4464 i
, cgroup_subsys_name
[i
], ss
->css_alloc
, ss
->css_free
,
4466 WARN(strlen(cgroup_subsys_name
[i
]) > MAX_CGROUP_TYPE_NAMELEN
,
4467 "cgroup_subsys_name %s too long\n", cgroup_subsys_name
[i
]);
4470 ss
->name
= cgroup_subsys_name
[i
];
4471 if (!ss
->legacy_name
)
4472 ss
->legacy_name
= cgroup_subsys_name
[i
];
4475 cgroup_init_subsys(ss
, true);
4480 static u16 cgroup_disable_mask __initdata
;
4483 * cgroup_init - cgroup initialization
4485 * Register cgroup filesystem and /proc file, and initialize
4486 * any subsystems that didn't request early init.
4488 int __init
cgroup_init(void)
4490 struct cgroup_subsys
*ss
;
4493 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT
> 16);
4494 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem
));
4495 BUG_ON(cgroup_init_cftypes(NULL
, cgroup_base_files
));
4496 BUG_ON(cgroup_init_cftypes(NULL
, cgroup1_base_files
));
4499 * The latency of the synchronize_sched() is too high for cgroups,
4500 * avoid it at the cost of forcing all readers into the slow path.
4502 rcu_sync_enter_start(&cgroup_threadgroup_rwsem
.rss
);
4504 get_user_ns(init_cgroup_ns
.user_ns
);
4506 mutex_lock(&cgroup_mutex
);
4509 * Add init_css_set to the hash table so that dfl_root can link to
4512 hash_add(css_set_table
, &init_css_set
.hlist
,
4513 css_set_hash(init_css_set
.subsys
));
4515 BUG_ON(cgroup_setup_root(&cgrp_dfl_root
, 0));
4517 mutex_unlock(&cgroup_mutex
);
4519 for_each_subsys(ss
, ssid
) {
4520 if (ss
->early_init
) {
4521 struct cgroup_subsys_state
*css
=
4522 init_css_set
.subsys
[ss
->id
];
4524 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2,
4526 BUG_ON(css
->id
< 0);
4528 cgroup_init_subsys(ss
, false);
4531 list_add_tail(&init_css_set
.e_cset_node
[ssid
],
4532 &cgrp_dfl_root
.cgrp
.e_csets
[ssid
]);
4535 * Setting dfl_root subsys_mask needs to consider the
4536 * disabled flag and cftype registration needs kmalloc,
4537 * both of which aren't available during early_init.
4539 if (cgroup_disable_mask
& (1 << ssid
)) {
4540 static_branch_disable(cgroup_subsys_enabled_key
[ssid
]);
4541 printk(KERN_INFO
"Disabling %s control group subsystem\n",
4546 if (cgroup1_ssid_disabled(ssid
))
4547 printk(KERN_INFO
"Disabling %s control group subsystem in v1 mounts\n",
4550 cgrp_dfl_root
.subsys_mask
|= 1 << ss
->id
;
4552 if (ss
->implicit_on_dfl
)
4553 cgrp_dfl_implicit_ss_mask
|= 1 << ss
->id
;
4554 else if (!ss
->dfl_cftypes
)
4555 cgrp_dfl_inhibit_ss_mask
|= 1 << ss
->id
;
4557 if (ss
->dfl_cftypes
== ss
->legacy_cftypes
) {
4558 WARN_ON(cgroup_add_cftypes(ss
, ss
->dfl_cftypes
));
4560 WARN_ON(cgroup_add_dfl_cftypes(ss
, ss
->dfl_cftypes
));
4561 WARN_ON(cgroup_add_legacy_cftypes(ss
, ss
->legacy_cftypes
));
4565 ss
->bind(init_css_set
.subsys
[ssid
]);
4568 /* init_css_set.subsys[] has been updated, re-hash */
4569 hash_del(&init_css_set
.hlist
);
4570 hash_add(css_set_table
, &init_css_set
.hlist
,
4571 css_set_hash(init_css_set
.subsys
));
4573 WARN_ON(sysfs_create_mount_point(fs_kobj
, "cgroup"));
4574 WARN_ON(register_filesystem(&cgroup_fs_type
));
4575 WARN_ON(register_filesystem(&cgroup2_fs_type
));
4576 WARN_ON(!proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
));
4581 static int __init
cgroup_wq_init(void)
4584 * There isn't much point in executing destruction path in
4585 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4586 * Use 1 for @max_active.
4588 * We would prefer to do this in cgroup_init() above, but that
4589 * is called before init_workqueues(): so leave this until after.
4591 cgroup_destroy_wq
= alloc_workqueue("cgroup_destroy", 0, 1);
4592 BUG_ON(!cgroup_destroy_wq
);
4595 core_initcall(cgroup_wq_init
);
4598 * proc_cgroup_show()
4599 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4600 * - Used for /proc/<pid>/cgroup.
4602 int proc_cgroup_show(struct seq_file
*m
, struct pid_namespace
*ns
,
4603 struct pid
*pid
, struct task_struct
*tsk
)
4607 struct cgroup_root
*root
;
4610 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
4614 mutex_lock(&cgroup_mutex
);
4615 spin_lock_irq(&css_set_lock
);
4617 for_each_root(root
) {
4618 struct cgroup_subsys
*ss
;
4619 struct cgroup
*cgrp
;
4620 int ssid
, count
= 0;
4622 if (root
== &cgrp_dfl_root
&& !cgrp_dfl_visible
)
4625 seq_printf(m
, "%d:", root
->hierarchy_id
);
4626 if (root
!= &cgrp_dfl_root
)
4627 for_each_subsys(ss
, ssid
)
4628 if (root
->subsys_mask
& (1 << ssid
))
4629 seq_printf(m
, "%s%s", count
++ ? "," : "",
4631 if (strlen(root
->name
))
4632 seq_printf(m
, "%sname=%s", count
? "," : "",
4636 cgrp
= task_cgroup_from_root(tsk
, root
);
4639 * On traditional hierarchies, all zombie tasks show up as
4640 * belonging to the root cgroup. On the default hierarchy,
4641 * while a zombie doesn't show up in "cgroup.procs" and
4642 * thus can't be migrated, its /proc/PID/cgroup keeps
4643 * reporting the cgroup it belonged to before exiting. If
4644 * the cgroup is removed before the zombie is reaped,
4645 * " (deleted)" is appended to the cgroup path.
4647 if (cgroup_on_dfl(cgrp
) || !(tsk
->flags
& PF_EXITING
)) {
4648 retval
= cgroup_path_ns_locked(cgrp
, buf
, PATH_MAX
,
4649 current
->nsproxy
->cgroup_ns
);
4650 if (retval
>= PATH_MAX
)
4651 retval
= -ENAMETOOLONG
;
4660 if (cgroup_on_dfl(cgrp
) && cgroup_is_dead(cgrp
))
4661 seq_puts(m
, " (deleted)\n");
4668 spin_unlock_irq(&css_set_lock
);
4669 mutex_unlock(&cgroup_mutex
);
4676 * cgroup_fork - initialize cgroup related fields during copy_process()
4677 * @child: pointer to task_struct of forking parent process.
4679 * A task is associated with the init_css_set until cgroup_post_fork()
4680 * attaches it to the parent's css_set. Empty cg_list indicates that
4681 * @child isn't holding reference to its css_set.
4683 void cgroup_fork(struct task_struct
*child
)
4685 RCU_INIT_POINTER(child
->cgroups
, &init_css_set
);
4686 INIT_LIST_HEAD(&child
->cg_list
);
4690 * cgroup_can_fork - called on a new task before the process is exposed
4691 * @child: the task in question.
4693 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
4694 * returns an error, the fork aborts with that error code. This allows for
4695 * a cgroup subsystem to conditionally allow or deny new forks.
4697 int cgroup_can_fork(struct task_struct
*child
)
4699 struct cgroup_subsys
*ss
;
4702 do_each_subsys_mask(ss
, i
, have_canfork_callback
) {
4703 ret
= ss
->can_fork(child
);
4706 } while_each_subsys_mask();
4711 for_each_subsys(ss
, j
) {
4714 if (ss
->cancel_fork
)
4715 ss
->cancel_fork(child
);
4722 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
4723 * @child: the task in question
4725 * This calls the cancel_fork() callbacks if a fork failed *after*
4726 * cgroup_can_fork() succeded.
4728 void cgroup_cancel_fork(struct task_struct
*child
)
4730 struct cgroup_subsys
*ss
;
4733 for_each_subsys(ss
, i
)
4734 if (ss
->cancel_fork
)
4735 ss
->cancel_fork(child
);
4739 * cgroup_post_fork - called on a new task after adding it to the task list
4740 * @child: the task in question
4742 * Adds the task to the list running through its css_set if necessary and
4743 * call the subsystem fork() callbacks. Has to be after the task is
4744 * visible on the task list in case we race with the first call to
4745 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
4748 void cgroup_post_fork(struct task_struct
*child
)
4750 struct cgroup_subsys
*ss
;
4754 * This may race against cgroup_enable_task_cg_lists(). As that
4755 * function sets use_task_css_set_links before grabbing
4756 * tasklist_lock and we just went through tasklist_lock to add
4757 * @child, it's guaranteed that either we see the set
4758 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4759 * @child during its iteration.
4761 * If we won the race, @child is associated with %current's
4762 * css_set. Grabbing css_set_lock guarantees both that the
4763 * association is stable, and, on completion of the parent's
4764 * migration, @child is visible in the source of migration or
4765 * already in the destination cgroup. This guarantee is necessary
4766 * when implementing operations which need to migrate all tasks of
4767 * a cgroup to another.
4769 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
4770 * will remain in init_css_set. This is safe because all tasks are
4771 * in the init_css_set before cg_links is enabled and there's no
4772 * operation which transfers all tasks out of init_css_set.
4774 if (use_task_css_set_links
) {
4775 struct css_set
*cset
;
4777 spin_lock_irq(&css_set_lock
);
4778 cset
= task_css_set(current
);
4779 if (list_empty(&child
->cg_list
)) {
4781 css_set_move_task(child
, NULL
, cset
, false);
4783 spin_unlock_irq(&css_set_lock
);
4787 * Call ss->fork(). This must happen after @child is linked on
4788 * css_set; otherwise, @child might change state between ->fork()
4789 * and addition to css_set.
4791 do_each_subsys_mask(ss
, i
, have_fork_callback
) {
4793 } while_each_subsys_mask();
4797 * cgroup_exit - detach cgroup from exiting task
4798 * @tsk: pointer to task_struct of exiting process
4800 * Description: Detach cgroup from @tsk and release it.
4802 * Note that cgroups marked notify_on_release force every task in
4803 * them to take the global cgroup_mutex mutex when exiting.
4804 * This could impact scaling on very large systems. Be reluctant to
4805 * use notify_on_release cgroups where very high task exit scaling
4806 * is required on large systems.
4808 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4809 * call cgroup_exit() while the task is still competent to handle
4810 * notify_on_release(), then leave the task attached to the root cgroup in
4811 * each hierarchy for the remainder of its exit. No need to bother with
4812 * init_css_set refcnting. init_css_set never goes away and we can't race
4813 * with migration path - PF_EXITING is visible to migration path.
4815 void cgroup_exit(struct task_struct
*tsk
)
4817 struct cgroup_subsys
*ss
;
4818 struct css_set
*cset
;
4822 * Unlink from @tsk from its css_set. As migration path can't race
4823 * with us, we can check css_set and cg_list without synchronization.
4825 cset
= task_css_set(tsk
);
4827 if (!list_empty(&tsk
->cg_list
)) {
4828 spin_lock_irq(&css_set_lock
);
4829 css_set_move_task(tsk
, cset
, NULL
, false);
4830 spin_unlock_irq(&css_set_lock
);
4835 /* see cgroup_post_fork() for details */
4836 do_each_subsys_mask(ss
, i
, have_exit_callback
) {
4838 } while_each_subsys_mask();
4841 void cgroup_free(struct task_struct
*task
)
4843 struct css_set
*cset
= task_css_set(task
);
4844 struct cgroup_subsys
*ss
;
4847 do_each_subsys_mask(ss
, ssid
, have_free_callback
) {
4849 } while_each_subsys_mask();
4854 static int __init
cgroup_disable(char *str
)
4856 struct cgroup_subsys
*ss
;
4860 while ((token
= strsep(&str
, ",")) != NULL
) {
4864 for_each_subsys(ss
, i
) {
4865 if (strcmp(token
, ss
->name
) &&
4866 strcmp(token
, ss
->legacy_name
))
4868 cgroup_disable_mask
|= 1 << i
;
4873 __setup("cgroup_disable=", cgroup_disable
);
4876 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
4877 * @dentry: directory dentry of interest
4878 * @ss: subsystem of interest
4880 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4881 * to get the corresponding css and return it. If such css doesn't exist
4882 * or can't be pinned, an ERR_PTR value is returned.
4884 struct cgroup_subsys_state
*css_tryget_online_from_dir(struct dentry
*dentry
,
4885 struct cgroup_subsys
*ss
)
4887 struct kernfs_node
*kn
= kernfs_node_from_dentry(dentry
);
4888 struct file_system_type
*s_type
= dentry
->d_sb
->s_type
;
4889 struct cgroup_subsys_state
*css
= NULL
;
4890 struct cgroup
*cgrp
;
4892 /* is @dentry a cgroup dir? */
4893 if ((s_type
!= &cgroup_fs_type
&& s_type
!= &cgroup2_fs_type
) ||
4894 !kn
|| kernfs_type(kn
) != KERNFS_DIR
)
4895 return ERR_PTR(-EBADF
);
4900 * This path doesn't originate from kernfs and @kn could already
4901 * have been or be removed at any point. @kn->priv is RCU
4902 * protected for this access. See css_release_work_fn() for details.
4904 cgrp
= rcu_dereference(*(void __rcu __force
**)&kn
->priv
);
4906 css
= cgroup_css(cgrp
, ss
);
4908 if (!css
|| !css_tryget_online(css
))
4909 css
= ERR_PTR(-ENOENT
);
4916 * css_from_id - lookup css by id
4917 * @id: the cgroup id
4918 * @ss: cgroup subsys to be looked into
4920 * Returns the css if there's valid one with @id, otherwise returns NULL.
4921 * Should be called under rcu_read_lock().
4923 struct cgroup_subsys_state
*css_from_id(int id
, struct cgroup_subsys
*ss
)
4925 WARN_ON_ONCE(!rcu_read_lock_held());
4926 return idr_find(&ss
->css_idr
, id
);
4930 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
4931 * @path: path on the default hierarchy
4933 * Find the cgroup at @path on the default hierarchy, increment its
4934 * reference count and return it. Returns pointer to the found cgroup on
4935 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
4936 * if @path points to a non-directory.
4938 struct cgroup
*cgroup_get_from_path(const char *path
)
4940 struct kernfs_node
*kn
;
4941 struct cgroup
*cgrp
;
4943 mutex_lock(&cgroup_mutex
);
4945 kn
= kernfs_walk_and_get(cgrp_dfl_root
.cgrp
.kn
, path
);
4947 if (kernfs_type(kn
) == KERNFS_DIR
) {
4951 cgrp
= ERR_PTR(-ENOTDIR
);
4955 cgrp
= ERR_PTR(-ENOENT
);
4958 mutex_unlock(&cgroup_mutex
);
4961 EXPORT_SYMBOL_GPL(cgroup_get_from_path
);
4964 * cgroup_get_from_fd - get a cgroup pointer from a fd
4965 * @fd: fd obtained by open(cgroup2_dir)
4967 * Find the cgroup from a fd which should be obtained
4968 * by opening a cgroup directory. Returns a pointer to the
4969 * cgroup on success. ERR_PTR is returned if the cgroup
4972 struct cgroup
*cgroup_get_from_fd(int fd
)
4974 struct cgroup_subsys_state
*css
;
4975 struct cgroup
*cgrp
;
4980 return ERR_PTR(-EBADF
);
4982 css
= css_tryget_online_from_dir(f
->f_path
.dentry
, NULL
);
4985 return ERR_CAST(css
);
4988 if (!cgroup_on_dfl(cgrp
)) {
4990 return ERR_PTR(-EBADF
);
4995 EXPORT_SYMBOL_GPL(cgroup_get_from_fd
);
4998 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
4999 * definition in cgroup-defs.h.
5001 #ifdef CONFIG_SOCK_CGROUP_DATA
5003 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5005 DEFINE_SPINLOCK(cgroup_sk_update_lock
);
5006 static bool cgroup_sk_alloc_disabled __read_mostly
;
5008 void cgroup_sk_alloc_disable(void)
5010 if (cgroup_sk_alloc_disabled
)
5012 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5013 cgroup_sk_alloc_disabled
= true;
5018 #define cgroup_sk_alloc_disabled false
5022 void cgroup_sk_alloc(struct sock_cgroup_data
*skcd
)
5024 if (cgroup_sk_alloc_disabled
)
5027 /* Socket clone path */
5029 cgroup_get(sock_cgroup_ptr(skcd
));
5036 struct css_set
*cset
;
5038 cset
= task_css_set(current
);
5039 if (likely(cgroup_tryget(cset
->dfl_cgrp
))) {
5040 skcd
->val
= (unsigned long)cset
->dfl_cgrp
;
5049 void cgroup_sk_free(struct sock_cgroup_data
*skcd
)
5051 cgroup_put(sock_cgroup_ptr(skcd
));
5054 #endif /* CONFIG_SOCK_CGROUP_DATA */
5056 #ifdef CONFIG_CGROUP_BPF
5057 int cgroup_bpf_update(struct cgroup
*cgrp
, struct bpf_prog
*prog
,
5058 enum bpf_attach_type type
, bool overridable
)
5060 struct cgroup
*parent
= cgroup_parent(cgrp
);
5063 mutex_lock(&cgroup_mutex
);
5064 ret
= __cgroup_bpf_update(cgrp
, parent
, prog
, type
, overridable
);
5065 mutex_unlock(&cgroup_mutex
);
5068 #endif /* CONFIG_CGROUP_BPF */