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