cgroup.h 9.4 KB

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