1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_CGROUP_H
3 #define _LINUX_CGROUP_H
7 * Copyright (C) 2003 BULL SA
8 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
12 #include <linux/sched.h>
13 #include <linux/nodemask.h>
14 #include <linux/list.h>
15 #include <linux/rculist.h>
16 #include <linux/cgroupstats.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/notifier.h>
23 #include <linux/ns_common.h>
24 #include <linux/nsproxy.h>
25 #include <linux/user_namespace.h>
26 #include <linux/refcount.h>
27 #include <linux/kernel_stat.h>
29 #include <linux/cgroup-defs.h>
31 struct kernel_clone_args;
34 * All weight knobs on the default hierarchy should use the following min,
35 * default and max values. The default value is the logarithmic center of
36 * MIN and MAX and allows 100x to be expressed in both directions.
38 #define CGROUP_WEIGHT_MIN 1
39 #define CGROUP_WEIGHT_DFL 100
40 #define CGROUP_WEIGHT_MAX 10000
44 enum css_task_iter_flags {
45 CSS_TASK_ITER_PROCS = (1U << 0), /* walk only threadgroup leaders */
46 CSS_TASK_ITER_THREADED = (1U << 1), /* walk all threaded css_sets in the domain */
47 CSS_TASK_ITER_SKIPPED = (1U << 16), /* internal flags */
50 /* a css_task_iter should be treated as an opaque object */
51 struct css_task_iter {
52 struct cgroup_subsys *ss;
55 struct list_head *cset_pos;
56 struct list_head *cset_head;
58 struct list_head *tcset_pos;
59 struct list_head *tcset_head;
61 struct list_head *task_pos;
63 struct list_head *cur_tasks_head;
64 struct css_set *cur_cset;
65 struct css_set *cur_dcset;
66 struct task_struct *cur_task;
67 struct list_head iters_node; /* css_set->task_iters */
70 enum cgroup_lifetime_events {
71 CGROUP_LIFETIME_ONLINE,
72 CGROUP_LIFETIME_OFFLINE,
75 extern struct file_system_type cgroup_fs_type;
76 extern struct cgroup_root cgrp_dfl_root;
77 extern struct css_set init_css_set;
78 extern spinlock_t css_set_lock;
79 extern struct blocking_notifier_head cgroup_lifetime_notifier;
81 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
82 #include <linux/cgroup_subsys.h>
86 extern struct static_key_true _x ## _cgrp_subsys_enabled_key; \
87 extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
88 #include <linux/cgroup_subsys.h>
92 * cgroup_subsys_enabled - fast test on whether a subsys is enabled
93 * @ss: subsystem in question
95 #define cgroup_subsys_enabled(ss) \
96 static_branch_likely(&ss ## _enabled_key)
99 * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
100 * @ss: subsystem in question
102 #define cgroup_subsys_on_dfl(ss) \
103 static_branch_likely(&ss ## _on_dfl_key)
105 bool css_has_online_children(struct cgroup_subsys_state *css);
106 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
107 struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgroup,
108 struct cgroup_subsys *ss);
109 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
110 struct cgroup_subsys *ss);
111 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
112 struct cgroup_subsys *ss);
114 struct cgroup *cgroup_get_from_path(const char *path);
115 struct cgroup *cgroup_get_from_fd(int fd);
116 struct cgroup *cgroup_v1v2_get_from_fd(int fd);
118 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
119 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
121 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
122 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
123 int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
124 int cgroup_rm_cftypes(struct cftype *cfts);
125 void cgroup_file_notify(struct cgroup_file *cfile);
126 void cgroup_file_show(struct cgroup_file *cfile, bool show);
128 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
129 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
130 struct pid *pid, struct task_struct *tsk);
132 void cgroup_fork(struct task_struct *p);
133 extern int cgroup_can_fork(struct task_struct *p,
134 struct kernel_clone_args *kargs);
135 extern void cgroup_cancel_fork(struct task_struct *p,
136 struct kernel_clone_args *kargs);
137 extern void cgroup_post_fork(struct task_struct *p,
138 struct kernel_clone_args *kargs);
139 void cgroup_exit(struct task_struct *p);
140 void cgroup_release(struct task_struct *p);
141 void cgroup_free(struct task_struct *p);
143 int cgroup_init_early(void);
144 int cgroup_init(void);
146 int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v);
149 * Iteration helpers and macros.
152 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
153 struct cgroup_subsys_state *parent);
154 struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
155 struct cgroup_subsys_state *css);
156 struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
157 struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
158 struct cgroup_subsys_state *css);
160 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
161 struct cgroup_subsys_state **dst_cssp);
162 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
163 struct cgroup_subsys_state **dst_cssp);
165 void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
166 struct css_task_iter *it);
167 struct task_struct *css_task_iter_next(struct css_task_iter *it);
168 void css_task_iter_end(struct css_task_iter *it);
171 * css_for_each_child - iterate through children of a css
172 * @pos: the css * to use as the loop cursor
173 * @parent: css whose children to walk
175 * Walk @parent's children. Must be called under rcu_read_lock().
177 * If a subsystem synchronizes ->css_online() and the start of iteration, a
178 * css which finished ->css_online() is guaranteed to be visible in the
179 * future iterations and will stay visible until the last reference is put.
180 * A css which hasn't finished ->css_online() or already finished
181 * ->css_offline() may show up during traversal. It's each subsystem's
182 * responsibility to synchronize against on/offlining.
184 * It is allowed to temporarily drop RCU read lock during iteration. The
185 * caller is responsible for ensuring that @pos remains accessible until
186 * the start of the next iteration by, for example, bumping the css refcnt.
188 #define css_for_each_child(pos, parent) \
189 for ((pos) = css_next_child(NULL, (parent)); (pos); \
190 (pos) = css_next_child((pos), (parent)))
193 * css_for_each_descendant_pre - pre-order walk of a css's descendants
194 * @pos: the css * to use as the loop cursor
195 * @root: css whose descendants to walk
197 * Walk @root's descendants. @root is included in the iteration and the
198 * first node to be visited. Must be called under rcu_read_lock().
200 * If a subsystem synchronizes ->css_online() and the start of iteration, a
201 * css which finished ->css_online() is guaranteed to be visible in the
202 * future iterations and will stay visible until the last reference is put.
203 * A css which hasn't finished ->css_online() or already finished
204 * ->css_offline() may show up during traversal. It's each subsystem's
205 * responsibility to synchronize against on/offlining.
207 * For example, the following guarantees that a descendant can't escape
208 * state updates of its ancestors.
212 * Lock @css's parent and @css;
213 * Inherit state from the parent;
217 * my_update_state(@css)
219 * css_for_each_descendant_pre(@pos, @css) {
222 * Update @css's state;
224 * Verify @pos is alive and inherit state from its parent;
229 * As long as the inheriting step, including checking the parent state, is
230 * enclosed inside @pos locking, double-locking the parent isn't necessary
231 * while inheriting. The state update to the parent is guaranteed to be
232 * visible by walking order and, as long as inheriting operations to the
233 * same @pos are atomic to each other, multiple updates racing each other
234 * still result in the correct state. It's guaranateed that at least one
235 * inheritance happens for any css after the latest update to its parent.
237 * If checking parent's state requires locking the parent, each inheriting
238 * iteration should lock and unlock both @pos->parent and @pos.
240 * Alternatively, a subsystem may choose to use a single global lock to
241 * synchronize ->css_online() and ->css_offline() against tree-walking
244 * It is allowed to temporarily drop RCU read lock during iteration. The
245 * caller is responsible for ensuring that @pos remains accessible until
246 * the start of the next iteration by, for example, bumping the css refcnt.
248 #define css_for_each_descendant_pre(pos, css) \
249 for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
250 (pos) = css_next_descendant_pre((pos), (css)))
253 * css_for_each_descendant_post - post-order walk of a css's descendants
254 * @pos: the css * to use as the loop cursor
255 * @css: css whose descendants to walk
257 * Similar to css_for_each_descendant_pre() but performs post-order
258 * traversal instead. @root is included in the iteration and the last
259 * node to be visited.
261 * If a subsystem synchronizes ->css_online() and the start of iteration, a
262 * css which finished ->css_online() is guaranteed to be visible in the
263 * future iterations and will stay visible until the last reference is put.
264 * A css which hasn't finished ->css_online() or already finished
265 * ->css_offline() may show up during traversal. It's each subsystem's
266 * responsibility to synchronize against on/offlining.
268 * Note that the walk visibility guarantee example described in pre-order
269 * walk doesn't apply the same to post-order walks.
271 #define css_for_each_descendant_post(pos, css) \
272 for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
273 (pos) = css_next_descendant_post((pos), (css)))
276 * cgroup_taskset_for_each - iterate cgroup_taskset
277 * @task: the loop cursor
278 * @dst_css: the destination css
279 * @tset: taskset to iterate
281 * @tset may contain multiple tasks and they may belong to multiple
284 * On the v2 hierarchy, there may be tasks from multiple processes and they
285 * may not share the source or destination csses.
287 * On traditional hierarchies, when there are multiple tasks in @tset, if a
288 * task of a process is in @tset, all tasks of the process are in @tset.
289 * Also, all are guaranteed to share the same source and destination csses.
291 * Iteration is not in any specific order.
293 #define cgroup_taskset_for_each(task, dst_css, tset) \
294 for ((task) = cgroup_taskset_first((tset), &(dst_css)); \
296 (task) = cgroup_taskset_next((tset), &(dst_css)))
299 * cgroup_taskset_for_each_leader - iterate group leaders in a cgroup_taskset
300 * @leader: the loop cursor
301 * @dst_css: the destination css
302 * @tset: taskset to iterate
304 * Iterate threadgroup leaders of @tset. For single-task migrations, @tset
305 * may not contain any.
307 #define cgroup_taskset_for_each_leader(leader, dst_css, tset) \
308 for ((leader) = cgroup_taskset_first((tset), &(dst_css)); \
310 (leader) = cgroup_taskset_next((tset), &(dst_css))) \
311 if ((leader) != (leader)->group_leader) \
319 #ifdef CONFIG_DEBUG_CGROUP_REF
320 void css_get(struct cgroup_subsys_state *css);
321 void css_get_many(struct cgroup_subsys_state *css, unsigned int n);
322 bool css_tryget(struct cgroup_subsys_state *css);
323 bool css_tryget_online(struct cgroup_subsys_state *css);
324 void css_put(struct cgroup_subsys_state *css);
325 void css_put_many(struct cgroup_subsys_state *css, unsigned int n);
327 #define CGROUP_REF_FN_ATTRS static inline
328 #define CGROUP_REF_EXPORT(fn)
329 #include <linux/cgroup_refcnt.h>
332 static inline u64 cgroup_id(const struct cgroup *cgrp)
338 * css_is_dying - test whether the specified css is dying
341 * Test whether @css is in the process of offlining or already offline. In
342 * most cases, ->css_online() and ->css_offline() callbacks should be
343 * enough; however, the actual offline operations are RCU delayed and this
344 * test returns %true also when @css is scheduled to be offlined.
346 * This is useful, for example, when the use case requires synchronous
347 * behavior with respect to cgroup removal. cgroup removal schedules css
348 * offlining but the css can seem alive while the operation is being
349 * delayed. If the delay affects user visible semantics, this test can be
350 * used to resolve the situation.
352 static inline bool css_is_dying(struct cgroup_subsys_state *css)
354 return css->flags & CSS_DYING;
357 static inline bool css_is_self(struct cgroup_subsys_state *css)
359 if (css == &css->cgroup->self) {
360 /* cgroup::self should not have subsystem association */
361 WARN_ON(css->ss != NULL);
368 static inline void cgroup_get(struct cgroup *cgrp)
370 css_get(&cgrp->self);
373 static inline bool cgroup_tryget(struct cgroup *cgrp)
375 return css_tryget(&cgrp->self);
378 static inline void cgroup_put(struct cgroup *cgrp)
380 css_put(&cgrp->self);
383 extern struct mutex cgroup_mutex;
385 static inline void cgroup_lock(void)
387 mutex_lock(&cgroup_mutex);
390 static inline void cgroup_unlock(void)
392 mutex_unlock(&cgroup_mutex);
396 * task_css_set_check - obtain a task's css_set with extra access conditions
397 * @task: the task to obtain css_set for
398 * @__c: extra condition expression to be passed to rcu_dereference_check()
400 * A task's css_set is RCU protected, initialized and exited while holding
401 * task_lock(), and can only be modified while holding both cgroup_mutex
402 * and task_lock() while the task is alive. This macro verifies that the
403 * caller is inside proper critical section and returns @task's css_set.
405 * The caller can also specify additional allowed conditions via @__c, such
406 * as locks used during the cgroup_subsys::attach() methods.
408 #ifdef CONFIG_PROVE_RCU
409 #define task_css_set_check(task, __c) \
410 rcu_dereference_check((task)->cgroups, \
411 rcu_read_lock_sched_held() || \
412 lockdep_is_held(&cgroup_mutex) || \
413 lockdep_is_held(&css_set_lock) || \
414 ((task)->flags & PF_EXITING) || (__c))
416 #define task_css_set_check(task, __c) \
417 rcu_dereference((task)->cgroups)
421 * task_css_check - obtain css for (task, subsys) w/ extra access conds
422 * @task: the target task
423 * @subsys_id: the target subsystem ID
424 * @__c: extra condition expression to be passed to rcu_dereference_check()
426 * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
427 * synchronization rules are the same as task_css_set_check().
429 #define task_css_check(task, subsys_id, __c) \
430 task_css_set_check((task), (__c))->subsys[(subsys_id)]
433 * task_css_set - obtain a task's css_set
434 * @task: the task to obtain css_set for
436 * See task_css_set_check().
438 static inline struct css_set *task_css_set(struct task_struct *task)
440 return task_css_set_check(task, false);
444 * task_css - obtain css for (task, subsys)
445 * @task: the target task
446 * @subsys_id: the target subsystem ID
448 * See task_css_check().
450 static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
453 return task_css_check(task, subsys_id, false);
457 * task_get_css - find and get the css for (task, subsys)
458 * @task: the target task
459 * @subsys_id: the target subsystem ID
461 * Find the css for the (@task, @subsys_id) combination, increment a
462 * reference on and return it. This function is guaranteed to return a
463 * valid css. The returned css may already have been offlined.
465 static inline struct cgroup_subsys_state *
466 task_get_css(struct task_struct *task, int subsys_id)
468 struct cgroup_subsys_state *css;
472 css = task_css(task, subsys_id);
474 * Can't use css_tryget_online() here. A task which has
475 * PF_EXITING set may stay associated with an offline css.
476 * If such task calls this function, css_tryget_online()
479 if (likely(css_tryget(css)))
488 * task_css_is_root - test whether a task belongs to the root css
489 * @task: the target task
490 * @subsys_id: the target subsystem ID
492 * Test whether @task belongs to the root css on the specified subsystem.
493 * May be invoked in any context.
495 static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
497 return task_css_check(task, subsys_id, true) ==
498 init_css_set.subsys[subsys_id];
501 static inline struct cgroup *task_cgroup(struct task_struct *task,
504 return task_css(task, subsys_id)->cgroup;
507 static inline struct cgroup *task_dfl_cgroup(struct task_struct *task)
509 return task_css_set(task)->dfl_cgrp;
512 static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
514 struct cgroup_subsys_state *parent_css = cgrp->self.parent;
517 return container_of(parent_css, struct cgroup, self);
522 * cgroup_is_descendant - test ancestry
523 * @cgrp: the cgroup to be tested
524 * @ancestor: possible ancestor of @cgrp
526 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
527 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
528 * and @ancestor are accessible.
530 static inline bool cgroup_is_descendant(struct cgroup *cgrp,
531 struct cgroup *ancestor)
533 if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
535 return cgrp->ancestors[ancestor->level] == ancestor;
539 * cgroup_ancestor - find ancestor of cgroup
540 * @cgrp: cgroup to find ancestor of
541 * @ancestor_level: level of ancestor to find starting from root
543 * Find ancestor of cgroup at specified level starting from root if it exists
544 * and return pointer to it. Return NULL if @cgrp doesn't have ancestor at
547 * This function is safe to call as long as @cgrp is accessible.
549 static inline struct cgroup *cgroup_ancestor(struct cgroup *cgrp,
552 if (ancestor_level < 0 || ancestor_level > cgrp->level)
554 return cgrp->ancestors[ancestor_level];
558 * task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
559 * @task: the task to be tested
560 * @ancestor: possible ancestor of @task's cgroup
562 * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
563 * It follows all the same rules as cgroup_is_descendant, and only applies
564 * to the default hierarchy.
566 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
567 struct cgroup *ancestor)
569 struct css_set *cset = task_css_set(task);
571 return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
574 /* no synchronization, the result can only be used as a hint */
575 static inline bool cgroup_is_populated(struct cgroup *cgrp)
577 return cgrp->nr_populated_csets + cgrp->nr_populated_domain_children +
578 cgrp->nr_populated_threaded_children;
581 /* returns ino associated with a cgroup */
582 static inline ino_t cgroup_ino(struct cgroup *cgrp)
584 return kernfs_ino(cgrp->kn);
587 /* cft/css accessors for cftype->write() operation */
588 static inline struct cftype *of_cft(struct kernfs_open_file *of)
593 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
595 /* cft/css accessors for cftype->seq_*() operations */
596 static inline struct cftype *seq_cft(struct seq_file *seq)
598 return of_cft(seq->private);
601 static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
603 return of_css(seq->private);
607 * Name / path handling functions. All are thin wrappers around the kernfs
608 * counterparts and can be called under any context.
611 static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
613 return kernfs_name(cgrp->kn, buf, buflen);
616 static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
618 return kernfs_path(cgrp->kn, buf, buflen);
621 static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
623 pr_cont_kernfs_name(cgrp->kn);
626 static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
628 pr_cont_kernfs_path(cgrp->kn);
631 bool cgroup_psi_enabled(void);
633 static inline void cgroup_init_kthreadd(void)
636 * kthreadd is inherited by all kthreads, keep it in the root so
637 * that the new kthreads are guaranteed to stay in the root until
638 * initialization is finished.
640 current->no_cgroup_migration = 1;
643 static inline void cgroup_kthread_ready(void)
646 * This kthread finished initialization. The creator should have
647 * set PF_NO_SETAFFINITY if this kthread should stay in the root.
649 current->no_cgroup_migration = 0;
652 void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen);
653 struct cgroup *cgroup_get_from_id(u64 id);
654 #else /* !CONFIG_CGROUPS */
656 struct cgroup_subsys_state;
659 static inline u64 cgroup_id(const struct cgroup *cgrp) { return 1; }
660 static inline void css_get(struct cgroup_subsys_state *css) {}
661 static inline void css_put(struct cgroup_subsys_state *css) {}
662 static inline void cgroup_lock(void) {}
663 static inline void cgroup_unlock(void) {}
664 static inline int cgroup_attach_task_all(struct task_struct *from,
665 struct task_struct *t) { return 0; }
666 static inline int cgroupstats_build(struct cgroupstats *stats,
667 struct dentry *dentry) { return -EINVAL; }
669 static inline void cgroup_fork(struct task_struct *p) {}
670 static inline int cgroup_can_fork(struct task_struct *p,
671 struct kernel_clone_args *kargs) { return 0; }
672 static inline void cgroup_cancel_fork(struct task_struct *p,
673 struct kernel_clone_args *kargs) {}
674 static inline void cgroup_post_fork(struct task_struct *p,
675 struct kernel_clone_args *kargs) {}
676 static inline void cgroup_exit(struct task_struct *p) {}
677 static inline void cgroup_release(struct task_struct *p) {}
678 static inline void cgroup_free(struct task_struct *p) {}
680 static inline int cgroup_init_early(void) { return 0; }
681 static inline int cgroup_init(void) { return 0; }
682 static inline void cgroup_init_kthreadd(void) {}
683 static inline void cgroup_kthread_ready(void) {}
685 static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
690 static inline bool cgroup_psi_enabled(void)
695 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
696 struct cgroup *ancestor)
701 static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
703 #endif /* !CONFIG_CGROUPS */
705 #ifdef CONFIG_CGROUPS
707 * cgroup scalable recursive statistics.
709 void css_rstat_updated(struct cgroup_subsys_state *css, int cpu);
710 void css_rstat_flush(struct cgroup_subsys_state *css);
713 * Basic resource stats.
715 #ifdef CONFIG_CGROUP_CPUACCT
716 void cpuacct_charge(struct task_struct *tsk, u64 cputime);
717 void cpuacct_account_field(struct task_struct *tsk, int index, u64 val);
719 static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
720 static inline void cpuacct_account_field(struct task_struct *tsk, int index,
724 void __cgroup_account_cputime(struct cgroup *cgrp, u64 delta_exec);
725 void __cgroup_account_cputime_field(struct cgroup *cgrp,
726 enum cpu_usage_stat index, u64 delta_exec);
728 static inline void cgroup_account_cputime(struct task_struct *task,
733 cpuacct_charge(task, delta_exec);
735 cgrp = task_dfl_cgroup(task);
736 if (cgroup_parent(cgrp))
737 __cgroup_account_cputime(cgrp, delta_exec);
740 static inline void cgroup_account_cputime_field(struct task_struct *task,
741 enum cpu_usage_stat index,
746 cpuacct_account_field(task, index, delta_exec);
748 cgrp = task_dfl_cgroup(task);
749 if (cgroup_parent(cgrp))
750 __cgroup_account_cputime_field(cgrp, index, delta_exec);
753 #else /* CONFIG_CGROUPS */
755 static inline void cgroup_account_cputime(struct task_struct *task,
757 static inline void cgroup_account_cputime_field(struct task_struct *task,
758 enum cpu_usage_stat index,
761 #endif /* CONFIG_CGROUPS */
764 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
765 * definition in cgroup-defs.h.
767 #ifdef CONFIG_SOCK_CGROUP_DATA
769 void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
770 void cgroup_sk_clone(struct sock_cgroup_data *skcd);
771 void cgroup_sk_free(struct sock_cgroup_data *skcd);
773 static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
778 #else /* CONFIG_CGROUP_DATA */
780 static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
781 static inline void cgroup_sk_clone(struct sock_cgroup_data *skcd) {}
782 static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
784 #endif /* CONFIG_CGROUP_DATA */
786 struct cgroup_namespace {
788 struct user_namespace *user_ns;
789 struct ucounts *ucounts;
790 struct css_set *root_cset;
793 extern struct cgroup_namespace init_cgroup_ns;
795 #ifdef CONFIG_CGROUPS
797 void free_cgroup_ns(struct cgroup_namespace *ns);
799 struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
800 struct user_namespace *user_ns,
801 struct cgroup_namespace *old_ns);
803 int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
804 struct cgroup_namespace *ns);
806 static inline void get_cgroup_ns(struct cgroup_namespace *ns)
808 refcount_inc(&ns->ns.count);
811 static inline void put_cgroup_ns(struct cgroup_namespace *ns)
813 if (refcount_dec_and_test(&ns->ns.count))
817 #else /* !CONFIG_CGROUPS */
819 static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
820 static inline struct cgroup_namespace *
821 copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
822 struct cgroup_namespace *old_ns)
827 static inline void get_cgroup_ns(struct cgroup_namespace *ns) { }
828 static inline void put_cgroup_ns(struct cgroup_namespace *ns) { }
830 #endif /* !CONFIG_CGROUPS */
832 #ifdef CONFIG_CGROUPS
834 void cgroup_enter_frozen(void);
835 void cgroup_leave_frozen(bool always_leave);
836 void cgroup_update_frozen(struct cgroup *cgrp);
837 void cgroup_freeze(struct cgroup *cgrp, bool freeze);
838 void cgroup_freezer_migrate_task(struct task_struct *task, struct cgroup *src,
841 static inline bool cgroup_task_frozen(struct task_struct *task)
846 #else /* !CONFIG_CGROUPS */
848 static inline void cgroup_enter_frozen(void) { }
849 static inline void cgroup_leave_frozen(bool always_leave) { }
850 static inline bool cgroup_task_frozen(struct task_struct *task)
855 #endif /* !CONFIG_CGROUPS */
857 #ifdef CONFIG_CGROUP_BPF
858 static inline void cgroup_bpf_get(struct cgroup *cgrp)
860 percpu_ref_get(&cgrp->bpf.refcnt);
863 static inline void cgroup_bpf_put(struct cgroup *cgrp)
865 percpu_ref_put(&cgrp->bpf.refcnt);
868 #else /* CONFIG_CGROUP_BPF */
870 static inline void cgroup_bpf_get(struct cgroup *cgrp) {}
871 static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
873 #endif /* CONFIG_CGROUP_BPF */
875 struct cgroup *task_get_cgroup1(struct task_struct *tsk, int hierarchy_id);
877 struct cgroup_of_peak *of_peak(struct kernfs_open_file *of);
879 #endif /* _LINUX_CGROUP_H */