Merge tag 'nfc-next-4.5-1' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo...
[cascardo/linux.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/fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/kernfs.h>
19 #include <linux/jump_label.h>
20
21 #include <linux/cgroup-defs.h>
22
23 #ifdef CONFIG_CGROUPS
24
25 /*
26  * All weight knobs on the default hierarhcy should use the following min,
27  * default and max values.  The default value is the logarithmic center of
28  * MIN and MAX and allows 100x to be expressed in both directions.
29  */
30 #define CGROUP_WEIGHT_MIN               1
31 #define CGROUP_WEIGHT_DFL               100
32 #define CGROUP_WEIGHT_MAX               10000
33
34 /* a css_task_iter should be treated as an opaque object */
35 struct css_task_iter {
36         struct cgroup_subsys            *ss;
37
38         struct list_head                *cset_pos;
39         struct list_head                *cset_head;
40
41         struct list_head                *task_pos;
42         struct list_head                *tasks_head;
43         struct list_head                *mg_tasks_head;
44
45         struct css_set                  *cur_cset;
46         struct task_struct              *cur_task;
47         struct list_head                iters_node;     /* css_set->task_iters */
48 };
49
50 extern struct cgroup_root cgrp_dfl_root;
51 extern struct css_set init_css_set;
52
53 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
54 #include <linux/cgroup_subsys.h>
55 #undef SUBSYS
56
57 #define SUBSYS(_x)                                                              \
58         extern struct static_key_true _x ## _cgrp_subsys_enabled_key;           \
59         extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
60 #include <linux/cgroup_subsys.h>
61 #undef SUBSYS
62
63 /**
64  * cgroup_subsys_enabled - fast test on whether a subsys is enabled
65  * @ss: subsystem in question
66  */
67 #define cgroup_subsys_enabled(ss)                                               \
68         static_branch_likely(&ss ## _enabled_key)
69
70 /**
71  * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
72  * @ss: subsystem in question
73  */
74 #define cgroup_subsys_on_dfl(ss)                                                \
75         static_branch_likely(&ss ## _on_dfl_key)
76
77 bool css_has_online_children(struct cgroup_subsys_state *css);
78 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
79 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
80                                              struct cgroup_subsys *ss);
81 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
82                                                        struct cgroup_subsys *ss);
83
84 struct cgroup *cgroup_get_from_path(const char *path);
85
86 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
87 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
88
89 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
90 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
91 int cgroup_rm_cftypes(struct cftype *cfts);
92 void cgroup_file_notify(struct cgroup_file *cfile);
93
94 char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
95 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
96 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
97                      struct pid *pid, struct task_struct *tsk);
98
99 void cgroup_fork(struct task_struct *p);
100 extern int cgroup_can_fork(struct task_struct *p,
101                            void *ss_priv[CGROUP_CANFORK_COUNT]);
102 extern void cgroup_cancel_fork(struct task_struct *p,
103                                void *ss_priv[CGROUP_CANFORK_COUNT]);
104 extern void cgroup_post_fork(struct task_struct *p,
105                              void *old_ss_priv[CGROUP_CANFORK_COUNT]);
106 void cgroup_exit(struct task_struct *p);
107 void cgroup_free(struct task_struct *p);
108
109 int cgroup_init_early(void);
110 int cgroup_init(void);
111
112 /*
113  * Iteration helpers and macros.
114  */
115
116 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
117                                            struct cgroup_subsys_state *parent);
118 struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
119                                                     struct cgroup_subsys_state *css);
120 struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
121 struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
122                                                      struct cgroup_subsys_state *css);
123
124 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
125                                          struct cgroup_subsys_state **dst_cssp);
126 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
127                                         struct cgroup_subsys_state **dst_cssp);
128
129 void css_task_iter_start(struct cgroup_subsys_state *css,
130                          struct css_task_iter *it);
131 struct task_struct *css_task_iter_next(struct css_task_iter *it);
132 void css_task_iter_end(struct css_task_iter *it);
133
134 /**
135  * css_for_each_child - iterate through children of a css
136  * @pos: the css * to use as the loop cursor
137  * @parent: css whose children to walk
138  *
139  * Walk @parent's children.  Must be called under rcu_read_lock().
140  *
141  * If a subsystem synchronizes ->css_online() and the start of iteration, a
142  * css which finished ->css_online() is guaranteed to be visible in the
143  * future iterations and will stay visible until the last reference is put.
144  * A css which hasn't finished ->css_online() or already finished
145  * ->css_offline() may show up during traversal.  It's each subsystem's
146  * responsibility to synchronize against on/offlining.
147  *
148  * It is allowed to temporarily drop RCU read lock during iteration.  The
149  * caller is responsible for ensuring that @pos remains accessible until
150  * the start of the next iteration by, for example, bumping the css refcnt.
151  */
152 #define css_for_each_child(pos, parent)                                 \
153         for ((pos) = css_next_child(NULL, (parent)); (pos);             \
154              (pos) = css_next_child((pos), (parent)))
155
156 /**
157  * css_for_each_descendant_pre - pre-order walk of a css's descendants
158  * @pos: the css * to use as the loop cursor
159  * @root: css whose descendants to walk
160  *
161  * Walk @root's descendants.  @root is included in the iteration and the
162  * first node to be visited.  Must be called under rcu_read_lock().
163  *
164  * If a subsystem synchronizes ->css_online() and the start of iteration, a
165  * css which finished ->css_online() is guaranteed to be visible in the
166  * future iterations and will stay visible until the last reference is put.
167  * A css which hasn't finished ->css_online() or already finished
168  * ->css_offline() may show up during traversal.  It's each subsystem's
169  * responsibility to synchronize against on/offlining.
170  *
171  * For example, the following guarantees that a descendant can't escape
172  * state updates of its ancestors.
173  *
174  * my_online(@css)
175  * {
176  *      Lock @css's parent and @css;
177  *      Inherit state from the parent;
178  *      Unlock both.
179  * }
180  *
181  * my_update_state(@css)
182  * {
183  *      css_for_each_descendant_pre(@pos, @css) {
184  *              Lock @pos;
185  *              if (@pos == @css)
186  *                      Update @css's state;
187  *              else
188  *                      Verify @pos is alive and inherit state from its parent;
189  *              Unlock @pos;
190  *      }
191  * }
192  *
193  * As long as the inheriting step, including checking the parent state, is
194  * enclosed inside @pos locking, double-locking the parent isn't necessary
195  * while inheriting.  The state update to the parent is guaranteed to be
196  * visible by walking order and, as long as inheriting operations to the
197  * same @pos are atomic to each other, multiple updates racing each other
198  * still result in the correct state.  It's guaranateed that at least one
199  * inheritance happens for any css after the latest update to its parent.
200  *
201  * If checking parent's state requires locking the parent, each inheriting
202  * iteration should lock and unlock both @pos->parent and @pos.
203  *
204  * Alternatively, a subsystem may choose to use a single global lock to
205  * synchronize ->css_online() and ->css_offline() against tree-walking
206  * operations.
207  *
208  * It is allowed to temporarily drop RCU read lock during iteration.  The
209  * caller is responsible for ensuring that @pos remains accessible until
210  * the start of the next iteration by, for example, bumping the css refcnt.
211  */
212 #define css_for_each_descendant_pre(pos, css)                           \
213         for ((pos) = css_next_descendant_pre(NULL, (css)); (pos);       \
214              (pos) = css_next_descendant_pre((pos), (css)))
215
216 /**
217  * css_for_each_descendant_post - post-order walk of a css's descendants
218  * @pos: the css * to use as the loop cursor
219  * @css: css whose descendants to walk
220  *
221  * Similar to css_for_each_descendant_pre() but performs post-order
222  * traversal instead.  @root is included in the iteration and the last
223  * node to be visited.
224  *
225  * If a subsystem synchronizes ->css_online() and the start of iteration, a
226  * css which finished ->css_online() is guaranteed to be visible in the
227  * future iterations and will stay visible until the last reference is put.
228  * A css which hasn't finished ->css_online() or already finished
229  * ->css_offline() may show up during traversal.  It's each subsystem's
230  * responsibility to synchronize against on/offlining.
231  *
232  * Note that the walk visibility guarantee example described in pre-order
233  * walk doesn't apply the same to post-order walks.
234  */
235 #define css_for_each_descendant_post(pos, css)                          \
236         for ((pos) = css_next_descendant_post(NULL, (css)); (pos);      \
237              (pos) = css_next_descendant_post((pos), (css)))
238
239 /**
240  * cgroup_taskset_for_each - iterate cgroup_taskset
241  * @task: the loop cursor
242  * @dst_css: the destination css
243  * @tset: taskset to iterate
244  *
245  * @tset may contain multiple tasks and they may belong to multiple
246  * processes.
247  *
248  * On the v2 hierarchy, there may be tasks from multiple processes and they
249  * may not share the source or destination csses.
250  *
251  * On traditional hierarchies, when there are multiple tasks in @tset, if a
252  * task of a process is in @tset, all tasks of the process are in @tset.
253  * Also, all are guaranteed to share the same source and destination csses.
254  *
255  * Iteration is not in any specific order.
256  */
257 #define cgroup_taskset_for_each(task, dst_css, tset)                    \
258         for ((task) = cgroup_taskset_first((tset), &(dst_css));         \
259              (task);                                                    \
260              (task) = cgroup_taskset_next((tset), &(dst_css)))
261
262 /**
263  * cgroup_taskset_for_each_leader - iterate group leaders in a cgroup_taskset
264  * @leader: the loop cursor
265  * @dst_css: the destination css
266  * @tset: takset to iterate
267  *
268  * Iterate threadgroup leaders of @tset.  For single-task migrations, @tset
269  * may not contain any.
270  */
271 #define cgroup_taskset_for_each_leader(leader, dst_css, tset)           \
272         for ((leader) = cgroup_taskset_first((tset), &(dst_css));       \
273              (leader);                                                  \
274              (leader) = cgroup_taskset_next((tset), &(dst_css)))        \
275                 if ((leader) != (leader)->group_leader)                 \
276                         ;                                               \
277                 else
278
279 /*
280  * Inline functions.
281  */
282
283 /**
284  * css_get - obtain a reference on the specified css
285  * @css: target css
286  *
287  * The caller must already have a reference.
288  */
289 static inline void css_get(struct cgroup_subsys_state *css)
290 {
291         if (!(css->flags & CSS_NO_REF))
292                 percpu_ref_get(&css->refcnt);
293 }
294
295 /**
296  * css_get_many - obtain references on the specified css
297  * @css: target css
298  * @n: number of references to get
299  *
300  * The caller must already have a reference.
301  */
302 static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
303 {
304         if (!(css->flags & CSS_NO_REF))
305                 percpu_ref_get_many(&css->refcnt, n);
306 }
307
308 /**
309  * css_tryget - try to obtain a reference on the specified css
310  * @css: target css
311  *
312  * Obtain a reference on @css unless it already has reached zero and is
313  * being released.  This function doesn't care whether @css is on or
314  * offline.  The caller naturally needs to ensure that @css is accessible
315  * but doesn't have to be holding a reference on it - IOW, RCU protected
316  * access is good enough for this function.  Returns %true if a reference
317  * count was successfully obtained; %false otherwise.
318  */
319 static inline bool css_tryget(struct cgroup_subsys_state *css)
320 {
321         if (!(css->flags & CSS_NO_REF))
322                 return percpu_ref_tryget(&css->refcnt);
323         return true;
324 }
325
326 /**
327  * css_tryget_online - try to obtain a reference on the specified css if online
328  * @css: target css
329  *
330  * Obtain a reference on @css if it's online.  The caller naturally needs
331  * to ensure that @css is accessible but doesn't have to be holding a
332  * reference on it - IOW, RCU protected access is good enough for this
333  * function.  Returns %true if a reference count was successfully obtained;
334  * %false otherwise.
335  */
336 static inline bool css_tryget_online(struct cgroup_subsys_state *css)
337 {
338         if (!(css->flags & CSS_NO_REF))
339                 return percpu_ref_tryget_live(&css->refcnt);
340         return true;
341 }
342
343 /**
344  * css_put - put a css reference
345  * @css: target css
346  *
347  * Put a reference obtained via css_get() and css_tryget_online().
348  */
349 static inline void css_put(struct cgroup_subsys_state *css)
350 {
351         if (!(css->flags & CSS_NO_REF))
352                 percpu_ref_put(&css->refcnt);
353 }
354
355 /**
356  * css_put_many - put css references
357  * @css: target css
358  * @n: number of references to put
359  *
360  * Put references obtained via css_get() and css_tryget_online().
361  */
362 static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
363 {
364         if (!(css->flags & CSS_NO_REF))
365                 percpu_ref_put_many(&css->refcnt, n);
366 }
367
368 static inline void cgroup_put(struct cgroup *cgrp)
369 {
370         css_put(&cgrp->self);
371 }
372
373 /**
374  * task_css_set_check - obtain a task's css_set with extra access conditions
375  * @task: the task to obtain css_set for
376  * @__c: extra condition expression to be passed to rcu_dereference_check()
377  *
378  * A task's css_set is RCU protected, initialized and exited while holding
379  * task_lock(), and can only be modified while holding both cgroup_mutex
380  * and task_lock() while the task is alive.  This macro verifies that the
381  * caller is inside proper critical section and returns @task's css_set.
382  *
383  * The caller can also specify additional allowed conditions via @__c, such
384  * as locks used during the cgroup_subsys::attach() methods.
385  */
386 #ifdef CONFIG_PROVE_RCU
387 extern struct mutex cgroup_mutex;
388 extern spinlock_t css_set_lock;
389 #define task_css_set_check(task, __c)                                   \
390         rcu_dereference_check((task)->cgroups,                          \
391                 lockdep_is_held(&cgroup_mutex) ||                       \
392                 lockdep_is_held(&css_set_lock) ||                       \
393                 ((task)->flags & PF_EXITING) || (__c))
394 #else
395 #define task_css_set_check(task, __c)                                   \
396         rcu_dereference((task)->cgroups)
397 #endif
398
399 /**
400  * task_css_check - obtain css for (task, subsys) w/ extra access conds
401  * @task: the target task
402  * @subsys_id: the target subsystem ID
403  * @__c: extra condition expression to be passed to rcu_dereference_check()
404  *
405  * Return the cgroup_subsys_state for the (@task, @subsys_id) pair.  The
406  * synchronization rules are the same as task_css_set_check().
407  */
408 #define task_css_check(task, subsys_id, __c)                            \
409         task_css_set_check((task), (__c))->subsys[(subsys_id)]
410
411 /**
412  * task_css_set - obtain a task's css_set
413  * @task: the task to obtain css_set for
414  *
415  * See task_css_set_check().
416  */
417 static inline struct css_set *task_css_set(struct task_struct *task)
418 {
419         return task_css_set_check(task, false);
420 }
421
422 /**
423  * task_css - obtain css for (task, subsys)
424  * @task: the target task
425  * @subsys_id: the target subsystem ID
426  *
427  * See task_css_check().
428  */
429 static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
430                                                    int subsys_id)
431 {
432         return task_css_check(task, subsys_id, false);
433 }
434
435 /**
436  * task_get_css - find and get the css for (task, subsys)
437  * @task: the target task
438  * @subsys_id: the target subsystem ID
439  *
440  * Find the css for the (@task, @subsys_id) combination, increment a
441  * reference on and return it.  This function is guaranteed to return a
442  * valid css.
443  */
444 static inline struct cgroup_subsys_state *
445 task_get_css(struct task_struct *task, int subsys_id)
446 {
447         struct cgroup_subsys_state *css;
448
449         rcu_read_lock();
450         while (true) {
451                 css = task_css(task, subsys_id);
452                 if (likely(css_tryget_online(css)))
453                         break;
454                 cpu_relax();
455         }
456         rcu_read_unlock();
457         return css;
458 }
459
460 /**
461  * task_css_is_root - test whether a task belongs to the root css
462  * @task: the target task
463  * @subsys_id: the target subsystem ID
464  *
465  * Test whether @task belongs to the root css on the specified subsystem.
466  * May be invoked in any context.
467  */
468 static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
469 {
470         return task_css_check(task, subsys_id, true) ==
471                 init_css_set.subsys[subsys_id];
472 }
473
474 static inline struct cgroup *task_cgroup(struct task_struct *task,
475                                          int subsys_id)
476 {
477         return task_css(task, subsys_id)->cgroup;
478 }
479
480 /**
481  * cgroup_is_descendant - test ancestry
482  * @cgrp: the cgroup to be tested
483  * @ancestor: possible ancestor of @cgrp
484  *
485  * Test whether @cgrp is a descendant of @ancestor.  It also returns %true
486  * if @cgrp == @ancestor.  This function is safe to call as long as @cgrp
487  * and @ancestor are accessible.
488  */
489 static inline bool cgroup_is_descendant(struct cgroup *cgrp,
490                                         struct cgroup *ancestor)
491 {
492         if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
493                 return false;
494         return cgrp->ancestor_ids[ancestor->level] == ancestor->id;
495 }
496
497 /* no synchronization, the result can only be used as a hint */
498 static inline bool cgroup_is_populated(struct cgroup *cgrp)
499 {
500         return cgrp->populated_cnt;
501 }
502
503 /* returns ino associated with a cgroup */
504 static inline ino_t cgroup_ino(struct cgroup *cgrp)
505 {
506         return cgrp->kn->ino;
507 }
508
509 /* cft/css accessors for cftype->write() operation */
510 static inline struct cftype *of_cft(struct kernfs_open_file *of)
511 {
512         return of->kn->priv;
513 }
514
515 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
516
517 /* cft/css accessors for cftype->seq_*() operations */
518 static inline struct cftype *seq_cft(struct seq_file *seq)
519 {
520         return of_cft(seq->private);
521 }
522
523 static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
524 {
525         return of_css(seq->private);
526 }
527
528 /*
529  * Name / path handling functions.  All are thin wrappers around the kernfs
530  * counterparts and can be called under any context.
531  */
532
533 static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
534 {
535         return kernfs_name(cgrp->kn, buf, buflen);
536 }
537
538 static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
539                                               size_t buflen)
540 {
541         return kernfs_path(cgrp->kn, buf, buflen);
542 }
543
544 static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
545 {
546         pr_cont_kernfs_name(cgrp->kn);
547 }
548
549 static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
550 {
551         pr_cont_kernfs_path(cgrp->kn);
552 }
553
554 #else /* !CONFIG_CGROUPS */
555
556 struct cgroup_subsys_state;
557
558 static inline void css_put(struct cgroup_subsys_state *css) {}
559 static inline int cgroup_attach_task_all(struct task_struct *from,
560                                          struct task_struct *t) { return 0; }
561 static inline int cgroupstats_build(struct cgroupstats *stats,
562                                     struct dentry *dentry) { return -EINVAL; }
563
564 static inline void cgroup_fork(struct task_struct *p) {}
565 static inline int cgroup_can_fork(struct task_struct *p,
566                                   void *ss_priv[CGROUP_CANFORK_COUNT])
567 { return 0; }
568 static inline void cgroup_cancel_fork(struct task_struct *p,
569                                       void *ss_priv[CGROUP_CANFORK_COUNT]) {}
570 static inline void cgroup_post_fork(struct task_struct *p,
571                                     void *ss_priv[CGROUP_CANFORK_COUNT]) {}
572 static inline void cgroup_exit(struct task_struct *p) {}
573 static inline void cgroup_free(struct task_struct *p) {}
574
575 static inline int cgroup_init_early(void) { return 0; }
576 static inline int cgroup_init(void) { return 0; }
577
578 #endif /* !CONFIG_CGROUPS */
579
580 /*
581  * sock->sk_cgrp_data handling.  For more info, see sock_cgroup_data
582  * definition in cgroup-defs.h.
583  */
584 #ifdef CONFIG_SOCK_CGROUP_DATA
585
586 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
587 extern spinlock_t cgroup_sk_update_lock;
588 #endif
589
590 void cgroup_sk_alloc_disable(void);
591 void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
592 void cgroup_sk_free(struct sock_cgroup_data *skcd);
593
594 static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
595 {
596 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
597         unsigned long v;
598
599         /*
600          * @skcd->val is 64bit but the following is safe on 32bit too as we
601          * just need the lower ulong to be written and read atomically.
602          */
603         v = READ_ONCE(skcd->val);
604
605         if (v & 1)
606                 return &cgrp_dfl_root.cgrp;
607
608         return (struct cgroup *)(unsigned long)v ?: &cgrp_dfl_root.cgrp;
609 #else
610         return (struct cgroup *)(unsigned long)skcd->val;
611 #endif
612 }
613
614 #else   /* CONFIG_CGROUP_DATA */
615
616 static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
617 static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
618
619 #endif  /* CONFIG_CGROUP_DATA */
620
621 #endif /* _LINUX_CGROUP_H */