xref: /linux-6.15/include/linux/cgroup.h (revision 4dbed85a)
1 #ifndef _LINUX_CGROUP_H
2 #define _LINUX_CGROUP_H
3 /*
4  *  cgroup interface
5  *
6  *  Copyright (C) 2003 BULL SA
7  *  Copyright (C) 2004-2006 Silicon Graphics, Inc.
8  *
9  */
10 
11 #include <linux/sched.h>
12 #include <linux/kref.h>
13 #include <linux/cpumask.h>
14 #include <linux/nodemask.h>
15 #include <linux/rcupdate.h>
16 #include <linux/cgroupstats.h>
17 
18 #ifdef CONFIG_CGROUPS
19 
20 struct cgroupfs_root;
21 struct cgroup_subsys;
22 struct inode;
23 
24 extern int cgroup_init_early(void);
25 extern int cgroup_init(void);
26 extern void cgroup_init_smp(void);
27 extern void cgroup_lock(void);
28 extern void cgroup_unlock(void);
29 extern void cgroup_fork(struct task_struct *p);
30 extern void cgroup_fork_callbacks(struct task_struct *p);
31 extern void cgroup_post_fork(struct task_struct *p);
32 extern void cgroup_exit(struct task_struct *p, int run_callbacks);
33 extern int cgroupstats_build(struct cgroupstats *stats,
34 				struct dentry *dentry);
35 
36 extern struct file_operations proc_cgroup_operations;
37 
38 /* Define the enumeration of all cgroup subsystems */
39 #define SUBSYS(_x) _x ## _subsys_id,
40 enum cgroup_subsys_id {
41 #include <linux/cgroup_subsys.h>
42 	CGROUP_SUBSYS_COUNT
43 };
44 #undef SUBSYS
45 
46 /* Per-subsystem/per-cgroup state maintained by the system. */
47 struct cgroup_subsys_state {
48 	/* The cgroup that this subsystem is attached to. Useful
49 	 * for subsystems that want to know about the cgroup
50 	 * hierarchy structure */
51 	struct cgroup *cgroup;
52 
53 	/* State maintained by the cgroup system to allow
54 	 * subsystems to be "busy". Should be accessed via css_get()
55 	 * and css_put() */
56 
57 	atomic_t refcnt;
58 
59 	unsigned long flags;
60 };
61 
62 /* bits in struct cgroup_subsys_state flags field */
63 enum {
64 	CSS_ROOT, /* This CSS is the root of the subsystem */
65 };
66 
67 /*
68  * Call css_get() to hold a reference on the cgroup;
69  *
70  */
71 
72 static inline void css_get(struct cgroup_subsys_state *css)
73 {
74 	/* We don't need to reference count the root state */
75 	if (!test_bit(CSS_ROOT, &css->flags))
76 		atomic_inc(&css->refcnt);
77 }
78 /*
79  * css_put() should be called to release a reference taken by
80  * css_get()
81  */
82 
83 extern void __css_put(struct cgroup_subsys_state *css);
84 static inline void css_put(struct cgroup_subsys_state *css)
85 {
86 	if (!test_bit(CSS_ROOT, &css->flags))
87 		__css_put(css);
88 }
89 
90 struct cgroup {
91 	unsigned long flags;		/* "unsigned long" so bitops work */
92 
93 	/* count users of this cgroup. >0 means busy, but doesn't
94 	 * necessarily indicate the number of tasks in the
95 	 * cgroup */
96 	atomic_t count;
97 
98 	/*
99 	 * We link our 'sibling' struct into our parent's 'children'.
100 	 * Our children link their 'sibling' into our 'children'.
101 	 */
102 	struct list_head sibling;	/* my parent's children */
103 	struct list_head children;	/* my children */
104 
105 	struct cgroup *parent;	/* my parent */
106 	struct dentry *dentry;	  	/* cgroup fs entry */
107 
108 	/* Private pointers for each registered subsystem */
109 	struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
110 
111 	struct cgroupfs_root *root;
112 	struct cgroup *top_cgroup;
113 
114 	/*
115 	 * List of cg_cgroup_links pointing at css_sets with
116 	 * tasks in this cgroup. Protected by css_set_lock
117 	 */
118 	struct list_head css_sets;
119 
120 	/*
121 	 * Linked list running through all cgroups that can
122 	 * potentially be reaped by the release agent. Protected by
123 	 * release_list_lock
124 	 */
125 	struct list_head release_list;
126 };
127 
128 /* A css_set is a structure holding pointers to a set of
129  * cgroup_subsys_state objects. This saves space in the task struct
130  * object and speeds up fork()/exit(), since a single inc/dec and a
131  * list_add()/del() can bump the reference count on the entire
132  * cgroup set for a task.
133  */
134 
135 struct css_set {
136 
137 	/* Reference count */
138 	struct kref ref;
139 
140 	/*
141 	 * List running through all cgroup groups. Protected by
142 	 * css_set_lock
143 	 */
144 	struct list_head list;
145 
146 	/*
147 	 * List running through all tasks using this cgroup
148 	 * group. Protected by css_set_lock
149 	 */
150 	struct list_head tasks;
151 
152 	/*
153 	 * List of cg_cgroup_link objects on link chains from
154 	 * cgroups referenced from this css_set. Protected by
155 	 * css_set_lock
156 	 */
157 	struct list_head cg_links;
158 
159 	/*
160 	 * Set of subsystem states, one for each subsystem. This array
161 	 * is immutable after creation apart from the init_css_set
162 	 * during subsystem registration (at boot time).
163 	 */
164 	struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
165 
166 };
167 
168 /* struct cftype:
169  *
170  * The files in the cgroup filesystem mostly have a very simple read/write
171  * handling, some common function will take care of it. Nevertheless some cases
172  * (read tasks) are special and therefore I define this structure for every
173  * kind of file.
174  *
175  *
176  * When reading/writing to a file:
177  *	- the cgroup to use in file->f_dentry->d_parent->d_fsdata
178  *	- the 'cftype' of the file is file->f_dentry->d_fsdata
179  */
180 
181 #define MAX_CFTYPE_NAME 64
182 struct cftype {
183 	/* By convention, the name should begin with the name of the
184 	 * subsystem, followed by a period */
185 	char name[MAX_CFTYPE_NAME];
186 	int private;
187 	int (*open) (struct inode *inode, struct file *file);
188 	ssize_t (*read) (struct cgroup *cont, struct cftype *cft,
189 			 struct file *file,
190 			 char __user *buf, size_t nbytes, loff_t *ppos);
191 	/*
192 	 * read_uint() is a shortcut for the common case of returning a
193 	 * single integer. Use it in place of read()
194 	 */
195 	u64 (*read_uint) (struct cgroup *cont, struct cftype *cft);
196 	ssize_t (*write) (struct cgroup *cont, struct cftype *cft,
197 			  struct file *file,
198 			  const char __user *buf, size_t nbytes, loff_t *ppos);
199 
200 	/*
201 	 * write_uint() is a shortcut for the common case of accepting
202 	 * a single integer (as parsed by simple_strtoull) from
203 	 * userspace. Use in place of write(); return 0 or error.
204 	 */
205 	int (*write_uint) (struct cgroup *cont, struct cftype *cft, u64 val);
206 
207 	int (*release) (struct inode *inode, struct file *file);
208 };
209 
210 /* Add a new file to the given cgroup directory. Should only be
211  * called by subsystems from within a populate() method */
212 int cgroup_add_file(struct cgroup *cont, struct cgroup_subsys *subsys,
213 		       const struct cftype *cft);
214 
215 /* Add a set of new files to the given cgroup directory. Should
216  * only be called by subsystems from within a populate() method */
217 int cgroup_add_files(struct cgroup *cont,
218 			struct cgroup_subsys *subsys,
219 			const struct cftype cft[],
220 			int count);
221 
222 int cgroup_is_removed(const struct cgroup *cont);
223 
224 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
225 
226 int cgroup_task_count(const struct cgroup *cont);
227 
228 /* Return true if the cgroup is a descendant of the current cgroup */
229 int cgroup_is_descendant(const struct cgroup *cont);
230 
231 /* Control Group subsystem type. See Documentation/cgroups.txt for details */
232 
233 struct cgroup_subsys {
234 	struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
235 						  struct cgroup *cont);
236 	void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cont);
237 	int (*can_attach)(struct cgroup_subsys *ss,
238 			  struct cgroup *cont, struct task_struct *tsk);
239 	void (*attach)(struct cgroup_subsys *ss, struct cgroup *cont,
240 			struct cgroup *old_cont, struct task_struct *tsk);
241 	void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
242 	void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
243 	int (*populate)(struct cgroup_subsys *ss,
244 			struct cgroup *cont);
245 	void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cont);
246 	void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
247 	int subsys_id;
248 	int active;
249 	int early_init;
250 #define MAX_CGROUP_TYPE_NAMELEN 32
251 	const char *name;
252 
253 	/* Protected by RCU */
254 	struct cgroupfs_root *root;
255 
256 	struct list_head sibling;
257 
258 	void *private;
259 };
260 
261 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
262 #include <linux/cgroup_subsys.h>
263 #undef SUBSYS
264 
265 static inline struct cgroup_subsys_state *cgroup_subsys_state(
266 	struct cgroup *cont, int subsys_id)
267 {
268 	return cont->subsys[subsys_id];
269 }
270 
271 static inline struct cgroup_subsys_state *task_subsys_state(
272 	struct task_struct *task, int subsys_id)
273 {
274 	return rcu_dereference(task->cgroups->subsys[subsys_id]);
275 }
276 
277 static inline struct cgroup* task_cgroup(struct task_struct *task,
278 					       int subsys_id)
279 {
280 	return task_subsys_state(task, subsys_id)->cgroup;
281 }
282 
283 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
284 
285 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss);
286 
287 /* A cgroup_iter should be treated as an opaque object */
288 struct cgroup_iter {
289 	struct list_head *cg_link;
290 	struct list_head *task;
291 };
292 
293 /* To iterate across the tasks in a cgroup:
294  *
295  * 1) call cgroup_iter_start to intialize an iterator
296  *
297  * 2) call cgroup_iter_next() to retrieve member tasks until it
298  *    returns NULL or until you want to end the iteration
299  *
300  * 3) call cgroup_iter_end() to destroy the iterator.
301  */
302 void cgroup_iter_start(struct cgroup *cont, struct cgroup_iter *it);
303 struct task_struct *cgroup_iter_next(struct cgroup *cont,
304 					struct cgroup_iter *it);
305 void cgroup_iter_end(struct cgroup *cont, struct cgroup_iter *it);
306 
307 #else /* !CONFIG_CGROUPS */
308 
309 static inline int cgroup_init_early(void) { return 0; }
310 static inline int cgroup_init(void) { return 0; }
311 static inline void cgroup_init_smp(void) {}
312 static inline void cgroup_fork(struct task_struct *p) {}
313 static inline void cgroup_fork_callbacks(struct task_struct *p) {}
314 static inline void cgroup_post_fork(struct task_struct *p) {}
315 static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
316 
317 static inline void cgroup_lock(void) {}
318 static inline void cgroup_unlock(void) {}
319 static inline int cgroupstats_build(struct cgroupstats *stats,
320 					struct dentry *dentry)
321 {
322 	return -EINVAL;
323 }
324 
325 #endif /* !CONFIG_CGROUPS */
326 
327 #endif /* _LINUX_CGROUP_H */
328