GNU Linux-libre 5.4.241-gnu1
[releases.git] / include / linux / cgroup.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_CGROUP_H
3 #define _LINUX_CGROUP_H
4 /*
5  *  cgroup interface
6  *
7  *  Copyright (C) 2003 BULL SA
8  *  Copyright (C) 2004-2006 Silicon Graphics, Inc.
9  *
10  */
11
12 #include <linux/sched.h>
13 #include <linux/cpumask.h>
14 #include <linux/nodemask.h>
15 #include <linux/rculist.h>
16 #include <linux/cgroupstats.h>
17 #include <linux/fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/kernfs.h>
20 #include <linux/jump_label.h>
21 #include <linux/types.h>
22 #include <linux/ns_common.h>
23 #include <linux/nsproxy.h>
24 #include <linux/user_namespace.h>
25 #include <linux/refcount.h>
26 #include <linux/kernel_stat.h>
27
28 #include <linux/cgroup-defs.h>
29
30 #ifdef CONFIG_CGROUPS
31
32 /*
33  * All weight knobs on the default hierarhcy should use the following min,
34  * default and max values.  The default value is the logarithmic center of
35  * MIN and MAX and allows 100x to be expressed in both directions.
36  */
37 #define CGROUP_WEIGHT_MIN               1
38 #define CGROUP_WEIGHT_DFL               100
39 #define CGROUP_WEIGHT_MAX               10000
40
41 /* walk only threadgroup leaders */
42 #define CSS_TASK_ITER_PROCS             (1U << 0)
43 /* walk all threaded css_sets in the domain */
44 #define CSS_TASK_ITER_THREADED          (1U << 1)
45
46 /* internal flags */
47 #define CSS_TASK_ITER_SKIPPED           (1U << 16)
48
49 /* a css_task_iter should be treated as an opaque object */
50 struct css_task_iter {
51         struct cgroup_subsys            *ss;
52         unsigned int                    flags;
53
54         struct list_head                *cset_pos;
55         struct list_head                *cset_head;
56
57         struct list_head                *tcset_pos;
58         struct list_head                *tcset_head;
59
60         struct list_head                *task_pos;
61         struct list_head                *tasks_head;
62         struct list_head                *mg_tasks_head;
63         struct list_head                *dying_tasks_head;
64
65         struct list_head                *cur_tasks_head;
66         struct css_set                  *cur_cset;
67         struct css_set                  *cur_dcset;
68         struct task_struct              *cur_task;
69         struct list_head                iters_node;     /* css_set->task_iters */
70 };
71
72 extern struct file_system_type cgroup_fs_type;
73 extern struct cgroup_root cgrp_dfl_root;
74 extern struct css_set init_css_set;
75
76 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
77 #include <linux/cgroup_subsys.h>
78 #undef SUBSYS
79
80 #define SUBSYS(_x)                                                              \
81         extern struct static_key_true _x ## _cgrp_subsys_enabled_key;           \
82         extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
83 #include <linux/cgroup_subsys.h>
84 #undef SUBSYS
85
86 /**
87  * cgroup_subsys_enabled - fast test on whether a subsys is enabled
88  * @ss: subsystem in question
89  */
90 #define cgroup_subsys_enabled(ss)                                               \
91         static_branch_likely(&ss ## _enabled_key)
92
93 /**
94  * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
95  * @ss: subsystem in question
96  */
97 #define cgroup_subsys_on_dfl(ss)                                                \
98         static_branch_likely(&ss ## _on_dfl_key)
99
100 bool css_has_online_children(struct cgroup_subsys_state *css);
101 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
102 struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgroup,
103                                          struct cgroup_subsys *ss);
104 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
105                                              struct cgroup_subsys *ss);
106 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
107                                                        struct cgroup_subsys *ss);
108
109 struct cgroup *cgroup_get_from_path(const char *path);
110 struct cgroup *cgroup_get_from_fd(int fd);
111
112 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
113 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
114
115 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
116 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
117 int cgroup_rm_cftypes(struct cftype *cfts);
118 void cgroup_file_notify(struct cgroup_file *cfile);
119
120 int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
121 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
122 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
123                      struct pid *pid, struct task_struct *tsk);
124
125 void cgroup_fork(struct task_struct *p);
126 extern int cgroup_can_fork(struct task_struct *p);
127 extern void cgroup_cancel_fork(struct task_struct *p);
128 extern void cgroup_post_fork(struct task_struct *p);
129 void cgroup_exit(struct task_struct *p);
130 void cgroup_release(struct task_struct *p);
131 void cgroup_free(struct task_struct *p);
132
133 int cgroup_init_early(void);
134 int cgroup_init(void);
135
136 int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v);
137
138 /*
139  * Iteration helpers and macros.
140  */
141
142 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
143                                            struct cgroup_subsys_state *parent);
144 struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
145                                                     struct cgroup_subsys_state *css);
146 struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
147 struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
148                                                      struct cgroup_subsys_state *css);
149
150 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
151                                          struct cgroup_subsys_state **dst_cssp);
152 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
153                                         struct cgroup_subsys_state **dst_cssp);
154
155 void cgroup_enable_task_cg_lists(void);
156 void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
157                          struct css_task_iter *it);
158 struct task_struct *css_task_iter_next(struct css_task_iter *it);
159 void css_task_iter_end(struct css_task_iter *it);
160
161 /**
162  * css_for_each_child - iterate through children of a css
163  * @pos: the css * to use as the loop cursor
164  * @parent: css whose children to walk
165  *
166  * Walk @parent's children.  Must be called under rcu_read_lock().
167  *
168  * If a subsystem synchronizes ->css_online() and the start of iteration, a
169  * css which finished ->css_online() is guaranteed to be visible in the
170  * future iterations and will stay visible until the last reference is put.
171  * A css which hasn't finished ->css_online() or already finished
172  * ->css_offline() may show up during traversal.  It's each subsystem's
173  * responsibility to synchronize against on/offlining.
174  *
175  * It is allowed to temporarily drop RCU read lock during iteration.  The
176  * caller is responsible for ensuring that @pos remains accessible until
177  * the start of the next iteration by, for example, bumping the css refcnt.
178  */
179 #define css_for_each_child(pos, parent)                                 \
180         for ((pos) = css_next_child(NULL, (parent)); (pos);             \
181              (pos) = css_next_child((pos), (parent)))
182
183 /**
184  * css_for_each_descendant_pre - pre-order walk of a css's descendants
185  * @pos: the css * to use as the loop cursor
186  * @root: css whose descendants to walk
187  *
188  * Walk @root's descendants.  @root is included in the iteration and the
189  * first node to be visited.  Must be called under rcu_read_lock().
190  *
191  * If a subsystem synchronizes ->css_online() and the start of iteration, a
192  * css which finished ->css_online() is guaranteed to be visible in the
193  * future iterations and will stay visible until the last reference is put.
194  * A css which hasn't finished ->css_online() or already finished
195  * ->css_offline() may show up during traversal.  It's each subsystem's
196  * responsibility to synchronize against on/offlining.
197  *
198  * For example, the following guarantees that a descendant can't escape
199  * state updates of its ancestors.
200  *
201  * my_online(@css)
202  * {
203  *      Lock @css's parent and @css;
204  *      Inherit state from the parent;
205  *      Unlock both.
206  * }
207  *
208  * my_update_state(@css)
209  * {
210  *      css_for_each_descendant_pre(@pos, @css) {
211  *              Lock @pos;
212  *              if (@pos == @css)
213  *                      Update @css's state;
214  *              else
215  *                      Verify @pos is alive and inherit state from its parent;
216  *              Unlock @pos;
217  *      }
218  * }
219  *
220  * As long as the inheriting step, including checking the parent state, is
221  * enclosed inside @pos locking, double-locking the parent isn't necessary
222  * while inheriting.  The state update to the parent is guaranteed to be
223  * visible by walking order and, as long as inheriting operations to the
224  * same @pos are atomic to each other, multiple updates racing each other
225  * still result in the correct state.  It's guaranateed that at least one
226  * inheritance happens for any css after the latest update to its parent.
227  *
228  * If checking parent's state requires locking the parent, each inheriting
229  * iteration should lock and unlock both @pos->parent and @pos.
230  *
231  * Alternatively, a subsystem may choose to use a single global lock to
232  * synchronize ->css_online() and ->css_offline() against tree-walking
233  * operations.
234  *
235  * It is allowed to temporarily drop RCU read lock during iteration.  The
236  * caller is responsible for ensuring that @pos remains accessible until
237  * the start of the next iteration by, for example, bumping the css refcnt.
238  */
239 #define css_for_each_descendant_pre(pos, css)                           \
240         for ((pos) = css_next_descendant_pre(NULL, (css)); (pos);       \
241              (pos) = css_next_descendant_pre((pos), (css)))
242
243 /**
244  * css_for_each_descendant_post - post-order walk of a css's descendants
245  * @pos: the css * to use as the loop cursor
246  * @css: css whose descendants to walk
247  *
248  * Similar to css_for_each_descendant_pre() but performs post-order
249  * traversal instead.  @root is included in the iteration and the last
250  * node to be visited.
251  *
252  * If a subsystem synchronizes ->css_online() and the start of iteration, a
253  * css which finished ->css_online() is guaranteed to be visible in the
254  * future iterations and will stay visible until the last reference is put.
255  * A css which hasn't finished ->css_online() or already finished
256  * ->css_offline() may show up during traversal.  It's each subsystem's
257  * responsibility to synchronize against on/offlining.
258  *
259  * Note that the walk visibility guarantee example described in pre-order
260  * walk doesn't apply the same to post-order walks.
261  */
262 #define css_for_each_descendant_post(pos, css)                          \
263         for ((pos) = css_next_descendant_post(NULL, (css)); (pos);      \
264              (pos) = css_next_descendant_post((pos), (css)))
265
266 /**
267  * cgroup_taskset_for_each - iterate cgroup_taskset
268  * @task: the loop cursor
269  * @dst_css: the destination css
270  * @tset: taskset to iterate
271  *
272  * @tset may contain multiple tasks and they may belong to multiple
273  * processes.
274  *
275  * On the v2 hierarchy, there may be tasks from multiple processes and they
276  * may not share the source or destination csses.
277  *
278  * On traditional hierarchies, when there are multiple tasks in @tset, if a
279  * task of a process is in @tset, all tasks of the process are in @tset.
280  * Also, all are guaranteed to share the same source and destination csses.
281  *
282  * Iteration is not in any specific order.
283  */
284 #define cgroup_taskset_for_each(task, dst_css, tset)                    \
285         for ((task) = cgroup_taskset_first((tset), &(dst_css));         \
286              (task);                                                    \
287              (task) = cgroup_taskset_next((tset), &(dst_css)))
288
289 /**
290  * cgroup_taskset_for_each_leader - iterate group leaders in a cgroup_taskset
291  * @leader: the loop cursor
292  * @dst_css: the destination css
293  * @tset: taskset to iterate
294  *
295  * Iterate threadgroup leaders of @tset.  For single-task migrations, @tset
296  * may not contain any.
297  */
298 #define cgroup_taskset_for_each_leader(leader, dst_css, tset)           \
299         for ((leader) = cgroup_taskset_first((tset), &(dst_css));       \
300              (leader);                                                  \
301              (leader) = cgroup_taskset_next((tset), &(dst_css)))        \
302                 if ((leader) != (leader)->group_leader)                 \
303                         ;                                               \
304                 else
305
306 /*
307  * Inline functions.
308  */
309
310 /**
311  * css_get - obtain a reference on the specified css
312  * @css: target css
313  *
314  * The caller must already have a reference.
315  */
316 static inline void css_get(struct cgroup_subsys_state *css)
317 {
318         if (!(css->flags & CSS_NO_REF))
319                 percpu_ref_get(&css->refcnt);
320 }
321
322 /**
323  * css_get_many - obtain references on the specified css
324  * @css: target css
325  * @n: number of references to get
326  *
327  * The caller must already have a reference.
328  */
329 static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
330 {
331         if (!(css->flags & CSS_NO_REF))
332                 percpu_ref_get_many(&css->refcnt, n);
333 }
334
335 /**
336  * css_tryget - try to obtain a reference on the specified css
337  * @css: target css
338  *
339  * Obtain a reference on @css unless it already has reached zero and is
340  * being released.  This function doesn't care whether @css is on or
341  * offline.  The caller naturally needs to ensure that @css is accessible
342  * but doesn't have to be holding a reference on it - IOW, RCU protected
343  * access is good enough for this function.  Returns %true if a reference
344  * count was successfully obtained; %false otherwise.
345  */
346 static inline bool css_tryget(struct cgroup_subsys_state *css)
347 {
348         if (!(css->flags & CSS_NO_REF))
349                 return percpu_ref_tryget(&css->refcnt);
350         return true;
351 }
352
353 /**
354  * css_tryget_online - try to obtain a reference on the specified css if online
355  * @css: target css
356  *
357  * Obtain a reference on @css if it's online.  The caller naturally needs
358  * to ensure that @css is accessible but doesn't have to be holding a
359  * reference on it - IOW, RCU protected access is good enough for this
360  * function.  Returns %true if a reference count was successfully obtained;
361  * %false otherwise.
362  */
363 static inline bool css_tryget_online(struct cgroup_subsys_state *css)
364 {
365         if (!(css->flags & CSS_NO_REF))
366                 return percpu_ref_tryget_live(&css->refcnt);
367         return true;
368 }
369
370 /**
371  * css_is_dying - test whether the specified css is dying
372  * @css: target css
373  *
374  * Test whether @css is in the process of offlining or already offline.  In
375  * most cases, ->css_online() and ->css_offline() callbacks should be
376  * enough; however, the actual offline operations are RCU delayed and this
377  * test returns %true also when @css is scheduled to be offlined.
378  *
379  * This is useful, for example, when the use case requires synchronous
380  * behavior with respect to cgroup removal.  cgroup removal schedules css
381  * offlining but the css can seem alive while the operation is being
382  * delayed.  If the delay affects user visible semantics, this test can be
383  * used to resolve the situation.
384  */
385 static inline bool css_is_dying(struct cgroup_subsys_state *css)
386 {
387         return !(css->flags & CSS_NO_REF) && percpu_ref_is_dying(&css->refcnt);
388 }
389
390 /**
391  * css_put - put a css reference
392  * @css: target css
393  *
394  * Put a reference obtained via css_get() and css_tryget_online().
395  */
396 static inline void css_put(struct cgroup_subsys_state *css)
397 {
398         if (!(css->flags & CSS_NO_REF))
399                 percpu_ref_put(&css->refcnt);
400 }
401
402 /**
403  * css_put_many - put css references
404  * @css: target css
405  * @n: number of references to put
406  *
407  * Put references obtained via css_get() and css_tryget_online().
408  */
409 static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
410 {
411         if (!(css->flags & CSS_NO_REF))
412                 percpu_ref_put_many(&css->refcnt, n);
413 }
414
415 static inline void cgroup_get(struct cgroup *cgrp)
416 {
417         css_get(&cgrp->self);
418 }
419
420 static inline bool cgroup_tryget(struct cgroup *cgrp)
421 {
422         return css_tryget(&cgrp->self);
423 }
424
425 static inline void cgroup_put(struct cgroup *cgrp)
426 {
427         css_put(&cgrp->self);
428 }
429
430 /**
431  * task_css_set_check - obtain a task's css_set with extra access conditions
432  * @task: the task to obtain css_set for
433  * @__c: extra condition expression to be passed to rcu_dereference_check()
434  *
435  * A task's css_set is RCU protected, initialized and exited while holding
436  * task_lock(), and can only be modified while holding both cgroup_mutex
437  * and task_lock() while the task is alive.  This macro verifies that the
438  * caller is inside proper critical section and returns @task's css_set.
439  *
440  * The caller can also specify additional allowed conditions via @__c, such
441  * as locks used during the cgroup_subsys::attach() methods.
442  */
443 #ifdef CONFIG_PROVE_RCU
444 extern struct mutex cgroup_mutex;
445 extern spinlock_t css_set_lock;
446 #define task_css_set_check(task, __c)                                   \
447         rcu_dereference_check((task)->cgroups,                          \
448                 lockdep_is_held(&cgroup_mutex) ||                       \
449                 lockdep_is_held(&css_set_lock) ||                       \
450                 ((task)->flags & PF_EXITING) || (__c))
451 #else
452 #define task_css_set_check(task, __c)                                   \
453         rcu_dereference((task)->cgroups)
454 #endif
455
456 /**
457  * task_css_check - obtain css for (task, subsys) w/ extra access conds
458  * @task: the target task
459  * @subsys_id: the target subsystem ID
460  * @__c: extra condition expression to be passed to rcu_dereference_check()
461  *
462  * Return the cgroup_subsys_state for the (@task, @subsys_id) pair.  The
463  * synchronization rules are the same as task_css_set_check().
464  */
465 #define task_css_check(task, subsys_id, __c)                            \
466         task_css_set_check((task), (__c))->subsys[(subsys_id)]
467
468 /**
469  * task_css_set - obtain a task's css_set
470  * @task: the task to obtain css_set for
471  *
472  * See task_css_set_check().
473  */
474 static inline struct css_set *task_css_set(struct task_struct *task)
475 {
476         return task_css_set_check(task, false);
477 }
478
479 /**
480  * task_css - obtain css for (task, subsys)
481  * @task: the target task
482  * @subsys_id: the target subsystem ID
483  *
484  * See task_css_check().
485  */
486 static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
487                                                    int subsys_id)
488 {
489         return task_css_check(task, subsys_id, false);
490 }
491
492 /**
493  * task_get_css - find and get the css for (task, subsys)
494  * @task: the target task
495  * @subsys_id: the target subsystem ID
496  *
497  * Find the css for the (@task, @subsys_id) combination, increment a
498  * reference on and return it.  This function is guaranteed to return a
499  * valid css.  The returned css may already have been offlined.
500  */
501 static inline struct cgroup_subsys_state *
502 task_get_css(struct task_struct *task, int subsys_id)
503 {
504         struct cgroup_subsys_state *css;
505
506         rcu_read_lock();
507         while (true) {
508                 css = task_css(task, subsys_id);
509                 /*
510                  * Can't use css_tryget_online() here.  A task which has
511                  * PF_EXITING set may stay associated with an offline css.
512                  * If such task calls this function, css_tryget_online()
513                  * will keep failing.
514                  */
515                 if (likely(css_tryget(css)))
516                         break;
517                 cpu_relax();
518         }
519         rcu_read_unlock();
520         return css;
521 }
522
523 /**
524  * task_css_is_root - test whether a task belongs to the root css
525  * @task: the target task
526  * @subsys_id: the target subsystem ID
527  *
528  * Test whether @task belongs to the root css on the specified subsystem.
529  * May be invoked in any context.
530  */
531 static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
532 {
533         return task_css_check(task, subsys_id, true) ==
534                 init_css_set.subsys[subsys_id];
535 }
536
537 static inline struct cgroup *task_cgroup(struct task_struct *task,
538                                          int subsys_id)
539 {
540         return task_css(task, subsys_id)->cgroup;
541 }
542
543 static inline struct cgroup *task_dfl_cgroup(struct task_struct *task)
544 {
545         return task_css_set(task)->dfl_cgrp;
546 }
547
548 static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
549 {
550         struct cgroup_subsys_state *parent_css = cgrp->self.parent;
551
552         if (parent_css)
553                 return container_of(parent_css, struct cgroup, self);
554         return NULL;
555 }
556
557 /**
558  * cgroup_is_descendant - test ancestry
559  * @cgrp: the cgroup to be tested
560  * @ancestor: possible ancestor of @cgrp
561  *
562  * Test whether @cgrp is a descendant of @ancestor.  It also returns %true
563  * if @cgrp == @ancestor.  This function is safe to call as long as @cgrp
564  * and @ancestor are accessible.
565  */
566 static inline bool cgroup_is_descendant(struct cgroup *cgrp,
567                                         struct cgroup *ancestor)
568 {
569         if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
570                 return false;
571         return cgrp->ancestor_ids[ancestor->level] == ancestor->id;
572 }
573
574 /**
575  * cgroup_ancestor - find ancestor of cgroup
576  * @cgrp: cgroup to find ancestor of
577  * @ancestor_level: level of ancestor to find starting from root
578  *
579  * Find ancestor of cgroup at specified level starting from root if it exists
580  * and return pointer to it. Return NULL if @cgrp doesn't have ancestor at
581  * @ancestor_level.
582  *
583  * This function is safe to call as long as @cgrp is accessible.
584  */
585 static inline struct cgroup *cgroup_ancestor(struct cgroup *cgrp,
586                                              int ancestor_level)
587 {
588         if (cgrp->level < ancestor_level)
589                 return NULL;
590         while (cgrp && cgrp->level > ancestor_level)
591                 cgrp = cgroup_parent(cgrp);
592         return cgrp;
593 }
594
595 /**
596  * task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
597  * @task: the task to be tested
598  * @ancestor: possible ancestor of @task's cgroup
599  *
600  * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
601  * It follows all the same rules as cgroup_is_descendant, and only applies
602  * to the default hierarchy.
603  */
604 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
605                                                struct cgroup *ancestor)
606 {
607         struct css_set *cset = task_css_set(task);
608
609         return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
610 }
611
612 /* no synchronization, the result can only be used as a hint */
613 static inline bool cgroup_is_populated(struct cgroup *cgrp)
614 {
615         return cgrp->nr_populated_csets + cgrp->nr_populated_domain_children +
616                 cgrp->nr_populated_threaded_children;
617 }
618
619 /* returns ino associated with a cgroup */
620 static inline ino_t cgroup_ino(struct cgroup *cgrp)
621 {
622         return cgrp->kn->id.ino;
623 }
624
625 /* cft/css accessors for cftype->write() operation */
626 static inline struct cftype *of_cft(struct kernfs_open_file *of)
627 {
628         return of->kn->priv;
629 }
630
631 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
632
633 /* cft/css accessors for cftype->seq_*() operations */
634 static inline struct cftype *seq_cft(struct seq_file *seq)
635 {
636         return of_cft(seq->private);
637 }
638
639 static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
640 {
641         return of_css(seq->private);
642 }
643
644 /*
645  * Name / path handling functions.  All are thin wrappers around the kernfs
646  * counterparts and can be called under any context.
647  */
648
649 static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
650 {
651         return kernfs_name(cgrp->kn, buf, buflen);
652 }
653
654 static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
655 {
656         return kernfs_path(cgrp->kn, buf, buflen);
657 }
658
659 static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
660 {
661         pr_cont_kernfs_name(cgrp->kn);
662 }
663
664 static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
665 {
666         pr_cont_kernfs_path(cgrp->kn);
667 }
668
669 static inline struct psi_group *cgroup_psi(struct cgroup *cgrp)
670 {
671         return &cgrp->psi;
672 }
673
674 static inline void cgroup_init_kthreadd(void)
675 {
676         /*
677          * kthreadd is inherited by all kthreads, keep it in the root so
678          * that the new kthreads are guaranteed to stay in the root until
679          * initialization is finished.
680          */
681         current->no_cgroup_migration = 1;
682 }
683
684 static inline void cgroup_kthread_ready(void)
685 {
686         /*
687          * This kthread finished initialization.  The creator should have
688          * set PF_NO_SETAFFINITY if this kthread should stay in the root.
689          */
690         current->no_cgroup_migration = 0;
691 }
692
693 static inline union kernfs_node_id *cgroup_get_kernfs_id(struct cgroup *cgrp)
694 {
695         return &cgrp->kn->id;
696 }
697
698 void cgroup_path_from_kernfs_id(const union kernfs_node_id *id,
699                                         char *buf, size_t buflen);
700 #else /* !CONFIG_CGROUPS */
701
702 struct cgroup_subsys_state;
703 struct cgroup;
704
705 static inline void css_get(struct cgroup_subsys_state *css) {}
706 static inline void css_put(struct cgroup_subsys_state *css) {}
707 static inline int cgroup_attach_task_all(struct task_struct *from,
708                                          struct task_struct *t) { return 0; }
709 static inline int cgroupstats_build(struct cgroupstats *stats,
710                                     struct dentry *dentry) { return -EINVAL; }
711
712 static inline void cgroup_fork(struct task_struct *p) {}
713 static inline int cgroup_can_fork(struct task_struct *p) { return 0; }
714 static inline void cgroup_cancel_fork(struct task_struct *p) {}
715 static inline void cgroup_post_fork(struct task_struct *p) {}
716 static inline void cgroup_exit(struct task_struct *p) {}
717 static inline void cgroup_release(struct task_struct *p) {}
718 static inline void cgroup_free(struct task_struct *p) {}
719
720 static inline int cgroup_init_early(void) { return 0; }
721 static inline int cgroup_init(void) { return 0; }
722 static inline void cgroup_init_kthreadd(void) {}
723 static inline void cgroup_kthread_ready(void) {}
724 static inline union kernfs_node_id *cgroup_get_kernfs_id(struct cgroup *cgrp)
725 {
726         return NULL;
727 }
728
729 static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
730 {
731         return NULL;
732 }
733
734 static inline struct psi_group *cgroup_psi(struct cgroup *cgrp)
735 {
736         return NULL;
737 }
738
739 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
740                                                struct cgroup *ancestor)
741 {
742         return true;
743 }
744
745 static inline void cgroup_path_from_kernfs_id(const union kernfs_node_id *id,
746         char *buf, size_t buflen) {}
747 #endif /* !CONFIG_CGROUPS */
748
749 #ifdef CONFIG_CGROUPS
750 /*
751  * cgroup scalable recursive statistics.
752  */
753 void cgroup_rstat_updated(struct cgroup *cgrp, int cpu);
754 void cgroup_rstat_flush(struct cgroup *cgrp);
755 void cgroup_rstat_flush_irqsafe(struct cgroup *cgrp);
756 void cgroup_rstat_flush_hold(struct cgroup *cgrp);
757 void cgroup_rstat_flush_release(void);
758
759 /*
760  * Basic resource stats.
761  */
762 #ifdef CONFIG_CGROUP_CPUACCT
763 void cpuacct_charge(struct task_struct *tsk, u64 cputime);
764 void cpuacct_account_field(struct task_struct *tsk, int index, u64 val);
765 #else
766 static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
767 static inline void cpuacct_account_field(struct task_struct *tsk, int index,
768                                          u64 val) {}
769 #endif
770
771 void __cgroup_account_cputime(struct cgroup *cgrp, u64 delta_exec);
772 void __cgroup_account_cputime_field(struct cgroup *cgrp,
773                                     enum cpu_usage_stat index, u64 delta_exec);
774
775 static inline void cgroup_account_cputime(struct task_struct *task,
776                                           u64 delta_exec)
777 {
778         struct cgroup *cgrp;
779
780         cpuacct_charge(task, delta_exec);
781
782         rcu_read_lock();
783         cgrp = task_dfl_cgroup(task);
784         if (cgroup_parent(cgrp))
785                 __cgroup_account_cputime(cgrp, delta_exec);
786         rcu_read_unlock();
787 }
788
789 static inline void cgroup_account_cputime_field(struct task_struct *task,
790                                                 enum cpu_usage_stat index,
791                                                 u64 delta_exec)
792 {
793         struct cgroup *cgrp;
794
795         cpuacct_account_field(task, index, delta_exec);
796
797         rcu_read_lock();
798         cgrp = task_dfl_cgroup(task);
799         if (cgroup_parent(cgrp))
800                 __cgroup_account_cputime_field(cgrp, index, delta_exec);
801         rcu_read_unlock();
802 }
803
804 #else   /* CONFIG_CGROUPS */
805
806 static inline void cgroup_account_cputime(struct task_struct *task,
807                                           u64 delta_exec) {}
808 static inline void cgroup_account_cputime_field(struct task_struct *task,
809                                                 enum cpu_usage_stat index,
810                                                 u64 delta_exec) {}
811
812 #endif  /* CONFIG_CGROUPS */
813
814 /*
815  * sock->sk_cgrp_data handling.  For more info, see sock_cgroup_data
816  * definition in cgroup-defs.h.
817  */
818 #ifdef CONFIG_SOCK_CGROUP_DATA
819
820 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
821 extern spinlock_t cgroup_sk_update_lock;
822 #endif
823
824 void cgroup_sk_alloc_disable(void);
825 void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
826 void cgroup_sk_clone(struct sock_cgroup_data *skcd);
827 void cgroup_sk_free(struct sock_cgroup_data *skcd);
828
829 static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
830 {
831 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
832         unsigned long v;
833
834         /*
835          * @skcd->val is 64bit but the following is safe on 32bit too as we
836          * just need the lower ulong to be written and read atomically.
837          */
838         v = READ_ONCE(skcd->val);
839
840         if (v & 3)
841                 return &cgrp_dfl_root.cgrp;
842
843         return (struct cgroup *)(unsigned long)v ?: &cgrp_dfl_root.cgrp;
844 #else
845         return (struct cgroup *)(unsigned long)skcd->val;
846 #endif
847 }
848
849 #else   /* CONFIG_CGROUP_DATA */
850
851 static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
852 static inline void cgroup_sk_clone(struct sock_cgroup_data *skcd) {}
853 static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
854
855 #endif  /* CONFIG_CGROUP_DATA */
856
857 struct cgroup_namespace {
858         refcount_t              count;
859         struct ns_common        ns;
860         struct user_namespace   *user_ns;
861         struct ucounts          *ucounts;
862         struct css_set          *root_cset;
863 };
864
865 extern struct cgroup_namespace init_cgroup_ns;
866
867 #ifdef CONFIG_CGROUPS
868
869 void free_cgroup_ns(struct cgroup_namespace *ns);
870
871 struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
872                                         struct user_namespace *user_ns,
873                                         struct cgroup_namespace *old_ns);
874
875 int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
876                    struct cgroup_namespace *ns);
877
878 #else /* !CONFIG_CGROUPS */
879
880 static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
881 static inline struct cgroup_namespace *
882 copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
883                struct cgroup_namespace *old_ns)
884 {
885         return old_ns;
886 }
887
888 #endif /* !CONFIG_CGROUPS */
889
890 static inline void get_cgroup_ns(struct cgroup_namespace *ns)
891 {
892         if (ns)
893                 refcount_inc(&ns->count);
894 }
895
896 static inline void put_cgroup_ns(struct cgroup_namespace *ns)
897 {
898         if (ns && refcount_dec_and_test(&ns->count))
899                 free_cgroup_ns(ns);
900 }
901
902 #ifdef CONFIG_CGROUPS
903
904 void cgroup_enter_frozen(void);
905 void cgroup_leave_frozen(bool always_leave);
906 void cgroup_update_frozen(struct cgroup *cgrp);
907 void cgroup_freeze(struct cgroup *cgrp, bool freeze);
908 void cgroup_freezer_migrate_task(struct task_struct *task, struct cgroup *src,
909                                  struct cgroup *dst);
910
911 static inline bool cgroup_task_freeze(struct task_struct *task)
912 {
913         bool ret;
914
915         if (task->flags & PF_KTHREAD)
916                 return false;
917
918         rcu_read_lock();
919         ret = test_bit(CGRP_FREEZE, &task_dfl_cgroup(task)->flags);
920         rcu_read_unlock();
921
922         return ret;
923 }
924
925 static inline bool cgroup_task_frozen(struct task_struct *task)
926 {
927         return task->frozen;
928 }
929
930 #else /* !CONFIG_CGROUPS */
931
932 static inline void cgroup_enter_frozen(void) { }
933 static inline void cgroup_leave_frozen(bool always_leave) { }
934 static inline bool cgroup_task_freeze(struct task_struct *task)
935 {
936         return false;
937 }
938 static inline bool cgroup_task_frozen(struct task_struct *task)
939 {
940         return false;
941 }
942
943 #endif /* !CONFIG_CGROUPS */
944
945 #ifdef CONFIG_CGROUP_BPF
946 static inline void cgroup_bpf_get(struct cgroup *cgrp)
947 {
948         percpu_ref_get(&cgrp->bpf.refcnt);
949 }
950
951 static inline void cgroup_bpf_put(struct cgroup *cgrp)
952 {
953         percpu_ref_put(&cgrp->bpf.refcnt);
954 }
955
956 #else /* CONFIG_CGROUP_BPF */
957
958 static inline void cgroup_bpf_get(struct cgroup *cgrp) {}
959 static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
960
961 #endif /* CONFIG_CGROUP_BPF */
962
963 #endif /* _LINUX_CGROUP_H */