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/rculist.h>
15 #include <linux/cgroupstats.h>
16 #include <linux/rwsem.h>
18 #include <linux/seq_file.h>
19 #include <linux/kernfs.h>
21 #include <linux/cgroup-defs.h>
25 /* a css_task_iter should be treated as an opaque object */
26 struct css_task_iter {
27 struct cgroup_subsys *ss;
29 struct list_head *cset_pos;
30 struct list_head *cset_head;
32 struct list_head *task_pos;
33 struct list_head *tasks_head;
34 struct list_head *mg_tasks_head;
37 extern struct cgroup_root cgrp_dfl_root;
38 extern struct css_set init_css_set;
40 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
41 #include <linux/cgroup_subsys.h>
44 bool css_has_online_children(struct cgroup_subsys_state *css);
45 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
46 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
47 struct cgroup_subsys *ss);
48 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
49 struct cgroup_subsys *ss);
51 bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
52 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
53 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
55 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
56 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
57 int cgroup_rm_cftypes(struct cftype *cfts);
59 char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
60 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
61 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
62 struct pid *pid, struct task_struct *tsk);
64 void cgroup_fork(struct task_struct *p);
65 extern int cgroup_can_fork(struct task_struct *p,
66 void *ss_priv[CGROUP_CANFORK_COUNT]);
67 extern void cgroup_cancel_fork(struct task_struct *p,
68 void *ss_priv[CGROUP_CANFORK_COUNT]);
69 extern void cgroup_post_fork(struct task_struct *p,
70 void *old_ss_priv[CGROUP_CANFORK_COUNT]);
71 void cgroup_exit(struct task_struct *p);
73 int cgroup_init_early(void);
74 int cgroup_init(void);
77 * Iteration helpers and macros.
80 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
81 struct cgroup_subsys_state *parent);
82 struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
83 struct cgroup_subsys_state *css);
84 struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
85 struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
86 struct cgroup_subsys_state *css);
88 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
89 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
91 void css_task_iter_start(struct cgroup_subsys_state *css,
92 struct css_task_iter *it);
93 struct task_struct *css_task_iter_next(struct css_task_iter *it);
94 void css_task_iter_end(struct css_task_iter *it);
97 * css_for_each_child - iterate through children of a css
98 * @pos: the css * to use as the loop cursor
99 * @parent: css whose children to walk
101 * Walk @parent's children. Must be called under rcu_read_lock().
103 * If a subsystem synchronizes ->css_online() and the start of iteration, a
104 * css which finished ->css_online() is guaranteed to be visible in the
105 * future iterations and will stay visible until the last reference is put.
106 * A css which hasn't finished ->css_online() or already finished
107 * ->css_offline() may show up during traversal. It's each subsystem's
108 * responsibility to synchronize against on/offlining.
110 * It is allowed to temporarily drop RCU read lock during iteration. The
111 * caller is responsible for ensuring that @pos remains accessible until
112 * the start of the next iteration by, for example, bumping the css refcnt.
114 #define css_for_each_child(pos, parent) \
115 for ((pos) = css_next_child(NULL, (parent)); (pos); \
116 (pos) = css_next_child((pos), (parent)))
119 * css_for_each_descendant_pre - pre-order walk of a css's descendants
120 * @pos: the css * to use as the loop cursor
121 * @root: css whose descendants to walk
123 * Walk @root's descendants. @root is included in the iteration and the
124 * first node to be visited. Must be called under rcu_read_lock().
126 * If a subsystem synchronizes ->css_online() and the start of iteration, a
127 * css which finished ->css_online() is guaranteed to be visible in the
128 * future iterations and will stay visible until the last reference is put.
129 * A css which hasn't finished ->css_online() or already finished
130 * ->css_offline() may show up during traversal. It's each subsystem's
131 * responsibility to synchronize against on/offlining.
133 * For example, the following guarantees that a descendant can't escape
134 * state updates of its ancestors.
138 * Lock @css's parent and @css;
139 * Inherit state from the parent;
143 * my_update_state(@css)
145 * css_for_each_descendant_pre(@pos, @css) {
148 * Update @css's state;
150 * Verify @pos is alive and inherit state from its parent;
155 * As long as the inheriting step, including checking the parent state, is
156 * enclosed inside @pos locking, double-locking the parent isn't necessary
157 * while inheriting. The state update to the parent is guaranteed to be
158 * visible by walking order and, as long as inheriting operations to the
159 * same @pos are atomic to each other, multiple updates racing each other
160 * still result in the correct state. It's guaranateed that at least one
161 * inheritance happens for any css after the latest update to its parent.
163 * If checking parent's state requires locking the parent, each inheriting
164 * iteration should lock and unlock both @pos->parent and @pos.
166 * Alternatively, a subsystem may choose to use a single global lock to
167 * synchronize ->css_online() and ->css_offline() against tree-walking
170 * It is allowed to temporarily drop RCU read lock during iteration. The
171 * caller is responsible for ensuring that @pos remains accessible until
172 * the start of the next iteration by, for example, bumping the css refcnt.
174 #define css_for_each_descendant_pre(pos, css) \
175 for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
176 (pos) = css_next_descendant_pre((pos), (css)))
179 * css_for_each_descendant_post - post-order walk of a css's descendants
180 * @pos: the css * to use as the loop cursor
181 * @css: css whose descendants to walk
183 * Similar to css_for_each_descendant_pre() but performs post-order
184 * traversal instead. @root is included in the iteration and the last
185 * node to be visited.
187 * If a subsystem synchronizes ->css_online() and the start of iteration, a
188 * css which finished ->css_online() is guaranteed to be visible in the
189 * future iterations and will stay visible until the last reference is put.
190 * A css which hasn't finished ->css_online() or already finished
191 * ->css_offline() may show up during traversal. It's each subsystem's
192 * responsibility to synchronize against on/offlining.
194 * Note that the walk visibility guarantee example described in pre-order
195 * walk doesn't apply the same to post-order walks.
197 #define css_for_each_descendant_post(pos, css) \
198 for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
199 (pos) = css_next_descendant_post((pos), (css)))
202 * cgroup_taskset_for_each - iterate cgroup_taskset
203 * @task: the loop cursor
204 * @tset: taskset to iterate
206 #define cgroup_taskset_for_each(task, tset) \
207 for ((task) = cgroup_taskset_first((tset)); (task); \
208 (task) = cgroup_taskset_next((tset)))
215 * css_get - obtain a reference on the specified css
218 * The caller must already have a reference.
220 static inline void css_get(struct cgroup_subsys_state *css)
222 if (!(css->flags & CSS_NO_REF))
223 percpu_ref_get(&css->refcnt);
227 * css_get_many - obtain references on the specified css
229 * @n: number of references to get
231 * The caller must already have a reference.
233 static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
235 if (!(css->flags & CSS_NO_REF))
236 percpu_ref_get_many(&css->refcnt, n);
240 * css_tryget - try to obtain a reference on the specified css
243 * Obtain a reference on @css unless it already has reached zero and is
244 * being released. This function doesn't care whether @css is on or
245 * offline. The caller naturally needs to ensure that @css is accessible
246 * but doesn't have to be holding a reference on it - IOW, RCU protected
247 * access is good enough for this function. Returns %true if a reference
248 * count was successfully obtained; %false otherwise.
250 static inline bool css_tryget(struct cgroup_subsys_state *css)
252 if (!(css->flags & CSS_NO_REF))
253 return percpu_ref_tryget(&css->refcnt);
258 * css_tryget_online - try to obtain a reference on the specified css if online
261 * Obtain a reference on @css if it's online. The caller naturally needs
262 * to ensure that @css is accessible but doesn't have to be holding a
263 * reference on it - IOW, RCU protected access is good enough for this
264 * function. Returns %true if a reference count was successfully obtained;
267 static inline bool css_tryget_online(struct cgroup_subsys_state *css)
269 if (!(css->flags & CSS_NO_REF))
270 return percpu_ref_tryget_live(&css->refcnt);
275 * css_put - put a css reference
278 * Put a reference obtained via css_get() and css_tryget_online().
280 static inline void css_put(struct cgroup_subsys_state *css)
282 if (!(css->flags & CSS_NO_REF))
283 percpu_ref_put(&css->refcnt);
287 * css_put_many - put css references
289 * @n: number of references to put
291 * Put references obtained via css_get() and css_tryget_online().
293 static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
295 if (!(css->flags & CSS_NO_REF))
296 percpu_ref_put_many(&css->refcnt, n);
300 * task_css_set_check - obtain a task's css_set with extra access conditions
301 * @task: the task to obtain css_set for
302 * @__c: extra condition expression to be passed to rcu_dereference_check()
304 * A task's css_set is RCU protected, initialized and exited while holding
305 * task_lock(), and can only be modified while holding both cgroup_mutex
306 * and task_lock() while the task is alive. This macro verifies that the
307 * caller is inside proper critical section and returns @task's css_set.
309 * The caller can also specify additional allowed conditions via @__c, such
310 * as locks used during the cgroup_subsys::attach() methods.
312 #ifdef CONFIG_PROVE_RCU
313 extern struct mutex cgroup_mutex;
314 extern struct rw_semaphore css_set_rwsem;
315 #define task_css_set_check(task, __c) \
316 rcu_dereference_check((task)->cgroups, \
317 lockdep_is_held(&cgroup_mutex) || \
318 lockdep_is_held(&css_set_rwsem) || \
319 ((task)->flags & PF_EXITING) || (__c))
321 #define task_css_set_check(task, __c) \
322 rcu_dereference((task)->cgroups)
326 * task_css_check - obtain css for (task, subsys) w/ extra access conds
327 * @task: the target task
328 * @subsys_id: the target subsystem ID
329 * @__c: extra condition expression to be passed to rcu_dereference_check()
331 * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
332 * synchronization rules are the same as task_css_set_check().
334 #define task_css_check(task, subsys_id, __c) \
335 task_css_set_check((task), (__c))->subsys[(subsys_id)]
338 * task_css_set - obtain a task's css_set
339 * @task: the task to obtain css_set for
341 * See task_css_set_check().
343 static inline struct css_set *task_css_set(struct task_struct *task)
345 return task_css_set_check(task, false);
349 * task_css - obtain css for (task, subsys)
350 * @task: the target task
351 * @subsys_id: the target subsystem ID
353 * See task_css_check().
355 static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
358 return task_css_check(task, subsys_id, false);
362 * task_get_css - find and get the css for (task, subsys)
363 * @task: the target task
364 * @subsys_id: the target subsystem ID
366 * Find the css for the (@task, @subsys_id) combination, increment a
367 * reference on and return it. This function is guaranteed to return a
370 static inline struct cgroup_subsys_state *
371 task_get_css(struct task_struct *task, int subsys_id)
373 struct cgroup_subsys_state *css;
377 css = task_css(task, subsys_id);
378 if (likely(css_tryget_online(css)))
387 * task_css_is_root - test whether a task belongs to the root css
388 * @task: the target task
389 * @subsys_id: the target subsystem ID
391 * Test whether @task belongs to the root css on the specified subsystem.
392 * May be invoked in any context.
394 static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
396 return task_css_check(task, subsys_id, true) ==
397 init_css_set.subsys[subsys_id];
400 static inline struct cgroup *task_cgroup(struct task_struct *task,
403 return task_css(task, subsys_id)->cgroup;
407 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
408 * @cgrp: the cgroup of interest
410 * The default hierarchy is the v2 interface of cgroup and this function
411 * can be used to test whether a cgroup is on the default hierarchy for
412 * cases where a subsystem should behave differnetly depending on the
415 * The set of behaviors which change on the default hierarchy are still
416 * being determined and the mount option is prefixed with __DEVEL__.
418 * List of changed behaviors:
420 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
421 * and "name" are disallowed.
423 * - When mounting an existing superblock, mount options should match.
425 * - Remount is disallowed.
427 * - rename(2) is disallowed.
429 * - "tasks" is removed. Everything should be at process granularity. Use
430 * "cgroup.procs" instead.
432 * - "cgroup.procs" is not sorted. pids will be unique unless they got
433 * recycled inbetween reads.
435 * - "release_agent" and "notify_on_release" are removed. Replacement
436 * notification mechanism will be implemented.
438 * - "cgroup.clone_children" is removed.
440 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
441 * and its descendants contain no task; otherwise, 1. The file also
442 * generates kernfs notification which can be monitored through poll and
443 * [di]notify when the value of the file changes.
445 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
446 * take masks of ancestors with non-empty cpus/mems, instead of being
447 * moved to an ancestor.
449 * - cpuset: a task can be moved into an empty cpuset, and again it takes
450 * masks of ancestors.
452 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
455 * - blkcg: blk-throttle becomes properly hierarchical.
457 * - debug: disallowed on the default hierarchy.
459 static inline bool cgroup_on_dfl(const struct cgroup *cgrp)
461 return cgrp->root == &cgrp_dfl_root;
464 /* no synchronization, the result can only be used as a hint */
465 static inline bool cgroup_has_tasks(struct cgroup *cgrp)
467 return !list_empty(&cgrp->cset_links);
470 /* returns ino associated with a cgroup */
471 static inline ino_t cgroup_ino(struct cgroup *cgrp)
473 return cgrp->kn->ino;
476 /* cft/css accessors for cftype->write() operation */
477 static inline struct cftype *of_cft(struct kernfs_open_file *of)
482 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
484 /* cft/css accessors for cftype->seq_*() operations */
485 static inline struct cftype *seq_cft(struct seq_file *seq)
487 return of_cft(seq->private);
490 static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
492 return of_css(seq->private);
496 * Name / path handling functions. All are thin wrappers around the kernfs
497 * counterparts and can be called under any context.
500 static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
502 return kernfs_name(cgrp->kn, buf, buflen);
505 static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
508 return kernfs_path(cgrp->kn, buf, buflen);
511 static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
513 pr_cont_kernfs_name(cgrp->kn);
516 static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
518 pr_cont_kernfs_path(cgrp->kn);
521 #else /* !CONFIG_CGROUPS */
523 struct cgroup_subsys_state;
525 static inline void css_put(struct cgroup_subsys_state *css) {}
526 static inline int cgroup_attach_task_all(struct task_struct *from,
527 struct task_struct *t) { return 0; }
528 static inline int cgroupstats_build(struct cgroupstats *stats,
529 struct dentry *dentry) { return -EINVAL; }
531 static inline void cgroup_fork(struct task_struct *p) {}
532 static inline int cgroup_can_fork(struct task_struct *p,
533 void *ss_priv[CGROUP_CANFORK_COUNT])
535 static inline void cgroup_cancel_fork(struct task_struct *p,
536 void *ss_priv[CGROUP_CANFORK_COUNT]) {}
537 static inline void cgroup_post_fork(struct task_struct *p,
538 void *ss_priv[CGROUP_CANFORK_COUNT]) {}
539 static inline void cgroup_exit(struct task_struct *p) {}
541 static inline int cgroup_init_early(void) { return 0; }
542 static inline int cgroup_init(void) { return 0; }
544 #endif /* !CONFIG_CGROUPS */
546 #endif /* _LINUX_CGROUP_H */