2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
11 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include "cgroup-internal.h"
33 #include <linux/cred.h>
34 #include <linux/errno.h>
35 #include <linux/init_task.h>
36 #include <linux/kernel.h>
37 #include <linux/magic.h>
38 #include <linux/mutex.h>
39 #include <linux/mount.h>
40 #include <linux/pagemap.h>
41 #include <linux/proc_fs.h>
42 #include <linux/rcupdate.h>
43 #include <linux/sched.h>
44 #include <linux/sched/task.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/percpu-rwsem.h>
48 #include <linux/string.h>
49 #include <linux/hashtable.h>
50 #include <linux/idr.h>
51 #include <linux/kthread.h>
52 #include <linux/atomic.h>
53 #include <linux/cpuset.h>
54 #include <linux/proc_ns.h>
55 #include <linux/nsproxy.h>
56 #include <linux/file.h>
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/cgroup.h>
62 #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
66 * cgroup_mutex is the master lock. Any modification to cgroup or its
67 * hierarchy must be performed while holding it.
69 * css_set_lock protects task->cgroups pointer, the list of css_set
70 * objects, and the chain of tasks off each css_set.
72 * These locks are exported if CONFIG_PROVE_RCU so that accessors in
73 * cgroup.h can use them for lockdep annotations.
75 DEFINE_MUTEX(cgroup_mutex);
76 DEFINE_SPINLOCK(css_set_lock);
78 #ifdef CONFIG_PROVE_RCU
79 EXPORT_SYMBOL_GPL(cgroup_mutex);
80 EXPORT_SYMBOL_GPL(css_set_lock);
84 * Protects cgroup_idr and css_idr so that IDs can be released without
85 * grabbing cgroup_mutex.
87 static DEFINE_SPINLOCK(cgroup_idr_lock);
90 * Protects cgroup_file->kn for !self csses. It synchronizes notifications
91 * against file removal/re-creation across css hiding.
93 static DEFINE_SPINLOCK(cgroup_file_kn_lock);
95 struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
97 #define cgroup_assert_mutex_or_rcu_locked() \
98 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
99 !lockdep_is_held(&cgroup_mutex), \
100 "cgroup_mutex or RCU read lock required");
103 * cgroup destruction makes heavy use of work items and there can be a lot
104 * of concurrent destructions. Use a separate workqueue so that cgroup
105 * destruction work items don't end up filling up max_active of system_wq
106 * which may lead to deadlock.
108 static struct workqueue_struct *cgroup_destroy_wq;
110 /* generate an array of cgroup subsystem pointers */
111 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
112 struct cgroup_subsys *cgroup_subsys[] = {
113 #include <linux/cgroup_subsys.h>
117 /* array of cgroup subsystem names */
118 #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
119 static const char *cgroup_subsys_name[] = {
120 #include <linux/cgroup_subsys.h>
124 /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
126 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
127 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
128 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
129 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
130 #include <linux/cgroup_subsys.h>
133 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
134 static struct static_key_true *cgroup_subsys_enabled_key[] = {
135 #include <linux/cgroup_subsys.h>
139 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
140 static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
141 #include <linux/cgroup_subsys.h>
146 * The default hierarchy, reserved for the subsystems that are otherwise
147 * unattached - it never has more than a single cgroup, and all tasks are
148 * part of that cgroup.
150 struct cgroup_root cgrp_dfl_root;
151 EXPORT_SYMBOL_GPL(cgrp_dfl_root);
154 * The default hierarchy always exists but is hidden until mounted for the
155 * first time. This is for backward compatibility.
157 static bool cgrp_dfl_visible;
159 /* some controllers are not supported in the default hierarchy */
160 static u16 cgrp_dfl_inhibit_ss_mask;
162 /* some controllers are implicitly enabled on the default hierarchy */
163 static u16 cgrp_dfl_implicit_ss_mask;
165 /* The list of hierarchy roots */
166 LIST_HEAD(cgroup_roots);
167 static int cgroup_root_count;
169 /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
170 static DEFINE_IDR(cgroup_hierarchy_idr);
173 * Assign a monotonically increasing serial number to csses. It guarantees
174 * cgroups with bigger numbers are newer than those with smaller numbers.
175 * Also, as csses are always appended to the parent's ->children list, it
176 * guarantees that sibling csses are always sorted in the ascending serial
177 * number order on the list. Protected by cgroup_mutex.
179 static u64 css_serial_nr_next = 1;
182 * These bitmasks identify subsystems with specific features to avoid
183 * having to do iterative checks repeatedly.
185 static u16 have_fork_callback __read_mostly;
186 static u16 have_exit_callback __read_mostly;
187 static u16 have_free_callback __read_mostly;
188 static u16 have_canfork_callback __read_mostly;
190 /* cgroup namespace for init task */
191 struct cgroup_namespace init_cgroup_ns = {
192 .count = { .counter = 2, },
193 .user_ns = &init_user_ns,
194 .ns.ops = &cgroupns_operations,
195 .ns.inum = PROC_CGROUP_INIT_INO,
196 .root_cset = &init_css_set,
199 static struct file_system_type cgroup2_fs_type;
200 static struct cftype cgroup_base_files[];
202 static int cgroup_apply_control(struct cgroup *cgrp);
203 static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
204 static void css_task_iter_advance(struct css_task_iter *it);
205 static int cgroup_destroy_locked(struct cgroup *cgrp);
206 static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
207 struct cgroup_subsys *ss);
208 static void css_release(struct percpu_ref *ref);
209 static void kill_css(struct cgroup_subsys_state *css);
210 static int cgroup_addrm_files(struct cgroup_subsys_state *css,
211 struct cgroup *cgrp, struct cftype cfts[],
215 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
216 * @ssid: subsys ID of interest
218 * cgroup_subsys_enabled() can only be used with literal subsys names which
219 * is fine for individual subsystems but unsuitable for cgroup core. This
220 * is slower static_key_enabled() based test indexed by @ssid.
222 bool cgroup_ssid_enabled(int ssid)
224 if (CGROUP_SUBSYS_COUNT == 0)
227 return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
231 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
232 * @cgrp: the cgroup of interest
234 * The default hierarchy is the v2 interface of cgroup and this function
235 * can be used to test whether a cgroup is on the default hierarchy for
236 * cases where a subsystem should behave differnetly depending on the
239 * The set of behaviors which change on the default hierarchy are still
240 * being determined and the mount option is prefixed with __DEVEL__.
242 * List of changed behaviors:
244 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
245 * and "name" are disallowed.
247 * - When mounting an existing superblock, mount options should match.
249 * - Remount is disallowed.
251 * - rename(2) is disallowed.
253 * - "tasks" is removed. Everything should be at process granularity. Use
254 * "cgroup.procs" instead.
256 * - "cgroup.procs" is not sorted. pids will be unique unless they got
257 * recycled inbetween reads.
259 * - "release_agent" and "notify_on_release" are removed. Replacement
260 * notification mechanism will be implemented.
262 * - "cgroup.clone_children" is removed.
264 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
265 * and its descendants contain no task; otherwise, 1. The file also
266 * generates kernfs notification which can be monitored through poll and
267 * [di]notify when the value of the file changes.
269 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
270 * take masks of ancestors with non-empty cpus/mems, instead of being
271 * moved to an ancestor.
273 * - cpuset: a task can be moved into an empty cpuset, and again it takes
274 * masks of ancestors.
276 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
279 * - blkcg: blk-throttle becomes properly hierarchical.
281 * - debug: disallowed on the default hierarchy.
283 bool cgroup_on_dfl(const struct cgroup *cgrp)
285 return cgrp->root == &cgrp_dfl_root;
288 /* IDR wrappers which synchronize using cgroup_idr_lock */
289 static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
294 idr_preload(gfp_mask);
295 spin_lock_bh(&cgroup_idr_lock);
296 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
297 spin_unlock_bh(&cgroup_idr_lock);
302 static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
306 spin_lock_bh(&cgroup_idr_lock);
307 ret = idr_replace(idr, ptr, id);
308 spin_unlock_bh(&cgroup_idr_lock);
312 static void cgroup_idr_remove(struct idr *idr, int id)
314 spin_lock_bh(&cgroup_idr_lock);
316 spin_unlock_bh(&cgroup_idr_lock);
319 static struct cgroup *cgroup_parent(struct cgroup *cgrp)
321 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
324 return container_of(parent_css, struct cgroup, self);
328 /* subsystems visibly enabled on a cgroup */
329 static u16 cgroup_control(struct cgroup *cgrp)
331 struct cgroup *parent = cgroup_parent(cgrp);
332 u16 root_ss_mask = cgrp->root->subsys_mask;
335 return parent->subtree_control;
337 if (cgroup_on_dfl(cgrp))
338 root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
339 cgrp_dfl_implicit_ss_mask);
343 /* subsystems enabled on a cgroup */
344 static u16 cgroup_ss_mask(struct cgroup *cgrp)
346 struct cgroup *parent = cgroup_parent(cgrp);
349 return parent->subtree_ss_mask;
351 return cgrp->root->subsys_mask;
355 * cgroup_css - obtain a cgroup's css for the specified subsystem
356 * @cgrp: the cgroup of interest
357 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
359 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
360 * function must be called either under cgroup_mutex or rcu_read_lock() and
361 * the caller is responsible for pinning the returned css if it wants to
362 * keep accessing it outside the said locks. This function may return
363 * %NULL if @cgrp doesn't have @subsys_id enabled.
365 static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
366 struct cgroup_subsys *ss)
369 return rcu_dereference_check(cgrp->subsys[ss->id],
370 lockdep_is_held(&cgroup_mutex));
376 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
377 * @cgrp: the cgroup of interest
378 * @ss: the subsystem of interest (%NULL returns @cgrp->self)
380 * Similar to cgroup_css() but returns the effective css, which is defined
381 * as the matching css of the nearest ancestor including self which has @ss
382 * enabled. If @ss is associated with the hierarchy @cgrp is on, this
383 * function is guaranteed to return non-NULL css.
385 static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
386 struct cgroup_subsys *ss)
388 lockdep_assert_held(&cgroup_mutex);
394 * This function is used while updating css associations and thus
395 * can't test the csses directly. Test ss_mask.
397 while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
398 cgrp = cgroup_parent(cgrp);
403 return cgroup_css(cgrp, ss);
407 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
408 * @cgrp: the cgroup of interest
409 * @ss: the subsystem of interest
411 * Find and get the effective css of @cgrp for @ss. The effective css is
412 * defined as the matching css of the nearest ancestor including self which
413 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
414 * the root css is returned, so this function always returns a valid css.
415 * The returned css must be put using css_put().
417 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
418 struct cgroup_subsys *ss)
420 struct cgroup_subsys_state *css;
425 css = cgroup_css(cgrp, ss);
427 if (css && css_tryget_online(css))
429 cgrp = cgroup_parent(cgrp);
432 css = init_css_set.subsys[ss->id];
439 static void cgroup_get(struct cgroup *cgrp)
441 WARN_ON_ONCE(cgroup_is_dead(cgrp));
442 css_get(&cgrp->self);
445 static bool cgroup_tryget(struct cgroup *cgrp)
447 return css_tryget(&cgrp->self);
450 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
452 struct cgroup *cgrp = of->kn->parent->priv;
453 struct cftype *cft = of_cft(of);
456 * This is open and unprotected implementation of cgroup_css().
457 * seq_css() is only called from a kernfs file operation which has
458 * an active reference on the file. Because all the subsystem
459 * files are drained before a css is disassociated with a cgroup,
460 * the matching css from the cgroup's subsys table is guaranteed to
461 * be and stay valid until the enclosing operation is complete.
464 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
468 EXPORT_SYMBOL_GPL(of_css);
471 * for_each_css - iterate all css's of a cgroup
472 * @css: the iteration cursor
473 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
474 * @cgrp: the target cgroup to iterate css's of
476 * Should be called under cgroup_[tree_]mutex.
478 #define for_each_css(css, ssid, cgrp) \
479 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
480 if (!((css) = rcu_dereference_check( \
481 (cgrp)->subsys[(ssid)], \
482 lockdep_is_held(&cgroup_mutex)))) { } \
486 * for_each_e_css - iterate all effective css's of a cgroup
487 * @css: the iteration cursor
488 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
489 * @cgrp: the target cgroup to iterate css's of
491 * Should be called under cgroup_[tree_]mutex.
493 #define for_each_e_css(css, ssid, cgrp) \
494 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
495 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
500 * do_each_subsys_mask - filter for_each_subsys with a bitmask
501 * @ss: the iteration cursor
502 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
503 * @ss_mask: the bitmask
505 * The block will only run for cases where the ssid-th bit (1 << ssid) of
508 #define do_each_subsys_mask(ss, ssid, ss_mask) do { \
509 unsigned long __ss_mask = (ss_mask); \
510 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
514 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
515 (ss) = cgroup_subsys[ssid]; \
518 #define while_each_subsys_mask() \
523 /* iterate over child cgrps, lock should be held throughout iteration */
524 #define cgroup_for_each_live_child(child, cgrp) \
525 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
526 if (({ lockdep_assert_held(&cgroup_mutex); \
527 cgroup_is_dead(child); })) \
531 /* walk live descendants in preorder */
532 #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
533 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
534 if (({ lockdep_assert_held(&cgroup_mutex); \
535 (dsct) = (d_css)->cgroup; \
536 cgroup_is_dead(dsct); })) \
540 /* walk live descendants in postorder */
541 #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
542 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
543 if (({ lockdep_assert_held(&cgroup_mutex); \
544 (dsct) = (d_css)->cgroup; \
545 cgroup_is_dead(dsct); })) \
550 * The default css_set - used by init and its children prior to any
551 * hierarchies being mounted. It contains a pointer to the root state
552 * for each subsystem. Also used to anchor the list of css_sets. Not
553 * reference-counted, to improve performance when child cgroups
554 * haven't been created.
556 struct css_set init_css_set = {
557 .refcount = ATOMIC_INIT(1),
558 .tasks = LIST_HEAD_INIT(init_css_set.tasks),
559 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
560 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
561 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
562 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
563 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
566 static int css_set_count = 1; /* 1 for init_css_set */
569 * css_set_populated - does a css_set contain any tasks?
570 * @cset: target css_set
572 static bool css_set_populated(struct css_set *cset)
574 lockdep_assert_held(&css_set_lock);
576 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
580 * cgroup_update_populated - updated populated count of a cgroup
581 * @cgrp: the target cgroup
582 * @populated: inc or dec populated count
584 * One of the css_sets associated with @cgrp is either getting its first
585 * task or losing the last. Update @cgrp->populated_cnt accordingly. The
586 * count is propagated towards root so that a given cgroup's populated_cnt
587 * is zero iff the cgroup and all its descendants don't contain any tasks.
589 * @cgrp's interface file "cgroup.populated" is zero if
590 * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
591 * changes from or to zero, userland is notified that the content of the
592 * interface file has changed. This can be used to detect when @cgrp and
593 * its descendants become populated or empty.
595 static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
597 lockdep_assert_held(&css_set_lock);
603 trigger = !cgrp->populated_cnt++;
605 trigger = !--cgrp->populated_cnt;
610 cgroup1_check_for_release(cgrp);
611 cgroup_file_notify(&cgrp->events_file);
613 cgrp = cgroup_parent(cgrp);
618 * css_set_update_populated - update populated state of a css_set
619 * @cset: target css_set
620 * @populated: whether @cset is populated or depopulated
622 * @cset is either getting the first task or losing the last. Update the
623 * ->populated_cnt of all associated cgroups accordingly.
625 static void css_set_update_populated(struct css_set *cset, bool populated)
627 struct cgrp_cset_link *link;
629 lockdep_assert_held(&css_set_lock);
631 list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
632 cgroup_update_populated(link->cgrp, populated);
636 * css_set_move_task - move a task from one css_set to another
637 * @task: task being moved
638 * @from_cset: css_set @task currently belongs to (may be NULL)
639 * @to_cset: new css_set @task is being moved to (may be NULL)
640 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
642 * Move @task from @from_cset to @to_cset. If @task didn't belong to any
643 * css_set, @from_cset can be NULL. If @task is being disassociated
644 * instead of moved, @to_cset can be NULL.
646 * This function automatically handles populated_cnt updates and
647 * css_task_iter adjustments but the caller is responsible for managing
648 * @from_cset and @to_cset's reference counts.
650 static void css_set_move_task(struct task_struct *task,
651 struct css_set *from_cset, struct css_set *to_cset,
654 lockdep_assert_held(&css_set_lock);
656 if (to_cset && !css_set_populated(to_cset))
657 css_set_update_populated(to_cset, true);
660 struct css_task_iter *it, *pos;
662 WARN_ON_ONCE(list_empty(&task->cg_list));
665 * @task is leaving, advance task iterators which are
666 * pointing to it so that they can resume at the next
667 * position. Advancing an iterator might remove it from
668 * the list, use safe walk. See css_task_iter_advance*()
671 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
673 if (it->task_pos == &task->cg_list)
674 css_task_iter_advance(it);
676 list_del_init(&task->cg_list);
677 if (!css_set_populated(from_cset))
678 css_set_update_populated(from_cset, false);
680 WARN_ON_ONCE(!list_empty(&task->cg_list));
685 * We are synchronized through cgroup_threadgroup_rwsem
686 * against PF_EXITING setting such that we can't race
687 * against cgroup_exit() changing the css_set to
688 * init_css_set and dropping the old one.
690 WARN_ON_ONCE(task->flags & PF_EXITING);
692 rcu_assign_pointer(task->cgroups, to_cset);
693 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
699 * hash table for cgroup groups. This improves the performance to find
700 * an existing css_set. This hash doesn't (currently) take into
701 * account cgroups in empty hierarchies.
703 #define CSS_SET_HASH_BITS 7
704 static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
706 static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
708 unsigned long key = 0UL;
709 struct cgroup_subsys *ss;
712 for_each_subsys(ss, i)
713 key += (unsigned long)css[i];
714 key = (key >> 16) ^ key;
719 void put_css_set_locked(struct css_set *cset)
721 struct cgrp_cset_link *link, *tmp_link;
722 struct cgroup_subsys *ss;
725 lockdep_assert_held(&css_set_lock);
727 if (!atomic_dec_and_test(&cset->refcount))
730 /* This css_set is dead. unlink it and release cgroup and css refs */
731 for_each_subsys(ss, ssid) {
732 list_del(&cset->e_cset_node[ssid]);
733 css_put(cset->subsys[ssid]);
735 hash_del(&cset->hlist);
738 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
739 list_del(&link->cset_link);
740 list_del(&link->cgrp_link);
741 if (cgroup_parent(link->cgrp))
742 cgroup_put(link->cgrp);
746 kfree_rcu(cset, rcu_head);
750 * compare_css_sets - helper function for find_existing_css_set().
751 * @cset: candidate css_set being tested
752 * @old_cset: existing css_set for a task
753 * @new_cgrp: cgroup that's being entered by the task
754 * @template: desired set of css pointers in css_set (pre-calculated)
756 * Returns true if "cset" matches "old_cset" except for the hierarchy
757 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
759 static bool compare_css_sets(struct css_set *cset,
760 struct css_set *old_cset,
761 struct cgroup *new_cgrp,
762 struct cgroup_subsys_state *template[])
764 struct list_head *l1, *l2;
767 * On the default hierarchy, there can be csets which are
768 * associated with the same set of cgroups but different csses.
769 * Let's first ensure that csses match.
771 if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
775 * Compare cgroup pointers in order to distinguish between
776 * different cgroups in hierarchies. As different cgroups may
777 * share the same effective css, this comparison is always
780 l1 = &cset->cgrp_links;
781 l2 = &old_cset->cgrp_links;
783 struct cgrp_cset_link *link1, *link2;
784 struct cgroup *cgrp1, *cgrp2;
788 /* See if we reached the end - both lists are equal length. */
789 if (l1 == &cset->cgrp_links) {
790 BUG_ON(l2 != &old_cset->cgrp_links);
793 BUG_ON(l2 == &old_cset->cgrp_links);
795 /* Locate the cgroups associated with these links. */
796 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
797 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
800 /* Hierarchies should be linked in the same order. */
801 BUG_ON(cgrp1->root != cgrp2->root);
804 * If this hierarchy is the hierarchy of the cgroup
805 * that's changing, then we need to check that this
806 * css_set points to the new cgroup; if it's any other
807 * hierarchy, then this css_set should point to the
808 * same cgroup as the old css_set.
810 if (cgrp1->root == new_cgrp->root) {
811 if (cgrp1 != new_cgrp)
822 * find_existing_css_set - init css array and find the matching css_set
823 * @old_cset: the css_set that we're using before the cgroup transition
824 * @cgrp: the cgroup that we're moving into
825 * @template: out param for the new set of csses, should be clear on entry
827 static struct css_set *find_existing_css_set(struct css_set *old_cset,
829 struct cgroup_subsys_state *template[])
831 struct cgroup_root *root = cgrp->root;
832 struct cgroup_subsys *ss;
833 struct css_set *cset;
838 * Build the set of subsystem state objects that we want to see in the
839 * new css_set. while subsystems can change globally, the entries here
840 * won't change, so no need for locking.
842 for_each_subsys(ss, i) {
843 if (root->subsys_mask & (1UL << i)) {
845 * @ss is in this hierarchy, so we want the
846 * effective css from @cgrp.
848 template[i] = cgroup_e_css(cgrp, ss);
851 * @ss is not in this hierarchy, so we don't want
854 template[i] = old_cset->subsys[i];
858 key = css_set_hash(template);
859 hash_for_each_possible(css_set_table, cset, hlist, key) {
860 if (!compare_css_sets(cset, old_cset, cgrp, template))
863 /* This css_set matches what we need */
867 /* No existing cgroup group matched */
871 static void free_cgrp_cset_links(struct list_head *links_to_free)
873 struct cgrp_cset_link *link, *tmp_link;
875 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
876 list_del(&link->cset_link);
882 * allocate_cgrp_cset_links - allocate cgrp_cset_links
883 * @count: the number of links to allocate
884 * @tmp_links: list_head the allocated links are put on
886 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
887 * through ->cset_link. Returns 0 on success or -errno.
889 static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
891 struct cgrp_cset_link *link;
894 INIT_LIST_HEAD(tmp_links);
896 for (i = 0; i < count; i++) {
897 link = kzalloc(sizeof(*link), GFP_KERNEL);
899 free_cgrp_cset_links(tmp_links);
902 list_add(&link->cset_link, tmp_links);
908 * link_css_set - a helper function to link a css_set to a cgroup
909 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
910 * @cset: the css_set to be linked
911 * @cgrp: the destination cgroup
913 static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
916 struct cgrp_cset_link *link;
918 BUG_ON(list_empty(tmp_links));
920 if (cgroup_on_dfl(cgrp))
921 cset->dfl_cgrp = cgrp;
923 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
928 * Always add links to the tail of the lists so that the lists are
929 * in choronological order.
931 list_move_tail(&link->cset_link, &cgrp->cset_links);
932 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
934 if (cgroup_parent(cgrp))
939 * find_css_set - return a new css_set with one cgroup updated
940 * @old_cset: the baseline css_set
941 * @cgrp: the cgroup to be updated
943 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
944 * substituted into the appropriate hierarchy.
946 static struct css_set *find_css_set(struct css_set *old_cset,
949 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
950 struct css_set *cset;
951 struct list_head tmp_links;
952 struct cgrp_cset_link *link;
953 struct cgroup_subsys *ss;
957 lockdep_assert_held(&cgroup_mutex);
959 /* First see if we already have a cgroup group that matches
961 spin_lock_irq(&css_set_lock);
962 cset = find_existing_css_set(old_cset, cgrp, template);
965 spin_unlock_irq(&css_set_lock);
970 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
974 /* Allocate all the cgrp_cset_link objects that we'll need */
975 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
980 atomic_set(&cset->refcount, 1);
981 INIT_LIST_HEAD(&cset->tasks);
982 INIT_LIST_HEAD(&cset->mg_tasks);
983 INIT_LIST_HEAD(&cset->task_iters);
984 INIT_HLIST_NODE(&cset->hlist);
985 INIT_LIST_HEAD(&cset->cgrp_links);
986 INIT_LIST_HEAD(&cset->mg_preload_node);
987 INIT_LIST_HEAD(&cset->mg_node);
989 /* Copy the set of subsystem state objects generated in
990 * find_existing_css_set() */
991 memcpy(cset->subsys, template, sizeof(cset->subsys));
993 spin_lock_irq(&css_set_lock);
994 /* Add reference counts and links from the new css_set. */
995 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
996 struct cgroup *c = link->cgrp;
998 if (c->root == cgrp->root)
1000 link_css_set(&tmp_links, cset, c);
1003 BUG_ON(!list_empty(&tmp_links));
1007 /* Add @cset to the hash table */
1008 key = css_set_hash(cset->subsys);
1009 hash_add(css_set_table, &cset->hlist, key);
1011 for_each_subsys(ss, ssid) {
1012 struct cgroup_subsys_state *css = cset->subsys[ssid];
1014 list_add_tail(&cset->e_cset_node[ssid],
1015 &css->cgroup->e_csets[ssid]);
1019 spin_unlock_irq(&css_set_lock);
1024 struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
1026 struct cgroup *root_cgrp = kf_root->kn->priv;
1028 return root_cgrp->root;
1031 static int cgroup_init_root_id(struct cgroup_root *root)
1035 lockdep_assert_held(&cgroup_mutex);
1037 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
1041 root->hierarchy_id = id;
1045 static void cgroup_exit_root_id(struct cgroup_root *root)
1047 lockdep_assert_held(&cgroup_mutex);
1049 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1052 void cgroup_free_root(struct cgroup_root *root)
1055 idr_destroy(&root->cgroup_idr);
1060 static void cgroup_destroy_root(struct cgroup_root *root)
1062 struct cgroup *cgrp = &root->cgrp;
1063 struct cgrp_cset_link *link, *tmp_link;
1065 trace_cgroup_destroy_root(root);
1067 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
1069 BUG_ON(atomic_read(&root->nr_cgrps));
1070 BUG_ON(!list_empty(&cgrp->self.children));
1072 /* Rebind all subsystems back to the default hierarchy */
1073 WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
1076 * Release all the links from cset_links to this hierarchy's
1079 spin_lock_irq(&css_set_lock);
1081 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1082 list_del(&link->cset_link);
1083 list_del(&link->cgrp_link);
1087 spin_unlock_irq(&css_set_lock);
1089 if (!list_empty(&root->root_list)) {
1090 list_del(&root->root_list);
1091 cgroup_root_count--;
1094 cgroup_exit_root_id(root);
1096 mutex_unlock(&cgroup_mutex);
1098 kernfs_destroy_root(root->kf_root);
1099 cgroup_free_root(root);
1103 * look up cgroup associated with current task's cgroup namespace on the
1104 * specified hierarchy
1106 static struct cgroup *
1107 current_cgns_cgroup_from_root(struct cgroup_root *root)
1109 struct cgroup *res = NULL;
1110 struct css_set *cset;
1112 lockdep_assert_held(&css_set_lock);
1116 cset = current->nsproxy->cgroup_ns->root_cset;
1117 if (cset == &init_css_set) {
1120 struct cgrp_cset_link *link;
1122 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1123 struct cgroup *c = link->cgrp;
1125 if (c->root == root) {
1137 /* look up cgroup associated with given css_set on the specified hierarchy */
1138 static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
1139 struct cgroup_root *root)
1141 struct cgroup *res = NULL;
1143 lockdep_assert_held(&cgroup_mutex);
1144 lockdep_assert_held(&css_set_lock);
1146 if (cset == &init_css_set) {
1149 struct cgrp_cset_link *link;
1151 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1152 struct cgroup *c = link->cgrp;
1154 if (c->root == root) {
1166 * Return the cgroup for "task" from the given hierarchy. Must be
1167 * called with cgroup_mutex and css_set_lock held.
1169 struct cgroup *task_cgroup_from_root(struct task_struct *task,
1170 struct cgroup_root *root)
1173 * No need to lock the task - since we hold cgroup_mutex the
1174 * task can't change groups, so the only thing that can happen
1175 * is that it exits and its css is set back to init_css_set.
1177 return cset_cgroup_from_root(task_css_set(task), root);
1181 * A task must hold cgroup_mutex to modify cgroups.
1183 * Any task can increment and decrement the count field without lock.
1184 * So in general, code holding cgroup_mutex can't rely on the count
1185 * field not changing. However, if the count goes to zero, then only
1186 * cgroup_attach_task() can increment it again. Because a count of zero
1187 * means that no tasks are currently attached, therefore there is no
1188 * way a task attached to that cgroup can fork (the other way to
1189 * increment the count). So code holding cgroup_mutex can safely
1190 * assume that if the count is zero, it will stay zero. Similarly, if
1191 * a task holds cgroup_mutex on a cgroup with zero count, it
1192 * knows that the cgroup won't be removed, as cgroup_rmdir()
1195 * A cgroup can only be deleted if both its 'count' of using tasks
1196 * is zero, and its list of 'children' cgroups is empty. Since all
1197 * tasks in the system use _some_ cgroup, and since there is always at
1198 * least one task in the system (init, pid == 1), therefore, root cgroup
1199 * always has either children cgroups and/or using tasks. So we don't
1200 * need a special hack to ensure that root cgroup cannot be deleted.
1202 * P.S. One more locking exception. RCU is used to guard the
1203 * update of a tasks cgroup pointer by cgroup_attach_task()
1206 static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
1208 static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1211 struct cgroup_subsys *ss = cft->ss;
1213 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1214 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1215 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
1216 cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1219 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1224 * cgroup_file_mode - deduce file mode of a control file
1225 * @cft: the control file in question
1227 * S_IRUGO for read, S_IWUSR for write.
1229 static umode_t cgroup_file_mode(const struct cftype *cft)
1233 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1236 if (cft->write_u64 || cft->write_s64 || cft->write) {
1237 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1247 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
1248 * @subtree_control: the new subtree_control mask to consider
1249 * @this_ss_mask: available subsystems
1251 * On the default hierarchy, a subsystem may request other subsystems to be
1252 * enabled together through its ->depends_on mask. In such cases, more
1253 * subsystems than specified in "cgroup.subtree_control" may be enabled.
1255 * This function calculates which subsystems need to be enabled if
1256 * @subtree_control is to be applied while restricted to @this_ss_mask.
1258 static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
1260 u16 cur_ss_mask = subtree_control;
1261 struct cgroup_subsys *ss;
1264 lockdep_assert_held(&cgroup_mutex);
1266 cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
1269 u16 new_ss_mask = cur_ss_mask;
1271 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
1272 new_ss_mask |= ss->depends_on;
1273 } while_each_subsys_mask();
1276 * Mask out subsystems which aren't available. This can
1277 * happen only if some depended-upon subsystems were bound
1278 * to non-default hierarchies.
1280 new_ss_mask &= this_ss_mask;
1282 if (new_ss_mask == cur_ss_mask)
1284 cur_ss_mask = new_ss_mask;
1291 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1292 * @kn: the kernfs_node being serviced
1294 * This helper undoes cgroup_kn_lock_live() and should be invoked before
1295 * the method finishes if locking succeeded. Note that once this function
1296 * returns the cgroup returned by cgroup_kn_lock_live() may become
1297 * inaccessible any time. If the caller intends to continue to access the
1298 * cgroup, it should pin it before invoking this function.
1300 void cgroup_kn_unlock(struct kernfs_node *kn)
1302 struct cgroup *cgrp;
1304 if (kernfs_type(kn) == KERNFS_DIR)
1307 cgrp = kn->parent->priv;
1309 mutex_unlock(&cgroup_mutex);
1311 kernfs_unbreak_active_protection(kn);
1316 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1317 * @kn: the kernfs_node being serviced
1318 * @drain_offline: perform offline draining on the cgroup
1320 * This helper is to be used by a cgroup kernfs method currently servicing
1321 * @kn. It breaks the active protection, performs cgroup locking and
1322 * verifies that the associated cgroup is alive. Returns the cgroup if
1323 * alive; otherwise, %NULL. A successful return should be undone by a
1324 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
1325 * cgroup is drained of offlining csses before return.
1327 * Any cgroup kernfs method implementation which requires locking the
1328 * associated cgroup should use this helper. It avoids nesting cgroup
1329 * locking under kernfs active protection and allows all kernfs operations
1330 * including self-removal.
1332 struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
1334 struct cgroup *cgrp;
1336 if (kernfs_type(kn) == KERNFS_DIR)
1339 cgrp = kn->parent->priv;
1342 * We're gonna grab cgroup_mutex which nests outside kernfs
1343 * active_ref. cgroup liveliness check alone provides enough
1344 * protection against removal. Ensure @cgrp stays accessible and
1345 * break the active_ref protection.
1347 if (!cgroup_tryget(cgrp))
1349 kernfs_break_active_protection(kn);
1352 cgroup_lock_and_drain_offline(cgrp);
1354 mutex_lock(&cgroup_mutex);
1356 if (!cgroup_is_dead(cgrp))
1359 cgroup_kn_unlock(kn);
1363 static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
1365 char name[CGROUP_FILE_NAME_MAX];
1367 lockdep_assert_held(&cgroup_mutex);
1369 if (cft->file_offset) {
1370 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1371 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1373 spin_lock_irq(&cgroup_file_kn_lock);
1375 spin_unlock_irq(&cgroup_file_kn_lock);
1378 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
1382 * css_clear_dir - remove subsys files in a cgroup directory
1385 static void css_clear_dir(struct cgroup_subsys_state *css)
1387 struct cgroup *cgrp = css->cgroup;
1388 struct cftype *cfts;
1390 if (!(css->flags & CSS_VISIBLE))
1393 css->flags &= ~CSS_VISIBLE;
1395 list_for_each_entry(cfts, &css->ss->cfts, node)
1396 cgroup_addrm_files(css, cgrp, cfts, false);
1400 * css_populate_dir - create subsys files in a cgroup directory
1403 * On failure, no file is added.
1405 static int css_populate_dir(struct cgroup_subsys_state *css)
1407 struct cgroup *cgrp = css->cgroup;
1408 struct cftype *cfts, *failed_cfts;
1411 if ((css->flags & CSS_VISIBLE) || !cgrp->kn)
1415 if (cgroup_on_dfl(cgrp))
1416 cfts = cgroup_base_files;
1418 cfts = cgroup1_base_files;
1420 return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1423 list_for_each_entry(cfts, &css->ss->cfts, node) {
1424 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1431 css->flags |= CSS_VISIBLE;
1435 list_for_each_entry(cfts, &css->ss->cfts, node) {
1436 if (cfts == failed_cfts)
1438 cgroup_addrm_files(css, cgrp, cfts, false);
1443 int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
1445 struct cgroup *dcgrp = &dst_root->cgrp;
1446 struct cgroup_subsys *ss;
1449 lockdep_assert_held(&cgroup_mutex);
1451 do_each_subsys_mask(ss, ssid, ss_mask) {
1453 * If @ss has non-root csses attached to it, can't move.
1454 * If @ss is an implicit controller, it is exempt from this
1455 * rule and can be stolen.
1457 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
1458 !ss->implicit_on_dfl)
1461 /* can't move between two non-dummy roots either */
1462 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
1464 } while_each_subsys_mask();
1466 do_each_subsys_mask(ss, ssid, ss_mask) {
1467 struct cgroup_root *src_root = ss->root;
1468 struct cgroup *scgrp = &src_root->cgrp;
1469 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
1470 struct css_set *cset;
1472 WARN_ON(!css || cgroup_css(dcgrp, ss));
1474 /* disable from the source */
1475 src_root->subsys_mask &= ~(1 << ssid);
1476 WARN_ON(cgroup_apply_control(scgrp));
1477 cgroup_finalize_control(scgrp, 0);
1480 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1481 rcu_assign_pointer(dcgrp->subsys[ssid], css);
1482 ss->root = dst_root;
1483 css->cgroup = dcgrp;
1485 spin_lock_irq(&css_set_lock);
1486 hash_for_each(css_set_table, i, cset, hlist)
1487 list_move_tail(&cset->e_cset_node[ss->id],
1488 &dcgrp->e_csets[ss->id]);
1489 spin_unlock_irq(&css_set_lock);
1491 /* default hierarchy doesn't enable controllers by default */
1492 dst_root->subsys_mask |= 1 << ssid;
1493 if (dst_root == &cgrp_dfl_root) {
1494 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1496 dcgrp->subtree_control |= 1 << ssid;
1497 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
1500 ret = cgroup_apply_control(dcgrp);
1502 pr_warn("partial failure to rebind %s controller (err=%d)\n",
1507 } while_each_subsys_mask();
1509 kernfs_activate(dcgrp->kn);
1513 int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
1514 struct kernfs_root *kf_root)
1518 struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
1519 struct cgroup *ns_cgroup;
1521 buf = kmalloc(PATH_MAX, GFP_KERNEL);
1525 spin_lock_irq(&css_set_lock);
1526 ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
1527 len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
1528 spin_unlock_irq(&css_set_lock);
1530 if (len >= PATH_MAX)
1533 seq_escape(sf, buf, " \t\n\\");
1540 static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
1542 pr_err("remount is not allowed\n");
1547 * To reduce the fork() overhead for systems that are not actually using
1548 * their cgroups capability, we don't maintain the lists running through
1549 * each css_set to its tasks until we see the list actually used - in other
1550 * words after the first mount.
1552 static bool use_task_css_set_links __read_mostly;
1554 static void cgroup_enable_task_cg_lists(void)
1556 struct task_struct *p, *g;
1558 spin_lock_irq(&css_set_lock);
1560 if (use_task_css_set_links)
1563 use_task_css_set_links = true;
1566 * We need tasklist_lock because RCU is not safe against
1567 * while_each_thread(). Besides, a forking task that has passed
1568 * cgroup_post_fork() without seeing use_task_css_set_links = 1
1569 * is not guaranteed to have its child immediately visible in the
1570 * tasklist if we walk through it with RCU.
1572 read_lock(&tasklist_lock);
1573 do_each_thread(g, p) {
1574 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1575 task_css_set(p) != &init_css_set);
1578 * We should check if the process is exiting, otherwise
1579 * it will race with cgroup_exit() in that the list
1580 * entry won't be deleted though the process has exited.
1581 * Do it while holding siglock so that we don't end up
1582 * racing against cgroup_exit().
1584 * Interrupts were already disabled while acquiring
1585 * the css_set_lock, so we do not need to disable it
1586 * again when acquiring the sighand->siglock here.
1588 spin_lock(&p->sighand->siglock);
1589 if (!(p->flags & PF_EXITING)) {
1590 struct css_set *cset = task_css_set(p);
1592 if (!css_set_populated(cset))
1593 css_set_update_populated(cset, true);
1594 list_add_tail(&p->cg_list, &cset->tasks);
1597 spin_unlock(&p->sighand->siglock);
1598 } while_each_thread(g, p);
1599 read_unlock(&tasklist_lock);
1601 spin_unlock_irq(&css_set_lock);
1604 static void init_cgroup_housekeeping(struct cgroup *cgrp)
1606 struct cgroup_subsys *ss;
1609 INIT_LIST_HEAD(&cgrp->self.sibling);
1610 INIT_LIST_HEAD(&cgrp->self.children);
1611 INIT_LIST_HEAD(&cgrp->cset_links);
1612 INIT_LIST_HEAD(&cgrp->pidlists);
1613 mutex_init(&cgrp->pidlist_mutex);
1614 cgrp->self.cgroup = cgrp;
1615 cgrp->self.flags |= CSS_ONLINE;
1617 for_each_subsys(ss, ssid)
1618 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
1620 init_waitqueue_head(&cgrp->offline_waitq);
1621 INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
1624 void init_cgroup_root(struct cgroup_root *root, struct cgroup_sb_opts *opts)
1626 struct cgroup *cgrp = &root->cgrp;
1628 INIT_LIST_HEAD(&root->root_list);
1629 atomic_set(&root->nr_cgrps, 1);
1631 init_cgroup_housekeeping(cgrp);
1632 idr_init(&root->cgroup_idr);
1634 root->flags = opts->flags;
1635 if (opts->release_agent)
1636 strcpy(root->release_agent_path, opts->release_agent);
1638 strcpy(root->name, opts->name);
1639 if (opts->cpuset_clone_children)
1640 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
1643 int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
1645 LIST_HEAD(tmp_links);
1646 struct cgroup *root_cgrp = &root->cgrp;
1647 struct kernfs_syscall_ops *kf_sops;
1648 struct css_set *cset;
1651 lockdep_assert_held(&cgroup_mutex);
1653 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
1656 root_cgrp->id = ret;
1657 root_cgrp->ancestor_ids[0] = ret;
1659 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1665 * We're accessing css_set_count without locking css_set_lock here,
1666 * but that's OK - it can only be increased by someone holding
1667 * cgroup_lock, and that's us. Later rebinding may disable
1668 * controllers on the default hierarchy and thus create new csets,
1669 * which can't be more than the existing ones. Allocate 2x.
1671 ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
1675 ret = cgroup_init_root_id(root);
1679 kf_sops = root == &cgrp_dfl_root ?
1680 &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
1682 root->kf_root = kernfs_create_root(kf_sops,
1683 KERNFS_ROOT_CREATE_DEACTIVATED,
1685 if (IS_ERR(root->kf_root)) {
1686 ret = PTR_ERR(root->kf_root);
1689 root_cgrp->kn = root->kf_root->kn;
1691 ret = css_populate_dir(&root_cgrp->self);
1695 ret = rebind_subsystems(root, ss_mask);
1699 trace_cgroup_setup_root(root);
1702 * There must be no failure case after here, since rebinding takes
1703 * care of subsystems' refcounts, which are explicitly dropped in
1704 * the failure exit path.
1706 list_add(&root->root_list, &cgroup_roots);
1707 cgroup_root_count++;
1710 * Link the root cgroup in this hierarchy into all the css_set
1713 spin_lock_irq(&css_set_lock);
1714 hash_for_each(css_set_table, i, cset, hlist) {
1715 link_css_set(&tmp_links, cset, root_cgrp);
1716 if (css_set_populated(cset))
1717 cgroup_update_populated(root_cgrp, true);
1719 spin_unlock_irq(&css_set_lock);
1721 BUG_ON(!list_empty(&root_cgrp->self.children));
1722 BUG_ON(atomic_read(&root->nr_cgrps) != 1);
1724 kernfs_activate(root_cgrp->kn);
1729 kernfs_destroy_root(root->kf_root);
1730 root->kf_root = NULL;
1732 cgroup_exit_root_id(root);
1734 percpu_ref_exit(&root_cgrp->self.refcnt);
1736 free_cgrp_cset_links(&tmp_links);
1740 struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
1741 struct cgroup_root *root, unsigned long magic,
1742 struct cgroup_namespace *ns)
1744 struct dentry *dentry;
1747 dentry = kernfs_mount(fs_type, flags, root->kf_root, magic, &new_sb);
1750 * In non-init cgroup namespace, instead of root cgroup's dentry,
1751 * we return the dentry corresponding to the cgroupns->root_cgrp.
1753 if (!IS_ERR(dentry) && ns != &init_cgroup_ns) {
1754 struct dentry *nsdentry;
1755 struct cgroup *cgrp;
1757 mutex_lock(&cgroup_mutex);
1758 spin_lock_irq(&css_set_lock);
1760 cgrp = cset_cgroup_from_root(ns->root_cset, root);
1762 spin_unlock_irq(&css_set_lock);
1763 mutex_unlock(&cgroup_mutex);
1765 nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb);
1770 if (IS_ERR(dentry) || !new_sb)
1771 cgroup_put(&root->cgrp);
1776 static struct dentry *cgroup_mount(struct file_system_type *fs_type,
1777 int flags, const char *unused_dev_name,
1780 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
1781 struct dentry *dentry;
1785 /* Check if the caller has permission to mount. */
1786 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) {
1788 return ERR_PTR(-EPERM);
1792 * The first time anyone tries to mount a cgroup, enable the list
1793 * linking each css_set to its tasks and fix up all existing tasks.
1795 if (!use_task_css_set_links)
1796 cgroup_enable_task_cg_lists();
1798 if (fs_type == &cgroup2_fs_type) {
1800 pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
1802 return ERR_PTR(-EINVAL);
1804 cgrp_dfl_visible = true;
1805 cgroup_get(&cgrp_dfl_root.cgrp);
1807 dentry = cgroup_do_mount(&cgroup2_fs_type, flags, &cgrp_dfl_root,
1808 CGROUP2_SUPER_MAGIC, ns);
1810 dentry = cgroup1_mount(&cgroup_fs_type, flags, data,
1811 CGROUP_SUPER_MAGIC, ns);
1818 static void cgroup_kill_sb(struct super_block *sb)
1820 struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
1821 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
1824 * If @root doesn't have any mounts or children, start killing it.
1825 * This prevents new mounts by disabling percpu_ref_tryget_live().
1826 * cgroup_mount() may wait for @root's release.
1828 * And don't kill the default root.
1830 if (!list_empty(&root->cgrp.self.children) ||
1831 root == &cgrp_dfl_root)
1832 cgroup_put(&root->cgrp);
1834 percpu_ref_kill(&root->cgrp.self.refcnt);
1839 struct file_system_type cgroup_fs_type = {
1841 .mount = cgroup_mount,
1842 .kill_sb = cgroup_kill_sb,
1843 .fs_flags = FS_USERNS_MOUNT,
1846 static struct file_system_type cgroup2_fs_type = {
1848 .mount = cgroup_mount,
1849 .kill_sb = cgroup_kill_sb,
1850 .fs_flags = FS_USERNS_MOUNT,
1853 int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
1854 struct cgroup_namespace *ns)
1856 struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
1858 return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
1861 int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
1862 struct cgroup_namespace *ns)
1866 mutex_lock(&cgroup_mutex);
1867 spin_lock_irq(&css_set_lock);
1869 ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
1871 spin_unlock_irq(&css_set_lock);
1872 mutex_unlock(&cgroup_mutex);
1876 EXPORT_SYMBOL_GPL(cgroup_path_ns);
1879 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1880 * @task: target task
1881 * @buf: the buffer to write the path into
1882 * @buflen: the length of the buffer
1884 * Determine @task's cgroup on the first (the one with the lowest non-zero
1885 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1886 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1887 * cgroup controller callbacks.
1889 * Return value is the same as kernfs_path().
1891 int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
1893 struct cgroup_root *root;
1894 struct cgroup *cgrp;
1895 int hierarchy_id = 1;
1898 mutex_lock(&cgroup_mutex);
1899 spin_lock_irq(&css_set_lock);
1901 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1904 cgrp = task_cgroup_from_root(task, root);
1905 ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
1907 /* if no hierarchy exists, everyone is in "/" */
1908 ret = strlcpy(buf, "/", buflen);
1911 spin_unlock_irq(&css_set_lock);
1912 mutex_unlock(&cgroup_mutex);
1915 EXPORT_SYMBOL_GPL(task_cgroup_path);
1918 * cgroup_migrate_add_task - add a migration target task to a migration context
1919 * @task: target task
1920 * @mgctx: target migration context
1922 * Add @task, which is a migration target, to @mgctx->tset. This function
1923 * becomes noop if @task doesn't need to be migrated. @task's css_set
1924 * should have been added as a migration source and @task->cg_list will be
1925 * moved from the css_set's tasks list to mg_tasks one.
1927 static void cgroup_migrate_add_task(struct task_struct *task,
1928 struct cgroup_mgctx *mgctx)
1930 struct css_set *cset;
1932 lockdep_assert_held(&css_set_lock);
1934 /* @task either already exited or can't exit until the end */
1935 if (task->flags & PF_EXITING)
1938 /* leave @task alone if post_fork() hasn't linked it yet */
1939 if (list_empty(&task->cg_list))
1942 cset = task_css_set(task);
1943 if (!cset->mg_src_cgrp)
1946 list_move_tail(&task->cg_list, &cset->mg_tasks);
1947 if (list_empty(&cset->mg_node))
1948 list_add_tail(&cset->mg_node,
1949 &mgctx->tset.src_csets);
1950 if (list_empty(&cset->mg_dst_cset->mg_node))
1951 list_add_tail(&cset->mg_dst_cset->mg_node,
1952 &mgctx->tset.dst_csets);
1956 * cgroup_taskset_first - reset taskset and return the first task
1957 * @tset: taskset of interest
1958 * @dst_cssp: output variable for the destination css
1960 * @tset iteration is initialized and the first task is returned.
1962 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
1963 struct cgroup_subsys_state **dst_cssp)
1965 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1966 tset->cur_task = NULL;
1968 return cgroup_taskset_next(tset, dst_cssp);
1972 * cgroup_taskset_next - iterate to the next task in taskset
1973 * @tset: taskset of interest
1974 * @dst_cssp: output variable for the destination css
1976 * Return the next task in @tset. Iteration must have been initialized
1977 * with cgroup_taskset_first().
1979 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
1980 struct cgroup_subsys_state **dst_cssp)
1982 struct css_set *cset = tset->cur_cset;
1983 struct task_struct *task = tset->cur_task;
1985 while (&cset->mg_node != tset->csets) {
1987 task = list_first_entry(&cset->mg_tasks,
1988 struct task_struct, cg_list);
1990 task = list_next_entry(task, cg_list);
1992 if (&task->cg_list != &cset->mg_tasks) {
1993 tset->cur_cset = cset;
1994 tset->cur_task = task;
1997 * This function may be called both before and
1998 * after cgroup_taskset_migrate(). The two cases
1999 * can be distinguished by looking at whether @cset
2000 * has its ->mg_dst_cset set.
2002 if (cset->mg_dst_cset)
2003 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2005 *dst_cssp = cset->subsys[tset->ssid];
2010 cset = list_next_entry(cset, mg_node);
2018 * cgroup_taskset_migrate - migrate a taskset
2019 * @mgctx: migration context
2021 * Migrate tasks in @mgctx as setup by migration preparation functions.
2022 * This function fails iff one of the ->can_attach callbacks fails and
2023 * guarantees that either all or none of the tasks in @mgctx are migrated.
2024 * @mgctx is consumed regardless of success.
2026 static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
2028 struct cgroup_taskset *tset = &mgctx->tset;
2029 struct cgroup_subsys *ss;
2030 struct task_struct *task, *tmp_task;
2031 struct css_set *cset, *tmp_cset;
2032 int ssid, failed_ssid, ret;
2034 /* methods shouldn't be called if no task is actually migrating */
2035 if (list_empty(&tset->src_csets))
2038 /* check that we can legitimately attach to the cgroup */
2039 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2040 if (ss->can_attach) {
2042 ret = ss->can_attach(tset);
2045 goto out_cancel_attach;
2048 } while_each_subsys_mask();
2051 * Now that we're guaranteed success, proceed to move all tasks to
2052 * the new cgroup. There are no failure cases after here, so this
2053 * is the commit point.
2055 spin_lock_irq(&css_set_lock);
2056 list_for_each_entry(cset, &tset->src_csets, mg_node) {
2057 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2058 struct css_set *from_cset = task_css_set(task);
2059 struct css_set *to_cset = cset->mg_dst_cset;
2061 get_css_set(to_cset);
2062 css_set_move_task(task, from_cset, to_cset, true);
2063 put_css_set_locked(from_cset);
2066 spin_unlock_irq(&css_set_lock);
2069 * Migration is committed, all target tasks are now on dst_csets.
2070 * Nothing is sensitive to fork() after this point. Notify
2071 * controllers that migration is complete.
2073 tset->csets = &tset->dst_csets;
2075 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2080 } while_each_subsys_mask();
2083 goto out_release_tset;
2086 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2087 if (ssid == failed_ssid)
2089 if (ss->cancel_attach) {
2091 ss->cancel_attach(tset);
2093 } while_each_subsys_mask();
2095 spin_lock_irq(&css_set_lock);
2096 list_splice_init(&tset->dst_csets, &tset->src_csets);
2097 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2098 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2099 list_del_init(&cset->mg_node);
2101 spin_unlock_irq(&css_set_lock);
2106 * cgroup_may_migrate_to - verify whether a cgroup can be migration destination
2107 * @dst_cgrp: destination cgroup to test
2109 * On the default hierarchy, except for the root, subtree_control must be
2110 * zero for migration destination cgroups with tasks so that child cgroups
2111 * don't compete against tasks.
2113 bool cgroup_may_migrate_to(struct cgroup *dst_cgrp)
2115 return !cgroup_on_dfl(dst_cgrp) || !cgroup_parent(dst_cgrp) ||
2116 !dst_cgrp->subtree_control;
2120 * cgroup_migrate_finish - cleanup after attach
2121 * @mgctx: migration context
2123 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
2124 * those functions for details.
2126 void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
2128 LIST_HEAD(preloaded);
2129 struct css_set *cset, *tmp_cset;
2131 lockdep_assert_held(&cgroup_mutex);
2133 spin_lock_irq(&css_set_lock);
2135 list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded);
2136 list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded);
2138 list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) {
2139 cset->mg_src_cgrp = NULL;
2140 cset->mg_dst_cgrp = NULL;
2141 cset->mg_dst_cset = NULL;
2142 list_del_init(&cset->mg_preload_node);
2143 put_css_set_locked(cset);
2146 spin_unlock_irq(&css_set_lock);
2150 * cgroup_migrate_add_src - add a migration source css_set
2151 * @src_cset: the source css_set to add
2152 * @dst_cgrp: the destination cgroup
2153 * @mgctx: migration context
2155 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
2156 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
2157 * up by cgroup_migrate_finish().
2159 * This function may be called without holding cgroup_threadgroup_rwsem
2160 * even if the target is a process. Threads may be created and destroyed
2161 * but as long as cgroup_mutex is not dropped, no new css_set can be put
2162 * into play and the preloaded css_sets are guaranteed to cover all
2165 void cgroup_migrate_add_src(struct css_set *src_cset,
2166 struct cgroup *dst_cgrp,
2167 struct cgroup_mgctx *mgctx)
2169 struct cgroup *src_cgrp;
2171 lockdep_assert_held(&cgroup_mutex);
2172 lockdep_assert_held(&css_set_lock);
2175 * If ->dead, @src_set is associated with one or more dead cgroups
2176 * and doesn't contain any migratable tasks. Ignore it early so
2177 * that the rest of migration path doesn't get confused by it.
2182 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2184 if (!list_empty(&src_cset->mg_preload_node))
2187 WARN_ON(src_cset->mg_src_cgrp);
2188 WARN_ON(src_cset->mg_dst_cgrp);
2189 WARN_ON(!list_empty(&src_cset->mg_tasks));
2190 WARN_ON(!list_empty(&src_cset->mg_node));
2192 src_cset->mg_src_cgrp = src_cgrp;
2193 src_cset->mg_dst_cgrp = dst_cgrp;
2194 get_css_set(src_cset);
2195 list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets);
2199 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
2200 * @mgctx: migration context
2202 * Tasks are about to be moved and all the source css_sets have been
2203 * preloaded to @mgctx->preloaded_src_csets. This function looks up and
2204 * pins all destination css_sets, links each to its source, and append them
2205 * to @mgctx->preloaded_dst_csets.
2207 * This function must be called after cgroup_migrate_add_src() has been
2208 * called on each migration source css_set. After migration is performed
2209 * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2212 int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx)
2214 struct css_set *src_cset, *tmp_cset;
2216 lockdep_assert_held(&cgroup_mutex);
2218 /* look up the dst cset for each src cset and link it to src */
2219 list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
2221 struct css_set *dst_cset;
2222 struct cgroup_subsys *ss;
2225 dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
2229 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
2232 * If src cset equals dst, it's noop. Drop the src.
2233 * cgroup_migrate() will skip the cset too. Note that we
2234 * can't handle src == dst as some nodes are used by both.
2236 if (src_cset == dst_cset) {
2237 src_cset->mg_src_cgrp = NULL;
2238 src_cset->mg_dst_cgrp = NULL;
2239 list_del_init(&src_cset->mg_preload_node);
2240 put_css_set(src_cset);
2241 put_css_set(dst_cset);
2245 src_cset->mg_dst_cset = dst_cset;
2247 if (list_empty(&dst_cset->mg_preload_node))
2248 list_add_tail(&dst_cset->mg_preload_node,
2249 &mgctx->preloaded_dst_csets);
2251 put_css_set(dst_cset);
2253 for_each_subsys(ss, ssid)
2254 if (src_cset->subsys[ssid] != dst_cset->subsys[ssid])
2255 mgctx->ss_mask |= 1 << ssid;
2260 cgroup_migrate_finish(mgctx);
2265 * cgroup_migrate - migrate a process or task to a cgroup
2266 * @leader: the leader of the process or the task to migrate
2267 * @threadgroup: whether @leader points to the whole process or a single task
2268 * @mgctx: migration context
2270 * Migrate a process or task denoted by @leader. If migrating a process,
2271 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
2272 * responsible for invoking cgroup_migrate_add_src() and
2273 * cgroup_migrate_prepare_dst() on the targets before invoking this
2274 * function and following up with cgroup_migrate_finish().
2276 * As long as a controller's ->can_attach() doesn't fail, this function is
2277 * guaranteed to succeed. This means that, excluding ->can_attach()
2278 * failure, when migrating multiple targets, the success or failure can be
2279 * decided for all targets by invoking group_migrate_prepare_dst() before
2280 * actually starting migrating.
2282 int cgroup_migrate(struct task_struct *leader, bool threadgroup,
2283 struct cgroup_mgctx *mgctx)
2285 struct task_struct *task;
2288 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2289 * already PF_EXITING could be freed from underneath us unless we
2290 * take an rcu_read_lock.
2292 spin_lock_irq(&css_set_lock);
2296 cgroup_migrate_add_task(task, mgctx);
2299 } while_each_thread(leader, task);
2301 spin_unlock_irq(&css_set_lock);
2303 return cgroup_migrate_execute(mgctx);
2307 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2308 * @dst_cgrp: the cgroup to attach to
2309 * @leader: the task or the leader of the threadgroup to be attached
2310 * @threadgroup: attach the whole threadgroup?
2312 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
2314 int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
2317 DEFINE_CGROUP_MGCTX(mgctx);
2318 struct task_struct *task;
2321 if (!cgroup_may_migrate_to(dst_cgrp))
2324 /* look up all src csets */
2325 spin_lock_irq(&css_set_lock);
2329 cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
2332 } while_each_thread(leader, task);
2334 spin_unlock_irq(&css_set_lock);
2336 /* prepare dst csets and commit */
2337 ret = cgroup_migrate_prepare_dst(&mgctx);
2339 ret = cgroup_migrate(leader, threadgroup, &mgctx);
2341 cgroup_migrate_finish(&mgctx);
2344 trace_cgroup_attach_task(dst_cgrp, leader, threadgroup);
2349 static int cgroup_procs_write_permission(struct task_struct *task,
2350 struct cgroup *dst_cgrp,
2351 struct kernfs_open_file *of)
2355 if (cgroup_on_dfl(dst_cgrp)) {
2356 struct super_block *sb = of->file->f_path.dentry->d_sb;
2357 struct cgroup *cgrp;
2358 struct inode *inode;
2360 spin_lock_irq(&css_set_lock);
2361 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
2362 spin_unlock_irq(&css_set_lock);
2364 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2365 cgrp = cgroup_parent(cgrp);
2368 inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
2370 ret = inode_permission(inode, MAY_WRITE);
2374 const struct cred *cred = current_cred();
2375 const struct cred *tcred = get_task_cred(task);
2378 * even if we're attaching all tasks in the thread group,
2379 * we only need to check permissions on one of them.
2381 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2382 !uid_eq(cred->euid, tcred->uid) &&
2383 !uid_eq(cred->euid, tcred->suid))
2392 * Find the task_struct of the task to attach by vpid and pass it along to the
2393 * function to attach either it or all tasks in its threadgroup. Will lock
2394 * cgroup_mutex and threadgroup.
2396 ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2397 size_t nbytes, loff_t off, bool threadgroup)
2399 struct task_struct *tsk;
2400 struct cgroup_subsys *ss;
2401 struct cgroup *cgrp;
2405 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2408 cgrp = cgroup_kn_lock_live(of->kn, false);
2412 percpu_down_write(&cgroup_threadgroup_rwsem);
2415 tsk = find_task_by_vpid(pid);
2418 goto out_unlock_rcu;
2425 tsk = tsk->group_leader;
2428 * kthreads may acquire PF_NO_SETAFFINITY during initialization.
2429 * If userland migrates such a kthread to a non-root cgroup, it can
2430 * become trapped in a cpuset, or RT kthread may be born in a
2431 * cgroup with no rt_runtime allocated. Just say no.
2433 if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) {
2435 goto out_unlock_rcu;
2438 get_task_struct(tsk);
2441 ret = cgroup_procs_write_permission(tsk, cgrp, of);
2443 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2445 put_task_struct(tsk);
2446 goto out_unlock_threadgroup;
2450 out_unlock_threadgroup:
2451 percpu_up_write(&cgroup_threadgroup_rwsem);
2452 for_each_subsys(ss, ssid)
2453 if (ss->post_attach)
2455 cgroup_kn_unlock(of->kn);
2456 return ret ?: nbytes;
2459 ssize_t cgroup_procs_write(struct kernfs_open_file *of, char *buf, size_t nbytes,
2462 return __cgroup_procs_write(of, buf, nbytes, off, true);
2465 static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
2467 struct cgroup_subsys *ss;
2468 bool printed = false;
2471 do_each_subsys_mask(ss, ssid, ss_mask) {
2474 seq_printf(seq, "%s", ss->name);
2476 } while_each_subsys_mask();
2478 seq_putc(seq, '\n');
2481 /* show controllers which are enabled from the parent */
2482 static int cgroup_controllers_show(struct seq_file *seq, void *v)
2484 struct cgroup *cgrp = seq_css(seq)->cgroup;
2486 cgroup_print_ss_mask(seq, cgroup_control(cgrp));
2490 /* show controllers which are enabled for a given cgroup's children */
2491 static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2493 struct cgroup *cgrp = seq_css(seq)->cgroup;
2495 cgroup_print_ss_mask(seq, cgrp->subtree_control);
2500 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2501 * @cgrp: root of the subtree to update csses for
2503 * @cgrp's control masks have changed and its subtree's css associations
2504 * need to be updated accordingly. This function looks up all css_sets
2505 * which are attached to the subtree, creates the matching updated css_sets
2506 * and migrates the tasks to the new ones.
2508 static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2510 DEFINE_CGROUP_MGCTX(mgctx);
2511 struct cgroup_subsys_state *d_css;
2512 struct cgroup *dsct;
2513 struct css_set *src_cset;
2516 lockdep_assert_held(&cgroup_mutex);
2518 percpu_down_write(&cgroup_threadgroup_rwsem);
2520 /* look up all csses currently attached to @cgrp's subtree */
2521 spin_lock_irq(&css_set_lock);
2522 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2523 struct cgrp_cset_link *link;
2525 list_for_each_entry(link, &dsct->cset_links, cset_link)
2526 cgroup_migrate_add_src(link->cset, dsct, &mgctx);
2528 spin_unlock_irq(&css_set_lock);
2530 /* NULL dst indicates self on default hierarchy */
2531 ret = cgroup_migrate_prepare_dst(&mgctx);
2535 spin_lock_irq(&css_set_lock);
2536 list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) {
2537 struct task_struct *task, *ntask;
2539 /* all tasks in src_csets need to be migrated */
2540 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
2541 cgroup_migrate_add_task(task, &mgctx);
2543 spin_unlock_irq(&css_set_lock);
2545 ret = cgroup_migrate_execute(&mgctx);
2547 cgroup_migrate_finish(&mgctx);
2548 percpu_up_write(&cgroup_threadgroup_rwsem);
2553 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
2554 * @cgrp: root of the target subtree
2556 * Because css offlining is asynchronous, userland may try to re-enable a
2557 * controller while the previous css is still around. This function grabs
2558 * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
2560 void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
2561 __acquires(&cgroup_mutex)
2563 struct cgroup *dsct;
2564 struct cgroup_subsys_state *d_css;
2565 struct cgroup_subsys *ss;
2569 mutex_lock(&cgroup_mutex);
2571 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2572 for_each_subsys(ss, ssid) {
2573 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2576 if (!css || !percpu_ref_is_dying(&css->refcnt))
2580 prepare_to_wait(&dsct->offline_waitq, &wait,
2581 TASK_UNINTERRUPTIBLE);
2583 mutex_unlock(&cgroup_mutex);
2585 finish_wait(&dsct->offline_waitq, &wait);
2594 * cgroup_save_control - save control masks of a subtree
2595 * @cgrp: root of the target subtree
2597 * Save ->subtree_control and ->subtree_ss_mask to the respective old_
2598 * prefixed fields for @cgrp's subtree including @cgrp itself.
2600 static void cgroup_save_control(struct cgroup *cgrp)
2602 struct cgroup *dsct;
2603 struct cgroup_subsys_state *d_css;
2605 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2606 dsct->old_subtree_control = dsct->subtree_control;
2607 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
2612 * cgroup_propagate_control - refresh control masks of a subtree
2613 * @cgrp: root of the target subtree
2615 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2616 * ->subtree_control and propagate controller availability through the
2617 * subtree so that descendants don't have unavailable controllers enabled.
2619 static void cgroup_propagate_control(struct cgroup *cgrp)
2621 struct cgroup *dsct;
2622 struct cgroup_subsys_state *d_css;
2624 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2625 dsct->subtree_control &= cgroup_control(dsct);
2626 dsct->subtree_ss_mask =
2627 cgroup_calc_subtree_ss_mask(dsct->subtree_control,
2628 cgroup_ss_mask(dsct));
2633 * cgroup_restore_control - restore control masks of a subtree
2634 * @cgrp: root of the target subtree
2636 * Restore ->subtree_control and ->subtree_ss_mask from the respective old_
2637 * prefixed fields for @cgrp's subtree including @cgrp itself.
2639 static void cgroup_restore_control(struct cgroup *cgrp)
2641 struct cgroup *dsct;
2642 struct cgroup_subsys_state *d_css;
2644 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2645 dsct->subtree_control = dsct->old_subtree_control;
2646 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
2650 static bool css_visible(struct cgroup_subsys_state *css)
2652 struct cgroup_subsys *ss = css->ss;
2653 struct cgroup *cgrp = css->cgroup;
2655 if (cgroup_control(cgrp) & (1 << ss->id))
2657 if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
2659 return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
2663 * cgroup_apply_control_enable - enable or show csses according to control
2664 * @cgrp: root of the target subtree
2666 * Walk @cgrp's subtree and create new csses or make the existing ones
2667 * visible. A css is created invisible if it's being implicitly enabled
2668 * through dependency. An invisible css is made visible when the userland
2669 * explicitly enables it.
2671 * Returns 0 on success, -errno on failure. On failure, csses which have
2672 * been processed already aren't cleaned up. The caller is responsible for
2673 * cleaning up with cgroup_apply_control_disable().
2675 static int cgroup_apply_control_enable(struct cgroup *cgrp)
2677 struct cgroup *dsct;
2678 struct cgroup_subsys_state *d_css;
2679 struct cgroup_subsys *ss;
2682 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2683 for_each_subsys(ss, ssid) {
2684 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2686 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
2688 if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
2692 css = css_create(dsct, ss);
2694 return PTR_ERR(css);
2697 if (css_visible(css)) {
2698 ret = css_populate_dir(css);
2709 * cgroup_apply_control_disable - kill or hide csses according to control
2710 * @cgrp: root of the target subtree
2712 * Walk @cgrp's subtree and kill and hide csses so that they match
2713 * cgroup_ss_mask() and cgroup_visible_mask().
2715 * A css is hidden when the userland requests it to be disabled while other
2716 * subsystems are still depending on it. The css must not actively control
2717 * resources and be in the vanilla state if it's made visible again later.
2718 * Controllers which may be depended upon should provide ->css_reset() for
2721 static void cgroup_apply_control_disable(struct cgroup *cgrp)
2723 struct cgroup *dsct;
2724 struct cgroup_subsys_state *d_css;
2725 struct cgroup_subsys *ss;
2728 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2729 for_each_subsys(ss, ssid) {
2730 struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2732 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
2738 !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
2740 } else if (!css_visible(css)) {
2750 * cgroup_apply_control - apply control mask updates to the subtree
2751 * @cgrp: root of the target subtree
2753 * subsystems can be enabled and disabled in a subtree using the following
2756 * 1. Call cgroup_save_control() to stash the current state.
2757 * 2. Update ->subtree_control masks in the subtree as desired.
2758 * 3. Call cgroup_apply_control() to apply the changes.
2759 * 4. Optionally perform other related operations.
2760 * 5. Call cgroup_finalize_control() to finish up.
2762 * This function implements step 3 and propagates the mask changes
2763 * throughout @cgrp's subtree, updates csses accordingly and perform
2764 * process migrations.
2766 static int cgroup_apply_control(struct cgroup *cgrp)
2770 cgroup_propagate_control(cgrp);
2772 ret = cgroup_apply_control_enable(cgrp);
2777 * At this point, cgroup_e_css() results reflect the new csses
2778 * making the following cgroup_update_dfl_csses() properly update
2779 * css associations of all tasks in the subtree.
2781 ret = cgroup_update_dfl_csses(cgrp);
2789 * cgroup_finalize_control - finalize control mask update
2790 * @cgrp: root of the target subtree
2791 * @ret: the result of the update
2793 * Finalize control mask update. See cgroup_apply_control() for more info.
2795 static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
2798 cgroup_restore_control(cgrp);
2799 cgroup_propagate_control(cgrp);
2802 cgroup_apply_control_disable(cgrp);
2805 /* change the enabled child controllers for a cgroup in the default hierarchy */
2806 static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2807 char *buf, size_t nbytes,
2810 u16 enable = 0, disable = 0;
2811 struct cgroup *cgrp, *child;
2812 struct cgroup_subsys *ss;
2817 * Parse input - space separated list of subsystem names prefixed
2818 * with either + or -.
2820 buf = strstrip(buf);
2821 while ((tok = strsep(&buf, " "))) {
2824 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
2825 if (!cgroup_ssid_enabled(ssid) ||
2826 strcmp(tok + 1, ss->name))
2830 enable |= 1 << ssid;
2831 disable &= ~(1 << ssid);
2832 } else if (*tok == '-') {
2833 disable |= 1 << ssid;
2834 enable &= ~(1 << ssid);
2839 } while_each_subsys_mask();
2840 if (ssid == CGROUP_SUBSYS_COUNT)
2844 cgrp = cgroup_kn_lock_live(of->kn, true);
2848 for_each_subsys(ss, ssid) {
2849 if (enable & (1 << ssid)) {
2850 if (cgrp->subtree_control & (1 << ssid)) {
2851 enable &= ~(1 << ssid);
2855 if (!(cgroup_control(cgrp) & (1 << ssid))) {
2859 } else if (disable & (1 << ssid)) {
2860 if (!(cgrp->subtree_control & (1 << ssid))) {
2861 disable &= ~(1 << ssid);
2865 /* a child has it enabled? */
2866 cgroup_for_each_live_child(child, cgrp) {
2867 if (child->subtree_control & (1 << ssid)) {
2875 if (!enable && !disable) {
2881 * Except for the root, subtree_control must be zero for a cgroup
2882 * with tasks so that child cgroups don't compete against tasks.
2884 if (enable && cgroup_parent(cgrp)) {
2885 struct cgrp_cset_link *link;
2888 * Because namespaces pin csets too, @cgrp->cset_links
2889 * might not be empty even when @cgrp is empty. Walk and
2892 spin_lock_irq(&css_set_lock);
2895 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
2896 if (css_set_populated(link->cset)) {
2902 spin_unlock_irq(&css_set_lock);
2908 /* save and update control masks and prepare csses */
2909 cgroup_save_control(cgrp);
2911 cgrp->subtree_control |= enable;
2912 cgrp->subtree_control &= ~disable;
2914 ret = cgroup_apply_control(cgrp);
2916 cgroup_finalize_control(cgrp, ret);
2918 kernfs_activate(cgrp->kn);
2921 cgroup_kn_unlock(of->kn);
2922 return ret ?: nbytes;
2925 static int cgroup_events_show(struct seq_file *seq, void *v)
2927 seq_printf(seq, "populated %d\n",
2928 cgroup_is_populated(seq_css(seq)->cgroup));
2932 static int cgroup_file_open(struct kernfs_open_file *of)
2934 struct cftype *cft = of->kn->priv;
2937 return cft->open(of);
2941 static void cgroup_file_release(struct kernfs_open_file *of)
2943 struct cftype *cft = of->kn->priv;
2949 static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2950 size_t nbytes, loff_t off)
2952 struct cgroup *cgrp = of->kn->parent->priv;
2953 struct cftype *cft = of->kn->priv;
2954 struct cgroup_subsys_state *css;
2958 return cft->write(of, buf, nbytes, off);
2961 * kernfs guarantees that a file isn't deleted with operations in
2962 * flight, which means that the matching css is and stays alive and
2963 * doesn't need to be pinned. The RCU locking is not necessary
2964 * either. It's just for the convenience of using cgroup_css().
2967 css = cgroup_css(cgrp, cft->ss);
2970 if (cft->write_u64) {
2971 unsigned long long v;
2972 ret = kstrtoull(buf, 0, &v);
2974 ret = cft->write_u64(css, cft, v);
2975 } else if (cft->write_s64) {
2977 ret = kstrtoll(buf, 0, &v);
2979 ret = cft->write_s64(css, cft, v);
2984 return ret ?: nbytes;
2987 static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
2989 return seq_cft(seq)->seq_start(seq, ppos);
2992 static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2994 return seq_cft(seq)->seq_next(seq, v, ppos);
2997 static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2999 if (seq_cft(seq)->seq_stop)
3000 seq_cft(seq)->seq_stop(seq, v);
3003 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3005 struct cftype *cft = seq_cft(m);
3006 struct cgroup_subsys_state *css = seq_css(m);
3009 return cft->seq_show(m, arg);
3012 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3013 else if (cft->read_s64)
3014 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3020 static struct kernfs_ops cgroup_kf_single_ops = {
3021 .atomic_write_len = PAGE_SIZE,
3022 .open = cgroup_file_open,
3023 .release = cgroup_file_release,
3024 .write = cgroup_file_write,
3025 .seq_show = cgroup_seqfile_show,
3028 static struct kernfs_ops cgroup_kf_ops = {
3029 .atomic_write_len = PAGE_SIZE,
3030 .open = cgroup_file_open,
3031 .release = cgroup_file_release,
3032 .write = cgroup_file_write,
3033 .seq_start = cgroup_seqfile_start,
3034 .seq_next = cgroup_seqfile_next,
3035 .seq_stop = cgroup_seqfile_stop,
3036 .seq_show = cgroup_seqfile_show,
3039 /* set uid and gid of cgroup dirs and files to that of the creator */
3040 static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3042 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3043 .ia_uid = current_fsuid(),
3044 .ia_gid = current_fsgid(), };
3046 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3047 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3050 return kernfs_setattr(kn, &iattr);
3053 static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3056 char name[CGROUP_FILE_NAME_MAX];
3057 struct kernfs_node *kn;
3058 struct lock_class_key *key = NULL;
3061 #ifdef CONFIG_DEBUG_LOCK_ALLOC
3062 key = &cft->lockdep_key;
3064 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3065 cgroup_file_mode(cft), 0, cft->kf_ops, cft,
3070 ret = cgroup_kn_set_ugid(kn);
3076 if (cft->file_offset) {
3077 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3079 spin_lock_irq(&cgroup_file_kn_lock);
3081 spin_unlock_irq(&cgroup_file_kn_lock);
3088 * cgroup_addrm_files - add or remove files to a cgroup directory
3089 * @css: the target css
3090 * @cgrp: the target cgroup (usually css->cgroup)
3091 * @cfts: array of cftypes to be added
3092 * @is_add: whether to add or remove
3094 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
3095 * For removals, this function never fails.
3097 static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3098 struct cgroup *cgrp, struct cftype cfts[],
3101 struct cftype *cft, *cft_end = NULL;
3104 lockdep_assert_held(&cgroup_mutex);
3107 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
3108 /* does cft->flags tell us to skip this file on @cgrp? */
3109 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
3111 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
3113 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
3115 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
3119 ret = cgroup_add_file(css, cgrp, cft);
3121 pr_warn("%s: failed to add %s, err=%d\n",
3122 __func__, cft->name, ret);
3128 cgroup_rm_file(cgrp, cft);
3134 static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
3137 struct cgroup_subsys *ss = cfts[0].ss;
3138 struct cgroup *root = &ss->root->cgrp;
3139 struct cgroup_subsys_state *css;
3142 lockdep_assert_held(&cgroup_mutex);
3144 /* add/rm files for all cgroups created before */
3145 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
3146 struct cgroup *cgrp = css->cgroup;
3148 if (!(css->flags & CSS_VISIBLE))
3151 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
3157 kernfs_activate(root->kn);
3161 static void cgroup_exit_cftypes(struct cftype *cfts)
3165 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3166 /* free copy for custom atomic_write_len, see init_cftypes() */
3167 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3172 /* revert flags set by cgroup core while adding @cfts */
3173 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
3177 static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3181 for (cft = cfts; cft->name[0] != '\0'; cft++) {
3182 struct kernfs_ops *kf_ops;
3184 WARN_ON(cft->ss || cft->kf_ops);
3187 kf_ops = &cgroup_kf_ops;
3189 kf_ops = &cgroup_kf_single_ops;
3192 * Ugh... if @cft wants a custom max_write_len, we need to
3193 * make a copy of kf_ops to set its atomic_write_len.
3195 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3196 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3198 cgroup_exit_cftypes(cfts);
3201 kf_ops->atomic_write_len = cft->max_write_len;
3204 cft->kf_ops = kf_ops;
3211 static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3213 lockdep_assert_held(&cgroup_mutex);
3215 if (!cfts || !cfts[0].ss)
3218 list_del(&cfts->node);
3219 cgroup_apply_cftypes(cfts, false);
3220 cgroup_exit_cftypes(cfts);
3225 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3226 * @cfts: zero-length name terminated array of cftypes
3228 * Unregister @cfts. Files described by @cfts are removed from all
3229 * existing cgroups and all future cgroups won't have them either. This
3230 * function can be called anytime whether @cfts' subsys is attached or not.
3232 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3235 int cgroup_rm_cftypes(struct cftype *cfts)
3239 mutex_lock(&cgroup_mutex);
3240 ret = cgroup_rm_cftypes_locked(cfts);
3241 mutex_unlock(&cgroup_mutex);
3246 * cgroup_add_cftypes - add an array of cftypes to a subsystem
3247 * @ss: target cgroup subsystem
3248 * @cfts: zero-length name terminated array of cftypes
3250 * Register @cfts to @ss. Files described by @cfts are created for all
3251 * existing cgroups to which @ss is attached and all future cgroups will
3252 * have them too. This function can be called anytime whether @ss is
3255 * Returns 0 on successful registration, -errno on failure. Note that this
3256 * function currently returns 0 as long as @cfts registration is successful
3257 * even if some file creation attempts on existing cgroups fail.
3259 static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3263 if (!cgroup_ssid_enabled(ss->id))
3266 if (!cfts || cfts[0].name[0] == '\0')
3269 ret = cgroup_init_cftypes(ss, cfts);
3273 mutex_lock(&cgroup_mutex);
3275 list_add_tail(&cfts->node, &ss->cfts);
3276 ret = cgroup_apply_cftypes(cfts, true);
3278 cgroup_rm_cftypes_locked(cfts);
3280 mutex_unlock(&cgroup_mutex);
3285 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3286 * @ss: target cgroup subsystem
3287 * @cfts: zero-length name terminated array of cftypes
3289 * Similar to cgroup_add_cftypes() but the added files are only used for
3290 * the default hierarchy.
3292 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3296 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3297 cft->flags |= __CFTYPE_ONLY_ON_DFL;
3298 return cgroup_add_cftypes(ss, cfts);
3302 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3303 * @ss: target cgroup subsystem
3304 * @cfts: zero-length name terminated array of cftypes
3306 * Similar to cgroup_add_cftypes() but the added files are only used for
3307 * the legacy hierarchies.
3309 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3313 for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3314 cft->flags |= __CFTYPE_NOT_ON_DFL;
3315 return cgroup_add_cftypes(ss, cfts);
3319 * cgroup_file_notify - generate a file modified event for a cgroup_file
3320 * @cfile: target cgroup_file
3322 * @cfile must have been obtained by setting cftype->file_offset.
3324 void cgroup_file_notify(struct cgroup_file *cfile)
3326 unsigned long flags;
3328 spin_lock_irqsave(&cgroup_file_kn_lock, flags);
3330 kernfs_notify(cfile->kn);
3331 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3335 * css_next_child - find the next child of a given css
3336 * @pos: the current position (%NULL to initiate traversal)
3337 * @parent: css whose children to walk
3339 * This function returns the next child of @parent and should be called
3340 * under either cgroup_mutex or RCU read lock. The only requirement is
3341 * that @parent and @pos are accessible. The next sibling is guaranteed to
3342 * be returned regardless of their states.
3344 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3345 * css which finished ->css_online() is guaranteed to be visible in the
3346 * future iterations and will stay visible until the last reference is put.
3347 * A css which hasn't finished ->css_online() or already finished
3348 * ->css_offline() may show up during traversal. It's each subsystem's
3349 * responsibility to synchronize against on/offlining.
3351 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3352 struct cgroup_subsys_state *parent)
3354 struct cgroup_subsys_state *next;
3356 cgroup_assert_mutex_or_rcu_locked();
3359 * @pos could already have been unlinked from the sibling list.
3360 * Once a cgroup is removed, its ->sibling.next is no longer
3361 * updated when its next sibling changes. CSS_RELEASED is set when
3362 * @pos is taken off list, at which time its next pointer is valid,
3363 * and, as releases are serialized, the one pointed to by the next
3364 * pointer is guaranteed to not have started release yet. This
3365 * implies that if we observe !CSS_RELEASED on @pos in this RCU
3366 * critical section, the one pointed to by its next pointer is
3367 * guaranteed to not have finished its RCU grace period even if we
3368 * have dropped rcu_read_lock() inbetween iterations.
3370 * If @pos has CSS_RELEASED set, its next pointer can't be
3371 * dereferenced; however, as each css is given a monotonically
3372 * increasing unique serial number and always appended to the
3373 * sibling list, the next one can be found by walking the parent's
3374 * children until the first css with higher serial number than
3375 * @pos's. While this path can be slower, it happens iff iteration
3376 * races against release and the race window is very small.
3379 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3380 } else if (likely(!(pos->flags & CSS_RELEASED))) {
3381 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
3383 list_for_each_entry_rcu(next, &parent->children, sibling)
3384 if (next->serial_nr > pos->serial_nr)
3389 * @next, if not pointing to the head, can be dereferenced and is
3392 if (&next->sibling != &parent->children)
3398 * css_next_descendant_pre - find the next descendant for pre-order walk
3399 * @pos: the current position (%NULL to initiate traversal)
3400 * @root: css whose descendants to walk
3402 * To be used by css_for_each_descendant_pre(). Find the next descendant
3403 * to visit for pre-order traversal of @root's descendants. @root is
3404 * included in the iteration and the first node to be visited.
3406 * While this function requires cgroup_mutex or RCU read locking, it
3407 * doesn't require the whole traversal to be contained in a single critical
3408 * section. This function will return the correct next descendant as long
3409 * as both @pos and @root are accessible and @pos is a descendant of @root.
3411 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3412 * css which finished ->css_online() is guaranteed to be visible in the
3413 * future iterations and will stay visible until the last reference is put.
3414 * A css which hasn't finished ->css_online() or already finished
3415 * ->css_offline() may show up during traversal. It's each subsystem's
3416 * responsibility to synchronize against on/offlining.
3418 struct cgroup_subsys_state *
3419 css_next_descendant_pre(struct cgroup_subsys_state *pos,
3420 struct cgroup_subsys_state *root)
3422 struct cgroup_subsys_state *next;
3424 cgroup_assert_mutex_or_rcu_locked();
3426 /* if first iteration, visit @root */
3430 /* visit the first child if exists */
3431 next = css_next_child(NULL, pos);
3435 /* no child, visit my or the closest ancestor's next sibling */
3436 while (pos != root) {
3437 next = css_next_child(pos, pos->parent);
3447 * css_rightmost_descendant - return the rightmost descendant of a css
3448 * @pos: css of interest
3450 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3451 * is returned. This can be used during pre-order traversal to skip
3454 * While this function requires cgroup_mutex or RCU read locking, it
3455 * doesn't require the whole traversal to be contained in a single critical
3456 * section. This function will return the correct rightmost descendant as
3457 * long as @pos is accessible.
3459 struct cgroup_subsys_state *
3460 css_rightmost_descendant(struct cgroup_subsys_state *pos)
3462 struct cgroup_subsys_state *last, *tmp;
3464 cgroup_assert_mutex_or_rcu_locked();
3468 /* ->prev isn't RCU safe, walk ->next till the end */
3470 css_for_each_child(tmp, last)
3477 static struct cgroup_subsys_state *
3478 css_leftmost_descendant(struct cgroup_subsys_state *pos)
3480 struct cgroup_subsys_state *last;
3484 pos = css_next_child(NULL, pos);
3491 * css_next_descendant_post - find the next descendant for post-order walk
3492 * @pos: the current position (%NULL to initiate traversal)
3493 * @root: css whose descendants to walk
3495 * To be used by css_for_each_descendant_post(). Find the next descendant
3496 * to visit for post-order traversal of @root's descendants. @root is
3497 * included in the iteration and the last node to be visited.
3499 * While this function requires cgroup_mutex or RCU read locking, it
3500 * doesn't require the whole traversal to be contained in a single critical
3501 * section. This function will return the correct next descendant as long
3502 * as both @pos and @cgroup are accessible and @pos is a descendant of
3505 * If a subsystem synchronizes ->css_online() and the start of iteration, a
3506 * css which finished ->css_online() is guaranteed to be visible in the
3507 * future iterations and will stay visible until the last reference is put.
3508 * A css which hasn't finished ->css_online() or already finished
3509 * ->css_offline() may show up during traversal. It's each subsystem's
3510 * responsibility to synchronize against on/offlining.
3512 struct cgroup_subsys_state *
3513 css_next_descendant_post(struct cgroup_subsys_state *pos,
3514 struct cgroup_subsys_state *root)
3516 struct cgroup_subsys_state *next;
3518 cgroup_assert_mutex_or_rcu_locked();
3520 /* if first iteration, visit leftmost descendant which may be @root */
3522 return css_leftmost_descendant(root);
3524 /* if we visited @root, we're done */
3528 /* if there's an unvisited sibling, visit its leftmost descendant */
3529 next = css_next_child(pos, pos->parent);
3531 return css_leftmost_descendant(next);
3533 /* no sibling left, visit parent */
3538 * css_has_online_children - does a css have online children
3539 * @css: the target css
3541 * Returns %true if @css has any online children; otherwise, %false. This
3542 * function can be called from any context but the caller is responsible
3543 * for synchronizing against on/offlining as necessary.
3545 bool css_has_online_children(struct cgroup_subsys_state *css)
3547 struct cgroup_subsys_state *child;
3551 css_for_each_child(child, css) {
3552 if (child->flags & CSS_ONLINE) {
3562 * css_task_iter_advance_css_set - advance a task itererator to the next css_set
3563 * @it: the iterator to advance
3565 * Advance @it to the next css_set to walk.
3567 static void css_task_iter_advance_css_set(struct css_task_iter *it)
3569 struct list_head *l = it->cset_pos;
3570 struct cgrp_cset_link *link;
3571 struct css_set *cset;
3573 lockdep_assert_held(&css_set_lock);
3575 /* Advance to the next non-empty css_set */
3578 if (l == it->cset_head) {
3579 it->cset_pos = NULL;
3580 it->task_pos = NULL;
3585 cset = container_of(l, struct css_set,
3586 e_cset_node[it->ss->id]);
3588 link = list_entry(l, struct cgrp_cset_link, cset_link);
3591 } while (!css_set_populated(cset));
3595 if (!list_empty(&cset->tasks))
3596 it->task_pos = cset->tasks.next;
3598 it->task_pos = cset->mg_tasks.next;
3600 it->tasks_head = &cset->tasks;
3601 it->mg_tasks_head = &cset->mg_tasks;
3604 * We don't keep css_sets locked across iteration steps and thus
3605 * need to take steps to ensure that iteration can be resumed after
3606 * the lock is re-acquired. Iteration is performed at two levels -
3607 * css_sets and tasks in them.
3609 * Once created, a css_set never leaves its cgroup lists, so a
3610 * pinned css_set is guaranteed to stay put and we can resume
3611 * iteration afterwards.
3613 * Tasks may leave @cset across iteration steps. This is resolved
3614 * by registering each iterator with the css_set currently being
3615 * walked and making css_set_move_task() advance iterators whose
3616 * next task is leaving.
3619 list_del(&it->iters_node);
3620 put_css_set_locked(it->cur_cset);
3623 it->cur_cset = cset;
3624 list_add(&it->iters_node, &cset->task_iters);
3627 static void css_task_iter_advance(struct css_task_iter *it)
3629 struct list_head *l = it->task_pos;
3631 lockdep_assert_held(&css_set_lock);
3635 * Advance iterator to find next entry. cset->tasks is consumed
3636 * first and then ->mg_tasks. After ->mg_tasks, we move onto the
3641 if (l == it->tasks_head)
3642 l = it->mg_tasks_head->next;
3644 if (l == it->mg_tasks_head)
3645 css_task_iter_advance_css_set(it);
3651 * css_task_iter_start - initiate task iteration
3652 * @css: the css to walk tasks of
3653 * @it: the task iterator to use
3655 * Initiate iteration through the tasks of @css. The caller can call
3656 * css_task_iter_next() to walk through the tasks until the function
3657 * returns NULL. On completion of iteration, css_task_iter_end() must be
3660 void css_task_iter_start(struct cgroup_subsys_state *css,
3661 struct css_task_iter *it)
3663 /* no one should try to iterate before mounting cgroups */
3664 WARN_ON_ONCE(!use_task_css_set_links);
3666 memset(it, 0, sizeof(*it));
3668 spin_lock_irq(&css_set_lock);
3673 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3675 it->cset_pos = &css->cgroup->cset_links;
3677 it->cset_head = it->cset_pos;
3679 css_task_iter_advance_css_set(it);
3681 spin_unlock_irq(&css_set_lock);
3685 * css_task_iter_next - return the next task for the iterator
3686 * @it: the task iterator being iterated
3688 * The "next" function for task iteration. @it should have been
3689 * initialized via css_task_iter_start(). Returns NULL when the iteration
3692 struct task_struct *css_task_iter_next(struct css_task_iter *it)
3695 put_task_struct(it->cur_task);
3696 it->cur_task = NULL;
3699 spin_lock_irq(&css_set_lock);
3702 it->cur_task = list_entry(it->task_pos, struct task_struct,
3704 get_task_struct(it->cur_task);
3705 css_task_iter_advance(it);
3708 spin_unlock_irq(&css_set_lock);
3710 return it->cur_task;
3714 * css_task_iter_end - finish task iteration
3715 * @it: the task iterator to finish
3717 * Finish task iteration started by css_task_iter_start().
3719 void css_task_iter_end(struct css_task_iter *it)
3722 spin_lock_irq(&css_set_lock);
3723 list_del(&it->iters_node);
3724 put_css_set_locked(it->cur_cset);
3725 spin_unlock_irq(&css_set_lock);
3729 put_task_struct(it->cur_task);
3732 static void cgroup_procs_release(struct kernfs_open_file *of)
3735 css_task_iter_end(of->priv);
3740 static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
3742 struct kernfs_open_file *of = s->private;
3743 struct css_task_iter *it = of->priv;
3744 struct task_struct *task;
3747 task = css_task_iter_next(it);
3748 } while (task && !thread_group_leader(task));
3753 static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
3755 struct kernfs_open_file *of = s->private;
3756 struct cgroup *cgrp = seq_css(s)->cgroup;
3757 struct css_task_iter *it = of->priv;
3760 * When a seq_file is seeked, it's always traversed sequentially
3761 * from position 0, so we can simply keep iterating on !0 *pos.
3764 if (WARN_ON_ONCE((*pos)++))
3765 return ERR_PTR(-EINVAL);
3767 it = kzalloc(sizeof(*it), GFP_KERNEL);
3769 return ERR_PTR(-ENOMEM);
3771 css_task_iter_start(&cgrp->self, it);
3772 } else if (!(*pos)++) {
3773 css_task_iter_end(it);
3774 css_task_iter_start(&cgrp->self, it);
3777 return cgroup_procs_next(s, NULL, NULL);
3780 static int cgroup_procs_show(struct seq_file *s, void *v)
3782 seq_printf(s, "%d\n", task_tgid_vnr(v));
3786 /* cgroup core interface files for the default hierarchy */
3787 static struct cftype cgroup_base_files[] = {
3789 .name = "cgroup.procs",
3790 .file_offset = offsetof(struct cgroup, procs_file),
3791 .release = cgroup_procs_release,
3792 .seq_start = cgroup_procs_start,
3793 .seq_next = cgroup_procs_next,
3794 .seq_show = cgroup_procs_show,
3795 .write = cgroup_procs_write,
3798 .name = "cgroup.controllers",
3799 .seq_show = cgroup_controllers_show,
3802 .name = "cgroup.subtree_control",
3803 .seq_show = cgroup_subtree_control_show,
3804 .write = cgroup_subtree_control_write,
3807 .name = "cgroup.events",
3808 .flags = CFTYPE_NOT_ON_ROOT,
3809 .file_offset = offsetof(struct cgroup, events_file),
3810 .seq_show = cgroup_events_show,
3816 * css destruction is four-stage process.
3818 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3819 * Implemented in kill_css().
3821 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3822 * and thus css_tryget_online() is guaranteed to fail, the css can be
3823 * offlined by invoking offline_css(). After offlining, the base ref is
3824 * put. Implemented in css_killed_work_fn().
3826 * 3. When the percpu_ref reaches zero, the only possible remaining
3827 * accessors are inside RCU read sections. css_release() schedules the
3830 * 4. After the grace period, the css can be freed. Implemented in
3831 * css_free_work_fn().
3833 * It is actually hairier because both step 2 and 4 require process context
3834 * and thus involve punting to css->destroy_work adding two additional
3835 * steps to the already complex sequence.
3837 static void css_free_work_fn(struct work_struct *work)
3839 struct cgroup_subsys_state *css =
3840 container_of(work, struct cgroup_subsys_state, destroy_work);
3841 struct cgroup_subsys *ss = css->ss;
3842 struct cgroup *cgrp = css->cgroup;
3844 percpu_ref_exit(&css->refcnt);
3848 struct cgroup_subsys_state *parent = css->parent;
3852 cgroup_idr_remove(&ss->css_idr, id);
3858 /* cgroup free path */
3859 atomic_dec(&cgrp->root->nr_cgrps);
3860 cgroup1_pidlist_destroy_all(cgrp);
3861 cancel_work_sync(&cgrp->release_agent_work);
3863 if (cgroup_parent(cgrp)) {
3865 * We get a ref to the parent, and put the ref when
3866 * this cgroup is being freed, so it's guaranteed
3867 * that the parent won't be destroyed before its
3870 cgroup_put(cgroup_parent(cgrp));
3871 kernfs_put(cgrp->kn);
3875 * This is root cgroup's refcnt reaching zero,
3876 * which indicates that the root should be
3879 cgroup_destroy_root(cgrp->root);
3884 static void css_free_rcu_fn(struct rcu_head *rcu_head)
3886 struct cgroup_subsys_state *css =
3887 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3889 INIT_WORK(&css->destroy_work, css_free_work_fn);
3890 queue_work(cgroup_destroy_wq, &css->destroy_work);
3893 static void css_release_work_fn(struct work_struct *work)
3895 struct cgroup_subsys_state *css =
3896 container_of(work, struct cgroup_subsys_state, destroy_work);
3897 struct cgroup_subsys *ss = css->ss;
3898 struct cgroup *cgrp = css->cgroup;
3900 mutex_lock(&cgroup_mutex);
3902 css->flags |= CSS_RELEASED;
3903 list_del_rcu(&css->sibling);
3906 /* css release path */
3907 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
3908 if (ss->css_released)
3909 ss->css_released(css);
3911 /* cgroup release path */
3912 trace_cgroup_release(cgrp);
3914 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
3918 * There are two control paths which try to determine
3919 * cgroup from dentry without going through kernfs -
3920 * cgroupstats_build() and css_tryget_online_from_dir().
3921 * Those are supported by RCU protecting clearing of
3922 * cgrp->kn->priv backpointer.
3925 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
3928 cgroup_bpf_put(cgrp);
3931 mutex_unlock(&cgroup_mutex);
3933 call_rcu(&css->rcu_head, css_free_rcu_fn);
3936 static void css_release(struct percpu_ref *ref)
3938 struct cgroup_subsys_state *css =
3939 container_of(ref, struct cgroup_subsys_state, refcnt);
3941 INIT_WORK(&css->destroy_work, css_release_work_fn);
3942 queue_work(cgroup_destroy_wq, &css->destroy_work);
3945 static void init_and_link_css(struct cgroup_subsys_state *css,
3946 struct cgroup_subsys *ss, struct cgroup *cgrp)
3948 lockdep_assert_held(&cgroup_mutex);
3952 memset(css, 0, sizeof(*css));
3956 INIT_LIST_HEAD(&css->sibling);
3957 INIT_LIST_HEAD(&css->children);
3958 css->serial_nr = css_serial_nr_next++;
3959 atomic_set(&css->online_cnt, 0);
3961 if (cgroup_parent(cgrp)) {
3962 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
3963 css_get(css->parent);
3966 BUG_ON(cgroup_css(cgrp, ss));
3969 /* invoke ->css_online() on a new CSS and mark it online if successful */
3970 static int online_css(struct cgroup_subsys_state *css)
3972 struct cgroup_subsys *ss = css->ss;
3975 lockdep_assert_held(&cgroup_mutex);
3978 ret = ss->css_online(css);
3980 css->flags |= CSS_ONLINE;
3981 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
3983 atomic_inc(&css->online_cnt);
3985 atomic_inc(&css->parent->online_cnt);
3990 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
3991 static void offline_css(struct cgroup_subsys_state *css)
3993 struct cgroup_subsys *ss = css->ss;
3995 lockdep_assert_held(&cgroup_mutex);
3997 if (!(css->flags & CSS_ONLINE))
4003 if (ss->css_offline)
4004 ss->css_offline(css);
4006 css->flags &= ~CSS_ONLINE;
4007 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
4009 wake_up_all(&css->cgroup->offline_waitq);
4013 * css_create - create a cgroup_subsys_state
4014 * @cgrp: the cgroup new css will be associated with
4015 * @ss: the subsys of new css
4017 * Create a new css associated with @cgrp - @ss pair. On success, the new
4018 * css is online and installed in @cgrp. This function doesn't create the
4019 * interface files. Returns 0 on success, -errno on failure.
4021 static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
4022 struct cgroup_subsys *ss)
4024 struct cgroup *parent = cgroup_parent(cgrp);
4025 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
4026 struct cgroup_subsys_state *css;
4029 lockdep_assert_held(&cgroup_mutex);
4031 css = ss->css_alloc(parent_css);
4033 css = ERR_PTR(-ENOMEM);
4037 init_and_link_css(css, ss, cgrp);
4039 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
4043 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
4048 /* @css is ready to be brought online now, make it visible */
4049 list_add_tail_rcu(&css->sibling, &parent_css->children);
4050 cgroup_idr_replace(&ss->css_idr, css, css->id);
4052 err = online_css(css);
4056 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4057 cgroup_parent(parent)) {
4058 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4059 current->comm, current->pid, ss->name);
4060 if (!strcmp(ss->name, "memory"))
4061 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
4062 ss->warned_broken_hierarchy = true;
4068 list_del_rcu(&css->sibling);
4070 call_rcu(&css->rcu_head, css_free_rcu_fn);
4071 return ERR_PTR(err);
4075 * The returned cgroup is fully initialized including its control mask, but
4076 * it isn't associated with its kernfs_node and doesn't have the control
4079 static struct cgroup *cgroup_create(struct cgroup *parent)
4081 struct cgroup_root *root = parent->root;
4082 struct cgroup *cgrp, *tcgrp;
4083 int level = parent->level + 1;
4086 /* allocate the cgroup and its ID, 0 is reserved for the root */
4087 cgrp = kzalloc(sizeof(*cgrp) +
4088 sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL);
4090 return ERR_PTR(-ENOMEM);
4092 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
4097 * Temporarily set the pointer to NULL, so idr_find() won't return
4098 * a half-baked cgroup.
4100 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
4103 goto out_cancel_ref;
4106 init_cgroup_housekeeping(cgrp);
4108 cgrp->self.parent = &parent->self;
4110 cgrp->level = level;
4112 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp))
4113 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
4115 if (notify_on_release(parent))
4116 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4118 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4119 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
4121 cgrp->self.serial_nr = css_serial_nr_next++;
4123 /* allocation complete, commit to creation */
4124 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
4125 atomic_inc(&root->nr_cgrps);
4129 * @cgrp is now fully operational. If something fails after this
4130 * point, it'll be released via the normal destruction path.
4132 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4135 * On the default hierarchy, a child doesn't automatically inherit
4136 * subtree_control from the parent. Each is configured manually.
4138 if (!cgroup_on_dfl(cgrp))
4139 cgrp->subtree_control = cgroup_control(cgrp);
4142 cgroup_bpf_inherit(cgrp, parent);
4144 cgroup_propagate_control(cgrp);
4149 percpu_ref_exit(&cgrp->self.refcnt);
4152 return ERR_PTR(ret);
4155 int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
4157 struct cgroup *parent, *cgrp;
4158 struct kernfs_node *kn;
4161 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
4162 if (strchr(name, '\n'))
4165 parent = cgroup_kn_lock_live(parent_kn, false);
4169 cgrp = cgroup_create(parent);
4171 ret = PTR_ERR(cgrp);
4175 /* create the directory */
4176 kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
4184 * This extra ref will be put in cgroup_free_fn() and guarantees
4185 * that @cgrp->kn is always accessible.
4189 ret = cgroup_kn_set_ugid(kn);
4193 ret = css_populate_dir(&cgrp->self);
4197 ret = cgroup_apply_control_enable(cgrp);
4201 trace_cgroup_mkdir(cgrp);
4203 /* let's create and online css's */
4204 kernfs_activate(kn);
4210 cgroup_destroy_locked(cgrp);
4212 cgroup_kn_unlock(parent_kn);
4217 * This is called when the refcnt of a css is confirmed to be killed.
4218 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
4219 * initate destruction and put the css ref from kill_css().
4221 static void css_killed_work_fn(struct work_struct *work)
4223 struct cgroup_subsys_state *css =
4224 container_of(work, struct cgroup_subsys_state, destroy_work);
4226 mutex_lock(&cgroup_mutex);
4231 /* @css can't go away while we're holding cgroup_mutex */
4233 } while (css && atomic_dec_and_test(&css->online_cnt));
4235 mutex_unlock(&cgroup_mutex);
4238 /* css kill confirmation processing requires process context, bounce */
4239 static void css_killed_ref_fn(struct percpu_ref *ref)
4241 struct cgroup_subsys_state *css =
4242 container_of(ref, struct cgroup_subsys_state, refcnt);
4244 if (atomic_dec_and_test(&css->online_cnt)) {
4245 INIT_WORK(&css->destroy_work, css_killed_work_fn);
4246 queue_work(cgroup_destroy_wq, &css->destroy_work);
4251 * kill_css - destroy a css
4252 * @css: css to destroy
4254 * This function initiates destruction of @css by removing cgroup interface
4255 * files and putting its base reference. ->css_offline() will be invoked
4256 * asynchronously once css_tryget_online() is guaranteed to fail and when
4257 * the reference count reaches zero, @css will be released.
4259 static void kill_css(struct cgroup_subsys_state *css)
4261 lockdep_assert_held(&cgroup_mutex);
4264 * This must happen before css is disassociated with its cgroup.
4265 * See seq_css() for details.
4270 * Killing would put the base ref, but we need to keep it alive
4271 * until after ->css_offline().
4276 * cgroup core guarantees that, by the time ->css_offline() is
4277 * invoked, no new css reference will be given out via
4278 * css_tryget_online(). We can't simply call percpu_ref_kill() and
4279 * proceed to offlining css's because percpu_ref_kill() doesn't
4280 * guarantee that the ref is seen as killed on all CPUs on return.
4282 * Use percpu_ref_kill_and_confirm() to get notifications as each
4283 * css is confirmed to be seen as killed on all CPUs.
4285 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
4289 * cgroup_destroy_locked - the first stage of cgroup destruction
4290 * @cgrp: cgroup to be destroyed
4292 * css's make use of percpu refcnts whose killing latency shouldn't be
4293 * exposed to userland and are RCU protected. Also, cgroup core needs to
4294 * guarantee that css_tryget_online() won't succeed by the time
4295 * ->css_offline() is invoked. To satisfy all the requirements,
4296 * destruction is implemented in the following two steps.
4298 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4299 * userland visible parts and start killing the percpu refcnts of
4300 * css's. Set up so that the next stage will be kicked off once all
4301 * the percpu refcnts are confirmed to be killed.
4303 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4304 * rest of destruction. Once all cgroup references are gone, the
4305 * cgroup is RCU-freed.
4307 * This function implements s1. After this step, @cgrp is gone as far as
4308 * the userland is concerned and a new cgroup with the same name may be
4309 * created. As cgroup doesn't care about the names internally, this
4310 * doesn't cause any problem.
4312 static int cgroup_destroy_locked(struct cgroup *cgrp)
4313 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4315 struct cgroup_subsys_state *css;
4316 struct cgrp_cset_link *link;
4319 lockdep_assert_held(&cgroup_mutex);
4322 * Only migration can raise populated from zero and we're already
4323 * holding cgroup_mutex.
4325 if (cgroup_is_populated(cgrp))
4329 * Make sure there's no live children. We can't test emptiness of
4330 * ->self.children as dead children linger on it while being
4331 * drained; otherwise, "rmdir parent/child parent" may fail.
4333 if (css_has_online_children(&cgrp->self))
4337 * Mark @cgrp and the associated csets dead. The former prevents
4338 * further task migration and child creation by disabling
4339 * cgroup_lock_live_group(). The latter makes the csets ignored by
4340 * the migration path.
4342 cgrp->self.flags &= ~CSS_ONLINE;
4344 spin_lock_irq(&css_set_lock);
4345 list_for_each_entry(link, &cgrp->cset_links, cset_link)
4346 link->cset->dead = true;
4347 spin_unlock_irq(&css_set_lock);
4349 /* initiate massacre of all css's */
4350 for_each_css(css, ssid, cgrp)
4354 * Remove @cgrp directory along with the base files. @cgrp has an
4355 * extra ref on its kn.
4357 kernfs_remove(cgrp->kn);
4359 cgroup1_check_for_release(cgroup_parent(cgrp));
4361 /* put the base reference */
4362 percpu_ref_kill(&cgrp->self.refcnt);
4367 int cgroup_rmdir(struct kernfs_node *kn)
4369 struct cgroup *cgrp;
4372 cgrp = cgroup_kn_lock_live(kn, false);
4376 ret = cgroup_destroy_locked(cgrp);
4379 trace_cgroup_rmdir(cgrp);
4381 cgroup_kn_unlock(kn);
4385 static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4386 .remount_fs = cgroup_remount,
4387 .mkdir = cgroup_mkdir,
4388 .rmdir = cgroup_rmdir,
4389 .show_path = cgroup_show_path,
4392 static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
4394 struct cgroup_subsys_state *css;
4396 pr_debug("Initializing cgroup subsys %s\n", ss->name);
4398 mutex_lock(&cgroup_mutex);
4400 idr_init(&ss->css_idr);
4401 INIT_LIST_HEAD(&ss->cfts);
4403 /* Create the root cgroup state for this subsystem */
4404 ss->root = &cgrp_dfl_root;
4405 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
4406 /* We don't handle early failures gracefully */
4407 BUG_ON(IS_ERR(css));
4408 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
4411 * Root csses are never destroyed and we can't initialize
4412 * percpu_ref during early init. Disable refcnting.
4414 css->flags |= CSS_NO_REF;
4417 /* allocation can't be done safely during early init */
4420 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4421 BUG_ON(css->id < 0);
4424 /* Update the init_css_set to contain a subsys
4425 * pointer to this state - since the subsystem is
4426 * newly registered, all tasks and hence the
4427 * init_css_set is in the subsystem's root cgroup. */
4428 init_css_set.subsys[ss->id] = css;
4430 have_fork_callback |= (bool)ss->fork << ss->id;
4431 have_exit_callback |= (bool)ss->exit << ss->id;
4432 have_free_callback |= (bool)ss->free << ss->id;
4433 have_canfork_callback |= (bool)ss->can_fork << ss->id;
4435 /* At system boot, before all subsystems have been
4436 * registered, no tasks have been forked, so we don't
4437 * need to invoke fork callbacks here. */
4438 BUG_ON(!list_empty(&init_task.tasks));
4440 BUG_ON(online_css(css));
4442 mutex_unlock(&cgroup_mutex);
4446 * cgroup_init_early - cgroup initialization at system boot
4448 * Initialize cgroups at system boot, and initialize any
4449 * subsystems that request early init.
4451 int __init cgroup_init_early(void)
4453 static struct cgroup_sb_opts __initdata opts;
4454 struct cgroup_subsys *ss;
4457 init_cgroup_root(&cgrp_dfl_root, &opts);
4458 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
4460 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
4462 for_each_subsys(ss, i) {
4463 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
4464 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
4465 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
4467 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4468 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4471 ss->name = cgroup_subsys_name[i];
4472 if (!ss->legacy_name)
4473 ss->legacy_name = cgroup_subsys_name[i];
4476 cgroup_init_subsys(ss, true);
4481 static u16 cgroup_disable_mask __initdata;
4484 * cgroup_init - cgroup initialization
4486 * Register cgroup filesystem and /proc file, and initialize
4487 * any subsystems that didn't request early init.
4489 int __init cgroup_init(void)
4491 struct cgroup_subsys *ss;
4494 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
4495 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
4496 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
4497 BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
4500 * The latency of the synchronize_sched() is too high for cgroups,
4501 * avoid it at the cost of forcing all readers into the slow path.
4503 rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss);
4505 get_user_ns(init_cgroup_ns.user_ns);
4507 mutex_lock(&cgroup_mutex);
4510 * Add init_css_set to the hash table so that dfl_root can link to
4513 hash_add(css_set_table, &init_css_set.hlist,
4514 css_set_hash(init_css_set.subsys));
4516 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
4518 mutex_unlock(&cgroup_mutex);
4520 for_each_subsys(ss, ssid) {
4521 if (ss->early_init) {
4522 struct cgroup_subsys_state *css =
4523 init_css_set.subsys[ss->id];
4525 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4527 BUG_ON(css->id < 0);
4529 cgroup_init_subsys(ss, false);
4532 list_add_tail(&init_css_set.e_cset_node[ssid],
4533 &cgrp_dfl_root.cgrp.e_csets[ssid]);
4536 * Setting dfl_root subsys_mask needs to consider the
4537 * disabled flag and cftype registration needs kmalloc,
4538 * both of which aren't available during early_init.
4540 if (cgroup_disable_mask & (1 << ssid)) {
4541 static_branch_disable(cgroup_subsys_enabled_key[ssid]);
4542 printk(KERN_INFO "Disabling %s control group subsystem\n",
4547 if (cgroup1_ssid_disabled(ssid))
4548 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
4551 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
4553 if (ss->implicit_on_dfl)
4554 cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
4555 else if (!ss->dfl_cftypes)
4556 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
4558 if (ss->dfl_cftypes == ss->legacy_cftypes) {
4559 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
4561 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
4562 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
4566 ss->bind(init_css_set.subsys[ssid]);
4569 /* init_css_set.subsys[] has been updated, re-hash */
4570 hash_del(&init_css_set.hlist);
4571 hash_add(css_set_table, &init_css_set.hlist,
4572 css_set_hash(init_css_set.subsys));
4574 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
4575 WARN_ON(register_filesystem(&cgroup_fs_type));
4576 WARN_ON(register_filesystem(&cgroup2_fs_type));
4577 WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
4582 static int __init cgroup_wq_init(void)
4585 * There isn't much point in executing destruction path in
4586 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4587 * Use 1 for @max_active.
4589 * We would prefer to do this in cgroup_init() above, but that
4590 * is called before init_workqueues(): so leave this until after.
4592 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
4593 BUG_ON(!cgroup_destroy_wq);
4596 core_initcall(cgroup_wq_init);
4599 * proc_cgroup_show()
4600 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4601 * - Used for /proc/<pid>/cgroup.
4603 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
4604 struct pid *pid, struct task_struct *tsk)
4608 struct cgroup_root *root;
4611 buf = kmalloc(PATH_MAX, GFP_KERNEL);
4615 mutex_lock(&cgroup_mutex);
4616 spin_lock_irq(&css_set_lock);
4618 for_each_root(root) {
4619 struct cgroup_subsys *ss;
4620 struct cgroup *cgrp;
4621 int ssid, count = 0;
4623 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
4626 seq_printf(m, "%d:", root->hierarchy_id);
4627 if (root != &cgrp_dfl_root)
4628 for_each_subsys(ss, ssid)
4629 if (root->subsys_mask & (1 << ssid))
4630 seq_printf(m, "%s%s", count++ ? "," : "",
4632 if (strlen(root->name))
4633 seq_printf(m, "%sname=%s", count ? "," : "",
4637 cgrp = task_cgroup_from_root(tsk, root);
4640 * On traditional hierarchies, all zombie tasks show up as
4641 * belonging to the root cgroup. On the default hierarchy,
4642 * while a zombie doesn't show up in "cgroup.procs" and
4643 * thus can't be migrated, its /proc/PID/cgroup keeps
4644 * reporting the cgroup it belonged to before exiting. If
4645 * the cgroup is removed before the zombie is reaped,
4646 * " (deleted)" is appended to the cgroup path.
4648 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
4649 retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
4650 current->nsproxy->cgroup_ns);
4651 if (retval >= PATH_MAX)
4652 retval = -ENAMETOOLONG;
4661 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
4662 seq_puts(m, " (deleted)\n");
4669 spin_unlock_irq(&css_set_lock);
4670 mutex_unlock(&cgroup_mutex);
4677 * cgroup_fork - initialize cgroup related fields during copy_process()
4678 * @child: pointer to task_struct of forking parent process.
4680 * A task is associated with the init_css_set until cgroup_post_fork()
4681 * attaches it to the parent's css_set. Empty cg_list indicates that
4682 * @child isn't holding reference to its css_set.
4684 void cgroup_fork(struct task_struct *child)
4686 RCU_INIT_POINTER(child->cgroups, &init_css_set);
4687 INIT_LIST_HEAD(&child->cg_list);
4691 * cgroup_can_fork - called on a new task before the process is exposed
4692 * @child: the task in question.
4694 * This calls the subsystem can_fork() callbacks. If the can_fork() callback
4695 * returns an error, the fork aborts with that error code. This allows for
4696 * a cgroup subsystem to conditionally allow or deny new forks.
4698 int cgroup_can_fork(struct task_struct *child)
4700 struct cgroup_subsys *ss;
4703 do_each_subsys_mask(ss, i, have_canfork_callback) {
4704 ret = ss->can_fork(child);
4707 } while_each_subsys_mask();
4712 for_each_subsys(ss, j) {
4715 if (ss->cancel_fork)
4716 ss->cancel_fork(child);
4723 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
4724 * @child: the task in question
4726 * This calls the cancel_fork() callbacks if a fork failed *after*
4727 * cgroup_can_fork() succeded.
4729 void cgroup_cancel_fork(struct task_struct *child)
4731 struct cgroup_subsys *ss;
4734 for_each_subsys(ss, i)
4735 if (ss->cancel_fork)
4736 ss->cancel_fork(child);
4740 * cgroup_post_fork - called on a new task after adding it to the task list
4741 * @child: the task in question
4743 * Adds the task to the list running through its css_set if necessary and
4744 * call the subsystem fork() callbacks. Has to be after the task is
4745 * visible on the task list in case we race with the first call to
4746 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
4749 void cgroup_post_fork(struct task_struct *child)
4751 struct cgroup_subsys *ss;
4755 * This may race against cgroup_enable_task_cg_lists(). As that
4756 * function sets use_task_css_set_links before grabbing
4757 * tasklist_lock and we just went through tasklist_lock to add
4758 * @child, it's guaranteed that either we see the set
4759 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4760 * @child during its iteration.
4762 * If we won the race, @child is associated with %current's
4763 * css_set. Grabbing css_set_lock guarantees both that the
4764 * association is stable, and, on completion of the parent's
4765 * migration, @child is visible in the source of migration or
4766 * already in the destination cgroup. This guarantee is necessary
4767 * when implementing operations which need to migrate all tasks of
4768 * a cgroup to another.
4770 * Note that if we lose to cgroup_enable_task_cg_lists(), @child
4771 * will remain in init_css_set. This is safe because all tasks are
4772 * in the init_css_set before cg_links is enabled and there's no
4773 * operation which transfers all tasks out of init_css_set.
4775 if (use_task_css_set_links) {
4776 struct css_set *cset;
4778 spin_lock_irq(&css_set_lock);
4779 cset = task_css_set(current);
4780 if (list_empty(&child->cg_list)) {
4782 css_set_move_task(child, NULL, cset, false);
4784 spin_unlock_irq(&css_set_lock);
4788 * Call ss->fork(). This must happen after @child is linked on
4789 * css_set; otherwise, @child might change state between ->fork()
4790 * and addition to css_set.
4792 do_each_subsys_mask(ss, i, have_fork_callback) {
4794 } while_each_subsys_mask();
4798 * cgroup_exit - detach cgroup from exiting task
4799 * @tsk: pointer to task_struct of exiting process
4801 * Description: Detach cgroup from @tsk and release it.
4803 * Note that cgroups marked notify_on_release force every task in
4804 * them to take the global cgroup_mutex mutex when exiting.
4805 * This could impact scaling on very large systems. Be reluctant to
4806 * use notify_on_release cgroups where very high task exit scaling
4807 * is required on large systems.
4809 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
4810 * call cgroup_exit() while the task is still competent to handle
4811 * notify_on_release(), then leave the task attached to the root cgroup in
4812 * each hierarchy for the remainder of its exit. No need to bother with
4813 * init_css_set refcnting. init_css_set never goes away and we can't race
4814 * with migration path - PF_EXITING is visible to migration path.
4816 void cgroup_exit(struct task_struct *tsk)
4818 struct cgroup_subsys *ss;
4819 struct css_set *cset;
4823 * Unlink from @tsk from its css_set. As migration path can't race
4824 * with us, we can check css_set and cg_list without synchronization.
4826 cset = task_css_set(tsk);
4828 if (!list_empty(&tsk->cg_list)) {
4829 spin_lock_irq(&css_set_lock);
4830 css_set_move_task(tsk, cset, NULL, false);
4831 spin_unlock_irq(&css_set_lock);
4836 /* see cgroup_post_fork() for details */
4837 do_each_subsys_mask(ss, i, have_exit_callback) {
4839 } while_each_subsys_mask();
4842 void cgroup_free(struct task_struct *task)
4844 struct css_set *cset = task_css_set(task);
4845 struct cgroup_subsys *ss;
4848 do_each_subsys_mask(ss, ssid, have_free_callback) {
4850 } while_each_subsys_mask();
4855 static int __init cgroup_disable(char *str)
4857 struct cgroup_subsys *ss;
4861 while ((token = strsep(&str, ",")) != NULL) {
4865 for_each_subsys(ss, i) {
4866 if (strcmp(token, ss->name) &&
4867 strcmp(token, ss->legacy_name))
4869 cgroup_disable_mask |= 1 << i;
4874 __setup("cgroup_disable=", cgroup_disable);
4877 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
4878 * @dentry: directory dentry of interest
4879 * @ss: subsystem of interest
4881 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4882 * to get the corresponding css and return it. If such css doesn't exist
4883 * or can't be pinned, an ERR_PTR value is returned.
4885 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
4886 struct cgroup_subsys *ss)
4888 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4889 struct file_system_type *s_type = dentry->d_sb->s_type;
4890 struct cgroup_subsys_state *css = NULL;
4891 struct cgroup *cgrp;
4893 /* is @dentry a cgroup dir? */
4894 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
4895 !kn || kernfs_type(kn) != KERNFS_DIR)
4896 return ERR_PTR(-EBADF);
4901 * This path doesn't originate from kernfs and @kn could already
4902 * have been or be removed at any point. @kn->priv is RCU
4903 * protected for this access. See css_release_work_fn() for details.
4905 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
4907 css = cgroup_css(cgrp, ss);
4909 if (!css || !css_tryget_online(css))
4910 css = ERR_PTR(-ENOENT);
4917 * css_from_id - lookup css by id
4918 * @id: the cgroup id
4919 * @ss: cgroup subsys to be looked into
4921 * Returns the css if there's valid one with @id, otherwise returns NULL.
4922 * Should be called under rcu_read_lock().
4924 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4926 WARN_ON_ONCE(!rcu_read_lock_held());
4927 return idr_find(&ss->css_idr, id);
4931 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
4932 * @path: path on the default hierarchy
4934 * Find the cgroup at @path on the default hierarchy, increment its
4935 * reference count and return it. Returns pointer to the found cgroup on
4936 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
4937 * if @path points to a non-directory.
4939 struct cgroup *cgroup_get_from_path(const char *path)
4941 struct kernfs_node *kn;
4942 struct cgroup *cgrp;
4944 mutex_lock(&cgroup_mutex);
4946 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
4948 if (kernfs_type(kn) == KERNFS_DIR) {
4952 cgrp = ERR_PTR(-ENOTDIR);
4956 cgrp = ERR_PTR(-ENOENT);
4959 mutex_unlock(&cgroup_mutex);
4962 EXPORT_SYMBOL_GPL(cgroup_get_from_path);
4965 * cgroup_get_from_fd - get a cgroup pointer from a fd
4966 * @fd: fd obtained by open(cgroup2_dir)
4968 * Find the cgroup from a fd which should be obtained
4969 * by opening a cgroup directory. Returns a pointer to the
4970 * cgroup on success. ERR_PTR is returned if the cgroup
4973 struct cgroup *cgroup_get_from_fd(int fd)
4975 struct cgroup_subsys_state *css;
4976 struct cgroup *cgrp;
4981 return ERR_PTR(-EBADF);
4983 css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
4986 return ERR_CAST(css);
4989 if (!cgroup_on_dfl(cgrp)) {
4991 return ERR_PTR(-EBADF);
4996 EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
4999 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
5000 * definition in cgroup-defs.h.
5002 #ifdef CONFIG_SOCK_CGROUP_DATA
5004 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5006 DEFINE_SPINLOCK(cgroup_sk_update_lock);
5007 static bool cgroup_sk_alloc_disabled __read_mostly;
5009 void cgroup_sk_alloc_disable(void)
5011 if (cgroup_sk_alloc_disabled)
5013 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5014 cgroup_sk_alloc_disabled = true;
5019 #define cgroup_sk_alloc_disabled false
5023 void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
5025 if (cgroup_sk_alloc_disabled)
5028 /* Socket clone path */
5030 cgroup_get(sock_cgroup_ptr(skcd));
5037 struct css_set *cset;
5039 cset = task_css_set(current);
5040 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
5041 skcd->val = (unsigned long)cset->dfl_cgrp;
5050 void cgroup_sk_free(struct sock_cgroup_data *skcd)
5052 cgroup_put(sock_cgroup_ptr(skcd));
5055 #endif /* CONFIG_SOCK_CGROUP_DATA */
5057 #ifdef CONFIG_CGROUP_BPF
5058 int cgroup_bpf_update(struct cgroup *cgrp, struct bpf_prog *prog,
5059 enum bpf_attach_type type, bool overridable)
5061 struct cgroup *parent = cgroup_parent(cgrp);
5064 mutex_lock(&cgroup_mutex);
5065 ret = __cgroup_bpf_update(cgrp, parent, prog, type, overridable);
5066 mutex_unlock(&cgroup_mutex);
5069 #endif /* CONFIG_CGROUP_BPF */