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