Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_CGROUP_H
0003 #define _LINUX_CGROUP_H
0004 /*
0005  *  cgroup interface
0006  *
0007  *  Copyright (C) 2003 BULL SA
0008  *  Copyright (C) 2004-2006 Silicon Graphics, Inc.
0009  *
0010  */
0011 
0012 #include <linux/sched.h>
0013 #include <linux/cpumask.h>
0014 #include <linux/nodemask.h>
0015 #include <linux/rculist.h>
0016 #include <linux/cgroupstats.h>
0017 #include <linux/fs.h>
0018 #include <linux/seq_file.h>
0019 #include <linux/kernfs.h>
0020 #include <linux/jump_label.h>
0021 #include <linux/types.h>
0022 #include <linux/ns_common.h>
0023 #include <linux/nsproxy.h>
0024 #include <linux/user_namespace.h>
0025 #include <linux/refcount.h>
0026 #include <linux/kernel_stat.h>
0027 
0028 #include <linux/cgroup-defs.h>
0029 
0030 struct kernel_clone_args;
0031 
0032 #ifdef CONFIG_CGROUPS
0033 
0034 /*
0035  * All weight knobs on the default hierarchy should use the following min,
0036  * default and max values.  The default value is the logarithmic center of
0037  * MIN and MAX and allows 100x to be expressed in both directions.
0038  */
0039 #define CGROUP_WEIGHT_MIN       1
0040 #define CGROUP_WEIGHT_DFL       100
0041 #define CGROUP_WEIGHT_MAX       10000
0042 
0043 /* walk only threadgroup leaders */
0044 #define CSS_TASK_ITER_PROCS     (1U << 0)
0045 /* walk all threaded css_sets in the domain */
0046 #define CSS_TASK_ITER_THREADED      (1U << 1)
0047 
0048 /* internal flags */
0049 #define CSS_TASK_ITER_SKIPPED       (1U << 16)
0050 
0051 /* a css_task_iter should be treated as an opaque object */
0052 struct css_task_iter {
0053     struct cgroup_subsys        *ss;
0054     unsigned int            flags;
0055 
0056     struct list_head        *cset_pos;
0057     struct list_head        *cset_head;
0058 
0059     struct list_head        *tcset_pos;
0060     struct list_head        *tcset_head;
0061 
0062     struct list_head        *task_pos;
0063 
0064     struct list_head        *cur_tasks_head;
0065     struct css_set          *cur_cset;
0066     struct css_set          *cur_dcset;
0067     struct task_struct      *cur_task;
0068     struct list_head        iters_node; /* css_set->task_iters */
0069 };
0070 
0071 extern struct cgroup_root cgrp_dfl_root;
0072 extern struct css_set init_css_set;
0073 
0074 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
0075 #include <linux/cgroup_subsys.h>
0076 #undef SUBSYS
0077 
0078 #define SUBSYS(_x)                              \
0079     extern struct static_key_true _x ## _cgrp_subsys_enabled_key;       \
0080     extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
0081 #include <linux/cgroup_subsys.h>
0082 #undef SUBSYS
0083 
0084 /**
0085  * cgroup_subsys_enabled - fast test on whether a subsys is enabled
0086  * @ss: subsystem in question
0087  */
0088 #define cgroup_subsys_enabled(ss)                       \
0089     static_branch_likely(&ss ## _enabled_key)
0090 
0091 /**
0092  * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
0093  * @ss: subsystem in question
0094  */
0095 #define cgroup_subsys_on_dfl(ss)                        \
0096     static_branch_likely(&ss ## _on_dfl_key)
0097 
0098 bool css_has_online_children(struct cgroup_subsys_state *css);
0099 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
0100 struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgroup,
0101                      struct cgroup_subsys *ss);
0102 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
0103                          struct cgroup_subsys *ss);
0104 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
0105                                struct cgroup_subsys *ss);
0106 
0107 struct cgroup *cgroup_get_from_path(const char *path);
0108 struct cgroup *cgroup_get_from_fd(int fd);
0109 
0110 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
0111 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
0112 
0113 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
0114 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
0115 int cgroup_rm_cftypes(struct cftype *cfts);
0116 void cgroup_file_notify(struct cgroup_file *cfile);
0117 
0118 int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
0119 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
0120 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
0121              struct pid *pid, struct task_struct *tsk);
0122 
0123 void cgroup_fork(struct task_struct *p);
0124 extern int cgroup_can_fork(struct task_struct *p,
0125                struct kernel_clone_args *kargs);
0126 extern void cgroup_cancel_fork(struct task_struct *p,
0127                    struct kernel_clone_args *kargs);
0128 extern void cgroup_post_fork(struct task_struct *p,
0129                  struct kernel_clone_args *kargs);
0130 void cgroup_exit(struct task_struct *p);
0131 void cgroup_release(struct task_struct *p);
0132 void cgroup_free(struct task_struct *p);
0133 
0134 int cgroup_init_early(void);
0135 int cgroup_init(void);
0136 
0137 int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v);
0138 
0139 /*
0140  * Iteration helpers and macros.
0141  */
0142 
0143 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
0144                        struct cgroup_subsys_state *parent);
0145 struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
0146                             struct cgroup_subsys_state *css);
0147 struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
0148 struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
0149                              struct cgroup_subsys_state *css);
0150 
0151 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
0152                      struct cgroup_subsys_state **dst_cssp);
0153 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
0154                     struct cgroup_subsys_state **dst_cssp);
0155 
0156 void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
0157              struct css_task_iter *it);
0158 struct task_struct *css_task_iter_next(struct css_task_iter *it);
0159 void css_task_iter_end(struct css_task_iter *it);
0160 
0161 /**
0162  * css_for_each_child - iterate through children of a css
0163  * @pos: the css * to use as the loop cursor
0164  * @parent: css whose children to walk
0165  *
0166  * Walk @parent's children.  Must be called under rcu_read_lock().
0167  *
0168  * If a subsystem synchronizes ->css_online() and the start of iteration, a
0169  * css which finished ->css_online() is guaranteed to be visible in the
0170  * future iterations and will stay visible until the last reference is put.
0171  * A css which hasn't finished ->css_online() or already finished
0172  * ->css_offline() may show up during traversal.  It's each subsystem's
0173  * responsibility to synchronize against on/offlining.
0174  *
0175  * It is allowed to temporarily drop RCU read lock during iteration.  The
0176  * caller is responsible for ensuring that @pos remains accessible until
0177  * the start of the next iteration by, for example, bumping the css refcnt.
0178  */
0179 #define css_for_each_child(pos, parent)                 \
0180     for ((pos) = css_next_child(NULL, (parent)); (pos);     \
0181          (pos) = css_next_child((pos), (parent)))
0182 
0183 /**
0184  * css_for_each_descendant_pre - pre-order walk of a css's descendants
0185  * @pos: the css * to use as the loop cursor
0186  * @root: css whose descendants to walk
0187  *
0188  * Walk @root's descendants.  @root is included in the iteration and the
0189  * first node to be visited.  Must be called under rcu_read_lock().
0190  *
0191  * If a subsystem synchronizes ->css_online() and the start of iteration, a
0192  * css which finished ->css_online() is guaranteed to be visible in the
0193  * future iterations and will stay visible until the last reference is put.
0194  * A css which hasn't finished ->css_online() or already finished
0195  * ->css_offline() may show up during traversal.  It's each subsystem's
0196  * responsibility to synchronize against on/offlining.
0197  *
0198  * For example, the following guarantees that a descendant can't escape
0199  * state updates of its ancestors.
0200  *
0201  * my_online(@css)
0202  * {
0203  *  Lock @css's parent and @css;
0204  *  Inherit state from the parent;
0205  *  Unlock both.
0206  * }
0207  *
0208  * my_update_state(@css)
0209  * {
0210  *  css_for_each_descendant_pre(@pos, @css) {
0211  *      Lock @pos;
0212  *      if (@pos == @css)
0213  *          Update @css's state;
0214  *      else
0215  *          Verify @pos is alive and inherit state from its parent;
0216  *      Unlock @pos;
0217  *  }
0218  * }
0219  *
0220  * As long as the inheriting step, including checking the parent state, is
0221  * enclosed inside @pos locking, double-locking the parent isn't necessary
0222  * while inheriting.  The state update to the parent is guaranteed to be
0223  * visible by walking order and, as long as inheriting operations to the
0224  * same @pos are atomic to each other, multiple updates racing each other
0225  * still result in the correct state.  It's guaranateed that at least one
0226  * inheritance happens for any css after the latest update to its parent.
0227  *
0228  * If checking parent's state requires locking the parent, each inheriting
0229  * iteration should lock and unlock both @pos->parent and @pos.
0230  *
0231  * Alternatively, a subsystem may choose to use a single global lock to
0232  * synchronize ->css_online() and ->css_offline() against tree-walking
0233  * operations.
0234  *
0235  * It is allowed to temporarily drop RCU read lock during iteration.  The
0236  * caller is responsible for ensuring that @pos remains accessible until
0237  * the start of the next iteration by, for example, bumping the css refcnt.
0238  */
0239 #define css_for_each_descendant_pre(pos, css)               \
0240     for ((pos) = css_next_descendant_pre(NULL, (css)); (pos);   \
0241          (pos) = css_next_descendant_pre((pos), (css)))
0242 
0243 /**
0244  * css_for_each_descendant_post - post-order walk of a css's descendants
0245  * @pos: the css * to use as the loop cursor
0246  * @css: css whose descendants to walk
0247  *
0248  * Similar to css_for_each_descendant_pre() but performs post-order
0249  * traversal instead.  @root is included in the iteration and the last
0250  * node to be visited.
0251  *
0252  * If a subsystem synchronizes ->css_online() and the start of iteration, a
0253  * css which finished ->css_online() is guaranteed to be visible in the
0254  * future iterations and will stay visible until the last reference is put.
0255  * A css which hasn't finished ->css_online() or already finished
0256  * ->css_offline() may show up during traversal.  It's each subsystem's
0257  * responsibility to synchronize against on/offlining.
0258  *
0259  * Note that the walk visibility guarantee example described in pre-order
0260  * walk doesn't apply the same to post-order walks.
0261  */
0262 #define css_for_each_descendant_post(pos, css)              \
0263     for ((pos) = css_next_descendant_post(NULL, (css)); (pos);  \
0264          (pos) = css_next_descendant_post((pos), (css)))
0265 
0266 /**
0267  * cgroup_taskset_for_each - iterate cgroup_taskset
0268  * @task: the loop cursor
0269  * @dst_css: the destination css
0270  * @tset: taskset to iterate
0271  *
0272  * @tset may contain multiple tasks and they may belong to multiple
0273  * processes.
0274  *
0275  * On the v2 hierarchy, there may be tasks from multiple processes and they
0276  * may not share the source or destination csses.
0277  *
0278  * On traditional hierarchies, when there are multiple tasks in @tset, if a
0279  * task of a process is in @tset, all tasks of the process are in @tset.
0280  * Also, all are guaranteed to share the same source and destination csses.
0281  *
0282  * Iteration is not in any specific order.
0283  */
0284 #define cgroup_taskset_for_each(task, dst_css, tset)            \
0285     for ((task) = cgroup_taskset_first((tset), &(dst_css));     \
0286          (task);                            \
0287          (task) = cgroup_taskset_next((tset), &(dst_css)))
0288 
0289 /**
0290  * cgroup_taskset_for_each_leader - iterate group leaders in a cgroup_taskset
0291  * @leader: the loop cursor
0292  * @dst_css: the destination css
0293  * @tset: taskset to iterate
0294  *
0295  * Iterate threadgroup leaders of @tset.  For single-task migrations, @tset
0296  * may not contain any.
0297  */
0298 #define cgroup_taskset_for_each_leader(leader, dst_css, tset)       \
0299     for ((leader) = cgroup_taskset_first((tset), &(dst_css));   \
0300          (leader);                          \
0301          (leader) = cgroup_taskset_next((tset), &(dst_css)))    \
0302         if ((leader) != (leader)->group_leader)         \
0303             ;                       \
0304         else
0305 
0306 /*
0307  * Inline functions.
0308  */
0309 
0310 static inline u64 cgroup_id(const struct cgroup *cgrp)
0311 {
0312     return cgrp->kn->id;
0313 }
0314 
0315 /**
0316  * css_get - obtain a reference on the specified css
0317  * @css: target css
0318  *
0319  * The caller must already have a reference.
0320  */
0321 static inline void css_get(struct cgroup_subsys_state *css)
0322 {
0323     if (!(css->flags & CSS_NO_REF))
0324         percpu_ref_get(&css->refcnt);
0325 }
0326 
0327 /**
0328  * css_get_many - obtain references on the specified css
0329  * @css: target css
0330  * @n: number of references to get
0331  *
0332  * The caller must already have a reference.
0333  */
0334 static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
0335 {
0336     if (!(css->flags & CSS_NO_REF))
0337         percpu_ref_get_many(&css->refcnt, n);
0338 }
0339 
0340 /**
0341  * css_tryget - try to obtain a reference on the specified css
0342  * @css: target css
0343  *
0344  * Obtain a reference on @css unless it already has reached zero and is
0345  * being released.  This function doesn't care whether @css is on or
0346  * offline.  The caller naturally needs to ensure that @css is accessible
0347  * but doesn't have to be holding a reference on it - IOW, RCU protected
0348  * access is good enough for this function.  Returns %true if a reference
0349  * count was successfully obtained; %false otherwise.
0350  */
0351 static inline bool css_tryget(struct cgroup_subsys_state *css)
0352 {
0353     if (!(css->flags & CSS_NO_REF))
0354         return percpu_ref_tryget(&css->refcnt);
0355     return true;
0356 }
0357 
0358 /**
0359  * css_tryget_online - try to obtain a reference on the specified css if online
0360  * @css: target css
0361  *
0362  * Obtain a reference on @css if it's online.  The caller naturally needs
0363  * to ensure that @css is accessible but doesn't have to be holding a
0364  * reference on it - IOW, RCU protected access is good enough for this
0365  * function.  Returns %true if a reference count was successfully obtained;
0366  * %false otherwise.
0367  */
0368 static inline bool css_tryget_online(struct cgroup_subsys_state *css)
0369 {
0370     if (!(css->flags & CSS_NO_REF))
0371         return percpu_ref_tryget_live(&css->refcnt);
0372     return true;
0373 }
0374 
0375 /**
0376  * css_is_dying - test whether the specified css is dying
0377  * @css: target css
0378  *
0379  * Test whether @css is in the process of offlining or already offline.  In
0380  * most cases, ->css_online() and ->css_offline() callbacks should be
0381  * enough; however, the actual offline operations are RCU delayed and this
0382  * test returns %true also when @css is scheduled to be offlined.
0383  *
0384  * This is useful, for example, when the use case requires synchronous
0385  * behavior with respect to cgroup removal.  cgroup removal schedules css
0386  * offlining but the css can seem alive while the operation is being
0387  * delayed.  If the delay affects user visible semantics, this test can be
0388  * used to resolve the situation.
0389  */
0390 static inline bool css_is_dying(struct cgroup_subsys_state *css)
0391 {
0392     return !(css->flags & CSS_NO_REF) && percpu_ref_is_dying(&css->refcnt);
0393 }
0394 
0395 /**
0396  * css_put - put a css reference
0397  * @css: target css
0398  *
0399  * Put a reference obtained via css_get() and css_tryget_online().
0400  */
0401 static inline void css_put(struct cgroup_subsys_state *css)
0402 {
0403     if (!(css->flags & CSS_NO_REF))
0404         percpu_ref_put(&css->refcnt);
0405 }
0406 
0407 /**
0408  * css_put_many - put css references
0409  * @css: target css
0410  * @n: number of references to put
0411  *
0412  * Put references obtained via css_get() and css_tryget_online().
0413  */
0414 static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
0415 {
0416     if (!(css->flags & CSS_NO_REF))
0417         percpu_ref_put_many(&css->refcnt, n);
0418 }
0419 
0420 static inline void cgroup_get(struct cgroup *cgrp)
0421 {
0422     css_get(&cgrp->self);
0423 }
0424 
0425 static inline bool cgroup_tryget(struct cgroup *cgrp)
0426 {
0427     return css_tryget(&cgrp->self);
0428 }
0429 
0430 static inline void cgroup_put(struct cgroup *cgrp)
0431 {
0432     css_put(&cgrp->self);
0433 }
0434 
0435 /**
0436  * task_css_set_check - obtain a task's css_set with extra access conditions
0437  * @task: the task to obtain css_set for
0438  * @__c: extra condition expression to be passed to rcu_dereference_check()
0439  *
0440  * A task's css_set is RCU protected, initialized and exited while holding
0441  * task_lock(), and can only be modified while holding both cgroup_mutex
0442  * and task_lock() while the task is alive.  This macro verifies that the
0443  * caller is inside proper critical section and returns @task's css_set.
0444  *
0445  * The caller can also specify additional allowed conditions via @__c, such
0446  * as locks used during the cgroup_subsys::attach() methods.
0447  */
0448 #ifdef CONFIG_PROVE_RCU
0449 extern struct mutex cgroup_mutex;
0450 extern spinlock_t css_set_lock;
0451 #define task_css_set_check(task, __c)                   \
0452     rcu_dereference_check((task)->cgroups,              \
0453         rcu_read_lock_sched_held() ||               \
0454         lockdep_is_held(&cgroup_mutex) ||           \
0455         lockdep_is_held(&css_set_lock) ||           \
0456         ((task)->flags & PF_EXITING) || (__c))
0457 #else
0458 #define task_css_set_check(task, __c)                   \
0459     rcu_dereference((task)->cgroups)
0460 #endif
0461 
0462 /**
0463  * task_css_check - obtain css for (task, subsys) w/ extra access conds
0464  * @task: the target task
0465  * @subsys_id: the target subsystem ID
0466  * @__c: extra condition expression to be passed to rcu_dereference_check()
0467  *
0468  * Return the cgroup_subsys_state for the (@task, @subsys_id) pair.  The
0469  * synchronization rules are the same as task_css_set_check().
0470  */
0471 #define task_css_check(task, subsys_id, __c)                \
0472     task_css_set_check((task), (__c))->subsys[(subsys_id)]
0473 
0474 /**
0475  * task_css_set - obtain a task's css_set
0476  * @task: the task to obtain css_set for
0477  *
0478  * See task_css_set_check().
0479  */
0480 static inline struct css_set *task_css_set(struct task_struct *task)
0481 {
0482     return task_css_set_check(task, false);
0483 }
0484 
0485 /**
0486  * task_css - obtain css for (task, subsys)
0487  * @task: the target task
0488  * @subsys_id: the target subsystem ID
0489  *
0490  * See task_css_check().
0491  */
0492 static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
0493                            int subsys_id)
0494 {
0495     return task_css_check(task, subsys_id, false);
0496 }
0497 
0498 /**
0499  * task_get_css - find and get the css for (task, subsys)
0500  * @task: the target task
0501  * @subsys_id: the target subsystem ID
0502  *
0503  * Find the css for the (@task, @subsys_id) combination, increment a
0504  * reference on and return it.  This function is guaranteed to return a
0505  * valid css.  The returned css may already have been offlined.
0506  */
0507 static inline struct cgroup_subsys_state *
0508 task_get_css(struct task_struct *task, int subsys_id)
0509 {
0510     struct cgroup_subsys_state *css;
0511 
0512     rcu_read_lock();
0513     while (true) {
0514         css = task_css(task, subsys_id);
0515         /*
0516          * Can't use css_tryget_online() here.  A task which has
0517          * PF_EXITING set may stay associated with an offline css.
0518          * If such task calls this function, css_tryget_online()
0519          * will keep failing.
0520          */
0521         if (likely(css_tryget(css)))
0522             break;
0523         cpu_relax();
0524     }
0525     rcu_read_unlock();
0526     return css;
0527 }
0528 
0529 /**
0530  * task_css_is_root - test whether a task belongs to the root css
0531  * @task: the target task
0532  * @subsys_id: the target subsystem ID
0533  *
0534  * Test whether @task belongs to the root css on the specified subsystem.
0535  * May be invoked in any context.
0536  */
0537 static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
0538 {
0539     return task_css_check(task, subsys_id, true) ==
0540         init_css_set.subsys[subsys_id];
0541 }
0542 
0543 static inline struct cgroup *task_cgroup(struct task_struct *task,
0544                      int subsys_id)
0545 {
0546     return task_css(task, subsys_id)->cgroup;
0547 }
0548 
0549 static inline struct cgroup *task_dfl_cgroup(struct task_struct *task)
0550 {
0551     return task_css_set(task)->dfl_cgrp;
0552 }
0553 
0554 static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
0555 {
0556     struct cgroup_subsys_state *parent_css = cgrp->self.parent;
0557 
0558     if (parent_css)
0559         return container_of(parent_css, struct cgroup, self);
0560     return NULL;
0561 }
0562 
0563 /**
0564  * cgroup_is_descendant - test ancestry
0565  * @cgrp: the cgroup to be tested
0566  * @ancestor: possible ancestor of @cgrp
0567  *
0568  * Test whether @cgrp is a descendant of @ancestor.  It also returns %true
0569  * if @cgrp == @ancestor.  This function is safe to call as long as @cgrp
0570  * and @ancestor are accessible.
0571  */
0572 static inline bool cgroup_is_descendant(struct cgroup *cgrp,
0573                     struct cgroup *ancestor)
0574 {
0575     if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
0576         return false;
0577     return cgrp->ancestor_ids[ancestor->level] == cgroup_id(ancestor);
0578 }
0579 
0580 /**
0581  * cgroup_ancestor - find ancestor of cgroup
0582  * @cgrp: cgroup to find ancestor of
0583  * @ancestor_level: level of ancestor to find starting from root
0584  *
0585  * Find ancestor of cgroup at specified level starting from root if it exists
0586  * and return pointer to it. Return NULL if @cgrp doesn't have ancestor at
0587  * @ancestor_level.
0588  *
0589  * This function is safe to call as long as @cgrp is accessible.
0590  */
0591 static inline struct cgroup *cgroup_ancestor(struct cgroup *cgrp,
0592                          int ancestor_level)
0593 {
0594     if (cgrp->level < ancestor_level)
0595         return NULL;
0596     while (cgrp && cgrp->level > ancestor_level)
0597         cgrp = cgroup_parent(cgrp);
0598     return cgrp;
0599 }
0600 
0601 /**
0602  * task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
0603  * @task: the task to be tested
0604  * @ancestor: possible ancestor of @task's cgroup
0605  *
0606  * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
0607  * It follows all the same rules as cgroup_is_descendant, and only applies
0608  * to the default hierarchy.
0609  */
0610 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
0611                            struct cgroup *ancestor)
0612 {
0613     struct css_set *cset = task_css_set(task);
0614 
0615     return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
0616 }
0617 
0618 /* no synchronization, the result can only be used as a hint */
0619 static inline bool cgroup_is_populated(struct cgroup *cgrp)
0620 {
0621     return cgrp->nr_populated_csets + cgrp->nr_populated_domain_children +
0622         cgrp->nr_populated_threaded_children;
0623 }
0624 
0625 /* returns ino associated with a cgroup */
0626 static inline ino_t cgroup_ino(struct cgroup *cgrp)
0627 {
0628     return kernfs_ino(cgrp->kn);
0629 }
0630 
0631 /* cft/css accessors for cftype->write() operation */
0632 static inline struct cftype *of_cft(struct kernfs_open_file *of)
0633 {
0634     return of->kn->priv;
0635 }
0636 
0637 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
0638 
0639 /* cft/css accessors for cftype->seq_*() operations */
0640 static inline struct cftype *seq_cft(struct seq_file *seq)
0641 {
0642     return of_cft(seq->private);
0643 }
0644 
0645 static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
0646 {
0647     return of_css(seq->private);
0648 }
0649 
0650 /*
0651  * Name / path handling functions.  All are thin wrappers around the kernfs
0652  * counterparts and can be called under any context.
0653  */
0654 
0655 static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
0656 {
0657     return kernfs_name(cgrp->kn, buf, buflen);
0658 }
0659 
0660 static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
0661 {
0662     return kernfs_path(cgrp->kn, buf, buflen);
0663 }
0664 
0665 static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
0666 {
0667     pr_cont_kernfs_name(cgrp->kn);
0668 }
0669 
0670 static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
0671 {
0672     pr_cont_kernfs_path(cgrp->kn);
0673 }
0674 
0675 static inline struct psi_group *cgroup_psi(struct cgroup *cgrp)
0676 {
0677     return cgrp->psi;
0678 }
0679 
0680 bool cgroup_psi_enabled(void);
0681 
0682 static inline void cgroup_init_kthreadd(void)
0683 {
0684     /*
0685      * kthreadd is inherited by all kthreads, keep it in the root so
0686      * that the new kthreads are guaranteed to stay in the root until
0687      * initialization is finished.
0688      */
0689     current->no_cgroup_migration = 1;
0690 }
0691 
0692 static inline void cgroup_kthread_ready(void)
0693 {
0694     /*
0695      * This kthread finished initialization.  The creator should have
0696      * set PF_NO_SETAFFINITY if this kthread should stay in the root.
0697      */
0698     current->no_cgroup_migration = 0;
0699 }
0700 
0701 void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen);
0702 struct cgroup *cgroup_get_from_id(u64 id);
0703 #else /* !CONFIG_CGROUPS */
0704 
0705 struct cgroup_subsys_state;
0706 struct cgroup;
0707 
0708 static inline u64 cgroup_id(const struct cgroup *cgrp) { return 1; }
0709 static inline void css_get(struct cgroup_subsys_state *css) {}
0710 static inline void css_put(struct cgroup_subsys_state *css) {}
0711 static inline int cgroup_attach_task_all(struct task_struct *from,
0712                      struct task_struct *t) { return 0; }
0713 static inline int cgroupstats_build(struct cgroupstats *stats,
0714                     struct dentry *dentry) { return -EINVAL; }
0715 
0716 static inline void cgroup_fork(struct task_struct *p) {}
0717 static inline int cgroup_can_fork(struct task_struct *p,
0718                   struct kernel_clone_args *kargs) { return 0; }
0719 static inline void cgroup_cancel_fork(struct task_struct *p,
0720                       struct kernel_clone_args *kargs) {}
0721 static inline void cgroup_post_fork(struct task_struct *p,
0722                     struct kernel_clone_args *kargs) {}
0723 static inline void cgroup_exit(struct task_struct *p) {}
0724 static inline void cgroup_release(struct task_struct *p) {}
0725 static inline void cgroup_free(struct task_struct *p) {}
0726 
0727 static inline int cgroup_init_early(void) { return 0; }
0728 static inline int cgroup_init(void) { return 0; }
0729 static inline void cgroup_init_kthreadd(void) {}
0730 static inline void cgroup_kthread_ready(void) {}
0731 
0732 static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
0733 {
0734     return NULL;
0735 }
0736 
0737 static inline bool cgroup_psi_enabled(void)
0738 {
0739     return false;
0740 }
0741 
0742 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
0743                            struct cgroup *ancestor)
0744 {
0745     return true;
0746 }
0747 
0748 static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
0749 {}
0750 
0751 static inline struct cgroup *cgroup_get_from_id(u64 id)
0752 {
0753     return NULL;
0754 }
0755 #endif /* !CONFIG_CGROUPS */
0756 
0757 #ifdef CONFIG_CGROUPS
0758 /*
0759  * cgroup scalable recursive statistics.
0760  */
0761 void cgroup_rstat_updated(struct cgroup *cgrp, int cpu);
0762 void cgroup_rstat_flush(struct cgroup *cgrp);
0763 void cgroup_rstat_flush_irqsafe(struct cgroup *cgrp);
0764 void cgroup_rstat_flush_hold(struct cgroup *cgrp);
0765 void cgroup_rstat_flush_release(void);
0766 
0767 /*
0768  * Basic resource stats.
0769  */
0770 #ifdef CONFIG_CGROUP_CPUACCT
0771 void cpuacct_charge(struct task_struct *tsk, u64 cputime);
0772 void cpuacct_account_field(struct task_struct *tsk, int index, u64 val);
0773 #else
0774 static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
0775 static inline void cpuacct_account_field(struct task_struct *tsk, int index,
0776                      u64 val) {}
0777 #endif
0778 
0779 void __cgroup_account_cputime(struct cgroup *cgrp, u64 delta_exec);
0780 void __cgroup_account_cputime_field(struct cgroup *cgrp,
0781                     enum cpu_usage_stat index, u64 delta_exec);
0782 
0783 static inline void cgroup_account_cputime(struct task_struct *task,
0784                       u64 delta_exec)
0785 {
0786     struct cgroup *cgrp;
0787 
0788     cpuacct_charge(task, delta_exec);
0789 
0790     cgrp = task_dfl_cgroup(task);
0791     if (cgroup_parent(cgrp))
0792         __cgroup_account_cputime(cgrp, delta_exec);
0793 }
0794 
0795 static inline void cgroup_account_cputime_field(struct task_struct *task,
0796                         enum cpu_usage_stat index,
0797                         u64 delta_exec)
0798 {
0799     struct cgroup *cgrp;
0800 
0801     cpuacct_account_field(task, index, delta_exec);
0802 
0803     cgrp = task_dfl_cgroup(task);
0804     if (cgroup_parent(cgrp))
0805         __cgroup_account_cputime_field(cgrp, index, delta_exec);
0806 }
0807 
0808 #else   /* CONFIG_CGROUPS */
0809 
0810 static inline void cgroup_account_cputime(struct task_struct *task,
0811                       u64 delta_exec) {}
0812 static inline void cgroup_account_cputime_field(struct task_struct *task,
0813                         enum cpu_usage_stat index,
0814                         u64 delta_exec) {}
0815 
0816 #endif  /* CONFIG_CGROUPS */
0817 
0818 /*
0819  * sock->sk_cgrp_data handling.  For more info, see sock_cgroup_data
0820  * definition in cgroup-defs.h.
0821  */
0822 #ifdef CONFIG_SOCK_CGROUP_DATA
0823 
0824 void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
0825 void cgroup_sk_clone(struct sock_cgroup_data *skcd);
0826 void cgroup_sk_free(struct sock_cgroup_data *skcd);
0827 
0828 static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
0829 {
0830     return skcd->cgroup;
0831 }
0832 
0833 #else   /* CONFIG_CGROUP_DATA */
0834 
0835 static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
0836 static inline void cgroup_sk_clone(struct sock_cgroup_data *skcd) {}
0837 static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
0838 
0839 #endif  /* CONFIG_CGROUP_DATA */
0840 
0841 struct cgroup_namespace {
0842     struct ns_common    ns;
0843     struct user_namespace   *user_ns;
0844     struct ucounts      *ucounts;
0845     struct css_set          *root_cset;
0846 };
0847 
0848 extern struct cgroup_namespace init_cgroup_ns;
0849 
0850 #ifdef CONFIG_CGROUPS
0851 
0852 void free_cgroup_ns(struct cgroup_namespace *ns);
0853 
0854 struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
0855                     struct user_namespace *user_ns,
0856                     struct cgroup_namespace *old_ns);
0857 
0858 int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
0859            struct cgroup_namespace *ns);
0860 
0861 #else /* !CONFIG_CGROUPS */
0862 
0863 static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
0864 static inline struct cgroup_namespace *
0865 copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
0866            struct cgroup_namespace *old_ns)
0867 {
0868     return old_ns;
0869 }
0870 
0871 #endif /* !CONFIG_CGROUPS */
0872 
0873 static inline void get_cgroup_ns(struct cgroup_namespace *ns)
0874 {
0875     if (ns)
0876         refcount_inc(&ns->ns.count);
0877 }
0878 
0879 static inline void put_cgroup_ns(struct cgroup_namespace *ns)
0880 {
0881     if (ns && refcount_dec_and_test(&ns->ns.count))
0882         free_cgroup_ns(ns);
0883 }
0884 
0885 #ifdef CONFIG_CGROUPS
0886 
0887 void cgroup_enter_frozen(void);
0888 void cgroup_leave_frozen(bool always_leave);
0889 void cgroup_update_frozen(struct cgroup *cgrp);
0890 void cgroup_freeze(struct cgroup *cgrp, bool freeze);
0891 void cgroup_freezer_migrate_task(struct task_struct *task, struct cgroup *src,
0892                  struct cgroup *dst);
0893 
0894 static inline bool cgroup_task_frozen(struct task_struct *task)
0895 {
0896     return task->frozen;
0897 }
0898 
0899 #else /* !CONFIG_CGROUPS */
0900 
0901 static inline void cgroup_enter_frozen(void) { }
0902 static inline void cgroup_leave_frozen(bool always_leave) { }
0903 static inline bool cgroup_task_frozen(struct task_struct *task)
0904 {
0905     return false;
0906 }
0907 
0908 #endif /* !CONFIG_CGROUPS */
0909 
0910 #ifdef CONFIG_CGROUP_BPF
0911 static inline void cgroup_bpf_get(struct cgroup *cgrp)
0912 {
0913     percpu_ref_get(&cgrp->bpf.refcnt);
0914 }
0915 
0916 static inline void cgroup_bpf_put(struct cgroup *cgrp)
0917 {
0918     percpu_ref_put(&cgrp->bpf.refcnt);
0919 }
0920 
0921 #else /* CONFIG_CGROUP_BPF */
0922 
0923 static inline void cgroup_bpf_get(struct cgroup *cgrp) {}
0924 static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
0925 
0926 #endif /* CONFIG_CGROUP_BPF */
0927 
0928 #endif /* _LINUX_CGROUP_H */