1 #ifndef _LINUX_CGROUP_H
2 #define _LINUX_CGROUP_H
6 * Copyright (C) 2003 BULL SA
7 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 #include <linux/sched.h>
12 #include <linux/cpumask.h>
13 #include <linux/nodemask.h>
14 #include <linux/rcupdate.h>
15 #include <linux/rculist.h>
16 #include <linux/cgroupstats.h>
17 #include <linux/rwsem.h>
18 #include <linux/idr.h>
19 #include <linux/workqueue.h>
21 #include <linux/percpu-refcount.h>
22 #include <linux/seq_file.h>
23 #include <linux/kernfs.h>
24 #include <linux/wait.h>
32 extern int cgroup_init_early(void);
33 extern int cgroup_init(void);
34 extern void cgroup_fork(struct task_struct *p);
35 extern void cgroup_post_fork(struct task_struct *p);
36 extern void cgroup_exit(struct task_struct *p);
37 extern int cgroupstats_build(struct cgroupstats *stats,
38 struct dentry *dentry);
40 extern int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
41 struct pid *pid, struct task_struct *tsk);
43 /* define the enumeration of all cgroup subsystems */
44 #define SUBSYS(_x) _x ## _cgrp_id,
45 enum cgroup_subsys_id {
46 #include <linux/cgroup_subsys.h>
52 * Per-subsystem/per-cgroup state maintained by the system. This is the
53 * fundamental structural building block that controllers deal with.
55 * Fields marked with "PI:" are public and immutable and may be accessed
56 * directly without synchronization.
58 struct cgroup_subsys_state {
59 /* PI: the cgroup that this css is attached to */
60 struct cgroup *cgroup;
62 /* PI: the cgroup subsystem that this css is attached to */
63 struct cgroup_subsys *ss;
65 /* reference count - access via css_[try]get() and css_put() */
66 struct percpu_ref refcnt;
68 /* PI: the parent css */
69 struct cgroup_subsys_state *parent;
71 /* siblings list anchored at the parent's ->children */
72 struct list_head sibling;
73 struct list_head children;
76 * PI: Subsys-unique ID. 0 is unused and root is always 1. The
77 * matching css can be looked up using css_from_id().
84 * Monotonically increasing unique serial number which defines a
85 * uniform order among all csses. It's guaranteed that all
86 * ->children lists are in the ascending order of ->serial_nr and
87 * used to allow interrupting and resuming iterations.
91 /* percpu_ref killing and RCU release */
92 struct rcu_head rcu_head;
93 struct work_struct destroy_work;
96 /* bits in struct cgroup_subsys_state flags field */
98 CSS_NO_REF = (1 << 0), /* no reference counting for this css */
99 CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */
100 CSS_RELEASED = (1 << 2), /* refcnt reached zero, released */
104 * css_get - obtain a reference on the specified css
107 * The caller must already have a reference.
109 static inline void css_get(struct cgroup_subsys_state *css)
111 if (!(css->flags & CSS_NO_REF))
112 percpu_ref_get(&css->refcnt);
116 * css_get_many - obtain references on the specified css
118 * @n: number of references to get
120 * The caller must already have a reference.
122 static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
124 if (!(css->flags & CSS_NO_REF))
125 percpu_ref_get_many(&css->refcnt, n);
129 * css_tryget - try to obtain a reference on the specified css
132 * Obtain a reference on @css unless it already has reached zero and is
133 * being released. This function doesn't care whether @css is on or
134 * offline. The caller naturally needs to ensure that @css is accessible
135 * but doesn't have to be holding a reference on it - IOW, RCU protected
136 * access is good enough for this function. Returns %true if a reference
137 * count was successfully obtained; %false otherwise.
139 static inline bool css_tryget(struct cgroup_subsys_state *css)
141 if (!(css->flags & CSS_NO_REF))
142 return percpu_ref_tryget(&css->refcnt);
147 * css_tryget_online - try to obtain a reference on the specified css if online
150 * Obtain a reference on @css if it's online. The caller naturally needs
151 * to ensure that @css is accessible but doesn't have to be holding a
152 * reference on it - IOW, RCU protected access is good enough for this
153 * function. Returns %true if a reference count was successfully obtained;
156 static inline bool css_tryget_online(struct cgroup_subsys_state *css)
158 if (!(css->flags & CSS_NO_REF))
159 return percpu_ref_tryget_live(&css->refcnt);
164 * css_put - put a css reference
167 * Put a reference obtained via css_get() and css_tryget_online().
169 static inline void css_put(struct cgroup_subsys_state *css)
171 if (!(css->flags & CSS_NO_REF))
172 percpu_ref_put(&css->refcnt);
176 * css_put_many - put css references
178 * @n: number of references to put
180 * Put references obtained via css_get() and css_tryget_online().
182 static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
184 if (!(css->flags & CSS_NO_REF))
185 percpu_ref_put_many(&css->refcnt, n);
188 /* bits in struct cgroup flags field */
190 /* Control Group requires release notifications to userspace */
191 CGRP_NOTIFY_ON_RELEASE,
193 * Clone the parent's configuration when creating a new child
194 * cpuset cgroup. For historical reasons, this option can be
195 * specified at mount time and thus is implemented here.
197 CGRP_CPUSET_CLONE_CHILDREN,
201 /* self css with NULL ->ss, points back to this cgroup */
202 struct cgroup_subsys_state self;
204 unsigned long flags; /* "unsigned long" so bitops work */
207 * idr allocated in-hierarchy ID.
209 * ID 0 is not used, the ID of the root cgroup is always 1, and a
210 * new cgroup will be assigned with a smallest available ID.
212 * Allocating/Removing ID must be protected by cgroup_mutex.
217 * If this cgroup contains any tasks, it contributes one to
218 * populated_cnt. All children with non-zero popuplated_cnt of
219 * their own contribute one. The count is zero iff there's no task
220 * in this cgroup or its subtree.
224 struct kernfs_node *kn; /* cgroup kernfs entry */
225 struct kernfs_node *populated_kn; /* kn for "cgroup.subtree_populated" */
228 * The bitmask of subsystems enabled on the child cgroups.
229 * ->subtree_control is the one configured through
230 * "cgroup.subtree_control" while ->child_subsys_mask is the
231 * effective one which may have more subsystems enabled.
232 * Controller knobs are made available iff it's enabled in
235 unsigned int subtree_control;
236 unsigned int child_subsys_mask;
238 /* Private pointers for each registered subsystem */
239 struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
241 struct cgroup_root *root;
244 * List of cgrp_cset_links pointing at css_sets with tasks in this
245 * cgroup. Protected by css_set_lock.
247 struct list_head cset_links;
250 * On the default hierarchy, a css_set for a cgroup with some
251 * susbsys disabled will point to css's which are associated with
252 * the closest ancestor which has the subsys enabled. The
253 * following lists all css_sets which point to this cgroup's css
254 * for the given subsystem.
256 struct list_head e_csets[CGROUP_SUBSYS_COUNT];
259 * list of pidlists, up to two for each namespace (one for procs, one
260 * for tasks); created on demand.
262 struct list_head pidlists;
263 struct mutex pidlist_mutex;
265 /* used to wait for offlining of csses */
266 wait_queue_head_t offline_waitq;
268 /* used to schedule release agent */
269 struct work_struct release_agent_work;
272 #define MAX_CGROUP_ROOT_NAMELEN 64
274 /* cgroup_root->flags */
276 CGRP_ROOT_SANE_BEHAVIOR = (1 << 0), /* __DEVEL__sane_behavior specified */
277 CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */
278 CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */
282 * A cgroup_root represents the root of a cgroup hierarchy, and may be
283 * associated with a kernfs_root to form an active hierarchy. This is
284 * internal to cgroup core. Don't access directly from controllers.
287 struct kernfs_root *kf_root;
289 /* The bitmask of subsystems attached to this hierarchy */
290 unsigned int subsys_mask;
292 /* Unique id for this hierarchy. */
295 /* The root cgroup. Root is destroyed on its release. */
298 /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
301 /* A list running through the active hierarchies */
302 struct list_head root_list;
304 /* Hierarchy-specific flags */
307 /* IDs for cgroups in this hierarchy */
308 struct idr cgroup_idr;
310 /* The path to use for release notifications. */
311 char release_agent_path[PATH_MAX];
313 /* The name for this hierarchy - may be empty */
314 char name[MAX_CGROUP_ROOT_NAMELEN];
318 * A css_set is a structure holding pointers to a set of
319 * cgroup_subsys_state objects. This saves space in the task struct
320 * object and speeds up fork()/exit(), since a single inc/dec and a
321 * list_add()/del() can bump the reference count on the entire cgroup
327 /* Reference count */
331 * List running through all cgroup groups in the same hash
332 * slot. Protected by css_set_lock
334 struct hlist_node hlist;
337 * Lists running through all tasks using this cgroup group.
338 * mg_tasks lists tasks which belong to this cset but are in the
339 * process of being migrated out or in. Protected by
340 * css_set_rwsem, but, during migration, once tasks are moved to
341 * mg_tasks, it can be read safely while holding cgroup_mutex.
343 struct list_head tasks;
344 struct list_head mg_tasks;
347 * List of cgrp_cset_links pointing at cgroups referenced from this
348 * css_set. Protected by css_set_lock.
350 struct list_head cgrp_links;
352 /* the default cgroup associated with this css_set */
353 struct cgroup *dfl_cgrp;
356 * Set of subsystem states, one for each subsystem. This array is
357 * immutable after creation apart from the init_css_set during
358 * subsystem registration (at boot time).
360 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
363 * List of csets participating in the on-going migration either as
364 * source or destination. Protected by cgroup_mutex.
366 struct list_head mg_preload_node;
367 struct list_head mg_node;
370 * If this cset is acting as the source of migration the following
371 * two fields are set. mg_src_cgrp is the source cgroup of the
372 * on-going migration and mg_dst_cset is the destination cset the
373 * target tasks on this cset should be migrated to. Protected by
376 struct cgroup *mg_src_cgrp;
377 struct css_set *mg_dst_cset;
380 * On the default hierarhcy, ->subsys[ssid] may point to a css
381 * attached to an ancestor instead of the cgroup this css_set is
382 * associated with. The following node is anchored at
383 * ->subsys[ssid]->cgroup->e_csets[ssid] and provides a way to
384 * iterate through all css's attached to a given cgroup.
386 struct list_head e_cset_node[CGROUP_SUBSYS_COUNT];
388 /* For RCU-protected deletion */
389 struct rcu_head rcu_head;
393 * struct cftype: handler definitions for cgroup control files
395 * When reading/writing to a file:
396 * - the cgroup to use is file->f_path.dentry->d_parent->d_fsdata
397 * - the 'cftype' of the file is file->f_path.dentry->d_fsdata
402 CFTYPE_ONLY_ON_ROOT = (1 << 0), /* only create on root cgrp */
403 CFTYPE_NOT_ON_ROOT = (1 << 1), /* don't create on root cgrp */
404 CFTYPE_NO_PREFIX = (1 << 3), /* (DON'T USE FOR NEW FILES) no subsys prefix */
406 /* internal flags, do not use outside cgroup core proper */
407 __CFTYPE_ONLY_ON_DFL = (1 << 16), /* only on default hierarchy */
408 __CFTYPE_NOT_ON_DFL = (1 << 17), /* not on default hierarchy */
411 #define MAX_CFTYPE_NAME 64
415 * By convention, the name should begin with the name of the
416 * subsystem, followed by a period. Zero length string indicates
417 * end of cftype array.
419 char name[MAX_CFTYPE_NAME];
422 * If not 0, file mode is set to this value, otherwise it will
423 * be figured out automatically
428 * The maximum length of string, excluding trailing nul, that can
429 * be passed to write. If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed.
431 size_t max_write_len;
437 * Fields used for internal bookkeeping. Initialized automatically
438 * during registration.
440 struct cgroup_subsys *ss; /* NULL for cgroup core files */
441 struct list_head node; /* anchored at ss->cfts */
442 struct kernfs_ops *kf_ops;
445 * read_u64() is a shortcut for the common case of returning a
446 * single integer. Use it in place of read()
448 u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
450 * read_s64() is a signed version of read_u64()
452 s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
454 /* generic seq_file read interface */
455 int (*seq_show)(struct seq_file *sf, void *v);
457 /* optional ops, implement all or none */
458 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
459 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
460 void (*seq_stop)(struct seq_file *sf, void *v);
463 * write_u64() is a shortcut for the common case of accepting
464 * a single integer (as parsed by simple_strtoull) from
465 * userspace. Use in place of write(); return 0 or error.
467 int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
470 * write_s64() is a signed version of write_u64()
472 int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
476 * write() is the generic write callback which maps directly to
477 * kernfs write operation and overrides all other operations.
478 * Maximum write size is determined by ->max_write_len. Use
479 * of_css/cft() to access the associated css and cft.
481 ssize_t (*write)(struct kernfs_open_file *of,
482 char *buf, size_t nbytes, loff_t off);
484 #ifdef CONFIG_DEBUG_LOCK_ALLOC
485 struct lock_class_key lockdep_key;
489 extern struct cgroup_root cgrp_dfl_root;
490 extern struct css_set init_css_set;
493 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
494 * @cgrp: the cgroup of interest
496 * The default hierarchy is the v2 interface of cgroup and this function
497 * can be used to test whether a cgroup is on the default hierarchy for
498 * cases where a subsystem should behave differnetly depending on the
501 * The set of behaviors which change on the default hierarchy are still
502 * being determined and the mount option is prefixed with __DEVEL__.
504 * List of changed behaviors:
506 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
507 * and "name" are disallowed.
509 * - When mounting an existing superblock, mount options should match.
511 * - Remount is disallowed.
513 * - rename(2) is disallowed.
515 * - "tasks" is removed. Everything should be at process granularity. Use
516 * "cgroup.procs" instead.
518 * - "cgroup.procs" is not sorted. pids will be unique unless they got
519 * recycled inbetween reads.
521 * - "release_agent" and "notify_on_release" are removed. Replacement
522 * notification mechanism will be implemented.
524 * - "cgroup.clone_children" is removed.
526 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
527 * and its descendants contain no task; otherwise, 1. The file also
528 * generates kernfs notification which can be monitored through poll and
529 * [di]notify when the value of the file changes.
531 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
532 * take masks of ancestors with non-empty cpus/mems, instead of being
533 * moved to an ancestor.
535 * - cpuset: a task can be moved into an empty cpuset, and again it takes
536 * masks of ancestors.
538 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
541 * - blkcg: blk-throttle becomes properly hierarchical.
543 * - debug: disallowed on the default hierarchy.
545 static inline bool cgroup_on_dfl(const struct cgroup *cgrp)
547 return cgrp->root == &cgrp_dfl_root;
550 /* no synchronization, the result can only be used as a hint */
551 static inline bool cgroup_has_tasks(struct cgroup *cgrp)
553 return !list_empty(&cgrp->cset_links);
556 /* returns ino associated with a cgroup */
557 static inline ino_t cgroup_ino(struct cgroup *cgrp)
559 return cgrp->kn->ino;
562 /* cft/css accessors for cftype->write() operation */
563 static inline struct cftype *of_cft(struct kernfs_open_file *of)
568 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
570 /* cft/css accessors for cftype->seq_*() operations */
571 static inline struct cftype *seq_cft(struct seq_file *seq)
573 return of_cft(seq->private);
576 static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
578 return of_css(seq->private);
582 * Name / path handling functions. All are thin wrappers around the kernfs
583 * counterparts and can be called under any context.
586 static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
588 return kernfs_name(cgrp->kn, buf, buflen);
591 static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
594 return kernfs_path(cgrp->kn, buf, buflen);
597 static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
599 pr_cont_kernfs_name(cgrp->kn);
602 static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
604 pr_cont_kernfs_path(cgrp->kn);
607 char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
609 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
610 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
611 int cgroup_rm_cftypes(struct cftype *cfts);
613 bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
616 * Control Group taskset, used to pass around set of tasks to cgroup_subsys
619 struct cgroup_taskset;
620 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
621 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
624 * cgroup_taskset_for_each - iterate cgroup_taskset
625 * @task: the loop cursor
626 * @tset: taskset to iterate
628 #define cgroup_taskset_for_each(task, tset) \
629 for ((task) = cgroup_taskset_first((tset)); (task); \
630 (task) = cgroup_taskset_next((tset)))
633 * Control Group subsystem type.
634 * See Documentation/cgroups/cgroups.txt for details
637 struct cgroup_subsys {
638 struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
639 int (*css_online)(struct cgroup_subsys_state *css);
640 void (*css_offline)(struct cgroup_subsys_state *css);
641 void (*css_free)(struct cgroup_subsys_state *css);
642 void (*css_reset)(struct cgroup_subsys_state *css);
644 int (*can_attach)(struct cgroup_subsys_state *css,
645 struct cgroup_taskset *tset);
646 void (*cancel_attach)(struct cgroup_subsys_state *css,
647 struct cgroup_taskset *tset);
648 void (*attach)(struct cgroup_subsys_state *css,
649 struct cgroup_taskset *tset);
650 void (*fork)(struct task_struct *task);
651 void (*exit)(struct cgroup_subsys_state *css,
652 struct cgroup_subsys_state *old_css,
653 struct task_struct *task);
654 void (*bind)(struct cgroup_subsys_state *root_css);
660 * If %false, this subsystem is properly hierarchical -
661 * configuration, resource accounting and restriction on a parent
662 * cgroup cover those of its children. If %true, hierarchy support
663 * is broken in some ways - some subsystems ignore hierarchy
664 * completely while others are only implemented half-way.
666 * It's now disallowed to create nested cgroups if the subsystem is
667 * broken and cgroup core will emit a warning message on such
668 * cases. Eventually, all subsystems will be made properly
669 * hierarchical and this will go away.
671 bool broken_hierarchy;
672 bool warned_broken_hierarchy;
674 /* the following two fields are initialized automtically during boot */
676 #define MAX_CGROUP_TYPE_NAMELEN 32
679 /* link to parent, protected by cgroup_lock() */
680 struct cgroup_root *root;
682 /* idr for css->id */
686 * List of cftypes. Each entry is the first entry of an array
687 * terminated by zero length name.
689 struct list_head cfts;
692 * Base cftypes which are automatically registered. The two can
693 * point to the same array.
695 struct cftype *dfl_cftypes; /* for the default hierarchy */
696 struct cftype *legacy_cftypes; /* for the legacy hierarchies */
699 * A subsystem may depend on other subsystems. When such subsystem
700 * is enabled on a cgroup, the depended-upon subsystems are enabled
701 * together if available. Subsystems enabled due to dependency are
702 * not visible to userland until explicitly enabled. The following
703 * specifies the mask of subsystems that this one depends on.
705 unsigned int depends_on;
708 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
709 #include <linux/cgroup_subsys.h>
713 * task_css_set_check - obtain a task's css_set with extra access conditions
714 * @task: the task to obtain css_set for
715 * @__c: extra condition expression to be passed to rcu_dereference_check()
717 * A task's css_set is RCU protected, initialized and exited while holding
718 * task_lock(), and can only be modified while holding both cgroup_mutex
719 * and task_lock() while the task is alive. This macro verifies that the
720 * caller is inside proper critical section and returns @task's css_set.
722 * The caller can also specify additional allowed conditions via @__c, such
723 * as locks used during the cgroup_subsys::attach() methods.
725 #ifdef CONFIG_PROVE_RCU
726 extern struct mutex cgroup_mutex;
727 extern struct rw_semaphore css_set_rwsem;
728 #define task_css_set_check(task, __c) \
729 rcu_dereference_check((task)->cgroups, \
730 lockdep_is_held(&cgroup_mutex) || \
731 lockdep_is_held(&css_set_rwsem) || \
732 ((task)->flags & PF_EXITING) || (__c))
734 #define task_css_set_check(task, __c) \
735 rcu_dereference((task)->cgroups)
739 * task_css_check - obtain css for (task, subsys) w/ extra access conds
740 * @task: the target task
741 * @subsys_id: the target subsystem ID
742 * @__c: extra condition expression to be passed to rcu_dereference_check()
744 * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
745 * synchronization rules are the same as task_css_set_check().
747 #define task_css_check(task, subsys_id, __c) \
748 task_css_set_check((task), (__c))->subsys[(subsys_id)]
751 * task_css_set - obtain a task's css_set
752 * @task: the task to obtain css_set for
754 * See task_css_set_check().
756 static inline struct css_set *task_css_set(struct task_struct *task)
758 return task_css_set_check(task, false);
762 * task_css - obtain css for (task, subsys)
763 * @task: the target task
764 * @subsys_id: the target subsystem ID
766 * See task_css_check().
768 static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
771 return task_css_check(task, subsys_id, false);
775 * task_css_is_root - test whether a task belongs to the root css
776 * @task: the target task
777 * @subsys_id: the target subsystem ID
779 * Test whether @task belongs to the root css on the specified subsystem.
780 * May be invoked in any context.
782 static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
784 return task_css_check(task, subsys_id, true) ==
785 init_css_set.subsys[subsys_id];
788 static inline struct cgroup *task_cgroup(struct task_struct *task,
791 return task_css(task, subsys_id)->cgroup;
794 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
795 struct cgroup_subsys_state *parent);
797 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
800 * css_for_each_child - iterate through children of a css
801 * @pos: the css * to use as the loop cursor
802 * @parent: css whose children to walk
804 * Walk @parent's children. Must be called under rcu_read_lock().
806 * If a subsystem synchronizes ->css_online() and the start of iteration, a
807 * css which finished ->css_online() is guaranteed to be visible in the
808 * future iterations and will stay visible until the last reference is put.
809 * A css which hasn't finished ->css_online() or already finished
810 * ->css_offline() may show up during traversal. It's each subsystem's
811 * responsibility to synchronize against on/offlining.
813 * It is allowed to temporarily drop RCU read lock during iteration. The
814 * caller is responsible for ensuring that @pos remains accessible until
815 * the start of the next iteration by, for example, bumping the css refcnt.
817 #define css_for_each_child(pos, parent) \
818 for ((pos) = css_next_child(NULL, (parent)); (pos); \
819 (pos) = css_next_child((pos), (parent)))
821 struct cgroup_subsys_state *
822 css_next_descendant_pre(struct cgroup_subsys_state *pos,
823 struct cgroup_subsys_state *css);
825 struct cgroup_subsys_state *
826 css_rightmost_descendant(struct cgroup_subsys_state *pos);
829 * css_for_each_descendant_pre - pre-order walk of a css's descendants
830 * @pos: the css * to use as the loop cursor
831 * @root: css whose descendants to walk
833 * Walk @root's descendants. @root is included in the iteration and the
834 * first node to be visited. Must be called under rcu_read_lock().
836 * If a subsystem synchronizes ->css_online() and the start of iteration, a
837 * css which finished ->css_online() is guaranteed to be visible in the
838 * future iterations and will stay visible until the last reference is put.
839 * A css which hasn't finished ->css_online() or already finished
840 * ->css_offline() may show up during traversal. It's each subsystem's
841 * responsibility to synchronize against on/offlining.
843 * For example, the following guarantees that a descendant can't escape
844 * state updates of its ancestors.
848 * Lock @css's parent and @css;
849 * Inherit state from the parent;
853 * my_update_state(@css)
855 * css_for_each_descendant_pre(@pos, @css) {
858 * Update @css's state;
860 * Verify @pos is alive and inherit state from its parent;
865 * As long as the inheriting step, including checking the parent state, is
866 * enclosed inside @pos locking, double-locking the parent isn't necessary
867 * while inheriting. The state update to the parent is guaranteed to be
868 * visible by walking order and, as long as inheriting operations to the
869 * same @pos are atomic to each other, multiple updates racing each other
870 * still result in the correct state. It's guaranateed that at least one
871 * inheritance happens for any css after the latest update to its parent.
873 * If checking parent's state requires locking the parent, each inheriting
874 * iteration should lock and unlock both @pos->parent and @pos.
876 * Alternatively, a subsystem may choose to use a single global lock to
877 * synchronize ->css_online() and ->css_offline() against tree-walking
880 * It is allowed to temporarily drop RCU read lock during iteration. The
881 * caller is responsible for ensuring that @pos remains accessible until
882 * the start of the next iteration by, for example, bumping the css refcnt.
884 #define css_for_each_descendant_pre(pos, css) \
885 for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
886 (pos) = css_next_descendant_pre((pos), (css)))
888 struct cgroup_subsys_state *
889 css_next_descendant_post(struct cgroup_subsys_state *pos,
890 struct cgroup_subsys_state *css);
893 * css_for_each_descendant_post - post-order walk of a css's descendants
894 * @pos: the css * to use as the loop cursor
895 * @css: css whose descendants to walk
897 * Similar to css_for_each_descendant_pre() but performs post-order
898 * traversal instead. @root is included in the iteration and the last
899 * node to be visited.
901 * If a subsystem synchronizes ->css_online() and the start of iteration, a
902 * css which finished ->css_online() is guaranteed to be visible in the
903 * future iterations and will stay visible until the last reference is put.
904 * A css which hasn't finished ->css_online() or already finished
905 * ->css_offline() may show up during traversal. It's each subsystem's
906 * responsibility to synchronize against on/offlining.
908 * Note that the walk visibility guarantee example described in pre-order
909 * walk doesn't apply the same to post-order walks.
911 #define css_for_each_descendant_post(pos, css) \
912 for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
913 (pos) = css_next_descendant_post((pos), (css)))
915 bool css_has_online_children(struct cgroup_subsys_state *css);
917 /* A css_task_iter should be treated as an opaque object */
918 struct css_task_iter {
919 struct cgroup_subsys *ss;
921 struct list_head *cset_pos;
922 struct list_head *cset_head;
924 struct list_head *task_pos;
925 struct list_head *tasks_head;
926 struct list_head *mg_tasks_head;
929 void css_task_iter_start(struct cgroup_subsys_state *css,
930 struct css_task_iter *it);
931 struct task_struct *css_task_iter_next(struct css_task_iter *it);
932 void css_task_iter_end(struct css_task_iter *it);
934 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
935 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
937 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
938 struct cgroup_subsys *ss);
940 #else /* !CONFIG_CGROUPS */
942 static inline int cgroup_init_early(void) { return 0; }
943 static inline int cgroup_init(void) { return 0; }
944 static inline void cgroup_fork(struct task_struct *p) {}
945 static inline void cgroup_post_fork(struct task_struct *p) {}
946 static inline void cgroup_exit(struct task_struct *p) {}
948 static inline int cgroupstats_build(struct cgroupstats *stats,
949 struct dentry *dentry)
954 /* No cgroups - nothing to do */
955 static inline int cgroup_attach_task_all(struct task_struct *from,
956 struct task_struct *t)
961 #endif /* !CONFIG_CGROUPS */
963 #endif /* _LINUX_CGROUP_H */