1#ifndef _LINUX_CGROUP_H
2#define _LINUX_CGROUP_H
3
4
5
6
7
8
9
10
11#include <linux/sched.h>
12#include <linux/cpumask.h>
13#include <linux/nodemask.h>
14#include <linux/rcupdate.h>
15#include <linux/cgroupstats.h>
16#include <linux/prio_heap.h>
17#include <linux/rwsem.h>
18
19#ifdef CONFIG_CGROUPS
20
21struct cgroupfs_root;
22struct cgroup_subsys;
23struct inode;
24struct cgroup;
25
26extern int cgroup_init_early(void);
27extern int cgroup_init(void);
28extern void cgroup_lock(void);
29extern bool cgroup_lock_live_group(struct cgroup *cgrp);
30extern void cgroup_unlock(void);
31extern void cgroup_fork(struct task_struct *p);
32extern void cgroup_fork_callbacks(struct task_struct *p);
33extern void cgroup_post_fork(struct task_struct *p);
34extern void cgroup_exit(struct task_struct *p, int run_callbacks);
35extern int cgroupstats_build(struct cgroupstats *stats,
36 struct dentry *dentry);
37
38extern struct file_operations proc_cgroup_operations;
39
40
41#define SUBSYS(_x) _x ## _subsys_id,
42enum cgroup_subsys_id {
43#include <linux/cgroup_subsys.h>
44 CGROUP_SUBSYS_COUNT
45};
46#undef SUBSYS
47
48
49struct cgroup_subsys_state {
50
51
52
53 struct cgroup *cgroup;
54
55
56
57
58
59 atomic_t refcnt;
60
61 unsigned long flags;
62};
63
64
65enum {
66 CSS_ROOT,
67 CSS_REMOVED,
68};
69
70
71
72
73
74
75
76
77static inline void css_get(struct cgroup_subsys_state *css)
78{
79
80 if (!test_bit(CSS_ROOT, &css->flags))
81 atomic_inc(&css->refcnt);
82}
83
84static inline bool css_is_removed(struct cgroup_subsys_state *css)
85{
86 return test_bit(CSS_REMOVED, &css->flags);
87}
88
89
90
91
92
93
94
95static inline bool css_tryget(struct cgroup_subsys_state *css)
96{
97 if (test_bit(CSS_ROOT, &css->flags))
98 return true;
99 while (!atomic_inc_not_zero(&css->refcnt)) {
100 if (test_bit(CSS_REMOVED, &css->flags))
101 return false;
102 cpu_relax();
103 }
104 return true;
105}
106
107
108
109
110
111
112extern void __css_put(struct cgroup_subsys_state *css);
113static inline void css_put(struct cgroup_subsys_state *css)
114{
115 if (!test_bit(CSS_ROOT, &css->flags))
116 __css_put(css);
117}
118
119
120enum {
121
122 CGRP_REMOVED,
123
124
125 CGRP_RELEASABLE,
126
127 CGRP_NOTIFY_ON_RELEASE,
128};
129
130struct cgroup {
131 unsigned long flags;
132
133
134
135
136 atomic_t count;
137
138
139
140
141
142 struct list_head sibling;
143 struct list_head children;
144
145 struct cgroup *parent;
146 struct dentry *dentry;
147
148
149 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
150
151 struct cgroupfs_root *root;
152 struct cgroup *top_cgroup;
153
154
155
156
157
158 struct list_head css_sets;
159
160
161
162
163
164
165 struct list_head release_list;
166
167
168 struct rw_semaphore pids_mutex;
169
170 pid_t *tasks_pids;
171
172 int pids_use_count;
173
174 int pids_length;
175
176
177 struct rcu_head rcu_head;
178};
179
180
181
182
183
184
185
186
187struct css_set {
188
189
190 atomic_t refcount;
191
192
193
194
195
196 struct hlist_node hlist;
197
198
199
200
201
202 struct list_head tasks;
203
204
205
206
207
208
209 struct list_head cg_links;
210
211
212
213
214
215
216 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
217};
218
219
220
221
222
223
224struct cgroup_map_cb {
225 int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
226 void *state;
227};
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242#define MAX_CFTYPE_NAME 64
243struct cftype {
244
245
246 char name[MAX_CFTYPE_NAME];
247 int private;
248
249
250
251
252
253 size_t max_write_len;
254
255 int (*open)(struct inode *inode, struct file *file);
256 ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
257 struct file *file,
258 char __user *buf, size_t nbytes, loff_t *ppos);
259
260
261
262
263 u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
264
265
266
267 s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
268
269
270
271
272
273
274 int (*read_map)(struct cgroup *cont, struct cftype *cft,
275 struct cgroup_map_cb *cb);
276
277
278
279
280 int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
281 struct seq_file *m);
282
283 ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
284 struct file *file,
285 const char __user *buf, size_t nbytes, loff_t *ppos);
286
287
288
289
290
291
292 int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
293
294
295
296 int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
297
298
299
300
301
302
303 int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
304 const char *buffer);
305
306
307
308
309
310
311 int (*trigger)(struct cgroup *cgrp, unsigned int event);
312
313 int (*release)(struct inode *inode, struct file *file);
314};
315
316struct cgroup_scanner {
317 struct cgroup *cg;
318 int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
319 void (*process_task)(struct task_struct *p,
320 struct cgroup_scanner *scan);
321 struct ptr_heap *heap;
322};
323
324
325
326int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
327 const struct cftype *cft);
328
329
330
331int cgroup_add_files(struct cgroup *cgrp,
332 struct cgroup_subsys *subsys,
333 const struct cftype cft[],
334 int count);
335
336int cgroup_is_removed(const struct cgroup *cgrp);
337
338int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
339
340int cgroup_task_count(const struct cgroup *cgrp);
341
342
343int cgroup_is_descendant(const struct cgroup *cgrp);
344
345
346
347struct cgroup_subsys {
348 struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
349 struct cgroup *cgrp);
350 void (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
351 void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
352 int (*can_attach)(struct cgroup_subsys *ss,
353 struct cgroup *cgrp, struct task_struct *tsk);
354 void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
355 struct cgroup *old_cgrp, struct task_struct *tsk);
356 void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
357 void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
358 int (*populate)(struct cgroup_subsys *ss,
359 struct cgroup *cgrp);
360 void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
361 void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
362
363 int subsys_id;
364 int active;
365 int disabled;
366 int early_init;
367#define MAX_CGROUP_TYPE_NAMELEN 32
368 const char *name;
369
370
371
372
373
374
375
376
377
378
379
380 struct mutex hierarchy_mutex;
381 struct lock_class_key subsys_key;
382
383
384
385
386
387 struct cgroupfs_root *root;
388 struct list_head sibling;
389};
390
391#define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
392#include <linux/cgroup_subsys.h>
393#undef SUBSYS
394
395static inline struct cgroup_subsys_state *cgroup_subsys_state(
396 struct cgroup *cgrp, int subsys_id)
397{
398 return cgrp->subsys[subsys_id];
399}
400
401static inline struct cgroup_subsys_state *task_subsys_state(
402 struct task_struct *task, int subsys_id)
403{
404 return rcu_dereference(task->cgroups->subsys[subsys_id]);
405}
406
407static inline struct cgroup* task_cgroup(struct task_struct *task,
408 int subsys_id)
409{
410 return task_subsys_state(task, subsys_id)->cgroup;
411}
412
413int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss,
414 char *nodename);
415
416
417struct cgroup_iter {
418 struct list_head *cg_link;
419 struct list_head *task;
420};
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
436struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
437 struct cgroup_iter *it);
438void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
439int cgroup_scan_tasks(struct cgroup_scanner *scan);
440int cgroup_attach_task(struct cgroup *, struct task_struct *);
441
442#else
443
444static inline int cgroup_init_early(void) { return 0; }
445static inline int cgroup_init(void) { return 0; }
446static inline void cgroup_fork(struct task_struct *p) {}
447static inline void cgroup_fork_callbacks(struct task_struct *p) {}
448static inline void cgroup_post_fork(struct task_struct *p) {}
449static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
450
451static inline void cgroup_lock(void) {}
452static inline void cgroup_unlock(void) {}
453static inline int cgroupstats_build(struct cgroupstats *stats,
454 struct dentry *dentry)
455{
456 return -EINVAL;
457}
458
459#endif
460
461#endif
462