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