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#include <linux/idr.h>
19
20#ifdef CONFIG_CGROUPS
21
22struct cgroupfs_root;
23struct cgroup_subsys;
24struct inode;
25struct cgroup;
26struct css_id;
27
28extern int cgroup_init_early(void);
29extern int cgroup_init(void);
30extern void cgroup_lock(void);
31extern int cgroup_lock_is_held(void);
32extern bool cgroup_lock_live_group(struct cgroup *cgrp);
33extern void cgroup_unlock(void);
34extern void cgroup_fork(struct task_struct *p);
35extern void cgroup_fork_callbacks(struct task_struct *p);
36extern void cgroup_post_fork(struct task_struct *p);
37extern void cgroup_exit(struct task_struct *p, int run_callbacks);
38extern int cgroupstats_build(struct cgroupstats *stats,
39 struct dentry *dentry);
40extern int cgroup_load_subsys(struct cgroup_subsys *ss);
41extern void cgroup_unload_subsys(struct cgroup_subsys *ss);
42
43extern const struct file_operations proc_cgroup_operations;
44
45
46#define SUBSYS(_x) _x ## _subsys_id,
47enum cgroup_subsys_id {
48#include <linux/cgroup_subsys.h>
49 CGROUP_BUILTIN_SUBSYS_COUNT
50};
51#undef SUBSYS
52
53
54
55
56
57#define CGROUP_SUBSYS_COUNT (BITS_PER_BYTE*sizeof(unsigned long))
58
59
60struct cgroup_subsys_state {
61
62
63
64
65
66 struct cgroup *cgroup;
67
68
69
70
71
72
73
74 atomic_t refcnt;
75
76 unsigned long flags;
77
78 struct css_id __rcu *id;
79};
80
81
82enum {
83 CSS_ROOT,
84 CSS_REMOVED,
85};
86
87
88static inline void __css_get(struct cgroup_subsys_state *css, int count)
89{
90 atomic_add(count, &css->refcnt);
91}
92
93
94
95
96
97
98
99
100static inline void css_get(struct cgroup_subsys_state *css)
101{
102
103 if (!test_bit(CSS_ROOT, &css->flags))
104 __css_get(css, 1);
105}
106
107static inline bool css_is_removed(struct cgroup_subsys_state *css)
108{
109 return test_bit(CSS_REMOVED, &css->flags);
110}
111
112
113
114
115
116
117
118static inline bool css_tryget(struct cgroup_subsys_state *css)
119{
120 if (test_bit(CSS_ROOT, &css->flags))
121 return true;
122 while (!atomic_inc_not_zero(&css->refcnt)) {
123 if (test_bit(CSS_REMOVED, &css->flags))
124 return false;
125 cpu_relax();
126 }
127 return true;
128}
129
130
131
132
133
134
135extern void __css_put(struct cgroup_subsys_state *css, int count);
136static inline void css_put(struct cgroup_subsys_state *css)
137{
138 if (!test_bit(CSS_ROOT, &css->flags))
139 __css_put(css, 1);
140}
141
142
143enum {
144
145 CGRP_REMOVED,
146
147
148
149
150 CGRP_RELEASABLE,
151
152 CGRP_NOTIFY_ON_RELEASE,
153
154
155
156 CGRP_WAIT_ON_RMDIR,
157
158
159
160 CGRP_CLONE_CHILDREN,
161};
162
163
164enum cgroup_filetype {
165 CGROUP_FILE_PROCS,
166 CGROUP_FILE_TASKS,
167};
168
169
170
171
172
173
174
175struct cgroup_pidlist {
176
177
178
179
180 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
181
182 pid_t *list;
183
184 int length;
185
186 int use_count;
187
188 struct list_head links;
189
190 struct cgroup *owner;
191
192 struct rw_semaphore mutex;
193};
194
195struct cgroup {
196 unsigned long flags;
197
198
199
200
201
202 atomic_t count;
203
204
205
206
207
208 struct list_head sibling;
209 struct list_head children;
210
211 struct cgroup *parent;
212 struct dentry __rcu *dentry;
213
214
215 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
216
217 struct cgroupfs_root *root;
218 struct cgroup *top_cgroup;
219
220
221
222
223
224 struct list_head css_sets;
225
226
227
228
229
230
231 struct list_head release_list;
232
233
234
235
236
237 struct list_head pidlists;
238 struct mutex pidlist_mutex;
239
240
241 struct rcu_head rcu_head;
242
243
244 struct list_head event_list;
245 spinlock_t event_list_lock;
246};
247
248
249
250
251
252
253
254
255
256struct css_set {
257
258
259 atomic_t refcount;
260
261
262
263
264
265 struct hlist_node hlist;
266
267
268
269
270
271 struct list_head tasks;
272
273
274
275
276
277
278 struct list_head cg_links;
279
280
281
282
283
284
285
286 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
287
288
289 struct rcu_head rcu_head;
290};
291
292
293
294
295
296
297struct cgroup_map_cb {
298 int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
299 void *state;
300};
301
302
303
304
305
306
307
308
309
310#define MAX_CFTYPE_NAME 64
311struct cftype {
312
313
314
315
316 char name[MAX_CFTYPE_NAME];
317 int private;
318
319
320
321
322 mode_t mode;
323
324
325
326
327
328 size_t max_write_len;
329
330 int (*open)(struct inode *inode, struct file *file);
331 ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
332 struct file *file,
333 char __user *buf, size_t nbytes, loff_t *ppos);
334
335
336
337
338 u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
339
340
341
342 s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
343
344
345
346
347
348
349 int (*read_map)(struct cgroup *cont, struct cftype *cft,
350 struct cgroup_map_cb *cb);
351
352
353
354
355 int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
356 struct seq_file *m);
357
358 ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
359 struct file *file,
360 const char __user *buf, size_t nbytes, loff_t *ppos);
361
362
363
364
365
366
367 int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
368
369
370
371 int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
372
373
374
375
376
377
378 int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
379 const char *buffer);
380
381
382
383
384
385
386 int (*trigger)(struct cgroup *cgrp, unsigned int event);
387
388 int (*release)(struct inode *inode, struct file *file);
389
390
391
392
393
394
395
396 int (*register_event)(struct cgroup *cgrp, struct cftype *cft,
397 struct eventfd_ctx *eventfd, const char *args);
398
399
400
401
402
403
404 void (*unregister_event)(struct cgroup *cgrp, struct cftype *cft,
405 struct eventfd_ctx *eventfd);
406};
407
408struct cgroup_scanner {
409 struct cgroup *cg;
410 int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
411 void (*process_task)(struct task_struct *p,
412 struct cgroup_scanner *scan);
413 struct ptr_heap *heap;
414 void *data;
415};
416
417
418
419
420
421int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
422 const struct cftype *cft);
423
424
425
426
427
428int cgroup_add_files(struct cgroup *cgrp,
429 struct cgroup_subsys *subsys,
430 const struct cftype cft[],
431 int count);
432
433int cgroup_is_removed(const struct cgroup *cgrp);
434
435int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
436
437int cgroup_task_count(const struct cgroup *cgrp);
438
439
440int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456void cgroup_exclude_rmdir(struct cgroup_subsys_state *css);
457void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css);
458
459
460
461
462
463
464struct cgroup_subsys {
465 struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
466 struct cgroup *cgrp);
467 int (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
468 void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
469 int (*can_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
470 struct task_struct *tsk, bool threadgroup);
471 void (*cancel_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
472 struct task_struct *tsk, bool threadgroup);
473 void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
474 struct cgroup *old_cgrp, struct task_struct *tsk,
475 bool threadgroup);
476 void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
477 void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
478 int (*populate)(struct cgroup_subsys *ss,
479 struct cgroup *cgrp);
480 void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
481 void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
482
483 int subsys_id;
484 int active;
485 int disabled;
486 int early_init;
487
488
489
490
491 bool use_id;
492#define MAX_CGROUP_TYPE_NAMELEN 32
493 const char *name;
494
495
496
497
498
499
500
501
502
503
504
505 struct mutex hierarchy_mutex;
506 struct lock_class_key subsys_key;
507
508
509
510
511
512 struct cgroupfs_root *root;
513 struct list_head sibling;
514
515 struct idr idr;
516 spinlock_t id_lock;
517
518
519 struct module *module;
520};
521
522#define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
523#include <linux/cgroup_subsys.h>
524#undef SUBSYS
525
526static inline struct cgroup_subsys_state *cgroup_subsys_state(
527 struct cgroup *cgrp, int subsys_id)
528{
529 return cgrp->subsys[subsys_id];
530}
531
532
533
534
535
536
537#define task_subsys_state_check(task, subsys_id, __c) \
538 rcu_dereference_check(task->cgroups->subsys[subsys_id], \
539 rcu_read_lock_held() || \
540 lockdep_is_held(&task->alloc_lock) || \
541 cgroup_lock_is_held() || (__c))
542
543static inline struct cgroup_subsys_state *
544task_subsys_state(struct task_struct *task, int subsys_id)
545{
546 return task_subsys_state_check(task, subsys_id, false);
547}
548
549static inline struct cgroup* task_cgroup(struct task_struct *task,
550 int subsys_id)
551{
552 return task_subsys_state(task, subsys_id)->cgroup;
553}
554
555int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss,
556 char *nodename);
557
558
559struct cgroup_iter {
560 struct list_head *cg_link;
561 struct list_head *task;
562};
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
580struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
581 struct cgroup_iter *it);
582void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
583int cgroup_scan_tasks(struct cgroup_scanner *scan);
584int cgroup_attach_task(struct cgroup *, struct task_struct *);
585int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
586
587static inline int cgroup_attach_task_current_cg(struct task_struct *tsk)
588{
589 return cgroup_attach_task_all(current, tsk);
590}
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
610
611
612
613struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
614
615
616
617
618
619struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
620 struct cgroup_subsys_state *root, int *foundid);
621
622
623bool css_is_ancestor(struct cgroup_subsys_state *cg,
624 const struct cgroup_subsys_state *root);
625
626
627unsigned short css_id(struct cgroup_subsys_state *css);
628unsigned short css_depth(struct cgroup_subsys_state *css);
629
630#else
631
632static inline int cgroup_init_early(void) { return 0; }
633static inline int cgroup_init(void) { return 0; }
634static inline void cgroup_fork(struct task_struct *p) {}
635static inline void cgroup_fork_callbacks(struct task_struct *p) {}
636static inline void cgroup_post_fork(struct task_struct *p) {}
637static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
638
639static inline void cgroup_lock(void) {}
640static inline void cgroup_unlock(void) {}
641static inline int cgroupstats_build(struct cgroupstats *stats,
642 struct dentry *dentry)
643{
644 return -EINVAL;
645}
646
647
648static inline int cgroup_attach_task_all(struct task_struct *from,
649 struct task_struct *t)
650{
651 return 0;
652}
653static inline int cgroup_attach_task_current_cg(struct task_struct *t)
654{
655 return 0;
656}
657
658#endif
659
660#endif
661