blob: 3c1613a7648cc7b7c2d2f466f434d216515be943 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Tejun Heo0a268db2016-12-27 14:49:06 -05002#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>
Elena Reshetova4b9502e62017-03-08 10:00:40 +02009#include <linux/refcount.h>
Al Viro90129622019-01-05 00:38:03 -050010#include <linux/fs_context.h>
Tejun Heo0a268db2016-12-27 14:49:06 -050011
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -040012#define TRACE_CGROUP_PATH_LEN 1024
13extern spinlock_t trace_cgroup_path_lock;
14extern char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
Waiman Long5cf81142018-11-08 10:08:46 -050015extern bool cgroup_debug;
16extern void __init enable_debug_cgroup(void);
Steven Rostedt (VMware)e4f8d812018-07-09 17:48:54 -040017
18/*
19 * cgroup_path() takes a spin lock. It is good practice not to take
20 * spin locks within trace point handlers, as they are mostly hidden
21 * from normal view. As cgroup_path() can take the kernfs_rename_lock
22 * spin lock, it is best to not call that function from the trace event
23 * handler.
24 *
25 * Note: trace_cgroup_##type##_enabled() is a static branch that will only
26 * be set when the trace event is enabled.
27 */
28#define TRACE_CGROUP_PATH(type, cgrp, ...) \
29 do { \
30 if (trace_cgroup_##type##_enabled()) { \
31 spin_lock(&trace_cgroup_path_lock); \
32 cgroup_path(cgrp, trace_cgroup_path, \
33 TRACE_CGROUP_PATH_LEN); \
34 trace_cgroup_##type(cgrp, trace_cgroup_path, \
35 ##__VA_ARGS__); \
36 spin_unlock(&trace_cgroup_path_lock); \
37 } \
38 } while (0)
39
Tejun Heo0a268db2016-12-27 14:49:06 -050040/*
Al Viro90129622019-01-05 00:38:03 -050041 * The cgroup filesystem superblock creation/mount context.
42 */
43struct cgroup_fs_context {
Al Virocf6299b12019-01-17 02:25:51 -050044 struct cgroup_root *root;
Al Virof5dfb532019-01-16 23:42:38 -050045 unsigned int flags; /* CGRP_ROOT_* flags */
46
47 /* cgroup1 bits */
48 bool cpuset_clone_children;
49 bool none; /* User explicitly requested empty subsystem */
50 bool all_ss; /* Seen 'all' option */
51 u16 subsys_mask; /* Selected subsystems */
52 char *name; /* Hierarchy name */
53 char *release_agent; /* Path for release notifications */
Al Viro90129622019-01-05 00:38:03 -050054};
55
56static inline struct cgroup_fs_context *cgroup_fc2context(struct fs_context *fc)
57{
58 return fc->fs_private;
59}
60
61/*
Tejun Heo0a268db2016-12-27 14:49:06 -050062 * A cgroup can be associated with multiple css_sets as different tasks may
63 * belong to different cgroups on different hierarchies. In the other
64 * direction, a css_set is naturally associated with multiple cgroups.
65 * This M:N relationship is represented by the following link structure
66 * which exists for each association and allows traversing the associations
67 * from both sides.
68 */
69struct cgrp_cset_link {
70 /* the cgroup and css_set this link associates */
71 struct cgroup *cgrp;
72 struct css_set *cset;
73
74 /* list of cgrp_cset_links anchored at cgrp->cset_links */
75 struct list_head cset_link;
76
77 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
78 struct list_head cgrp_link;
79};
80
Tejun Heoe595cd72017-01-15 19:03:41 -050081/* used to track tasks and csets during migration */
82struct cgroup_taskset {
83 /* the src and dst cset list running through cset->mg_node */
84 struct list_head src_csets;
85 struct list_head dst_csets;
86
Tejun Heo61046722017-07-08 07:17:02 -040087 /* the number of tasks in the set */
88 int nr_tasks;
89
Tejun Heoe595cd72017-01-15 19:03:41 -050090 /* the subsys currently being processed */
91 int ssid;
92
93 /*
94 * Fields for cgroup_taskset_*() iteration.
95 *
96 * Before migration is committed, the target migration tasks are on
97 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
98 * the csets on ->dst_csets. ->csets point to either ->src_csets
99 * or ->dst_csets depending on whether migration is committed.
100 *
101 * ->cur_csets and ->cur_task point to the current task position
102 * during iteration.
103 */
104 struct list_head *csets;
105 struct css_set *cur_cset;
106 struct task_struct *cur_task;
107};
108
109/* migration context also tracks preloading */
110struct cgroup_mgctx {
111 /*
112 * Preloaded source and destination csets. Used to guarantee
113 * atomic success or failure on actual migration.
114 */
115 struct list_head preloaded_src_csets;
116 struct list_head preloaded_dst_csets;
117
118 /* tasks and csets to migrate */
119 struct cgroup_taskset tset;
Tejun Heobfc2cf62017-01-15 19:03:41 -0500120
121 /* subsystems affected by migration */
122 u16 ss_mask;
Tejun Heoe595cd72017-01-15 19:03:41 -0500123};
124
125#define CGROUP_TASKSET_INIT(tset) \
126{ \
127 .src_csets = LIST_HEAD_INIT(tset.src_csets), \
128 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
129 .csets = &tset.src_csets, \
130}
131
132#define CGROUP_MGCTX_INIT(name) \
133{ \
134 LIST_HEAD_INIT(name.preloaded_src_csets), \
135 LIST_HEAD_INIT(name.preloaded_dst_csets), \
136 CGROUP_TASKSET_INIT(name.tset), \
137}
138
139#define DEFINE_CGROUP_MGCTX(name) \
140 struct cgroup_mgctx name = CGROUP_MGCTX_INIT(name)
141
Tejun Heo0a268db2016-12-27 14:49:06 -0500142extern struct mutex cgroup_mutex;
143extern spinlock_t css_set_lock;
144extern struct cgroup_subsys *cgroup_subsys[];
145extern struct list_head cgroup_roots;
146extern struct file_system_type cgroup_fs_type;
147
148/* iterate across the hierarchies */
149#define for_each_root(root) \
150 list_for_each_entry((root), &cgroup_roots, root_list)
151
152/**
153 * for_each_subsys - iterate all enabled cgroup subsystems
154 * @ss: the iteration cursor
155 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
156 */
157#define for_each_subsys(ss, ssid) \
158 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
159 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
160
161static inline bool cgroup_is_dead(const struct cgroup *cgrp)
162{
163 return !(cgrp->self.flags & CSS_ONLINE);
164}
165
166static inline bool notify_on_release(const struct cgroup *cgrp)
167{
168 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
169}
170
Tejun Heodcfe1492016-12-27 14:49:09 -0500171void put_css_set_locked(struct css_set *cset);
172
173static inline void put_css_set(struct css_set *cset)
174{
175 unsigned long flags;
176
177 /*
178 * Ensure that the refcount doesn't hit zero while any readers
179 * can see it. Similar to atomic_dec_and_lock(), but for an
180 * rwlock
181 */
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200182 if (refcount_dec_not_one(&cset->refcount))
Tejun Heodcfe1492016-12-27 14:49:09 -0500183 return;
184
185 spin_lock_irqsave(&css_set_lock, flags);
186 put_css_set_locked(cset);
187 spin_unlock_irqrestore(&css_set_lock, flags);
188}
189
190/*
191 * refcounted get/put for css_set objects
192 */
193static inline void get_css_set(struct css_set *cset)
194{
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200195 refcount_inc(&cset->refcount);
Tejun Heodcfe1492016-12-27 14:49:09 -0500196}
197
Tejun Heo0a268db2016-12-27 14:49:06 -0500198bool cgroup_ssid_enabled(int ssid);
199bool cgroup_on_dfl(const struct cgroup *cgrp);
Waiman Long7a0cf0e2017-07-21 11:14:51 -0400200bool cgroup_is_thread_root(struct cgroup *cgrp);
201bool cgroup_is_threaded(struct cgroup *cgrp);
Tejun Heo0a268db2016-12-27 14:49:06 -0500202
203struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root);
204struct cgroup *task_cgroup_from_root(struct task_struct *task,
205 struct cgroup_root *root);
206struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline);
207void cgroup_kn_unlock(struct kernfs_node *kn);
208int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
209 struct cgroup_namespace *ns);
210
Tejun Heo1592c9b2016-12-27 14:49:08 -0500211void cgroup_free_root(struct cgroup_root *root);
Al Virocf6299b12019-01-17 02:25:51 -0500212void init_cgroup_root(struct cgroup_fs_context *ctx);
Al Viro35ac1182019-01-12 00:20:54 -0500213int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask);
Tejun Heo0a268db2016-12-27 14:49:06 -0500214int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask);
Tejun Heo1592c9b2016-12-27 14:49:08 -0500215struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
216 struct cgroup_root *root, unsigned long magic,
217 struct cgroup_namespace *ns);
Tejun Heo0a268db2016-12-27 14:49:06 -0500218
Tejun Heo8cfd8142017-07-21 11:14:51 -0400219int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp);
Tejun Heoe595cd72017-01-15 19:03:41 -0500220void cgroup_migrate_finish(struct cgroup_mgctx *mgctx);
221void cgroup_migrate_add_src(struct css_set *src_cset, struct cgroup *dst_cgrp,
222 struct cgroup_mgctx *mgctx);
223int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx);
Tejun Heo0a268db2016-12-27 14:49:06 -0500224int cgroup_migrate(struct task_struct *leader, bool threadgroup,
Tejun Heobfc2cf62017-01-15 19:03:41 -0500225 struct cgroup_mgctx *mgctx);
Tejun Heo0a268db2016-12-27 14:49:06 -0500226
227int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
228 bool threadgroup);
Tejun Heo715c8092017-05-15 09:34:00 -0400229struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup)
230 __acquires(&cgroup_threadgroup_rwsem);
231void cgroup_procs_write_finish(struct task_struct *task)
232 __releases(&cgroup_threadgroup_rwsem);
Tejun Heo0a268db2016-12-27 14:49:06 -0500233
234void cgroup_lock_and_drain_offline(struct cgroup *cgrp);
235
Tejun Heo1592c9b2016-12-27 14:49:08 -0500236int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode);
237int cgroup_rmdir(struct kernfs_node *kn);
238int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
239 struct kernfs_root *kf_root);
240
Waiman Longa28f8f52017-06-13 17:18:02 -0400241int cgroup_task_count(const struct cgroup *cgrp);
242
Tejun Heo0a268db2016-12-27 14:49:06 -0500243/*
Tejun Heoc58632b2018-04-26 14:29:04 -0700244 * rstat.c
Tejun Heo041cd642017-09-25 08:12:05 -0700245 */
Tejun Heoc58632b2018-04-26 14:29:04 -0700246int cgroup_rstat_init(struct cgroup *cgrp);
247void cgroup_rstat_exit(struct cgroup *cgrp);
Tejun Heoc58632b2018-04-26 14:29:04 -0700248void cgroup_rstat_boot(void);
Tejun Heoa17556f82018-04-26 14:29:05 -0700249void cgroup_base_stat_cputime_show(struct seq_file *seq);
Tejun Heo041cd642017-09-25 08:12:05 -0700250
251/*
Tejun Heodcfe1492016-12-27 14:49:09 -0500252 * namespace.c
253 */
254extern const struct proc_ns_operations cgroupns_operations;
255
256/*
Tejun Heo0a268db2016-12-27 14:49:06 -0500257 * cgroup-v1.c
258 */
Tejun Heod62beb72016-12-27 14:49:08 -0500259extern struct cftype cgroup1_base_files[];
Tejun Heo1592c9b2016-12-27 14:49:08 -0500260extern struct kernfs_syscall_ops cgroup1_kf_syscall_ops;
Al Viro8d2451f2019-01-17 00:15:11 -0500261extern const struct fs_parameter_description cgroup1_fs_parameters;
Tejun Heo0a268db2016-12-27 14:49:06 -0500262
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200263int proc_cgroupstats_show(struct seq_file *m, void *v);
Tejun Heod62beb72016-12-27 14:49:08 -0500264bool cgroup1_ssid_disabled(int ssid);
265void cgroup1_pidlist_destroy_all(struct cgroup *cgrp);
266void cgroup1_release_agent(struct work_struct *work);
267void cgroup1_check_for_release(struct cgroup *cgrp);
Al Viro8d2451f2019-01-17 00:15:11 -0500268int cgroup1_parse_param(struct fs_context *fc, struct fs_parameter *param);
Al Viro7feeef52019-01-16 21:23:02 -0500269int cgroup1_get_tree(struct fs_context *fc);
Al Viro90129622019-01-05 00:38:03 -0500270int cgroup1_reconfigure(struct fs_context *ctx);
Tejun Heo0a268db2016-12-27 14:49:06 -0500271
272#endif /* __CGROUP_INTERNAL_H */