cgroup.h 9.1 KB

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