]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/cgroup.h
Task Control Groups: add procfs interface
[mirror_ubuntu-jammy-kernel.git] / include / linux / cgroup.h
CommitLineData
ddbcc7e8
PM
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
17#ifdef CONFIG_CGROUPS
18
19struct cgroupfs_root;
20struct cgroup_subsys;
21struct inode;
22
23extern int cgroup_init_early(void);
24extern int cgroup_init(void);
25extern void cgroup_init_smp(void);
26extern void cgroup_lock(void);
27extern void cgroup_unlock(void);
b4f48b63
PM
28extern void cgroup_fork(struct task_struct *p);
29extern void cgroup_fork_callbacks(struct task_struct *p);
30extern void cgroup_exit(struct task_struct *p, int run_callbacks);
ddbcc7e8 31
a424316c
PM
32extern struct file_operations proc_cgroup_operations;
33
ddbcc7e8
PM
34/* Per-subsystem/per-cgroup state maintained by the system. */
35struct cgroup_subsys_state {
36 /* The cgroup that this subsystem is attached to. Useful
37 * for subsystems that want to know about the cgroup
38 * hierarchy structure */
39 struct cgroup *cgroup;
40
41 /* State maintained by the cgroup system to allow
42 * subsystems to be "busy". Should be accessed via css_get()
43 * and css_put() */
44
45 atomic_t refcnt;
46
47 unsigned long flags;
48};
49
50/* bits in struct cgroup_subsys_state flags field */
51enum {
52 CSS_ROOT, /* This CSS is the root of the subsystem */
53};
54
55/*
56 * Call css_get() to hold a reference on the cgroup;
57 *
58 */
59
60static inline void css_get(struct cgroup_subsys_state *css)
61{
62 /* We don't need to reference count the root state */
63 if (!test_bit(CSS_ROOT, &css->flags))
64 atomic_inc(&css->refcnt);
65}
66/*
67 * css_put() should be called to release a reference taken by
68 * css_get()
69 */
70
71static inline void css_put(struct cgroup_subsys_state *css)
72{
73 if (!test_bit(CSS_ROOT, &css->flags))
74 atomic_dec(&css->refcnt);
75}
76
77struct cgroup {
78 unsigned long flags; /* "unsigned long" so bitops work */
79
80 /* count users of this cgroup. >0 means busy, but doesn't
81 * necessarily indicate the number of tasks in the
82 * cgroup */
83 atomic_t count;
84
85 /*
86 * We link our 'sibling' struct into our parent's 'children'.
87 * Our children link their 'sibling' into our 'children'.
88 */
89 struct list_head sibling; /* my parent's children */
90 struct list_head children; /* my children */
91
92 struct cgroup *parent; /* my parent */
93 struct dentry *dentry; /* cgroup fs entry */
94
95 /* Private pointers for each registered subsystem */
96 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
97
98 struct cgroupfs_root *root;
99 struct cgroup *top_cgroup;
100};
101
102/* struct cftype:
103 *
104 * The files in the cgroup filesystem mostly have a very simple read/write
105 * handling, some common function will take care of it. Nevertheless some cases
106 * (read tasks) are special and therefore I define this structure for every
107 * kind of file.
108 *
109 *
110 * When reading/writing to a file:
111 * - the cgroup to use in file->f_dentry->d_parent->d_fsdata
112 * - the 'cftype' of the file is file->f_dentry->d_fsdata
113 */
114
115#define MAX_CFTYPE_NAME 64
116struct cftype {
117 /* By convention, the name should begin with the name of the
118 * subsystem, followed by a period */
119 char name[MAX_CFTYPE_NAME];
120 int private;
121 int (*open) (struct inode *inode, struct file *file);
122 ssize_t (*read) (struct cgroup *cont, struct cftype *cft,
123 struct file *file,
124 char __user *buf, size_t nbytes, loff_t *ppos);
125 /*
126 * read_uint() is a shortcut for the common case of returning a
127 * single integer. Use it in place of read()
128 */
129 u64 (*read_uint) (struct cgroup *cont, struct cftype *cft);
130 ssize_t (*write) (struct cgroup *cont, struct cftype *cft,
131 struct file *file,
132 const char __user *buf, size_t nbytes, loff_t *ppos);
355e0c48
PM
133
134 /*
135 * write_uint() is a shortcut for the common case of accepting
136 * a single integer (as parsed by simple_strtoull) from
137 * userspace. Use in place of write(); return 0 or error.
138 */
139 int (*write_uint) (struct cgroup *cont, struct cftype *cft, u64 val);
140
ddbcc7e8
PM
141 int (*release) (struct inode *inode, struct file *file);
142};
143
144/* Add a new file to the given cgroup directory. Should only be
145 * called by subsystems from within a populate() method */
146int cgroup_add_file(struct cgroup *cont, struct cgroup_subsys *subsys,
147 const struct cftype *cft);
148
149/* Add a set of new files to the given cgroup directory. Should
150 * only be called by subsystems from within a populate() method */
151int cgroup_add_files(struct cgroup *cont,
152 struct cgroup_subsys *subsys,
153 const struct cftype cft[],
154 int count);
155
156int cgroup_is_removed(const struct cgroup *cont);
157
158int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
159
bbcb81d0
PM
160int __cgroup_task_count(const struct cgroup *cont);
161static inline int cgroup_task_count(const struct cgroup *cont)
162{
163 int task_count;
164 rcu_read_lock();
165 task_count = __cgroup_task_count(cont);
166 rcu_read_unlock();
167 return task_count;
168}
169
ddbcc7e8
PM
170/* Return true if the cgroup is a descendant of the current cgroup */
171int cgroup_is_descendant(const struct cgroup *cont);
172
173/* Control Group subsystem type. See Documentation/cgroups.txt for details */
174
175struct cgroup_subsys {
176 struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
177 struct cgroup *cont);
178 void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cont);
179 int (*can_attach)(struct cgroup_subsys *ss,
180 struct cgroup *cont, struct task_struct *tsk);
181 void (*attach)(struct cgroup_subsys *ss, struct cgroup *cont,
182 struct cgroup *old_cont, struct task_struct *tsk);
183 void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
184 void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
185 int (*populate)(struct cgroup_subsys *ss,
186 struct cgroup *cont);
697f4161 187 void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cont);
ddbcc7e8
PM
188 void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
189 int subsys_id;
190 int active;
191 int early_init;
192#define MAX_CGROUP_TYPE_NAMELEN 32
193 const char *name;
194
195 /* Protected by RCU */
196 struct cgroupfs_root *root;
197
198 struct list_head sibling;
199
200 void *private;
201};
202
203#define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
204#include <linux/cgroup_subsys.h>
205#undef SUBSYS
206
207static inline struct cgroup_subsys_state *cgroup_subsys_state(
208 struct cgroup *cont, int subsys_id)
209{
210 return cont->subsys[subsys_id];
211}
212
213static inline struct cgroup_subsys_state *task_subsys_state(
214 struct task_struct *task, int subsys_id)
215{
216 return rcu_dereference(task->cgroups.subsys[subsys_id]);
217}
218
219static inline struct cgroup* task_cgroup(struct task_struct *task,
220 int subsys_id)
221{
222 return task_subsys_state(task, subsys_id)->cgroup;
223}
224
225int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
226
697f4161
PM
227int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss);
228
ddbcc7e8
PM
229#else /* !CONFIG_CGROUPS */
230
231static inline int cgroup_init_early(void) { return 0; }
232static inline int cgroup_init(void) { return 0; }
233static inline void cgroup_init_smp(void) {}
b4f48b63
PM
234static inline void cgroup_fork(struct task_struct *p) {}
235static inline void cgroup_fork_callbacks(struct task_struct *p) {}
236static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
ddbcc7e8
PM
237
238static inline void cgroup_lock(void) {}
239static inline void cgroup_unlock(void) {}
240
241#endif /* !CONFIG_CGROUPS */
242
243#endif /* _LINUX_CGROUP_H */