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 <linux/cgroup.h>
32 #include <linux/cred.h>
33 #include <linux/ctype.h>
34 #include <linux/errno.h>
35 #include <linux/init_task.h>
36 #include <linux/kernel.h>
37 #include <linux/list.h>
38 #include <linux/magic.h>
40 #include <linux/mutex.h>
41 #include <linux/mount.h>
42 #include <linux/pagemap.h>
43 #include <linux/proc_fs.h>
44 #include <linux/rcupdate.h>
45 #include <linux/sched.h>
46 #include <linux/slab.h>
47 #include <linux/spinlock.h>
48 #include <linux/percpu-rwsem.h>
49 #include <linux/string.h>
50 #include <linux/sort.h>
51 #include <linux/kmod.h>
52 #include <linux/delayacct.h>
53 #include <linux/cgroupstats.h>
54 #include <linux/hashtable.h>
55 #include <linux/pid_namespace.h>
56 #include <linux/idr.h>
57 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
58 #include <linux/kthread.h>
59 #include <linux/delay.h>
61 #include <linux/atomic.h>
62 #include <linux/cpuset.h>
63 #include <linux/nsproxy.h>
64 #include <linux/proc_ns.h>
68 * pidlists linger the following amount before being destroyed. The goal
69 * is avoiding frequent destruction in the middle of consecutive read calls
70 * Expiring in the middle is a performance problem not a correctness one.
71 * 1 sec should be enough.
73 #define CGROUP_PIDLIST_DESTROY_DELAY HZ
75 #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
79 * cgroup_mutex is the master lock. Any modification to cgroup or its
80 * hierarchy must be performed while holding it.
82 * css_set_lock protects task->cgroups pointer, the list of css_set
83 * objects, and the chain of tasks off each css_set.
85 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
86 * cgroup.h can use them for lockdep annotations.
88 #ifdef CONFIG_PROVE_RCU
89 DEFINE_MUTEX(cgroup_mutex
);
90 DEFINE_SPINLOCK(css_set_lock
);
91 EXPORT_SYMBOL_GPL(cgroup_mutex
);
92 EXPORT_SYMBOL_GPL(css_set_lock
);
94 static DEFINE_MUTEX(cgroup_mutex
);
95 static DEFINE_SPINLOCK(css_set_lock
);
99 * Protects cgroup_idr and css_idr so that IDs can be released without
100 * grabbing cgroup_mutex.
102 static DEFINE_SPINLOCK(cgroup_idr_lock
);
105 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
106 * against file removal/re-creation across css hiding.
108 static DEFINE_SPINLOCK(cgroup_file_kn_lock
);
111 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
112 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
114 static DEFINE_SPINLOCK(release_agent_path_lock
);
116 struct percpu_rw_semaphore cgroup_threadgroup_rwsem
;
118 #define cgroup_assert_mutex_or_rcu_locked() \
119 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
120 !lockdep_is_held(&cgroup_mutex), \
121 "cgroup_mutex or RCU read lock required");
124 * cgroup destruction makes heavy use of work items and there can be a lot
125 * of concurrent destructions. Use a separate workqueue so that cgroup
126 * destruction work items don't end up filling up max_active of system_wq
127 * which may lead to deadlock.
129 static struct workqueue_struct
*cgroup_destroy_wq
;
132 * pidlist destructions need to be flushed on cgroup destruction. Use a
133 * separate workqueue as flush domain.
135 static struct workqueue_struct
*cgroup_pidlist_destroy_wq
;
137 /* generate an array of cgroup subsystem pointers */
138 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
139 static struct cgroup_subsys
*cgroup_subsys
[] = {
140 #include <linux/cgroup_subsys.h>
144 /* array of cgroup subsystem names */
145 #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
146 static const char *cgroup_subsys_name
[] = {
147 #include <linux/cgroup_subsys.h>
151 /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
153 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
154 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
155 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
156 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
157 #include <linux/cgroup_subsys.h>
160 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
161 static struct static_key_true
*cgroup_subsys_enabled_key
[] = {
162 #include <linux/cgroup_subsys.h>
166 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
167 static struct static_key_true
*cgroup_subsys_on_dfl_key
[] = {
168 #include <linux/cgroup_subsys.h>
173 * The default hierarchy, reserved for the subsystems that are otherwise
174 * unattached - it never has more than a single cgroup, and all tasks are
175 * part of that cgroup.
177 struct cgroup_root cgrp_dfl_root
;
178 EXPORT_SYMBOL_GPL(cgrp_dfl_root
);
181 * The default hierarchy always exists but is hidden until mounted for the
182 * first time. This is for backward compatibility.
184 static bool cgrp_dfl_root_visible
;
186 /* some controllers are not supported in the default hierarchy */
187 static unsigned long cgrp_dfl_root_inhibit_ss_mask
;
189 /* The list of hierarchy roots */
191 static LIST_HEAD(cgroup_roots
);
192 static int cgroup_root_count
;
194 /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
195 static DEFINE_IDR(cgroup_hierarchy_idr
);
198 * Assign a monotonically increasing serial number to csses. It guarantees
199 * cgroups with bigger numbers are newer than those with smaller numbers.
200 * Also, as csses are always appended to the parent's ->children list, it
201 * guarantees that sibling csses are always sorted in the ascending serial
202 * number order on the list. Protected by cgroup_mutex.
204 static u64 css_serial_nr_next
= 1;
207 * These bitmask flags indicate whether tasks in the fork and exit paths have
208 * fork/exit handlers to call. This avoids us having to do extra work in the
209 * fork/exit path to check which subsystems have fork/exit callbacks.
211 static unsigned long have_fork_callback __read_mostly
;
212 static unsigned long have_exit_callback __read_mostly
;
213 static unsigned long have_free_callback __read_mostly
;
215 /* Cgroup namespace for init task */
216 struct cgroup_namespace init_cgroup_ns
= {
217 .count
= { .counter
= 2, },
218 .user_ns
= &init_user_ns
,
219 .ns
.ops
= &cgroupns_operations
,
220 .ns
.inum
= PROC_CGROUP_INIT_INO
,
221 .root_cset
= &init_css_set
,
224 /* Ditto for the can_fork callback. */
225 static unsigned long have_canfork_callback __read_mostly
;
227 static struct cftype cgroup_dfl_base_files
[];
228 static struct cftype cgroup_legacy_base_files
[];
230 static int rebind_subsystems(struct cgroup_root
*dst_root
,
231 unsigned long ss_mask
);
232 static void css_task_iter_advance(struct css_task_iter
*it
);
233 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
234 static int create_css(struct cgroup
*cgrp
, struct cgroup_subsys
*ss
,
236 static void css_release(struct percpu_ref
*ref
);
237 static void kill_css(struct cgroup_subsys_state
*css
);
238 static int cgroup_addrm_files(struct cgroup_subsys_state
*css
,
239 struct cgroup
*cgrp
, struct cftype cfts
[],
243 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
244 * @ssid: subsys ID of interest
246 * cgroup_subsys_enabled() can only be used with literal subsys names which
247 * is fine for individual subsystems but unsuitable for cgroup core. This
248 * is slower static_key_enabled() based test indexed by @ssid.
250 static bool cgroup_ssid_enabled(int ssid
)
252 return static_key_enabled(cgroup_subsys_enabled_key
[ssid
]);
256 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
257 * @cgrp: the cgroup of interest
259 * The default hierarchy is the v2 interface of cgroup and this function
260 * can be used to test whether a cgroup is on the default hierarchy for
261 * cases where a subsystem should behave differnetly depending on the
264 * The set of behaviors which change on the default hierarchy are still
265 * being determined and the mount option is prefixed with __DEVEL__.
267 * List of changed behaviors:
269 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
270 * and "name" are disallowed.
272 * - When mounting an existing superblock, mount options should match.
274 * - Remount is disallowed.
276 * - rename(2) is disallowed.
278 * - "tasks" is removed. Everything should be at process granularity. Use
279 * "cgroup.procs" instead.
281 * - "cgroup.procs" is not sorted. pids will be unique unless they got
282 * recycled inbetween reads.
284 * - "release_agent" and "notify_on_release" are removed. Replacement
285 * notification mechanism will be implemented.
287 * - "cgroup.clone_children" is removed.
289 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
290 * and its descendants contain no task; otherwise, 1. The file also
291 * generates kernfs notification which can be monitored through poll and
292 * [di]notify when the value of the file changes.
294 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
295 * take masks of ancestors with non-empty cpus/mems, instead of being
296 * moved to an ancestor.
298 * - cpuset: a task can be moved into an empty cpuset, and again it takes
299 * masks of ancestors.
301 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
304 * - blkcg: blk-throttle becomes properly hierarchical.
306 * - debug: disallowed on the default hierarchy.
308 static bool cgroup_on_dfl(const struct cgroup
*cgrp
)
310 return cgrp
->root
== &cgrp_dfl_root
;
313 /* IDR wrappers which synchronize using cgroup_idr_lock */
314 static int cgroup_idr_alloc(struct idr
*idr
, void *ptr
, int start
, int end
,
319 idr_preload(gfp_mask
);
320 spin_lock_bh(&cgroup_idr_lock
);
321 ret
= idr_alloc(idr
, ptr
, start
, end
, gfp_mask
& ~__GFP_DIRECT_RECLAIM
);
322 spin_unlock_bh(&cgroup_idr_lock
);
327 static void *cgroup_idr_replace(struct idr
*idr
, void *ptr
, int id
)
331 spin_lock_bh(&cgroup_idr_lock
);
332 ret
= idr_replace(idr
, ptr
, id
);
333 spin_unlock_bh(&cgroup_idr_lock
);
337 static void cgroup_idr_remove(struct idr
*idr
, int id
)
339 spin_lock_bh(&cgroup_idr_lock
);
341 spin_unlock_bh(&cgroup_idr_lock
);
344 static struct cgroup
*cgroup_parent(struct cgroup
*cgrp
)
346 struct cgroup_subsys_state
*parent_css
= cgrp
->self
.parent
;
349 return container_of(parent_css
, struct cgroup
, self
);
354 * cgroup_css - obtain a cgroup's css for the specified subsystem
355 * @cgrp: the cgroup of interest
356 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
358 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
359 * function must be called either under cgroup_mutex or rcu_read_lock() and
360 * the caller is responsible for pinning the returned css if it wants to
361 * keep accessing it outside the said locks. This function may return
362 * %NULL if @cgrp doesn't have @subsys_id enabled.
364 static struct cgroup_subsys_state
*cgroup_css(struct cgroup
*cgrp
,
365 struct cgroup_subsys
*ss
)
368 return rcu_dereference_check(cgrp
->subsys
[ss
->id
],
369 lockdep_is_held(&cgroup_mutex
));
375 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
376 * @cgrp: the cgroup of interest
377 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
379 * Similar to cgroup_css() but returns the effective css, which is defined
380 * as the matching css of the nearest ancestor including self which has @ss
381 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
382 * function is guaranteed to return non-NULL css.
384 static struct cgroup_subsys_state
*cgroup_e_css(struct cgroup
*cgrp
,
385 struct cgroup_subsys
*ss
)
387 lockdep_assert_held(&cgroup_mutex
);
392 if (!(cgrp
->root
->subsys_mask
& (1 << ss
->id
)))
396 * This function is used while updating css associations and thus
397 * can't test the csses directly. Use ->child_subsys_mask.
399 while (cgroup_parent(cgrp
) &&
400 !(cgroup_parent(cgrp
)->child_subsys_mask
& (1 << ss
->id
)))
401 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 /* convenient tests for these bits */
440 static inline bool cgroup_is_dead(const struct cgroup
*cgrp
)
442 return !(cgrp
->self
.flags
& CSS_ONLINE
);
445 static void cgroup_get(struct cgroup
*cgrp
)
447 WARN_ON_ONCE(cgroup_is_dead(cgrp
));
448 css_get(&cgrp
->self
);
451 static bool cgroup_tryget(struct cgroup
*cgrp
)
453 return css_tryget(&cgrp
->self
);
456 static void cgroup_put(struct cgroup
*cgrp
)
458 css_put(&cgrp
->self
);
461 struct cgroup_subsys_state
*of_css(struct kernfs_open_file
*of
)
463 struct cgroup
*cgrp
= of
->kn
->parent
->priv
;
464 struct cftype
*cft
= of_cft(of
);
467 * This is open and unprotected implementation of cgroup_css().
468 * seq_css() is only called from a kernfs file operation which has
469 * an active reference on the file. Because all the subsystem
470 * files are drained before a css is disassociated with a cgroup,
471 * the matching css from the cgroup's subsys table is guaranteed to
472 * be and stay valid until the enclosing operation is complete.
475 return rcu_dereference_raw(cgrp
->subsys
[cft
->ss
->id
]);
479 EXPORT_SYMBOL_GPL(of_css
);
482 * cgroup_is_descendant - test ancestry
483 * @cgrp: the cgroup to be tested
484 * @ancestor: possible ancestor of @cgrp
486 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
487 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
488 * and @ancestor are accessible.
490 bool cgroup_is_descendant(struct cgroup
*cgrp
, struct cgroup
*ancestor
)
493 if (cgrp
== ancestor
)
495 cgrp
= cgroup_parent(cgrp
);
500 static int notify_on_release(const struct cgroup
*cgrp
)
502 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
506 * for_each_css - iterate all css's of a cgroup
507 * @css: the iteration cursor
508 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
509 * @cgrp: the target cgroup to iterate css's of
511 * Should be called under cgroup_[tree_]mutex.
513 #define for_each_css(css, ssid, cgrp) \
514 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
515 if (!((css) = rcu_dereference_check( \
516 (cgrp)->subsys[(ssid)], \
517 lockdep_is_held(&cgroup_mutex)))) { } \
521 * for_each_e_css - iterate all effective css's of a cgroup
522 * @css: the iteration cursor
523 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
524 * @cgrp: the target cgroup to iterate css's of
526 * Should be called under cgroup_[tree_]mutex.
528 #define for_each_e_css(css, ssid, cgrp) \
529 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
530 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
535 * for_each_subsys - iterate all enabled cgroup subsystems
536 * @ss: the iteration cursor
537 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
539 #define for_each_subsys(ss, ssid) \
540 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
541 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
544 * for_each_subsys_which - filter for_each_subsys with a bitmask
545 * @ss: the iteration cursor
546 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
547 * @ss_maskp: a pointer to the bitmask
549 * The block will only run for cases where the ssid-th bit (1 << ssid) of
552 #define for_each_subsys_which(ss, ssid, ss_maskp) \
553 if (!CGROUP_SUBSYS_COUNT) /* to avoid spurious gcc warning */ \
556 for_each_set_bit(ssid, ss_maskp, CGROUP_SUBSYS_COUNT) \
557 if (((ss) = cgroup_subsys[ssid]) && false) \
561 /* iterate across the hierarchies */
562 #define for_each_root(root) \
563 list_for_each_entry((root), &cgroup_roots, root_list)
565 /* iterate over child cgrps, lock should be held throughout iteration */
566 #define cgroup_for_each_live_child(child, cgrp) \
567 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
568 if (({ lockdep_assert_held(&cgroup_mutex); \
569 cgroup_is_dead(child); })) \
573 static void cgroup_release_agent(struct work_struct
*work
);
574 static void check_for_release(struct cgroup
*cgrp
);
577 * A cgroup can be associated with multiple css_sets as different tasks may
578 * belong to different cgroups on different hierarchies. In the other
579 * direction, a css_set is naturally associated with multiple cgroups.
580 * This M:N relationship is represented by the following link structure
581 * which exists for each association and allows traversing the associations
584 struct cgrp_cset_link
{
585 /* the cgroup and css_set this link associates */
587 struct css_set
*cset
;
589 /* list of cgrp_cset_links anchored at cgrp->cset_links */
590 struct list_head cset_link
;
592 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
593 struct list_head cgrp_link
;
597 * The default css_set - used by init and its children prior to any
598 * hierarchies being mounted. It contains a pointer to the root state
599 * for each subsystem. Also used to anchor the list of css_sets. Not
600 * reference-counted, to improve performance when child cgroups
601 * haven't been created.
603 struct css_set init_css_set
= {
604 .refcount
= ATOMIC_INIT(1),
605 .cgrp_links
= LIST_HEAD_INIT(init_css_set
.cgrp_links
),
606 .tasks
= LIST_HEAD_INIT(init_css_set
.tasks
),
607 .mg_tasks
= LIST_HEAD_INIT(init_css_set
.mg_tasks
),
608 .mg_preload_node
= LIST_HEAD_INIT(init_css_set
.mg_preload_node
),
609 .mg_node
= LIST_HEAD_INIT(init_css_set
.mg_node
),
610 .task_iters
= LIST_HEAD_INIT(init_css_set
.task_iters
),
613 static int css_set_count
= 1; /* 1 for init_css_set */
616 * css_set_populated - does a css_set contain any tasks?
617 * @cset: target css_set
619 static bool css_set_populated(struct css_set
*cset
)
621 lockdep_assert_held(&css_set_lock
);
623 return !list_empty(&cset
->tasks
) || !list_empty(&cset
->mg_tasks
);
627 * cgroup_update_populated - updated populated count of a cgroup
628 * @cgrp: the target cgroup
629 * @populated: inc or dec populated count
631 * One of the css_sets associated with @cgrp is either getting its first
632 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
633 * count is propagated towards root so that a given cgroup's populated_cnt
634 * is zero iff the cgroup and all its descendants don't contain any tasks.
636 * @cgrp's interface file "cgroup.populated" is zero if
637 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
638 * changes from or to zero, userland is notified that the content of the
639 * interface file has changed. This can be used to detect when @cgrp and
640 * its descendants become populated or empty.
642 static void cgroup_update_populated(struct cgroup
*cgrp
, bool populated
)
644 lockdep_assert_held(&css_set_lock
);
650 trigger
= !cgrp
->populated_cnt
++;
652 trigger
= !--cgrp
->populated_cnt
;
657 check_for_release(cgrp
);
658 cgroup_file_notify(&cgrp
->events_file
);
660 cgrp
= cgroup_parent(cgrp
);
665 * css_set_update_populated - update populated state of a css_set
666 * @cset: target css_set
667 * @populated: whether @cset is populated or depopulated
669 * @cset is either getting the first task or losing the last. Update the
670 * ->populated_cnt of all associated cgroups accordingly.
672 static void css_set_update_populated(struct css_set
*cset
, bool populated
)
674 struct cgrp_cset_link
*link
;
676 lockdep_assert_held(&css_set_lock
);
678 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
)
679 cgroup_update_populated(link
->cgrp
, populated
);
683 * css_set_move_task - move a task from one css_set to another
684 * @task: task being moved
685 * @from_cset: css_set @task currently belongs to (may be NULL)
686 * @to_cset: new css_set @task is being moved to (may be NULL)
687 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
689 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
690 * css_set, @from_cset can be NULL. If @task is being disassociated
691 * instead of moved, @to_cset can be NULL.
693 * This function automatically handles populated_cnt updates and
694 * css_task_iter adjustments but the caller is responsible for managing
695 * @from_cset and @to_cset's reference counts.
697 static void css_set_move_task(struct task_struct
*task
,
698 struct css_set
*from_cset
, struct css_set
*to_cset
,
701 lockdep_assert_held(&css_set_lock
);
704 struct css_task_iter
*it
, *pos
;
706 WARN_ON_ONCE(list_empty(&task
->cg_list
));
709 * @task is leaving, advance task iterators which are
710 * pointing to it so that they can resume at the next
711 * position. Advancing an iterator might remove it from
712 * the list, use safe walk. See css_task_iter_advance*()
715 list_for_each_entry_safe(it
, pos
, &from_cset
->task_iters
,
717 if (it
->task_pos
== &task
->cg_list
)
718 css_task_iter_advance(it
);
720 list_del_init(&task
->cg_list
);
721 if (!css_set_populated(from_cset
))
722 css_set_update_populated(from_cset
, false);
724 WARN_ON_ONCE(!list_empty(&task
->cg_list
));
729 * We are synchronized through cgroup_threadgroup_rwsem
730 * against PF_EXITING setting such that we can't race
731 * against cgroup_exit() changing the css_set to
732 * init_css_set and dropping the old one.
734 WARN_ON_ONCE(task
->flags
& PF_EXITING
);
736 if (!css_set_populated(to_cset
))
737 css_set_update_populated(to_cset
, true);
738 rcu_assign_pointer(task
->cgroups
, to_cset
);
739 list_add_tail(&task
->cg_list
, use_mg_tasks
? &to_cset
->mg_tasks
:
745 * hash table for cgroup groups. This improves the performance to find
746 * an existing css_set. This hash doesn't (currently) take into
747 * account cgroups in empty hierarchies.
749 #define CSS_SET_HASH_BITS 7
750 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
752 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
754 unsigned long key
= 0UL;
755 struct cgroup_subsys
*ss
;
758 for_each_subsys(ss
, i
)
759 key
+= (unsigned long)css
[i
];
760 key
= (key
>> 16) ^ key
;
765 static void put_css_set_locked(struct css_set
*cset
)
767 struct cgrp_cset_link
*link
, *tmp_link
;
768 struct cgroup_subsys
*ss
;
771 lockdep_assert_held(&css_set_lock
);
773 if (!atomic_dec_and_test(&cset
->refcount
))
776 /* This css_set is dead. unlink it and release cgroup and css refs */
777 for_each_subsys(ss
, ssid
) {
778 list_del(&cset
->e_cset_node
[ssid
]);
779 css_put(cset
->subsys
[ssid
]);
781 hash_del(&cset
->hlist
);
784 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
785 list_del(&link
->cset_link
);
786 list_del(&link
->cgrp_link
);
787 if (cgroup_parent(link
->cgrp
))
788 cgroup_put(link
->cgrp
);
792 kfree_rcu(cset
, rcu_head
);
795 static void put_css_set(struct css_set
*cset
)
798 * Ensure that the refcount doesn't hit zero while any readers
799 * can see it. Similar to atomic_dec_and_lock(), but for an
802 if (atomic_add_unless(&cset
->refcount
, -1, 1))
805 spin_lock_bh(&css_set_lock
);
806 put_css_set_locked(cset
);
807 spin_unlock_bh(&css_set_lock
);
811 * refcounted get/put for css_set objects
813 static inline void get_css_set(struct css_set
*cset
)
815 atomic_inc(&cset
->refcount
);
819 * compare_css_sets - helper function for find_existing_css_set().
820 * @cset: candidate css_set being tested
821 * @old_cset: existing css_set for a task
822 * @new_cgrp: cgroup that's being entered by the task
823 * @template: desired set of css pointers in css_set (pre-calculated)
825 * Returns true if "cset" matches "old_cset" except for the hierarchy
826 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
828 static bool compare_css_sets(struct css_set
*cset
,
829 struct css_set
*old_cset
,
830 struct cgroup
*new_cgrp
,
831 struct cgroup_subsys_state
*template[])
833 struct list_head
*l1
, *l2
;
836 * On the default hierarchy, there can be csets which are
837 * associated with the same set of cgroups but different csses.
838 * Let's first ensure that csses match.
840 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
)))
844 * Compare cgroup pointers in order to distinguish between
845 * different cgroups in hierarchies. As different cgroups may
846 * share the same effective css, this comparison is always
849 l1
= &cset
->cgrp_links
;
850 l2
= &old_cset
->cgrp_links
;
852 struct cgrp_cset_link
*link1
, *link2
;
853 struct cgroup
*cgrp1
, *cgrp2
;
857 /* See if we reached the end - both lists are equal length. */
858 if (l1
== &cset
->cgrp_links
) {
859 BUG_ON(l2
!= &old_cset
->cgrp_links
);
862 BUG_ON(l2
== &old_cset
->cgrp_links
);
864 /* Locate the cgroups associated with these links. */
865 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
866 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
869 /* Hierarchies should be linked in the same order. */
870 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
873 * If this hierarchy is the hierarchy of the cgroup
874 * that's changing, then we need to check that this
875 * css_set points to the new cgroup; if it's any other
876 * hierarchy, then this css_set should point to the
877 * same cgroup as the old css_set.
879 if (cgrp1
->root
== new_cgrp
->root
) {
880 if (cgrp1
!= new_cgrp
)
891 * find_existing_css_set - init css array and find the matching css_set
892 * @old_cset: the css_set that we're using before the cgroup transition
893 * @cgrp: the cgroup that we're moving into
894 * @template: out param for the new set of csses, should be clear on entry
896 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
898 struct cgroup_subsys_state
*template[])
900 struct cgroup_root
*root
= cgrp
->root
;
901 struct cgroup_subsys
*ss
;
902 struct css_set
*cset
;
907 * Build the set of subsystem state objects that we want to see in the
908 * new css_set. while subsystems can change globally, the entries here
909 * won't change, so no need for locking.
911 for_each_subsys(ss
, i
) {
912 if (root
->subsys_mask
& (1UL << i
)) {
914 * @ss is in this hierarchy, so we want the
915 * effective css from @cgrp.
917 template[i
] = cgroup_e_css(cgrp
, ss
);
920 * @ss is not in this hierarchy, so we don't want
923 template[i
] = old_cset
->subsys
[i
];
927 key
= css_set_hash(template);
928 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
929 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
932 /* This css_set matches what we need */
936 /* No existing cgroup group matched */
940 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
942 struct cgrp_cset_link
*link
, *tmp_link
;
944 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
945 list_del(&link
->cset_link
);
951 * allocate_cgrp_cset_links - allocate cgrp_cset_links
952 * @count: the number of links to allocate
953 * @tmp_links: list_head the allocated links are put on
955 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
956 * through ->cset_link. Returns 0 on success or -errno.
958 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
960 struct cgrp_cset_link
*link
;
963 INIT_LIST_HEAD(tmp_links
);
965 for (i
= 0; i
< count
; i
++) {
966 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
968 free_cgrp_cset_links(tmp_links
);
971 list_add(&link
->cset_link
, tmp_links
);
977 * link_css_set - a helper function to link a css_set to a cgroup
978 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
979 * @cset: the css_set to be linked
980 * @cgrp: the destination cgroup
982 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
985 struct cgrp_cset_link
*link
;
987 BUG_ON(list_empty(tmp_links
));
989 if (cgroup_on_dfl(cgrp
))
990 cset
->dfl_cgrp
= cgrp
;
992 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
997 * Always add links to the tail of the lists so that the lists are
998 * in choronological order.
1000 list_move_tail(&link
->cset_link
, &cgrp
->cset_links
);
1001 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
1003 if (cgroup_parent(cgrp
))
1008 * find_css_set - return a new css_set with one cgroup updated
1009 * @old_cset: the baseline css_set
1010 * @cgrp: the cgroup to be updated
1012 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
1013 * substituted into the appropriate hierarchy.
1015 static struct css_set
*find_css_set(struct css_set
*old_cset
,
1016 struct cgroup
*cgrp
)
1018 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
1019 struct css_set
*cset
;
1020 struct list_head tmp_links
;
1021 struct cgrp_cset_link
*link
;
1022 struct cgroup_subsys
*ss
;
1026 lockdep_assert_held(&cgroup_mutex
);
1028 /* First see if we already have a cgroup group that matches
1029 * the desired set */
1030 spin_lock_bh(&css_set_lock
);
1031 cset
= find_existing_css_set(old_cset
, cgrp
, template);
1034 spin_unlock_bh(&css_set_lock
);
1039 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
1043 /* Allocate all the cgrp_cset_link objects that we'll need */
1044 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
1049 atomic_set(&cset
->refcount
, 1);
1050 INIT_LIST_HEAD(&cset
->cgrp_links
);
1051 INIT_LIST_HEAD(&cset
->tasks
);
1052 INIT_LIST_HEAD(&cset
->mg_tasks
);
1053 INIT_LIST_HEAD(&cset
->mg_preload_node
);
1054 INIT_LIST_HEAD(&cset
->mg_node
);
1055 INIT_LIST_HEAD(&cset
->task_iters
);
1056 INIT_HLIST_NODE(&cset
->hlist
);
1058 /* Copy the set of subsystem state objects generated in
1059 * find_existing_css_set() */
1060 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
1062 spin_lock_bh(&css_set_lock
);
1063 /* Add reference counts and links from the new css_set. */
1064 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
1065 struct cgroup
*c
= link
->cgrp
;
1067 if (c
->root
== cgrp
->root
)
1069 link_css_set(&tmp_links
, cset
, c
);
1072 BUG_ON(!list_empty(&tmp_links
));
1076 /* Add @cset to the hash table */
1077 key
= css_set_hash(cset
->subsys
);
1078 hash_add(css_set_table
, &cset
->hlist
, key
);
1080 for_each_subsys(ss
, ssid
) {
1081 struct cgroup_subsys_state
*css
= cset
->subsys
[ssid
];
1083 list_add_tail(&cset
->e_cset_node
[ssid
],
1084 &css
->cgroup
->e_csets
[ssid
]);
1088 spin_unlock_bh(&css_set_lock
);
1093 static struct cgroup_root
*cgroup_root_from_kf(struct kernfs_root
*kf_root
)
1095 struct cgroup
*root_cgrp
= kf_root
->kn
->priv
;
1097 return root_cgrp
->root
;
1100 static int cgroup_init_root_id(struct cgroup_root
*root
)
1104 lockdep_assert_held(&cgroup_mutex
);
1106 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, 0, 0, GFP_KERNEL
);
1110 root
->hierarchy_id
= id
;
1114 static void cgroup_exit_root_id(struct cgroup_root
*root
)
1116 lockdep_assert_held(&cgroup_mutex
);
1118 if (root
->hierarchy_id
) {
1119 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1120 root
->hierarchy_id
= 0;
1124 static void cgroup_free_root(struct cgroup_root
*root
)
1127 /* hierarchy ID should already have been released */
1128 WARN_ON_ONCE(root
->hierarchy_id
);
1130 idr_destroy(&root
->cgroup_idr
);
1135 static void cgroup_destroy_root(struct cgroup_root
*root
)
1137 struct cgroup
*cgrp
= &root
->cgrp
;
1138 struct cgrp_cset_link
*link
, *tmp_link
;
1140 mutex_lock(&cgroup_mutex
);
1142 BUG_ON(atomic_read(&root
->nr_cgrps
));
1143 BUG_ON(!list_empty(&cgrp
->self
.children
));
1145 /* Rebind all subsystems back to the default hierarchy */
1146 rebind_subsystems(&cgrp_dfl_root
, root
->subsys_mask
);
1149 * Release all the links from cset_links to this hierarchy's
1152 spin_lock_bh(&css_set_lock
);
1154 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1155 list_del(&link
->cset_link
);
1156 list_del(&link
->cgrp_link
);
1160 spin_unlock_bh(&css_set_lock
);
1162 if (!list_empty(&root
->root_list
)) {
1163 list_del(&root
->root_list
);
1164 cgroup_root_count
--;
1167 cgroup_exit_root_id(root
);
1169 mutex_unlock(&cgroup_mutex
);
1171 kernfs_destroy_root(root
->kf_root
);
1172 cgroup_free_root(root
);
1175 /* look up cgroup associated with given css_set on the specified hierarchy */
1176 static struct cgroup
*cset_cgroup_from_root(struct css_set
*cset
,
1177 struct cgroup_root
*root
)
1179 struct cgroup
*res
= NULL
;
1181 lockdep_assert_held(&cgroup_mutex
);
1182 lockdep_assert_held(&css_set_lock
);
1184 if (cset
== &init_css_set
) {
1187 struct cgrp_cset_link
*link
;
1189 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
1190 struct cgroup
*c
= link
->cgrp
;
1192 if (c
->root
== root
) {
1204 * Return the cgroup for "task" from the given hierarchy. Must be
1205 * called with cgroup_mutex and css_set_lock held.
1207 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
1208 struct cgroup_root
*root
)
1211 * No need to lock the task - since we hold cgroup_mutex the
1212 * task can't change groups, so the only thing that can happen
1213 * is that it exits and its css is set back to init_css_set.
1215 return cset_cgroup_from_root(task_css_set(task
), root
);
1219 * A task must hold cgroup_mutex to modify cgroups.
1221 * Any task can increment and decrement the count field without lock.
1222 * So in general, code holding cgroup_mutex can't rely on the count
1223 * field not changing. However, if the count goes to zero, then only
1224 * cgroup_attach_task() can increment it again. Because a count of zero
1225 * means that no tasks are currently attached, therefore there is no
1226 * way a task attached to that cgroup can fork (the other way to
1227 * increment the count). So code holding cgroup_mutex can safely
1228 * assume that if the count is zero, it will stay zero. Similarly, if
1229 * a task holds cgroup_mutex on a cgroup with zero count, it
1230 * knows that the cgroup won't be removed, as cgroup_rmdir()
1233 * A cgroup can only be deleted if both its 'count' of using tasks
1234 * is zero, and its list of 'children' cgroups is empty. Since all
1235 * tasks in the system use _some_ cgroup, and since there is always at
1236 * least one task in the system (init, pid == 1), therefore, root cgroup
1237 * always has either children cgroups and/or using tasks. So we don't
1238 * need a special hack to ensure that root cgroup cannot be deleted.
1240 * P.S. One more locking exception. RCU is used to guard the
1241 * update of a tasks cgroup pointer by cgroup_attach_task()
1244 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
;
1245 static const struct file_operations proc_cgroupstats_operations
;
1247 static char *cgroup_file_name(struct cgroup
*cgrp
, const struct cftype
*cft
,
1250 struct cgroup_subsys
*ss
= cft
->ss
;
1252 if (cft
->ss
&& !(cft
->flags
& CFTYPE_NO_PREFIX
) &&
1253 !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
))
1254 snprintf(buf
, CGROUP_FILE_NAME_MAX
, "%s.%s",
1255 cgroup_on_dfl(cgrp
) ? ss
->name
: ss
->legacy_name
,
1258 strncpy(buf
, cft
->name
, CGROUP_FILE_NAME_MAX
);
1263 * cgroup_file_mode - deduce file mode of a control file
1264 * @cft: the control file in question
1266 * S_IRUGO for read, S_IWUSR for write.
1268 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
1272 if (cft
->read_u64
|| cft
->read_s64
|| cft
->seq_show
)
1275 if (cft
->write_u64
|| cft
->write_s64
|| cft
->write
) {
1276 if (cft
->flags
& CFTYPE_WORLD_WRITABLE
)
1286 * cgroup_calc_child_subsys_mask - calculate child_subsys_mask
1287 * @cgrp: the target cgroup
1288 * @subtree_control: the new subtree_control mask to consider
1290 * On the default hierarchy, a subsystem may request other subsystems to be
1291 * enabled together through its ->depends_on mask. In such cases, more
1292 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1294 * This function calculates which subsystems need to be enabled if
1295 * @subtree_control is to be applied to @cgrp. The returned mask is always
1296 * a superset of @subtree_control and follows the usual hierarchy rules.
1298 static unsigned long cgroup_calc_child_subsys_mask(struct cgroup
*cgrp
,
1299 unsigned long subtree_control
)
1301 struct cgroup
*parent
= cgroup_parent(cgrp
);
1302 unsigned long cur_ss_mask
= subtree_control
;
1303 struct cgroup_subsys
*ss
;
1306 lockdep_assert_held(&cgroup_mutex
);
1308 if (!cgroup_on_dfl(cgrp
))
1312 unsigned long new_ss_mask
= cur_ss_mask
;
1314 for_each_subsys_which(ss
, ssid
, &cur_ss_mask
)
1315 new_ss_mask
|= ss
->depends_on
;
1318 * Mask out subsystems which aren't available. This can
1319 * happen only if some depended-upon subsystems were bound
1320 * to non-default hierarchies.
1323 new_ss_mask
&= parent
->child_subsys_mask
;
1325 new_ss_mask
&= cgrp
->root
->subsys_mask
;
1327 if (new_ss_mask
== cur_ss_mask
)
1329 cur_ss_mask
= new_ss_mask
;
1336 * cgroup_refresh_child_subsys_mask - update child_subsys_mask
1337 * @cgrp: the target cgroup
1339 * Update @cgrp->child_subsys_mask according to the current
1340 * @cgrp->subtree_control using cgroup_calc_child_subsys_mask().
1342 static void cgroup_refresh_child_subsys_mask(struct cgroup
*cgrp
)
1344 cgrp
->child_subsys_mask
=
1345 cgroup_calc_child_subsys_mask(cgrp
, cgrp
->subtree_control
);
1349 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1350 * @kn: the kernfs_node being serviced
1352 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1353 * the method finishes if locking succeeded. Note that once this function
1354 * returns the cgroup returned by cgroup_kn_lock_live() may become
1355 * inaccessible any time. If the caller intends to continue to access the
1356 * cgroup, it should pin it before invoking this function.
1358 static void cgroup_kn_unlock(struct kernfs_node
*kn
)
1360 struct cgroup
*cgrp
;
1362 if (kernfs_type(kn
) == KERNFS_DIR
)
1365 cgrp
= kn
->parent
->priv
;
1367 mutex_unlock(&cgroup_mutex
);
1369 kernfs_unbreak_active_protection(kn
);
1374 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1375 * @kn: the kernfs_node being serviced
1377 * This helper is to be used by a cgroup kernfs method currently servicing
1378 * @kn. It breaks the active protection, performs cgroup locking and
1379 * verifies that the associated cgroup is alive. Returns the cgroup if
1380 * alive; otherwise, %NULL. A successful return should be undone by a
1381 * matching cgroup_kn_unlock() invocation.
1383 * Any cgroup kernfs method implementation which requires locking the
1384 * associated cgroup should use this helper. It avoids nesting cgroup
1385 * locking under kernfs active protection and allows all kernfs operations
1386 * including self-removal.
1388 static struct cgroup
*cgroup_kn_lock_live(struct kernfs_node
*kn
)
1390 struct cgroup
*cgrp
;
1392 if (kernfs_type(kn
) == KERNFS_DIR
)
1395 cgrp
= kn
->parent
->priv
;
1398 * We're gonna grab cgroup_mutex which nests outside kernfs
1399 * active_ref. cgroup liveliness check alone provides enough
1400 * protection against removal. Ensure @cgrp stays accessible and
1401 * break the active_ref protection.
1403 if (!cgroup_tryget(cgrp
))
1405 kernfs_break_active_protection(kn
);
1407 mutex_lock(&cgroup_mutex
);
1409 if (!cgroup_is_dead(cgrp
))
1412 cgroup_kn_unlock(kn
);
1416 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
1418 char name
[CGROUP_FILE_NAME_MAX
];
1420 lockdep_assert_held(&cgroup_mutex
);
1422 if (cft
->file_offset
) {
1423 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, cft
->ss
);
1424 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
1426 spin_lock_irq(&cgroup_file_kn_lock
);
1428 spin_unlock_irq(&cgroup_file_kn_lock
);
1431 kernfs_remove_by_name(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
));
1435 * css_clear_dir - remove subsys files in a cgroup directory
1437 * @cgrp_override: specify if target cgroup is different from css->cgroup
1439 static void css_clear_dir(struct cgroup_subsys_state
*css
,
1440 struct cgroup
*cgrp_override
)
1442 struct cgroup
*cgrp
= cgrp_override
?: css
->cgroup
;
1443 struct cftype
*cfts
;
1445 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
)
1446 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1450 * css_populate_dir - create subsys files in a cgroup directory
1452 * @cgrp_overried: specify if target cgroup is different from css->cgroup
1454 * On failure, no file is added.
1456 static int css_populate_dir(struct cgroup_subsys_state
*css
,
1457 struct cgroup
*cgrp_override
)
1459 struct cgroup
*cgrp
= cgrp_override
?: css
->cgroup
;
1460 struct cftype
*cfts
, *failed_cfts
;
1464 if (cgroup_on_dfl(cgrp
))
1465 cfts
= cgroup_dfl_base_files
;
1467 cfts
= cgroup_legacy_base_files
;
1469 return cgroup_addrm_files(&cgrp
->self
, cgrp
, cfts
, true);
1472 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1473 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, true);
1481 list_for_each_entry(cfts
, &css
->ss
->cfts
, node
) {
1482 if (cfts
== failed_cfts
)
1484 cgroup_addrm_files(css
, cgrp
, cfts
, false);
1489 static int rebind_subsystems(struct cgroup_root
*dst_root
,
1490 unsigned long ss_mask
)
1492 struct cgroup
*dcgrp
= &dst_root
->cgrp
;
1493 struct cgroup_subsys
*ss
;
1494 unsigned long tmp_ss_mask
;
1497 lockdep_assert_held(&cgroup_mutex
);
1499 for_each_subsys_which(ss
, ssid
, &ss_mask
) {
1500 /* if @ss has non-root csses attached to it, can't move */
1501 if (css_next_child(NULL
, cgroup_css(&ss
->root
->cgrp
, ss
)))
1504 /* can't move between two non-dummy roots either */
1505 if (ss
->root
!= &cgrp_dfl_root
&& dst_root
!= &cgrp_dfl_root
)
1509 /* skip creating root files on dfl_root for inhibited subsystems */
1510 tmp_ss_mask
= ss_mask
;
1511 if (dst_root
== &cgrp_dfl_root
)
1512 tmp_ss_mask
&= ~cgrp_dfl_root_inhibit_ss_mask
;
1514 for_each_subsys_which(ss
, ssid
, &tmp_ss_mask
) {
1515 struct cgroup
*scgrp
= &ss
->root
->cgrp
;
1518 ret
= css_populate_dir(cgroup_css(scgrp
, ss
), dcgrp
);
1523 * Rebinding back to the default root is not allowed to
1524 * fail. Using both default and non-default roots should
1525 * be rare. Moving subsystems back and forth even more so.
1526 * Just warn about it and continue.
1528 if (dst_root
== &cgrp_dfl_root
) {
1529 if (cgrp_dfl_root_visible
) {
1530 pr_warn("failed to create files (%d) while rebinding 0x%lx to default root\n",
1532 pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
1537 for_each_subsys_which(ss
, tssid
, &tmp_ss_mask
) {
1540 css_clear_dir(cgroup_css(scgrp
, ss
), dcgrp
);
1546 * Nothing can fail from this point on. Remove files for the
1547 * removed subsystems and rebind each subsystem.
1549 for_each_subsys_which(ss
, ssid
, &ss_mask
) {
1550 struct cgroup_root
*src_root
= ss
->root
;
1551 struct cgroup
*scgrp
= &src_root
->cgrp
;
1552 struct cgroup_subsys_state
*css
= cgroup_css(scgrp
, ss
);
1553 struct css_set
*cset
;
1555 WARN_ON(!css
|| cgroup_css(dcgrp
, ss
));
1557 css_clear_dir(css
, NULL
);
1559 RCU_INIT_POINTER(scgrp
->subsys
[ssid
], NULL
);
1560 rcu_assign_pointer(dcgrp
->subsys
[ssid
], css
);
1561 ss
->root
= dst_root
;
1562 css
->cgroup
= dcgrp
;
1564 spin_lock_bh(&css_set_lock
);
1565 hash_for_each(css_set_table
, i
, cset
, hlist
)
1566 list_move_tail(&cset
->e_cset_node
[ss
->id
],
1567 &dcgrp
->e_csets
[ss
->id
]);
1568 spin_unlock_bh(&css_set_lock
);
1570 src_root
->subsys_mask
&= ~(1 << ssid
);
1571 scgrp
->subtree_control
&= ~(1 << ssid
);
1572 cgroup_refresh_child_subsys_mask(scgrp
);
1574 /* default hierarchy doesn't enable controllers by default */
1575 dst_root
->subsys_mask
|= 1 << ssid
;
1576 if (dst_root
== &cgrp_dfl_root
) {
1577 static_branch_enable(cgroup_subsys_on_dfl_key
[ssid
]);
1579 dcgrp
->subtree_control
|= 1 << ssid
;
1580 cgroup_refresh_child_subsys_mask(dcgrp
);
1581 static_branch_disable(cgroup_subsys_on_dfl_key
[ssid
]);
1588 kernfs_activate(dcgrp
->kn
);
1592 static int cgroup_show_options(struct seq_file
*seq
,
1593 struct kernfs_root
*kf_root
)
1595 struct cgroup_root
*root
= cgroup_root_from_kf(kf_root
);
1596 struct cgroup_subsys
*ss
;
1599 if (root
!= &cgrp_dfl_root
)
1600 for_each_subsys(ss
, ssid
)
1601 if (root
->subsys_mask
& (1 << ssid
))
1602 seq_show_option(seq
, ss
->legacy_name
, NULL
);
1603 if (root
->flags
& CGRP_ROOT_NOPREFIX
)
1604 seq_puts(seq
, ",noprefix");
1605 if (root
->flags
& CGRP_ROOT_XATTR
)
1606 seq_puts(seq
, ",xattr");
1608 spin_lock(&release_agent_path_lock
);
1609 if (strlen(root
->release_agent_path
))
1610 seq_show_option(seq
, "release_agent",
1611 root
->release_agent_path
);
1612 spin_unlock(&release_agent_path_lock
);
1614 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->cgrp
.flags
))
1615 seq_puts(seq
, ",clone_children");
1616 if (strlen(root
->name
))
1617 seq_show_option(seq
, "name", root
->name
);
1621 struct cgroup_sb_opts
{
1622 unsigned long subsys_mask
;
1624 char *release_agent
;
1625 bool cpuset_clone_children
;
1627 /* User explicitly requested empty subsystem */
1631 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1633 char *token
, *o
= data
;
1634 bool all_ss
= false, one_ss
= false;
1635 unsigned long mask
= -1UL;
1636 struct cgroup_subsys
*ss
;
1640 #ifdef CONFIG_CPUSETS
1641 mask
= ~(1U << cpuset_cgrp_id
);
1644 memset(opts
, 0, sizeof(*opts
));
1646 while ((token
= strsep(&o
, ",")) != NULL
) {
1651 if (!strcmp(token
, "none")) {
1652 /* Explicitly have no subsystems */
1656 if (!strcmp(token
, "all")) {
1657 /* Mutually exclusive option 'all' + subsystem name */
1663 if (!strcmp(token
, "__DEVEL__sane_behavior")) {
1664 opts
->flags
|= CGRP_ROOT_SANE_BEHAVIOR
;
1667 if (!strcmp(token
, "noprefix")) {
1668 opts
->flags
|= CGRP_ROOT_NOPREFIX
;
1671 if (!strcmp(token
, "clone_children")) {
1672 opts
->cpuset_clone_children
= true;
1675 if (!strcmp(token
, "xattr")) {
1676 opts
->flags
|= CGRP_ROOT_XATTR
;
1679 if (!strncmp(token
, "release_agent=", 14)) {
1680 /* Specifying two release agents is forbidden */
1681 if (opts
->release_agent
)
1683 opts
->release_agent
=
1684 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1685 if (!opts
->release_agent
)
1689 if (!strncmp(token
, "name=", 5)) {
1690 const char *name
= token
+ 5;
1691 /* Can't specify an empty name */
1694 /* Must match [\w.-]+ */
1695 for (i
= 0; i
< strlen(name
); i
++) {
1699 if ((c
== '.') || (c
== '-') || (c
== '_'))
1703 /* Specifying two names is forbidden */
1706 opts
->name
= kstrndup(name
,
1707 MAX_CGROUP_ROOT_NAMELEN
- 1,
1715 for_each_subsys(ss
, i
) {
1716 if (strcmp(token
, ss
->legacy_name
))
1718 if (!cgroup_ssid_enabled(i
))
1721 /* Mutually exclusive option 'all' + subsystem name */
1724 opts
->subsys_mask
|= (1 << i
);
1729 if (i
== CGROUP_SUBSYS_COUNT
)
1733 if (opts
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1734 pr_warn("sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1736 pr_err("sane_behavior: no other mount options allowed\n");
1743 * If the 'all' option was specified select all the subsystems,
1744 * otherwise if 'none', 'name=' and a subsystem name options were
1745 * not specified, let's default to 'all'
1747 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
))
1748 for_each_subsys(ss
, i
)
1749 if (cgroup_ssid_enabled(i
))
1750 opts
->subsys_mask
|= (1 << i
);
1753 * We either have to specify by name or by subsystems. (So all
1754 * empty hierarchies must have a name).
1756 if (!opts
->subsys_mask
&& !opts
->name
)
1760 * Option noprefix was introduced just for backward compatibility
1761 * with the old cpuset, so we allow noprefix only if mounting just
1762 * the cpuset subsystem.
1764 if ((opts
->flags
& CGRP_ROOT_NOPREFIX
) && (opts
->subsys_mask
& mask
))
1767 /* Can't specify "none" and some subsystems */
1768 if (opts
->subsys_mask
&& opts
->none
)
1774 static int cgroup_remount(struct kernfs_root
*kf_root
, int *flags
, char *data
)
1777 struct cgroup_root
*root
= cgroup_root_from_kf(kf_root
);
1778 struct cgroup_sb_opts opts
;
1779 unsigned long added_mask
, removed_mask
;
1781 if (root
== &cgrp_dfl_root
) {
1782 pr_err("remount is not allowed\n");
1786 mutex_lock(&cgroup_mutex
);
1788 /* See what subsystems are wanted */
1789 ret
= parse_cgroupfs_options(data
, &opts
);
1793 if (opts
.subsys_mask
!= root
->subsys_mask
|| opts
.release_agent
)
1794 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
1795 task_tgid_nr(current
), current
->comm
);
1797 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1798 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1800 /* Don't allow flags or name to change at remount */
1801 if ((opts
.flags
^ root
->flags
) ||
1802 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1803 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
1804 opts
.flags
, opts
.name
?: "", root
->flags
, root
->name
);
1809 /* remounting is not allowed for populated hierarchies */
1810 if (!list_empty(&root
->cgrp
.self
.children
)) {
1815 ret
= rebind_subsystems(root
, added_mask
);
1819 rebind_subsystems(&cgrp_dfl_root
, removed_mask
);
1821 if (opts
.release_agent
) {
1822 spin_lock(&release_agent_path_lock
);
1823 strcpy(root
->release_agent_path
, opts
.release_agent
);
1824 spin_unlock(&release_agent_path_lock
);
1827 kfree(opts
.release_agent
);
1829 mutex_unlock(&cgroup_mutex
);
1834 * To reduce the fork() overhead for systems that are not actually using
1835 * their cgroups capability, we don't maintain the lists running through
1836 * each css_set to its tasks until we see the list actually used - in other
1837 * words after the first mount.
1839 static bool use_task_css_set_links __read_mostly
;
1841 static void cgroup_enable_task_cg_lists(void)
1843 struct task_struct
*p
, *g
;
1845 spin_lock_bh(&css_set_lock
);
1847 if (use_task_css_set_links
)
1850 use_task_css_set_links
= true;
1853 * We need tasklist_lock because RCU is not safe against
1854 * while_each_thread(). Besides, a forking task that has passed
1855 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1856 * is not guaranteed to have its child immediately visible in the
1857 * tasklist if we walk through it with RCU.
1859 read_lock(&tasklist_lock
);
1860 do_each_thread(g
, p
) {
1861 WARN_ON_ONCE(!list_empty(&p
->cg_list
) ||
1862 task_css_set(p
) != &init_css_set
);
1865 * We should check if the process is exiting, otherwise
1866 * it will race with cgroup_exit() in that the list
1867 * entry won't be deleted though the process has exited.
1868 * Do it while holding siglock so that we don't end up
1869 * racing against cgroup_exit().
1871 spin_lock_irq(&p
->sighand
->siglock
);
1872 if (!(p
->flags
& PF_EXITING
)) {
1873 struct css_set
*cset
= task_css_set(p
);
1875 if (!css_set_populated(cset
))
1876 css_set_update_populated(cset
, true);
1877 list_add_tail(&p
->cg_list
, &cset
->tasks
);
1880 spin_unlock_irq(&p
->sighand
->siglock
);
1881 } while_each_thread(g
, p
);
1882 read_unlock(&tasklist_lock
);
1884 spin_unlock_bh(&css_set_lock
);
1887 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1889 struct cgroup_subsys
*ss
;
1892 INIT_LIST_HEAD(&cgrp
->self
.sibling
);
1893 INIT_LIST_HEAD(&cgrp
->self
.children
);
1894 INIT_LIST_HEAD(&cgrp
->cset_links
);
1895 INIT_LIST_HEAD(&cgrp
->pidlists
);
1896 mutex_init(&cgrp
->pidlist_mutex
);
1897 cgrp
->self
.cgroup
= cgrp
;
1898 cgrp
->self
.flags
|= CSS_ONLINE
;
1900 for_each_subsys(ss
, ssid
)
1901 INIT_LIST_HEAD(&cgrp
->e_csets
[ssid
]);
1903 init_waitqueue_head(&cgrp
->offline_waitq
);
1904 INIT_WORK(&cgrp
->release_agent_work
, cgroup_release_agent
);
1907 static void init_cgroup_root(struct cgroup_root
*root
,
1908 struct cgroup_sb_opts
*opts
)
1910 struct cgroup
*cgrp
= &root
->cgrp
;
1912 INIT_LIST_HEAD(&root
->root_list
);
1913 atomic_set(&root
->nr_cgrps
, 1);
1915 init_cgroup_housekeeping(cgrp
);
1916 idr_init(&root
->cgroup_idr
);
1918 root
->flags
= opts
->flags
;
1919 if (opts
->release_agent
)
1920 strcpy(root
->release_agent_path
, opts
->release_agent
);
1922 strcpy(root
->name
, opts
->name
);
1923 if (opts
->cpuset_clone_children
)
1924 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->cgrp
.flags
);
1927 static int cgroup_setup_root(struct cgroup_root
*root
, unsigned long ss_mask
)
1929 LIST_HEAD(tmp_links
);
1930 struct cgroup
*root_cgrp
= &root
->cgrp
;
1931 struct css_set
*cset
;
1934 lockdep_assert_held(&cgroup_mutex
);
1936 ret
= cgroup_idr_alloc(&root
->cgroup_idr
, root_cgrp
, 1, 2, GFP_KERNEL
);
1939 root_cgrp
->id
= ret
;
1941 ret
= percpu_ref_init(&root_cgrp
->self
.refcnt
, css_release
, 0,
1947 * We're accessing css_set_count without locking css_set_lock here,
1948 * but that's OK - it can only be increased by someone holding
1949 * cgroup_lock, and that's us. The worst that can happen is that we
1950 * have some link structures left over
1952 ret
= allocate_cgrp_cset_links(css_set_count
, &tmp_links
);
1956 ret
= cgroup_init_root_id(root
);
1960 root
->kf_root
= kernfs_create_root(&cgroup_kf_syscall_ops
,
1961 KERNFS_ROOT_CREATE_DEACTIVATED
,
1963 if (IS_ERR(root
->kf_root
)) {
1964 ret
= PTR_ERR(root
->kf_root
);
1967 root_cgrp
->kn
= root
->kf_root
->kn
;
1969 ret
= css_populate_dir(&root_cgrp
->self
, NULL
);
1973 ret
= rebind_subsystems(root
, ss_mask
);
1978 * There must be no failure case after here, since rebinding takes
1979 * care of subsystems' refcounts, which are explicitly dropped in
1980 * the failure exit path.
1982 list_add(&root
->root_list
, &cgroup_roots
);
1983 cgroup_root_count
++;
1986 * Link the root cgroup in this hierarchy into all the css_set
1989 spin_lock_bh(&css_set_lock
);
1990 hash_for_each(css_set_table
, i
, cset
, hlist
) {
1991 link_css_set(&tmp_links
, cset
, root_cgrp
);
1992 if (css_set_populated(cset
))
1993 cgroup_update_populated(root_cgrp
, true);
1995 spin_unlock_bh(&css_set_lock
);
1997 BUG_ON(!list_empty(&root_cgrp
->self
.children
));
1998 BUG_ON(atomic_read(&root
->nr_cgrps
) != 1);
2000 kernfs_activate(root_cgrp
->kn
);
2005 kernfs_destroy_root(root
->kf_root
);
2006 root
->kf_root
= NULL
;
2008 cgroup_exit_root_id(root
);
2010 percpu_ref_exit(&root_cgrp
->self
.refcnt
);
2012 free_cgrp_cset_links(&tmp_links
);
2016 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
2017 int flags
, const char *unused_dev_name
,
2020 struct super_block
*pinned_sb
= NULL
;
2021 struct cgroup_namespace
*ns
= current
->nsproxy
->cgroup_ns
;
2022 struct cgroup_subsys
*ss
;
2023 struct cgroup_root
*root
;
2024 struct cgroup_sb_opts opts
;
2025 struct dentry
*dentry
;
2032 /* Check if the caller has permission to mount. */
2033 if (!ns_capable(ns
->user_ns
, CAP_SYS_ADMIN
)) {
2035 return ERR_PTR(-EPERM
);
2039 * The first time anyone tries to mount a cgroup, enable the list
2040 * linking each css_set to its tasks and fix up all existing tasks.
2042 if (!use_task_css_set_links
)
2043 cgroup_enable_task_cg_lists();
2045 mutex_lock(&cgroup_mutex
);
2047 /* First find the desired set of subsystems */
2048 ret
= parse_cgroupfs_options(data
, &opts
);
2052 /* look for a matching existing root */
2053 if (opts
.flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
2054 cgrp_dfl_root_visible
= true;
2055 root
= &cgrp_dfl_root
;
2056 cgroup_get(&root
->cgrp
);
2062 * Destruction of cgroup root is asynchronous, so subsystems may
2063 * still be dying after the previous unmount. Let's drain the
2064 * dying subsystems. We just need to ensure that the ones
2065 * unmounted previously finish dying and don't care about new ones
2066 * starting. Testing ref liveliness is good enough.
2068 for_each_subsys(ss
, i
) {
2069 if (!(opts
.subsys_mask
& (1 << i
)) ||
2070 ss
->root
== &cgrp_dfl_root
)
2073 if (!percpu_ref_tryget_live(&ss
->root
->cgrp
.self
.refcnt
)) {
2074 mutex_unlock(&cgroup_mutex
);
2076 ret
= restart_syscall();
2079 cgroup_put(&ss
->root
->cgrp
);
2082 for_each_root(root
) {
2083 bool name_match
= false;
2085 if (root
== &cgrp_dfl_root
)
2089 * If we asked for a name then it must match. Also, if
2090 * name matches but sybsys_mask doesn't, we should fail.
2091 * Remember whether name matched.
2094 if (strcmp(opts
.name
, root
->name
))
2100 * If we asked for subsystems (or explicitly for no
2101 * subsystems) then they must match.
2103 if ((opts
.subsys_mask
|| opts
.none
) &&
2104 (opts
.subsys_mask
!= root
->subsys_mask
)) {
2111 if (root
->flags
^ opts
.flags
)
2112 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
2115 * We want to reuse @root whose lifetime is governed by its
2116 * ->cgrp. Let's check whether @root is alive and keep it
2117 * that way. As cgroup_kill_sb() can happen anytime, we
2118 * want to block it by pinning the sb so that @root doesn't
2119 * get killed before mount is complete.
2121 * With the sb pinned, tryget_live can reliably indicate
2122 * whether @root can be reused. If it's being killed,
2123 * drain it. We can use wait_queue for the wait but this
2124 * path is super cold. Let's just sleep a bit and retry.
2126 pinned_sb
= kernfs_pin_sb(root
->kf_root
, NULL
);
2127 if (IS_ERR(pinned_sb
) ||
2128 !percpu_ref_tryget_live(&root
->cgrp
.self
.refcnt
)) {
2129 mutex_unlock(&cgroup_mutex
);
2130 if (!IS_ERR_OR_NULL(pinned_sb
))
2131 deactivate_super(pinned_sb
);
2133 ret
= restart_syscall();
2142 * No such thing, create a new one. name= matching without subsys
2143 * specification is allowed for already existing hierarchies but we
2144 * can't create new one without subsys specification.
2146 if (!opts
.subsys_mask
&& !opts
.none
) {
2152 * We know this subsystem has not yet been bound. Users in a non-init
2153 * user namespace may only mount hierarchies with no bound subsystems,
2154 * i.e. 'none,name=user1'
2156 if (!opts
.none
&& !capable(CAP_SYS_ADMIN
)) {
2161 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
2167 init_cgroup_root(root
, &opts
);
2169 ret
= cgroup_setup_root(root
, opts
.subsys_mask
);
2171 cgroup_free_root(root
);
2174 mutex_unlock(&cgroup_mutex
);
2176 kfree(opts
.release_agent
);
2181 return ERR_PTR(ret
);
2184 dentry
= kernfs_mount(fs_type
, flags
, root
->kf_root
,
2185 CGROUP_SUPER_MAGIC
, &new_sb
);
2188 * In non-init cgroup namespace, instead of root cgroup's
2189 * dentry, we return the dentry corresponding to the
2190 * cgroupns->root_cgrp.
2192 if (!IS_ERR(dentry
) && ns
!= &init_cgroup_ns
) {
2193 struct dentry
*nsdentry
;
2194 struct cgroup
*cgrp
;
2196 mutex_lock(&cgroup_mutex
);
2197 spin_lock_bh(&css_set_lock
);
2199 cgrp
= cset_cgroup_from_root(ns
->root_cset
, root
);
2201 spin_unlock_bh(&css_set_lock
);
2202 mutex_unlock(&cgroup_mutex
);
2204 nsdentry
= kernfs_node_dentry(cgrp
->kn
, dentry
->d_sb
);
2209 if (IS_ERR(dentry
) || !new_sb
)
2210 cgroup_put(&root
->cgrp
);
2213 * If @pinned_sb, we're reusing an existing root and holding an
2214 * extra ref on its sb. Mount is complete. Put the extra ref.
2218 deactivate_super(pinned_sb
);
2225 static void cgroup_kill_sb(struct super_block
*sb
)
2227 struct kernfs_root
*kf_root
= kernfs_root_from_sb(sb
);
2228 struct cgroup_root
*root
= cgroup_root_from_kf(kf_root
);
2231 * If @root doesn't have any mounts or children, start killing it.
2232 * This prevents new mounts by disabling percpu_ref_tryget_live().
2233 * cgroup_mount() may wait for @root's release.
2235 * And don't kill the default root.
2237 if (!list_empty(&root
->cgrp
.self
.children
) ||
2238 root
== &cgrp_dfl_root
)
2239 cgroup_put(&root
->cgrp
);
2241 percpu_ref_kill(&root
->cgrp
.self
.refcnt
);
2246 static struct file_system_type cgroup_fs_type
= {
2248 .mount
= cgroup_mount
,
2249 .kill_sb
= cgroup_kill_sb
,
2250 .fs_flags
= FS_USERNS_MOUNT
,
2254 cgroup_path_ns_locked(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
2255 struct cgroup_namespace
*ns
)
2258 struct cgroup
*root
= cset_cgroup_from_root(ns
->root_cset
, cgrp
->root
);
2260 ret
= kernfs_path_from_node(cgrp
->kn
, root
->kn
, buf
, buflen
);
2261 if (ret
< 0 || ret
>= buflen
)
2266 char *cgroup_path_ns(struct cgroup
*cgrp
, char *buf
, size_t buflen
,
2267 struct cgroup_namespace
*ns
)
2271 mutex_lock(&cgroup_mutex
);
2272 spin_lock_bh(&css_set_lock
);
2274 ret
= cgroup_path_ns_locked(cgrp
, buf
, buflen
, ns
);
2276 spin_unlock_bh(&css_set_lock
);
2277 mutex_unlock(&cgroup_mutex
);
2281 EXPORT_SYMBOL_GPL(cgroup_path_ns
);
2284 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
2285 * @task: target task
2286 * @buf: the buffer to write the path into
2287 * @buflen: the length of the buffer
2289 * Determine @task's cgroup on the first (the one with the lowest non-zero
2290 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
2291 * function grabs cgroup_mutex and shouldn't be used inside locks used by
2292 * cgroup controller callbacks.
2294 * Return value is the same as kernfs_path().
2296 char *task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
2298 struct cgroup_root
*root
;
2299 struct cgroup
*cgrp
;
2300 int hierarchy_id
= 1;
2303 mutex_lock(&cgroup_mutex
);
2304 spin_lock_bh(&css_set_lock
);
2306 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
2309 cgrp
= task_cgroup_from_root(task
, root
);
2310 path
= cgroup_path_ns_locked(cgrp
, buf
, buflen
,
2313 /* if no hierarchy exists, everyone is in "/" */
2314 if (strlcpy(buf
, "/", buflen
) < buflen
)
2318 spin_unlock_bh(&css_set_lock
);
2319 mutex_unlock(&cgroup_mutex
);
2322 EXPORT_SYMBOL_GPL(task_cgroup_path
);
2324 /* used to track tasks and other necessary states during migration */
2325 struct cgroup_taskset
{
2326 /* the src and dst cset list running through cset->mg_node */
2327 struct list_head src_csets
;
2328 struct list_head dst_csets
;
2330 /* the subsys currently being processed */
2334 * Fields for cgroup_taskset_*() iteration.
2336 * Before migration is committed, the target migration tasks are on
2337 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
2338 * the csets on ->dst_csets. ->csets point to either ->src_csets
2339 * or ->dst_csets depending on whether migration is committed.
2341 * ->cur_csets and ->cur_task point to the current task position
2344 struct list_head
*csets
;
2345 struct css_set
*cur_cset
;
2346 struct task_struct
*cur_task
;
2349 #define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \
2350 .src_csets = LIST_HEAD_INIT(tset.src_csets), \
2351 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
2352 .csets = &tset.src_csets, \
2356 * cgroup_taskset_add - try to add a migration target task to a taskset
2357 * @task: target task
2358 * @tset: target taskset
2360 * Add @task, which is a migration target, to @tset. This function becomes
2361 * noop if @task doesn't need to be migrated. @task's css_set should have
2362 * been added as a migration source and @task->cg_list will be moved from
2363 * the css_set's tasks list to mg_tasks one.
2365 static void cgroup_taskset_add(struct task_struct
*task
,
2366 struct cgroup_taskset
*tset
)
2368 struct css_set
*cset
;
2370 lockdep_assert_held(&css_set_lock
);
2372 /* @task either already exited or can't exit until the end */
2373 if (task
->flags
& PF_EXITING
)
2376 /* leave @task alone if post_fork() hasn't linked it yet */
2377 if (list_empty(&task
->cg_list
))
2380 cset
= task_css_set(task
);
2381 if (!cset
->mg_src_cgrp
)
2384 list_move_tail(&task
->cg_list
, &cset
->mg_tasks
);
2385 if (list_empty(&cset
->mg_node
))
2386 list_add_tail(&cset
->mg_node
, &tset
->src_csets
);
2387 if (list_empty(&cset
->mg_dst_cset
->mg_node
))
2388 list_move_tail(&cset
->mg_dst_cset
->mg_node
,
2393 * cgroup_taskset_first - reset taskset and return the first task
2394 * @tset: taskset of interest
2395 * @dst_cssp: output variable for the destination css
2397 * @tset iteration is initialized and the first task is returned.
2399 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
,
2400 struct cgroup_subsys_state
**dst_cssp
)
2402 tset
->cur_cset
= list_first_entry(tset
->csets
, struct css_set
, mg_node
);
2403 tset
->cur_task
= NULL
;
2405 return cgroup_taskset_next(tset
, dst_cssp
);
2409 * cgroup_taskset_next - iterate to the next task in taskset
2410 * @tset: taskset of interest
2411 * @dst_cssp: output variable for the destination css
2413 * Return the next task in @tset. Iteration must have been initialized
2414 * with cgroup_taskset_first().
2416 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
,
2417 struct cgroup_subsys_state
**dst_cssp
)
2419 struct css_set
*cset
= tset
->cur_cset
;
2420 struct task_struct
*task
= tset
->cur_task
;
2422 while (&cset
->mg_node
!= tset
->csets
) {
2424 task
= list_first_entry(&cset
->mg_tasks
,
2425 struct task_struct
, cg_list
);
2427 task
= list_next_entry(task
, cg_list
);
2429 if (&task
->cg_list
!= &cset
->mg_tasks
) {
2430 tset
->cur_cset
= cset
;
2431 tset
->cur_task
= task
;
2434 * This function may be called both before and
2435 * after cgroup_taskset_migrate(). The two cases
2436 * can be distinguished by looking at whether @cset
2437 * has its ->mg_dst_cset set.
2439 if (cset
->mg_dst_cset
)
2440 *dst_cssp
= cset
->mg_dst_cset
->subsys
[tset
->ssid
];
2442 *dst_cssp
= cset
->subsys
[tset
->ssid
];
2447 cset
= list_next_entry(cset
, mg_node
);
2455 * cgroup_taskset_migrate - migrate a taskset to a cgroup
2456 * @tset: taget taskset
2457 * @dst_cgrp: destination cgroup
2459 * Migrate tasks in @tset to @dst_cgrp. This function fails iff one of the
2460 * ->can_attach callbacks fails and guarantees that either all or none of
2461 * the tasks in @tset are migrated. @tset is consumed regardless of
2464 static int cgroup_taskset_migrate(struct cgroup_taskset
*tset
,
2465 struct cgroup
*dst_cgrp
)
2467 struct cgroup_subsys_state
*css
, *failed_css
= NULL
;
2468 struct task_struct
*task
, *tmp_task
;
2469 struct css_set
*cset
, *tmp_cset
;
2472 /* methods shouldn't be called if no task is actually migrating */
2473 if (list_empty(&tset
->src_csets
))
2476 /* check that we can legitimately attach to the cgroup */
2477 for_each_e_css(css
, i
, dst_cgrp
) {
2478 if (css
->ss
->can_attach
) {
2480 ret
= css
->ss
->can_attach(tset
);
2483 goto out_cancel_attach
;
2489 * Now that we're guaranteed success, proceed to move all tasks to
2490 * the new cgroup. There are no failure cases after here, so this
2491 * is the commit point.
2493 spin_lock_bh(&css_set_lock
);
2494 list_for_each_entry(cset
, &tset
->src_csets
, mg_node
) {
2495 list_for_each_entry_safe(task
, tmp_task
, &cset
->mg_tasks
, cg_list
) {
2496 struct css_set
*from_cset
= task_css_set(task
);
2497 struct css_set
*to_cset
= cset
->mg_dst_cset
;
2499 get_css_set(to_cset
);
2500 css_set_move_task(task
, from_cset
, to_cset
, true);
2501 put_css_set_locked(from_cset
);
2504 spin_unlock_bh(&css_set_lock
);
2507 * Migration is committed, all target tasks are now on dst_csets.
2508 * Nothing is sensitive to fork() after this point. Notify
2509 * controllers that migration is complete.
2511 tset
->csets
= &tset
->dst_csets
;
2513 for_each_e_css(css
, i
, dst_cgrp
) {
2514 if (css
->ss
->attach
) {
2516 css
->ss
->attach(tset
);
2521 goto out_release_tset
;
2524 for_each_e_css(css
, i
, dst_cgrp
) {
2525 if (css
== failed_css
)
2527 if (css
->ss
->cancel_attach
) {
2529 css
->ss
->cancel_attach(tset
);
2533 spin_lock_bh(&css_set_lock
);
2534 list_splice_init(&tset
->dst_csets
, &tset
->src_csets
);
2535 list_for_each_entry_safe(cset
, tmp_cset
, &tset
->src_csets
, mg_node
) {
2536 list_splice_tail_init(&cset
->mg_tasks
, &cset
->tasks
);
2537 list_del_init(&cset
->mg_node
);
2539 spin_unlock_bh(&css_set_lock
);
2544 * cgroup_migrate_finish - cleanup after attach
2545 * @preloaded_csets: list of preloaded css_sets
2547 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2548 * those functions for details.
2550 static void cgroup_migrate_finish(struct list_head
*preloaded_csets
)
2552 struct css_set
*cset
, *tmp_cset
;
2554 lockdep_assert_held(&cgroup_mutex
);
2556 spin_lock_bh(&css_set_lock
);
2557 list_for_each_entry_safe(cset
, tmp_cset
, preloaded_csets
, mg_preload_node
) {
2558 cset
->mg_src_cgrp
= NULL
;
2559 cset
->mg_dst_cset
= NULL
;
2560 list_del_init(&cset
->mg_preload_node
);
2561 put_css_set_locked(cset
);
2563 spin_unlock_bh(&css_set_lock
);
2567 * cgroup_migrate_add_src - add a migration source css_set
2568 * @src_cset: the source css_set to add
2569 * @dst_cgrp: the destination cgroup
2570 * @preloaded_csets: list of preloaded css_sets
2572 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2573 * @src_cset and add it to @preloaded_csets, which should later be cleaned
2574 * up by cgroup_migrate_finish().
2576 * This function may be called without holding cgroup_threadgroup_rwsem
2577 * even if the target is a process. Threads may be created and destroyed
2578 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2579 * into play and the preloaded css_sets are guaranteed to cover all
2582 static void cgroup_migrate_add_src(struct css_set
*src_cset
,
2583 struct cgroup
*dst_cgrp
,
2584 struct list_head
*preloaded_csets
)
2586 struct cgroup
*src_cgrp
;
2588 lockdep_assert_held(&cgroup_mutex
);
2589 lockdep_assert_held(&css_set_lock
);
2591 src_cgrp
= cset_cgroup_from_root(src_cset
, dst_cgrp
->root
);
2593 if (!list_empty(&src_cset
->mg_preload_node
))
2596 WARN_ON(src_cset
->mg_src_cgrp
);
2597 WARN_ON(!list_empty(&src_cset
->mg_tasks
));
2598 WARN_ON(!list_empty(&src_cset
->mg_node
));
2600 src_cset
->mg_src_cgrp
= src_cgrp
;
2601 get_css_set(src_cset
);
2602 list_add(&src_cset
->mg_preload_node
, preloaded_csets
);
2606 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
2607 * @dst_cgrp: the destination cgroup (may be %NULL)
2608 * @preloaded_csets: list of preloaded source css_sets
2610 * Tasks are about to be moved to @dst_cgrp and all the source css_sets
2611 * have been preloaded to @preloaded_csets. This function looks up and
2612 * pins all destination css_sets, links each to its source, and append them
2613 * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2614 * source css_set is assumed to be its cgroup on the default hierarchy.
2616 * This function must be called after cgroup_migrate_add_src() has been
2617 * called on each migration source css_set. After migration is performed
2618 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2621 static int cgroup_migrate_prepare_dst(struct cgroup
*dst_cgrp
,
2622 struct list_head
*preloaded_csets
)
2625 struct css_set
*src_cset
, *tmp_cset
;
2627 lockdep_assert_held(&cgroup_mutex
);
2630 * Except for the root, child_subsys_mask must be zero for a cgroup
2631 * with tasks so that child cgroups don't compete against tasks.
2633 if (dst_cgrp
&& cgroup_on_dfl(dst_cgrp
) && cgroup_parent(dst_cgrp
) &&
2634 dst_cgrp
->child_subsys_mask
)
2637 /* look up the dst cset for each src cset and link it to src */
2638 list_for_each_entry_safe(src_cset
, tmp_cset
, preloaded_csets
, mg_preload_node
) {
2639 struct css_set
*dst_cset
;
2641 dst_cset
= find_css_set(src_cset
,
2642 dst_cgrp
?: src_cset
->dfl_cgrp
);
2646 WARN_ON_ONCE(src_cset
->mg_dst_cset
|| dst_cset
->mg_dst_cset
);
2649 * If src cset equals dst, it's noop. Drop the src.
2650 * cgroup_migrate() will skip the cset too. Note that we
2651 * can't handle src == dst as some nodes are used by both.
2653 if (src_cset
== dst_cset
) {
2654 src_cset
->mg_src_cgrp
= NULL
;
2655 list_del_init(&src_cset
->mg_preload_node
);
2656 put_css_set(src_cset
);
2657 put_css_set(dst_cset
);
2661 src_cset
->mg_dst_cset
= dst_cset
;
2663 if (list_empty(&dst_cset
->mg_preload_node
))
2664 list_add(&dst_cset
->mg_preload_node
, &csets
);
2666 put_css_set(dst_cset
);
2669 list_splice_tail(&csets
, preloaded_csets
);
2672 cgroup_migrate_finish(&csets
);
2677 * cgroup_migrate - migrate a process or task to a cgroup
2678 * @leader: the leader of the process or the task to migrate
2679 * @threadgroup: whether @leader points to the whole process or a single task
2680 * @cgrp: the destination cgroup
2682 * Migrate a process or task denoted by @leader to @cgrp. If migrating a
2683 * process, the caller must be holding cgroup_threadgroup_rwsem. The
2684 * caller is also responsible for invoking cgroup_migrate_add_src() and
2685 * cgroup_migrate_prepare_dst() on the targets before invoking this
2686 * function and following up with cgroup_migrate_finish().
2688 * As long as a controller's ->can_attach() doesn't fail, this function is
2689 * guaranteed to succeed. This means that, excluding ->can_attach()
2690 * failure, when migrating multiple targets, the success or failure can be
2691 * decided for all targets by invoking group_migrate_prepare_dst() before
2692 * actually starting migrating.
2694 static int cgroup_migrate(struct task_struct
*leader
, bool threadgroup
,
2695 struct cgroup
*cgrp
)
2697 struct cgroup_taskset tset
= CGROUP_TASKSET_INIT(tset
);
2698 struct task_struct
*task
;
2701 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2702 * already PF_EXITING could be freed from underneath us unless we
2703 * take an rcu_read_lock.
2705 spin_lock_bh(&css_set_lock
);
2709 cgroup_taskset_add(task
, &tset
);
2712 } while_each_thread(leader
, task
);
2714 spin_unlock_bh(&css_set_lock
);
2716 return cgroup_taskset_migrate(&tset
, cgrp
);
2720 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2721 * @dst_cgrp: the cgroup to attach to
2722 * @leader: the task or the leader of the threadgroup to be attached
2723 * @threadgroup: attach the whole threadgroup?
2725 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
2727 static int cgroup_attach_task(struct cgroup
*dst_cgrp
,
2728 struct task_struct
*leader
, bool threadgroup
)
2730 LIST_HEAD(preloaded_csets
);
2731 struct task_struct
*task
;
2734 /* look up all src csets */
2735 spin_lock_bh(&css_set_lock
);
2739 cgroup_migrate_add_src(task_css_set(task
), dst_cgrp
,
2743 } while_each_thread(leader
, task
);
2745 spin_unlock_bh(&css_set_lock
);
2747 /* prepare dst csets and commit */
2748 ret
= cgroup_migrate_prepare_dst(dst_cgrp
, &preloaded_csets
);
2750 ret
= cgroup_migrate(leader
, threadgroup
, dst_cgrp
);
2752 cgroup_migrate_finish(&preloaded_csets
);
2756 static int cgroup_procs_write_permission(struct task_struct
*task
,
2757 struct cgroup
*dst_cgrp
,
2758 struct kernfs_open_file
*of
)
2760 const struct cred
*cred
= current_cred();
2761 const struct cred
*tcred
= get_task_cred(task
);
2765 * even if we're attaching all tasks in the thread group, we only
2766 * need to check permissions on one of them.
2768 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2769 !uid_eq(cred
->euid
, tcred
->uid
) &&
2770 !uid_eq(cred
->euid
, tcred
->suid
))
2773 if (!ret
&& cgroup_on_dfl(dst_cgrp
)) {
2774 struct super_block
*sb
= of
->file
->f_path
.dentry
->d_sb
;
2775 struct cgroup
*cgrp
;
2776 struct inode
*inode
;
2778 spin_lock_bh(&css_set_lock
);
2779 cgrp
= task_cgroup_from_root(task
, &cgrp_dfl_root
);
2780 spin_unlock_bh(&css_set_lock
);
2782 while (!cgroup_is_descendant(dst_cgrp
, cgrp
))
2783 cgrp
= cgroup_parent(cgrp
);
2786 inode
= kernfs_get_inode(sb
, cgrp
->procs_file
.kn
);
2788 ret
= inode_permission(inode
, MAY_WRITE
);
2798 * Find the task_struct of the task to attach by vpid and pass it along to the
2799 * function to attach either it or all tasks in its threadgroup. Will lock
2800 * cgroup_mutex and threadgroup.
2802 static ssize_t
__cgroup_procs_write(struct kernfs_open_file
*of
, char *buf
,
2803 size_t nbytes
, loff_t off
, bool threadgroup
)
2805 struct task_struct
*tsk
;
2806 struct cgroup
*cgrp
;
2810 if (kstrtoint(strstrip(buf
), 0, &pid
) || pid
< 0)
2813 cgrp
= cgroup_kn_lock_live(of
->kn
);
2817 percpu_down_write(&cgroup_threadgroup_rwsem
);
2820 tsk
= find_task_by_vpid(pid
);
2823 goto out_unlock_rcu
;
2830 tsk
= tsk
->group_leader
;
2833 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2834 * trapped in a cpuset, or RT worker may be born in a cgroup
2835 * with no rt_runtime allocated. Just say no.
2837 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2839 goto out_unlock_rcu
;
2842 get_task_struct(tsk
);
2845 ret
= cgroup_procs_write_permission(tsk
, cgrp
, of
);
2847 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2849 put_task_struct(tsk
);
2850 goto out_unlock_threadgroup
;
2854 out_unlock_threadgroup
:
2855 percpu_up_write(&cgroup_threadgroup_rwsem
);
2856 cgroup_kn_unlock(of
->kn
);
2857 return ret
?: nbytes
;
2861 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2862 * @from: attach to all cgroups of a given task
2863 * @tsk: the task to be attached
2865 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
2867 struct cgroup_root
*root
;
2870 mutex_lock(&cgroup_mutex
);
2871 for_each_root(root
) {
2872 struct cgroup
*from_cgrp
;
2874 if (root
== &cgrp_dfl_root
)
2877 spin_lock_bh(&css_set_lock
);
2878 from_cgrp
= task_cgroup_from_root(from
, root
);
2879 spin_unlock_bh(&css_set_lock
);
2881 retval
= cgroup_attach_task(from_cgrp
, tsk
, false);
2885 mutex_unlock(&cgroup_mutex
);
2889 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
2891 static ssize_t
cgroup_tasks_write(struct kernfs_open_file
*of
,
2892 char *buf
, size_t nbytes
, loff_t off
)
2894 return __cgroup_procs_write(of
, buf
, nbytes
, off
, false);
2897 static ssize_t
cgroup_procs_write(struct kernfs_open_file
*of
,
2898 char *buf
, size_t nbytes
, loff_t off
)
2900 return __cgroup_procs_write(of
, buf
, nbytes
, off
, true);
2903 static ssize_t
cgroup_release_agent_write(struct kernfs_open_file
*of
,
2904 char *buf
, size_t nbytes
, loff_t off
)
2906 struct cgroup
*cgrp
;
2908 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
2910 cgrp
= cgroup_kn_lock_live(of
->kn
);
2913 spin_lock(&release_agent_path_lock
);
2914 strlcpy(cgrp
->root
->release_agent_path
, strstrip(buf
),
2915 sizeof(cgrp
->root
->release_agent_path
));
2916 spin_unlock(&release_agent_path_lock
);
2917 cgroup_kn_unlock(of
->kn
);
2921 static int cgroup_release_agent_show(struct seq_file
*seq
, void *v
)
2923 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2925 spin_lock(&release_agent_path_lock
);
2926 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2927 spin_unlock(&release_agent_path_lock
);
2928 seq_putc(seq
, '\n');
2932 static int cgroup_sane_behavior_show(struct seq_file
*seq
, void *v
)
2934 seq_puts(seq
, "0\n");
2938 static void cgroup_print_ss_mask(struct seq_file
*seq
, unsigned long ss_mask
)
2940 struct cgroup_subsys
*ss
;
2941 bool printed
= false;
2944 for_each_subsys_which(ss
, ssid
, &ss_mask
) {
2947 seq_printf(seq
, "%s", ss
->name
);
2951 seq_putc(seq
, '\n');
2954 /* show controllers which are currently attached to the default hierarchy */
2955 static int cgroup_root_controllers_show(struct seq_file
*seq
, void *v
)
2957 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2959 cgroup_print_ss_mask(seq
, cgrp
->root
->subsys_mask
&
2960 ~cgrp_dfl_root_inhibit_ss_mask
);
2964 /* show controllers which are enabled from the parent */
2965 static int cgroup_controllers_show(struct seq_file
*seq
, void *v
)
2967 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2969 cgroup_print_ss_mask(seq
, cgroup_parent(cgrp
)->subtree_control
);
2973 /* show controllers which are enabled for a given cgroup's children */
2974 static int cgroup_subtree_control_show(struct seq_file
*seq
, void *v
)
2976 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2978 cgroup_print_ss_mask(seq
, cgrp
->subtree_control
);
2983 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2984 * @cgrp: root of the subtree to update csses for
2986 * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
2987 * css associations need to be updated accordingly. This function looks up
2988 * all css_sets which are attached to the subtree, creates the matching
2989 * updated css_sets and migrates the tasks to the new ones.
2991 static int cgroup_update_dfl_csses(struct cgroup
*cgrp
)
2993 LIST_HEAD(preloaded_csets
);
2994 struct cgroup_taskset tset
= CGROUP_TASKSET_INIT(tset
);
2995 struct cgroup_subsys_state
*css
;
2996 struct css_set
*src_cset
;
2999 lockdep_assert_held(&cgroup_mutex
);
3001 percpu_down_write(&cgroup_threadgroup_rwsem
);
3003 /* look up all csses currently attached to @cgrp's subtree */
3004 spin_lock_bh(&css_set_lock
);
3005 css_for_each_descendant_pre(css
, cgroup_css(cgrp
, NULL
)) {
3006 struct cgrp_cset_link
*link
;
3008 /* self is not affected by child_subsys_mask change */
3009 if (css
->cgroup
== cgrp
)
3012 list_for_each_entry(link
, &css
->cgroup
->cset_links
, cset_link
)
3013 cgroup_migrate_add_src(link
->cset
, cgrp
,
3016 spin_unlock_bh(&css_set_lock
);
3018 /* NULL dst indicates self on default hierarchy */
3019 ret
= cgroup_migrate_prepare_dst(NULL
, &preloaded_csets
);
3023 spin_lock_bh(&css_set_lock
);
3024 list_for_each_entry(src_cset
, &preloaded_csets
, mg_preload_node
) {
3025 struct task_struct
*task
, *ntask
;
3027 /* src_csets precede dst_csets, break on the first dst_cset */
3028 if (!src_cset
->mg_src_cgrp
)
3031 /* all tasks in src_csets need to be migrated */
3032 list_for_each_entry_safe(task
, ntask
, &src_cset
->tasks
, cg_list
)
3033 cgroup_taskset_add(task
, &tset
);
3035 spin_unlock_bh(&css_set_lock
);
3037 ret
= cgroup_taskset_migrate(&tset
, cgrp
);
3039 cgroup_migrate_finish(&preloaded_csets
);
3040 percpu_up_write(&cgroup_threadgroup_rwsem
);
3044 /* change the enabled child controllers for a cgroup in the default hierarchy */
3045 static ssize_t
cgroup_subtree_control_write(struct kernfs_open_file
*of
,
3046 char *buf
, size_t nbytes
,
3049 unsigned long enable
= 0, disable
= 0;
3050 unsigned long css_enable
, css_disable
, old_sc
, new_sc
, old_ss
, new_ss
;
3051 struct cgroup
*cgrp
, *child
;
3052 struct cgroup_subsys
*ss
;
3057 * Parse input - space separated list of subsystem names prefixed
3058 * with either + or -.
3060 buf
= strstrip(buf
);
3061 while ((tok
= strsep(&buf
, " "))) {
3062 unsigned long tmp_ss_mask
= ~cgrp_dfl_root_inhibit_ss_mask
;
3066 for_each_subsys_which(ss
, ssid
, &tmp_ss_mask
) {
3067 if (!cgroup_ssid_enabled(ssid
) ||
3068 strcmp(tok
+ 1, ss
->name
))
3072 enable
|= 1 << ssid
;
3073 disable
&= ~(1 << ssid
);
3074 } else if (*tok
== '-') {
3075 disable
|= 1 << ssid
;
3076 enable
&= ~(1 << ssid
);
3082 if (ssid
== CGROUP_SUBSYS_COUNT
)
3086 cgrp
= cgroup_kn_lock_live(of
->kn
);
3090 for_each_subsys(ss
, ssid
) {
3091 if (enable
& (1 << ssid
)) {
3092 if (cgrp
->subtree_control
& (1 << ssid
)) {
3093 enable
&= ~(1 << ssid
);
3097 /* unavailable or not enabled on the parent? */
3098 if (!(cgrp_dfl_root
.subsys_mask
& (1 << ssid
)) ||
3099 (cgroup_parent(cgrp
) &&
3100 !(cgroup_parent(cgrp
)->subtree_control
& (1 << ssid
)))) {
3104 } else if (disable
& (1 << ssid
)) {
3105 if (!(cgrp
->subtree_control
& (1 << ssid
))) {
3106 disable
&= ~(1 << ssid
);
3110 /* a child has it enabled? */
3111 cgroup_for_each_live_child(child
, cgrp
) {
3112 if (child
->subtree_control
& (1 << ssid
)) {
3120 if (!enable
&& !disable
) {
3126 * Except for the root, subtree_control must be zero for a cgroup
3127 * with tasks so that child cgroups don't compete against tasks.
3129 if (enable
&& cgroup_parent(cgrp
) && !list_empty(&cgrp
->cset_links
)) {
3135 * Update subsys masks and calculate what needs to be done. More
3136 * subsystems than specified may need to be enabled or disabled
3137 * depending on subsystem dependencies.
3139 old_sc
= cgrp
->subtree_control
;
3140 old_ss
= cgrp
->child_subsys_mask
;
3141 new_sc
= (old_sc
| enable
) & ~disable
;
3142 new_ss
= cgroup_calc_child_subsys_mask(cgrp
, new_sc
);
3144 css_enable
= ~old_ss
& new_ss
;
3145 css_disable
= old_ss
& ~new_ss
;
3146 enable
|= css_enable
;
3147 disable
|= css_disable
;
3150 * Because css offlining is asynchronous, userland might try to
3151 * re-enable the same controller while the previous instance is
3152 * still around. In such cases, wait till it's gone using
3155 for_each_subsys_which(ss
, ssid
, &css_enable
) {
3156 cgroup_for_each_live_child(child
, cgrp
) {
3159 if (!cgroup_css(child
, ss
))
3163 prepare_to_wait(&child
->offline_waitq
, &wait
,
3164 TASK_UNINTERRUPTIBLE
);
3165 cgroup_kn_unlock(of
->kn
);
3167 finish_wait(&child
->offline_waitq
, &wait
);
3170 return restart_syscall();
3174 cgrp
->subtree_control
= new_sc
;
3175 cgrp
->child_subsys_mask
= new_ss
;
3178 * Create new csses or make the existing ones visible. A css is
3179 * created invisible if it's being implicitly enabled through
3180 * dependency. An invisible css is made visible when the userland
3181 * explicitly enables it.
3183 for_each_subsys(ss
, ssid
) {
3184 if (!(enable
& (1 << ssid
)))
3187 cgroup_for_each_live_child(child
, cgrp
) {
3188 if (css_enable
& (1 << ssid
))
3189 ret
= create_css(child
, ss
,
3190 cgrp
->subtree_control
& (1 << ssid
));
3192 ret
= css_populate_dir(cgroup_css(child
, ss
),
3200 * At this point, cgroup_e_css() results reflect the new csses
3201 * making the following cgroup_update_dfl_csses() properly update
3202 * css associations of all tasks in the subtree.
3204 ret
= cgroup_update_dfl_csses(cgrp
);
3209 * All tasks are migrated out of disabled csses. Kill or hide
3210 * them. A css is hidden when the userland requests it to be
3211 * disabled while other subsystems are still depending on it. The
3212 * css must not actively control resources and be in the vanilla
3213 * state if it's made visible again later. Controllers which may
3214 * be depended upon should provide ->css_reset() for this purpose.
3216 for_each_subsys(ss
, ssid
) {
3217 if (!(disable
& (1 << ssid
)))
3220 cgroup_for_each_live_child(child
, cgrp
) {
3221 struct cgroup_subsys_state
*css
= cgroup_css(child
, ss
);
3223 if (css_disable
& (1 << ssid
)) {
3226 css_clear_dir(css
, NULL
);
3234 * The effective csses of all the descendants (excluding @cgrp) may
3235 * have changed. Subsystems can optionally subscribe to this event
3236 * by implementing ->css_e_css_changed() which is invoked if any of
3237 * the effective csses seen from the css's cgroup may have changed.
3239 for_each_subsys(ss
, ssid
) {
3240 struct cgroup_subsys_state
*this_css
= cgroup_css(cgrp
, ss
);
3241 struct cgroup_subsys_state
*css
;
3243 if (!ss
->css_e_css_changed
|| !this_css
)
3246 css_for_each_descendant_pre(css
, this_css
)
3247 if (css
!= this_css
)
3248 ss
->css_e_css_changed(css
);
3251 kernfs_activate(cgrp
->kn
);
3254 cgroup_kn_unlock(of
->kn
);
3255 return ret
?: nbytes
;
3258 cgrp
->subtree_control
= old_sc
;
3259 cgrp
->child_subsys_mask
= old_ss
;
3261 for_each_subsys(ss
, ssid
) {
3262 if (!(enable
& (1 << ssid
)))
3265 cgroup_for_each_live_child(child
, cgrp
) {
3266 struct cgroup_subsys_state
*css
= cgroup_css(child
, ss
);
3271 if (css_enable
& (1 << ssid
))
3274 css_clear_dir(css
, NULL
);
3280 static int cgroup_events_show(struct seq_file
*seq
, void *v
)
3282 seq_printf(seq
, "populated %d\n",
3283 cgroup_is_populated(seq_css(seq
)->cgroup
));
3287 static ssize_t
cgroup_file_write(struct kernfs_open_file
*of
, char *buf
,
3288 size_t nbytes
, loff_t off
)
3290 struct cgroup
*cgrp
= of
->kn
->parent
->priv
;
3291 struct cftype
*cft
= of
->kn
->priv
;
3292 struct cgroup_subsys_state
*css
;
3296 return cft
->write(of
, buf
, nbytes
, off
);
3299 * kernfs guarantees that a file isn't deleted with operations in
3300 * flight, which means that the matching css is and stays alive and
3301 * doesn't need to be pinned. The RCU locking is not necessary
3302 * either. It's just for the convenience of using cgroup_css().
3305 css
= cgroup_css(cgrp
, cft
->ss
);
3308 if (cft
->write_u64
) {
3309 unsigned long long v
;
3310 ret
= kstrtoull(buf
, 0, &v
);
3312 ret
= cft
->write_u64(css
, cft
, v
);
3313 } else if (cft
->write_s64
) {
3315 ret
= kstrtoll(buf
, 0, &v
);
3317 ret
= cft
->write_s64(css
, cft
, v
);
3322 return ret
?: nbytes
;
3325 static void *cgroup_seqfile_start(struct seq_file
*seq
, loff_t
*ppos
)
3327 return seq_cft(seq
)->seq_start(seq
, ppos
);
3330 static void *cgroup_seqfile_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
3332 return seq_cft(seq
)->seq_next(seq
, v
, ppos
);
3335 static void cgroup_seqfile_stop(struct seq_file
*seq
, void *v
)
3337 seq_cft(seq
)->seq_stop(seq
, v
);
3340 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
3342 struct cftype
*cft
= seq_cft(m
);
3343 struct cgroup_subsys_state
*css
= seq_css(m
);
3346 return cft
->seq_show(m
, arg
);
3349 seq_printf(m
, "%llu\n", cft
->read_u64(css
, cft
));
3350 else if (cft
->read_s64
)
3351 seq_printf(m
, "%lld\n", cft
->read_s64(css
, cft
));
3357 static struct kernfs_ops cgroup_kf_single_ops
= {
3358 .atomic_write_len
= PAGE_SIZE
,
3359 .write
= cgroup_file_write
,
3360 .seq_show
= cgroup_seqfile_show
,
3363 static struct kernfs_ops cgroup_kf_ops
= {
3364 .atomic_write_len
= PAGE_SIZE
,
3365 .write
= cgroup_file_write
,
3366 .seq_start
= cgroup_seqfile_start
,
3367 .seq_next
= cgroup_seqfile_next
,
3368 .seq_stop
= cgroup_seqfile_stop
,
3369 .seq_show
= cgroup_seqfile_show
,
3373 * cgroup_rename - Only allow simple rename of directories in place.
3375 static int cgroup_rename(struct kernfs_node
*kn
, struct kernfs_node
*new_parent
,
3376 const char *new_name_str
)
3378 struct cgroup
*cgrp
= kn
->priv
;
3381 if (kernfs_type(kn
) != KERNFS_DIR
)
3383 if (kn
->parent
!= new_parent
)
3387 * This isn't a proper migration and its usefulness is very
3388 * limited. Disallow on the default hierarchy.
3390 if (cgroup_on_dfl(cgrp
))
3394 * We're gonna grab cgroup_mutex which nests outside kernfs
3395 * active_ref. kernfs_rename() doesn't require active_ref
3396 * protection. Break them before grabbing cgroup_mutex.
3398 kernfs_break_active_protection(new_parent
);
3399 kernfs_break_active_protection(kn
);
3401 mutex_lock(&cgroup_mutex
);
3403 ret
= kernfs_rename(kn
, new_parent
, new_name_str
);
3405 mutex_unlock(&cgroup_mutex
);
3407 kernfs_unbreak_active_protection(kn
);
3408 kernfs_unbreak_active_protection(new_parent
);
3412 /* set uid and gid of cgroup dirs and files to that of the creator */
3413 static int cgroup_kn_set_ugid(struct kernfs_node
*kn
)
3415 struct iattr iattr
= { .ia_valid
= ATTR_UID
| ATTR_GID
,
3416 .ia_uid
= current_fsuid(),
3417 .ia_gid
= current_fsgid(), };
3419 if (uid_eq(iattr
.ia_uid
, GLOBAL_ROOT_UID
) &&
3420 gid_eq(iattr
.ia_gid
, GLOBAL_ROOT_GID
))
3423 return kernfs_setattr(kn
, &iattr
);
3426 static int cgroup_add_file(struct cgroup_subsys_state
*css
, struct cgroup
*cgrp
,
3429 char name
[CGROUP_FILE_NAME_MAX
];
3430 struct kernfs_node
*kn
;
3431 struct lock_class_key
*key
= NULL
;
3434 #ifdef CONFIG_DEBUG_LOCK_ALLOC
3435 key
= &cft
->lockdep_key
;
3437 kn
= __kernfs_create_file(cgrp
->kn
, cgroup_file_name(cgrp
, cft
, name
),
3438 cgroup_file_mode(cft
), 0, cft
->kf_ops
, cft
,
3443 ret
= cgroup_kn_set_ugid(kn
);
3449 if (cft
->file_offset
) {
3450 struct cgroup_file
*cfile
= (void *)css
+ cft
->file_offset
;
3452 spin_lock_irq(&cgroup_file_kn_lock
);
3454 spin_unlock_irq(&cgroup_file_kn_lock
);
3461 * cgroup_addrm_files - add or remove files to a cgroup directory
3462 * @css: the target css
3463 * @cgrp: the target cgroup (usually css->cgroup)
3464 * @cfts: array of cftypes to be added
3465 * @is_add: whether to add or remove
3467 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
3468 * For removals, this function never fails.
3470 static int cgroup_addrm_files(struct cgroup_subsys_state
*css
,
3471 struct cgroup
*cgrp
, struct cftype cfts
[],
3474 struct cftype
*cft
, *cft_end
= NULL
;
3477 lockdep_assert_held(&cgroup_mutex
);
3480 for (cft
= cfts
; cft
!= cft_end
&& cft
->name
[0] != '\0'; cft
++) {
3481 /* does cft->flags tell us to skip this file on @cgrp? */
3482 if ((cft
->flags
& __CFTYPE_ONLY_ON_DFL
) && !cgroup_on_dfl(cgrp
))
3484 if ((cft
->flags
& __CFTYPE_NOT_ON_DFL
) && cgroup_on_dfl(cgrp
))
3486 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgroup_parent(cgrp
))
3488 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgroup_parent(cgrp
))
3492 ret
= cgroup_add_file(css
, cgrp
, cft
);
3494 pr_warn("%s: failed to add %s, err=%d\n",
3495 __func__
, cft
->name
, ret
);
3501 cgroup_rm_file(cgrp
, cft
);
3507 static int cgroup_apply_cftypes(struct cftype
*cfts
, bool is_add
)
3510 struct cgroup_subsys
*ss
= cfts
[0].ss
;
3511 struct cgroup
*root
= &ss
->root
->cgrp
;
3512 struct cgroup_subsys_state
*css
;
3515 lockdep_assert_held(&cgroup_mutex
);
3517 /* add/rm files for all cgroups created before */
3518 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
)) {
3519 struct cgroup
*cgrp
= css
->cgroup
;
3521 if (cgroup_is_dead(cgrp
))
3524 ret
= cgroup_addrm_files(css
, cgrp
, cfts
, is_add
);
3530 kernfs_activate(root
->kn
);
3534 static void cgroup_exit_cftypes(struct cftype
*cfts
)
3538 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3539 /* free copy for custom atomic_write_len, see init_cftypes() */
3540 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
)
3545 /* revert flags set by cgroup core while adding @cfts */
3546 cft
->flags
&= ~(__CFTYPE_ONLY_ON_DFL
| __CFTYPE_NOT_ON_DFL
);
3550 static int cgroup_init_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3554 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
3555 struct kernfs_ops
*kf_ops
;
3557 WARN_ON(cft
->ss
|| cft
->kf_ops
);
3560 kf_ops
= &cgroup_kf_ops
;
3562 kf_ops
= &cgroup_kf_single_ops
;
3565 * Ugh... if @cft wants a custom max_write_len, we need to
3566 * make a copy of kf_ops to set its atomic_write_len.
3568 if (cft
->max_write_len
&& cft
->max_write_len
!= PAGE_SIZE
) {
3569 kf_ops
= kmemdup(kf_ops
, sizeof(*kf_ops
), GFP_KERNEL
);
3571 cgroup_exit_cftypes(cfts
);
3574 kf_ops
->atomic_write_len
= cft
->max_write_len
;
3577 cft
->kf_ops
= kf_ops
;
3584 static int cgroup_rm_cftypes_locked(struct cftype
*cfts
)
3586 lockdep_assert_held(&cgroup_mutex
);
3588 if (!cfts
|| !cfts
[0].ss
)
3591 list_del(&cfts
->node
);
3592 cgroup_apply_cftypes(cfts
, false);
3593 cgroup_exit_cftypes(cfts
);
3598 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3599 * @cfts: zero-length name terminated array of cftypes
3601 * Unregister @cfts. Files described by @cfts are removed from all
3602 * existing cgroups and all future cgroups won't have them either. This
3603 * function can be called anytime whether @cfts' subsys is attached or not.
3605 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3608 int cgroup_rm_cftypes(struct cftype
*cfts
)
3612 mutex_lock(&cgroup_mutex
);
3613 ret
= cgroup_rm_cftypes_locked(cfts
);
3614 mutex_unlock(&cgroup_mutex
);
3619 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3620 * @ss: target cgroup subsystem
3621 * @cfts: zero-length name terminated array of cftypes
3623 * Register @cfts to @ss. Files described by @cfts are created for all
3624 * existing cgroups to which @ss is attached and all future cgroups will
3625 * have them too. This function can be called anytime whether @ss is
3628 * Returns 0 on successful registration, -errno on failure. Note that this
3629 * function currently returns 0 as long as @cfts registration is successful
3630 * even if some file creation attempts on existing cgroups fail.
3632 static int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3636 if (!cgroup_ssid_enabled(ss
->id
))
3639 if (!cfts
|| cfts
[0].name
[0] == '\0')
3642 ret
= cgroup_init_cftypes(ss
, cfts
);
3646 mutex_lock(&cgroup_mutex
);
3648 list_add_tail(&cfts
->node
, &ss
->cfts
);
3649 ret
= cgroup_apply_cftypes(cfts
, true);
3651 cgroup_rm_cftypes_locked(cfts
);
3653 mutex_unlock(&cgroup_mutex
);
3658 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3659 * @ss: target cgroup subsystem
3660 * @cfts: zero-length name terminated array of cftypes
3662 * Similar to cgroup_add_cftypes() but the added files are only used for
3663 * the default hierarchy.
3665 int cgroup_add_dfl_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3669 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3670 cft
->flags
|= __CFTYPE_ONLY_ON_DFL
;
3671 return cgroup_add_cftypes(ss
, cfts
);
3675 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3676 * @ss: target cgroup subsystem
3677 * @cfts: zero-length name terminated array of cftypes
3679 * Similar to cgroup_add_cftypes() but the added files are only used for
3680 * the legacy hierarchies.
3682 int cgroup_add_legacy_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
3686 for (cft
= cfts
; cft
&& cft
->name
[0] != '\0'; cft
++)
3687 cft
->flags
|= __CFTYPE_NOT_ON_DFL
;
3688 return cgroup_add_cftypes(ss
, cfts
);
3692 * cgroup_file_notify - generate a file modified event for a cgroup_file
3693 * @cfile: target cgroup_file
3695 * @cfile must have been obtained by setting cftype->file_offset.
3697 void cgroup_file_notify(struct cgroup_file
*cfile
)
3699 unsigned long flags
;
3701 spin_lock_irqsave(&cgroup_file_kn_lock
, flags
);
3703 kernfs_notify(cfile
->kn
);
3704 spin_unlock_irqrestore(&cgroup_file_kn_lock
, flags
);
3708 * cgroup_task_count - count the number of tasks in a cgroup.
3709 * @cgrp: the cgroup in question
3711 * Return the number of tasks in the cgroup.
3713 static int cgroup_task_count(const struct cgroup
*cgrp
)
3716 struct cgrp_cset_link
*link
;
3718 spin_lock_bh(&css_set_lock
);
3719 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
3720 count
+= atomic_read(&link
->cset
->refcount
);
3721 spin_unlock_bh(&css_set_lock
);
3726 * css_next_child - find the next child of a given css
3727 * @pos: the current position (%NULL to initiate traversal)
3728 * @parent: css whose children to walk
3730 * This function returns the next child of @parent and should be called
3731 * under either cgroup_mutex or RCU read lock. The only requirement is
3732 * that @parent and @pos are accessible. The next sibling is guaranteed to
3733 * be returned regardless of their states.
3735 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3736 * css which finished ->css_online() is guaranteed to be visible in the
3737 * future iterations and will stay visible until the last reference is put.
3738 * A css which hasn't finished ->css_online() or already finished
3739 * ->css_offline() may show up during traversal. It's each subsystem's
3740 * responsibility to synchronize against on/offlining.
3742 struct cgroup_subsys_state
*css_next_child(struct cgroup_subsys_state
*pos
,
3743 struct cgroup_subsys_state
*parent
)
3745 struct cgroup_subsys_state
*next
;
3747 cgroup_assert_mutex_or_rcu_locked();
3750 * @pos could already have been unlinked from the sibling list.
3751 * Once a cgroup is removed, its ->sibling.next is no longer
3752 * updated when its next sibling changes. CSS_RELEASED is set when
3753 * @pos is taken off list, at which time its next pointer is valid,
3754 * and, as releases are serialized, the one pointed to by the next
3755 * pointer is guaranteed to not have started release yet. This
3756 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3757 * critical section, the one pointed to by its next pointer is
3758 * guaranteed to not have finished its RCU grace period even if we
3759 * have dropped rcu_read_lock() inbetween iterations.
3761 * If @pos has CSS_RELEASED set, its next pointer can't be
3762 * dereferenced; however, as each css is given a monotonically
3763 * increasing unique serial number and always appended to the
3764 * sibling list, the next one can be found by walking the parent's
3765 * children until the first css with higher serial number than
3766 * @pos's. While this path can be slower, it happens iff iteration
3767 * races against release and the race window is very small.
3770 next
= list_entry_rcu(parent
->children
.next
, struct cgroup_subsys_state
, sibling
);
3771 } else if (likely(!(pos
->flags
& CSS_RELEASED
))) {
3772 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup_subsys_state
, sibling
);
3774 list_for_each_entry_rcu(next
, &parent
->children
, sibling
)
3775 if (next
->serial_nr
> pos
->serial_nr
)
3780 * @next, if not pointing to the head, can be dereferenced and is
3783 if (&next
->sibling
!= &parent
->children
)
3789 * css_next_descendant_pre - find the next descendant for pre-order walk
3790 * @pos: the current position (%NULL to initiate traversal)
3791 * @root: css whose descendants to walk
3793 * To be used by css_for_each_descendant_pre(). Find the next descendant
3794 * to visit for pre-order traversal of @root's descendants. @root is
3795 * included in the iteration and the first node to be visited.
3797 * While this function requires cgroup_mutex or RCU read locking, it
3798 * doesn't require the whole traversal to be contained in a single critical
3799 * section. This function will return the correct next descendant as long
3800 * as both @pos and @root are accessible and @pos is a descendant of @root.
3802 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3803 * css which finished ->css_online() is guaranteed to be visible in the
3804 * future iterations and will stay visible until the last reference is put.
3805 * A css which hasn't finished ->css_online() or already finished
3806 * ->css_offline() may show up during traversal. It's each subsystem's
3807 * responsibility to synchronize against on/offlining.
3809 struct cgroup_subsys_state
*
3810 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
3811 struct cgroup_subsys_state
*root
)
3813 struct cgroup_subsys_state
*next
;
3815 cgroup_assert_mutex_or_rcu_locked();
3817 /* if first iteration, visit @root */
3821 /* visit the first child if exists */
3822 next
= css_next_child(NULL
, pos
);
3826 /* no child, visit my or the closest ancestor's next sibling */
3827 while (pos
!= root
) {
3828 next
= css_next_child(pos
, pos
->parent
);
3838 * css_rightmost_descendant - return the rightmost descendant of a css
3839 * @pos: css of interest
3841 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3842 * is returned. This can be used during pre-order traversal to skip
3845 * While this function requires cgroup_mutex or RCU read locking, it
3846 * doesn't require the whole traversal to be contained in a single critical
3847 * section. This function will return the correct rightmost descendant as
3848 * long as @pos is accessible.
3850 struct cgroup_subsys_state
*
3851 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
3853 struct cgroup_subsys_state
*last
, *tmp
;
3855 cgroup_assert_mutex_or_rcu_locked();
3859 /* ->prev isn't RCU safe, walk ->next till the end */
3861 css_for_each_child(tmp
, last
)
3868 static struct cgroup_subsys_state
*
3869 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
3871 struct cgroup_subsys_state
*last
;
3875 pos
= css_next_child(NULL
, pos
);
3882 * css_next_descendant_post - find the next descendant for post-order walk
3883 * @pos: the current position (%NULL to initiate traversal)
3884 * @root: css whose descendants to walk
3886 * To be used by css_for_each_descendant_post(). Find the next descendant
3887 * to visit for post-order traversal of @root's descendants. @root is
3888 * included in the iteration and the last node to be visited.
3890 * While this function requires cgroup_mutex or RCU read locking, it
3891 * doesn't require the whole traversal to be contained in a single critical
3892 * section. This function will return the correct next descendant as long
3893 * as both @pos and @cgroup are accessible and @pos is a descendant of
3896 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3897 * css which finished ->css_online() is guaranteed to be visible in the
3898 * future iterations and will stay visible until the last reference is put.
3899 * A css which hasn't finished ->css_online() or already finished
3900 * ->css_offline() may show up during traversal. It's each subsystem's
3901 * responsibility to synchronize against on/offlining.
3903 struct cgroup_subsys_state
*
3904 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
3905 struct cgroup_subsys_state
*root
)
3907 struct cgroup_subsys_state
*next
;
3909 cgroup_assert_mutex_or_rcu_locked();
3911 /* if first iteration, visit leftmost descendant which may be @root */
3913 return css_leftmost_descendant(root
);
3915 /* if we visited @root, we're done */
3919 /* if there's an unvisited sibling, visit its leftmost descendant */
3920 next
= css_next_child(pos
, pos
->parent
);
3922 return css_leftmost_descendant(next
);
3924 /* no sibling left, visit parent */
3929 * css_has_online_children - does a css have online children
3930 * @css: the target css
3932 * Returns %true if @css has any online children; otherwise, %false. This
3933 * function can be called from any context but the caller is responsible
3934 * for synchronizing against on/offlining as necessary.
3936 bool css_has_online_children(struct cgroup_subsys_state
*css
)
3938 struct cgroup_subsys_state
*child
;
3942 css_for_each_child(child
, css
) {
3943 if (child
->flags
& CSS_ONLINE
) {
3953 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
3954 * @it: the iterator to advance
3956 * Advance @it to the next css_set to walk.
3958 static void css_task_iter_advance_css_set(struct css_task_iter
*it
)
3960 struct list_head
*l
= it
->cset_pos
;
3961 struct cgrp_cset_link
*link
;
3962 struct css_set
*cset
;
3964 lockdep_assert_held(&css_set_lock
);
3966 /* Advance to the next non-empty css_set */
3969 if (l
== it
->cset_head
) {
3970 it
->cset_pos
= NULL
;
3971 it
->task_pos
= NULL
;
3976 cset
= container_of(l
, struct css_set
,
3977 e_cset_node
[it
->ss
->id
]);
3979 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
3982 } while (!css_set_populated(cset
));
3986 if (!list_empty(&cset
->tasks
))
3987 it
->task_pos
= cset
->tasks
.next
;
3989 it
->task_pos
= cset
->mg_tasks
.next
;
3991 it
->tasks_head
= &cset
->tasks
;
3992 it
->mg_tasks_head
= &cset
->mg_tasks
;
3995 * We don't keep css_sets locked across iteration steps and thus
3996 * need to take steps to ensure that iteration can be resumed after
3997 * the lock is re-acquired. Iteration is performed at two levels -
3998 * css_sets and tasks in them.
4000 * Once created, a css_set never leaves its cgroup lists, so a
4001 * pinned css_set is guaranteed to stay put and we can resume
4002 * iteration afterwards.
4004 * Tasks may leave @cset across iteration steps. This is resolved
4005 * by registering each iterator with the css_set currently being
4006 * walked and making css_set_move_task() advance iterators whose
4007 * next task is leaving.
4010 list_del(&it
->iters_node
);
4011 put_css_set_locked(it
->cur_cset
);
4014 it
->cur_cset
= cset
;
4015 list_add(&it
->iters_node
, &cset
->task_iters
);
4018 static void css_task_iter_advance(struct css_task_iter
*it
)
4020 struct list_head
*l
= it
->task_pos
;
4022 lockdep_assert_held(&css_set_lock
);
4026 * Advance iterator to find next entry. cset->tasks is consumed
4027 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
4032 if (l
== it
->tasks_head
)
4033 l
= it
->mg_tasks_head
->next
;
4035 if (l
== it
->mg_tasks_head
)
4036 css_task_iter_advance_css_set(it
);
4042 * css_task_iter_start - initiate task iteration
4043 * @css: the css to walk tasks of
4044 * @it: the task iterator to use
4046 * Initiate iteration through the tasks of @css. The caller can call
4047 * css_task_iter_next() to walk through the tasks until the function
4048 * returns NULL. On completion of iteration, css_task_iter_end() must be
4051 void css_task_iter_start(struct cgroup_subsys_state
*css
,
4052 struct css_task_iter
*it
)
4054 /* no one should try to iterate before mounting cgroups */
4055 WARN_ON_ONCE(!use_task_css_set_links
);
4057 memset(it
, 0, sizeof(*it
));
4059 spin_lock_bh(&css_set_lock
);
4064 it
->cset_pos
= &css
->cgroup
->e_csets
[css
->ss
->id
];
4066 it
->cset_pos
= &css
->cgroup
->cset_links
;
4068 it
->cset_head
= it
->cset_pos
;
4070 css_task_iter_advance_css_set(it
);
4072 spin_unlock_bh(&css_set_lock
);
4076 * css_task_iter_next - return the next task for the iterator
4077 * @it: the task iterator being iterated
4079 * The "next" function for task iteration. @it should have been
4080 * initialized via css_task_iter_start(). Returns NULL when the iteration
4083 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
4086 put_task_struct(it
->cur_task
);
4087 it
->cur_task
= NULL
;
4090 spin_lock_bh(&css_set_lock
);
4093 it
->cur_task
= list_entry(it
->task_pos
, struct task_struct
,
4095 get_task_struct(it
->cur_task
);
4096 css_task_iter_advance(it
);
4099 spin_unlock_bh(&css_set_lock
);
4101 return it
->cur_task
;
4105 * css_task_iter_end - finish task iteration
4106 * @it: the task iterator to finish
4108 * Finish task iteration started by css_task_iter_start().
4110 void css_task_iter_end(struct css_task_iter
*it
)
4113 spin_lock_bh(&css_set_lock
);
4114 list_del(&it
->iters_node
);
4115 put_css_set_locked(it
->cur_cset
);
4116 spin_unlock_bh(&css_set_lock
);
4120 put_task_struct(it
->cur_task
);
4124 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
4125 * @to: cgroup to which the tasks will be moved
4126 * @from: cgroup in which the tasks currently reside
4128 * Locking rules between cgroup_post_fork() and the migration path
4129 * guarantee that, if a task is forking while being migrated, the new child
4130 * is guaranteed to be either visible in the source cgroup after the
4131 * parent's migration is complete or put into the target cgroup. No task
4132 * can slip out of migration through forking.
4134 int cgroup_transfer_tasks(struct cgroup
*to
, struct cgroup
*from
)
4136 LIST_HEAD(preloaded_csets
);
4137 struct cgrp_cset_link
*link
;
4138 struct css_task_iter it
;
4139 struct task_struct
*task
;
4142 mutex_lock(&cgroup_mutex
);
4144 /* all tasks in @from are being moved, all csets are source */
4145 spin_lock_bh(&css_set_lock
);
4146 list_for_each_entry(link
, &from
->cset_links
, cset_link
)
4147 cgroup_migrate_add_src(link
->cset
, to
, &preloaded_csets
);
4148 spin_unlock_bh(&css_set_lock
);
4150 ret
= cgroup_migrate_prepare_dst(to
, &preloaded_csets
);
4155 * Migrate tasks one-by-one until @form is empty. This fails iff
4156 * ->can_attach() fails.
4159 css_task_iter_start(&from
->self
, &it
);
4160 task
= css_task_iter_next(&it
);
4162 get_task_struct(task
);
4163 css_task_iter_end(&it
);
4166 ret
= cgroup_migrate(task
, false, to
);
4167 put_task_struct(task
);
4169 } while (task
&& !ret
);
4171 cgroup_migrate_finish(&preloaded_csets
);
4172 mutex_unlock(&cgroup_mutex
);
4177 * Stuff for reading the 'tasks'/'procs' files.
4179 * Reading this file can return large amounts of data if a cgroup has
4180 * *lots* of attached tasks. So it may need several calls to read(),
4181 * but we cannot guarantee that the information we produce is correct
4182 * unless we produce it entirely atomically.
4186 /* which pidlist file are we talking about? */
4187 enum cgroup_filetype
{
4193 * A pidlist is a list of pids that virtually represents the contents of one
4194 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
4195 * a pair (one each for procs, tasks) for each pid namespace that's relevant
4198 struct cgroup_pidlist
{
4200 * used to find which pidlist is wanted. doesn't change as long as
4201 * this particular list stays in the list.
4203 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
4206 /* how many elements the above list has */
4208 /* each of these stored in a list by its cgroup */
4209 struct list_head links
;
4210 /* pointer to the cgroup we belong to, for list removal purposes */
4211 struct cgroup
*owner
;
4212 /* for delayed destruction */
4213 struct delayed_work destroy_dwork
;
4217 * The following two functions "fix" the issue where there are more pids
4218 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
4219 * TODO: replace with a kernel-wide solution to this problem
4221 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
4222 static void *pidlist_allocate(int count
)
4224 if (PIDLIST_TOO_LARGE(count
))
4225 return vmalloc(count
* sizeof(pid_t
));
4227 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
4230 static void pidlist_free(void *p
)
4236 * Used to destroy all pidlists lingering waiting for destroy timer. None
4237 * should be left afterwards.
4239 static void cgroup_pidlist_destroy_all(struct cgroup
*cgrp
)
4241 struct cgroup_pidlist
*l
, *tmp_l
;
4243 mutex_lock(&cgrp
->pidlist_mutex
);
4244 list_for_each_entry_safe(l
, tmp_l
, &cgrp
->pidlists
, links
)
4245 mod_delayed_work(cgroup_pidlist_destroy_wq
, &l
->destroy_dwork
, 0);
4246 mutex_unlock(&cgrp
->pidlist_mutex
);
4248 flush_workqueue(cgroup_pidlist_destroy_wq
);
4249 BUG_ON(!list_empty(&cgrp
->pidlists
));
4252 static void cgroup_pidlist_destroy_work_fn(struct work_struct
*work
)
4254 struct delayed_work
*dwork
= to_delayed_work(work
);
4255 struct cgroup_pidlist
*l
= container_of(dwork
, struct cgroup_pidlist
,
4257 struct cgroup_pidlist
*tofree
= NULL
;
4259 mutex_lock(&l
->owner
->pidlist_mutex
);
4262 * Destroy iff we didn't get queued again. The state won't change
4263 * as destroy_dwork can only be queued while locked.
4265 if (!delayed_work_pending(dwork
)) {
4266 list_del(&l
->links
);
4267 pidlist_free(l
->list
);
4268 put_pid_ns(l
->key
.ns
);
4272 mutex_unlock(&l
->owner
->pidlist_mutex
);
4277 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
4278 * Returns the number of unique elements.
4280 static int pidlist_uniq(pid_t
*list
, int length
)
4285 * we presume the 0th element is unique, so i starts at 1. trivial
4286 * edge cases first; no work needs to be done for either
4288 if (length
== 0 || length
== 1)
4290 /* src and dest walk down the list; dest counts unique elements */
4291 for (src
= 1; src
< length
; src
++) {
4292 /* find next unique element */
4293 while (list
[src
] == list
[src
-1]) {
4298 /* dest always points to where the next unique element goes */
4299 list
[dest
] = list
[src
];
4307 * The two pid files - task and cgroup.procs - guaranteed that the result
4308 * is sorted, which forced this whole pidlist fiasco. As pid order is
4309 * different per namespace, each namespace needs differently sorted list,
4310 * making it impossible to use, for example, single rbtree of member tasks
4311 * sorted by task pointer. As pidlists can be fairly large, allocating one
4312 * per open file is dangerous, so cgroup had to implement shared pool of
4313 * pidlists keyed by cgroup and namespace.
4315 * All this extra complexity was caused by the original implementation
4316 * committing to an entirely unnecessary property. In the long term, we
4317 * want to do away with it. Explicitly scramble sort order if on the
4318 * default hierarchy so that no such expectation exists in the new
4321 * Scrambling is done by swapping every two consecutive bits, which is
4322 * non-identity one-to-one mapping which disturbs sort order sufficiently.
4324 static pid_t
pid_fry(pid_t pid
)
4326 unsigned a
= pid
& 0x55555555;
4327 unsigned b
= pid
& 0xAAAAAAAA;
4329 return (a
<< 1) | (b
>> 1);
4332 static pid_t
cgroup_pid_fry(struct cgroup
*cgrp
, pid_t pid
)
4334 if (cgroup_on_dfl(cgrp
))
4335 return pid_fry(pid
);
4340 static int cmppid(const void *a
, const void *b
)
4342 return *(pid_t
*)a
- *(pid_t
*)b
;
4345 static int fried_cmppid(const void *a
, const void *b
)
4347 return pid_fry(*(pid_t
*)a
) - pid_fry(*(pid_t
*)b
);
4350 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
4351 enum cgroup_filetype type
)
4353 struct cgroup_pidlist
*l
;
4354 /* don't need task_nsproxy() if we're looking at ourself */
4355 struct pid_namespace
*ns
= task_active_pid_ns(current
);
4357 lockdep_assert_held(&cgrp
->pidlist_mutex
);
4359 list_for_each_entry(l
, &cgrp
->pidlists
, links
)
4360 if (l
->key
.type
== type
&& l
->key
.ns
== ns
)
4366 * find the appropriate pidlist for our purpose (given procs vs tasks)
4367 * returns with the lock on that pidlist already held, and takes care
4368 * of the use count, or returns NULL with no locks held if we're out of
4371 static struct cgroup_pidlist
*cgroup_pidlist_find_create(struct cgroup
*cgrp
,
4372 enum cgroup_filetype type
)
4374 struct cgroup_pidlist
*l
;
4376 lockdep_assert_held(&cgrp
->pidlist_mutex
);
4378 l
= cgroup_pidlist_find(cgrp
, type
);
4382 /* entry not found; create a new one */
4383 l
= kzalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
4387 INIT_DELAYED_WORK(&l
->destroy_dwork
, cgroup_pidlist_destroy_work_fn
);
4389 /* don't need task_nsproxy() if we're looking at ourself */
4390 l
->key
.ns
= get_pid_ns(task_active_pid_ns(current
));
4392 list_add(&l
->links
, &cgrp
->pidlists
);
4397 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
4399 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
4400 struct cgroup_pidlist
**lp
)
4404 int pid
, n
= 0; /* used for populating the array */
4405 struct css_task_iter it
;
4406 struct task_struct
*tsk
;
4407 struct cgroup_pidlist
*l
;
4409 lockdep_assert_held(&cgrp
->pidlist_mutex
);
4412 * If cgroup gets more users after we read count, we won't have
4413 * enough space - tough. This race is indistinguishable to the
4414 * caller from the case that the additional cgroup users didn't
4415 * show up until sometime later on.
4417 length
= cgroup_task_count(cgrp
);
4418 array
= pidlist_allocate(length
);
4421 /* now, populate the array */
4422 css_task_iter_start(&cgrp
->self
, &it
);
4423 while ((tsk
= css_task_iter_next(&it
))) {
4424 if (unlikely(n
== length
))
4426 /* get tgid or pid for procs or tasks file respectively */
4427 if (type
== CGROUP_FILE_PROCS
)
4428 pid
= task_tgid_vnr(tsk
);
4430 pid
= task_pid_vnr(tsk
);
4431 if (pid
> 0) /* make sure to only use valid results */
4434 css_task_iter_end(&it
);
4436 /* now sort & (if procs) strip out duplicates */
4437 if (cgroup_on_dfl(cgrp
))
4438 sort(array
, length
, sizeof(pid_t
), fried_cmppid
, NULL
);
4440 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
4441 if (type
== CGROUP_FILE_PROCS
)
4442 length
= pidlist_uniq(array
, length
);
4444 l
= cgroup_pidlist_find_create(cgrp
, type
);
4446 pidlist_free(array
);
4450 /* store array, freeing old if necessary */
4451 pidlist_free(l
->list
);
4459 * cgroupstats_build - build and fill cgroupstats
4460 * @stats: cgroupstats to fill information into
4461 * @dentry: A dentry entry belonging to the cgroup for which stats have
4464 * Build and fill cgroupstats so that taskstats can export it to user
4467 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
4469 struct kernfs_node
*kn
= kernfs_node_from_dentry(dentry
);
4470 struct cgroup
*cgrp
;
4471 struct css_task_iter it
;
4472 struct task_struct
*tsk
;
4474 /* it should be kernfs_node belonging to cgroupfs and is a directory */
4475 if (dentry
->d_sb
->s_type
!= &cgroup_fs_type
|| !kn
||
4476 kernfs_type(kn
) != KERNFS_DIR
)
4479 mutex_lock(&cgroup_mutex
);
4482 * We aren't being called from kernfs and there's no guarantee on
4483 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
4484 * @kn->priv is RCU safe. Let's do the RCU dancing.
4487 cgrp
= rcu_dereference(kn
->priv
);
4488 if (!cgrp
|| cgroup_is_dead(cgrp
)) {
4490 mutex_unlock(&cgroup_mutex
);
4495 css_task_iter_start(&cgrp
->self
, &it
);
4496 while ((tsk
= css_task_iter_next(&it
))) {
4497 switch (tsk
->state
) {
4499 stats
->nr_running
++;
4501 case TASK_INTERRUPTIBLE
:
4502 stats
->nr_sleeping
++;
4504 case TASK_UNINTERRUPTIBLE
:
4505 stats
->nr_uninterruptible
++;
4508 stats
->nr_stopped
++;
4511 if (delayacct_is_task_waiting_on_io(tsk
))
4512 stats
->nr_io_wait
++;
4516 css_task_iter_end(&it
);
4518 mutex_unlock(&cgroup_mutex
);
4524 * seq_file methods for the tasks/procs files. The seq_file position is the
4525 * next pid to display; the seq_file iterator is a pointer to the pid
4526 * in the cgroup->l->list array.
4529 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
4532 * Initially we receive a position value that corresponds to
4533 * one more than the last pid shown (or 0 on the first call or
4534 * after a seek to the start). Use a binary-search to find the
4535 * next pid to display, if any
4537 struct kernfs_open_file
*of
= s
->private;
4538 struct cgroup
*cgrp
= seq_css(s
)->cgroup
;
4539 struct cgroup_pidlist
*l
;
4540 enum cgroup_filetype type
= seq_cft(s
)->private;
4541 int index
= 0, pid
= *pos
;
4544 mutex_lock(&cgrp
->pidlist_mutex
);
4547 * !NULL @of->priv indicates that this isn't the first start()
4548 * after open. If the matching pidlist is around, we can use that.
4549 * Look for it. Note that @of->priv can't be used directly. It
4550 * could already have been destroyed.
4553 of
->priv
= cgroup_pidlist_find(cgrp
, type
);
4556 * Either this is the first start() after open or the matching
4557 * pidlist has been destroyed inbetween. Create a new one.
4560 ret
= pidlist_array_load(cgrp
, type
,
4561 (struct cgroup_pidlist
**)&of
->priv
);
4563 return ERR_PTR(ret
);
4568 int end
= l
->length
;
4570 while (index
< end
) {
4571 int mid
= (index
+ end
) / 2;
4572 if (cgroup_pid_fry(cgrp
, l
->list
[mid
]) == pid
) {
4575 } else if (cgroup_pid_fry(cgrp
, l
->list
[mid
]) <= pid
)
4581 /* If we're off the end of the array, we're done */
4582 if (index
>= l
->length
)
4584 /* Update the abstract position to be the actual pid that we found */
4585 iter
= l
->list
+ index
;
4586 *pos
= cgroup_pid_fry(cgrp
, *iter
);
4590 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
4592 struct kernfs_open_file
*of
= s
->private;
4593 struct cgroup_pidlist
*l
= of
->priv
;
4596 mod_delayed_work(cgroup_pidlist_destroy_wq
, &l
->destroy_dwork
,
4597 CGROUP_PIDLIST_DESTROY_DELAY
);
4598 mutex_unlock(&seq_css(s
)->cgroup
->pidlist_mutex
);
4601 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
4603 struct kernfs_open_file
*of
= s
->private;
4604 struct cgroup_pidlist
*l
= of
->priv
;
4606 pid_t
*end
= l
->list
+ l
->length
;
4608 * Advance to the next pid in the array. If this goes off the
4615 *pos
= cgroup_pid_fry(seq_css(s
)->cgroup
, *p
);
4620 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
4622 seq_printf(s
, "%d\n", *(int *)v
);
4627 static u64
cgroup_read_notify_on_release(struct cgroup_subsys_state
*css
,
4630 return notify_on_release(css
->cgroup
);
4633 static int cgroup_write_notify_on_release(struct cgroup_subsys_state
*css
,
4634 struct cftype
*cft
, u64 val
)
4637 set_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
4639 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
4643 static u64
cgroup_clone_children_read(struct cgroup_subsys_state
*css
,
4646 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4649 static int cgroup_clone_children_write(struct cgroup_subsys_state
*css
,
4650 struct cftype
*cft
, u64 val
)
4653 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4655 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4659 /* cgroup core interface files for the default hierarchy */
4660 static struct cftype cgroup_dfl_base_files
[] = {
4662 .name
= "cgroup.procs",
4663 .file_offset
= offsetof(struct cgroup
, procs_file
),
4664 .seq_start
= cgroup_pidlist_start
,
4665 .seq_next
= cgroup_pidlist_next
,
4666 .seq_stop
= cgroup_pidlist_stop
,
4667 .seq_show
= cgroup_pidlist_show
,
4668 .private = CGROUP_FILE_PROCS
,
4669 .write
= cgroup_procs_write
,
4672 .name
= "cgroup.controllers",
4673 .flags
= CFTYPE_ONLY_ON_ROOT
,
4674 .seq_show
= cgroup_root_controllers_show
,
4677 .name
= "cgroup.controllers",
4678 .flags
= CFTYPE_NOT_ON_ROOT
,
4679 .seq_show
= cgroup_controllers_show
,
4682 .name
= "cgroup.subtree_control",
4683 .seq_show
= cgroup_subtree_control_show
,
4684 .write
= cgroup_subtree_control_write
,
4687 .name
= "cgroup.events",
4688 .flags
= CFTYPE_NOT_ON_ROOT
,
4689 .file_offset
= offsetof(struct cgroup
, events_file
),
4690 .seq_show
= cgroup_events_show
,
4695 /* cgroup core interface files for the legacy hierarchies */
4696 static struct cftype cgroup_legacy_base_files
[] = {
4698 .name
= "cgroup.procs",
4699 .seq_start
= cgroup_pidlist_start
,
4700 .seq_next
= cgroup_pidlist_next
,
4701 .seq_stop
= cgroup_pidlist_stop
,
4702 .seq_show
= cgroup_pidlist_show
,
4703 .private = CGROUP_FILE_PROCS
,
4704 .write
= cgroup_procs_write
,
4707 .name
= "cgroup.clone_children",
4708 .read_u64
= cgroup_clone_children_read
,
4709 .write_u64
= cgroup_clone_children_write
,
4712 .name
= "cgroup.sane_behavior",
4713 .flags
= CFTYPE_ONLY_ON_ROOT
,
4714 .seq_show
= cgroup_sane_behavior_show
,
4718 .seq_start
= cgroup_pidlist_start
,
4719 .seq_next
= cgroup_pidlist_next
,
4720 .seq_stop
= cgroup_pidlist_stop
,
4721 .seq_show
= cgroup_pidlist_show
,
4722 .private = CGROUP_FILE_TASKS
,
4723 .write
= cgroup_tasks_write
,
4726 .name
= "notify_on_release",
4727 .read_u64
= cgroup_read_notify_on_release
,
4728 .write_u64
= cgroup_write_notify_on_release
,
4731 .name
= "release_agent",
4732 .flags
= CFTYPE_ONLY_ON_ROOT
,
4733 .seq_show
= cgroup_release_agent_show
,
4734 .write
= cgroup_release_agent_write
,
4735 .max_write_len
= PATH_MAX
- 1,
4741 * css destruction is four-stage process.
4743 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4744 * Implemented in kill_css().
4746 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4747 * and thus css_tryget_online() is guaranteed to fail, the css can be
4748 * offlined by invoking offline_css(). After offlining, the base ref is
4749 * put. Implemented in css_killed_work_fn().
4751 * 3. When the percpu_ref reaches zero, the only possible remaining
4752 * accessors are inside RCU read sections. css_release() schedules the
4755 * 4. After the grace period, the css can be freed. Implemented in
4756 * css_free_work_fn().
4758 * It is actually hairier because both step 2 and 4 require process context
4759 * and thus involve punting to css->destroy_work adding two additional
4760 * steps to the already complex sequence.
4762 static void css_free_work_fn(struct work_struct
*work
)
4764 struct cgroup_subsys_state
*css
=
4765 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4766 struct cgroup_subsys
*ss
= css
->ss
;
4767 struct cgroup
*cgrp
= css
->cgroup
;
4769 percpu_ref_exit(&css
->refcnt
);
4776 css_put(css
->parent
);
4779 cgroup_idr_remove(&ss
->css_idr
, id
);
4782 /* cgroup free path */
4783 atomic_dec(&cgrp
->root
->nr_cgrps
);
4784 cgroup_pidlist_destroy_all(cgrp
);
4785 cancel_work_sync(&cgrp
->release_agent_work
);
4787 if (cgroup_parent(cgrp
)) {
4789 * We get a ref to the parent, and put the ref when
4790 * this cgroup is being freed, so it's guaranteed
4791 * that the parent won't be destroyed before its
4794 cgroup_put(cgroup_parent(cgrp
));
4795 kernfs_put(cgrp
->kn
);
4799 * This is root cgroup's refcnt reaching zero,
4800 * which indicates that the root should be
4803 cgroup_destroy_root(cgrp
->root
);
4808 static void css_free_rcu_fn(struct rcu_head
*rcu_head
)
4810 struct cgroup_subsys_state
*css
=
4811 container_of(rcu_head
, struct cgroup_subsys_state
, rcu_head
);
4813 INIT_WORK(&css
->destroy_work
, css_free_work_fn
);
4814 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4817 static void css_release_work_fn(struct work_struct
*work
)
4819 struct cgroup_subsys_state
*css
=
4820 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4821 struct cgroup_subsys
*ss
= css
->ss
;
4822 struct cgroup
*cgrp
= css
->cgroup
;
4824 mutex_lock(&cgroup_mutex
);
4826 css
->flags
|= CSS_RELEASED
;
4827 list_del_rcu(&css
->sibling
);
4830 /* css release path */
4831 cgroup_idr_replace(&ss
->css_idr
, NULL
, css
->id
);
4832 if (ss
->css_released
)
4833 ss
->css_released(css
);
4835 /* cgroup release path */
4836 cgroup_idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
4840 * There are two control paths which try to determine
4841 * cgroup from dentry without going through kernfs -
4842 * cgroupstats_build() and css_tryget_online_from_dir().
4843 * Those are supported by RCU protecting clearing of
4844 * cgrp->kn->priv backpointer.
4846 RCU_INIT_POINTER(*(void __rcu __force
**)&cgrp
->kn
->priv
, NULL
);
4849 mutex_unlock(&cgroup_mutex
);
4851 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4854 static void css_release(struct percpu_ref
*ref
)
4856 struct cgroup_subsys_state
*css
=
4857 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4859 INIT_WORK(&css
->destroy_work
, css_release_work_fn
);
4860 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4863 static void init_and_link_css(struct cgroup_subsys_state
*css
,
4864 struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4866 lockdep_assert_held(&cgroup_mutex
);
4870 memset(css
, 0, sizeof(*css
));
4873 INIT_LIST_HEAD(&css
->sibling
);
4874 INIT_LIST_HEAD(&css
->children
);
4875 css
->serial_nr
= css_serial_nr_next
++;
4877 if (cgroup_parent(cgrp
)) {
4878 css
->parent
= cgroup_css(cgroup_parent(cgrp
), ss
);
4879 css_get(css
->parent
);
4882 BUG_ON(cgroup_css(cgrp
, ss
));
4885 /* invoke ->css_online() on a new CSS and mark it online if successful */
4886 static int online_css(struct cgroup_subsys_state
*css
)
4888 struct cgroup_subsys
*ss
= css
->ss
;
4891 lockdep_assert_held(&cgroup_mutex
);
4894 ret
= ss
->css_online(css
);
4896 css
->flags
|= CSS_ONLINE
;
4897 rcu_assign_pointer(css
->cgroup
->subsys
[ss
->id
], css
);
4902 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4903 static void offline_css(struct cgroup_subsys_state
*css
)
4905 struct cgroup_subsys
*ss
= css
->ss
;
4907 lockdep_assert_held(&cgroup_mutex
);
4909 if (!(css
->flags
& CSS_ONLINE
))
4912 if (ss
->css_offline
)
4913 ss
->css_offline(css
);
4915 css
->flags
&= ~CSS_ONLINE
;
4916 RCU_INIT_POINTER(css
->cgroup
->subsys
[ss
->id
], NULL
);
4918 wake_up_all(&css
->cgroup
->offline_waitq
);
4922 * create_css - create a cgroup_subsys_state
4923 * @cgrp: the cgroup new css will be associated with
4924 * @ss: the subsys of new css
4925 * @visible: whether to create control knobs for the new css or not
4927 * Create a new css associated with @cgrp - @ss pair. On success, the new
4928 * css is online and installed in @cgrp with all interface files created if
4929 * @visible. Returns 0 on success, -errno on failure.
4931 static int create_css(struct cgroup
*cgrp
, struct cgroup_subsys
*ss
,
4934 struct cgroup
*parent
= cgroup_parent(cgrp
);
4935 struct cgroup_subsys_state
*parent_css
= cgroup_css(parent
, ss
);
4936 struct cgroup_subsys_state
*css
;
4939 lockdep_assert_held(&cgroup_mutex
);
4941 css
= ss
->css_alloc(parent_css
);
4943 return PTR_ERR(css
);
4945 init_and_link_css(css
, ss
, cgrp
);
4947 err
= percpu_ref_init(&css
->refcnt
, css_release
, 0, GFP_KERNEL
);
4951 err
= cgroup_idr_alloc(&ss
->css_idr
, NULL
, 2, 0, GFP_KERNEL
);
4953 goto err_free_percpu_ref
;
4957 err
= css_populate_dir(css
, NULL
);
4962 /* @css is ready to be brought online now, make it visible */
4963 list_add_tail_rcu(&css
->sibling
, &parent_css
->children
);
4964 cgroup_idr_replace(&ss
->css_idr
, css
, css
->id
);
4966 err
= online_css(css
);
4970 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4971 cgroup_parent(parent
)) {
4972 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4973 current
->comm
, current
->pid
, ss
->name
);
4974 if (!strcmp(ss
->name
, "memory"))
4975 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
4976 ss
->warned_broken_hierarchy
= true;
4982 list_del_rcu(&css
->sibling
);
4983 css_clear_dir(css
, NULL
);
4985 cgroup_idr_remove(&ss
->css_idr
, css
->id
);
4986 err_free_percpu_ref
:
4987 percpu_ref_exit(&css
->refcnt
);
4989 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4993 static int cgroup_mkdir(struct kernfs_node
*parent_kn
, const char *name
,
4996 struct cgroup
*parent
, *cgrp
;
4997 struct cgroup_root
*root
;
4998 struct cgroup_subsys
*ss
;
4999 struct kernfs_node
*kn
;
5002 /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
5004 if (strchr(name
, '\n'))
5007 parent
= cgroup_kn_lock_live(parent_kn
);
5010 root
= parent
->root
;
5012 /* allocate the cgroup and its ID, 0 is reserved for the root */
5013 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
5019 ret
= percpu_ref_init(&cgrp
->self
.refcnt
, css_release
, 0, GFP_KERNEL
);
5024 * Temporarily set the pointer to NULL, so idr_find() won't return
5025 * a half-baked cgroup.
5027 cgrp
->id
= cgroup_idr_alloc(&root
->cgroup_idr
, NULL
, 2, 0, GFP_KERNEL
);
5030 goto out_cancel_ref
;
5033 init_cgroup_housekeeping(cgrp
);
5035 cgrp
->self
.parent
= &parent
->self
;
5038 if (notify_on_release(parent
))
5039 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
5041 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
5042 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
5044 /* create the directory */
5045 kn
= kernfs_create_dir(parent
->kn
, name
, mode
, cgrp
);
5053 * This extra ref will be put in cgroup_free_fn() and guarantees
5054 * that @cgrp->kn is always accessible.
5058 cgrp
->self
.serial_nr
= css_serial_nr_next
++;
5060 /* allocation complete, commit to creation */
5061 list_add_tail_rcu(&cgrp
->self
.sibling
, &cgroup_parent(cgrp
)->self
.children
);
5062 atomic_inc(&root
->nr_cgrps
);
5066 * @cgrp is now fully operational. If something fails after this
5067 * point, it'll be released via the normal destruction path.
5069 cgroup_idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
5071 ret
= cgroup_kn_set_ugid(kn
);
5075 ret
= css_populate_dir(&cgrp
->self
, NULL
);
5079 /* let's create and online css's */
5080 for_each_subsys(ss
, ssid
) {
5081 if (parent
->child_subsys_mask
& (1 << ssid
)) {
5082 ret
= create_css(cgrp
, ss
,
5083 parent
->subtree_control
& (1 << ssid
));
5090 * On the default hierarchy, a child doesn't automatically inherit
5091 * subtree_control from the parent. Each is configured manually.
5093 if (!cgroup_on_dfl(cgrp
)) {
5094 cgrp
->subtree_control
= parent
->subtree_control
;
5095 cgroup_refresh_child_subsys_mask(cgrp
);
5098 kernfs_activate(kn
);
5104 cgroup_idr_remove(&root
->cgroup_idr
, cgrp
->id
);
5106 percpu_ref_exit(&cgrp
->self
.refcnt
);
5110 cgroup_kn_unlock(parent_kn
);
5114 cgroup_destroy_locked(cgrp
);
5119 * This is called when the refcnt of a css is confirmed to be killed.
5120 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
5121 * initate destruction and put the css ref from kill_css().
5123 static void css_killed_work_fn(struct work_struct
*work
)
5125 struct cgroup_subsys_state
*css
=
5126 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
5128 mutex_lock(&cgroup_mutex
);
5130 mutex_unlock(&cgroup_mutex
);
5135 /* css kill confirmation processing requires process context, bounce */
5136 static void css_killed_ref_fn(struct percpu_ref
*ref
)
5138 struct cgroup_subsys_state
*css
=
5139 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
5141 INIT_WORK(&css
->destroy_work
, css_killed_work_fn
);
5142 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
5146 * kill_css - destroy a css
5147 * @css: css to destroy
5149 * This function initiates destruction of @css by removing cgroup interface
5150 * files and putting its base reference. ->css_offline() will be invoked
5151 * asynchronously once css_tryget_online() is guaranteed to fail and when
5152 * the reference count reaches zero, @css will be released.
5154 static void kill_css(struct cgroup_subsys_state
*css
)
5156 lockdep_assert_held(&cgroup_mutex
);
5159 * This must happen before css is disassociated with its cgroup.
5160 * See seq_css() for details.
5162 css_clear_dir(css
, NULL
);
5165 * Killing would put the base ref, but we need to keep it alive
5166 * until after ->css_offline().
5171 * cgroup core guarantees that, by the time ->css_offline() is
5172 * invoked, no new css reference will be given out via
5173 * css_tryget_online(). We can't simply call percpu_ref_kill() and
5174 * proceed to offlining css's because percpu_ref_kill() doesn't
5175 * guarantee that the ref is seen as killed on all CPUs on return.
5177 * Use percpu_ref_kill_and_confirm() to get notifications as each
5178 * css is confirmed to be seen as killed on all CPUs.
5180 percpu_ref_kill_and_confirm(&css
->refcnt
, css_killed_ref_fn
);
5184 * cgroup_destroy_locked - the first stage of cgroup destruction
5185 * @cgrp: cgroup to be destroyed
5187 * css's make use of percpu refcnts whose killing latency shouldn't be
5188 * exposed to userland and are RCU protected. Also, cgroup core needs to
5189 * guarantee that css_tryget_online() won't succeed by the time
5190 * ->css_offline() is invoked. To satisfy all the requirements,
5191 * destruction is implemented in the following two steps.
5193 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
5194 * userland visible parts and start killing the percpu refcnts of
5195 * css's. Set up so that the next stage will be kicked off once all
5196 * the percpu refcnts are confirmed to be killed.
5198 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
5199 * rest of destruction. Once all cgroup references are gone, the
5200 * cgroup is RCU-freed.
5202 * This function implements s1. After this step, @cgrp is gone as far as
5203 * the userland is concerned and a new cgroup with the same name may be
5204 * created. As cgroup doesn't care about the names internally, this
5205 * doesn't cause any problem.
5207 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
5208 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
5210 struct cgroup_subsys_state
*css
;
5213 lockdep_assert_held(&cgroup_mutex
);
5216 * Only migration can raise populated from zero and we're already
5217 * holding cgroup_mutex.
5219 if (cgroup_is_populated(cgrp
))
5223 * Make sure there's no live children. We can't test emptiness of
5224 * ->self.children as dead children linger on it while being
5225 * drained; otherwise, "rmdir parent/child parent" may fail.
5227 if (css_has_online_children(&cgrp
->self
))
5231 * Mark @cgrp dead. This prevents further task migration and child
5232 * creation by disabling cgroup_lock_live_group().
5234 cgrp
->self
.flags
&= ~CSS_ONLINE
;
5236 /* initiate massacre of all css's */
5237 for_each_css(css
, ssid
, cgrp
)
5241 * Remove @cgrp directory along with the base files. @cgrp has an
5242 * extra ref on its kn.
5244 kernfs_remove(cgrp
->kn
);
5246 check_for_release(cgroup_parent(cgrp
));
5248 /* put the base reference */
5249 percpu_ref_kill(&cgrp
->self
.refcnt
);
5254 static int cgroup_rmdir(struct kernfs_node
*kn
)
5256 struct cgroup
*cgrp
;
5259 cgrp
= cgroup_kn_lock_live(kn
);
5263 ret
= cgroup_destroy_locked(cgrp
);
5265 cgroup_kn_unlock(kn
);
5269 static struct kernfs_syscall_ops cgroup_kf_syscall_ops
= {
5270 .remount_fs
= cgroup_remount
,
5271 .show_options
= cgroup_show_options
,
5272 .mkdir
= cgroup_mkdir
,
5273 .rmdir
= cgroup_rmdir
,
5274 .rename
= cgroup_rename
,
5277 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
, bool early
)
5279 struct cgroup_subsys_state
*css
;
5281 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
5283 mutex_lock(&cgroup_mutex
);
5285 idr_init(&ss
->css_idr
);
5286 INIT_LIST_HEAD(&ss
->cfts
);
5288 /* Create the root cgroup state for this subsystem */
5289 ss
->root
= &cgrp_dfl_root
;
5290 css
= ss
->css_alloc(cgroup_css(&cgrp_dfl_root
.cgrp
, ss
));
5291 /* We don't handle early failures gracefully */
5292 BUG_ON(IS_ERR(css
));
5293 init_and_link_css(css
, ss
, &cgrp_dfl_root
.cgrp
);
5296 * Root csses are never destroyed and we can't initialize
5297 * percpu_ref during early init. Disable refcnting.
5299 css
->flags
|= CSS_NO_REF
;
5302 /* allocation can't be done safely during early init */
5305 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2, GFP_KERNEL
);
5306 BUG_ON(css
->id
< 0);
5309 /* Update the init_css_set to contain a subsys
5310 * pointer to this state - since the subsystem is
5311 * newly registered, all tasks and hence the
5312 * init_css_set is in the subsystem's root cgroup. */
5313 init_css_set
.subsys
[ss
->id
] = css
;
5315 have_fork_callback
|= (bool)ss
->fork
<< ss
->id
;
5316 have_exit_callback
|= (bool)ss
->exit
<< ss
->id
;
5317 have_free_callback
|= (bool)ss
->free
<< ss
->id
;
5318 have_canfork_callback
|= (bool)ss
->can_fork
<< ss
->id
;
5320 /* At system boot, before all subsystems have been
5321 * registered, no tasks have been forked, so we don't
5322 * need to invoke fork callbacks here. */
5323 BUG_ON(!list_empty(&init_task
.tasks
));
5325 BUG_ON(online_css(css
));
5327 mutex_unlock(&cgroup_mutex
);
5331 * cgroup_init_early - cgroup initialization at system boot
5333 * Initialize cgroups at system boot, and initialize any
5334 * subsystems that request early init.
5336 int __init
cgroup_init_early(void)
5338 static struct cgroup_sb_opts __initdata opts
;
5339 struct cgroup_subsys
*ss
;
5342 init_cgroup_root(&cgrp_dfl_root
, &opts
);
5343 cgrp_dfl_root
.cgrp
.self
.flags
|= CSS_NO_REF
;
5345 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
5347 for_each_subsys(ss
, i
) {
5348 WARN(!ss
->css_alloc
|| !ss
->css_free
|| ss
->name
|| ss
->id
,
5349 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
5350 i
, cgroup_subsys_name
[i
], ss
->css_alloc
, ss
->css_free
,
5352 WARN(strlen(cgroup_subsys_name
[i
]) > MAX_CGROUP_TYPE_NAMELEN
,
5353 "cgroup_subsys_name %s too long\n", cgroup_subsys_name
[i
]);
5356 ss
->name
= cgroup_subsys_name
[i
];
5357 if (!ss
->legacy_name
)
5358 ss
->legacy_name
= cgroup_subsys_name
[i
];
5361 cgroup_init_subsys(ss
, true);
5366 static unsigned long cgroup_disable_mask __initdata
;
5369 * cgroup_init - cgroup initialization
5371 * Register cgroup filesystem and /proc file, and initialize
5372 * any subsystems that didn't request early init.
5374 int __init
cgroup_init(void)
5376 struct cgroup_subsys
*ss
;
5380 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem
));
5381 BUG_ON(cgroup_init_cftypes(NULL
, cgroup_dfl_base_files
));
5382 BUG_ON(cgroup_init_cftypes(NULL
, cgroup_legacy_base_files
));
5384 get_user_ns(init_cgroup_ns
.user_ns
);
5386 mutex_lock(&cgroup_mutex
);
5388 /* Add init_css_set to the hash table */
5389 key
= css_set_hash(init_css_set
.subsys
);
5390 hash_add(css_set_table
, &init_css_set
.hlist
, key
);
5392 BUG_ON(cgroup_setup_root(&cgrp_dfl_root
, 0));
5394 mutex_unlock(&cgroup_mutex
);
5396 for_each_subsys(ss
, ssid
) {
5397 if (ss
->early_init
) {
5398 struct cgroup_subsys_state
*css
=
5399 init_css_set
.subsys
[ss
->id
];
5401 css
->id
= cgroup_idr_alloc(&ss
->css_idr
, css
, 1, 2,
5403 BUG_ON(css
->id
< 0);
5405 cgroup_init_subsys(ss
, false);
5408 list_add_tail(&init_css_set
.e_cset_node
[ssid
],
5409 &cgrp_dfl_root
.cgrp
.e_csets
[ssid
]);
5412 * Setting dfl_root subsys_mask needs to consider the
5413 * disabled flag and cftype registration needs kmalloc,
5414 * both of which aren't available during early_init.
5416 if (cgroup_disable_mask
& (1 << ssid
)) {
5417 static_branch_disable(cgroup_subsys_enabled_key
[ssid
]);
5418 printk(KERN_INFO
"Disabling %s control group subsystem\n",
5423 cgrp_dfl_root
.subsys_mask
|= 1 << ss
->id
;
5425 if (!ss
->dfl_cftypes
)
5426 cgrp_dfl_root_inhibit_ss_mask
|= 1 << ss
->id
;
5428 if (ss
->dfl_cftypes
== ss
->legacy_cftypes
) {
5429 WARN_ON(cgroup_add_cftypes(ss
, ss
->dfl_cftypes
));
5431 WARN_ON(cgroup_add_dfl_cftypes(ss
, ss
->dfl_cftypes
));
5432 WARN_ON(cgroup_add_legacy_cftypes(ss
, ss
->legacy_cftypes
));
5436 ss
->bind(init_css_set
.subsys
[ssid
]);
5439 WARN_ON(sysfs_create_mount_point(fs_kobj
, "cgroup"));
5440 WARN_ON(register_filesystem(&cgroup_fs_type
));
5441 WARN_ON(!proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
));
5446 static int __init
cgroup_wq_init(void)
5449 * There isn't much point in executing destruction path in
5450 * parallel. Good chunk is serialized with cgroup_mutex anyway.
5451 * Use 1 for @max_active.
5453 * We would prefer to do this in cgroup_init() above, but that
5454 * is called before init_workqueues(): so leave this until after.
5456 cgroup_destroy_wq
= alloc_workqueue("cgroup_destroy", 0, 1);
5457 BUG_ON(!cgroup_destroy_wq
);
5460 * Used to destroy pidlists and separate to serve as flush domain.
5461 * Cap @max_active to 1 too.
5463 cgroup_pidlist_destroy_wq
= alloc_workqueue("cgroup_pidlist_destroy",
5465 BUG_ON(!cgroup_pidlist_destroy_wq
);
5469 core_initcall(cgroup_wq_init
);
5472 * proc_cgroup_show()
5473 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5474 * - Used for /proc/<pid>/cgroup.
5476 int proc_cgroup_show(struct seq_file
*m
, struct pid_namespace
*ns
,
5477 struct pid
*pid
, struct task_struct
*tsk
)
5481 struct cgroup_root
*root
;
5484 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
5488 mutex_lock(&cgroup_mutex
);
5489 spin_lock_bh(&css_set_lock
);
5491 for_each_root(root
) {
5492 struct cgroup_subsys
*ss
;
5493 struct cgroup
*cgrp
;
5494 int ssid
, count
= 0;
5496 if (root
== &cgrp_dfl_root
&& !cgrp_dfl_root_visible
)
5499 seq_printf(m
, "%d:", root
->hierarchy_id
);
5500 if (root
!= &cgrp_dfl_root
)
5501 for_each_subsys(ss
, ssid
)
5502 if (root
->subsys_mask
& (1 << ssid
))
5503 seq_printf(m
, "%s%s", count
++ ? "," : "",
5505 if (strlen(root
->name
))
5506 seq_printf(m
, "%sname=%s", count
? "," : "",
5510 cgrp
= task_cgroup_from_root(tsk
, root
);
5513 * On traditional hierarchies, all zombie tasks show up as
5514 * belonging to the root cgroup. On the default hierarchy,
5515 * while a zombie doesn't show up in "cgroup.procs" and
5516 * thus can't be migrated, its /proc/PID/cgroup keeps
5517 * reporting the cgroup it belonged to before exiting. If
5518 * the cgroup is removed before the zombie is reaped,
5519 * " (deleted)" is appended to the cgroup path.
5521 if (cgroup_on_dfl(cgrp
) || !(tsk
->flags
& PF_EXITING
)) {
5522 path
= cgroup_path_ns_locked(cgrp
, buf
, PATH_MAX
,
5523 current
->nsproxy
->cgroup_ns
);
5525 retval
= -ENAMETOOLONG
;
5534 if (cgroup_on_dfl(cgrp
) && cgroup_is_dead(cgrp
))
5535 seq_puts(m
, " (deleted)\n");
5542 spin_unlock_bh(&css_set_lock
);
5543 mutex_unlock(&cgroup_mutex
);
5549 /* Display information about each subsystem and each hierarchy */
5550 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
5552 struct cgroup_subsys
*ss
;
5555 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
5557 * ideally we don't want subsystems moving around while we do this.
5558 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5559 * subsys/hierarchy state.
5561 mutex_lock(&cgroup_mutex
);
5563 for_each_subsys(ss
, i
)
5564 seq_printf(m
, "%s\t%d\t%d\t%d\n",
5565 ss
->legacy_name
, ss
->root
->hierarchy_id
,
5566 atomic_read(&ss
->root
->nr_cgrps
),
5567 cgroup_ssid_enabled(i
));
5569 mutex_unlock(&cgroup_mutex
);
5573 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
5575 return single_open(file
, proc_cgroupstats_show
, NULL
);
5578 static const struct file_operations proc_cgroupstats_operations
= {
5579 .open
= cgroupstats_open
,
5581 .llseek
= seq_lseek
,
5582 .release
= single_release
,
5585 static void **subsys_canfork_priv_p(void *ss_priv
[CGROUP_CANFORK_COUNT
], int i
)
5587 if (CGROUP_CANFORK_START
<= i
&& i
< CGROUP_CANFORK_END
)
5588 return &ss_priv
[i
- CGROUP_CANFORK_START
];
5592 static void *subsys_canfork_priv(void *ss_priv
[CGROUP_CANFORK_COUNT
], int i
)
5594 void **private = subsys_canfork_priv_p(ss_priv
, i
);
5595 return private ? *private : NULL
;
5599 * cgroup_fork - initialize cgroup related fields during copy_process()
5600 * @child: pointer to task_struct of forking parent process.
5602 * A task is associated with the init_css_set until cgroup_post_fork()
5603 * attaches it to the parent's css_set. Empty cg_list indicates that
5604 * @child isn't holding reference to its css_set.
5606 void cgroup_fork(struct task_struct
*child
)
5608 RCU_INIT_POINTER(child
->cgroups
, &init_css_set
);
5609 INIT_LIST_HEAD(&child
->cg_list
);
5613 * cgroup_can_fork - called on a new task before the process is exposed
5614 * @child: the task in question.
5616 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
5617 * returns an error, the fork aborts with that error code. This allows for
5618 * a cgroup subsystem to conditionally allow or deny new forks.
5620 int cgroup_can_fork(struct task_struct
*child
,
5621 void *ss_priv
[CGROUP_CANFORK_COUNT
])
5623 struct cgroup_subsys
*ss
;
5626 for_each_subsys_which(ss
, i
, &have_canfork_callback
) {
5627 ret
= ss
->can_fork(child
, subsys_canfork_priv_p(ss_priv
, i
));
5635 for_each_subsys(ss
, j
) {
5638 if (ss
->cancel_fork
)
5639 ss
->cancel_fork(child
, subsys_canfork_priv(ss_priv
, j
));
5646 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
5647 * @child: the task in question
5649 * This calls the cancel_fork() callbacks if a fork failed *after*
5650 * cgroup_can_fork() succeded.
5652 void cgroup_cancel_fork(struct task_struct
*child
,
5653 void *ss_priv
[CGROUP_CANFORK_COUNT
])
5655 struct cgroup_subsys
*ss
;
5658 for_each_subsys(ss
, i
)
5659 if (ss
->cancel_fork
)
5660 ss
->cancel_fork(child
, subsys_canfork_priv(ss_priv
, i
));
5664 * cgroup_post_fork - called on a new task after adding it to the task list
5665 * @child: the task in question
5667 * Adds the task to the list running through its css_set if necessary and
5668 * call the subsystem fork() callbacks. Has to be after the task is
5669 * visible on the task list in case we race with the first call to
5670 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5673 void cgroup_post_fork(struct task_struct
*child
,
5674 void *old_ss_priv
[CGROUP_CANFORK_COUNT
])
5676 struct cgroup_subsys
*ss
;
5680 * This may race against cgroup_enable_task_cg_lists(). As that
5681 * function sets use_task_css_set_links before grabbing
5682 * tasklist_lock and we just went through tasklist_lock to add
5683 * @child, it's guaranteed that either we see the set
5684 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
5685 * @child during its iteration.
5687 * If we won the race, @child is associated with %current's
5688 * css_set. Grabbing css_set_lock guarantees both that the
5689 * association is stable, and, on completion of the parent's
5690 * migration, @child is visible in the source of migration or
5691 * already in the destination cgroup. This guarantee is necessary
5692 * when implementing operations which need to migrate all tasks of
5693 * a cgroup to another.
5695 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
5696 * will remain in init_css_set. This is safe because all tasks are
5697 * in the init_css_set before cg_links is enabled and there's no
5698 * operation which transfers all tasks out of init_css_set.
5700 if (use_task_css_set_links
) {
5701 struct css_set
*cset
;
5703 spin_lock_bh(&css_set_lock
);
5704 cset
= task_css_set(current
);
5705 if (list_empty(&child
->cg_list
)) {
5707 css_set_move_task(child
, NULL
, cset
, false);
5709 spin_unlock_bh(&css_set_lock
);
5713 * Call ss->fork(). This must happen after @child is linked on
5714 * css_set; otherwise, @child might change state between ->fork()
5715 * and addition to css_set.
5717 for_each_subsys_which(ss
, i
, &have_fork_callback
)
5718 ss
->fork(child
, subsys_canfork_priv(old_ss_priv
, i
));
5722 * cgroup_exit - detach cgroup from exiting task
5723 * @tsk: pointer to task_struct of exiting process
5725 * Description: Detach cgroup from @tsk and release it.
5727 * Note that cgroups marked notify_on_release force every task in
5728 * them to take the global cgroup_mutex mutex when exiting.
5729 * This could impact scaling on very large systems. Be reluctant to
5730 * use notify_on_release cgroups where very high task exit scaling
5731 * is required on large systems.
5733 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
5734 * call cgroup_exit() while the task is still competent to handle
5735 * notify_on_release(), then leave the task attached to the root cgroup in
5736 * each hierarchy for the remainder of its exit. No need to bother with
5737 * init_css_set refcnting. init_css_set never goes away and we can't race
5738 * with migration path - PF_EXITING is visible to migration path.
5740 void cgroup_exit(struct task_struct
*tsk
)
5742 struct cgroup_subsys
*ss
;
5743 struct css_set
*cset
;
5747 * Unlink from @tsk from its css_set. As migration path can't race
5748 * with us, we can check css_set and cg_list without synchronization.
5750 cset
= task_css_set(tsk
);
5752 if (!list_empty(&tsk
->cg_list
)) {
5753 spin_lock_bh(&css_set_lock
);
5754 css_set_move_task(tsk
, cset
, NULL
, false);
5755 spin_unlock_bh(&css_set_lock
);
5760 /* see cgroup_post_fork() for details */
5761 for_each_subsys_which(ss
, i
, &have_exit_callback
)
5765 void cgroup_free(struct task_struct
*task
)
5767 struct css_set
*cset
= task_css_set(task
);
5768 struct cgroup_subsys
*ss
;
5771 for_each_subsys_which(ss
, ssid
, &have_free_callback
)
5777 static void check_for_release(struct cgroup
*cgrp
)
5779 if (notify_on_release(cgrp
) && !cgroup_is_populated(cgrp
) &&
5780 !css_has_online_children(&cgrp
->self
) && !cgroup_is_dead(cgrp
))
5781 schedule_work(&cgrp
->release_agent_work
);
5785 * Notify userspace when a cgroup is released, by running the
5786 * configured release agent with the name of the cgroup (path
5787 * relative to the root of cgroup file system) as the argument.
5789 * Most likely, this user command will try to rmdir this cgroup.
5791 * This races with the possibility that some other task will be
5792 * attached to this cgroup before it is removed, or that some other
5793 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5794 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5795 * unused, and this cgroup will be reprieved from its death sentence,
5796 * to continue to serve a useful existence. Next time it's released,
5797 * we will get notified again, if it still has 'notify_on_release' set.
5799 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5800 * means only wait until the task is successfully execve()'d. The
5801 * separate release agent task is forked by call_usermodehelper(),
5802 * then control in this thread returns here, without waiting for the
5803 * release agent task. We don't bother to wait because the caller of
5804 * this routine has no use for the exit status of the release agent
5805 * task, so no sense holding our caller up for that.
5807 static void cgroup_release_agent(struct work_struct
*work
)
5809 struct cgroup
*cgrp
=
5810 container_of(work
, struct cgroup
, release_agent_work
);
5811 char *pathbuf
= NULL
, *agentbuf
= NULL
, *path
;
5812 char *argv
[3], *envp
[3];
5814 mutex_lock(&cgroup_mutex
);
5816 pathbuf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
5817 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5818 if (!pathbuf
|| !agentbuf
)
5821 spin_lock_bh(&css_set_lock
);
5822 path
= cgroup_path_ns_locked(cgrp
, pathbuf
, PATH_MAX
,
5824 spin_unlock_bh(&css_set_lock
);
5832 /* minimal command environment */
5834 envp
[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5837 mutex_unlock(&cgroup_mutex
);
5838 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5841 mutex_unlock(&cgroup_mutex
);
5847 static int __init
cgroup_disable(char *str
)
5849 struct cgroup_subsys
*ss
;
5853 while ((token
= strsep(&str
, ",")) != NULL
) {
5857 for_each_subsys(ss
, i
) {
5858 if (strcmp(token
, ss
->name
) &&
5859 strcmp(token
, ss
->legacy_name
))
5861 cgroup_disable_mask
|= 1 << i
;
5866 __setup("cgroup_disable=", cgroup_disable
);
5869 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
5870 * @dentry: directory dentry of interest
5871 * @ss: subsystem of interest
5873 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5874 * to get the corresponding css and return it. If such css doesn't exist
5875 * or can't be pinned, an ERR_PTR value is returned.
5877 struct cgroup_subsys_state
*css_tryget_online_from_dir(struct dentry
*dentry
,
5878 struct cgroup_subsys
*ss
)
5880 struct kernfs_node
*kn
= kernfs_node_from_dentry(dentry
);
5881 struct cgroup_subsys_state
*css
= NULL
;
5882 struct cgroup
*cgrp
;
5884 /* is @dentry a cgroup dir? */
5885 if (dentry
->d_sb
->s_type
!= &cgroup_fs_type
|| !kn
||
5886 kernfs_type(kn
) != KERNFS_DIR
)
5887 return ERR_PTR(-EBADF
);
5892 * This path doesn't originate from kernfs and @kn could already
5893 * have been or be removed at any point. @kn->priv is RCU
5894 * protected for this access. See css_release_work_fn() for details.
5896 cgrp
= rcu_dereference(kn
->priv
);
5898 css
= cgroup_css(cgrp
, ss
);
5900 if (!css
|| !css_tryget_online(css
))
5901 css
= ERR_PTR(-ENOENT
);
5908 * css_from_id - lookup css by id
5909 * @id: the cgroup id
5910 * @ss: cgroup subsys to be looked into
5912 * Returns the css if there's valid one with @id, otherwise returns NULL.
5913 * Should be called under rcu_read_lock().
5915 struct cgroup_subsys_state
*css_from_id(int id
, struct cgroup_subsys
*ss
)
5917 WARN_ON_ONCE(!rcu_read_lock_held());
5918 return id
> 0 ? idr_find(&ss
->css_idr
, id
) : NULL
;
5921 /* cgroup namespaces */
5923 static struct cgroup_namespace
*alloc_cgroup_ns(void)
5925 struct cgroup_namespace
*new_ns
;
5928 new_ns
= kzalloc(sizeof(struct cgroup_namespace
), GFP_KERNEL
);
5930 return ERR_PTR(-ENOMEM
);
5931 ret
= ns_alloc_inum(&new_ns
->ns
);
5934 return ERR_PTR(ret
);
5936 atomic_set(&new_ns
->count
, 1);
5937 new_ns
->ns
.ops
= &cgroupns_operations
;
5941 void free_cgroup_ns(struct cgroup_namespace
*ns
)
5943 put_css_set(ns
->root_cset
);
5944 put_user_ns(ns
->user_ns
);
5945 ns_free_inum(&ns
->ns
);
5948 EXPORT_SYMBOL(free_cgroup_ns
);
5950 struct cgroup_namespace
*
5951 copy_cgroup_ns(unsigned long flags
, struct user_namespace
*user_ns
,
5952 struct cgroup_namespace
*old_ns
)
5954 struct cgroup_namespace
*new_ns
= NULL
;
5955 struct css_set
*cset
= NULL
;
5960 if (!(flags
& CLONE_NEWCGROUP
)) {
5961 get_cgroup_ns(old_ns
);
5965 /* Allow only sysadmin to create cgroup namespace. */
5967 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
5970 mutex_lock(&cgroup_mutex
);
5971 spin_lock_bh(&css_set_lock
);
5973 cset
= task_css_set(current
);
5976 spin_unlock_bh(&css_set_lock
);
5977 mutex_unlock(&cgroup_mutex
);
5980 new_ns
= alloc_cgroup_ns();
5984 new_ns
->user_ns
= get_user_ns(user_ns
);
5985 new_ns
->root_cset
= cset
;
5993 return ERR_PTR(err
);
5996 static inline struct cgroup_namespace
*to_cg_ns(struct ns_common
*ns
)
5998 return container_of(ns
, struct cgroup_namespace
, ns
);
6001 static int cgroupns_install(struct nsproxy
*nsproxy
, struct ns_common
*ns
)
6003 struct cgroup_namespace
*cgroup_ns
= to_cg_ns(ns
);
6005 if (!ns_capable(current_user_ns(), CAP_SYS_ADMIN
) ||
6006 !ns_capable(cgroup_ns
->user_ns
, CAP_SYS_ADMIN
))
6009 /* Don't need to do anything if we are attaching to our own cgroupns. */
6010 if (cgroup_ns
== nsproxy
->cgroup_ns
)
6013 get_cgroup_ns(cgroup_ns
);
6014 put_cgroup_ns(nsproxy
->cgroup_ns
);
6015 nsproxy
->cgroup_ns
= cgroup_ns
;
6020 static struct ns_common
*cgroupns_get(struct task_struct
*task
)
6022 struct cgroup_namespace
*ns
= NULL
;
6023 struct nsproxy
*nsproxy
;
6026 nsproxy
= task
->nsproxy
;
6028 ns
= nsproxy
->cgroup_ns
;
6033 return ns
? &ns
->ns
: NULL
;
6036 static void cgroupns_put(struct ns_common
*ns
)
6038 put_cgroup_ns(to_cg_ns(ns
));
6041 const struct proc_ns_operations cgroupns_operations
= {
6043 .type
= CLONE_NEWCGROUP
,
6044 .get
= cgroupns_get
,
6045 .put
= cgroupns_put
,
6046 .install
= cgroupns_install
,
6049 static __init
int cgroup_namespaces_init(void)
6053 subsys_initcall(cgroup_namespaces_init
);
6055 #ifdef CONFIG_CGROUP_DEBUG
6056 static struct cgroup_subsys_state
*
6057 debug_css_alloc(struct cgroup_subsys_state
*parent_css
)
6059 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
6062 return ERR_PTR(-ENOMEM
);
6067 static void debug_css_free(struct cgroup_subsys_state
*css
)
6072 static u64
debug_taskcount_read(struct cgroup_subsys_state
*css
,
6075 return cgroup_task_count(css
->cgroup
);
6078 static u64
current_css_set_read(struct cgroup_subsys_state
*css
,
6081 return (u64
)(unsigned long)current
->cgroups
;
6084 static u64
current_css_set_refcount_read(struct cgroup_subsys_state
*css
,
6090 count
= atomic_read(&task_css_set(current
)->refcount
);
6095 static int current_css_set_cg_links_read(struct seq_file
*seq
, void *v
)
6097 struct cgrp_cset_link
*link
;
6098 struct css_set
*cset
;
6101 name_buf
= kmalloc(NAME_MAX
+ 1, GFP_KERNEL
);
6105 spin_lock_bh(&css_set_lock
);
6107 cset
= rcu_dereference(current
->cgroups
);
6108 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
6109 struct cgroup
*c
= link
->cgrp
;
6111 cgroup_name(c
, name_buf
, NAME_MAX
+ 1);
6112 seq_printf(seq
, "Root %d group %s\n",
6113 c
->root
->hierarchy_id
, name_buf
);
6116 spin_unlock_bh(&css_set_lock
);
6121 #define MAX_TASKS_SHOWN_PER_CSS 25
6122 static int cgroup_css_links_read(struct seq_file
*seq
, void *v
)
6124 struct cgroup_subsys_state
*css
= seq_css(seq
);
6125 struct cgrp_cset_link
*link
;
6127 spin_lock_bh(&css_set_lock
);
6128 list_for_each_entry(link
, &css
->cgroup
->cset_links
, cset_link
) {
6129 struct css_set
*cset
= link
->cset
;
6130 struct task_struct
*task
;
6133 seq_printf(seq
, "css_set %p\n", cset
);
6135 list_for_each_entry(task
, &cset
->tasks
, cg_list
) {
6136 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
)
6138 seq_printf(seq
, " task %d\n", task_pid_vnr(task
));
6141 list_for_each_entry(task
, &cset
->mg_tasks
, cg_list
) {
6142 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
)
6144 seq_printf(seq
, " task %d\n", task_pid_vnr(task
));
6148 seq_puts(seq
, " ...\n");
6150 spin_unlock_bh(&css_set_lock
);
6154 static u64
releasable_read(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
6156 return (!cgroup_is_populated(css
->cgroup
) &&
6157 !css_has_online_children(&css
->cgroup
->self
));
6160 static struct cftype debug_files
[] = {
6162 .name
= "taskcount",
6163 .read_u64
= debug_taskcount_read
,
6167 .name
= "current_css_set",
6168 .read_u64
= current_css_set_read
,
6172 .name
= "current_css_set_refcount",
6173 .read_u64
= current_css_set_refcount_read
,
6177 .name
= "current_css_set_cg_links",
6178 .seq_show
= current_css_set_cg_links_read
,
6182 .name
= "cgroup_css_links",
6183 .seq_show
= cgroup_css_links_read
,
6187 .name
= "releasable",
6188 .read_u64
= releasable_read
,
6194 struct cgroup_subsys debug_cgrp_subsys
= {
6195 .css_alloc
= debug_css_alloc
,
6196 .css_free
= debug_css_free
,
6197 .legacy_cftypes
= debug_files
,
6199 #endif /* CONFIG_CGROUP_DEBUG */