cgroup: allow a cgroup subsystem to reject a fork
[firefly-linux-kernel-4.4.55.git] / include / linux / cgroup.h
1 #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>
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>
17 #include <linux/fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/kernfs.h>
20
21 #include <linux/cgroup-defs.h>
22
23 #ifdef CONFIG_CGROUPS
24
25 /* a css_task_iter should be treated as an opaque object */
26 struct css_task_iter {
27         struct cgroup_subsys            *ss;
28
29         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
37 extern struct cgroup_root cgrp_dfl_root;
38 extern 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
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);
50
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);
54
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);
58
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);
63
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);
72
73 int cgroup_init_early(void);
74 int cgroup_init(void);
75
76 /*
77  * Iteration helpers and macros.
78  */
79
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);
87
88 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
89 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
90
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);
95
96 /**
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
100  *
101  * Walk @parent's children.  Must be called under rcu_read_lock().
102  *
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.
109  *
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.
113  */
114 #define css_for_each_child(pos, parent)                                 \
115         for ((pos) = css_next_child(NULL, (parent)); (pos);             \
116              (pos) = css_next_child((pos), (parent)))
117
118 /**
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
122  *
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().
125  *
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.
132  *
133  * For example, the following guarantees that a descendant can't escape
134  * state updates of its ancestors.
135  *
136  * my_online(@css)
137  * {
138  *      Lock @css's parent and @css;
139  *      Inherit state from the parent;
140  *      Unlock both.
141  * }
142  *
143  * my_update_state(@css)
144  * {
145  *      css_for_each_descendant_pre(@pos, @css) {
146  *              Lock @pos;
147  *              if (@pos == @css)
148  *                      Update @css's state;
149  *              else
150  *                      Verify @pos is alive and inherit state from its parent;
151  *              Unlock @pos;
152  *      }
153  * }
154  *
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.
162  *
163  * If checking parent's state requires locking the parent, each inheriting
164  * iteration should lock and unlock both @pos->parent and @pos.
165  *
166  * Alternatively, a subsystem may choose to use a single global lock to
167  * synchronize ->css_online() and ->css_offline() against tree-walking
168  * operations.
169  *
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.
173  */
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)))
177
178 /**
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
182  *
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.
186  *
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.
193  *
194  * Note that the walk visibility guarantee example described in pre-order
195  * walk doesn't apply the same to post-order walks.
196  */
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)))
200
201 /**
202  * cgroup_taskset_for_each - iterate cgroup_taskset
203  * @task: the loop cursor
204  * @tset: taskset to iterate
205  */
206 #define cgroup_taskset_for_each(task, tset)                             \
207         for ((task) = cgroup_taskset_first((tset)); (task);             \
208              (task) = cgroup_taskset_next((tset)))
209
210 /*
211  * Inline functions.
212  */
213
214 /**
215  * css_get - obtain a reference on the specified css
216  * @css: target css
217  *
218  * The caller must already have a reference.
219  */
220 static inline void css_get(struct cgroup_subsys_state *css)
221 {
222         if (!(css->flags & CSS_NO_REF))
223                 percpu_ref_get(&css->refcnt);
224 }
225
226 /**
227  * css_get_many - obtain references on the specified css
228  * @css: target css
229  * @n: number of references to get
230  *
231  * The caller must already have a reference.
232  */
233 static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
234 {
235         if (!(css->flags & CSS_NO_REF))
236                 percpu_ref_get_many(&css->refcnt, n);
237 }
238
239 /**
240  * css_tryget - try to obtain a reference on the specified css
241  * @css: target css
242  *
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.
249  */
250 static inline bool css_tryget(struct cgroup_subsys_state *css)
251 {
252         if (!(css->flags & CSS_NO_REF))
253                 return percpu_ref_tryget(&css->refcnt);
254         return true;
255 }
256
257 /**
258  * css_tryget_online - try to obtain a reference on the specified css if online
259  * @css: target css
260  *
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;
265  * %false otherwise.
266  */
267 static inline bool css_tryget_online(struct cgroup_subsys_state *css)
268 {
269         if (!(css->flags & CSS_NO_REF))
270                 return percpu_ref_tryget_live(&css->refcnt);
271         return true;
272 }
273
274 /**
275  * css_put - put a css reference
276  * @css: target css
277  *
278  * Put a reference obtained via css_get() and css_tryget_online().
279  */
280 static inline void css_put(struct cgroup_subsys_state *css)
281 {
282         if (!(css->flags & CSS_NO_REF))
283                 percpu_ref_put(&css->refcnt);
284 }
285
286 /**
287  * css_put_many - put css references
288  * @css: target css
289  * @n: number of references to put
290  *
291  * Put references obtained via css_get() and css_tryget_online().
292  */
293 static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
294 {
295         if (!(css->flags & CSS_NO_REF))
296                 percpu_ref_put_many(&css->refcnt, n);
297 }
298
299 /**
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()
303  *
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.
308  *
309  * The caller can also specify additional allowed conditions via @__c, such
310  * as locks used during the cgroup_subsys::attach() methods.
311  */
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))
320 #else
321 #define task_css_set_check(task, __c)                                   \
322         rcu_dereference((task)->cgroups)
323 #endif
324
325 /**
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()
330  *
331  * Return the cgroup_subsys_state for the (@task, @subsys_id) pair.  The
332  * synchronization rules are the same as task_css_set_check().
333  */
334 #define task_css_check(task, subsys_id, __c)                            \
335         task_css_set_check((task), (__c))->subsys[(subsys_id)]
336
337 /**
338  * task_css_set - obtain a task's css_set
339  * @task: the task to obtain css_set for
340  *
341  * See task_css_set_check().
342  */
343 static inline struct css_set *task_css_set(struct task_struct *task)
344 {
345         return task_css_set_check(task, false);
346 }
347
348 /**
349  * task_css - obtain css for (task, subsys)
350  * @task: the target task
351  * @subsys_id: the target subsystem ID
352  *
353  * See task_css_check().
354  */
355 static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
356                                                    int subsys_id)
357 {
358         return task_css_check(task, subsys_id, false);
359 }
360
361 /**
362  * task_get_css - find and get the css for (task, subsys)
363  * @task: the target task
364  * @subsys_id: the target subsystem ID
365  *
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
368  * valid css.
369  */
370 static inline struct cgroup_subsys_state *
371 task_get_css(struct task_struct *task, int subsys_id)
372 {
373         struct cgroup_subsys_state *css;
374
375         rcu_read_lock();
376         while (true) {
377                 css = task_css(task, subsys_id);
378                 if (likely(css_tryget_online(css)))
379                         break;
380                 cpu_relax();
381         }
382         rcu_read_unlock();
383         return css;
384 }
385
386 /**
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
390  *
391  * Test whether @task belongs to the root css on the specified subsystem.
392  * May be invoked in any context.
393  */
394 static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
395 {
396         return task_css_check(task, subsys_id, true) ==
397                 init_css_set.subsys[subsys_id];
398 }
399
400 static inline struct cgroup *task_cgroup(struct task_struct *task,
401                                          int subsys_id)
402 {
403         return task_css(task, subsys_id)->cgroup;
404 }
405
406 /**
407  * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
408  * @cgrp: the cgroup of interest
409  *
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
413  * interface version.
414  *
415  * The set of behaviors which change on the default hierarchy are still
416  * being determined and the mount option is prefixed with __DEVEL__.
417  *
418  * List of changed behaviors:
419  *
420  * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
421  *   and "name" are disallowed.
422  *
423  * - When mounting an existing superblock, mount options should match.
424  *
425  * - Remount is disallowed.
426  *
427  * - rename(2) is disallowed.
428  *
429  * - "tasks" is removed.  Everything should be at process granularity.  Use
430  *   "cgroup.procs" instead.
431  *
432  * - "cgroup.procs" is not sorted.  pids will be unique unless they got
433  *   recycled inbetween reads.
434  *
435  * - "release_agent" and "notify_on_release" are removed.  Replacement
436  *   notification mechanism will be implemented.
437  *
438  * - "cgroup.clone_children" is removed.
439  *
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.
444  *
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.
448  *
449  * - cpuset: a task can be moved into an empty cpuset, and again it takes
450  *   masks of ancestors.
451  *
452  * - memcg: use_hierarchy is on by default and the cgroup file for the flag
453  *   is not created.
454  *
455  * - blkcg: blk-throttle becomes properly hierarchical.
456  *
457  * - debug: disallowed on the default hierarchy.
458  */
459 static inline bool cgroup_on_dfl(const struct cgroup *cgrp)
460 {
461         return cgrp->root == &cgrp_dfl_root;
462 }
463
464 /* no synchronization, the result can only be used as a hint */
465 static inline bool cgroup_has_tasks(struct cgroup *cgrp)
466 {
467         return !list_empty(&cgrp->cset_links);
468 }
469
470 /* returns ino associated with a cgroup */
471 static inline ino_t cgroup_ino(struct cgroup *cgrp)
472 {
473         return cgrp->kn->ino;
474 }
475
476 /* cft/css accessors for cftype->write() operation */
477 static inline struct cftype *of_cft(struct kernfs_open_file *of)
478 {
479         return of->kn->priv;
480 }
481
482 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
483
484 /* cft/css accessors for cftype->seq_*() operations */
485 static inline struct cftype *seq_cft(struct seq_file *seq)
486 {
487         return of_cft(seq->private);
488 }
489
490 static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
491 {
492         return of_css(seq->private);
493 }
494
495 /*
496  * Name / path handling functions.  All are thin wrappers around the kernfs
497  * counterparts and can be called under any context.
498  */
499
500 static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
501 {
502         return kernfs_name(cgrp->kn, buf, buflen);
503 }
504
505 static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
506                                               size_t buflen)
507 {
508         return kernfs_path(cgrp->kn, buf, buflen);
509 }
510
511 static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
512 {
513         pr_cont_kernfs_name(cgrp->kn);
514 }
515
516 static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
517 {
518         pr_cont_kernfs_path(cgrp->kn);
519 }
520
521 #else /* !CONFIG_CGROUPS */
522
523 struct cgroup_subsys_state;
524
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; }
530
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])
534 { return 0; }
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) {}
540
541 static inline int cgroup_init_early(void) { return 0; }
542 static inline int cgroup_init(void) { return 0; }
543
544 #endif /* !CONFIG_CGROUPS */
545
546 #endif /* _LINUX_CGROUP_H */