Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __CGROUP_INTERNAL_H
0003 #define __CGROUP_INTERNAL_H
0004 
0005 #include <linux/cgroup.h>
0006 #include <linux/kernfs.h>
0007 #include <linux/workqueue.h>
0008 #include <linux/list.h>
0009 #include <linux/refcount.h>
0010 #include <linux/fs_parser.h>
0011 
0012 #define TRACE_CGROUP_PATH_LEN 1024
0013 extern spinlock_t trace_cgroup_path_lock;
0014 extern char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
0015 extern void __init enable_debug_cgroup(void);
0016 
0017 /*
0018  * cgroup_path() takes a spin lock. It is good practice not to take
0019  * spin locks within trace point handlers, as they are mostly hidden
0020  * from normal view. As cgroup_path() can take the kernfs_rename_lock
0021  * spin lock, it is best to not call that function from the trace event
0022  * handler.
0023  *
0024  * Note: trace_cgroup_##type##_enabled() is a static branch that will only
0025  *       be set when the trace event is enabled.
0026  */
0027 #define TRACE_CGROUP_PATH(type, cgrp, ...)              \
0028     do {                                \
0029         if (trace_cgroup_##type##_enabled()) {          \
0030             unsigned long flags;                \
0031             spin_lock_irqsave(&trace_cgroup_path_lock,  \
0032                       flags);           \
0033             cgroup_path(cgrp, trace_cgroup_path,        \
0034                     TRACE_CGROUP_PATH_LEN);     \
0035             trace_cgroup_##type(cgrp, trace_cgroup_path,    \
0036                         ##__VA_ARGS__);     \
0037             spin_unlock_irqrestore(&trace_cgroup_path_lock, \
0038                            flags);          \
0039         }                           \
0040     } while (0)
0041 
0042 /*
0043  * The cgroup filesystem superblock creation/mount context.
0044  */
0045 struct cgroup_fs_context {
0046     struct kernfs_fs_context kfc;
0047     struct cgroup_root  *root;
0048     struct cgroup_namespace *ns;
0049     unsigned int    flags;          /* CGRP_ROOT_* flags */
0050 
0051     /* cgroup1 bits */
0052     bool        cpuset_clone_children;
0053     bool        none;           /* User explicitly requested empty subsystem */
0054     bool        all_ss;         /* Seen 'all' option */
0055     u16     subsys_mask;        /* Selected subsystems */
0056     char        *name;          /* Hierarchy name */
0057     char        *release_agent;     /* Path for release notifications */
0058 };
0059 
0060 static inline struct cgroup_fs_context *cgroup_fc2context(struct fs_context *fc)
0061 {
0062     struct kernfs_fs_context *kfc = fc->fs_private;
0063 
0064     return container_of(kfc, struct cgroup_fs_context, kfc);
0065 }
0066 
0067 struct cgroup_pidlist;
0068 
0069 struct cgroup_file_ctx {
0070     struct cgroup_namespace *ns;
0071 
0072     struct {
0073         void            *trigger;
0074     } psi;
0075 
0076     struct {
0077         bool            started;
0078         struct css_task_iter    iter;
0079     } procs;
0080 
0081     struct {
0082         struct cgroup_pidlist   *pidlist;
0083     } procs1;
0084 };
0085 
0086 /*
0087  * A cgroup can be associated with multiple css_sets as different tasks may
0088  * belong to different cgroups on different hierarchies.  In the other
0089  * direction, a css_set is naturally associated with multiple cgroups.
0090  * This M:N relationship is represented by the following link structure
0091  * which exists for each association and allows traversing the associations
0092  * from both sides.
0093  */
0094 struct cgrp_cset_link {
0095     /* the cgroup and css_set this link associates */
0096     struct cgroup       *cgrp;
0097     struct css_set      *cset;
0098 
0099     /* list of cgrp_cset_links anchored at cgrp->cset_links */
0100     struct list_head    cset_link;
0101 
0102     /* list of cgrp_cset_links anchored at css_set->cgrp_links */
0103     struct list_head    cgrp_link;
0104 };
0105 
0106 /* used to track tasks and csets during migration */
0107 struct cgroup_taskset {
0108     /* the src and dst cset list running through cset->mg_node */
0109     struct list_head    src_csets;
0110     struct list_head    dst_csets;
0111 
0112     /* the number of tasks in the set */
0113     int         nr_tasks;
0114 
0115     /* the subsys currently being processed */
0116     int         ssid;
0117 
0118     /*
0119      * Fields for cgroup_taskset_*() iteration.
0120      *
0121      * Before migration is committed, the target migration tasks are on
0122      * ->mg_tasks of the csets on ->src_csets.  After, on ->mg_tasks of
0123      * the csets on ->dst_csets.  ->csets point to either ->src_csets
0124      * or ->dst_csets depending on whether migration is committed.
0125      *
0126      * ->cur_csets and ->cur_task point to the current task position
0127      * during iteration.
0128      */
0129     struct list_head    *csets;
0130     struct css_set      *cur_cset;
0131     struct task_struct  *cur_task;
0132 };
0133 
0134 /* migration context also tracks preloading */
0135 struct cgroup_mgctx {
0136     /*
0137      * Preloaded source and destination csets.  Used to guarantee
0138      * atomic success or failure on actual migration.
0139      */
0140     struct list_head    preloaded_src_csets;
0141     struct list_head    preloaded_dst_csets;
0142 
0143     /* tasks and csets to migrate */
0144     struct cgroup_taskset   tset;
0145 
0146     /* subsystems affected by migration */
0147     u16         ss_mask;
0148 };
0149 
0150 #define CGROUP_TASKSET_INIT(tset)                       \
0151 {                                       \
0152     .src_csets      = LIST_HEAD_INIT(tset.src_csets),       \
0153     .dst_csets      = LIST_HEAD_INIT(tset.dst_csets),       \
0154     .csets          = &tset.src_csets,              \
0155 }
0156 
0157 #define CGROUP_MGCTX_INIT(name)                         \
0158 {                                       \
0159     LIST_HEAD_INIT(name.preloaded_src_csets),               \
0160     LIST_HEAD_INIT(name.preloaded_dst_csets),               \
0161     CGROUP_TASKSET_INIT(name.tset),                     \
0162 }
0163 
0164 #define DEFINE_CGROUP_MGCTX(name)                       \
0165     struct cgroup_mgctx name = CGROUP_MGCTX_INIT(name)
0166 
0167 extern struct mutex cgroup_mutex;
0168 extern spinlock_t css_set_lock;
0169 extern struct cgroup_subsys *cgroup_subsys[];
0170 extern struct list_head cgroup_roots;
0171 extern struct file_system_type cgroup_fs_type;
0172 
0173 /* iterate across the hierarchies */
0174 #define for_each_root(root)                     \
0175     list_for_each_entry((root), &cgroup_roots, root_list)
0176 
0177 /**
0178  * for_each_subsys - iterate all enabled cgroup subsystems
0179  * @ss: the iteration cursor
0180  * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
0181  */
0182 #define for_each_subsys(ss, ssid)                   \
0183     for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT &&        \
0184          (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
0185 
0186 static inline bool cgroup_is_dead(const struct cgroup *cgrp)
0187 {
0188     return !(cgrp->self.flags & CSS_ONLINE);
0189 }
0190 
0191 static inline bool notify_on_release(const struct cgroup *cgrp)
0192 {
0193     return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
0194 }
0195 
0196 void put_css_set_locked(struct css_set *cset);
0197 
0198 static inline void put_css_set(struct css_set *cset)
0199 {
0200     unsigned long flags;
0201 
0202     /*
0203      * Ensure that the refcount doesn't hit zero while any readers
0204      * can see it. Similar to atomic_dec_and_lock(), but for an
0205      * rwlock
0206      */
0207     if (refcount_dec_not_one(&cset->refcount))
0208         return;
0209 
0210     spin_lock_irqsave(&css_set_lock, flags);
0211     put_css_set_locked(cset);
0212     spin_unlock_irqrestore(&css_set_lock, flags);
0213 }
0214 
0215 /*
0216  * refcounted get/put for css_set objects
0217  */
0218 static inline void get_css_set(struct css_set *cset)
0219 {
0220     refcount_inc(&cset->refcount);
0221 }
0222 
0223 bool cgroup_ssid_enabled(int ssid);
0224 bool cgroup_on_dfl(const struct cgroup *cgrp);
0225 bool cgroup_is_thread_root(struct cgroup *cgrp);
0226 bool cgroup_is_threaded(struct cgroup *cgrp);
0227 
0228 struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root);
0229 struct cgroup *task_cgroup_from_root(struct task_struct *task,
0230                      struct cgroup_root *root);
0231 struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline);
0232 void cgroup_kn_unlock(struct kernfs_node *kn);
0233 int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
0234               struct cgroup_namespace *ns);
0235 
0236 void cgroup_favor_dynmods(struct cgroup_root *root, bool favor);
0237 void cgroup_free_root(struct cgroup_root *root);
0238 void init_cgroup_root(struct cgroup_fs_context *ctx);
0239 int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask);
0240 int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask);
0241 int cgroup_do_get_tree(struct fs_context *fc);
0242 
0243 int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp);
0244 void cgroup_migrate_finish(struct cgroup_mgctx *mgctx);
0245 void cgroup_migrate_add_src(struct css_set *src_cset, struct cgroup *dst_cgrp,
0246                 struct cgroup_mgctx *mgctx);
0247 int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx);
0248 int cgroup_migrate(struct task_struct *leader, bool threadgroup,
0249            struct cgroup_mgctx *mgctx);
0250 
0251 int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
0252                bool threadgroup);
0253 struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,
0254                          bool *locked)
0255     __acquires(&cgroup_threadgroup_rwsem);
0256 void cgroup_procs_write_finish(struct task_struct *task, bool locked)
0257     __releases(&cgroup_threadgroup_rwsem);
0258 
0259 void cgroup_lock_and_drain_offline(struct cgroup *cgrp);
0260 
0261 int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode);
0262 int cgroup_rmdir(struct kernfs_node *kn);
0263 int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
0264              struct kernfs_root *kf_root);
0265 
0266 int __cgroup_task_count(const struct cgroup *cgrp);
0267 int cgroup_task_count(const struct cgroup *cgrp);
0268 
0269 /*
0270  * rstat.c
0271  */
0272 int cgroup_rstat_init(struct cgroup *cgrp);
0273 void cgroup_rstat_exit(struct cgroup *cgrp);
0274 void cgroup_rstat_boot(void);
0275 void cgroup_base_stat_cputime_show(struct seq_file *seq);
0276 
0277 /*
0278  * namespace.c
0279  */
0280 extern const struct proc_ns_operations cgroupns_operations;
0281 
0282 /*
0283  * cgroup-v1.c
0284  */
0285 extern struct cftype cgroup1_base_files[];
0286 extern struct kernfs_syscall_ops cgroup1_kf_syscall_ops;
0287 extern const struct fs_parameter_spec cgroup1_fs_parameters[];
0288 
0289 int proc_cgroupstats_show(struct seq_file *m, void *v);
0290 bool cgroup1_ssid_disabled(int ssid);
0291 void cgroup1_pidlist_destroy_all(struct cgroup *cgrp);
0292 void cgroup1_release_agent(struct work_struct *work);
0293 void cgroup1_check_for_release(struct cgroup *cgrp);
0294 int cgroup1_parse_param(struct fs_context *fc, struct fs_parameter *param);
0295 int cgroup1_get_tree(struct fs_context *fc);
0296 int cgroup1_reconfigure(struct fs_context *ctx);
0297 
0298 #endif /* __CGROUP_INTERNAL_H */