cgroup.h 9.9 KB

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