blob: 82319fb31cfe89a959f1e763c5288a8269e6516a [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001#ifndef _LINUX_CGROUP_H
2#define _LINUX_CGROUP_H
3/*
4 * cgroup interface
5 *
6 * Copyright (C) 2003 BULL SA
7 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
8 *
9 */
10
11#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070012#include <linux/cpumask.h>
13#include <linux/nodemask.h>
Tejun Heoeb6fd502012-11-09 09:12:29 -080014#include <linux/rculist.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070015#include <linux/cgroupstats.h>
Paul Menagecc31edc2008-10-18 20:28:04 -070016#include <linux/rwsem.h>
Tejun Heo25a7e682013-04-14 20:15:25 -070017#include <linux/fs.h>
Tejun Heo7da11272013-12-05 12:28:04 -050018#include <linux/seq_file.h>
Tejun Heo2bd59d42014-02-11 11:52:49 -050019#include <linux/kernfs.h>
Tejun Heob4a04ab2015-05-13 15:38:40 -040020
21#include <linux/cgroup-defs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070022
23#ifdef CONFIG_CGROUPS
24
Tejun Heoc326aa22015-05-13 16:24:16 -040025/* a css_task_iter should be treated as an opaque object */
26struct css_task_iter {
27 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -070028
Tejun Heoc326aa22015-05-13 16:24:16 -040029 struct list_head *cset_pos;
30 struct list_head *cset_head;
31
32 struct list_head *task_pos;
33 struct list_head *tasks_head;
34 struct list_head *mg_tasks_head;
35};
36
37extern struct cgroup_root cgrp_dfl_root;
38extern struct css_set init_css_set;
39
40#define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
41#include <linux/cgroup_subsys.h>
42#undef SUBSYS
43
44bool css_has_online_children(struct cgroup_subsys_state *css);
45struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
46struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
47 struct cgroup_subsys *ss);
48struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
49 struct cgroup_subsys *ss);
50
51bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
52int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
53int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
54
55int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
56int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
57int cgroup_rm_cftypes(struct cftype *cfts);
58
59char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
60int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
61int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
62 struct pid *pid, struct task_struct *tsk);
63
64void cgroup_fork(struct task_struct *p);
65void cgroup_post_fork(struct task_struct *p);
66void cgroup_exit(struct task_struct *p);
67
68int cgroup_init_early(void);
69int cgroup_init(void);
70
71/*
72 * Iteration helpers and macros.
73 */
74
75struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
76 struct cgroup_subsys_state *parent);
77struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
78 struct cgroup_subsys_state *css);
79struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
80struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
81 struct cgroup_subsys_state *css);
82
83struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
84struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
85
86void css_task_iter_start(struct cgroup_subsys_state *css,
87 struct css_task_iter *it);
88struct task_struct *css_task_iter_next(struct css_task_iter *it);
89void css_task_iter_end(struct css_task_iter *it);
90
91/**
92 * css_for_each_child - iterate through children of a css
93 * @pos: the css * to use as the loop cursor
94 * @parent: css whose children to walk
95 *
96 * Walk @parent's children. Must be called under rcu_read_lock().
97 *
98 * If a subsystem synchronizes ->css_online() and the start of iteration, a
99 * css which finished ->css_online() is guaranteed to be visible in the
100 * future iterations and will stay visible until the last reference is put.
101 * A css which hasn't finished ->css_online() or already finished
102 * ->css_offline() may show up during traversal. It's each subsystem's
103 * responsibility to synchronize against on/offlining.
104 *
105 * It is allowed to temporarily drop RCU read lock during iteration. The
106 * caller is responsible for ensuring that @pos remains accessible until
107 * the start of the next iteration by, for example, bumping the css refcnt.
108 */
109#define css_for_each_child(pos, parent) \
110 for ((pos) = css_next_child(NULL, (parent)); (pos); \
111 (pos) = css_next_child((pos), (parent)))
112
113/**
114 * css_for_each_descendant_pre - pre-order walk of a css's descendants
115 * @pos: the css * to use as the loop cursor
116 * @root: css whose descendants to walk
117 *
118 * Walk @root's descendants. @root is included in the iteration and the
119 * first node to be visited. Must be called under rcu_read_lock().
120 *
121 * If a subsystem synchronizes ->css_online() and the start of iteration, a
122 * css which finished ->css_online() is guaranteed to be visible in the
123 * future iterations and will stay visible until the last reference is put.
124 * A css which hasn't finished ->css_online() or already finished
125 * ->css_offline() may show up during traversal. It's each subsystem's
126 * responsibility to synchronize against on/offlining.
127 *
128 * For example, the following guarantees that a descendant can't escape
129 * state updates of its ancestors.
130 *
131 * my_online(@css)
132 * {
133 * Lock @css's parent and @css;
134 * Inherit state from the parent;
135 * Unlock both.
136 * }
137 *
138 * my_update_state(@css)
139 * {
140 * css_for_each_descendant_pre(@pos, @css) {
141 * Lock @pos;
142 * if (@pos == @css)
143 * Update @css's state;
144 * else
145 * Verify @pos is alive and inherit state from its parent;
146 * Unlock @pos;
147 * }
148 * }
149 *
150 * As long as the inheriting step, including checking the parent state, is
151 * enclosed inside @pos locking, double-locking the parent isn't necessary
152 * while inheriting. The state update to the parent is guaranteed to be
153 * visible by walking order and, as long as inheriting operations to the
154 * same @pos are atomic to each other, multiple updates racing each other
155 * still result in the correct state. It's guaranateed that at least one
156 * inheritance happens for any css after the latest update to its parent.
157 *
158 * If checking parent's state requires locking the parent, each inheriting
159 * iteration should lock and unlock both @pos->parent and @pos.
160 *
161 * Alternatively, a subsystem may choose to use a single global lock to
162 * synchronize ->css_online() and ->css_offline() against tree-walking
163 * operations.
164 *
165 * It is allowed to temporarily drop RCU read lock during iteration. The
166 * caller is responsible for ensuring that @pos remains accessible until
167 * the start of the next iteration by, for example, bumping the css refcnt.
168 */
169#define css_for_each_descendant_pre(pos, css) \
170 for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
171 (pos) = css_next_descendant_pre((pos), (css)))
172
173/**
174 * css_for_each_descendant_post - post-order walk of a css's descendants
175 * @pos: the css * to use as the loop cursor
176 * @css: css whose descendants to walk
177 *
178 * Similar to css_for_each_descendant_pre() but performs post-order
179 * traversal instead. @root is included in the iteration and the last
180 * node to be visited.
181 *
182 * If a subsystem synchronizes ->css_online() and the start of iteration, a
183 * css which finished ->css_online() is guaranteed to be visible in the
184 * future iterations and will stay visible until the last reference is put.
185 * A css which hasn't finished ->css_online() or already finished
186 * ->css_offline() may show up during traversal. It's each subsystem's
187 * responsibility to synchronize against on/offlining.
188 *
189 * Note that the walk visibility guarantee example described in pre-order
190 * walk doesn't apply the same to post-order walks.
191 */
192#define css_for_each_descendant_post(pos, css) \
193 for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
194 (pos) = css_next_descendant_post((pos), (css)))
195
196/**
197 * cgroup_taskset_for_each - iterate cgroup_taskset
198 * @task: the loop cursor
199 * @tset: taskset to iterate
200 */
201#define cgroup_taskset_for_each(task, tset) \
202 for ((task) = cgroup_taskset_first((tset)); (task); \
203 (task) = cgroup_taskset_next((tset)))
204
205/*
206 * Inline functions.
207 */
Paul Menagea4243162007-10-18 23:39:35 -0700208
Tejun Heo5de01072013-06-12 21:04:52 -0700209/**
210 * css_get - obtain a reference on the specified css
211 * @css: target css
212 *
213 * The caller must already have a reference.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700214 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215static inline void css_get(struct cgroup_subsys_state *css)
216{
Tejun Heo3b514d22014-05-16 13:22:47 -0400217 if (!(css->flags & CSS_NO_REF))
218 percpu_ref_get(&css->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700219}
Paul Menagee7c5ec92009-01-07 18:08:38 -0800220
Tejun Heo5de01072013-06-12 21:04:52 -0700221/**
Johannes Weinere8ea14c2014-12-10 15:42:42 -0800222 * css_get_many - obtain references on the specified css
223 * @css: target css
224 * @n: number of references to get
225 *
226 * The caller must already have a reference.
227 */
228static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
229{
230 if (!(css->flags & CSS_NO_REF))
231 percpu_ref_get_many(&css->refcnt, n);
232}
233
234/**
Tejun Heo6f4524d2014-05-16 13:22:52 -0400235 * css_tryget - try to obtain a reference on the specified css
236 * @css: target css
237 *
238 * Obtain a reference on @css unless it already has reached zero and is
239 * being released. This function doesn't care whether @css is on or
240 * offline. The caller naturally needs to ensure that @css is accessible
241 * but doesn't have to be holding a reference on it - IOW, RCU protected
242 * access is good enough for this function. Returns %true if a reference
243 * count was successfully obtained; %false otherwise.
244 */
245static inline bool css_tryget(struct cgroup_subsys_state *css)
246{
247 if (!(css->flags & CSS_NO_REF))
248 return percpu_ref_tryget(&css->refcnt);
249 return true;
250}
251
252/**
Tejun Heoec903c02014-05-13 12:11:01 -0400253 * css_tryget_online - try to obtain a reference on the specified css if online
Tejun Heo5de01072013-06-12 21:04:52 -0700254 * @css: target css
255 *
Tejun Heoec903c02014-05-13 12:11:01 -0400256 * Obtain a reference on @css if it's online. The caller naturally needs
257 * to ensure that @css is accessible but doesn't have to be holding a
Tejun Heo5de01072013-06-12 21:04:52 -0700258 * reference on it - IOW, RCU protected access is good enough for this
259 * function. Returns %true if a reference count was successfully obtained;
260 * %false otherwise.
261 */
Tejun Heoec903c02014-05-13 12:11:01 -0400262static inline bool css_tryget_online(struct cgroup_subsys_state *css)
Paul Menagee7c5ec92009-01-07 18:08:38 -0800263{
Tejun Heo3b514d22014-05-16 13:22:47 -0400264 if (!(css->flags & CSS_NO_REF))
265 return percpu_ref_tryget_live(&css->refcnt);
266 return true;
Paul Menagee7c5ec92009-01-07 18:08:38 -0800267}
268
Tejun Heo5de01072013-06-12 21:04:52 -0700269/**
270 * css_put - put a css reference
271 * @css: target css
272 *
Tejun Heoec903c02014-05-13 12:11:01 -0400273 * Put a reference obtained via css_get() and css_tryget_online().
Tejun Heo5de01072013-06-12 21:04:52 -0700274 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700275static inline void css_put(struct cgroup_subsys_state *css)
276{
Tejun Heo3b514d22014-05-16 13:22:47 -0400277 if (!(css->flags & CSS_NO_REF))
278 percpu_ref_put(&css->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700279}
280
Johannes Weinere8ea14c2014-12-10 15:42:42 -0800281/**
282 * css_put_many - put css references
283 * @css: target css
284 * @n: number of references to put
285 *
286 * Put references obtained via css_get() and css_tryget_online().
287 */
288static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
289{
290 if (!(css->flags & CSS_NO_REF))
291 percpu_ref_put_many(&css->refcnt, n);
292}
293
Tejun Heoc326aa22015-05-13 16:24:16 -0400294/**
295 * task_css_set_check - obtain a task's css_set with extra access conditions
296 * @task: the task to obtain css_set for
297 * @__c: extra condition expression to be passed to rcu_dereference_check()
298 *
299 * A task's css_set is RCU protected, initialized and exited while holding
300 * task_lock(), and can only be modified while holding both cgroup_mutex
301 * and task_lock() while the task is alive. This macro verifies that the
302 * caller is inside proper critical section and returns @task's css_set.
303 *
304 * The caller can also specify additional allowed conditions via @__c, such
305 * as locks used during the cgroup_subsys::attach() methods.
306 */
307#ifdef CONFIG_PROVE_RCU
308extern struct mutex cgroup_mutex;
309extern struct rw_semaphore css_set_rwsem;
310#define task_css_set_check(task, __c) \
311 rcu_dereference_check((task)->cgroups, \
312 lockdep_is_held(&cgroup_mutex) || \
313 lockdep_is_held(&css_set_rwsem) || \
314 ((task)->flags & PF_EXITING) || (__c))
315#else
316#define task_css_set_check(task, __c) \
317 rcu_dereference((task)->cgroups)
318#endif
319
320/**
321 * task_css_check - obtain css for (task, subsys) w/ extra access conds
322 * @task: the target task
323 * @subsys_id: the target subsystem ID
324 * @__c: extra condition expression to be passed to rcu_dereference_check()
325 *
326 * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
327 * synchronization rules are the same as task_css_set_check().
328 */
329#define task_css_check(task, subsys_id, __c) \
330 task_css_set_check((task), (__c))->subsys[(subsys_id)]
331
332/**
333 * task_css_set - obtain a task's css_set
334 * @task: the task to obtain css_set for
335 *
336 * See task_css_set_check().
337 */
338static inline struct css_set *task_css_set(struct task_struct *task)
339{
340 return task_css_set_check(task, false);
341}
342
343/**
344 * task_css - obtain css for (task, subsys)
345 * @task: the target task
346 * @subsys_id: the target subsystem ID
347 *
348 * See task_css_check().
349 */
350static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
351 int subsys_id)
352{
353 return task_css_check(task, subsys_id, false);
354}
355
356/**
357 * task_css_is_root - test whether a task belongs to the root css
358 * @task: the target task
359 * @subsys_id: the target subsystem ID
360 *
361 * Test whether @task belongs to the root css on the specified subsystem.
362 * May be invoked in any context.
363 */
364static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
365{
366 return task_css_check(task, subsys_id, true) ==
367 init_css_set.subsys[subsys_id];
368}
369
370static inline struct cgroup *task_cgroup(struct task_struct *task,
371 int subsys_id)
372{
373 return task_css(task, subsys_id)->cgroup;
374}
Tejun Heoa2dd4242014-03-19 10:23:55 -0400375
Tejun Heoaa6ec292014-07-09 10:08:08 -0400376/**
377 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
378 * @cgrp: the cgroup of interest
379 *
380 * The default hierarchy is the v2 interface of cgroup and this function
381 * can be used to test whether a cgroup is on the default hierarchy for
382 * cases where a subsystem should behave differnetly depending on the
383 * interface version.
384 *
385 * The set of behaviors which change on the default hierarchy are still
386 * being determined and the mount option is prefixed with __DEVEL__.
387 *
388 * List of changed behaviors:
389 *
390 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
391 * and "name" are disallowed.
392 *
393 * - When mounting an existing superblock, mount options should match.
394 *
395 * - Remount is disallowed.
396 *
397 * - rename(2) is disallowed.
398 *
399 * - "tasks" is removed. Everything should be at process granularity. Use
400 * "cgroup.procs" instead.
401 *
402 * - "cgroup.procs" is not sorted. pids will be unique unless they got
403 * recycled inbetween reads.
404 *
405 * - "release_agent" and "notify_on_release" are removed. Replacement
406 * notification mechanism will be implemented.
407 *
408 * - "cgroup.clone_children" is removed.
409 *
410 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
411 * and its descendants contain no task; otherwise, 1. The file also
412 * generates kernfs notification which can be monitored through poll and
413 * [di]notify when the value of the file changes.
414 *
415 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
416 * take masks of ancestors with non-empty cpus/mems, instead of being
417 * moved to an ancestor.
418 *
419 * - cpuset: a task can be moved into an empty cpuset, and again it takes
420 * masks of ancestors.
421 *
422 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
423 * is not created.
424 *
425 * - blkcg: blk-throttle becomes properly hierarchical.
426 *
427 * - debug: disallowed on the default hierarchy.
428 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400429static inline bool cgroup_on_dfl(const struct cgroup *cgrp)
430{
431 return cgrp->root == &cgrp_dfl_root;
432}
433
Tejun Heo07bc3562014-02-13 06:58:39 -0500434/* no synchronization, the result can only be used as a hint */
435static inline bool cgroup_has_tasks(struct cgroup *cgrp)
436{
437 return !list_empty(&cgrp->cset_links);
438}
439
Zefan Lif29374b2014-09-19 16:29:31 +0800440/* returns ino associated with a cgroup */
Tejun Heob1664922014-02-11 11:52:49 -0500441static inline ino_t cgroup_ino(struct cgroup *cgrp)
442{
Zefan Lif29374b2014-09-19 16:29:31 +0800443 return cgrp->kn->ino;
Tejun Heob1664922014-02-11 11:52:49 -0500444}
445
Tejun Heob4168642014-05-13 12:16:21 -0400446/* cft/css accessors for cftype->write() operation */
447static inline struct cftype *of_cft(struct kernfs_open_file *of)
Tejun Heo7da11272013-12-05 12:28:04 -0500448{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500449 return of->kn->priv;
Tejun Heo7da11272013-12-05 12:28:04 -0500450}
451
Tejun Heob4168642014-05-13 12:16:21 -0400452struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
453
454/* cft/css accessors for cftype->seq_*() operations */
455static inline struct cftype *seq_cft(struct seq_file *seq)
456{
457 return of_cft(seq->private);
458}
459
460static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
461{
462 return of_css(seq->private);
463}
Tejun Heo59f52962014-02-11 11:52:49 -0500464
Tejun Heoe61734c2014-02-12 09:29:50 -0500465/*
466 * Name / path handling functions. All are thin wrappers around the kernfs
467 * counterparts and can be called under any context.
468 */
469
470static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
471{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400472 return kernfs_name(cgrp->kn, buf, buflen);
Tejun Heoe61734c2014-02-12 09:29:50 -0500473}
474
475static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
476 size_t buflen)
477{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400478 return kernfs_path(cgrp->kn, buf, buflen);
Tejun Heoe61734c2014-02-12 09:29:50 -0500479}
480
481static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
482{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400483 pr_cont_kernfs_name(cgrp->kn);
Tejun Heoe61734c2014-02-12 09:29:50 -0500484}
485
486static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
487{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400488 pr_cont_kernfs_path(cgrp->kn);
Tejun Heoe61734c2014-02-12 09:29:50 -0500489}
490
Paul Menageddbcc7e2007-10-18 23:39:30 -0700491#else /* !CONFIG_CGROUPS */
492
Tejun Heof3ba5382015-01-06 12:02:46 -0500493struct cgroup_subsys_state;
494
Tejun Heoc326aa22015-05-13 16:24:16 -0400495static inline void css_put(struct cgroup_subsys_state *css) {}
496static inline int cgroup_attach_task_all(struct task_struct *from,
497 struct task_struct *t) { return 0; }
498static inline int cgroupstats_build(struct cgroupstats *stats,
499 struct dentry *dentry) { return -EINVAL; }
500
Paul Menageb4f48b62007-10-18 23:39:33 -0700501static inline void cgroup_fork(struct task_struct *p) {}
Paul Menage817929e2007-10-18 23:39:36 -0700502static inline void cgroup_post_fork(struct task_struct *p) {}
Li Zefan1ec41832014-03-28 15:22:19 +0800503static inline void cgroup_exit(struct task_struct *p) {}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700504
Tejun Heoc326aa22015-05-13 16:24:16 -0400505static inline int cgroup_init_early(void) { return 0; }
506static inline int cgroup_init(void) { return 0; }
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200507
Paul Menageddbcc7e2007-10-18 23:39:30 -0700508#endif /* !CONFIG_CGROUPS */
509
Paul Menageddbcc7e2007-10-18 23:39:30 -0700510#endif /* _LINUX_CGROUP_H */