1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __CGROUP_INTERNAL_H 3 #define __CGROUP_INTERNAL_H 4 5 #include <linux/cgroup.h> 6 #include <linux/kernfs.h> 7 #include <linux/workqueue.h> 8 #include <linux/list.h> 9 #include <linux/refcount.h> 10 11 #define TRACE_CGROUP_PATH_LEN 1024 12 extern spinlock_t trace_cgroup_path_lock; 13 extern char trace_cgroup_path[TRACE_CGROUP_PATH_LEN]; 14 15 /* 16 * cgroup_path() takes a spin lock. It is good practice not to take 17 * spin locks within trace point handlers, as they are mostly hidden 18 * from normal view. As cgroup_path() can take the kernfs_rename_lock 19 * spin lock, it is best to not call that function from the trace event 20 * handler. 21 * 22 * Note: trace_cgroup_##type##_enabled() is a static branch that will only 23 * be set when the trace event is enabled. 24 */ 25 #define TRACE_CGROUP_PATH(type, cgrp, ...) \ 26 do { \ 27 if (trace_cgroup_##type##_enabled()) { \ 28 spin_lock(&trace_cgroup_path_lock); \ 29 cgroup_path(cgrp, trace_cgroup_path, \ 30 TRACE_CGROUP_PATH_LEN); \ 31 trace_cgroup_##type(cgrp, trace_cgroup_path, \ 32 ##__VA_ARGS__); \ 33 spin_unlock(&trace_cgroup_path_lock); \ 34 } \ 35 } while (0) 36 37 /* 38 * A cgroup can be associated with multiple css_sets as different tasks may 39 * belong to different cgroups on different hierarchies. In the other 40 * direction, a css_set is naturally associated with multiple cgroups. 41 * This M:N relationship is represented by the following link structure 42 * which exists for each association and allows traversing the associations 43 * from both sides. 44 */ 45 struct cgrp_cset_link { 46 /* the cgroup and css_set this link associates */ 47 struct cgroup *cgrp; 48 struct css_set *cset; 49 50 /* list of cgrp_cset_links anchored at cgrp->cset_links */ 51 struct list_head cset_link; 52 53 /* list of cgrp_cset_links anchored at css_set->cgrp_links */ 54 struct list_head cgrp_link; 55 }; 56 57 /* used to track tasks and csets during migration */ 58 struct cgroup_taskset { 59 /* the src and dst cset list running through cset->mg_node */ 60 struct list_head src_csets; 61 struct list_head dst_csets; 62 63 /* the number of tasks in the set */ 64 int nr_tasks; 65 66 /* the subsys currently being processed */ 67 int ssid; 68 69 /* 70 * Fields for cgroup_taskset_*() iteration. 71 * 72 * Before migration is committed, the target migration tasks are on 73 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of 74 * the csets on ->dst_csets. ->csets point to either ->src_csets 75 * or ->dst_csets depending on whether migration is committed. 76 * 77 * ->cur_csets and ->cur_task point to the current task position 78 * during iteration. 79 */ 80 struct list_head *csets; 81 struct css_set *cur_cset; 82 struct task_struct *cur_task; 83 }; 84 85 /* migration context also tracks preloading */ 86 struct cgroup_mgctx { 87 /* 88 * Preloaded source and destination csets. Used to guarantee 89 * atomic success or failure on actual migration. 90 */ 91 struct list_head preloaded_src_csets; 92 struct list_head preloaded_dst_csets; 93 94 /* tasks and csets to migrate */ 95 struct cgroup_taskset tset; 96 97 /* subsystems affected by migration */ 98 u16 ss_mask; 99 }; 100 101 #define CGROUP_TASKSET_INIT(tset) \ 102 { \ 103 .src_csets = LIST_HEAD_INIT(tset.src_csets), \ 104 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \ 105 .csets = &tset.src_csets, \ 106 } 107 108 #define CGROUP_MGCTX_INIT(name) \ 109 { \ 110 LIST_HEAD_INIT(name.preloaded_src_csets), \ 111 LIST_HEAD_INIT(name.preloaded_dst_csets), \ 112 CGROUP_TASKSET_INIT(name.tset), \ 113 } 114 115 #define DEFINE_CGROUP_MGCTX(name) \ 116 struct cgroup_mgctx name = CGROUP_MGCTX_INIT(name) 117 118 struct cgroup_sb_opts { 119 u16 subsys_mask; 120 unsigned int flags; 121 char *release_agent; 122 bool cpuset_clone_children; 123 char *name; 124 /* User explicitly requested empty subsystem */ 125 bool none; 126 }; 127 128 extern struct mutex cgroup_mutex; 129 extern spinlock_t css_set_lock; 130 extern struct cgroup_subsys *cgroup_subsys[]; 131 extern struct list_head cgroup_roots; 132 extern struct file_system_type cgroup_fs_type; 133 134 /* iterate across the hierarchies */ 135 #define for_each_root(root) \ 136 list_for_each_entry((root), &cgroup_roots, root_list) 137 138 /** 139 * for_each_subsys - iterate all enabled cgroup subsystems 140 * @ss: the iteration cursor 141 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end 142 */ 143 #define for_each_subsys(ss, ssid) \ 144 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \ 145 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) 146 147 static inline bool cgroup_is_dead(const struct cgroup *cgrp) 148 { 149 return !(cgrp->self.flags & CSS_ONLINE); 150 } 151 152 static inline bool notify_on_release(const struct cgroup *cgrp) 153 { 154 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); 155 } 156 157 void put_css_set_locked(struct css_set *cset); 158 159 static inline void put_css_set(struct css_set *cset) 160 { 161 unsigned long flags; 162 163 /* 164 * Ensure that the refcount doesn't hit zero while any readers 165 * can see it. Similar to atomic_dec_and_lock(), but for an 166 * rwlock 167 */ 168 if (refcount_dec_not_one(&cset->refcount)) 169 return; 170 171 spin_lock_irqsave(&css_set_lock, flags); 172 put_css_set_locked(cset); 173 spin_unlock_irqrestore(&css_set_lock, flags); 174 } 175 176 /* 177 * refcounted get/put for css_set objects 178 */ 179 static inline void get_css_set(struct css_set *cset) 180 { 181 refcount_inc(&cset->refcount); 182 } 183 184 bool cgroup_ssid_enabled(int ssid); 185 bool cgroup_on_dfl(const struct cgroup *cgrp); 186 bool cgroup_is_thread_root(struct cgroup *cgrp); 187 bool cgroup_is_threaded(struct cgroup *cgrp); 188 189 struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root); 190 struct cgroup *task_cgroup_from_root(struct task_struct *task, 191 struct cgroup_root *root); 192 struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline); 193 void cgroup_kn_unlock(struct kernfs_node *kn); 194 int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen, 195 struct cgroup_namespace *ns); 196 197 void cgroup_free_root(struct cgroup_root *root); 198 void init_cgroup_root(struct cgroup_root *root, struct cgroup_sb_opts *opts); 199 int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask, int ref_flags); 200 int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask); 201 struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags, 202 struct cgroup_root *root, unsigned long magic, 203 struct cgroup_namespace *ns); 204 205 int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp); 206 void cgroup_migrate_finish(struct cgroup_mgctx *mgctx); 207 void cgroup_migrate_add_src(struct css_set *src_cset, struct cgroup *dst_cgrp, 208 struct cgroup_mgctx *mgctx); 209 int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx); 210 int cgroup_migrate(struct task_struct *leader, bool threadgroup, 211 struct cgroup_mgctx *mgctx); 212 213 int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, 214 bool threadgroup); 215 struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup) 216 __acquires(&cgroup_threadgroup_rwsem); 217 void cgroup_procs_write_finish(struct task_struct *task) 218 __releases(&cgroup_threadgroup_rwsem); 219 220 void cgroup_lock_and_drain_offline(struct cgroup *cgrp); 221 222 int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode); 223 int cgroup_rmdir(struct kernfs_node *kn); 224 int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node, 225 struct kernfs_root *kf_root); 226 227 int cgroup_task_count(const struct cgroup *cgrp); 228 229 /* 230 * rstat.c 231 */ 232 int cgroup_rstat_init(struct cgroup *cgrp); 233 void cgroup_rstat_exit(struct cgroup *cgrp); 234 void cgroup_rstat_boot(void); 235 void cgroup_base_stat_cputime_show(struct seq_file *seq); 236 237 /* 238 * namespace.c 239 */ 240 extern const struct proc_ns_operations cgroupns_operations; 241 242 /* 243 * cgroup-v1.c 244 */ 245 extern struct cftype cgroup1_base_files[]; 246 extern struct kernfs_syscall_ops cgroup1_kf_syscall_ops; 247 248 int proc_cgroupstats_show(struct seq_file *m, void *v); 249 bool cgroup1_ssid_disabled(int ssid); 250 void cgroup1_pidlist_destroy_all(struct cgroup *cgrp); 251 void cgroup1_release_agent(struct work_struct *work); 252 void cgroup1_check_for_release(struct cgroup *cgrp); 253 struct dentry *cgroup1_mount(struct file_system_type *fs_type, int flags, 254 void *data, unsigned long magic, 255 struct cgroup_namespace *ns); 256 257 #endif /* __CGROUP_INTERNAL_H */ 258