blob: bf54ade001be4ada1d9ebd45d3a12930828d94e1 [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>
Tejun Heo0a268db2016-12-27 14:49:06 -050010
11/*
12 * A cgroup can be associated with multiple css_sets as different tasks may
13 * belong to different cgroups on different hierarchies. In the other
14 * direction, a css_set is naturally associated with multiple cgroups.
15 * This M:N relationship is represented by the following link structure
16 * which exists for each association and allows traversing the associations
17 * from both sides.
18 */
19struct cgrp_cset_link {
20 /* the cgroup and css_set this link associates */
21 struct cgroup *cgrp;
22 struct css_set *cset;
23
24 /* list of cgrp_cset_links anchored at cgrp->cset_links */
25 struct list_head cset_link;
26
27 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
28 struct list_head cgrp_link;
29};
30
Tejun Heoe595cd72017-01-15 19:03:41 -050031/* used to track tasks and csets during migration */
32struct cgroup_taskset {
33 /* the src and dst cset list running through cset->mg_node */
34 struct list_head src_csets;
35 struct list_head dst_csets;
36
Tejun Heo61046722017-07-08 07:17:02 -040037 /* the number of tasks in the set */
38 int nr_tasks;
39
Tejun Heoe595cd72017-01-15 19:03:41 -050040 /* the subsys currently being processed */
41 int ssid;
42
43 /*
44 * Fields for cgroup_taskset_*() iteration.
45 *
46 * Before migration is committed, the target migration tasks are on
47 * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
48 * the csets on ->dst_csets. ->csets point to either ->src_csets
49 * or ->dst_csets depending on whether migration is committed.
50 *
51 * ->cur_csets and ->cur_task point to the current task position
52 * during iteration.
53 */
54 struct list_head *csets;
55 struct css_set *cur_cset;
56 struct task_struct *cur_task;
57};
58
59/* migration context also tracks preloading */
60struct cgroup_mgctx {
61 /*
62 * Preloaded source and destination csets. Used to guarantee
63 * atomic success or failure on actual migration.
64 */
65 struct list_head preloaded_src_csets;
66 struct list_head preloaded_dst_csets;
67
68 /* tasks and csets to migrate */
69 struct cgroup_taskset tset;
Tejun Heobfc2cf62017-01-15 19:03:41 -050070
71 /* subsystems affected by migration */
72 u16 ss_mask;
Tejun Heoe595cd72017-01-15 19:03:41 -050073};
74
75#define CGROUP_TASKSET_INIT(tset) \
76{ \
77 .src_csets = LIST_HEAD_INIT(tset.src_csets), \
78 .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
79 .csets = &tset.src_csets, \
80}
81
82#define CGROUP_MGCTX_INIT(name) \
83{ \
84 LIST_HEAD_INIT(name.preloaded_src_csets), \
85 LIST_HEAD_INIT(name.preloaded_dst_csets), \
86 CGROUP_TASKSET_INIT(name.tset), \
87}
88
89#define DEFINE_CGROUP_MGCTX(name) \
90 struct cgroup_mgctx name = CGROUP_MGCTX_INIT(name)
91
Tejun Heo1592c9b2016-12-27 14:49:08 -050092struct cgroup_sb_opts {
93 u16 subsys_mask;
94 unsigned int flags;
95 char *release_agent;
96 bool cpuset_clone_children;
97 char *name;
98 /* User explicitly requested empty subsystem */
99 bool none;
100};
101
Tejun Heo0a268db2016-12-27 14:49:06 -0500102extern struct mutex cgroup_mutex;
103extern spinlock_t css_set_lock;
104extern struct cgroup_subsys *cgroup_subsys[];
105extern struct list_head cgroup_roots;
106extern struct file_system_type cgroup_fs_type;
107
108/* iterate across the hierarchies */
109#define for_each_root(root) \
110 list_for_each_entry((root), &cgroup_roots, root_list)
111
112/**
113 * for_each_subsys - iterate all enabled cgroup subsystems
114 * @ss: the iteration cursor
115 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
116 */
117#define for_each_subsys(ss, ssid) \
118 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
119 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
120
121static inline bool cgroup_is_dead(const struct cgroup *cgrp)
122{
123 return !(cgrp->self.flags & CSS_ONLINE);
124}
125
126static inline bool notify_on_release(const struct cgroup *cgrp)
127{
128 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
129}
130
Tejun Heodcfe1492016-12-27 14:49:09 -0500131void put_css_set_locked(struct css_set *cset);
132
133static inline void put_css_set(struct css_set *cset)
134{
135 unsigned long flags;
136
137 /*
138 * Ensure that the refcount doesn't hit zero while any readers
139 * can see it. Similar to atomic_dec_and_lock(), but for an
140 * rwlock
141 */
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200142 if (refcount_dec_not_one(&cset->refcount))
Tejun Heodcfe1492016-12-27 14:49:09 -0500143 return;
144
145 spin_lock_irqsave(&css_set_lock, flags);
146 put_css_set_locked(cset);
147 spin_unlock_irqrestore(&css_set_lock, flags);
148}
149
150/*
151 * refcounted get/put for css_set objects
152 */
153static inline void get_css_set(struct css_set *cset)
154{
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200155 refcount_inc(&cset->refcount);
Tejun Heodcfe1492016-12-27 14:49:09 -0500156}
157
Tejun Heo0a268db2016-12-27 14:49:06 -0500158bool cgroup_ssid_enabled(int ssid);
159bool cgroup_on_dfl(const struct cgroup *cgrp);
Waiman Long7a0cf0e2017-07-21 11:14:51 -0400160bool cgroup_is_thread_root(struct cgroup *cgrp);
161bool cgroup_is_threaded(struct cgroup *cgrp);
Tejun Heo0a268db2016-12-27 14:49:06 -0500162
163struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root);
164struct cgroup *task_cgroup_from_root(struct task_struct *task,
165 struct cgroup_root *root);
166struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline);
167void cgroup_kn_unlock(struct kernfs_node *kn);
168int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
169 struct cgroup_namespace *ns);
170
Tejun Heo1592c9b2016-12-27 14:49:08 -0500171void cgroup_free_root(struct cgroup_root *root);
172void init_cgroup_root(struct cgroup_root *root, struct cgroup_sb_opts *opts);
Zefan Li9732adc2017-04-19 10:15:59 +0800173int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask, int ref_flags);
Tejun Heo0a268db2016-12-27 14:49:06 -0500174int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask);
Tejun Heo1592c9b2016-12-27 14:49:08 -0500175struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
176 struct cgroup_root *root, unsigned long magic,
177 struct cgroup_namespace *ns);
Tejun Heo0a268db2016-12-27 14:49:06 -0500178
Tejun Heo8cfd8142017-07-21 11:14:51 -0400179int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp);
Tejun Heoe595cd72017-01-15 19:03:41 -0500180void cgroup_migrate_finish(struct cgroup_mgctx *mgctx);
181void cgroup_migrate_add_src(struct css_set *src_cset, struct cgroup *dst_cgrp,
182 struct cgroup_mgctx *mgctx);
183int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx);
Tejun Heo0a268db2016-12-27 14:49:06 -0500184int cgroup_migrate(struct task_struct *leader, bool threadgroup,
Tejun Heobfc2cf62017-01-15 19:03:41 -0500185 struct cgroup_mgctx *mgctx);
Tejun Heo0a268db2016-12-27 14:49:06 -0500186
187int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
188 bool threadgroup);
Tejun Heo715c8092017-05-15 09:34:00 -0400189struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup)
190 __acquires(&cgroup_threadgroup_rwsem);
191void cgroup_procs_write_finish(struct task_struct *task)
192 __releases(&cgroup_threadgroup_rwsem);
Tejun Heo0a268db2016-12-27 14:49:06 -0500193
194void cgroup_lock_and_drain_offline(struct cgroup *cgrp);
195
Tejun Heo1592c9b2016-12-27 14:49:08 -0500196int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode);
197int cgroup_rmdir(struct kernfs_node *kn);
198int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
199 struct kernfs_root *kf_root);
200
Waiman Longa28f8f52017-06-13 17:18:02 -0400201int cgroup_task_count(const struct cgroup *cgrp);
202
Tejun Heo0a268db2016-12-27 14:49:06 -0500203/*
Tejun Heodcfe1492016-12-27 14:49:09 -0500204 * namespace.c
205 */
206extern const struct proc_ns_operations cgroupns_operations;
207
208/*
Tejun Heo0a268db2016-12-27 14:49:06 -0500209 * cgroup-v1.c
210 */
Tejun Heod62beb72016-12-27 14:49:08 -0500211extern struct cftype cgroup1_base_files[];
Tejun Heo0a268db2016-12-27 14:49:06 -0500212extern const struct file_operations proc_cgroupstats_operations;
Tejun Heo1592c9b2016-12-27 14:49:08 -0500213extern struct kernfs_syscall_ops cgroup1_kf_syscall_ops;
Tejun Heo0a268db2016-12-27 14:49:06 -0500214
Tejun Heod62beb72016-12-27 14:49:08 -0500215bool cgroup1_ssid_disabled(int ssid);
216void cgroup1_pidlist_destroy_all(struct cgroup *cgrp);
217void cgroup1_release_agent(struct work_struct *work);
218void cgroup1_check_for_release(struct cgroup *cgrp);
Tejun Heo1592c9b2016-12-27 14:49:08 -0500219struct dentry *cgroup1_mount(struct file_system_type *fs_type, int flags,
220 void *data, unsigned long magic,
221 struct cgroup_namespace *ns);
Tejun Heo0a268db2016-12-27 14:49:06 -0500222
223#endif /* __CGROUP_INTERNAL_H */