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